diff --git a/cmd/util/cmd/debug-script/cmd.go b/cmd/util/cmd/debug-script/cmd.go new file mode 100644 index 00000000000..3c0a2718f7f --- /dev/null +++ b/cmd/util/cmd/debug-script/cmd.go @@ -0,0 +1,73 @@ +package debug_tx + +import ( + "os" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/onflow/flow-go/model/flow" + "github.com/onflow/flow-go/utils/debug" +) + +// use the following command to forward port 9000 from the EN to localhost:9001 +// `gcloud compute ssh '--ssh-flag=-A' --no-user-output-enabled --tunnel-through-iap migrationmainnet1-execution-001 --project flow-multi-region -- -NL 9001:localhost:9000` + +var ( + flagExecutionAddress string + flagChain string + flagScript string +) + +var Cmd = &cobra.Command{ + Use: "debug-script", + Short: "debug a script", + Run: run, +} + +func init() { + + Cmd.Flags().StringVar( + &flagChain, + "chain", + "", + "Chain name", + ) + _ = Cmd.MarkFlagRequired("chain") + + Cmd.Flags().StringVar(&flagExecutionAddress, "execution-address", "", "address of the execution node") + _ = Cmd.MarkFlagRequired("execution-address") + + Cmd.Flags().StringVar(&flagScript, "script", "", "path to script") + _ = Cmd.MarkFlagRequired("script") +} + +func run(*cobra.Command, []string) { + + chainID := flow.ChainID(flagChain) + chain := chainID.Chain() + + code, err := os.ReadFile(flagScript) + if err != nil { + log.Fatal().Err(err).Msgf("failed to read script from file %s", flagScript) + } + + debugger := debug.NewRemoteDebugger( + flagExecutionAddress, + chain, + log.Logger, + ) + + // TODO: add support for arguments + var arguments [][]byte + + result, scriptErr, processErr := debugger.RunScript(code, arguments) + + if scriptErr != nil { + log.Fatal().Err(scriptErr).Msg("transaction error") + } + if processErr != nil { + log.Fatal().Err(processErr).Msg("process error") + } + log.Info().Msgf("result: %s", result) +} diff --git a/cmd/util/cmd/root.go b/cmd/util/cmd/root.go index c4464fba648..dd11c40b14b 100644 --- a/cmd/util/cmd/root.go +++ b/cmd/util/cmd/root.go @@ -17,6 +17,7 @@ import ( 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" + debug_script "github.com/onflow/flow-go/cmd/util/cmd/debug-script" debug_tx "github.com/onflow/flow-go/cmd/util/cmd/debug-tx" diff_states "github.com/onflow/flow-go/cmd/util/cmd/diff-states" epochs "github.com/onflow/flow-go/cmd/util/cmd/epochs/cmd" @@ -120,6 +121,7 @@ func addCommands() { rootCmd.AddCommand(system_addresses.Cmd) rootCmd.AddCommand(check_storage.Cmd) rootCmd.AddCommand(debug_tx.Cmd) + rootCmd.AddCommand(debug_script.Cmd) } func initConfig() {