Skip to content

Commit eff58ff

Browse files
committed
eth/downloader: add support for requests in downloader
1 parent a5186f3 commit eff58ff

File tree

7 files changed

+36
-15
lines changed

7 files changed

+36
-15
lines changed

core/types/block.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ func (h *Header) SanityCheck() error {
166166
// EmptyBody returns true if there is no additional 'body' to complete the header
167167
// that is: no transactions, no uncles and no withdrawals.
168168
func (h *Header) EmptyBody() bool {
169-
if h.WithdrawalsHash != nil {
170-
return h.TxHash == EmptyTxsHash && *h.WithdrawalsHash == EmptyWithdrawalsHash
171-
}
172-
return h.TxHash == EmptyTxsHash && h.UncleHash == EmptyUncleHash
169+
var (
170+
emptyWithdrawals = h.WithdrawalsHash == nil || *h.WithdrawalsHash == EmptyWithdrawalsHash
171+
emptyRequests = h.RequestsHash == nil || *h.RequestsHash == EmptyReceiptsHash
172+
)
173+
return h.TxHash == EmptyTxsHash && h.UncleHash == EmptyUncleHash && emptyWithdrawals && emptyRequests
173174
}
174175

175176
// EmptyReceipts returns true if there are no receipts for this header/block.

eth/downloader/downloader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et
230230
txsHashes = make([]common.Hash, len(bodies))
231231
uncleHashes = make([]common.Hash, len(bodies))
232232
withdrawalHashes = make([]common.Hash, len(bodies))
233+
requestsHashes = make([]common.Hash, len(bodies))
233234
)
234235
hasher := trie.NewStackTrie(nil)
235236
for i, body := range bodies {
@@ -248,7 +249,7 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et
248249
res := &eth.Response{
249250
Req: req,
250251
Res: (*eth.BlockBodiesResponse)(&bodies),
251-
Meta: [][]common.Hash{txsHashes, uncleHashes, withdrawalHashes},
252+
Meta: [][]common.Hash{txsHashes, uncleHashes, withdrawalHashes, requestsHashes},
252253
Time: 1,
253254
Done: make(chan error, 1), // Ignore the returned status
254255
}

eth/downloader/fetchers_concurrent_bodies.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ func (q *bodyQueue) request(peer *peerConnection, req *fetchRequest, resCh chan
8888
// deliver is responsible for taking a generic response packet from the concurrent
8989
// fetcher, unpacking the body data and delivering it to the downloader's queue.
9090
func (q *bodyQueue) deliver(peer *peerConnection, packet *eth.Response) (int, error) {
91-
txs, uncles, withdrawals := packet.Res.(*eth.BlockBodiesResponse).Unpack()
92-
hashsets := packet.Meta.([][]common.Hash) // {txs hashes, uncle hashes, withdrawal hashes}
91+
txs, uncles, withdrawals, requests := packet.Res.(*eth.BlockBodiesResponse).Unpack()
92+
hashsets := packet.Meta.([][]common.Hash) // {txs hashes, uncle hashes, withdrawal hashes, requests hashes}
9393

94-
accepted, err := q.queue.DeliverBodies(peer.id, txs, hashsets[0], uncles, hashsets[1], withdrawals, hashsets[2])
94+
accepted, err := q.queue.DeliverBodies(peer.id, txs, hashsets[0], uncles, hashsets[1], withdrawals, hashsets[2], requests, hashsets[3])
9595
switch {
9696
case err == nil && len(txs) == 0:
9797
peer.log.Trace("Requested bodies delivered")

eth/downloader/queue.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ func (q *queue) DeliverHeaders(id string, headers []*types.Header, hashes []comm
784784
// also wakes any threads waiting for data delivery.
785785
func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, txListHashes []common.Hash,
786786
uncleLists [][]*types.Header, uncleListHashes []common.Hash,
787-
withdrawalLists [][]*types.Withdrawal, withdrawalListHashes []common.Hash) (int, error) {
787+
withdrawalLists [][]*types.Withdrawal, withdrawalListHashes []common.Hash,
788+
requestsLists [][]*types.Request, requestsListHashes []common.Hash) (int, error) {
788789
q.lock.Lock()
789790
defer q.lock.Unlock()
790791

@@ -808,6 +809,19 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, txListH
808809
return errInvalidBody
809810
}
810811
}
812+
if header.RequestsHash == nil {
813+
// nil hash means that requests should not be present in body
814+
if requestsLists[index] != nil {
815+
return errInvalidBody
816+
}
817+
} else { // non-nil hash: body must have requests
818+
if requestsLists[index] == nil {
819+
return errInvalidBody
820+
}
821+
if requestsListHashes[index] != *header.RequestsHash {
822+
return errInvalidBody
823+
}
824+
}
811825
// Blocks must have a number of blobs corresponding to the header gas usage,
812826
// and zero before the Cancun hardfork.
813827
var blobs int

eth/downloader/queue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func XTestDelivery(t *testing.T) {
341341
uncleHashes[i] = types.CalcUncleHash(uncles)
342342
}
343343
time.Sleep(100 * time.Millisecond)
344-
_, err := q.DeliverBodies(peer.id, txset, txsHashes, uncleset, uncleHashes, nil, nil)
344+
_, err := q.DeliverBodies(peer.id, txset, txsHashes, uncleset, uncleHashes, nil, nil, nil, nil)
345345
if err != nil {
346346
fmt.Printf("delivered %d bodies %v\n", len(txset), err)
347347
}

eth/protocols/eth/handlers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ func handleBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
316316
txsHashes = make([]common.Hash, len(res.BlockBodiesResponse))
317317
uncleHashes = make([]common.Hash, len(res.BlockBodiesResponse))
318318
withdrawalHashes = make([]common.Hash, len(res.BlockBodiesResponse))
319+
requestsHashes = make([]common.Hash, len(res.BlockBodiesResponse))
319320
)
320321
hasher := trie.NewStackTrie(nil)
321322
for i, body := range res.BlockBodiesResponse {
@@ -324,8 +325,11 @@ func handleBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
324325
if body.Withdrawals != nil {
325326
withdrawalHashes[i] = types.DeriveSha(types.Withdrawals(body.Withdrawals), hasher)
326327
}
328+
if body.Requests != nil {
329+
requestsHashes[i] = types.DeriveSha(types.Requests(body.Requests), hasher)
330+
}
327331
}
328-
return [][]common.Hash{txsHashes, uncleHashes, withdrawalHashes}
332+
return [][]common.Hash{txsHashes, uncleHashes, withdrawalHashes, requestsHashes}
329333
}
330334
return peer.dispatchResponse(&Response{
331335
id: res.RequestId,

eth/protocols/eth/protocol.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,22 @@ type BlockBody struct {
224224
Transactions []*types.Transaction // Transactions contained within a block
225225
Uncles []*types.Header // Uncles contained within a block
226226
Withdrawals []*types.Withdrawal `rlp:"optional"` // Withdrawals contained within a block
227+
Requests []*types.Request `rlp:"optional"` // Requests contained within a block
227228
}
228229

229230
// Unpack retrieves the transactions and uncles from the range packet and returns
230231
// them in a split flat format that's more consistent with the internal data structures.
231-
func (p *BlockBodiesResponse) Unpack() ([][]*types.Transaction, [][]*types.Header, [][]*types.Withdrawal) {
232-
// TODO(matt): add support for withdrawals to fetchers
232+
func (p *BlockBodiesResponse) Unpack() ([][]*types.Transaction, [][]*types.Header, [][]*types.Withdrawal, [][]*types.Request) {
233233
var (
234234
txset = make([][]*types.Transaction, len(*p))
235235
uncleset = make([][]*types.Header, len(*p))
236236
withdrawalset = make([][]*types.Withdrawal, len(*p))
237+
requestset = make([][]*types.Request, len(*p))
237238
)
238239
for i, body := range *p {
239-
txset[i], uncleset[i], withdrawalset[i] = body.Transactions, body.Uncles, body.Withdrawals
240+
txset[i], uncleset[i], withdrawalset[i], requestset[i] = body.Transactions, body.Uncles, body.Withdrawals, body.Requests
240241
}
241-
return txset, uncleset, withdrawalset
242+
return txset, uncleset, withdrawalset, requestset
242243
}
243244

244245
// GetReceiptsRequest represents a block receipts query.

0 commit comments

Comments
 (0)