From 7350d9f5a4cba9c199906158df494df7072cb7d2 Mon Sep 17 00:00:00 2001 From: novosandara <160175841+novosandara@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:39:28 +0200 Subject: [PATCH] Creating method debug_traceBlockFromFile (#334) --- jsonrpc/debug_endpoint.go | 33 +++++++++++++-- jsonrpc/debug_endpoint_test.go | 75 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/jsonrpc/debug_endpoint.go b/jsonrpc/debug_endpoint.go index dd47fef4a0..68a1644deb 100644 --- a/jsonrpc/debug_endpoint.go +++ b/jsonrpc/debug_endpoint.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "time" "github.com/0xPolygon/polygon-edge/helper/hex" @@ -68,14 +69,16 @@ type debugStore interface { // Debug is the debug jsonrpc endpoint type Debug struct { - store debugStore - throttling *Throttling + store debugStore + throttling *Throttling + ReadFileFunc func(filename string) ([]byte, error) } func NewDebug(store debugStore, requestsPerSecond uint64) *Debug { return &Debug{ - store: store, - throttling: NewThrottling(requestsPerSecond, time.Second), + store: store, + throttling: NewThrottling(requestsPerSecond, time.Second), + ReadFileFunc: os.ReadFile, } } @@ -150,6 +153,28 @@ func (d *Debug) TraceBlock( ) } +func (d *Debug) TraceBlockFromFile( + input string, + config *TraceConfig, +) (interface{}, error) { + return d.throttling.AttemptRequest( + context.Background(), + func() (interface{}, error) { + blockByte, decodeErr := d.ReadFileFunc(input) + if decodeErr != nil { + return nil, fmt.Errorf("could not read file: %w", decodeErr) + } + + block := &types.Block{} + if err := block.UnmarshalRLP(blockByte); err != nil { + return nil, err + } + + return d.traceBlock(block, config) + }, + ) +} + func (d *Debug) TraceTransaction( txHash types.Hash, config *TraceConfig, diff --git a/jsonrpc/debug_endpoint_test.go b/jsonrpc/debug_endpoint_test.go index 5e7f8871d2..9658b3d516 100644 --- a/jsonrpc/debug_endpoint_test.go +++ b/jsonrpc/debug_endpoint_test.go @@ -2,6 +2,7 @@ package jsonrpc import ( "encoding/json" + "fmt" "math/big" "testing" "time" @@ -429,6 +430,80 @@ func TestTraceBlock(t *testing.T) { } } +func TestTraceBlockFromFile(t *testing.T) { + t.Parallel() + + blockBytes := testLatestBlock.MarshalRLP() + + tests := []struct { + name string + input []byte + fileErr error + config *TraceConfig + store *debugEndpointMockStore + result interface{} + err bool + }{ + { + name: "should follow the given block from the file", + input: blockBytes, + fileErr: nil, + config: &TraceConfig{}, + store: &debugEndpointMockStore{ + traceBlockFn: func(block *types.Block, tracer tracer.Tracer) ([]interface{}, error) { + assert.Equal(t, testLatestBlock, block) + + return testTraceResults, nil + }, + }, + result: testTraceResults, + err: false, + }, + { + name: "should return error in case of invalid block from the file", + input: nil, + fileErr: fmt.Errorf("file is incorrect"), + config: &TraceConfig{}, + store: &debugEndpointMockStore{}, + result: nil, + err: true, + }, + { + name: "should return error in case of invalid block from the UnmarshalRLP method", + input: []byte{}, + fileErr: nil, + config: &TraceConfig{}, + store: &debugEndpointMockStore{}, + result: nil, + err: true, + }, + } + + for _, test := range tests { + test := test + + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + endpoint := NewDebug(test.store, 100000) + + endpoint.ReadFileFunc = func(filename string) ([]byte, error) { + return test.input, test.fileErr + } + + res, err := endpoint.TraceBlockFromFile("testfile.txt", test.config) + + assert.Equal(t, test.result, res) + + if test.err { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + func TestTraceTransaction(t *testing.T) { t.Parallel()