Skip to content

Commit

Permalink
Creating method debug_traceBlockFromFile (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
novosandara authored Aug 15, 2024
1 parent f4f4be8 commit 7350d9f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
33 changes: 29 additions & 4 deletions jsonrpc/debug_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"time"

"github.com/0xPolygon/polygon-edge/helper/hex"
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand Down
75 changes: 75 additions & 0 deletions jsonrpc/debug_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonrpc

import (
"encoding/json"
"fmt"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 7350d9f

Please sign in to comment.