From aceeeeca33bcf776fefdbcce81d242f55c4ef6fc Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 14 Nov 2022 10:22:52 +0300 Subject: [PATCH] cli: properly close storage on `db reset` And fix failing test along the way: ``` 2022-11-11T12:37:47.0413934Z === RUN TestResetDB 2022-11-11T12:37:47.0414557Z 2022-11-11T12:36:54.510Z INFO initial gas supply is not set or wrong, setting default value {"InitialGASSupply": "52000000"} 2022-11-11T12:37:47.0415288Z 2022-11-11T12:36:54.510Z INFO MaxBlockSize is not set or wrong, setting default value {"MaxBlockSize": 262144} 2022-11-11T12:37:47.0416020Z 2022-11-11T12:36:54.510Z INFO MaxBlockSystemFee is not set or wrong, setting default value {"MaxBlockSystemFee": 900000000000} 2022-11-11T12:37:47.0416786Z 2022-11-11T12:36:54.510Z INFO MaxTransactionsPerBlock is not set or wrong, using default value {"MaxTransactionsPerBlock": 512} 2022-11-11T12:37:47.0417725Z 2022-11-11T12:36:54.510Z INFO MaxValidUntilBlockIncrement is not set or wrong, using default value {"MaxValidUntilBlockIncrement": 5760} 2022-11-11T12:37:47.0418415Z 2022-11-11T12:36:54.510Z INFO Hardforks are not set, using default value 2022-11-11T12:37:47.0419272Z 2022-11-11T12:36:54.523Z INFO no storage version found! creating genesis block 2022-11-11T12:37:47.0419997Z 2022-11-11T12:36:54.529Z INFO chain is already at the proper state {"height": 0} 2022-11-11T12:37:47.0420974Z testing.go:1097: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestResetDB671187463\001\chains\privnet\000001.log: The process cannot access the file because it is being used by another process. 2022-11-11T12:37:47.0421606Z --- FAIL: TestResetDB (1.99s) ``` --- cli/server/server.go | 24 ++++++++++++++++++------ cli/server/server_test.go | 4 ++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index 5aad55f77..c5f44f1ae 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -131,7 +131,7 @@ func newGraceContext() context.Context { } func initBCWithMetrics(cfg config.Config, log *zap.Logger) (*core.Blockchain, *metrics.Service, *metrics.Service, error) { - chain, err := initBlockChain(cfg, log) + chain, _, err := initBlockChain(cfg, log) if err != nil { return nil, nil, nil, cli.NewExitError(err, 1) } @@ -333,7 +333,7 @@ func resetDB(ctx *cli.Context) error { if logCloser != nil { defer func() { _ = logCloser() }() } - chain, err := initBlockChain(cfg, log) + chain, store, err := initBlockChain(cfg, log) if err != nil { return cli.NewExitError(fmt.Errorf("failed to create Blockchain instance: %w", err), 1) } @@ -342,6 +342,10 @@ func resetDB(ctx *cli.Context) error { if err != nil { return cli.NewExitError(fmt.Errorf("failed to reset chain state to height %d: %w", h, err), 1) } + err = store.Close() + if err != nil { + return cli.NewExitError(fmt.Errorf("failed to close the DB: %w", err), 1) + } return nil } @@ -616,17 +620,25 @@ func configureAddresses(cfg *config.ApplicationConfiguration) { } // initBlockChain initializes BlockChain with preselected DB. -func initBlockChain(cfg config.Config, log *zap.Logger) (*core.Blockchain, error) { +func initBlockChain(cfg config.Config, log *zap.Logger) (*core.Blockchain, storage.Store, error) { store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration) if err != nil { - return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %w", err), 1) + return nil, nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %w", err), 1) } chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration, log) if err != nil { - return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %w", err), 1) + errText := "could not initialize blockchain: %w" + errArgs := []interface{}{err} + closeErr := store.Close() + if closeErr != nil { + errText += "; failed to close the DB: %w" + errArgs = append(errArgs, closeErr) + } + + return nil, nil, cli.NewExitError(fmt.Errorf(errText, errArgs), 1) } - return chain, nil + return chain, store, nil } // Logo returns Neo-Go logo. diff --git a/cli/server/server_test.go b/cli/server/server_test.go index ca5d3540a..074f4abfd 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -336,12 +336,12 @@ func TestConfigureAddresses(t *testing.T) { func TestInitBlockChain(t *testing.T) { t.Run("bad storage", func(t *testing.T) { - _, err := initBlockChain(config.Config{}, nil) + _, _, err := initBlockChain(config.Config{}, nil) require.Error(t, err) }) t.Run("empty logger", func(t *testing.T) { - _, err := initBlockChain(config.Config{ + _, _, err := initBlockChain(config.Config{ ApplicationConfiguration: config.ApplicationConfiguration{ DBConfiguration: dbconfig.DBConfiguration{ Type: dbconfig.InMemoryDB,