Skip to content

Commit

Permalink
Add GetUncle* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MDobak committed Feb 12, 2024
1 parent d9d3529 commit 84bdff5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
18 changes: 18 additions & 0 deletions rpc/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ func (c *baseClient) GetTransactionReceipt(ctx context.Context, hash types.Hash)
return &res, nil
}

// GetUncleByBlockHashAndIndex implements the RPC interface.
func (c *baseClient) GetUncleByBlockHashAndIndex(ctx context.Context, hash types.Hash, index uint64) (*types.Block, error) {
var res types.Block
if err := c.transport.Call(ctx, &res, "eth_getUncleByBlockHashAndIndex", hash, types.NumberFromUint64(index)); err != nil {
return nil, err
}
return &res, nil
}

// GetUncleByBlockNumberAndIndex implements the RPC interface.
func (c *baseClient) GetUncleByBlockNumberAndIndex(ctx context.Context, number types.BlockNumber, index uint64) (*types.Block, error) {
var res types.Block
if err := c.transport.Call(ctx, &res, "eth_getUncleByBlockNumberAndIndex", number, types.NumberFromUint64(index)); err != nil {
return nil, err
}
return &res, nil
}

// GetLogs implements the RPC interface.
func (c *baseClient) GetLogs(ctx context.Context, query *types.FilterLogsQuery) ([]types.Log, error) {
var res []types.Log
Expand Down
64 changes: 64 additions & 0 deletions rpc/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,70 @@ func TestBaseClient_GetTransactionReceipt(t *testing.T) {
assert.Equal(t, false, receipt.Logs[0].Removed)
}

const mockGetUncleByBlockHashAndIndexRequest = `
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getUncleByBlockHashAndIndex",
"params": [
"0x1111111111111111111111111111111111111111111111111111111111111111",
"0x0"
]
}
`

func TestBaseClient_GetUncleByBlockHashAndIndex(t *testing.T) {
httpMock := newHTTPMock()
client := &baseClient{transport: httpMock}

httpMock.ResponseMock = &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewBufferString(mockBlockByNumberResponse)),
}

block, err := client.GetUncleByBlockHashAndIndex(
context.Background(),
types.MustHashFromHex("0x1111111111111111111111111111111111111111111111111111111111111111", types.PadNone),
0,
)

require.NoError(t, err)
assert.JSONEq(t, mockGetUncleByBlockHashAndIndexRequest, readBody(httpMock.Request))
assert.Equal(t, types.MustHashFromHex("0x2222222222222222222222222222222222222222222222222222222222222222", types.PadNone), block.Hash)
}

const mockGetUncleByBlockNumberAndIndexRequest = `
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getUncleByBlockNumberAndIndex",
"params": [
"0x1",
"0x2"
]
}
`

func TestBaseClient_GetUncleByBlockNumberAndIndex(t *testing.T) {
httpMock := newHTTPMock()
client := &baseClient{transport: httpMock}

httpMock.ResponseMock = &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewBufferString(mockBlockByNumberResponse)),
}

block, err := client.GetUncleByBlockNumberAndIndex(
context.Background(),
types.MustBlockNumberFromHex("0x1"),
2,
)

require.NoError(t, err)
assert.JSONEq(t, mockGetUncleByBlockNumberAndIndexRequest, readBody(httpMock.Request))
assert.Equal(t, types.MustHashFromHex("0x2222222222222222222222222222222222222222222222222222222222222222", types.PadNone), block.Hash)
}

const mockGetLogsRequest = `
{
"jsonrpc": "2.0",
Expand Down
10 changes: 5 additions & 5 deletions rpc/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ func (k *keyMock) Address() types.Address {
return k.addressCallback()
}

func (k *keyMock) SignHash(hash types.Hash) (*types.Signature, error) {
func (k *keyMock) SignHash(ctx context.Context, hash types.Hash) (*types.Signature, error) {
return k.signHashCallback(hash)
}

func (k *keyMock) SignMessage(data []byte) (*types.Signature, error) {
func (k *keyMock) SignMessage(ctx context.Context, data []byte) (*types.Signature, error) {
return k.signMessageCallback(data)
}

func (k *keyMock) SignTransaction(tx *types.Transaction) error {
func (k *keyMock) SignTransaction(ctx context.Context, tx *types.Transaction) error {
return k.signTransactionCallback(tx)
}

func (k *keyMock) VerifyHash(hash types.Hash, sig types.Signature) bool {
func (k *keyMock) VerifyHash(ctx context.Context, hash types.Hash, sig types.Signature) bool {
return false
}

func (k keyMock) VerifyMessage(data []byte, sig types.Signature) bool {
func (k keyMock) VerifyMessage(ctx context.Context, data []byte, sig types.Signature) bool {
return false
}
20 changes: 10 additions & 10 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,16 @@ type RPC interface {
// It returns the receipt of a transaction by transaction hash.
GetTransactionReceipt(ctx context.Context, hash types.Hash) (*types.TransactionReceipt, error)

// TODO: eth_getUncleByBlockHashAndIndex
// TODO: eth_getUncleByBlockNumberAndIndex
// TODO: eth_getCompilers
// TODO: eth_compileSolidity
// TODO: eth_compileLLL
// TODO: eth_compileSerpent
// GetUncleByBlockHashAndIndex performs eth_getUncleByBlockNumberAndIndex RPC call.
//
// It returns information about an uncle of a block by number and uncle index position.
GetUncleByBlockHashAndIndex(ctx context.Context, hash types.Hash, index uint64) (*types.Block, error)

// GetUncleByBlockNumberAndIndex performs eth_getUncleByBlockNumberAndIndex RPC call.
//
// It returns information about an uncle of a block by hash and uncle index position.
GetUncleByBlockNumberAndIndex(ctx context.Context, number types.BlockNumber, index uint64) (*types.Block, error)

// TODO: eth_newFilter
// TODO: eth_newBlockFilter
// TODO: eth_newPendingTransactionFilter
Expand All @@ -168,10 +172,6 @@ type RPC interface {
// It returns logs that match the given query.
GetLogs(ctx context.Context, query *types.FilterLogsQuery) ([]types.Log, error)

// TODO: eth_getWork
// TODO: eth_submitWork
// TODO: eth_submitHashrate

// MaxPriorityFeePerGas performs eth_maxPriorityFeePerGas RPC call.
//
// It returns the estimated maximum priority fee per gas.
Expand Down

0 comments on commit 84bdff5

Please sign in to comment.