From 176c0e98d6eba33d9dfc0383c34820c2e5f75575 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 24 Jun 2020 16:09:54 +0300 Subject: [PATCH] cli/server: dump genesis block state when restoring Neo 3 node does it too and it's very useful because now the genesis block also does some state changes that are important. --- cli/server/dump.go | 8 ++++---- cli/server/server.go | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cli/server/dump.go b/cli/server/dump.go index e6084a9ee..303c7e903 100644 --- a/cli/server/dump.go +++ b/cli/server/dump.go @@ -165,8 +165,8 @@ func readFile(path string) (*dump, error) { // File dump-block-$FILENO.json contains blocks from $FILENO-999, $FILENO // Example: file `BlockStorage_100000/dump-block-6000.json` contains blocks from 5001 to 6000. func getPath(prefix string, index uint32) (string, error) { - dirN := (index-1)/100000 + 1 - dir := fmt.Sprintf("BlockStorage_%d00000", dirN) + dirN := ((index + 99999) / 100000) * 100000 + dir := fmt.Sprintf("BlockStorage_%d", dirN) path := filepath.Join(prefix, dir) info, err := os.Stat(path) @@ -179,7 +179,7 @@ func getPath(prefix string, index uint32) (string, error) { return "", fmt.Errorf("file `%s` is not a directory", path) } - fileN := (index-1)/1000 + 1 - file := fmt.Sprintf("dump-block-%d000.json", fileN) + fileN := ((index + 999) / 1000) * 1000 + file := fmt.Sprintf("dump-block-%d.json", fileN) return filepath.Join(path, file), nil } diff --git a/cli/server/server.go b/cli/server/server.go index be46edd2d..63b494655 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -292,16 +292,19 @@ func restoreDB(ctx *cli.Context) error { genesis, err := chain.GetBlock(block.Hash()) if err == nil && genesis.Index == 0 { log.Info("skipped genesis block", zap.String("hash", block.Hash().StringLE())) - continue + } + } else { + err = chain.AddBlock(block) + if err != nil { + return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1) } } - err = chain.AddBlock(block) - if err != nil { - return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1) - } - if dumpDir != "" { batch := chain.LastBatch() + // The genesis block may already be persisted, so LastBatch() will return nil. + if batch == nil && block.Index == 0 { + continue + } dump.add(block.Index, batch) lastIndex = block.Index if block.Index%1000 == 0 {