core: add Close() to blockchainer, implement it to properly close chain

Before it the deferred function in Run() was actually never able to properly
close the Store, so we weren't synching the latest state to the disk.
This commit is contained in:
Roman Khimov 2019-11-07 20:47:48 +03:00
parent d33083e1e1
commit b05754deac
8 changed files with 50 additions and 23 deletions

View file

@ -154,14 +154,11 @@ func dumpDB(ctx *cli.Context) error {
defer outStream.Close()
writer := io.NewBinWriterFromIO(outStream)
grace, cancel := context.WithCancel(newGraceContext())
defer cancel()
chain, err := initBlockChain(cfg)
if err != nil {
return cli.NewExitError(err, 1)
}
go chain.Run(grace)
go chain.Run()
chainHeight := chain.BlockHeight()
if skip+count > chainHeight {
@ -182,6 +179,7 @@ func dumpDB(ctx *cli.Context) error {
return cli.NewExitError(err, 1)
}
}
chain.Close()
return nil
}
func restoreDB(ctx *cli.Context) error {
@ -204,14 +202,11 @@ func restoreDB(ctx *cli.Context) error {
defer inStream.Close()
reader := io.NewBinReaderFromIO(inStream)
grace, cancel := context.WithCancel(newGraceContext())
defer cancel()
chain, err := initBlockChain(cfg)
if err != nil {
return err
}
go chain.Run(grace)
go chain.Run()
var allBlocks uint32
reader.ReadLE(&allBlocks)
@ -243,6 +238,7 @@ func restoreDB(ctx *cli.Context) error {
return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1)
}
}
chain.Close()
return nil
}
@ -272,7 +268,7 @@ func startServer(ctx *cli.Context) error {
errChan := make(chan error)
monitoring := metrics.NewMetricsService(cfg.ApplicationConfiguration.Monitoring)
go chain.Run(grace)
go chain.Run()
go server.Start(errChan)
go rpcServer.Start(errChan)
go monitoring.Start()
@ -295,6 +291,7 @@ Main:
shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server")
}
monitoring.ShutDown()
chain.Close()
break Main
}
}