From 3ec01ab1335a92da03d3806626f92133e969fd30 Mon Sep 17 00:00:00 2001 From: Shane Bammel Date: Wed, 7 Feb 2024 10:39:14 -0600 Subject: [PATCH] Fix missing uncles in new block downloader --- .../block_downloader.go | 2 +- .../eth1/eth1_chain_reader.go/chain_reader.go | 22 ++++++++++--- turbo/execution/eth1/eth1_utils/grpc.go | 32 ++++++++++++++++--- turbo/execution/eth1/eth1_utils/grpc_test.go | 5 ++- turbo/execution/eth1/inserters.go | 5 ++- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/turbo/engineapi/engine_block_downloader/block_downloader.go b/turbo/engineapi/engine_block_downloader/block_downloader.go index 83c0e4dfe..17b2e48ae 100644 --- a/turbo/engineapi/engine_block_downloader/block_downloader.go +++ b/turbo/engineapi/engine_block_downloader/block_downloader.go @@ -245,7 +245,7 @@ func (e *EngineBlockDownloader) insertHeadersAndBodies(tx kv.Tx, fromBlock uint6 if body == nil { return fmt.Errorf("missing body at block=%d", number) } - blocksBatch = append(blocksBatch, types.NewBlockFromStorage(hash, header, body.Transactions, nil, body.Withdrawals)) + blocksBatch = append(blocksBatch, types.NewBlockFromStorage(hash, header, body.Transactions, body.Uncles, body.Withdrawals)) if number%uint64(blockWrittenLogSize) == 0 { e.logger.Info("[insertHeadersAndBodies] Written blocks", "progress", number, "to", toBlock) } diff --git a/turbo/execution/eth1/eth1_chain_reader.go/chain_reader.go b/turbo/execution/eth1/eth1_chain_reader.go/chain_reader.go index 756352b7d..c21899d90 100644 --- a/turbo/execution/eth1/eth1_chain_reader.go/chain_reader.go +++ b/turbo/execution/eth1/eth1_chain_reader.go/chain_reader.go @@ -93,7 +93,11 @@ func (c ChainReaderWriterEth1) GetBlockByHash(hash libcommon.Hash) *types.Block if resp == nil || resp.Body == nil { return nil } - body := eth1_utils.ConvertRawBlockBodyFromRpc(resp.Body) + body, err := eth1_utils.ConvertRawBlockBodyFromRpc(resp.Body) + if err != nil { + log.Error("GetBlockByHash failed", "err", err) + return nil + } txs, err := types.DecodeTransactions(body.Transactions) if err != nil { log.Error("GetBlockByHash failed", "err", err) @@ -118,7 +122,11 @@ func (c ChainReaderWriterEth1) GetBlockByNumber(number uint64) *types.Block { if resp == nil || resp.Body == nil { return nil } - body := eth1_utils.ConvertRawBlockBodyFromRpc(resp.Body) + body, err := eth1_utils.ConvertRawBlockBodyFromRpc(resp.Body) + if err != nil { + log.Error("GetBlockByNumber failed", "err", err) + return nil + } txs, err := types.DecodeTransactions(body.Transactions) if err != nil { log.Error("GetBlockByNumber failed", "err", err) @@ -195,7 +203,10 @@ func (c ChainReaderWriterEth1) GetBodiesByHashes(hashes []libcommon.Hash) ([]*ty } ret := make([]*types.RawBody, len(resp.Bodies)) for i := range ret { - ret[i] = eth1_utils.ConvertRawBlockBodyFromRpc(resp.Bodies[i]) + ret[i], err = eth1_utils.ConvertRawBlockBodyFromRpc(resp.Bodies[i]) + if err != nil { + return nil, err + } } return ret, nil } @@ -210,7 +221,10 @@ func (c ChainReaderWriterEth1) GetBodiesByRange(start, count uint64) ([]*types.R } ret := make([]*types.RawBody, len(resp.Bodies)) for i := range ret { - ret[i] = eth1_utils.ConvertRawBlockBodyFromRpc(resp.Bodies[i]) + ret[i], err = eth1_utils.ConvertRawBlockBodyFromRpc(resp.Bodies[i]) + if err != nil { + return nil, err + } } return ret, nil } diff --git a/turbo/execution/eth1/eth1_utils/grpc.go b/turbo/execution/eth1/eth1_utils/grpc.go index 692e07553..738528883 100644 --- a/turbo/execution/eth1/eth1_utils/grpc.go +++ b/turbo/execution/eth1/eth1_utils/grpc.go @@ -65,7 +65,10 @@ func HeaderToHeaderRPC(header *types.Header) *execution.Header { } func HeadersToHeadersRPC(headers []*types.Header) []*execution.Header { - ret := []*execution.Header{} + if headers == nil { + return nil + } + ret := make([]*execution.Header, 0, len(headers)) for _, header := range headers { ret = append(ret, HeaderToHeaderRPC(header)) } @@ -136,6 +139,21 @@ func HeaderRpcToHeader(header *execution.Header) (*types.Header, error) { return h, nil } +func HeadersRpcToHeaders(headers []*execution.Header) ([]*types.Header, error) { + if headers == nil { + return nil, nil + } + out := make([]*types.Header, 0, len(headers)) + for _, h := range headers { + header, err := HeaderRpcToHeader(h) + if err != nil { + return nil, err + } + out = append(out, header) + } + return out, nil +} + func ConvertWithdrawalsFromRpc(in []*types2.Withdrawal) []*types.Withdrawal { if in == nil { return nil @@ -177,6 +195,7 @@ func ConvertRawBlockBodyToRpc(in *types.RawBody, blockNumber uint64, blockHash l BlockNumber: blockNumber, BlockHash: gointerfaces.ConvertHashToH256(blockHash), Transactions: in.Transactions, + Uncles: HeadersToHeadersRPC(in.Uncles), Withdrawals: ConvertWithdrawalsToRpc(in.Withdrawals), } } @@ -190,14 +209,19 @@ func ConvertRawBlockBodiesToRpc(in []*types.RawBody, blockNumbers []uint64, bloc return ret } -func ConvertRawBlockBodyFromRpc(in *execution.BlockBody) *types.RawBody { +func ConvertRawBlockBodyFromRpc(in *execution.BlockBody) (*types.RawBody, error) { if in == nil { - return nil + return nil, nil + } + uncles, err := HeadersRpcToHeaders(in.Uncles) + if err != nil { + return nil, err } return &types.RawBody{ Transactions: in.Transactions, + Uncles: uncles, Withdrawals: ConvertWithdrawalsFromRpc(in.Withdrawals), - } + }, nil } func ConvertBigIntFromRpc(in *types2.H256) *big.Int { diff --git a/turbo/execution/eth1/eth1_utils/grpc_test.go b/turbo/execution/eth1/eth1_utils/grpc_test.go index 25bf718bc..eeb684d50 100644 --- a/turbo/execution/eth1/eth1_utils/grpc_test.go +++ b/turbo/execution/eth1/eth1_utils/grpc_test.go @@ -76,7 +76,10 @@ func TestBlockRpcConversion(t *testing.T) { // body conversions rpcBlock := ConvertBlockToRPC(testBlock) - roundTripBody := ConvertRawBlockBodyFromRpc(rpcBlock.Body) + roundTripBody, err := ConvertRawBlockBodyFromRpc(rpcBlock.Body) + if err != nil { + panic(err) + } testBlockRaw := testBlock.RawBody() require.Greater(len(testBlockRaw.Transactions), 0) require.Greater(len(testBlockRaw.Uncles), 0) diff --git a/turbo/execution/eth1/inserters.go b/turbo/execution/eth1/inserters.go index c49a34de8..fef634858 100644 --- a/turbo/execution/eth1/inserters.go +++ b/turbo/execution/eth1/inserters.go @@ -28,7 +28,10 @@ func (e *EthereumExecutionModule) InsertBlocks(ctx context.Context, req *executi if err != nil { return nil, fmt.Errorf("ethereumExecutionModule.InsertBlocks: cannot convert headers: %s", err) } - body := eth1_utils.ConvertRawBlockBodyFromRpc(block.Body) + body, err := eth1_utils.ConvertRawBlockBodyFromRpc(block.Body) + if err != nil { + return nil, fmt.Errorf("ethereumExecutionModule.InsertBlocks: cannot convert body: %s", err) + } height := header.Number.Uint64() // Parent's total difficulty parentTd, err := rawdb.ReadTd(tx, header.ParentHash, height-1)