gethclient: preserve requireCanonical in BlockReceipts

This commit is contained in:
Bilog WEB3 2025-11-17 15:48:20 +00:00
parent 5a750b6614
commit b956774634
4 changed files with 53 additions and 43 deletions

View file

@ -124,7 +124,7 @@ func (ec *Client) PeerCount(ctx context.Context) (uint64, error) {
// BlockReceipts returns the receipts of a given block number or hash. // BlockReceipts returns the receipts of a given block number or hash.
func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error) { func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
var r []*types.Receipt var r []*types.Receipt
err := ec.c.CallContext(ctx, &r, "eth_getBlockReceipts", blockNrOrHash) err := ec.c.CallContext(ctx, &r, "eth_getBlockReceipts", blockNrOrHash.String())
if err == nil && r == nil { if err == nil && r == nil {
return nil, ethereum.NotFound return nil, ethereum.NotFound
} }

View file

@ -1001,45 +1001,3 @@ func TestSimulateV1WithBlockNumberOrHash(t *testing.T) {
t.Fatalf("expected 1 block result, got %d", len(results)) t.Fatalf("expected 1 block result, got %d", len(results))
} }
} }
func TestBlockReceiptsPreservesRequireCanonical(t *testing.T) {
server := rpc.NewServer()
service := &testBlockReceiptsService{}
if err := server.RegisterName("eth", service); err != nil {
t.Fatalf("failed to register test service: %v", err)
}
defer server.Stop()
rpcClient := rpc.DialInProc(server)
defer rpcClient.Close()
client := ethclient.NewClient(rpcClient)
defer client.Close()
hash := common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
arg := rpc.BlockNumberOrHashWithHash(hash, true)
if _, err := client.BlockReceipts(context.Background(), arg); err != nil {
t.Fatalf("BlockReceipts returned error: %v", err)
}
if !service.called {
t.Fatalf("expected GetBlockReceipts to be called")
}
if !service.lastArg.RequireCanonical {
t.Fatalf("requireCanonical flag was not preserved")
}
if service.lastArg.BlockHash == nil || *service.lastArg.BlockHash != hash {
t.Fatalf("unexpected block hash: %v", service.lastArg.BlockHash)
}
}
type testBlockReceiptsService struct {
lastArg rpc.BlockNumberOrHash
called bool
}
func (s *testBlockReceiptsService) GetBlockReceipts(ctx context.Context, arg rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
s.lastArg = arg
s.called = true
return []*types.Receipt{}, nil
}

View file

@ -166,6 +166,17 @@ func (ec *Client) CallContractWithBlockOverrides(ctx context.Context, msg ethere
return hex, err return hex, err
} }
// BlockReceipts returns the receipts of a given block number or hash.
// This wrapper preserves geth-specific flags such as requireCanonical.
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)
if err == nil && r == nil {
return nil, ethereum.NotFound
}
return r, err
}
// GCStats retrieves the current garbage collection stats from a geth node. // GCStats retrieves the current garbage collection stats from a geth node.
func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) { func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) {
var result debug.GCStats var result debug.GCStats

View file

@ -620,3 +620,44 @@ func testCallContractWithBlockOverrides(t *testing.T, client *rpc.Client) {
t.Fatalf("unexpected result: %x", res) t.Fatalf("unexpected result: %x", res)
} }
} }
func TestGethClientBlockReceiptsPreservesRequireCanonical(t *testing.T) {
server := rpc.NewServer()
service := &testGethBlockReceiptsService{}
if err := server.RegisterName("eth", service); err != nil {
t.Fatalf("failed to register test service: %v", err)
}
defer server.Stop()
rpcClient := rpc.DialInProc(server)
defer rpcClient.Close()
client := New(rpcClient)
hash := common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
arg := rpc.BlockNumberOrHashWithHash(hash, true)
if _, err := client.BlockReceipts(context.Background(), arg); err != nil {
t.Fatalf("BlockReceipts returned error: %v", err)
}
if !service.called {
t.Fatalf("expected GetBlockReceipts to be called")
}
if !service.lastArg.RequireCanonical {
t.Fatalf("requireCanonical flag was not preserved")
}
if service.lastArg.BlockHash == nil || *service.lastArg.BlockHash != hash {
t.Fatalf("unexpected block hash: %v", service.lastArg.BlockHash)
}
}
type testGethBlockReceiptsService struct {
lastArg rpc.BlockNumberOrHash
called bool
}
func (s *testGethBlockReceiptsService) GetBlockReceipts(ctx context.Context, arg rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
s.lastArg = arg
s.called = true
return []*types.Receipt{}, nil
}