Skip to content

Commit

Permalink
fixed empty result for...
Browse files Browse the repository at this point in the history
- `eth_getBlockByNumber`
- `eth_getBlockByHash`
- `eth_getTransactionByHash`
- `eth_getReceiptByHash`
  • Loading branch information
lmittmann committed Feb 5, 2022
1 parent bd6cb5d commit ae8221e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions module/eth/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

var (
errNotFound = fmt.Errorf("not found")
)

func toBlockNumberArg(blockNumber *big.Int) string {
if blockNumber == nil {
return "latest"
Expand Down
3 changes: 3 additions & 0 deletions module/eth/get_block_by_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (f *BlockByHashFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
if len(f.result) <= 4 { // empty or "null"
return errNotFound
}

var head *types.Header
var body rpcBlock
Expand Down
21 changes: 21 additions & 0 deletions module/eth/get_block_by_hash_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package eth_test

import (
"fmt"
"math/big"
"sync/atomic"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -146,3 +148,22 @@ func TestHeaderByHash__12965000(t *testing.T) {
t.Fatalf("(-want, +got)\n%s", diff)
}
}

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

srv := rpctest.NewFileServer(t, "testdata/get_block_by_hash__0x00.golden")
defer srv.Close()

client := w3.MustDial(srv.URL())
defer client.Close()

var (
block = new(types.Block)
wantErr = fmt.Errorf("w3: response handling failed: not found")
)

if gotErr := client.Call(eth.BlockByHash(common.Hash{}).Returns(block)); wantErr.Error() != gotErr.Error() {
t.Fatalf("want %v, got %v", wantErr, gotErr)
}
}
3 changes: 3 additions & 0 deletions module/eth/get_block_by_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (f *BlockByNumberFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
if len(f.result) <= 4 { // empty or "null"
return errNotFound
}

var head *types.Header
var body rpcBlock
Expand Down
20 changes: 20 additions & 0 deletions module/eth/get_block_by_number_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth_test

import (
"fmt"
"math/big"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -146,3 +147,22 @@ func TestHeaderByNumber__12965000(t *testing.T) {
t.Fatalf("(-want, +got)\n%s", diff)
}
}

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

srv := rpctest.NewFileServer(t, "testdata/get_block_by_number__999999999.golden")
defer srv.Close()

client := w3.MustDial(srv.URL())
defer client.Close()

var (
block = new(types.Block)
wantErr = fmt.Errorf("w3: response handling failed: not found")
)

if gotErr := client.Call(eth.BlockByNumber(big.NewInt(999999999)).Returns(block)); wantErr.Error() != gotErr.Error() {
t.Fatalf("want %v, got %v", wantErr, gotErr)
}
}
4 changes: 4 additions & 0 deletions module/eth/get_transaction_by_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (f *TransactionByHashFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
if f.result == nil {
return errNotFound
}

*f.returns = *f.result
return nil
}
21 changes: 21 additions & 0 deletions module/eth/get_transaction_by_hash_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package eth_test

import (
"fmt"
"math/big"
"sync/atomic"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -81,3 +83,22 @@ func TestTransactionByHash_Type2(t *testing.T) {
t.Fatalf("(-want, +got)\n%s", diff)
}
}

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

srv := rpctest.NewFileServer(t, "testdata/get_transaction_by_hash__0x00.golden")
defer srv.Close()

client := w3.MustDial(srv.URL())
defer client.Close()

var (
tx = new(types.Transaction)
wantErr = fmt.Errorf("w3: response handling failed: not found")
)

if gotErr := client.Call(eth.TransactionByHash(common.Hash{}).Returns(tx)); wantErr.Error() != gotErr.Error() {
t.Fatalf("want %v, got %v", wantErr, gotErr)
}
}
4 changes: 4 additions & 0 deletions module/eth/get_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (f *TransactionReceiptFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
if f.result == nil {
return errNotFound
}

*f.returns = *f.result
return nil
}
20 changes: 20 additions & 0 deletions module/eth/get_transaction_receipt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth_test

import (
"fmt"
"math/big"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -65,3 +66,22 @@ func TestTransactionReceipt(t *testing.T) {
t.Fatalf("(-want, +got)\n%s", diff)
}
}

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

srv := rpctest.NewFileServer(t, "testdata/get_transaction_receipt_0x00.golden")
defer srv.Close()

client := w3.MustDial(srv.URL())
defer client.Close()

var (
receipt = new(types.Receipt)
wantErr = fmt.Errorf("w3: response handling failed: not found")
)

if gotErr := client.Call(eth.TransactionReceipt(common.Hash{}).Returns(receipt)); wantErr.Error() != gotErr.Error() {
t.Fatalf("want %v, got %v", wantErr, gotErr)
}
}
2 changes: 2 additions & 0 deletions module/eth/testdata/get_block_by_hash__0x00.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [{"jsonrpc":"2.0","id":1,"method":"eth_getBlockByHash","params":["0x0000000000000000000000000000000000000000000000000000000000000000",true]}]
< [{"jsonrpc":"2.0","id":1,"result":null}]
2 changes: 2 additions & 0 deletions module/eth/testdata/get_block_by_number__999999999.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [{"jsonrpc":"2.0","id":1,"method":"eth_getBlockByNumber","params":["0x3b9ac9ff",true]}]
< [{"jsonrpc":"2.0","id":1,"result":null}]
2 changes: 2 additions & 0 deletions module/eth/testdata/get_transaction_by_hash__0x00.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionByHash","params":["0x0000000000000000000000000000000000000000000000000000000000000000"]}]
< [{"jsonrpc":"2.0","id":1,"result":null}]
2 changes: 2 additions & 0 deletions module/eth/testdata/get_transaction_receipt_0x00.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionReceipt","params":["0x0000000000000000000000000000000000000000000000000000000000000000"]}]
< [{"jsonrpc":"2.0","id":1,"result":null}]

0 comments on commit ae8221e

Please sign in to comment.