From 9ee3e00c2c15cab83c6ce0b955cebae43c35e249 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 18:20:22 -0300 Subject: [PATCH 01/10] fix: skip a `headers` that does not start on the locator of our outstanding `getheaders` (#92) An announcement can come before the answer to our `getheaders`. elo read it as the answer, and ended the connection with `NotOnLocator`. An answer starts on a hash of our locator, so elo now skips a `headers` that does not. The answer, or the next `getheaders`, gets those headers again. A skip does not restart the headers timeout. --- ...aders-off-tip-after-sync-twice-then-answer | Bin 0 -> 32 bytes .../sync-low-work-regtest-second-break-second | Bin 0 -> 26 bytes src/net_processing.rs | 113 ++++++++++++------ 3 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 fuzz/corpus/machines/headers-off-tip-after-sync-twice-then-answer create mode 100644 fuzz/corpus/machines/sync-low-work-regtest-second-break-second diff --git a/fuzz/corpus/machines/headers-off-tip-after-sync-twice-then-answer b/fuzz/corpus/machines/headers-off-tip-after-sync-twice-then-answer new file mode 100644 index 0000000000000000000000000000000000000000..cd7097faf3208ba8e88ed4cd26a8dcf6272d7893 GIT binary patch literal 32 gcmZQzWMpJuU}E54WdH(BRwf{iixnis%?czK00bxiMgRZ+ literal 0 HcmV?d00001 diff --git a/fuzz/corpus/machines/sync-low-work-regtest-second-break-second b/fuzz/corpus/machines/sync-low-work-regtest-second-break-second new file mode 100644 index 0000000000000000000000000000000000000000..3847d65b6ae5a950ae8735025a881846d97c8d10 GIT binary patch literal 26 ecmYdbWMpJuU}E54{m;O_zzHU~SpPFIFaQ854+D= work_min); Download::Chain } + + fn locator(&self, chain: &crate::chain::Chain) -> Vec { + match self { + Download::Genesis(_) | Download::Chain => chain.locator(), + Download::Presync(presync) => vec![presync.last_hash()], + Download::Redownload(redownload) => vec![redownload.last_hash()], + Download::Branch(branch) => vec![branch.last_hash()], + Download::BranchPresync(sync) => vec![sync.presync.last_hash()], + Download::BranchRedownload(sync) => vec![sync.redownload.last_hash()], + } + } + + fn is_answered_by( + &self, + chain: &crate::chain::Chain, + headers: &crate::protocol::Headers, + ) -> bool { + headers + .first() + .is_none_or(|first| self.locator(chain).contains(&first.prev)) + } } impl<'a> Peer<'a> { @@ -464,15 +485,21 @@ impl<'a> Peer<'a> { let mut input = message.payload(); let headers = crate::protocol::Headers::decode(&mut input).map_err(PeerError::Headers)?; let next = match self.sync { - SyncState::Outstanding { download, .. } => receive_answer( - self.chain, - self.work_min, - &mut self.salt, - &mut self.branch_tip, - download, - headers, - clock, - )?, + SyncState::Outstanding { download, sent } => { + if !download.is_answered_by(self.chain, &headers) { + self.sync = SyncState::Outstanding { download, sent }; + return Ok((self, Received::NoReply)); + } + receive_answer( + self.chain, + self.work_min, + &mut self.salt, + &mut self.branch_tip, + download, + headers, + clock, + )? + } SyncState::Synced => receive_announcement( self.chain, self.work_min, @@ -588,15 +615,7 @@ impl SyncState { > { match self { SyncState::Due(download) => { - let locator = match &download { - Download::Genesis(_) | Download::Chain => chain.locator(), - Download::Presync(presync) => vec![presync.last_hash()], - Download::Redownload(redownload) => vec![redownload.last_hash()], - Download::Branch(branch) => vec![branch.last_hash()], - Download::BranchPresync(sync) => vec![sync.presync.last_hash()], - Download::BranchRedownload(sync) => vec![sync.redownload.last_hash()], - }; - let get_headers = crate::protocol::GetHeaders::new(locator); + let get_headers = crate::protocol::GetHeaders::new(download.locator(chain)); let sync = SyncState::Outstanding { download, sent: now, @@ -1032,11 +1051,12 @@ pub(crate) mod fuzz { seed!("headers-cut", "PeerRefused(Headers(Payload(UnexpectedEnd)))"), seed!("headers-cut-after-sync", "PeerRefused(Headers(Payload(UnexpectedEnd)))"), seed!("headers-empty-payload", "PeerRefused(Headers(Payload(UnexpectedEnd)))"), - seed!("headers-off-tip", "PeerRefused(Extend(NotOnLocator))"), - seed!("headers-off-tip-after-full", "PeerRefused(Extend(NotOnLocator))"), + seed!("headers-off-tip", "PeerOpen { height: 0, synced: false }"), + seed!("headers-off-tip-after-full", "PeerOpen { height: 2000, synced: false }"), seed!("headers-off-tip-after-sync", "PeerOpen { height: 0, synced: false }"), seed!("headers-off-tip-after-sync-then-headers", "PeerOpen { height: 1, synced: true }"), - seed!("headers-off-tip-after-sync-twice", "PeerRefused(Extend(NotOnLocator))"), + seed!("headers-off-tip-after-sync-twice", "PeerOpen { height: 0, synced: false }"), + seed!("headers-off-tip-after-sync-twice-then-answer", "PeerOpen { height: 1, synced: true }"), seed!("headers-wrong-index-past-end", "PeerOpen { height: 2, synced: true }"), seed!("inv-block-after-sync", "PeerOpen { height: 2, synced: false }"), seed!("inv-block-after-sync-then-headers", "PeerOpen { height: 3, synced: true }"), @@ -1068,13 +1088,14 @@ pub(crate) mod fuzz { seed!("sync-full-then-short", "PeerOpen { height: 2002, synced: true }"), seed!("sync-longest-short-byte", "PeerOpen { height: 252, synced: true }"), seed!("sync-low-work-full-mainnet", "PeerRefused(HeadersSync(ProofOfWork))"), - seed!("sync-low-work-full-off-tip-signet", "PeerRefused(HeadersSync(NotContinuous))"), + seed!("sync-low-work-full-off-tip-signet", "PeerOpen { height: 0, synced: false }"), seed!("sync-low-work-mainnet", "PeerRefused(ChainWorkTooLow)"), seed!("sync-low-work-regtest", "PeerOpen { height: 4002, synced: true }"), seed!("sync-low-work-regtest-at-batch-end", "PeerOpen { height: 2000, synced: true }"), seed!("sync-low-work-regtest-first-short", "PeerRefused(HeadersSync(WorkTooLow))"), seed!("sync-low-work-regtest-second-above-first", "PeerOpen { height: 4002, synced: true }"), - seed!("sync-low-work-regtest-second-off-last", "PeerRefused(HeadersSync(NotContinuous))"), + seed!("sync-low-work-regtest-second-break-second", "PeerRefused(HeadersSync(NotContinuous))"), + seed!("sync-low-work-regtest-second-off-last", "PeerOpen { height: 0, synced: false }"), seed!("sync-low-work-regtest-second-other", "PeerRefused(HeadersSync(CommitmentMismatch))"), seed!("sync-low-work-regtest-second-short", "PeerRefused(HeadersSync(WorkTooLow))"), seed!("sync-low-work-regtest-short-at-min", "PeerOpen { height: 250, synced: true }"), @@ -1899,7 +1920,13 @@ pub(crate) mod fuzz { assert!(headers.len() <= HEADERS_FULL_COUNT); let announced = matches!(self.get_headers, GetHeadersModel::Ended); match self.get_headers { - GetHeadersModel::Outstanding(_) => {} + GetHeadersModel::Outstanding(_) => { + if let Some(first) = headers.first() + && !self.locator().contains(&first.prev) + { + return Ok(ReceivedExpected::NoReply); + } + } GetHeadersModel::Ended => { assert!(matches!(self.download, DownloadModel::Chain)); if let Some(first) = headers.first() @@ -3368,7 +3395,6 @@ mod tests { fn refused_answer_keeps_the_chain() { let headers = crate::fixtures::headers_on_genesis(14); let on_tip = crate::fixtures::headers_on(&headers[13], 1); - let off_locator = crate::fixtures::branch_on(&headers[1], 20); let mut unproven = headers[0]; unproven.nonce = (0..=u32::MAX) .find(|nonce| { @@ -3381,7 +3407,6 @@ mod tests { .unwrap(); for (batch, error) in [ (vec![unproven], crate::chain::ExtendError::ProofOfWork), - (off_locator, crate::chain::ExtendError::NotOnLocator), ( vec![on_tip[0], on_tip[0]], crate::chain::ExtendError::NotContinuous, @@ -3540,9 +3565,13 @@ mod tests { .work() .saturating_add(tip_after(&[]).work()); let (peer, _) = batch_then_tick(asked_with(&mut chain, work_min, start), &headers, start); + let next = crate::fixtures::headers_on(&headers[1_999], 1); assert_eq!( - peer.receive(&headers_message(&headers), crate::fixtures::CLOCK) - .err(), + peer.receive( + &headers_message(&[next[0], next[0]]), + crate::fixtures::CLOCK + ) + .err(), Some(super::PeerError::HeadersSync( crate::headerssync::HeadersSyncError::NotContinuous )) @@ -3722,7 +3751,7 @@ mod tests { } #[test] - fn headers_off_our_tip_in_answer_to_our_getheaders_end_the_connection() { + fn headers_off_our_locator_while_our_getheaders_is_outstanding_are_skipped() { let start = std::time::Instant::now(); let headers = crate::fixtures::headers_on_genesis(2); let (mut asked_chain, mut announced_chain) = (regtest(), regtest()); @@ -3730,14 +3759,26 @@ mod tests { let (announced, _) = batch_then_tick(pinged(&mut announced_chain, start, 7), &headers[1..], later); for peer in [asked(&mut asked_chain, start, 7), announced] { - assert_eq!( - peer.receive(&headers_message(&headers[1..]), crate::fixtures::CLOCK) - .err(), - Some(super::PeerError::Extend( - crate::chain::ExtendError::NotOnLocator - )) - ); + let peer = accept(peer, &headers_message(&headers[1..])); + assert_eq!(peer.chain.tip(), tip_after(&[])); + synced(peer, &headers); } + assert_eq!(asked_chain.tip(), tip_after(&headers)); + assert_eq!(announced_chain.tip(), tip_after(&headers)); + } + + #[test] + fn headers_off_our_locator_do_not_restart_the_headers_timeout() { + let mut chain = regtest(); + let start = std::time::Instant::now(); + let headers = crate::fixtures::headers_on_genesis(2); + let timeout = start + super::HEADERS_TIMEOUT; + let (peer, _) = idle(asked(&mut chain, start, 7), timeout - NANOSECOND); + let peer = accept(peer, &headers_message(&headers[1..])); + assert_eq!( + peer.tick(timeout, 8).err(), + Some(super::PeerError::HeadersTimeout) + ); } #[test] From 9ca4d447818dc90ab890ef18c2ca4b752544e956 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 18:20:22 -0300 Subject: [PATCH 02/10] test: keep the connection through the reorg in the L3 test of a branch that lost, with no ban (#92) The ban was only there to avoid #92. The test now waits until bitcoind and elo relay no more headers, and checks that elo kept its tip and its connection. --- tests/bitcoind.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/tests/bitcoind.rs b/tests/bitcoind.rs index 87e6e93..2a3dbc8 100644 --- a/tests/bitcoind.rs +++ b/tests/bitcoind.rs @@ -538,14 +538,9 @@ fn announcement_on_a_branch_that_lost_to_ours_after_a_low_work_sync_gets_no_geth let expected = format!("level=info event=tip peer={} height=3001 ", bitcoind.peer); assert!(ours.starts_with(&expected), "{ours}"); - bitcoind.cli(&["setban", "127.0.0.1", "add"]); - elo.event("end", END_TIMEOUT); bitcoind.cli(&["invalidateblock", second.trim_end()]); - bitcoind.cli(&["setban", "127.0.0.1", "remove"]); - elo.event("version", RECONNECT_TIMEOUT); - assert_eq!(elo.event("tip", SYNC_TIMEOUT), ours); - let ends = elo.events("end").len(); - pong_from(&bitcoind, &elo, ends); + settle(&bitcoind, &elo); + assert_eq!(elo.events("tip"), [ours]); let get_headers = message_bytes(&bitcoind, "bytesrecv_per_msg", "getheaders"); let headers_sent = message_bytes(&bitcoind, "bytessent_per_msg", "headers"); @@ -558,14 +553,38 @@ fn announcement_on_a_branch_that_lost_to_ours_after_a_low_work_sync_gets_no_geth ); std::thread::sleep(std::time::Duration::from_millis(100)); } - pong_from(&bitcoind, &elo, ends); - pong_from(&bitcoind, &elo, ends); + pong_from(&bitcoind, &elo, 0); + pong_from(&bitcoind, &elo, 0); assert_eq!( message_bytes(&bitcoind, "bytesrecv_per_msg", "getheaders"), get_headers ); } +fn settle(bitcoind: &Bitcoind, elo: &Elo) { + let start = std::time::Instant::now(); + loop { + let before = relay_bytes(bitcoind); + pong_from(bitcoind, elo, 0); + pong_from(bitcoind, elo, 0); + if relay_bytes(bitcoind) == before { + return; + } + assert!( + start.elapsed() < SYNC_TIMEOUT, + "bitcoind and elo still relay headers" + ); + } +} + +fn relay_bytes(bitcoind: &Bitcoind) -> [u64; 3] { + [ + message_bytes(bitcoind, "bytesrecv_per_msg", "getheaders"), + message_bytes(bitcoind, "bytessent_per_msg", "headers"), + message_bytes(bitcoind, "bytessent_per_msg", "inv"), + ] +} + fn pong_from(bitcoind: &Bitcoind, elo: &Elo, ends: usize) { let start = std::time::Instant::now(); let before = message_bytes(bitcoind, "bytesrecv_per_msg", "pong"); From dbd987cc575b51945c43d909f9e4243da56092b9 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 18:23:49 -0300 Subject: [PATCH 03/10] test: L3 test for an announcement that comes before the answer to our `getheaders` (#92) bitcoind drops its tip and mines 2 blocks, 10 times. Each time it announces the first block off our tip, and elo asks. Its announcement of the second block can come before the answer. Without the fix, elo ended the connection with `NotOnLocator` in 5 of 5 runs. --- tests/bitcoind.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/bitcoind.rs b/tests/bitcoind.rs index 2a3dbc8..de313a0 100644 --- a/tests/bitcoind.rs +++ b/tests/bitcoind.rs @@ -561,6 +561,35 @@ fn announcement_on_a_branch_that_lost_to_ours_after_a_low_work_sync_gets_no_geth ); } +#[test] +fn announcement_before_the_answer_to_our_getheaders_keeps_the_connection() { + let bitcoind = Bitcoind::start(); + bitcoind.cli(&["generatetodescriptor", "1", "raw(51)"]); + let elo = Elo::start(&bitcoind); + elo.event("tip", SYNC_TIMEOUT); + wait_for_send_headers(&bitcoind, &elo); + + for round in 0..10 { + let tip = bitcoind.cli(&["getbestblockhash"]); + bitcoind.cli(&["invalidateblock", tip.trim_end()]); + let script = format!("raw({:x})", 0x53 + round % 2); + bitcoind.cli(&["generatetodescriptor", "2", &script]); + } + settle(&bitcoind, &elo); + + let best = bitcoind.cli(&["getbestblockhash"]); + let tips = elo.events("tip"); + let expected = format!( + "level=info event=tip peer={} height=11 hash={} ", + bitcoind.peer, + best.trim_end() + ); + assert!( + tips.last().is_some_and(|tip| tip.starts_with(&expected)), + "{tips:?}" + ); +} + fn settle(bitcoind: &Bitcoind, elo: &Elo) { let start = std::time::Instant::now(); loop { From 913363d12268814ab7d7ba581af87c8f971d3c27 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 18:23:50 -0300 Subject: [PATCH 04/10] docs: say that elo skips an announcement while its `getheaders` is out (#92) --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 30dd066..62d6820 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,10 @@ fork. Headers that the peer announces on that header do not make elo ask. elo adds the work of each one that has its proof of work. It asks when the branch has more work, or when the announcement is a full batch. A peer announces a block with an `inv` when its headers do not connect to the -last header it sent, so an `inv` of a block makes elo ask too. From the +last header it sent, so an `inv` of a block makes elo ask too. While a +`getheaders` of elo is out, a `headers` that does not start on one of its hashes +is an announcement. elo skips it, and the answer or the next `getheaders` gets +those headers again. From the handshake on, it pings the peer every 2 min. It drops a peer that does not answer a `ping` within 20 min, or a `getheaders` within 2 min. It also drops a peer that sends `wtxidrelay` or `sendaddrv2` after From 4083ec96303beea4f3fea1a871b70a27700b196e Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:31:09 -0300 Subject: [PATCH 05/10] fix: put genesis in the locator of a low-work sync and of a branch, so that an answer from genesis ends the connection at once (#92) A peer that does not have the one hash of such a locator on its chain answers from genesis. elo skipped that answer as an announcement, and the connection ended with `HeadersTimeout` after 2 min. With genesis in the locator, the answer starts on a hash of the locator. The sync then refuses it at once with `NotContinuous`, as before the skip. The model puts genesis below the last hash too, so the script can write an answer on it. --- .../sync-low-work-regtest-first-on-genesis | Bin 0 -> 21 bytes src/net_processing.rs | 79 ++++++++++++++++-- 2 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 fuzz/corpus/machines/sync-low-work-regtest-first-on-genesis diff --git a/fuzz/corpus/machines/sync-low-work-regtest-first-on-genesis b/fuzz/corpus/machines/sync-low-work-regtest-first-on-genesis new file mode 100644 index 0000000000000000000000000000000000000000..1b954c95d27c2c4d1751b76e5697b63bca3acf02 GIT binary patch literal 21 ZcmYdbWMpJuU}E54{m;O_z$pqM82}lb0*?Ry literal 0 HcmV?d00001 diff --git a/src/net_processing.rs b/src/net_processing.rs index b8ba8e0..f92f568 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -391,14 +391,22 @@ impl Download { } fn locator(&self, chain: &crate::chain::Chain) -> Vec { - match self { - Download::Genesis(_) | Download::Chain => chain.locator(), - Download::Presync(presync) => vec![presync.last_hash()], - Download::Redownload(redownload) => vec![redownload.last_hash()], - Download::Branch(branch) => vec![branch.last_hash()], - Download::BranchPresync(sync) => vec![sync.presync.last_hash()], - Download::BranchRedownload(sync) => vec![sync.redownload.last_hash()], + let last = match self { + Download::Genesis(_) | Download::Chain => return chain.locator(), + Download::Presync(presync) => presync.last_hash(), + Download::Redownload(redownload) => redownload.last_hash(), + Download::Branch(branch) => branch.last_hash(), + Download::BranchPresync(sync) => sync.presync.last_hash(), + Download::BranchRedownload(sync) => sync.redownload.last_hash(), + }; + let genesis = chain.network().genesis().hash(); + let mut locator = vec![last]; + if last != genesis { + locator.push(genesis); } + assert_eq!(locator.first(), Some(&last)); + assert_eq!(locator.last(), Some(&genesis)); + locator } fn is_answered_by( @@ -1092,6 +1100,7 @@ pub(crate) mod fuzz { seed!("sync-low-work-mainnet", "PeerRefused(ChainWorkTooLow)"), seed!("sync-low-work-regtest", "PeerOpen { height: 4002, synced: true }"), seed!("sync-low-work-regtest-at-batch-end", "PeerOpen { height: 2000, synced: true }"), + seed!("sync-low-work-regtest-first-on-genesis", "PeerRefused(HeadersSync(NotContinuous))"), seed!("sync-low-work-regtest-first-short", "PeerRefused(HeadersSync(WorkTooLow))"), seed!("sync-low-work-regtest-second-above-first", "PeerOpen { height: 4002, synced: true }"), seed!("sync-low-work-regtest-second-break-second", "PeerRefused(HeadersSync(NotContinuous))"), @@ -1809,7 +1818,12 @@ pub(crate) mod fuzz { fn locator_below(&self) -> Vec<(bitcoin_hashes::sha256d::Hash, usize)> { if !matches!(self.download, DownloadModel::Chain) { - return Vec::new(); + let (_, height) = self.download_last(); + return if height == 0 { + Vec::new() + } else { + vec![(self.chain[0], 0)] + }; } self.locator_heights() .into_iter() @@ -2498,7 +2512,12 @@ mod tests { } fn get_headers_after(header: &crate::block::BlockHeader) -> crate::protocol::GetHeaders { - crate::protocol::GetHeaders::new(vec![header.hash()]) + let genesis = crate::chainparams::Network::Regtest.genesis().hash(); + let mut locator = vec![header.hash()]; + if header.hash() != genesis { + locator.push(genesis); + } + crate::protocol::GetHeaders::new(locator) } fn get_headers_on(headers: &[crate::block::BlockHeader]) -> crate::protocol::GetHeaders { @@ -3578,6 +3597,48 @@ mod tests { ); } + #[test] + fn low_work_sync_ends_the_connection_at_once_on_an_answer_from_genesis() { + let mut chain = regtest(); + let start = std::time::Instant::now(); + let headers = crate::fixtures::headers_on_genesis(2_000); + let work_min = tip_after(&headers) + .work() + .saturating_add(tip_after(&[]).work()); + let (peer, get_headers) = + batch_then_tick(asked_with(&mut chain, work_min, start), &headers, start); + assert_eq!(get_headers, get_headers_after(&headers[1_999])); + let other = crate::fixtures::branch_on(&crate::chainparams::Network::Regtest.genesis(), 1); + assert_eq!( + peer.receive(&headers_message(&other), crate::fixtures::CLOCK) + .err(), + Some(super::PeerError::HeadersSync( + crate::headerssync::HeadersSyncError::NotContinuous + )) + ); + } + + #[test] + fn branch_download_ends_the_connection_at_once_on_an_answer_from_genesis() { + let start = std::time::Instant::now(); + let headers = crate::fixtures::headers_on_genesis(2_001); + let mut chain = regtest(); + for batch in crate::protocol::Headers::batches(headers.clone()) { + chain.extend(batch, crate::fixtures::CLOCK).unwrap(); + } + let (peer, get_headers) = + batch_then_tick(asked(&mut chain, start, 7), &headers[..2_000], start); + assert_eq!(get_headers, get_headers_after(&headers[1_999])); + let other = crate::fixtures::branch_on(&crate::chainparams::Network::Regtest.genesis(), 1); + assert_eq!( + peer.receive(&headers_message(&other), crate::fixtures::CLOCK) + .err(), + Some(super::PeerError::Extend( + crate::chain::ExtendError::NotContinuous + )) + ); + } + #[test] fn each_header_that_a_low_work_sync_releases_goes_through_the_checks_of_the_chain() { let mut chain = regtest(); From c6ffd05742c4068869c8c6679d453f3bca634386 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:31:22 -0300 Subject: [PATCH 06/10] refactor: check that a `headers` answers our `getheaders` before the state moves, so that a skip leaves the state as it is (#92) The skip took the outstanding `getheaders` out of the state and put it back. The check now reads the state in place, and only an answer moves it. --- src/net_processing.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/net_processing.rs b/src/net_processing.rs index f92f568..8fde146 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -492,22 +492,21 @@ impl<'a> Peer<'a> { let mut input = message.payload(); let headers = crate::protocol::Headers::decode(&mut input).map_err(PeerError::Headers)?; + if let SyncState::Outstanding { download, .. } = &self.sync + && !download.is_answered_by(self.chain, &headers) + { + return Ok((self, Received::NoReply)); + } let next = match self.sync { - SyncState::Outstanding { download, sent } => { - if !download.is_answered_by(self.chain, &headers) { - self.sync = SyncState::Outstanding { download, sent }; - return Ok((self, Received::NoReply)); - } - receive_answer( - self.chain, - self.work_min, - &mut self.salt, - &mut self.branch_tip, - download, - headers, - clock, - )? - } + SyncState::Outstanding { download, .. } => receive_answer( + self.chain, + self.work_min, + &mut self.salt, + &mut self.branch_tip, + download, + headers, + clock, + )?, SyncState::Synced => receive_announcement( self.chain, self.work_min, From 6d44b1597ea30971aadadcd5ba8ff794fa6c6839 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:31:48 -0300 Subject: [PATCH 07/10] refactor: crash on an answer that is not on our locator, and drop `NotOnLocator` (#92) elo now skips a `headers` that does not start on a hash of our locator, so no peer can send such an answer. `Chain::extend_on_locator` and the model found the same headers again and returned `NotOnLocator`. That error can no longer happen, so both now crash on it, as a bug in elo. A Peer test takes the place of the Chain test: a branch on a header in a gap of our locator is skipped. --- src/chain.rs | 23 ++++++++++------------- src/net_processing.rs | 27 +++++++++++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index 3c12934..864a844 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -95,7 +95,6 @@ enum AboveFork { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ExtendError { NotOnTip, - NotOnLocator, NotContinuous, ProofOfWork, WrongBits, @@ -161,10 +160,12 @@ impl Chain { } let tip_height = self.below_tip.len(); let first_prev = first.prev; - let fork_height = locator_heights(tip_height) + let Some(fork_height) = locator_heights(tip_height) .skip(1) .find(|height| self.header_at(*height).hash() == first_prev) - .ok_or(ExtendError::NotOnLocator)?; + else { + panic!("an answer to our `getheaders` does not start on our locator"); + }; assert!(fork_height < tip_height); let branch = self.branch_on_fork(fork_height); self.extend_above(branch, Batch::Answer(headers), clock, work_min) @@ -779,9 +780,6 @@ impl std::fmt::Display for ExtendError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ExtendError::NotOnTip => write!(f, "headers do not start at our tip"), - ExtendError::NotOnLocator => { - write!(f, "headers do not start at a header of our locator") - } ExtendError::NotContinuous => write!(f, "headers do not form a chain"), ExtendError::ProofOfWork => write!(f, "a header fails proof of work"), ExtendError::WrongBits => write!(f, "a header has the wrong bits"), @@ -1764,16 +1762,15 @@ mod tests { } #[test] - fn extend_on_locator_refuses_a_batch_on_a_header_that_is_not_in_the_locator() { + fn extend_on_locator_takes_a_branch_on_each_side_of_a_gap_in_the_locator() { let headers = crate::fixtures::headers_on_genesis(14); - for (below, expected) in [ - (&headers[0], Ok(super::Extended::Taken { full: false })), - (&headers[1], Err(super::ExtendError::NotOnLocator)), - (&headers[2], Ok(super::Extended::Taken { full: false })), - ] { + for below in [&headers[0], &headers[2]] { let branch = crate::fixtures::branch_on(below, 20); let mut chain = regtest_with(&headers); - assert_eq!(on_locator(&mut chain, &branch), expected); + assert_eq!( + on_locator(&mut chain, &branch), + Ok(super::Extended::Taken { full: false }) + ); } } diff --git a/src/net_processing.rs b/src/net_processing.rs index 8fde146..88c5423 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -2045,17 +2045,12 @@ pub(crate) mod fuzz { let Some(first) = headers.first() else { return Ok(ExtendedModel::Kept); }; - let fork = match self + let Some(fork) = self .locator_heights() .into_iter() .find(|height| self.chain[*height] == first.prev) - { - Some(height) => height, - None => { - return Err(super::PeerError::Extend( - crate::chain::ExtendError::NotOnLocator, - )); - } + else { + panic!("an answer to our `getheaders` does not start on our locator"); }; let batch = script_hashes(self.network, first.prev, headers).map_err(extend_error)?; if fork == self.height() { @@ -3827,6 +3822,22 @@ mod tests { assert_eq!(announced_chain.tip(), tip_after(&headers)); } + #[test] + fn headers_on_a_header_in_a_gap_of_our_locator_are_skipped() { + let mut chain = regtest(); + let start = std::time::Instant::now(); + let headers = crate::fixtures::headers_on_genesis(14); + chain + .extend( + crate::fixtures::headers_batch(&headers), + crate::fixtures::CLOCK, + ) + .unwrap(); + let branch = crate::fixtures::branch_on(&headers[1], 20); + let peer = accept(asked(&mut chain, start, 7), &headers_message(&branch)); + assert_eq!(peer.chain.tip(), tip_after(&headers)); + } + #[test] fn headers_off_our_locator_do_not_restart_the_headers_timeout() { let mut chain = regtest(); From 34d29269eb07196fcec7832aeedba3a93515190e Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:43:33 -0300 Subject: [PATCH 08/10] refactor: let the chain find where an answer starts on our locator, and drop the crash (#92) The `headers` check found the first hash in our locator, and gave only `true` or `false`. `Chain::extend_on_locator` then found the same hash again, and crashed when it did not find it. The two searches agreed only because both read `locator_heights`. Now `Chain::extend_on_locator` gives `None` for headers that do not start on our locator, and changes nothing. For `Download::Chain`, this search is the check. `receive_answer` gives `Answer::Skipped` with the download, and the `Peer` puts it back with the same `sent`. The check of the other downloads moves into `receive_answer`, before the state moves. `receive_on_genesis` uses `Chain::extend`: the check already found that the answer starts on genesis, which is our tip. Its match on `Extended` and its crash go. The model finds the height of the first hash in the check, and gives it to its `extend_on_locator`, so the crash of the model goes too. Removed: 3 crashes, the match on `Extended` in `receive_on_genesis`, the second search of our locator, and the `if let` before the match on the state. Added: `Answer`, with 2 variants. --- src/chain.rs | 69 +++++++++++-------- src/net_processing.rs | 151 +++++++++++++++++++++++------------------- 2 files changed, 125 insertions(+), 95 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index 864a844..203dd5a 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -149,26 +149,26 @@ impl Chain { headers: crate::protocol::Headers, clock: u64, work_min: crate::pow::Work, - ) -> Result { + ) -> Result, ExtendError> { let Some(first) = headers.first() else { - return Ok(Extended::Kept); + return Ok(Some(Extended::Kept)); }; - if first.prev == self.tip.hash() { - let full = headers.is_full(); - self.extend_tip(headers.into_vec(), time_max(clock))?; - return Ok(Extended::Taken { full }); - } let tip_height = self.below_tip.len(); let first_prev = first.prev; - let Some(fork_height) = locator_heights(tip_height) - .skip(1) - .find(|height| self.header_at(*height).hash() == first_prev) + let Some(fork_height) = + locator_heights(tip_height).find(|height| self.header_at(*height).hash() == first_prev) else { - panic!("an answer to our `getheaders` does not start on our locator"); + return Ok(None); }; + if fork_height == tip_height { + let full = headers.is_full(); + self.extend_tip(headers.into_vec(), time_max(clock))?; + return Ok(Some(Extended::Taken { full })); + } assert!(fork_height < tip_height); let branch = self.branch_on_fork(fork_height); self.extend_above(branch, Batch::Answer(headers), clock, work_min) + .map(Some) } pub(crate) fn extend_branch( @@ -1587,11 +1587,14 @@ mod tests { chain: &mut super::Chain, headers: &[crate::block::BlockHeader], ) -> Result { - chain.extend_on_locator( - crate::fixtures::headers_batch(headers), - crate::fixtures::CLOCK, - crate::pow::Work::ZERO, - ) + chain + .extend_on_locator( + crate::fixtures::headers_batch(headers), + crate::fixtures::CLOCK, + crate::pow::Work::ZERO, + ) + .transpose() + .unwrap() } fn on_branch( @@ -1762,15 +1765,26 @@ mod tests { } #[test] - fn extend_on_locator_takes_a_branch_on_each_side_of_a_gap_in_the_locator() { + fn extend_on_locator_takes_no_batch_on_a_header_that_is_not_in_the_locator() { let headers = crate::fixtures::headers_on_genesis(14); - for below in [&headers[0], &headers[2]] { + for (below, expected) in [ + (&headers[0], Some(super::Extended::Taken { full: false })), + (&headers[1], None), + (&headers[2], Some(super::Extended::Taken { full: false })), + ] { let branch = crate::fixtures::branch_on(below, 20); let mut chain = regtest_with(&headers); - assert_eq!( - on_locator(&mut chain, &branch), - Ok(super::Extended::Taken { full: false }) + let tip = chain.tip(); + let extended = chain.extend_on_locator( + crate::fixtures::headers_batch(&branch), + crate::fixtures::CLOCK, + crate::pow::Work::ZERO, ); + let skipped = expected.is_none(); + assert_eq!(extended, Ok(expected)); + if skipped { + assert_eq!(chain.tip(), tip); + } } } @@ -1931,11 +1945,14 @@ mod tests { for (count, goes_on) in [(2_000, true), (2_001, false)] { let work_min = regtest_with(&headers[..count]).tip().work(); let mut chain = regtest_with(&headers); - let extended = chain.extend_on_locator( - crate::fixtures::headers_batch(&branch), - crate::fixtures::CLOCK, - work_min, - ); + let extended = chain + .extend_on_locator( + crate::fixtures::headers_batch(&branch), + crate::fixtures::CLOCK, + work_min, + ) + .transpose() + .unwrap(); assert_eq!( matches!(extended, Ok(super::Extended::Branch(_))), goes_on, diff --git a/src/net_processing.rs b/src/net_processing.rs index 88c5423..4e1a219 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -354,6 +354,12 @@ enum SyncState { Synced, } +#[derive(Debug)] +enum Answer { + Next(Option), + Skipped(Download), +} + #[derive(Debug)] enum SendHeadersState { Unsent, @@ -414,9 +420,17 @@ impl Download { chain: &crate::chain::Chain, headers: &crate::protocol::Headers, ) -> bool { - headers - .first() - .is_none_or(|first| self.locator(chain).contains(&first.prev)) + match self { + Download::Chain => true, + Download::Genesis(_) + | Download::Presync(_) + | Download::Redownload(_) + | Download::Branch(_) + | Download::BranchPresync(_) + | Download::BranchRedownload(_) => headers + .first() + .is_none_or(|first| self.locator(chain).contains(&first.prev)), + } } } @@ -492,21 +506,24 @@ impl<'a> Peer<'a> { let mut input = message.payload(); let headers = crate::protocol::Headers::decode(&mut input).map_err(PeerError::Headers)?; - if let SyncState::Outstanding { download, .. } = &self.sync - && !download.is_answered_by(self.chain, &headers) - { - return Ok((self, Received::NoReply)); - } let next = match self.sync { - SyncState::Outstanding { download, .. } => receive_answer( - self.chain, - self.work_min, - &mut self.salt, - &mut self.branch_tip, - download, - headers, - clock, - )?, + SyncState::Outstanding { download, sent } => { + match receive_answer( + self.chain, + self.work_min, + &mut self.salt, + &mut self.branch_tip, + download, + headers, + clock, + )? { + Answer::Next(next) => next, + Answer::Skipped(download) => { + self.sync = SyncState::Outstanding { download, sent }; + return Ok((self, Received::NoReply)); + } + } + } SyncState::Synced => receive_announcement( self.chain, self.work_min, @@ -654,28 +671,34 @@ fn receive_answer( download: Download, headers: crate::protocol::Headers, clock: u64, -) -> Result, PeerError> { +) -> Result { + if !download.is_answered_by(chain, &headers) { + return Ok(Answer::Skipped(download)); + } let full = headers.is_full(); - match download { + let next = match download { Download::Genesis(genesis_salt) => { - receive_on_genesis(chain, work_min, salt, genesis_salt, headers, clock) + receive_on_genesis(chain, work_min, salt, genesis_salt, headers, clock)? } Download::Chain => { - let extended = chain + let Some(extended) = chain .extend_on_locator(headers, clock, work_min) - .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, branch_tip, extended, clock) + .map_err(PeerError::Extend)? + else { + return Ok(Answer::Skipped(Download::Chain)); + }; + chain_next(chain, work_min, salt, branch_tip, extended, clock)? } Download::Branch(branch) => { let extended = chain .extend_branch(*branch, headers, clock, work_min) .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, branch_tip, extended, clock) + chain_next(chain, work_min, salt, branch_tip, extended, clock)? } Download::BranchPresync(sync) => { let BranchPresync { start, presync } = *sync; let step = presync.receive(headers).map_err(PeerError::HeadersSync)?; - Ok(Some(branch_presync_next(start, step))) + Some(branch_presync_next(start, step)) } Download::BranchRedownload(sync) => { let BranchRedownload { @@ -689,26 +712,24 @@ fn receive_answer( { crate::headerssync::RedownloadStep::More(redownload, more) => { keep_released(chain.network(), &mut released, more); - Ok(Some(Download::BranchRedownload(Box::new( - BranchRedownload { - start, - redownload: *redownload, - released, - }, - )))) + Some(Download::BranchRedownload(Box::new(BranchRedownload { + start, + redownload: *redownload, + released, + }))) } crate::headerssync::RedownloadStep::Done(rest) => { keep_released(chain.network(), &mut released, rest); let extended = chain .extend_low_work_branch(start, released, full, clock, work_min) .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, branch_tip, extended, clock) + chain_next(chain, work_min, salt, branch_tip, extended, clock)? } } } Download::Presync(presync) => { let step = presync.receive(headers).map_err(PeerError::HeadersSync)?; - Ok(Some(presync_next(step))) + Some(presync_next(step)) } Download::Redownload(redownload) => { match redownload @@ -717,16 +738,17 @@ fn receive_answer( { crate::headerssync::RedownloadStep::More(redownload, released) => { extend_released(chain, released, clock)?; - Ok(Some(Download::Redownload(redownload))) + Some(Download::Redownload(redownload)) } crate::headerssync::RedownloadStep::Done(released) => { extend_released(chain, released, clock)?; let next = Download::on_tip(chain, work_min); - Ok(full.then_some(next)) + full.then_some(next) } } } - } + }; + Ok(Answer::Next(next)) } fn receive_on_genesis( @@ -745,22 +767,7 @@ fn receive_on_genesis( let step = presync.receive(headers).map_err(PeerError::HeadersSync)?; return Ok(Some(presync_next(step))); } - let empty = headers.first().is_none(); - match chain - .extend_on_locator(headers, clock, work_min) - .map_err(PeerError::Extend)? - { - crate::chain::Extended::Taken { full } => { - assert!(!full); - assert!(!empty); - } - crate::chain::Extended::Kept => assert!(empty), - crate::chain::Extended::KeptOver(_) - | crate::chain::Extended::Branch(_) - | crate::chain::Extended::BelowMargin { .. } => { - panic!("genesis has no header below it") - } - } + chain.extend(headers, clock).map_err(PeerError::Extend)?; assert!(salt_left.replace(salt).is_none()); if chain.tip().work() >= work_min { Ok(None) @@ -1838,6 +1845,13 @@ pub(crate) mod fuzz { .collect() } + fn locator_height(&self, prev: bitcoin_hashes::sha256d::Hash) -> Option { + std::iter::once(self.download_last()) + .chain(self.locator_below()) + .find(|(hash, _)| *hash == prev) + .map(|(_, height)| height) + } + fn is_synced(&self) -> bool { matches!(self.get_headers, GetHeadersModel::Ended) } @@ -1932,12 +1946,14 @@ pub(crate) mod fuzz { .into_vec(); assert!(headers.len() <= HEADERS_FULL_COUNT); let announced = matches!(self.get_headers, GetHeadersModel::Ended); + let mut fork = None; match self.get_headers { GetHeadersModel::Outstanding(_) => { - if let Some(first) = headers.first() - && !self.locator().contains(&first.prev) - { - return Ok(ReceivedExpected::NoReply); + if let Some(first) = headers.first() { + let Some(height) = self.locator_height(first.prev) else { + return Ok(ReceivedExpected::NoReply); + }; + fork = Some(height); } } GetHeadersModel::Ended => { @@ -1967,7 +1983,10 @@ pub(crate) mod fuzz { self.presync_start(0, hashes, full)? } DownloadModel::Chain => { - let extended = self.extend_on_locator(&headers)?; + let extended = match fork { + None => ExtendedModel::Kept, + Some(fork) => self.extend_on_locator(fork, &headers)?, + }; self.chain_next(extended, full)? } DownloadModel::Branch { fork, hashes } => { @@ -2040,19 +2059,13 @@ pub(crate) mod fuzz { fn extend_on_locator( &mut self, + fork: usize, headers: &[crate::block::BlockHeader], ) -> Result { - let Some(first) = headers.first() else { - return Ok(ExtendedModel::Kept); - }; - let Some(fork) = self - .locator_heights() - .into_iter() - .find(|height| self.chain[*height] == first.prev) - else { - panic!("an answer to our `getheaders` does not start on our locator"); - }; - let batch = script_hashes(self.network, first.prev, headers).map_err(extend_error)?; + assert!(!headers.is_empty()); + assert!(fork <= self.height()); + let batch = + script_hashes(self.network, self.chain[fork], headers).map_err(extend_error)?; if fork == self.height() { self.chain.extend(batch); return Ok(ExtendedModel::Taken); From 85fdc5e263364e954e99893c69460e95563093e0 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:43:41 -0300 Subject: [PATCH 09/10] refactor: keep our tip of the time of our `getheaders`, and crash when it changed before the answer (#92) The check of a `headers` makes our locator again. It is the locator that we sent only while nothing writes the chain while our `getheaders` is out. Only a convention holds this: `receive_inv` and `receive_announcement` act only after the sync. A change that takes an announcement while our `getheaders` is out breaks it, and the check then reads the wrong locator with no error. `SyncState::Outstanding` now keeps the hash of our tip when we send the `getheaders`. `receive_headers` crashes when our tip is a different one. To keep the locator itself is not sufficient: `extend_on_locator` reads the chain, not the locator that we sent. The hash makes `Peer` 32 bytes larger, and `Step::Done` is then 216 bytes larger than `Step::Reply`, above the limit of `clippy::large_enum_variant`. `Step::Done` now holds a `Box`: one allocation for each connection. --- src/net.rs | 2 +- src/net_processing.rs | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/net.rs b/src/net.rs index ad92039..eef9937 100644 --- a/src/net.rs +++ b/src/net.rs @@ -539,7 +539,7 @@ async fn handshake_stream_exchange( crate::net_processing::Step::Done(peer, theirs) => { let connection = Connection { socket, - peer, + peer: *peer, address, }; return Ok((connection, theirs)); diff --git a/src/net_processing.rs b/src/net_processing.rs index 4e1a219..e33c4e0 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -172,7 +172,7 @@ pub(crate) enum Received { pub(crate) enum Step<'a> { Wait(Handshake<'a>), Reply(Handshake<'a>, HandshakeOutbound), - Done(Peer<'a>, crate::protocol::Version), + Done(Box>, crate::protocol::Version), } impl<'a> Handshake<'a> { @@ -264,7 +264,7 @@ impl<'a> Handshake<'a> { salt, branch_tip: None, }; - Ok(Step::Done(peer, theirs)) + Ok(Step::Done(Box::new(peer), theirs)) } else { let state = State::AwaitingVerack(theirs); let handshake = next( @@ -350,6 +350,7 @@ enum SyncState { Outstanding { download: Download, sent: std::time::Instant, + tip: bitcoin_hashes::sha256d::Hash, }, Synced, } @@ -507,7 +508,12 @@ impl<'a> Peer<'a> { let mut input = message.payload(); let headers = crate::protocol::Headers::decode(&mut input).map_err(PeerError::Headers)?; let next = match self.sync { - SyncState::Outstanding { download, sent } => { + SyncState::Outstanding { + download, + sent, + tip, + } => { + assert_eq!(self.chain.tip().hash(), tip); match receive_answer( self.chain, self.work_min, @@ -519,7 +525,11 @@ impl<'a> Peer<'a> { )? { Answer::Next(next) => next, Answer::Skipped(download) => { - self.sync = SyncState::Outstanding { download, sent }; + self.sync = SyncState::Outstanding { + download, + sent, + tip, + }; return Ok((self, Received::NoReply)); } } @@ -643,6 +653,7 @@ impl SyncState { let sync = SyncState::Outstanding { download, sent: now, + tip: chain.tip().hash(), }; Ok((sync, Some(get_headers), Some(now + HEADERS_TIMEOUT))) } @@ -1269,7 +1280,7 @@ pub(crate) mod fuzz { ) => handshake = next, (Ok(super::Step::Done(peer, theirs)), Ok(HandshakeExpected::Done(expected))) => { assert_eq!(theirs, expected); - return Ok(peer); + return Ok(*peer); } (Err(error), Err(expected)) => { assert_eq!(error, expected); @@ -2581,7 +2592,7 @@ mod tests { let (handshake, _) = reply(handshake.receive(&core_message(crate::fixtures::VERSION_FRAME))); match handshake.receive(&core_message(crate::fixtures::VERACK_FRAME)) { - Ok(super::Step::Done(peer, _)) => peer, + Ok(super::Step::Done(peer, _)) => *peer, other => panic!("{other:?}"), } } From c06bb4a78fb8dc957386094c269e2075f0b5de34 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 19:45:04 -0300 Subject: [PATCH 10/10] test: skip headers off the locator of a low-work sync, and go on with the sync (#92) After the change to `Chain::extend_on_locator`, only the downloads that are not `Download::Chain` use `Download::is_answered_by`. No test that runs without `cfg(fuzzing)` sent such a download headers off its locator, so cargo-mutants did not kill `is_answered_by -> true`. --- src/net_processing.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/net_processing.rs b/src/net_processing.rs index e33c4e0..0795e0f 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -3593,6 +3593,28 @@ mod tests { assert_eq!(tip, tip_after(&headers)); } + #[test] + fn headers_off_the_locator_of_a_low_work_sync_are_skipped() { + let mut chain = regtest(); + let start = std::time::Instant::now(); + let headers = crate::fixtures::headers_on_genesis(3_500); + let work_min = tip_after(&headers).work(); + let (peer, _) = batch_then_tick( + asked_with(&mut chain, work_min, start), + &headers[..2_000], + start, + ); + let off_locator = crate::fixtures::branch_on(&headers[1_000], 20); + let peer = accept(peer, &headers_message(&off_locator)); + let (peer, _) = batch_then_tick(peer, &headers[2_000..], start); + let (peer, _) = batch_then_tick(peer, &headers[..2_000], start); + let received = peer.receive(&headers_message(&headers[2_000..]), crate::fixtures::CLOCK); + let Ok((_, super::Received::Synced(tip))) = received else { + panic!("{received:?}"); + }; + assert_eq!(tip, tip_after(&headers)); + } + #[test] fn low_work_sync_that_refuses_a_batch_ends_the_connection() { let mut chain = regtest();