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

ethclient: fix BlockReceipts parameter encoding #28087

Merged
merged 3 commits into from
Sep 25, 2023
Merged

Conversation

kandrianov
Copy link
Contributor

There is an issue. After marshaling rpc.BlockNumberOrHash we have e.g. {"jsonrpc":"2.0","id":3,"method":"eth_getBlockReceipts","params":[{"blockNumber":"0x2"}]} instead of {"jsonrpc":"2.0","id":3,"method":"eth_getBlockReceipts","params":["0x2"]} and invalid response.

There is an issue. After marshaling rpc.BlockNumberOrHash we have e.g. {"jsonrpc":"2.0","id":3,"method":"eth_getBlockReceipts","params":[{"blockNumber":"0x2"}]} instead of {"jsonrpc":"2.0","id":3,"method":"eth_getBlockReceipts","params":["0x2"]} and invalid response.
internal/ethapi: Fix eth_getBlockReceipts
@@ -111,7 +111,7 @@ func (ec *Client) PeerCount(ctx context.Context) (uint64, error) {
// BlockReceipts returns the receipts of a given block number or hash
func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
var r []*types.Receipt
err := ec.c.CallContext(ctx, &r, "eth_getBlockReceipts", blockNrOrHash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, Please also add a test case

@MariusVanDerWijden
Copy link
Member

Afaict the method we provide eth_getBlockReceipts will accept both [{"blockNumber":"0x2"}] and ["0x2"] so the marshalling should be okay for geth. Other clients might require the latter form, but I'm not sure

@fjl fjl changed the title internal/ethapi: Fix eth_getBlockReceipts ethclient: fix BlockReceipts method Sep 19, 2023
@fjl fjl changed the title ethclient: fix BlockReceipts method ethclient: fix BlockReceipts parameter encoding Sep 19, 2023
@fjl fjl added this to the 1.13.2 milestone Sep 19, 2023
@fjl fjl self-requested a review September 25, 2023 13:14
@fjl fjl merged commit 4985d83 into ethereum:master Sep 25, 2023
1 check passed
tyler-smith pushed a commit to blocknative/go-ethereum that referenced this pull request Oct 12, 2023
tyler-smith pushed a commit to blocknative/go-ethereum that referenced this pull request Oct 12, 2023
siosw pushed a commit to fabriqnetwork/go-ethereum that referenced this pull request Oct 16, 2023
Co-authored-by: Felix Lange <fjl@twurst.com>
@ajsutton
Copy link
Contributor

Doesn't this change result in block numbers being sent in decimal? ie it winds up executing

go-ethereum/rpc/types.go

Lines 222 to 225 in 9231770

func (bnh *BlockNumberOrHash) String() string {
if bnh.BlockNumber != nil {
return strconv.Itoa(int(*bnh.BlockNumber))
}
It also seems to be missing the encoding for labels like latest now. So I think this now only works for block hashes.

unit test demonstrating this (add to rpc/types_test.go:

func TestBlockNumberOrHash_WithNumber_StringAndUnmarshal(t *testing.T) {
	tests := []struct {
		name  string
		value BlockNumberOrHash
	}{
		{"max", BlockNumberOrHashWithNumber(math.MaxInt64)},
		{"pending", BlockNumberOrHashWithNumber(PendingBlockNumber)},
		{"latest", BlockNumberOrHashWithNumber(LatestBlockNumber)},
		{"earliest", BlockNumberOrHashWithNumber(EarliestBlockNumber)},
		{"0x20", BlockNumberOrHashWithNumber(32)},
		{"hash", BlockNumberOrHashWithHash(common.Hash{0xaa}, false)},
	}
	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			bnh := test.value
			marshalled := []byte("\"" + bnh.String() + "\"")
			var unmarshalled BlockNumberOrHash
			err := json.Unmarshal(marshalled, &unmarshalled)
			if err != nil {
				t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err)
			}
			if !reflect.DeepEqual(bnh, unmarshalled) {
				t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
			}
		})
	}
}

devopsbo3 pushed a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
sduchesneau pushed a commit to streamingfast/go-ethereum that referenced this pull request Dec 19, 2023
marun added a commit to ava-labs/coreth that referenced this pull request Dec 19, 2023
darioush pushed a commit to ava-labs/coreth that referenced this pull request Dec 20, 2023
* Copy ethereum/go-ethereum#27702

* Fix TestRPCGetBlockReceipts

* Get ethclient compiling

* Copy ethereum/go-ethereum#28087

* Copy ethereum/go-ethereum#28358
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants