Skip to content

Commit

Permalink
add util command to generate payloads for bootsrapped execution state
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Mar 1, 2024
1 parent 1726b7b commit 78a0c62
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 78a0c62

Please sign in to comment.