From 02bea52f407b7f83043837c323c1f39a0616d450 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 24 Oct 2022 14:36:24 +0300 Subject: [PATCH] [#1959] neofs-adm: Allow to test all commands with a local dump Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/initialize.go | 5 +--- .../internal/modules/morph/initialize_test.go | 1 + .../internal/modules/morph/local_client.go | 26 +++++++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go index 1631af94f..147dddec6 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -126,13 +126,10 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex var c Client if v.GetString(localDumpFlag) != "" { - if cmd.Name() != "init" { - return nil, errors.New("dump creation is only supported for `init` command") - } if v.GetString(endpointFlag) != "" { return nil, fmt.Errorf("`%s` and `%s` flags are mutually exclusive", endpointFlag, localDumpFlag) } - c, err = newLocalClient(v, wallets) + c, err = newLocalClient(cmd, v, wallets) } else { c, err = getN3Client(v) } diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_test.go b/cmd/neofs-adm/internal/modules/morph/initialize_test.go index 1b6e17fc5..925a792d4 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_test.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_test.go @@ -70,4 +70,5 @@ func TestInitialize(t *testing.T) { v.Set(epochDurationInitFlag, 1) v.Set(maxObjectSizeInitFlag, 1024) require.NoError(t, initializeSideChainCmd(initCmd, nil)) + require.NoError(t, forceNewEpochCmd(forceNewEpoch, nil)) } diff --git a/cmd/neofs-adm/internal/modules/morph/local_client.go b/cmd/neofs-adm/internal/modules/morph/local_client.go index 1273192d4..b994b281d 100644 --- a/cmd/neofs-adm/internal/modules/morph/local_client.go +++ b/cmd/neofs-adm/internal/modules/morph/local_client.go @@ -38,6 +38,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" ) @@ -50,7 +51,7 @@ type localClient struct { maxGasInvoke int64 } -func newLocalClient(v *viper.Viper, wallets []*wallet.Wallet) (*localClient, error) { +func newLocalClient(cmd *cobra.Command, v *viper.Viper, wallets []*wallet.Wallet) (*localClient, error) { cfg, err := config.LoadFile(v.GetString(protoConfigPath)) if err != nil { return nil, err @@ -88,9 +89,30 @@ func newLocalClient(v *viper.Viper, wallets []*wallet.Wallet) (*localClient, err go bc.Run() + dumpPath := v.GetString(localDumpFlag) + if cmd.Name() != "init" { + f, err := os.OpenFile(dumpPath, os.O_RDONLY, 0600) + if err != nil { + return nil, fmt.Errorf("can't open local dump: %w", err) + } + defer f.Close() + + r := io.NewBinReaderFromIO(f) + + var skip uint32 + if bc.BlockHeight() != 0 { + skip = bc.BlockHeight() + 1 + } + + count := r.ReadU32LE() - skip + if err := chaindump.Restore(bc, r, skip, count, nil); err != nil { + return nil, fmt.Errorf("can't restore local dump: %w", err) + } + } + return &localClient{ bc: bc, - dumpPath: v.GetString(localDumpFlag), + dumpPath: dumpPath, accounts: accounts[:m], maxGasInvoke: 15_0000_0000, }, nil