diff --git a/cmd/main.go b/cmd/main.go index 68c22a0b..7f359f79 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "os" + "github.com/onflow/flow-evm-gateway/cmd/export-evm-state" "github.com/onflow/flow-evm-gateway/cmd/run" "github.com/onflow/flow-evm-gateway/cmd/version" "github.com/rs/zerolog/log" @@ -23,6 +24,7 @@ func Execute() { func main() { rootCmd.AddCommand(version.Cmd) + rootCmd.AddCommand(export.Cmd) rootCmd.AddCommand(run.Cmd) Execute() diff --git a/cmd/util/main.go b/cmd/util/main.go deleted file mode 100644 index a88022df..00000000 --- a/cmd/util/main.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import ( - "flag" - "os" - - "github.com/rs/zerolog/log" - - "github.com/onflow/flow-evm-gateway/storage/pebble" - "github.com/onflow/flow-go/fvm/evm" - "github.com/onflow/flow-go/fvm/evm/emulator/state" - "github.com/onflow/flow-go/fvm/evm/offchain/storage" - flowGo "github.com/onflow/flow-go/model/flow" -) - -func main() { - var ( - height uint64 - outputDir string - registerStoreDir string - ) - - flag.Uint64Var(&height, "evm-height", 0, "EVM Block height for EVM state export") - flag.StringVar(&outputDir, "output", "", "Output directory for exported EVM state") - flag.StringVar(®isterStoreDir, "register-store", "", "Directory of the register store") - - flag.Parse() - - if height == 0 || outputDir == "" || registerStoreDir == "" { - log.Error().Msg("All flags (height, output, register-store) must be provided") - flag.Usage() - os.Exit(1) - } - - chainID := flowGo.Testnet - - log.Info().Msgf("exporting EVM state for height %v from registerStoreDir %v, outputDir: %v", height, registerStoreDir, outputDir) - err := ExportEVMStateForHeight(height, outputDir, registerStoreDir, chainID) - if err != nil { - log.Fatal().Err(err).Msgf("fail to export") - } - - log.Info().Msgf("successfully exported EVM state to %v", outputDir) -} - -func ExportEVMStateForHeight(height uint64, outputDir string, registerStoreDir string, chainID flowGo.ChainID) error { - store, err := pebble.New(registerStoreDir, log.Logger) - if err != nil { - return err - } - - storageAddress := evm.StorageAccountAddress(chainID) - registerStore := pebble.NewRegisterStorage(store, storageAddress) - snapshot, err := registerStore.GetSnapshotAt(height) - if err != nil { - return err - } - - ledger := storage.NewReadOnlyStorage(snapshot) - exporter, err := state.NewExporter(ledger, storageAddress) - if err != nil { - return err - } - - err = exporter.ExportGob(outputDir) - if err != nil { - return err - } - - return nil -} diff --git a/services/evm/extract_test.go b/services/evm/extract_test.go index dc7e08de..f4bca319 100644 --- a/services/evm/extract_test.go +++ b/services/evm/extract_test.go @@ -30,8 +30,9 @@ func extractEVMState( t *testing.T, chainID flowGo.ChainID, registerStoreDir string, evmHeight uint64) *state.EVMState { - store, err := pebble.New(registerStoreDir, log.Logger) + pebbleDB, err := pebble.OpenDB(registerStoreDir) require.NoError(t, err) + store := pebble.New(pebbleDB, log.Logger) evmState, err := evmState.ExtractEVMState(chainID, evmHeight, store) require.NoError(t, err)