Skip to content

Commit

Permalink
Merge pull request #6929 from onflow/bastian/improve-remote-debugger
Browse files Browse the repository at this point in the history
Refactor the remote debugger and add support for using the execution data API
  • Loading branch information
turbolent authored Jan 28, 2025
2 parents 64c8b79 + 15f26d2 commit c8240d3
Show file tree
Hide file tree
Showing 8 changed files with 588 additions and 265 deletions.
89 changes: 80 additions & 9 deletions cmd/util/cmd/debug-script/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package debug_tx

import (
"context"
"os"

"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/execution"
"github.com/onflow/flow/protobuf/go/flow/executiondata"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/debug"
)
Expand All @@ -14,9 +21,12 @@ import (
// `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
flagAccessAddress string
flagExecutionAddress string
flagBlockID string
flagChain string
flagScript string
flagUseExecutionDataAPI bool
)

var Cmd = &cobra.Command{
Expand All @@ -33,13 +43,22 @@ func init() {
"",
"Chain name",
)
_ = Cmd.MarkFlagRequired("chain")

Cmd.Flags().StringVar(&flagAccessAddress, "access-address", "", "address of the access node")
_ = Cmd.MarkFlagRequired("access-address")

Cmd.Flags().StringVar(&flagExecutionAddress, "execution-address", "", "address of the execution node")
_ = Cmd.MarkFlagRequired("execution-address")

Cmd.Flags().StringVar(&flagBlockID, "block-id", "", "block ID")
_ = Cmd.MarkFlagRequired("block-id")

_ = Cmd.MarkFlagRequired("chain")

Cmd.Flags().StringVar(&flagScript, "script", "", "path to script")
_ = Cmd.MarkFlagRequired("script")

Cmd.Flags().BoolVar(&flagUseExecutionDataAPI, "use-execution-data-api", false, "use the execution data API")
}

func run(*cobra.Command, []string) {
Expand All @@ -52,16 +71,68 @@ func run(*cobra.Command, []string) {
log.Fatal().Err(err).Msgf("failed to read script from file %s", flagScript)
}

debugger := debug.NewRemoteDebugger(
flagExecutionAddress,
chain,
log.Logger,
log.Info().Msg("Fetching block header ...")

accessConn, err := grpc.NewClient(
flagAccessAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatal().Err(err).Msg("failed to create access connection")
}
defer accessConn.Close()

accessClient := access.NewAccessAPIClient(accessConn)

blockID, err := flow.HexStringToIdentifier(flagBlockID)
if err != nil {
log.Fatal().Err(err).Msg("failed to parse block ID")
}

header, err := debug.GetAccessAPIBlockHeader(accessClient, context.Background(), blockID)
if err != nil {
log.Fatal().Err(err).Msg("failed to fetch block header")
}

blockHeight := header.Height

log.Info().Msgf(
"Fetched block header: %s (height %d)",
header.ID(),
blockHeight,
)

var snap snapshot.StorageSnapshot

if flagUseExecutionDataAPI {
executionDataClient := executiondata.NewExecutionDataAPIClient(accessConn)
snap, err = debug.NewExecutionDataStorageSnapshot(executionDataClient, nil, blockHeight)
if err != nil {
log.Fatal().Err(err).Msg("failed to create storage snapshot")
}
} else {
executionConn, err := grpc.NewClient(
flagExecutionAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatal().Err(err).Msg("failed to create execution connection")
}
defer executionConn.Close()

executionClient := execution.NewExecutionAPIClient(executionConn)
snap, err = debug.NewExecutionNodeStorageSnapshot(executionClient, nil, blockID)
if err != nil {
log.Fatal().Err(err).Msg("failed to create storage snapshot")
}
}

debugger := debug.NewRemoteDebugger(chain, log.Logger)

// TODO: add support for arguments
var arguments [][]byte

result, scriptErr, processErr := debugger.RunScript(code, arguments)
result, scriptErr, processErr := debugger.RunScript(code, arguments, snap, header)

if scriptErr != nil {
log.Fatal().Err(scriptErr).Msg("transaction error")
Expand Down
Loading

0 comments on commit c8240d3

Please sign in to comment.