-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from onflow/leo/extract-evm-state
Extract EVM state
- Loading branch information
Showing
8 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package export | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"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" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "export-evm-state", | ||
Short: "Export EVM state at a specific height", | ||
RunE: func(*cobra.Command, []string) error { | ||
if height == 0 || outputDir == "" || registerStoreDir == "" { | ||
return fmt.Errorf("all flags (height, output, register-store) must be provided") | ||
} | ||
|
||
log.Info().Msgf("exporting EVM state for height %v from registerStoreDir %v, outputDir: %v, chain: %v", height, registerStoreDir, outputDir, chain) | ||
|
||
chainID := flowGo.ChainID(chain) | ||
|
||
err := ExportEVMStateForHeight(height, outputDir, registerStoreDir, chainID) | ||
if err != nil { | ||
return fmt.Errorf("fail to export: %w", err) | ||
} | ||
|
||
log.Info().Msgf("successfully exported EVM state to %v", outputDir) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var ( | ||
height uint64 | ||
outputDir string | ||
chain string | ||
registerStoreDir string | ||
) | ||
|
||
func init() { | ||
Cmd.Flags().Uint64Var(&height, "evm-height", 0, "EVM Block height for EVM state export") | ||
Cmd.Flags().StringVar(&outputDir, "output", "", "Output directory for exported EVM state") | ||
Cmd.Flags().StringVar(&chain, "chain-id", "testnet", "Chain ID for the EVM state") | ||
Cmd.Flags().StringVar(®isterStoreDir, "register-store", "", "Directory of the register store") | ||
} | ||
|
||
func ExportEVMStateForHeight(height uint64, outputDir string, registerStoreDir string, chainID flowGo.ChainID) error { | ||
storageAddress := evm.StorageAccountAddress(chainID) | ||
|
||
pebbleDB, err := pebble.OpenDB(registerStoreDir) | ||
if err != nil { | ||
return fmt.Errorf("failed to open pebble db: %w", err) | ||
} | ||
|
||
store := pebble.New(pebbleDB, log.Logger) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package evm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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 ExtractEVMState( | ||
chainID flowGo.ChainID, | ||
evmHeight uint64, | ||
store *pebble.Storage, | ||
) (*state.EVMState, error) { | ||
storageRoot := evm.StorageAccountAddress(chainID) | ||
registerStore := pebble.NewRegisterStorage(store, storageRoot) | ||
snapshot, err := registerStore.GetSnapshotAt(evmHeight) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get snapshot at evm height %d: %w", evmHeight, err) | ||
} | ||
|
||
ledger := storage.NewReadOnlyStorage(snapshot) | ||
bv, err := state.NewBaseView(ledger, storageRoot) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create base view: %w", err) | ||
} | ||
|
||
evmState, err := state.Extract(storageRoot, bv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return evmState, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package evm_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/onflow/flow-evm-gateway/storage/pebble" | ||
"github.com/onflow/flow-go/fvm/evm/emulator/state" | ||
flowGo "github.com/onflow/flow-go/model/flow" | ||
"github.com/rs/zerolog/log" | ||
"github.com/stretchr/testify/require" | ||
|
||
evmState "github.com/onflow/flow-evm-gateway/services/evm" | ||
) | ||
|
||
func StateDiff(t *testing.T) { | ||
state1 := extractEVMState(t, flowGo.Testnet, "/var/flow52/evm/data/db", uint64(17724990)) | ||
state2 := evmStateFromCheckpointExtract(t, "/var/flow52/evm-state-from-checkpoint-228901661") | ||
|
||
differences := state.Diff(state1, state2) | ||
|
||
for i, diff := range differences { | ||
fmt.Printf("Difference %d: %v\n", i, diff) | ||
} | ||
|
||
require.Len(t, differences, 0) | ||
} | ||
|
||
func extractEVMState( | ||
t *testing.T, chainID flowGo.ChainID, | ||
registerStoreDir string, evmHeight uint64) *state.EVMState { | ||
|
||
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) | ||
return evmState | ||
} | ||
|
||
func evmStateFromCheckpointExtract(t *testing.T, dir string) *state.EVMState { | ||
enState, err := state.ImportEVMStateFromGob(dir) | ||
require.NoError(t, err) | ||
return enState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters