Skip to content

Commit

Permalink
fix(dot): no error logged for init check (#2502)
Browse files Browse the repository at this point in the history
- Rename `NodeInitialized` to `IsNodeInitialised`
- Rename `nodeInitialised` to `isNodeInitialised`
- Do not log `IsNodeInitialised` error
  • Loading branch information
qdm12 committed May 3, 2022
1 parent 51c5983 commit 2971325
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) error

func setDotGlobalConfigName(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.GlobalConfig) error {
globalBasePath := utils.ExpandDir(cfg.BasePath)
initialised := dot.NodeInitialized(globalBasePath)
initialised := dot.IsNodeInitialised(globalBasePath)

// consider the --name flag as higher priority
if ctx.GlobalString(NameFlag.Name) != "" {
Expand Down
5 changes: 2 additions & 3 deletions cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func gossamerAction(ctx *cli.Context) error {
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)

if !dot.NodeInitialized(cfg.Global.BasePath) {
if !dot.IsNodeInitialised(cfg.Global.BasePath) {
// initialise node (initialise state database and load genesis data)
err = dot.InitNode(cfg)
if err != nil {
Expand Down Expand Up @@ -320,8 +320,7 @@ func initAction(ctx *cli.Context) error {
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)
// check if node has been initialised (expected false - no warning log)
if dot.NodeInitialized(cfg.Global.BasePath) {

if dot.IsNodeInitialised(cfg.Global.BasePath) {
// use --force value to force initialise the node
force := ctx.Bool(ForceFlag.Name)

Expand Down
28 changes: 14 additions & 14 deletions dot/mock_node_builder_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Node struct {
//go:generate mockgen -source=node.go -destination=mock_node_builder_test.go -package=$GOPACKAGE

type nodeBuilderIface interface {
nodeInitialised(string) error
isNodeInitialised(basepath string) error
initNode(config *Config) error
createStateService(config *Config) (*state.Service, error)
createNetworkService(cfg *Config, stateSrvc *state.Service, telemetryMailer telemetry.Client) (*network.Service,
Expand All @@ -76,19 +76,17 @@ var _ nodeBuilderIface = (*nodeBuilder)(nil)

type nodeBuilder struct{}

// NodeInitialized returns true if, within the configured data directory for the
// node, the state database has been created and the genesis data has been loaded
func NodeInitialized(basepath string) bool {
// IsNodeInitialised returns true if, within the configured data directory for the
// node, the state database has been created and the genesis data can been loaded
func IsNodeInitialised(basepath string) bool {
nodeInstance := nodeBuilder{}
err := nodeInstance.nodeInitialised(basepath)
if err != nil {
logger.Errorf("failed to initialise node from base path %s: %s", basepath, err)
return false
}
return true
err := nodeInstance.isNodeInitialised(basepath)
return err == nil
}

func (*nodeBuilder) nodeInitialised(basepath string) error {
// isNodeInitialised returns nil if the node is successfully initialised
// and an error otherwise.
func (*nodeBuilder) isNodeInitialised(basepath string) error {
// check if key registry exists
registry := filepath.Join(basepath, utils.DefaultDatabaseDir, "KEYREGISTRY")

Expand Down Expand Up @@ -235,7 +233,7 @@ func newNode(cfg *Config,
debug.SetGCPercent(prev)
}

if builder.nodeInitialised(cfg.Global.BasePath) != nil {
if builder.isNodeInitialised(cfg.Global.BasePath) != nil {
err := builder.initNode(cfg)
if err != nil {
return nil, fmt.Errorf("cannot initialise node: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func TestNodeInitializedIntegration(t *testing.T) {

cfg.Init.Genesis = genFile

result := NodeInitialized(cfg.Global.BasePath)
result := IsNodeInitialised(cfg.Global.BasePath)
require.False(t, result)

err := InitNode(cfg)
require.NoError(t, err)

result = NodeInitialized(cfg.Global.BasePath)
result = IsNodeInitialised(cfg.Global.BasePath)
require.True(t, result)
}

Expand Down
4 changes: 2 additions & 2 deletions dot/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestNewNode(t *testing.T) {
mockServiceRegistry.EXPECT().RegisterService(gomock.Any()).Times(8)

m := NewMocknodeBuilderIface(ctrl)
m.EXPECT().nodeInitialised(dotConfig.Global.BasePath).Return(nil)
m.EXPECT().isNodeInitialised(dotConfig.Global.BasePath).Return(nil)
m.EXPECT().createStateService(dotConfig).DoAndReturn(func(cfg *Config) (*state.Service, error) {
stateSrvc := state.NewService(config)
// create genesis from configuration file
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestNodeInitialized(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NodeInitialized(tt.basepath)
got := IsNodeInitialised(tt.basepath)
assert.Equal(t, tt.want, got)
})
}
Expand Down

0 comments on commit 2971325

Please sign in to comment.