From a5c89b371bff3eaaec7971c44599903fe46dd219 Mon Sep 17 00:00:00 2001 From: Novo Sandara Date: Sun, 25 Aug 2024 17:13:11 +0200 Subject: [PATCH 1/2] Implementation of the debug_GetRawBlock method --- jsonrpc/debug_endpoint.go | 20 +++++++++ jsonrpc/debug_endpoint_test.go | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/jsonrpc/debug_endpoint.go b/jsonrpc/debug_endpoint.go index 68a1644deb..d95c6065df 100644 --- a/jsonrpc/debug_endpoint.go +++ b/jsonrpc/debug_endpoint.go @@ -238,6 +238,26 @@ func (d *Debug) TraceCall( ) } +// GetRawBlock retrieves the RLP encoded for a single block. +func (d *Debug) GetRawBlock(filter BlockNumberOrHash) (interface{}, error) { + return d.throttling.AttemptRequest( + context.Background(), + func() (interface{}, error) { + header, err := GetHeaderFromBlockNumberOrHash(filter, d.store) + if err != nil { + return nil, ErrHeaderNotFound + } + + block, ok := d.store.GetBlockByHash(header.Hash, true) + if !ok { + return nil, fmt.Errorf("block %s not found", header.Hash) + } + + return block.MarshalRLP(), nil + }, + ) +} + func (d *Debug) traceBlock( block *types.Block, config *TraceConfig, diff --git a/jsonrpc/debug_endpoint_test.go b/jsonrpc/debug_endpoint_test.go index 9658b3d516..051fc1f320 100644 --- a/jsonrpc/debug_endpoint_test.go +++ b/jsonrpc/debug_endpoint_test.go @@ -801,6 +801,84 @@ func TestTraceCall(t *testing.T) { } } +func TestGetRawBlock(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + filter BlockNumberOrHash + store *debugEndpointMockStore + result interface{} + err bool + }{ + { + name: "HeaderNotFound", + filter: BlockNumberOrHash{}, + store: &debugEndpointMockStore{ + headerFn: func() *types.Header { + return testLatestBlock.Header + }, + getBlockByHashFn: func(hash types.Hash, full bool) (*types.Block, bool) { + return nil, false + }, + }, + result: nil, + err: true, + }, + { + name: "BlockNotFound", + filter: LatestBlockNumberOrHash, + + store: &debugEndpointMockStore{ + headerFn: func() *types.Header { + return testLatestBlock.Header + }, + + getBlockByHashFn: func(hash types.Hash, full bool) (*types.Block, bool) { + return nil, false + }, + }, + result: nil, + err: true, + }, + { + name: "Success", + filter: LatestBlockNumberOrHash, + store: &debugEndpointMockStore{ + headerFn: func() *types.Header { + return testLatestBlock.Header + }, + + getBlockByHashFn: func(hash types.Hash, full bool) (*types.Block, bool) { + return testLatestBlock, true + }, + }, + result: testLatestBlock.MarshalRLP(), + err: false, + }, + } + + for _, test := range tests { + test := test + + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + endpoint := NewDebug(test.store, 100000) + + res, err := endpoint.GetRawBlock(test.filter) + + assert.Equal(t, test.result, res) + + if test.err { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + func Test_newTracer(t *testing.T) { t.Parallel() From 212e6b15822d7a9fb5e1c99e2449639539d9a167 Mon Sep 17 00:00:00 2001 From: Novo Sandara Date: Mon, 26 Aug 2024 22:16:03 +0200 Subject: [PATCH 2/2] CR fix --- jsonrpc/debug_endpoint.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonrpc/debug_endpoint.go b/jsonrpc/debug_endpoint.go index d95c6065df..17ec0e9e7c 100644 --- a/jsonrpc/debug_endpoint.go +++ b/jsonrpc/debug_endpoint.go @@ -245,7 +245,7 @@ func (d *Debug) GetRawBlock(filter BlockNumberOrHash) (interface{}, error) { func() (interface{}, error) { header, err := GetHeaderFromBlockNumberOrHash(filter, d.store) if err != nil { - return nil, ErrHeaderNotFound + return nil, err } block, ok := d.store.GetBlockByHash(header.Hash, true)