Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of the debug_getRawReceipts method #344

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions jsonrpc/debug_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type debugBlockchainStore interface {
// GetHeaderByNumber gets a header using the provided number
GetHeaderByNumber(uint64) (*types.Header, bool)

// GetReceiptsByHash returns the receipts by block hash
GetReceiptsByHash(types.Hash) ([]*types.Receipt, error)

// ReadTxLookup returns a block hash in which a given txn was mined
ReadTxLookup(txnHash types.Hash) (uint64, bool)

Expand Down Expand Up @@ -295,6 +298,33 @@ func (d *Debug) GetRawTransaction(txHash types.Hash) (interface{}, error) {
)
}

// GetRawReceipts retrieves the binary-encoded receipts of a single block.
func (d *Debug) GetRawReceipts(filter BlockNumberOrHash) (interface{}, error) {
return d.throttling.AttemptRequest(
context.Background(),
func() (interface{}, error) {
header, err := GetHeaderFromBlockNumberOrHash(filter, d.store)
if err != nil {
return nil, err
}

receipts, err := d.store.GetReceiptsByHash(header.Hash)
if err != nil {
return nil, err
}

result := make([][]byte, len(receipts))

for i, receipt := range receipts {
b := receipt.MarshalRLP()
result[i] = b
novosandara marked this conversation as resolved.
Show resolved Hide resolved
}

return result, nil
},
)
}

func (d *Debug) traceBlock(
block *types.Block,
config *TraceConfig,
Expand Down
96 changes: 96 additions & 0 deletions jsonrpc/debug_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type debugEndpointMockStore struct {
headerFn func() *types.Header
getHeaderByNumberFn func(uint64) (*types.Header, bool)
getReceiptsByHashFn func(types.Hash) ([]*types.Receipt, error)
readTxLookupFn func(types.Hash) (uint64, bool)
getPendingTxFn func(types.Hash) (*types.Transaction, bool)
getBlockByHashFn func(types.Hash, bool) (*types.Block, bool)
Expand All @@ -38,6 +39,10 @@ func (s *debugEndpointMockStore) GetHeaderByNumber(num uint64) (*types.Header, b
return s.getHeaderByNumberFn(num)
}

func (s *debugEndpointMockStore) GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error) {
return s.getReceiptsByHashFn(hash)
}

func (s *debugEndpointMockStore) ReadTxLookup(txnHash types.Hash) (uint64, bool) {
return s.readTxLookupFn(txnHash)
}
Expand Down Expand Up @@ -1070,6 +1075,97 @@ func TestGetRawTransaction(t *testing.T) {
}
}

func TestGetRawReceipts(t *testing.T) {
t.Parallel()

const (
cumulativeGasUsed = 28000
gasUsed = 26000
)

rec := createTestReceipt(nil, cumulativeGasUsed, gasUsed, testTx1.Hash())
receipts := []*types.Receipt{rec}

tests := []struct {
name string
filter BlockNumberOrHash
store *debugEndpointMockStore
result interface{}
err bool
}{
{
name: "HeaderNotFound",
filter: EarliestBlockNumberOrHash,
store: &debugEndpointMockStore{
getHeaderByNumberFn: func(num uint64) (*types.Header, bool) {
assert.Equal(t, uint64(0), num)

return nil, false
},
},
result: nil,
err: true,
},
{
name: "ReceiptsNotFound",
filter: LatestBlockNumberOrHash,

store: &debugEndpointMockStore{
headerFn: func() *types.Header {
return testLatestBlock.Header
},
getBlockByHashFn: func(hash types.Hash, full bool) (*types.Block, bool) {
return testLatestBlock, true
},
getReceiptsByHashFn: func(hash types.Hash) ([]*types.Receipt, error) {
return nil, ErrExecutionTimeout
},
},
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
},

getReceiptsByHashFn: func(hash types.Hash) ([]*types.Receipt, error) {
return receipts, nil
},
},
result: [][]byte{rec.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.GetRawReceipts(test.filter)

assert.Equal(t, test.result, res)
novosandara marked this conversation as resolved.
Show resolved Hide resolved

if test.err {
assert.Error(t, err)
novosandara marked this conversation as resolved.
Show resolved Hide resolved
} else {
assert.NoError(t, err)
novosandara marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
}

func Test_newTracer(t *testing.T) {
t.Parallel()

Expand Down
Loading