Commit 97d9a411 authored by Axel Kingsley's avatar Axel Kingsley Committed by GitHub

export rpcBlock (#9246)

* export rpcBlock

* full refactor instead of alias

* update unit test class string
parent 73a74857
...@@ -187,7 +187,7 @@ func (n numberID) CheckID(id eth.BlockID) error { ...@@ -187,7 +187,7 @@ func (n numberID) CheckID(id eth.BlockID) error {
} }
func (s *EthClient) headerCall(ctx context.Context, method string, id rpcBlockID) (eth.BlockInfo, error) { func (s *EthClient) headerCall(ctx context.Context, method string, id rpcBlockID) (eth.BlockInfo, error) {
var header *rpcHeader var header *RPCHeader
err := s.client.CallContext(ctx, &header, method, id.Arg(), false) // headers are just blocks without txs err := s.client.CallContext(ctx, &header, method, id.Arg(), false) // headers are just blocks without txs
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -207,7 +207,7 @@ func (s *EthClient) headerCall(ctx context.Context, method string, id rpcBlockID ...@@ -207,7 +207,7 @@ func (s *EthClient) headerCall(ctx context.Context, method string, id rpcBlockID
} }
func (s *EthClient) blockCall(ctx context.Context, method string, id rpcBlockID) (eth.BlockInfo, types.Transactions, error) { func (s *EthClient) blockCall(ctx context.Context, method string, id rpcBlockID) (eth.BlockInfo, types.Transactions, error) {
var block *rpcBlock var block *RPCBlock
err := s.client.CallContext(ctx, &block, method, id.Arg(), true) err := s.client.CallContext(ctx, &block, method, id.Arg(), true)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -228,7 +228,7 @@ func (s *EthClient) blockCall(ctx context.Context, method string, id rpcBlockID) ...@@ -228,7 +228,7 @@ func (s *EthClient) blockCall(ctx context.Context, method string, id rpcBlockID)
} }
func (s *EthClient) payloadCall(ctx context.Context, method string, id rpcBlockID) (*eth.ExecutionPayloadEnvelope, error) { func (s *EthClient) payloadCall(ctx context.Context, method string, id rpcBlockID) (*eth.ExecutionPayloadEnvelope, error) {
var block *rpcBlock var block *RPCBlock
err := s.client.CallContext(ctx, &block, method, id.Arg(), true) err := s.client.CallContext(ctx, &block, method, id.Arg(), true)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -62,7 +62,7 @@ func randHash() (out common.Hash) { ...@@ -62,7 +62,7 @@ func randHash() (out common.Hash) {
return out return out
} }
func randHeader() (*types.Header, *rpcHeader) { func randHeader() (*types.Header, *RPCHeader) {
hdr := &types.Header{ hdr := &types.Header{
ParentHash: randHash(), ParentHash: randHash(),
UncleHash: randHash(), UncleHash: randHash(),
...@@ -81,7 +81,7 @@ func randHeader() (*types.Header, *rpcHeader) { ...@@ -81,7 +81,7 @@ func randHeader() (*types.Header, *rpcHeader) {
Nonce: types.BlockNonce{}, Nonce: types.BlockNonce{},
BaseFee: big.NewInt(100), BaseFee: big.NewInt(100),
} }
rhdr := &rpcHeader{ rhdr := &RPCHeader{
ParentHash: hdr.ParentHash, ParentHash: hdr.ParentHash,
UncleHash: hdr.UncleHash, UncleHash: hdr.UncleHash,
Coinbase: hdr.Coinbase, Coinbase: hdr.Coinbase,
...@@ -108,9 +108,9 @@ func TestEthClient_InfoByHash(t *testing.T) { ...@@ -108,9 +108,9 @@ func TestEthClient_InfoByHash(t *testing.T) {
_, rhdr := randHeader() _, rhdr := randHeader()
expectedInfo, _ := rhdr.Info(true, false) expectedInfo, _ := rhdr.Info(true, false)
ctx := context.Background() ctx := context.Background()
m.On("CallContext", ctx, new(*rpcHeader), m.On("CallContext", ctx, new(*RPCHeader),
"eth_getBlockByHash", []any{rhdr.Hash, false}).Run(func(args mock.Arguments) { "eth_getBlockByHash", []any{rhdr.Hash, false}).Run(func(args mock.Arguments) {
*args[1].(**rpcHeader) = rhdr *args[1].(**RPCHeader) = rhdr
}).Return([]error{nil}) }).Return([]error{nil})
s, err := NewEthClient(m, nil, nil, testEthClientConfig) s, err := NewEthClient(m, nil, nil, testEthClientConfig)
require.NoError(t, err) require.NoError(t, err)
...@@ -131,9 +131,9 @@ func TestEthClient_InfoByNumber(t *testing.T) { ...@@ -131,9 +131,9 @@ func TestEthClient_InfoByNumber(t *testing.T) {
expectedInfo, _ := rhdr.Info(true, false) expectedInfo, _ := rhdr.Info(true, false)
n := rhdr.Number n := rhdr.Number
ctx := context.Background() ctx := context.Background()
m.On("CallContext", ctx, new(*rpcHeader), m.On("CallContext", ctx, new(*RPCHeader),
"eth_getBlockByNumber", []any{n.String(), false}).Run(func(args mock.Arguments) { "eth_getBlockByNumber", []any{n.String(), false}).Run(func(args mock.Arguments) {
*args[1].(**rpcHeader) = rhdr *args[1].(**RPCHeader) = rhdr
}).Return([]error{nil}) }).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard)) s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err) require.NoError(t, err)
...@@ -150,9 +150,9 @@ func TestEthClient_WrongInfoByNumber(t *testing.T) { ...@@ -150,9 +150,9 @@ func TestEthClient_WrongInfoByNumber(t *testing.T) {
rhdr2.Number += 1 rhdr2.Number += 1
n := rhdr.Number n := rhdr.Number
ctx := context.Background() ctx := context.Background()
m.On("CallContext", ctx, new(*rpcHeader), m.On("CallContext", ctx, new(*RPCHeader),
"eth_getBlockByNumber", []any{n.String(), false}).Run(func(args mock.Arguments) { "eth_getBlockByNumber", []any{n.String(), false}).Run(func(args mock.Arguments) {
*args[1].(**rpcHeader) = &rhdr2 *args[1].(**RPCHeader) = &rhdr2
}).Return([]error{nil}) }).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard)) s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err) require.NoError(t, err)
...@@ -169,9 +169,9 @@ func TestEthClient_WrongInfoByHash(t *testing.T) { ...@@ -169,9 +169,9 @@ func TestEthClient_WrongInfoByHash(t *testing.T) {
rhdr2.Hash = rhdr2.computeBlockHash() rhdr2.Hash = rhdr2.computeBlockHash()
k := rhdr.Hash k := rhdr.Hash
ctx := context.Background() ctx := context.Background()
m.On("CallContext", ctx, new(*rpcHeader), m.On("CallContext", ctx, new(*RPCHeader),
"eth_getBlockByHash", []any{k, false}).Run(func(args mock.Arguments) { "eth_getBlockByHash", []any{k, false}).Run(func(args mock.Arguments) {
*args[1].(**rpcHeader) = &rhdr2 *args[1].(**RPCHeader) = &rhdr2
}).Return([]error{nil}) }).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard)) s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err) require.NoError(t, err)
...@@ -235,10 +235,10 @@ func testEthClient_validateReceipts(t *testing.T, test validateReceiptsTest) { ...@@ -235,10 +235,10 @@ func testEthClient_validateReceipts(t *testing.T, test validateReceiptsTest) {
receipts = mut(receipts) receipts = mut(receipts)
} }
mrpc.On("CallContext", ctx, mock.AnythingOfType("**sources.rpcBlock"), mrpc.On("CallContext", ctx, mock.AnythingOfType("**sources.RPCBlock"),
"eth_getBlockByHash", []any{block.Hash, true}). "eth_getBlockByHash", []any{block.Hash, true}).
Run(func(args mock.Arguments) { Run(func(args mock.Arguments) {
*(args[1].(**rpcBlock)) = block *(args[1].(**RPCBlock)) = block
}). }).
Return([]error{nil}).Once() Return([]error{nil}).Once()
......
...@@ -130,7 +130,7 @@ func TestBasicRPCReceiptsFetcher_Concurrency(t *testing.T) { ...@@ -130,7 +130,7 @@ func TestBasicRPCReceiptsFetcher_Concurrency(t *testing.T) {
require.Less(finalNumCalls, numFetchers, "Some IterativeBatchCalls should have been shared.") require.Less(finalNumCalls, numFetchers, "Some IterativeBatchCalls should have been shared.")
} }
func runConcurrentFetchingTest(t *testing.T, rp ReceiptsProvider, numFetchers int, receipts types.Receipts, block *rpcBlock) { func runConcurrentFetchingTest(t *testing.T, rp ReceiptsProvider, numFetchers int, receipts types.Receipts, block *RPCBlock) {
require := require.New(t) require := require.New(t)
txHashes := receiptTxHashes(receipts) txHashes := receiptTxHashes(receipts)
......
...@@ -27,9 +27,9 @@ type ethBackend struct { ...@@ -27,9 +27,9 @@ type ethBackend struct {
*mock.Mock *mock.Mock
} }
func (b *ethBackend) GetBlockByHash(id common.Hash, fullTxs bool) (*rpcBlock, error) { func (b *ethBackend) GetBlockByHash(id common.Hash, fullTxs bool) (*RPCBlock, error) {
out := b.Mock.MethodCalled("eth_getBlockByHash", id, fullTxs) out := b.Mock.MethodCalled("eth_getBlockByHash", id, fullTxs)
return out[0].(*rpcBlock), nil return out[0].(*RPCBlock), nil
} }
func (b *ethBackend) GetTransactionReceipt(txHash common.Hash) (*types.Receipt, error) { func (b *ethBackend) GetTransactionReceipt(txHash common.Hash) (*types.Receipt, error) {
...@@ -98,7 +98,7 @@ type ReceiptsTestCase struct { ...@@ -98,7 +98,7 @@ type ReceiptsTestCase struct {
name string name string
providerKind RPCProviderKind providerKind RPCProviderKind
staticMethod bool staticMethod bool
setup func(t *testing.T) (*rpcBlock, []ReceiptsRequest) setup func(t *testing.T) (*RPCBlock, []ReceiptsRequest)
} }
func (tc *ReceiptsTestCase) Run(t *testing.T) { func (tc *ReceiptsTestCase) Run(t *testing.T) {
...@@ -185,10 +185,10 @@ func (tc *ReceiptsTestCase) Run(t *testing.T) { ...@@ -185,10 +185,10 @@ func (tc *ReceiptsTestCase) Run(t *testing.T) {
m.AssertExpectations(t) m.AssertExpectations(t)
} }
func randomRpcBlockAndReceipts(rng *rand.Rand, txCount uint64) (*rpcBlock, []*types.Receipt) { func randomRpcBlockAndReceipts(rng *rand.Rand, txCount uint64) (*RPCBlock, []*types.Receipt) {
block, receipts := testutils.RandomBlock(rng, txCount) block, receipts := testutils.RandomBlock(rng, txCount)
return &rpcBlock{ return &RPCBlock{
rpcHeader: rpcHeader{ RPCHeader: RPCHeader{
ParentHash: block.ParentHash(), ParentHash: block.ParentHash(),
UncleHash: block.UncleHash(), UncleHash: block.UncleHash(),
Coinbase: block.Coinbase(), Coinbase: block.Coinbase(),
...@@ -214,8 +214,8 @@ func randomRpcBlockAndReceipts(rng *rand.Rand, txCount uint64) (*rpcBlock, []*ty ...@@ -214,8 +214,8 @@ func randomRpcBlockAndReceipts(rng *rand.Rand, txCount uint64) (*rpcBlock, []*ty
func TestEthClient_FetchReceipts(t *testing.T) { func TestEthClient_FetchReceipts(t *testing.T) {
// Helper to quickly define the test case requests scenario: // Helper to quickly define the test case requests scenario:
// each method fails to fetch the receipts, except the last // each method fails to fetch the receipts, except the last
fallbackCase := func(txCount uint64, methods ...ReceiptsFetchingMethod) func(t *testing.T) (*rpcBlock, []ReceiptsRequest) { fallbackCase := func(txCount uint64, methods ...ReceiptsFetchingMethod) func(t *testing.T) (*RPCBlock, []ReceiptsRequest) {
return func(t *testing.T) (*rpcBlock, []ReceiptsRequest) { return func(t *testing.T) (*RPCBlock, []ReceiptsRequest) {
block, receipts := randomRpcBlockAndReceipts(rand.New(rand.NewSource(123)), txCount) block, receipts := randomRpcBlockAndReceipts(rand.New(rand.NewSource(123)), txCount)
// zero out the data we don't want to verify // zero out the data we don't want to verify
for _, r := range receipts { for _, r := range receipts {
......
...@@ -98,7 +98,7 @@ func (h headerInfo) HeaderRLP() ([]byte, error) { ...@@ -98,7 +98,7 @@ func (h headerInfo) HeaderRLP() ([]byte, error) {
return rlp.EncodeToBytes(h.Header) return rlp.EncodeToBytes(h.Header)
} }
type rpcHeader struct { type RPCHeader struct {
ParentHash common.Hash `json:"parentHash"` ParentHash common.Hash `json:"parentHash"`
UncleHash common.Hash `json:"sha3Uncles"` UncleHash common.Hash `json:"sha3Uncles"`
Coinbase common.Address `json:"miner"` Coinbase common.Address `json:"miner"`
...@@ -136,7 +136,7 @@ type rpcHeader struct { ...@@ -136,7 +136,7 @@ type rpcHeader struct {
// checkPostMerge checks that the block header meets all criteria to be a valid ExecutionPayloadHeader, // checkPostMerge checks that the block header meets all criteria to be a valid ExecutionPayloadHeader,
// see EIP-3675 (block header changes) and EIP-4399 (mixHash usage for prev-randao) // see EIP-3675 (block header changes) and EIP-4399 (mixHash usage for prev-randao)
func (hdr *rpcHeader) checkPostMerge() error { func (hdr *RPCHeader) checkPostMerge() error {
// TODO: the genesis block has a non-zero difficulty number value. // TODO: the genesis block has a non-zero difficulty number value.
// Either this block needs to change, or we special case it. This is not valid w.r.t. EIP-3675. // Either this block needs to change, or we special case it. This is not valid w.r.t. EIP-3675.
if hdr.Number != 0 && (*big.Int)(&hdr.Difficulty).Cmp(common.Big0) != 0 { if hdr.Number != 0 && (*big.Int)(&hdr.Difficulty).Cmp(common.Big0) != 0 {
...@@ -157,12 +157,12 @@ func (hdr *rpcHeader) checkPostMerge() error { ...@@ -157,12 +157,12 @@ func (hdr *rpcHeader) checkPostMerge() error {
return nil return nil
} }
func (hdr *rpcHeader) computeBlockHash() common.Hash { func (hdr *RPCHeader) computeBlockHash() common.Hash {
gethHeader := hdr.createGethHeader() gethHeader := hdr.createGethHeader()
return gethHeader.Hash() return gethHeader.Hash()
} }
func (hdr *rpcHeader) createGethHeader() *types.Header { func (hdr *RPCHeader) createGethHeader() *types.Header {
return &types.Header{ return &types.Header{
ParentHash: hdr.ParentHash, ParentHash: hdr.ParentHash,
UncleHash: hdr.UncleHash, UncleHash: hdr.UncleHash,
...@@ -188,7 +188,7 @@ func (hdr *rpcHeader) createGethHeader() *types.Header { ...@@ -188,7 +188,7 @@ func (hdr *rpcHeader) createGethHeader() *types.Header {
} }
} }
func (hdr *rpcHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo, error) { func (hdr *RPCHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo, error) {
if mustBePostMerge { if mustBePostMerge {
if err := hdr.checkPostMerge(); err != nil { if err := hdr.checkPostMerge(); err != nil {
return nil, err return nil, err
...@@ -202,20 +202,20 @@ func (hdr *rpcHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo ...@@ -202,20 +202,20 @@ func (hdr *rpcHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo
return &headerInfo{hdr.Hash, hdr.createGethHeader()}, nil return &headerInfo{hdr.Hash, hdr.createGethHeader()}, nil
} }
func (hdr *rpcHeader) BlockID() eth.BlockID { func (hdr *RPCHeader) BlockID() eth.BlockID {
return eth.BlockID{ return eth.BlockID{
Hash: hdr.Hash, Hash: hdr.Hash,
Number: uint64(hdr.Number), Number: uint64(hdr.Number),
} }
} }
type rpcBlock struct { type RPCBlock struct {
rpcHeader RPCHeader
Transactions []*types.Transaction `json:"transactions"` Transactions []*types.Transaction `json:"transactions"`
Withdrawals *types.Withdrawals `json:"withdrawals,omitempty"` Withdrawals *types.Withdrawals `json:"withdrawals,omitempty"`
} }
func (block *rpcBlock) verify() error { func (block *RPCBlock) verify() error {
if computed := block.computeBlockHash(); computed != block.Hash { if computed := block.computeBlockHash(); computed != block.Hash {
return fmt.Errorf("failed to verify block hash: computed %s but RPC said %s", computed, block.Hash) return fmt.Errorf("failed to verify block hash: computed %s but RPC said %s", computed, block.Hash)
} }
...@@ -247,7 +247,7 @@ func (block *rpcBlock) verify() error { ...@@ -247,7 +247,7 @@ func (block *rpcBlock) verify() error {
return nil return nil
} }
func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo, types.Transactions, error) { func (block *RPCBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo, types.Transactions, error) {
if mustBePostMerge { if mustBePostMerge {
if err := block.checkPostMerge(); err != nil { if err := block.checkPostMerge(); err != nil {
return nil, nil, err return nil, nil, err
...@@ -260,7 +260,7 @@ func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInf ...@@ -260,7 +260,7 @@ func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInf
} }
// verify the header data // verify the header data
info, err := block.rpcHeader.Info(trustCache, mustBePostMerge) info, err := block.RPCHeader.Info(trustCache, mustBePostMerge)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed to verify block from RPC: %w", err) return nil, nil, fmt.Errorf("failed to verify block from RPC: %w", err)
} }
...@@ -268,7 +268,7 @@ func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInf ...@@ -268,7 +268,7 @@ func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInf
return info, block.Transactions, nil return info, block.Transactions, nil
} }
func (block *rpcBlock) ExecutionPayloadEnvelope(trustCache bool) (*eth.ExecutionPayloadEnvelope, error) { func (block *RPCBlock) ExecutionPayloadEnvelope(trustCache bool) (*eth.ExecutionPayloadEnvelope, error) {
if err := block.checkPostMerge(); err != nil { if err := block.checkPostMerge(); err != nil {
return nil, err return nil, err
} }
......
...@@ -42,7 +42,7 @@ func TestBlockHeaderJSON(t *testing.T) { ...@@ -42,7 +42,7 @@ func TestBlockHeaderJSON(t *testing.T) {
var metadata testMetadata var metadata testMetadata
readJsonTestdata(t, "testdata/data/headers/"+entry.Name(), &metadata) readJsonTestdata(t, "testdata/data/headers/"+entry.Name(), &metadata)
t.Run(metadata.Name, func(t *testing.T) { t.Run(metadata.Name, func(t *testing.T) {
var header rpcHeader var header RPCHeader
readJsonTestdata(t, "testdata/data/headers/"+strings.Replace(entry.Name(), "_metadata.json", "_data.json", 1), &header) readJsonTestdata(t, "testdata/data/headers/"+strings.Replace(entry.Name(), "_metadata.json", "_data.json", 1), &header)
h := header.computeBlockHash() h := header.computeBlockHash()
...@@ -67,7 +67,7 @@ func TestBlockJSON(t *testing.T) { ...@@ -67,7 +67,7 @@ func TestBlockJSON(t *testing.T) {
var metadata testMetadata var metadata testMetadata
readJsonTestdata(t, "testdata/data/blocks/"+entry.Name(), &metadata) readJsonTestdata(t, "testdata/data/blocks/"+entry.Name(), &metadata)
t.Run(metadata.Name, func(t *testing.T) { t.Run(metadata.Name, func(t *testing.T) {
var block rpcBlock var block RPCBlock
readJsonTestdata(t, "testdata/data/blocks/"+strings.Replace(entry.Name(), "_metadata.json", "_data.json", 1), &block) readJsonTestdata(t, "testdata/data/blocks/"+strings.Replace(entry.Name(), "_metadata.json", "_data.json", 1), &block)
err := block.verify() err := block.verify()
...@@ -106,7 +106,7 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) { ...@@ -106,7 +106,7 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) {
BlobGasUsed: &zero, BlobGasUsed: &zero,
ParentBeaconRoot: &common.Hash{}, ParentBeaconRoot: &common.Hash{},
} }
rhdr := rpcHeader{ rhdr := RPCHeader{
ParentBeaconRoot: hdr.ParentBeaconRoot, ParentBeaconRoot: hdr.ParentBeaconRoot,
ParentHash: hdr.ParentHash, ParentHash: hdr.ParentHash,
WithdrawalsRoot: hdr.WithdrawalsHash, WithdrawalsRoot: hdr.WithdrawalsHash,
...@@ -130,8 +130,8 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) { ...@@ -130,8 +130,8 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) {
ExcessBlobGas: (*hexutil.Uint64)(hdr.ExcessBlobGas), ExcessBlobGas: (*hexutil.Uint64)(hdr.ExcessBlobGas),
} }
block := rpcBlock{ block := RPCBlock{
rpcHeader: rhdr, RPCHeader: rhdr,
Transactions: types.Transactions{}, Transactions: types.Transactions{},
Withdrawals: &types.Withdrawals{}, Withdrawals: &types.Withdrawals{},
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment