From a30f098f7381dd04cb470a34ed992dcef51113a1 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 29 Jun 2023 11:41:04 +0300 Subject: [PATCH 1/2] server: allow to create incremental dumps Anything not starting from 0 is incremental by definition. Signed-off-by: Roman Khimov --- cli/server/server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/server/server.go b/cli/server/server.go index 8dd586e9a..b55982a4e 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -198,6 +198,9 @@ func dumpDB(ctx *cli.Context) error { if count == 0 { count = chainCount - start } + if start != 0 { + writer.WriteU32LE(start) + } writer.WriteU32LE(count) err = chaindump.Dump(chain, writer, start, count) if err != nil { From bfe8867e635efc6df42f528f5ae784e3ff53d31b Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 3 Jul 2023 13:24:11 +0300 Subject: [PATCH 2/2] cli: add test for incremental DB dump and restore Signed-off-by: Anna Shaleva --- cli/server/cli_dump_test.go | 49 +++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/cli/server/cli_dump_test.go b/cli/server/cli_dump_test.go index b743f9015..f7756add5 100644 --- a/cli/server/cli_dump_test.go +++ b/cli/server/cli_dump_test.go @@ -13,6 +13,9 @@ import ( "gopkg.in/yaml.v3" ) +// generated via `go run ./scripts/gendump/main.go --out ./cli/server/testdata/chain50x2.acc --blocks 50 --txs 2`. +const inDump = "./testdata/chain50x2.acc" + func TestDBRestoreDump(t *testing.T) { tmpDir := t.TempDir() @@ -32,8 +35,6 @@ func TestDBRestoreDump(t *testing.T) { cfgPath := filepath.Join(tmpDir, "protocol.unit_testnet.yml") require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm)) - // generated via `go run ./scripts/gendump/main.go --out ./cli/server/testdata/chain50x2.acc --blocks 50 --txs 2` - const inDump = "./testdata/chain50x2.acc" e := testcli.NewExecutor(t, false) stateDump := filepath.Join(tmpDir, "neogo.teststate") @@ -111,3 +112,47 @@ func TestDBRestoreDump(t *testing.T) { require.NoError(t, err) require.Equal(t, d1, d2, "dumps differ") } + +func TestDBDumpRestoreIncremental(t *testing.T) { + tmpDir := t.TempDir() + chainPath := filepath.Join(tmpDir, "neogotestchain") + nonincDump := filepath.Join(tmpDir, "nonincDump.acc") + incDump := filepath.Join(tmpDir, "incDump.acc") + + cfg, err := config.LoadFile(filepath.Join("..", "..", "config", "protocol.unit_testnet.yml")) + require.NoError(t, err, "could not load config") + cfg.ApplicationConfiguration.DBConfiguration.Type = dbconfig.LevelDB + cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath = chainPath + out, err := yaml.Marshal(cfg) + require.NoError(t, err) + + cfgPath := filepath.Join(tmpDir, "protocol.unit_testnet.yml") + require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm)) + + e := testcli.NewExecutor(t, false) + + // Create DB from dump. + e.Run(t, "neo-go", "db", "restore", "--unittest", "--config-path", tmpDir, "--in", inDump) + + // Create two dumps: non-incremental and incremental. + dumpBaseArgs := []string{"neo-go", "db", "dump", "--unittest", + "--config-path", tmpDir} + + // Dump first 15 blocks to a non-incremental dump. + e.Run(t, append(dumpBaseArgs, "--out", nonincDump, "--count", "15")...) + + // Dump second 15 blocks to an incremental dump. + e.Run(t, append(dumpBaseArgs, "--out", incDump, "--start", "15", "--count", "15")...) + + // Clean the DB. + require.NoError(t, os.RemoveAll(chainPath)) + + // Restore chain from two dumps. + restoreBaseArgs := []string{"neo-go", "db", "restore", "--unittest", "--config-path", tmpDir} + + // Restore first 15 blocks from non-incremental dump. + e.Run(t, append(restoreBaseArgs, "--in", nonincDump)...) + + // Restore second 15 blocks from incremental dump. + e.Run(t, append(restoreBaseArgs, "--in", incDump, "-n", "--count", "15")...) +}