From 92cef0a740102ca81e646be5db78f4141067d55b Mon Sep 17 00:00:00 2001 From: healthykim Date: Fri, 17 Apr 2026 11:09:50 +0200 Subject: [PATCH] eth/protocols: put empty list as a placeholder for unavailable responses --- eth/protocols/eth/handler_test.go | 18 +++++++++++------- eth/protocols/eth/handlers.go | 15 +++++++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index e07a591904..52607155ba 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -422,7 +422,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) { {limit + 1, nil, nil, limit}, // No more than the possible block count should be returned {0, []common.Hash{backend.chain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable {0, []common.Hash{backend.chain.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable - {0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned + {0, []common.Hash{{}}, []bool{false}, 1}, // A non existent block should not be returned // Existing and non-existing blocks interleaved should not cause problems {0, []common.Hash{ @@ -433,7 +433,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) { {}, backend.chain.GetBlockByNumber(100).Hash(), {}, - }, []bool{false, true, false, true, false, true, false}, 3}, + }, []bool{false, true, false, true, false, true, false}, 7}, } // Run each of the tests and verify the results against the chain for i, tt := range tests { @@ -460,9 +460,13 @@ func testGetBlockBodies(t *testing.T, protocol uint) { } for j, hash := range tt.explicit { hashes = append(hashes, hash) - if tt.available[j] && len(bodies) < tt.expected { - block := backend.chain.GetBlockByHash(hash) - bodies = append(bodies, encodeBody(block)) + if len(bodies) < tt.expected { + if tt.available[j] { + block := backend.chain.GetBlockByHash(hash) + bodies = append(bodies, encodeBody(block)) + } else { + bodies = append(bodies, BlockBody{}) + } } } @@ -590,9 +594,9 @@ func testGetBlockReceipts(t *testing.T, protocol uint) { ) for i := uint64(0); i <= backend.chain.CurrentBlock().Number.Uint64(); i++ { if i == 1 { - // Insert a missing block hash to verify the server returns a nil placeholder + // Insert a missing block hash to verify the server returns an empty placeholder hashes = append(hashes, common.HexToHash("0xdeadbeef")) - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) } block := backend.chain.GetBlockByNumber(i) hashes = append(hashes, block.Hash()) diff --git a/eth/protocols/eth/handlers.go b/eth/protocols/eth/handlers.go index bf388a022c..4426a722cf 100644 --- a/eth/protocols/eth/handlers.go +++ b/eth/protocols/eth/handlers.go @@ -241,6 +241,9 @@ func ServiceGetBlockBodiesQuery(chain *core.BlockChain, query GetBlockBodiesRequ if data := chain.GetBodyRLP(hash); len(data) != 0 { bodies = append(bodies, data) bytes += len(data) + } else { + // Append an empty body [[], []] as placeholder for missing block + bodies = append(bodies, []byte{0xc2, 0xc0, 0xc0}) } } return bodies @@ -281,18 +284,18 @@ func ServiceGetReceiptsQuery69(chain *core.BlockChain, query GetReceiptsRequest) // Retrieve the requested block's receipts results := chain.GetReceiptsRLP(hash) if results == nil { - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } body := chain.GetBodyRLP(hash) if body == nil { - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } results, _, err := blockReceiptsToNetwork(results, body, receiptQueryParams{}) if err != nil { log.Error("Error in block receipts conversion", "hash", hash, "err", err) - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } receipts.AppendRaw(results) @@ -316,12 +319,12 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, } results := chain.GetReceiptsRLP(hash) if results == nil { - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } body := chain.GetBodyRLP(hash) if body == nil { - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } q := receiptQueryParams{sizeLimit: uint64(maxPacketSize - bytes)} @@ -331,7 +334,7 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, results, incomplete, err := blockReceiptsToNetwork(results, body, q) if err != nil { log.Error("Error in block receipts conversion", "hash", hash, "err", err) - receipts.Append(nil) + receipts.AppendRaw(rlp.EmptyList) continue } if results == nil {