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 {
}
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
if err != nil {
return nil, err
......@@ -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) {
var block *rpcBlock
var block *RPCBlock
err := s.client.CallContext(ctx, &block, method, id.Arg(), true)
if err != nil {
return nil, nil, err
......@@ -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) {
var block *rpcBlock
var block *RPCBlock
err := s.client.CallContext(ctx, &block, method, id.Arg(), true)
if err != nil {
return nil, err
......
......@@ -62,7 +62,7 @@ func randHash() (out common.Hash) {
return out
}
func randHeader() (*types.Header, *rpcHeader) {
func randHeader() (*types.Header, *RPCHeader) {
hdr := &types.Header{
ParentHash: randHash(),
UncleHash: randHash(),
......@@ -81,7 +81,7 @@ func randHeader() (*types.Header, *rpcHeader) {
Nonce: types.BlockNonce{},
BaseFee: big.NewInt(100),
}
rhdr := &rpcHeader{
rhdr := &RPCHeader{
ParentHash: hdr.ParentHash,
UncleHash: hdr.UncleHash,
Coinbase: hdr.Coinbase,
......@@ -108,9 +108,9 @@ func TestEthClient_InfoByHash(t *testing.T) {
_, rhdr := randHeader()
expectedInfo, _ := rhdr.Info(true, false)
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) {
*args[1].(**rpcHeader) = rhdr
*args[1].(**RPCHeader) = rhdr
}).Return([]error{nil})
s, err := NewEthClient(m, nil, nil, testEthClientConfig)
require.NoError(t, err)
......@@ -131,9 +131,9 @@ func TestEthClient_InfoByNumber(t *testing.T) {
expectedInfo, _ := rhdr.Info(true, false)
n := rhdr.Number
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) {
*args[1].(**rpcHeader) = rhdr
*args[1].(**RPCHeader) = rhdr
}).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err)
......@@ -150,9 +150,9 @@ func TestEthClient_WrongInfoByNumber(t *testing.T) {
rhdr2.Number += 1
n := rhdr.Number
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) {
*args[1].(**rpcHeader) = &rhdr2
*args[1].(**RPCHeader) = &rhdr2
}).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err)
......@@ -169,9 +169,9 @@ func TestEthClient_WrongInfoByHash(t *testing.T) {
rhdr2.Hash = rhdr2.computeBlockHash()
k := rhdr.Hash
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) {
*args[1].(**rpcHeader) = &rhdr2
*args[1].(**RPCHeader) = &rhdr2
}).Return([]error{nil})
s, err := NewL1Client(m, nil, nil, L1ClientDefaultConfig(&rollup.Config{SeqWindowSize: 10}, true, RPCKindStandard))
require.NoError(t, err)
......@@ -235,10 +235,10 @@ func testEthClient_validateReceipts(t *testing.T, test validateReceiptsTest) {
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}).
Run(func(args mock.Arguments) {
*(args[1].(**rpcBlock)) = block
*(args[1].(**RPCBlock)) = block
}).
Return([]error{nil}).Once()
......
......@@ -130,7 +130,7 @@ func TestBasicRPCReceiptsFetcher_Concurrency(t *testing.T) {
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)
txHashes := receiptTxHashes(receipts)
......
......@@ -27,9 +27,9 @@ type ethBackend struct {
*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)
return out[0].(*rpcBlock), nil
return out[0].(*RPCBlock), nil
}
func (b *ethBackend) GetTransactionReceipt(txHash common.Hash) (*types.Receipt, error) {
......@@ -98,7 +98,7 @@ type ReceiptsTestCase struct {
name string
providerKind RPCProviderKind
staticMethod bool
setup func(t *testing.T) (*rpcBlock, []ReceiptsRequest)
setup func(t *testing.T) (*RPCBlock, []ReceiptsRequest)
}
func (tc *ReceiptsTestCase) Run(t *testing.T) {
......@@ -185,10 +185,10 @@ func (tc *ReceiptsTestCase) Run(t *testing.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)
return &rpcBlock{
rpcHeader: rpcHeader{
return &RPCBlock{
RPCHeader: RPCHeader{
ParentHash: block.ParentHash(),
UncleHash: block.UncleHash(),
Coinbase: block.Coinbase(),
......@@ -214,8 +214,8 @@ func randomRpcBlockAndReceipts(rng *rand.Rand, txCount uint64) (*rpcBlock, []*ty
func TestEthClient_FetchReceipts(t *testing.T) {
// Helper to quickly define the test case requests scenario:
// each method fails to fetch the receipts, except the last
fallbackCase := func(txCount uint64, methods ...ReceiptsFetchingMethod) func(t *testing.T) (*rpcBlock, []ReceiptsRequest) {
return 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) {
block, receipts := randomRpcBlockAndReceipts(rand.New(rand.NewSource(123)), txCount)
// zero out the data we don't want to verify
for _, r := range receipts {
......
......@@ -98,7 +98,7 @@ func (h headerInfo) HeaderRLP() ([]byte, error) {
return rlp.EncodeToBytes(h.Header)
}
type rpcHeader struct {
type RPCHeader struct {
ParentHash common.Hash `json:"parentHash"`
UncleHash common.Hash `json:"sha3Uncles"`
Coinbase common.Address `json:"miner"`
......@@ -136,7 +136,7 @@ type rpcHeader struct {
// 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)
func (hdr *rpcHeader) checkPostMerge() error {
func (hdr *RPCHeader) checkPostMerge() error {
// 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.
if hdr.Number != 0 && (*big.Int)(&hdr.Difficulty).Cmp(common.Big0) != 0 {
......@@ -157,12 +157,12 @@ func (hdr *rpcHeader) checkPostMerge() error {
return nil
}
func (hdr *rpcHeader) computeBlockHash() common.Hash {
func (hdr *RPCHeader) computeBlockHash() common.Hash {
gethHeader := hdr.createGethHeader()
return gethHeader.Hash()
}
func (hdr *rpcHeader) createGethHeader() *types.Header {
func (hdr *RPCHeader) createGethHeader() *types.Header {
return &types.Header{
ParentHash: hdr.ParentHash,
UncleHash: hdr.UncleHash,
......@@ -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 err := hdr.checkPostMerge(); err != nil {
return nil, err
......@@ -202,20 +202,20 @@ func (hdr *rpcHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo
return &headerInfo{hdr.Hash, hdr.createGethHeader()}, nil
}
func (hdr *rpcHeader) BlockID() eth.BlockID {
func (hdr *RPCHeader) BlockID() eth.BlockID {
return eth.BlockID{
Hash: hdr.Hash,
Number: uint64(hdr.Number),
}
}
type rpcBlock struct {
rpcHeader
type RPCBlock struct {
RPCHeader
Transactions []*types.Transaction `json:"transactions"`
Withdrawals *types.Withdrawals `json:"withdrawals,omitempty"`
}
func (block *rpcBlock) verify() error {
func (block *RPCBlock) verify() error {
if computed := block.computeBlockHash(); 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 {
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 err := block.checkPostMerge(); err != nil {
return nil, nil, err
......@@ -260,7 +260,7 @@ func (block *rpcBlock) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInf
}
// verify the header data
info, err := block.rpcHeader.Info(trustCache, mustBePostMerge)
info, err := block.RPCHeader.Info(trustCache, mustBePostMerge)
if err != nil {
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
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 {
return nil, err
}
......
......@@ -42,7 +42,7 @@ func TestBlockHeaderJSON(t *testing.T) {
var metadata testMetadata
readJsonTestdata(t, "testdata/data/headers/"+entry.Name(), &metadata)
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)
h := header.computeBlockHash()
......@@ -67,7 +67,7 @@ func TestBlockJSON(t *testing.T) {
var metadata testMetadata
readJsonTestdata(t, "testdata/data/blocks/"+entry.Name(), &metadata)
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)
err := block.verify()
......@@ -106,7 +106,7 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) {
BlobGasUsed: &zero,
ParentBeaconRoot: &common.Hash{},
}
rhdr := rpcHeader{
rhdr := RPCHeader{
ParentBeaconRoot: hdr.ParentBeaconRoot,
ParentHash: hdr.ParentHash,
WithdrawalsRoot: hdr.WithdrawalsHash,
......@@ -130,8 +130,8 @@ func TestBlockToExecutionPayloadIncludesEcotoneProperties(t *testing.T) {
ExcessBlobGas: (*hexutil.Uint64)(hdr.ExcessBlobGas),
}
block := rpcBlock{
rpcHeader: rhdr,
block := RPCBlock{
RPCHeader: rhdr,
Transactions: types.Transactions{},
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