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)
```
This commit is contained in:
Anna Shaleva 2022-11-14 10:22:52 +03:00
parent b930a970ce
commit aceeeeca33
2 changed files with 20 additions and 8 deletions

View file

@ -131,7 +131,7 @@ func newGraceContext() context.Context {
} }
func initBCWithMetrics(cfg config.Config, log *zap.Logger) (*core.Blockchain, *metrics.Service, *metrics.Service, error) { 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 { if err != nil {
return nil, nil, nil, cli.NewExitError(err, 1) return nil, nil, nil, cli.NewExitError(err, 1)
} }
@ -333,7 +333,7 @@ func resetDB(ctx *cli.Context) error {
if logCloser != nil { if logCloser != nil {
defer func() { _ = logCloser() }() defer func() { _ = logCloser() }()
} }
chain, err := initBlockChain(cfg, log) chain, store, err := initBlockChain(cfg, log)
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create Blockchain instance: %w", err), 1) 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 { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to reset chain state to height %d: %w", h, err), 1) 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 return nil
} }
@ -616,17 +620,25 @@ func configureAddresses(cfg *config.ApplicationConfiguration) {
} }
// initBlockChain initializes BlockChain with preselected DB. // 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) store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)
if err != nil { 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) chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration, log)
if err != nil { 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 chain, nil
return nil, nil, cli.NewExitError(fmt.Errorf(errText, errArgs), 1)
}
return chain, store, nil
} }
// Logo returns Neo-Go logo. // Logo returns Neo-Go logo.

View file

@ -336,12 +336,12 @@ func TestConfigureAddresses(t *testing.T) {
func TestInitBlockChain(t *testing.T) { func TestInitBlockChain(t *testing.T) {
t.Run("bad storage", func(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) require.Error(t, err)
}) })
t.Run("empty logger", func(t *testing.T) { t.Run("empty logger", func(t *testing.T) {
_, err := initBlockChain(config.Config{ _, _, err := initBlockChain(config.Config{
ApplicationConfiguration: config.ApplicationConfiguration{ ApplicationConfiguration: config.ApplicationConfiguration{
DBConfiguration: dbconfig.DBConfiguration{ DBConfiguration: dbconfig.DBConfiguration{
Type: dbconfig.InMemoryDB, Type: dbconfig.InMemoryDB,