-
Notifications
You must be signed in to change notification settings - Fork 180
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 #6561 from onflow/ramtin/evm-add-cmd-for-exporting…
…-evm-state [EVM] add utility command to export EVM state
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 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,109 @@ | ||
package evm_exporter | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-go/cmd/util/ledger/util" | ||
"github.com/onflow/flow-go/fvm/evm" | ||
"github.com/onflow/flow-go/fvm/evm/emulator/state" | ||
"github.com/onflow/flow-go/ledger" | ||
"github.com/onflow/flow-go/ledger/common/convert" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
var ( | ||
flagChain string | ||
flagExecutionStateDir string | ||
flagOutputDir string | ||
flagStateCommitment string | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "export-evm-state", | ||
Short: "exports evm state into a several binary files", | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
Cmd.Flags().StringVar(&flagChain, "chain", "", "Chain name") | ||
_ = Cmd.MarkFlagRequired("chain") | ||
|
||
Cmd.Flags().StringVar(&flagExecutionStateDir, "execution-state-dir", "", | ||
"Execution Node state dir (where WAL logs are written") | ||
_ = Cmd.MarkFlagRequired("execution-state-dir") | ||
|
||
Cmd.Flags().StringVar(&flagOutputDir, "output-dir", "", | ||
"Directory to write new Execution State to") | ||
_ = Cmd.MarkFlagRequired("output-dir") | ||
|
||
Cmd.Flags().StringVar(&flagStateCommitment, "state-commitment", "", | ||
"State commitment (hex-encoded, 64 characters)") | ||
} | ||
|
||
func run(*cobra.Command, []string) { | ||
log.Info().Msg("start exporting evm state") | ||
err := ExportEVMState(flagChain, flagExecutionStateDir, flagStateCommitment, flagOutputDir) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("cannot get export evm state") | ||
} | ||
} | ||
|
||
// ExportEVMState evm state | ||
func ExportEVMState( | ||
chainName string, | ||
ledgerPath string, | ||
targetState string, | ||
outputPath string) error { | ||
|
||
chainID := flow.ChainID(chainName) | ||
|
||
storageRoot := evm.StorageAccountAddress(chainID) | ||
rootOwner := string(storageRoot.Bytes()) | ||
|
||
payloads, err := util.ReadTrie(ledgerPath, util.ParseStateCommitment(targetState)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// filter payloads of evm storage | ||
filteredPayloads := make(map[flow.RegisterID]*ledger.Payload, 0) | ||
for _, payload := range payloads { | ||
registerID, _, err := convert.PayloadToRegister(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert payload to register: %w", err) | ||
} | ||
if registerID.Owner == rootOwner { | ||
filteredPayloads[registerID] = payload | ||
} | ||
} | ||
|
||
payloadsLedger := util.NewPayloadsLedger(filteredPayloads) | ||
|
||
exporter, err := state.NewExporter(payloadsLedger, storageRoot) | ||
if err != nil { | ||
return fmt.Errorf("failed to create exporter: %w", err) | ||
} | ||
|
||
if _, err := os.Stat(outputPath); os.IsNotExist(err) { | ||
err := os.Mkdir(outputPath, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("failed to create path: %w", err) | ||
} | ||
} | ||
|
||
fi, err := os.Create(outputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer fi.Close() | ||
|
||
err = exporter.Export(outputPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to export: %w", 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