debug

package
v0.29.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: AGPL-3.0 Imports: 15 Imported by: 0

README

Remote Debugger

Remote debugger provides utils needed to run transactions and scripts against live network data. It uses GRPC endpoints on an execution nodes to fetch registers and block info when running a transaction. This is mostly provided for debugging purpose and should not be used for production level operations. If you use the caching method you can run the transaction once and use the cached values to run transaction in debugging mode.

sample code

package debug_test

import (
	"fmt"
	"os"
	"testing"

	"github.com/rs/zerolog"
	"github.com/stretchr/testify/require"

	"github.com/onflow/flow-go/model/flow"
	"github.com/onflow/flow-go/utils/debug"
)

func TestDebugger_RunTransaction(t *testing.T) {

	grpcAddress := "localhost:3600"
	chain := flow.Emulator.Chain()
	debugger := debug.NewRemoteDebugger(grpcAddress, chain, zerolog.New(os.Stdout).With().Logger())

	const scriptTemplate = `
	import FlowServiceAccount from 0x%s
	transaction() {
		prepare(signer: AuthAccount) {
			log(signer.balance)
		}
	  }
	`

	script := []byte(fmt.Sprintf(scriptTemplate, chain.ServiceAddress()))
	txBody := flow.NewTransactionBody().
		SetGasLimit(9999).
		SetScript([]byte(script)).
		SetPayer(chain.ServiceAddress()).
		SetProposalKey(chain.ServiceAddress(), 0, 0)
	txBody.Authorizers = []flow.Address{chain.ServiceAddress()}

	// Run at the latest blockID
	txErr, err := debugger.RunTransaction(txBody)
	require.NoError(t, txErr)
	require.NoError(t, err)

	// Run with blockID (use the file cache)
	blockId, err := flow.HexStringToIdentifier("3a8281395e2c1aaa3b8643d148594b19e2acb477611a8e0cab8a55c46c40b563")
	require.NoError(t, err)
	txErr, err = debugger.RunTransactionAtBlockID(txBody, blockId, "")
	require.NoError(t, txErr)
	require.NoError(t, err)

	testCacheFile := "test.cache"
	defer os.Remove(testCacheFile)
	// the first run would cache the results
	txErr, err = debugger.RunTransactionAtBlockID(txBody, blockId, testCacheFile)
	require.NoError(t, txErr)
	require.NoError(t, err)

	// second one should only use the cache
	// make blockId invalid so if it endsup looking up by id it should fail
	blockId = flow.Identifier{}
	txErr, err = debugger.RunTransactionAtBlockID(txBody, blockId, testCacheFile)
	require.NoError(t, txErr)
	require.NoError(t, err)
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHeapAllocsBytes

func GetHeapAllocsBytes() uint64

GetHeapAllocsBytes returns the value of /gc/heap/allocs:bytes.

Types

type RemoteDebugger

type RemoteDebugger struct {
	// contains filtered or unexported fields
}

func NewRemoteDebugger

func NewRemoteDebugger(grpcAddress string,
	chain flow.Chain,
	logger zerolog.Logger) *RemoteDebugger

Warning : make sure you use the proper flow-go version, same version as the network you are collecting registers from, otherwise the execution might differ from the way runs on the network

func (*RemoteDebugger) RunScript

func (d *RemoteDebugger) RunScript(code []byte, arguments [][]byte) (value cadence.Value, scriptError, processError error)

func (*RemoteDebugger) RunScriptAtBlockID

func (d *RemoteDebugger) RunScriptAtBlockID(code []byte, arguments [][]byte, blockID flow.Identifier) (value cadence.Value, scriptError, processError error)

func (*RemoteDebugger) RunTransaction

func (d *RemoteDebugger) RunTransaction(txBody *flow.TransactionBody) (txErr, processError error)

RunTransaction runs the transaction given the latest sealed block data

func (*RemoteDebugger) RunTransactionAtBlockID

func (d *RemoteDebugger) RunTransactionAtBlockID(txBody *flow.TransactionBody, blockID flow.Identifier, regCachePath string) (txErr, processError error)

RunTransaction runs the transaction and tries to collect the registers at the given blockID note that it would be very likely that block is far in the past and you can't find the trie to read the registers from if regCachePath is empty, the register values won't be cached

type RemoteView

type RemoteView struct {
	Parent      *RemoteView
	Delta       map[string]flow.RegisterValue
	Cache       registerCache
	BlockID     []byte
	BlockHeader *flow.Header
	// contains filtered or unexported fields
}

RemoteView provides a view connected to a live execution node to read the registers writen values are kept inside a map

TODO implement register touches

func NewRemoteView

func NewRemoteView(grpcAddress string, opts ...RemoteViewOption) *RemoteView

func (*RemoteView) AllRegisters

func (v *RemoteView) AllRegisters() []flow.RegisterID

returns all the registers that has been touched

func (*RemoteView) Delete

func (v *RemoteView) Delete(owner, key string) error

func (*RemoteView) Done

func (v *RemoteView) Done()

func (*RemoteView) DropDelta

func (v *RemoteView) DropDelta()

func (*RemoteView) Get

func (v *RemoteView) Get(owner, key string) (flow.RegisterValue, error)

func (*RemoteView) MergeView

func (v *RemoteView) MergeView(o state.View) error

func (*RemoteView) NewChild

func (v *RemoteView) NewChild() state.View

func (*RemoteView) RegisterUpdates

func (v *RemoteView) RegisterUpdates() ([]flow.RegisterID, []flow.RegisterValue)

func (*RemoteView) Set

func (v *RemoteView) Set(owner, key string, value flow.RegisterValue) error

func (*RemoteView) Touch

func (v *RemoteView) Touch(owner, key string) error

type RemoteViewOption

type RemoteViewOption func(view *RemoteView) *RemoteView

A RemoteViewOption sets a configuration parameter for the remote view

func WithBlockID

func WithBlockID(blockID flow.Identifier) RemoteViewOption

WithBlockID sets the blockID for the remote view, if not used remote view will use the latest sealed block

func WithCache

func WithCache(cache registerCache) RemoteViewOption

WithFileCache sets the output path to store register values so can be fetched from a file cache it loads the values from the cache upon object construction

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL