Commit 2677d5e
feat(p2p): use BlocksByRange for long-range sync (#351)
## 🗒️ Description / Motivation
This PR closes #347 by wiring the `BlocksByRange` protocol added in #348
into the status-response sync path.
Previously, when a peer's `Status` response revealed it was ahead of our
local head, we had no mechanism to backfill the gap. Now, when the gap
exceeds a configurable threshold (`LONG_RANGE_SYNC_THRESHOLD = 2
slots`), we request the missing range using `BlocksByRange` instead of
relying on gossip or individual `BlocksByRoot` fetches.
For small gaps (1–2 slots), we defer to the existing `FetchBlock` path
since roots are typically already available from gossip and
`BlocksByRoot` is more precise for that case.
---
## What Changed
**`lib.rs`**
- Added `LONG_RANGE_SYNC_THRESHOLD: u64 = 2` constant
**`req_resp/handlers.rs`**
- Updated `handle_status_response` to branch on gap size:
- `gap > LONG_RANGE_SYNC_THRESHOLD` →
`request_blocks_by_range_from_peer`
- `gap ≤ threshold` → defers to gossip / `FetchBlock`
---
## Correctness / Behavior Guarantees
- `request_blocks_by_range_from_peer` already batches internally at
`MAX_REQUEST_BLOCKS` (1024), so nodes thousands of slots behind are
handled correctly across multiple requests with no additional changes
- `handle_blocks_by_range_response` (added in #348) already forwards
each block to the blockchain layer — the response path is complete
- `BlocksByRoot` behavior for individual missing blocks (`FetchBlock`,
retry/backoff logic) is unchanged
---
## Tests Added / Run
No new tests required. The range response handling and canonical block
selection are covered by the test added in #348
(`blocks_by_range_returns_canonical_blocks_in_requested_order`).
## Related Issues / PRs
- Closes [Use BlocksByRange for long range syncing
#347](#347)
- Depends on / builds on [feat(p2p): add inbound BlocksByRange req/resp
support #348](#348)
- Related to [feat: add BlocksByRange req/resp protocol
leanEthereum/leanSpec#691](leanEthereum/leanSpec#691)
## ✅ Verification Checklist
- [x] Ran `make fmt` — clean
- [x] Ran `make lint` (clippy with `-D warnings`) — clean
- [x] Ran `cargo test --workspace --release` — all passing
---------
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: Tomás Grüner <47506558+MegaRedHand@users.noreply.github.com>
Co-authored-by: Pablo Deymonnaz <pdeymon@fi.uba.ar>1 parent e9fa1f8 commit 2677d5e
2 files changed
Lines changed: 346 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
68 | 138 | | |
69 | 139 | | |
70 | 140 | | |
| |||
300 | 370 | | |
301 | 371 | | |
302 | 372 | | |
303 | | - | |
304 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
305 | 376 | | |
306 | 377 | | |
307 | 378 | | |
| |||
336 | 407 | | |
337 | 408 | | |
338 | 409 | | |
339 | | - | |
340 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
341 | 413 | | |
342 | 414 | | |
343 | 415 | | |
| |||
371 | 443 | | |
372 | 444 | | |
373 | 445 | | |
374 | | - | |
| 446 | + | |
375 | 447 | | |
376 | 448 | | |
377 | 449 | | |
| |||
380 | 452 | | |
381 | 453 | | |
382 | 454 | | |
383 | | - | |
| 455 | + | |
384 | 456 | | |
385 | 457 | | |
386 | 458 | | |
| |||
436 | 508 | | |
437 | 509 | | |
438 | 510 | | |
439 | | - | |
| 511 | + | |
440 | 512 | | |
441 | 513 | | |
442 | 514 | | |
| |||
719 | 791 | | |
720 | 792 | | |
721 | 793 | | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
722 | 842 | | |
723 | 843 | | |
724 | 844 | | |
| |||
0 commit comments