eth/protocols: put empty list as a placeholder for unavailable responses

This commit is contained in:
healthykim 2026-04-17 11:09:50 +02:00
parent 765a37f426
commit 92cef0a740
2 changed files with 20 additions and 13 deletions

View file

@ -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())

View file

@ -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 {