Skip to content

Commit

Permalink
Merge pull request #6425 from onflow/bastian/debug-script-command
Browse files Browse the repository at this point in the history
[Util] Add command to debug a script
  • Loading branch information
turbolent authored Sep 2, 2024
2 parents 8652dc9 + ba65416 commit b47fa60
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
73 changes: 73 additions & 0 deletions cmd/util/cmd/debug-script/cmd.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit b47fa60

Please sign in to comment.