From 78a0c62ba66443650dac14d663225314532c1795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 29 Feb 2024 16:34:25 -0800 Subject: [PATCH] add util command to generate payloads for bootsrapped execution state --- .../bootstrap-execution-state-payloads/cmd.go | 90 +++++++++++++++++++ cmd/util/cmd/root.go | 2 + 2 files changed, 92 insertions(+) create mode 100644 cmd/util/cmd/bootstrap-execution-state-payloads/cmd.go diff --git a/cmd/util/cmd/bootstrap-execution-state-payloads/cmd.go b/cmd/util/cmd/bootstrap-execution-state-payloads/cmd.go new file mode 100644 index 00000000000..94a317326ac --- /dev/null +++ b/cmd/util/cmd/bootstrap-execution-state-payloads/cmd.go @@ -0,0 +1,90 @@ +package addresses + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/onflow/flow-go/cmd/util/ledger/util" + "github.com/onflow/flow-go/fvm" + "github.com/onflow/flow-go/fvm/storage/snapshot" + "github.com/onflow/flow-go/ledger" + "github.com/onflow/flow-go/ledger/common/convert" + "github.com/onflow/flow-go/model/flow" + "github.com/onflow/flow-go/utils/unittest" +) + +var ( + flagChain string + flagOutputPayloadFileName string +) + +var Cmd = &cobra.Command{ + Use: "bootstrap-execution-state-payloads", + Short: "generate payloads for execution state of bootstrapped chain", + Run: run, +} + +func init() { + Cmd.Flags().StringVar(&flagChain, "chain", "", "Chain name") + _ = Cmd.MarkFlagRequired("chain") + + Cmd.Flags().StringVar( + &flagOutputPayloadFileName, + "output-filename", + "", + "Output payload file name") + _ = Cmd.MarkFlagRequired("output-filename") + +} + +func run(*cobra.Command, []string) { + + chain := flow.ChainID(flagChain).Chain() + + log.Info().Msgf("creating payloads for chain %s", chain) + + ctx := fvm.NewContext( + fvm.WithChain(chain), + ) + + vm := fvm.NewVirtualMachine() + + storageSnapshot := snapshot.MapStorageSnapshot{} + + bootstrapProcedure := fvm.Bootstrap( + unittest.ServiceAccountPublicKey, + fvm.WithInitialTokenSupply(unittest.GenesisTokenSupply), + ) + + executionSnapshot, _, err := vm.Run( + ctx, + bootstrapProcedure, + storageSnapshot, + ) + if err != nil { + log.Fatal().Err(err) + } + + payloads := make([]*ledger.Payload, 0, len(executionSnapshot.WriteSet)) + + for registerID, registerValue := range executionSnapshot.WriteSet { + payloadKey := convert.RegisterIDToLedgerKey(registerID) + payload := ledger.NewPayload(payloadKey, registerValue) + payloads = append(payloads, payload) + } + + log.Info().Msgf("writing payloads to %s", flagOutputPayloadFileName) + + numOfPayloadWritten, err := util.CreatePayloadFile( + log.Logger, + flagOutputPayloadFileName, + payloads, + nil, + false, + ) + if err != nil { + log.Fatal().Err(err) + } + + log.Info().Msgf("wrote %d payloads", numOfPayloadWritten) +} diff --git a/cmd/util/cmd/root.go b/cmd/util/cmd/root.go index b668e850db0..6f333dbddd6 100644 --- a/cmd/util/cmd/root.go +++ b/cmd/util/cmd/root.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/viper" "github.com/onflow/flow-go/cmd/util/cmd/addresses" + bootstrap_execution_state_payloads "github.com/onflow/flow-go/cmd/util/cmd/bootstrap-execution-state-payloads" checkpoint_collect_stats "github.com/onflow/flow-go/cmd/util/cmd/checkpoint-collect-stats" checkpoint_list_tries "github.com/onflow/flow-go/cmd/util/cmd/checkpoint-list-tries" checkpoint_trie_stats "github.com/onflow/flow-go/cmd/util/cmd/checkpoint-trie-stats" @@ -82,6 +83,7 @@ func addCommands() { rootCmd.AddCommand(export_json_transactions.Cmd) rootCmd.AddCommand(read_hotstuff.RootCmd) rootCmd.AddCommand(addresses.Cmd) + rootCmd.AddCommand(bootstrap_execution_state_payloads.Cmd) } func initConfig() {