Skip to content

Commit

Permalink
Merge pull request onflow#5487 from onflow/bastian/bootstrap-executio…
Browse files Browse the repository at this point in the history
…n-state-payloads

Add util command to generate payloads for bootsrapped execution state
  • Loading branch information
turbolent authored Mar 1, 2024
2 parents 5dcab61 + 78a0c62 commit 649345d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
90 changes: 90 additions & 0 deletions cmd/util/cmd/bootstrap-execution-state-payloads/cmd.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 649345d

Please sign in to comment.