Skip to content

Commit 7d7ff42

Browse files
committed
fix: add reqeust id validation before the buffer allocation
1 parent d33276f commit 7d7ff42

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

eth/protocols/eth/handlers.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,7 @@ func handleReceipts70(backend Backend, msg Decoder, peer *Peer) error {
531531
return err
532532
}
533533
if res.LastBlockIncomplete {
534-
if err := peer.RequestPartialReceipts(res.RequestId); err != nil {
535-
return err
536-
}
537-
return nil
534+
return peer.RequestPartialReceipts(res.RequestId)
538535
}
539536

540537
// Assign buffers shared between list elements

eth/protocols/eth/peer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ func (p *Peer) RequestReceipts(hashes []common.Hash, sink chan *Response) (*Requ
357357
FirstBlockReceiptIndex: 0,
358358
},
359359
}
360+
p.requestedReceipts[id] = hashes
360361
} else {
361362
req = &Request{
362363
id: id,
@@ -373,7 +374,6 @@ func (p *Peer) RequestReceipts(hashes []common.Hash, sink chan *Response) (*Requ
373374
return nil, err
374375
}
375376

376-
p.requestedReceipts[id] = hashes
377377
return req, nil
378378
}
379379

@@ -406,6 +406,12 @@ func (p *Peer) RequestPartialReceipts(id uint64) error {
406406
// If the request is completed, it appends previously collected receipts.
407407
func (p *Peer) BufferReceiptsPacket(packet *ReceiptsPacket70) error {
408408
requestId := packet.RequestId
409+
410+
// Do not assign buffer to the response not requested
411+
if _, ok := p.requestedReceipts[requestId]; !ok {
412+
return fmt.Errorf("No partial receipt retreival in progress with id %d", requestId)
413+
}
414+
409415
if len(packet.List) == 0 {
410416
return nil
411417
}

eth/protocols/eth/peer_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,55 @@ func TestPartialReceipt(t *testing.T) {
211211
t.Fatalf("requestedReceipts should be cleared after delivery")
212212
}
213213
}
214+
215+
func TestPartialReceiptDisconnectRequestId(t *testing.T) {
216+
app, net := p2p.MsgPipe()
217+
var id enode.ID
218+
if _, err := rand.Read(id[:]); err != nil {
219+
t.Fatalf("failed to create random peer: %v", err)
220+
}
221+
222+
peer := NewPeer(ETH70, p2p.NewPeer(id, "peer", nil), net, nil)
223+
224+
packetCh := make(chan *GetReceiptsPacket70, 1)
225+
go func() {
226+
for {
227+
msg, err := app.ReadMsg()
228+
if err != nil {
229+
return
230+
}
231+
if msg.Code == GetReceiptsMsg {
232+
var pkt GetReceiptsPacket70
233+
if err := msg.Decode(&pkt); err == nil {
234+
select {
235+
case packetCh <- &pkt:
236+
default:
237+
}
238+
}
239+
}
240+
msg.Discard()
241+
}
242+
}()
243+
244+
// If a peer delivers response which is never requested, it should fail the validation
245+
delivery := &ReceiptsPacket70{
246+
RequestId: 66,
247+
LastBlockIncomplete: true,
248+
List: []*ReceiptList69{
249+
{
250+
items: []Receipt{
251+
{GasUsed: 21_000, Logs: rlp.RawValue(make([]byte, 1))},
252+
},
253+
},
254+
{
255+
items: []Receipt{
256+
{GasUsed: 21_000, Logs: rlp.RawValue(make([]byte, 2))},
257+
},
258+
},
259+
},
260+
}
261+
err := peer.BufferReceiptsPacket(delivery)
262+
if err == nil {
263+
t.Fatal("Unknown response should be dropped")
264+
}
265+
}

eth/protocols/eth/receipt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ func (rl *ReceiptList69) EncodeRLP(_w io.Writer) error {
403403
return w.Flush()
404404
}
405405

406+
// Append appends all items from another ReceiptList69 to this list.
407+
func (rl *ReceiptList69) Append(other *ReceiptList69) {
408+
rl.items = append(rl.items, other.items...)
409+
}
410+
406411
// blockReceiptsToNetwork69 takes a slice of rlp-encoded receipts, and transactions,
407412
// and applies the type-encoding on the receipts (for non-legacy receipts).
408413
// e.g. for non-legacy receipts: receipt-data -> {tx-type || receipt-data}

0 commit comments

Comments
 (0)