ethclient: restore BlockReceipts support for BlockNumberOrHash objects #33242 (#1941)

This commit is contained in:
Daniel Liu 2026-01-16 19:53:11 +08:00 committed by GitHub
parent 2b10e68b10
commit 5f911680c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View file

@ -122,7 +122,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.String())
err := ec.c.CallContext(ctx, &r, "eth_getBlockReceipts", blockNrOrHash)
if err == nil && r == nil {
return nil, ethereum.ErrNotFound
}

View file

@ -17,6 +17,7 @@
package ethclient
import (
"context"
"errors"
"math/big"
"reflect"
@ -24,6 +25,8 @@ import (
ethereum "github.com/XinFinOrg/XDPoSChain"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/rpc"
)
// Verify that Client implements the ethereum interfaces.
@ -167,3 +170,46 @@ func TestToFilterArg(t *testing.T) {
})
}
}
func TestBlockReceiptsPreservesCanonicalFlag(t *testing.T) {
srv := rpc.NewServer()
service := &blockReceiptsTestService{calls: make(chan rpc.BlockNumberOrHash, 1)}
if err := srv.RegisterName("eth", service); err != nil {
t.Fatalf("failed to register service: %v", err)
}
defer srv.Stop()
client := rpc.DialInProc(srv)
defer client.Close()
ec := NewClient(client)
defer ec.Close()
hash := common.HexToHash("0x01")
ref := rpc.BlockNumberOrHashWithHash(hash, true)
if _, err := ec.BlockReceipts(context.Background(), ref); err != nil {
t.Fatalf("BlockReceipts returned error: %v", err)
}
select {
case call := <-service.calls:
if call.BlockHash == nil || *call.BlockHash != hash {
t.Fatalf("unexpected block hash: got %v, want %v", call.BlockHash, hash)
}
if !call.RequireCanonical {
t.Fatalf("requireCanonical flag was lost: %+v", call)
}
default:
t.Fatal("service was not called")
}
}
type blockReceiptsTestService struct {
calls chan rpc.BlockNumberOrHash
}
func (s *blockReceiptsTestService) GetBlockReceipts(ctx context.Context, block rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
s.calls <- block
return []*types.Receipt{}, nil
}