From c1cebea43b8c1804eff1c6a4a204b48eb34afd4f Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 13:31:12 -0300 Subject: [PATCH 01/13] fix: keep the tip of a branch with no more work than ours, so that an announcement on it does not get the branch again --- README.md | 5 +- src/chain.rs | 117 ++++++++++++++++++++++++++++++---- src/net_processing.rs | 145 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 225 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 2680fcd..c1b41ab 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ is on, as from genesis, and the minimum of that sync is the work of elo less the 144 headers. Then the branch goes on as one that elo kept. A connection has at most one low-work sync, so a second one ends it, and the next connection gets a new salt. Each `getheaders` of a low-work sync or of a branch has only the hash -of the last header that elo got. +of the last header that elo got. When a branch ends with no more work, elo keeps +the hash and the work of its last header until the connection ends. 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, and asks only when the branch has more work. 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 handshake on, it pings the peer every 2 min. diff --git a/src/chain.rs b/src/chain.rs index 8d8c3f2..8c563cb 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -27,6 +27,7 @@ pub(crate) const BRANCH_TIP_HEADERS_BEHIND_MAX: usize = 144; pub(crate) enum Extended { Taken, Kept, + KeptOver(BranchTip), Branch(Branch), BelowMargin { start: SyncStart, @@ -35,6 +36,12 @@ pub(crate) enum Extended { }, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct BranchTip { + hash: bitcoin_hashes::sha256d::Hash, + work: crate::pow::Work, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct SyncStart { network: crate::chainparams::Network, @@ -201,7 +208,12 @@ impl Chain { } assert_eq!(self.work, branch.chain_work); if batch.headers().is_empty() { - return Ok(Extended::Kept); + return Ok(match branch.above_fork { + AboveFork::Empty { .. } => Extended::Kept, + AboveFork::Headers { + work, last_hash, .. + } => Extended::KeptOver(self.branch_tip(branch.fork_height, work, last_hash)), + }); } self.extend_above(branch, batch, clock, work_min) } @@ -303,7 +315,11 @@ impl Chain { chain_work: self.work, })); } - return Ok(Extended::Kept); + let work_at_fork = self.work.minus(work_above_fork); + return Ok(Extended::KeptOver(BranchTip { + hash: last_hash, + work: work_at_fork.saturating_add(work_branch), + })); } let work_at_fork = self.work.minus(work_above_fork); @@ -376,6 +392,17 @@ impl Chain { } } + fn branch_tip( + &self, + fork_height: usize, + work_above_fork: crate::pow::Work, + hash: bitcoin_hashes::sha256d::Hash, + ) -> BranchTip { + let work = self.work_at(fork_height).saturating_add(work_above_fork); + assert!(work <= self.work); + BranchTip { hash, work } + } + fn branch_on_fork(&self, fork_height: usize) -> Branch { assert!(fork_height < self.below_tip.len()); Branch { @@ -669,6 +696,34 @@ impl Branch { } } +impl BranchTip { + pub(crate) fn hash(&self) -> bitcoin_hashes::sha256d::Hash { + self.hash + } + + pub(crate) fn work(&self) -> crate::pow::Work { + self.work + } + + pub(crate) fn extend( + self, + network: crate::chainparams::Network, + headers: &[crate::block::BlockHeader], + ) -> Result { + headers.iter().try_fold(self, |tip, header| { + if header.prev != tip.hash { + return Err(ExtendError::NotContinuous); + } + let (hash, work) = + crate::pow::proven(header, network).ok_or(ExtendError::ProofOfWork)?; + Ok(BranchTip { + hash, + work: tip.work.saturating_add(work), + }) + }) + } +} + impl std::fmt::Display for ExtendError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -811,18 +866,23 @@ mod tests { assert_eq!(chain.tip(), before); } - #[test] - fn extend_refuses_a_batch_with_a_hash_above_its_target() { - let mut headers = crate::fixtures::headers_on_genesis(2); - headers[1].nonce = (0..=u32::MAX) + fn unproven(header: crate::block::BlockHeader) -> crate::block::BlockHeader { + let nonce = (0..=u32::MAX) .find(|nonce| { let header = crate::block::BlockHeader { nonce: *nonce, - ..headers[1] + ..header }; header.hash().as_byte_array()[31] >= 0x80 }) .unwrap(); + crate::block::BlockHeader { nonce, ..header } + } + + #[test] + fn extend_refuses_a_batch_with_a_hash_above_its_target() { + let mut headers = crate::fixtures::headers_on_genesis(2); + headers[1] = unproven(headers[1]); let mut chain = super::Chain::new(crate::chainparams::Network::Regtest); let before = chain.tip(); @@ -1498,6 +1558,18 @@ mod tests { ) } + fn tip_of_branch(headers: &[crate::block::BlockHeader]) -> super::BranchTip { + let tip = regtest_with(headers).tip(); + super::BranchTip { + hash: tip.hash(), + work: tip.work(), + } + } + + fn kept_over(headers: &[crate::block::BlockHeader]) -> super::Extended { + super::Extended::KeptOver(tip_of_branch(headers)) + } + fn branch_of(extended: Result) -> super::Branch { match extended { Ok(super::Extended::Branch(branch)) => branch, @@ -1612,7 +1684,8 @@ mod tests { [&headers[..1], &branch[..]].concat(), ) } else { - (super::Extended::Kept, headers.to_vec()) + let theirs = [&headers[..1], &branch[..]].concat(); + (kept_over(&theirs), headers.to_vec()) }; assert_eq!(on_locator(&mut chain, &branch), Ok(expected), "{count}"); assert_eq!(chain.tip(), regtest_with(&ours).tip(), "{count}"); @@ -1708,13 +1781,10 @@ mod tests { fn extend_branch_keeps_the_chain_when_the_peer_ends_the_branch_with_no_more_work() { let headers = crate::fixtures::headers_on_genesis(2_100); let branch = crate::fixtures::branch_on(&genesis(), 2_100); - for rest in [&branch[2_000..], &[]] { + for (rest, theirs) in [(&branch[2_000..], &branch[..]), (&[], &branch[..2_000])] { let mut chain = regtest_with(&headers); let pending = branch_of(on_locator(&mut chain, &branch[..2_000])); - assert_eq!( - on_branch(&mut chain, pending, rest), - Ok(super::Extended::Kept) - ); + assert_eq!(on_branch(&mut chain, pending, rest), Ok(kept_over(theirs))); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } } @@ -1886,7 +1956,7 @@ mod tests { assert_eq!(chain.tip(), regtest_with(&branch).tip()); } (_, false) => { - assert_eq!(extended, Ok(super::Extended::Kept), "{count}"); + assert_eq!(extended, Ok(kept_over(&branch)), "{count}"); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } (_, true) => { @@ -1897,4 +1967,23 @@ mod tests { } } } + + #[test] + fn branch_tip_takes_the_work_of_headers_on_it_that_have_their_proof_of_work() { + let branch = crate::fixtures::branch_on(&genesis(), 3); + let network = crate::chainparams::Network::Regtest; + let tip = tip_of_branch(&branch[..1]); + assert_eq!(tip.extend(network, &[]), Ok(tip)); + assert_eq!( + tip.extend(network, &branch[1..]), + Ok(tip_of_branch(&branch)) + ); + let unproven = unproven(branch[2]); + for (headers, error) in [ + (&branch[2..], super::ExtendError::NotContinuous), + (&[branch[1], unproven][..], super::ExtendError::ProofOfWork), + ] { + assert_eq!(tip.extend(network, headers), Err(error)); + } + } } diff --git a/src/net_processing.rs b/src/net_processing.rs index c6c8597..fce483d 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -262,6 +262,7 @@ impl<'a> Handshake<'a> { chain: self.chain, work_min: self.work_min, salt, + branch_tip: None, }; Ok(Step::Done(peer, theirs)) } else { @@ -328,6 +329,7 @@ pub(crate) struct Peer<'a> { chain: &'a mut crate::chain::Chain, work_min: crate::pow::Work, salt: Option, + branch_tip: Option, } #[derive(Debug)] @@ -466,11 +468,18 @@ impl<'a> Peer<'a> { self.chain, self.work_min, &mut self.salt, + &mut self.branch_tip, download, headers, clock, )?, - SyncState::Synced => receive_announcement(self.chain, self.work_min, headers, clock)?, + SyncState::Synced => receive_announcement( + self.chain, + self.work_min, + &mut self.branch_tip, + headers, + clock, + )?, SyncState::Due(_) => return Err(PeerError::UnsolicitedHeaders), }; match next { @@ -498,6 +507,7 @@ impl<'a> Peer<'a> { chain, work_min, salt, + branch_tip, } = self; let (ping, ping_nonce, ping_wake) = ping.tick(now, nonce)?; let (sync, get_headers, headers_wake) = sync.tick(now, chain, work_min)?; @@ -521,6 +531,7 @@ impl<'a> Peer<'a> { chain, work_min, salt, + branch_tip, }; Ok((peer, outbound, wake)) } @@ -613,6 +624,7 @@ fn receive_answer( chain: &mut crate::chain::Chain, work_min: crate::pow::Work, salt: &mut Option, + branch_tip: &mut Option, download: Download, headers: crate::protocol::Headers, clock: u64, @@ -626,13 +638,13 @@ fn receive_answer( let extended = chain .extend_on_locator(headers, clock, work_min) .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, full, extended, clock) + chain_next(chain, work_min, salt, branch_tip, full, 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, full, extended, clock) + chain_next(chain, work_min, salt, branch_tip, full, extended, clock) } Download::BranchPresync(sync) => { let BranchPresync { start, presync } = *sync; @@ -664,7 +676,7 @@ fn receive_answer( let extended = chain .extend_low_work_branch(start, released, full, clock, work_min) .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, full, extended, clock) + chain_next(chain, work_min, salt, branch_tip, full, extended, clock) } } } @@ -714,7 +726,9 @@ fn receive_on_genesis( { crate::chain::Extended::Taken => assert!(!empty), crate::chain::Extended::Kept => assert!(empty), - crate::chain::Extended::Branch(_) | crate::chain::Extended::BelowMargin { .. } => { + crate::chain::Extended::KeptOver(_) + | crate::chain::Extended::Branch(_) + | crate::chain::Extended::BelowMargin { .. } => { panic!("genesis has no header below it") } } @@ -730,6 +744,7 @@ fn chain_next( chain: &crate::chain::Chain, work_min: crate::pow::Work, salt: &mut Option, + branch_tip: &mut Option, full: bool, extended: crate::chain::Extended, clock: u64, @@ -748,6 +763,12 @@ fn chain_next( assert!(chain.tip().work() >= work_min); Ok(None) } + crate::chain::Extended::KeptOver(tip) => { + assert!(!full); + assert!(chain.tip().work() >= tip.work()); + *branch_tip = Some(tip); + Ok(None) + } crate::chain::Extended::BelowMargin { start, branch_work_min, @@ -766,9 +787,26 @@ fn chain_next( fn receive_announcement( chain: &mut crate::chain::Chain, work_min: crate::pow::Work, + branch_tip: &mut Option, headers: crate::protocol::Headers, clock: u64, ) -> Result, PeerError> { + let first_prev = headers.first().map(|first| first.prev); + match branch_tip.take() { + Some(tip) if first_prev == Some(tip.hash()) => { + assert!(tip.work() <= chain.tip().work()); + assert_ne!(tip.hash(), chain.tip().hash()); + let tip = tip + .extend(chain.network(), headers.as_slice()) + .map_err(PeerError::Extend)?; + if tip.work() > chain.tip().work() { + return Ok(Some(Download::on_tip(chain, work_min))); + } + *branch_tip = Some(tip); + return Ok(None); + } + tip => *branch_tip = tip, + } let full = headers.is_full(); match chain.extend(headers, clock) { Ok(()) => { @@ -1609,6 +1647,7 @@ pub(crate) mod fuzz { network: crate::chainparams::Network, work_min: u128, salt_left: bool, + branch_tip: Option<(bitcoin_hashes::sha256d::Hash, u128)>, download: DownloadModel, } @@ -1691,6 +1730,7 @@ pub(crate) mod fuzz { network, work_min, salt_left: true, + branch_tip: None, download: DownloadModel::Chain, } } @@ -1843,11 +1883,10 @@ pub(crate) mod fuzz { GetHeadersModel::Outstanding(_) => {} GetHeadersModel::Ended => { assert!(matches!(self.download, DownloadModel::Chain)); - if headers - .first() - .is_some_and(|first| first.prev != self.tip()) + if let Some(first) = headers.first() + && first.prev != self.tip() { - self.get_headers = GetHeadersModel::Due; + self.receive_off_tip(first.prev, &headers)?; return Ok(ReceivedExpected::NoReply); } } @@ -1867,9 +1906,13 @@ pub(crate) mod fuzz { self.chain_next(extended, full)? } DownloadModel::Branch { fork, hashes } => { - let extended = match headers.first() { - None => ExtendedModel::Kept, - Some(_) => { + let extended = match (headers.first(), hashes.last()) { + (None, None) => ExtendedModel::Kept, + (None, Some(last)) => { + self.branch_tip = Some((*last, self.work_at(fork + hashes.len()))); + ExtendedModel::Kept + } + (Some(_), _) => { let last = hashes.last().copied().unwrap_or(self.chain[fork]); let batch = script_hashes(self.network, last, &headers) .map_err(extend_error)?; @@ -1903,6 +1946,30 @@ pub(crate) mod fuzz { } } + fn receive_off_tip( + &mut self, + prev: bitcoin_hashes::sha256d::Hash, + headers: &[crate::block::BlockHeader], + ) -> Result<(), super::PeerError> { + if let Some((hash, work)) = self.branch_tip + && hash == prev + { + self.branch_tip = None; + let hashes = script_hashes(self.network, hash, headers).map_err(extend_error)?; + const { assert!(usize::BITS <= 64) }; + let work = work + hashes.len() as u128 * SCRIPT_HEADER_WORK; + if work <= self.work() { + let Some(last) = hashes.last() else { + panic!("an announcement off our tip has no header"); + }; + self.branch_tip = Some((*last, work)); + return Ok(()); + } + } + self.get_headers = GetHeadersModel::Due; + Ok(()) + } + fn extend_on_locator( &mut self, headers: &[crate::block::BlockHeader], @@ -1996,6 +2063,12 @@ pub(crate) mod fuzz { match extended { ExtendedModel::Taken => Ok(full.then_some(DownloadModel::Chain)), ExtendedModel::Kept => Ok(None), + ExtendedModel::Branch { fork, hashes } if !full => { + if let Some(last) = hashes.last() { + self.branch_tip = Some((*last, self.work_at(fork + hashes.len()))); + } + Ok(None) + } _ if !full => Ok(None), ExtendedModel::Branch { fork, hashes } => { Ok(Some(DownloadModel::Branch { fork, hashes })) @@ -3088,19 +3161,22 @@ mod tests { assert_eq!(tip, tip_after(&branch)); } - #[test] - fn second_low_work_sync_in_one_connection_ends_it() { - let start = std::time::Instant::now(); - let mut chain = regtest(); - let (peer, theirs) = low_work_sync_on_block_50(&mut chain, 2_100, start); - let peer = synced(peer, &theirs[2_050..]); - let peer = match peer.tick(start, 0) { + fn sent_send_headers(peer: super::Peer<'_>, now: std::time::Instant) -> super::Peer<'_> { + match peer.tick(now, 0) { Ok((peer, outbound, _)) => { assert!(outbound.send_headers, "{outbound:?}"); peer } other => panic!("{other:?}"), - }; + } + } + + #[test] + fn second_low_work_sync_in_one_connection_ends_it() { + let start = std::time::Instant::now(); + let mut chain = regtest(); + let (peer, theirs) = low_work_sync_on_block_50(&mut chain, 2_100, start); + let peer = sent_send_headers(synced(peer, &theirs[2_050..]), start); let (peer, get_headers) = batch_then_tick(peer, &theirs[2_149..], start); assert_eq!( get_headers, @@ -3114,6 +3190,27 @@ mod tests { assert_eq!(refused, Some(super::PeerError::SecondLowWorkSync)); } + #[test] + fn announcement_on_a_branch_that_lost_to_ours_asks_for_it_only_with_more_work_than_ours() { + let start = std::time::Instant::now(); + let mut chain = regtest(); + let (peer, theirs) = low_work_sync_on_block_50(&mut chain, 2_100, start); + let peer = sent_send_headers(synced(peer, &theirs[2_050..]), start); + let more = crate::fixtures::headers_on(&theirs[2_149], 52); + let peer = synced(peer, &more[..25]); + let (peer, _) = idle(peer, start); + let ours = crate::fixtures::headers_on_genesis(2_200); + let peer = synced(peer, &crate::fixtures::headers_on(&ours[2_199], 1)); + let (peer, _) = idle(peer, start); + let peer = synced(peer, &more[25..51]); + let (peer, _) = idle(peer, start); + let (peer, get_headers) = batch_then_tick(peer, &more[51..], start); + assert_eq!( + get_headers, + crate::protocol::GetHeaders::new(peer.chain.locator()) + ); + } + #[test] fn first_batch_on_genesis_that_starts_no_low_work_sync_keeps_the_salt_for_a_branch() { let start = std::time::Instant::now(); @@ -3123,13 +3220,7 @@ mod tests { crate::fixtures::branch_on(&crate::chainparams::Network::Regtest.genesis(), 2_000); let work_min = tip_after(&headers[..2]).work(); let peer = synced(asked_with(&mut chain, work_min, start), &headers[..1_000]); - let peer = match peer.tick(start, 0) { - Ok((peer, outbound, _)) => { - assert!(outbound.send_headers, "{outbound:?}"); - peer - } - other => panic!("{other:?}"), - }; + let peer = sent_send_headers(peer, start); let (peer, get_headers) = batch_then_tick(peer, &headers[1_000..], start); assert_eq!(get_headers, get_headers_on(&headers)); let (peer, get_headers) = batch_then_tick(peer, &branch, start); From 9a44f0029d0ca94f67f132d057ae643beac81b4d Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 13:31:12 -0300 Subject: [PATCH 02/13] test: hold with bitcoind that an announcement on a branch that lost to ours after a low-work sync gets no getheaders --- tests/bitcoind.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/bitcoind.rs b/tests/bitcoind.rs index 12d660f..87e6e93 100644 --- a/tests/bitcoind.rs +++ b/tests/bitcoind.rs @@ -525,6 +525,74 @@ fn branch_below_the_margin_takes_the_place_of_ours_after_a_low_work_sync() { assert!(ends.is_empty(), "elo ended the connection: {ends:?}"); } +#[test] +fn announcement_on_a_branch_that_lost_to_ours_after_a_low_work_sync_gets_no_getheaders() { + let bitcoind = Bitcoind::start(); + bitcoind.cli(&["generatetodescriptor", "3001", "raw(51)"]); + let second = bitcoind.cli(&["getblockhash", "2"]); + bitcoind.cli(&["invalidateblock", second.trim_end()]); + bitcoind.cli(&["generatetodescriptor", "2950", "raw(52)"]); + bitcoind.cli(&["reconsiderblock", second.trim_end()]); + let elo = Elo::start(&bitcoind); + let ours = elo.event("tip", SYNC_TIMEOUT); + 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); + + let get_headers = message_bytes(&bitcoind, "bytesrecv_per_msg", "getheaders"); + let headers_sent = message_bytes(&bitcoind, "bytessent_per_msg", "headers"); + bitcoind.cli(&["generatetodescriptor", "1", "raw(52)"]); + let start = std::time::Instant::now(); + while message_bytes(&bitcoind, "bytessent_per_msg", "headers") == headers_sent { + assert!( + start.elapsed() < PEER_INFO_TIMEOUT, + "bitcoind sent no headers" + ); + std::thread::sleep(std::time::Duration::from_millis(100)); + } + pong_from(&bitcoind, &elo, ends); + pong_from(&bitcoind, &elo, ends); + assert_eq!( + message_bytes(&bitcoind, "bytesrecv_per_msg", "getheaders"), + get_headers + ); +} + +fn pong_from(bitcoind: &Bitcoind, elo: &Elo, ends: usize) { + let start = std::time::Instant::now(); + let before = message_bytes(bitcoind, "bytesrecv_per_msg", "pong"); + bitcoind.cli(&["ping"]); + while message_bytes(bitcoind, "bytesrecv_per_msg", "pong") == before { + let all = elo.events("end"); + assert_eq!(all.len(), ends, "elo ended the connection: {all:?}"); + assert!( + start.elapsed() < PEER_INFO_TIMEOUT, + "bitcoind got no pong from elo" + ); + std::thread::sleep(std::time::Duration::from_millis(10)); + } + let all = elo.events("end"); + assert_eq!(all.len(), ends, "elo ended the connection: {all:?}"); +} + +fn message_bytes(bitcoind: &Bitcoind, field: &str, message: &str) -> u64 { + let peer_info = bitcoind.cli(&["getpeerinfo"]); + let key = format!("\"{message}\": "); + let Some((_, rest)) = object_text(&peer_info, field).split_once(&key) else { + return 0; + }; + let digits: String = rest.chars().take_while(char::is_ascii_digit).collect(); + digits.parse().unwrap() +} + fn wait_for_send_headers(bitcoind: &Bitcoind, elo: &Elo) { let start = std::time::Instant::now(); loop { From cf45c88f9046a6d4a98d3c86325c7439a9f057d9 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:18:01 -0300 Subject: [PATCH 03/13] fix: forget the branch tip when elo takes a branch, so that the tip is never a header of our chain After a reorg onto the branch of the tip, a peer could announce our tip again on the branch tip. The branch tip then moved to our tip, and the next announcement on our tip panicked at the assertion that the branch tip is not our tip. Each `Taken` in `chain_next` now clears the branch tip, in elo and in the `machines` model. A `Taken` that only extends our tip also clears it, which costs one more batch of the branch when the peer announces on it again. --- README.md | 7 ++++--- src/net_processing.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c1b41ab..c69af2d 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,10 @@ is on, as from genesis, and the minimum of that sync is the work of elo less the most one low-work sync, so a second one ends it, and the next connection gets a new salt. Each `getheaders` of a low-work sync or of a branch has only the hash of the last header that elo got. When a branch ends with no more work, elo keeps -the hash and the work of its last header until the connection ends. 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, and asks only when the branch has more work. +the hash and the work of its last header until it takes a branch or the +connection ends. 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, and asks only +when the branch has more work. 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 handshake on, it pings the peer every 2 min. diff --git a/src/net_processing.rs b/src/net_processing.rs index fce483d..9f98fee 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -751,6 +751,7 @@ fn chain_next( ) -> Result, PeerError> { match extended { crate::chain::Extended::Taken => { + *branch_tip = None; let next = Download::on_tip(chain, work_min); Ok(full.then_some(next)) } @@ -2061,7 +2062,10 @@ pub(crate) mod fuzz { full: bool, ) -> Result, super::PeerError> { match extended { - ExtendedModel::Taken => Ok(full.then_some(DownloadModel::Chain)), + ExtendedModel::Taken => { + self.branch_tip = None; + Ok(full.then_some(DownloadModel::Chain)) + } ExtendedModel::Kept => Ok(None), ExtendedModel::Branch { fork, hashes } if !full => { if let Some(last) = hashes.last() { @@ -3211,6 +3215,34 @@ mod tests { ); } + #[test] + fn branch_that_lost_to_ours_and_then_took_our_chain_is_no_longer_a_branch_tip() { + let start = std::time::Instant::now(); + let mut chain = regtest(); + let ours = crate::fixtures::headers_on_genesis(100); + let work_min = tip_after(&ours[..2]).work(); + let peer = synced(asked_with(&mut chain, work_min, start), &ours); + let peer = sent_send_headers(peer, start); + let branch = crate::fixtures::branch_on(&ours[97], 2); + let (peer, _) = batch_then_tick(peer, &branch, start); + let peer = synced(peer, &branch); + let more = crate::fixtures::headers_on(&branch[1], 2); + let (peer, _) = batch_then_tick(peer, &[branch[1], more[0]], start); + let peer = synced(peer, &[branch[0], branch[1], more[0]]); + let theirs = [&ours[..98], &branch[..], &more[..]].concat(); + assert_eq!(peer.chain.tip(), tip_after(&theirs[..101])); + let (peer, get_headers) = batch_then_tick(peer, &more[..1], start); + assert_eq!( + get_headers, + crate::protocol::GetHeaders::new(peer.chain.locator()) + ); + let peer = synced(peer, &[]); + let (peer, _) = idle(peer, start); + let peer = synced(peer, &more[1..]); + let (peer, _) = idle(peer, start); + assert_eq!(peer.chain.tip(), tip_after(&theirs)); + } + #[test] fn first_batch_on_genesis_that_starts_no_low_work_sync_keeps_the_salt_for_a_branch() { let start = std::time::Instant::now(); From 24af67e9255dcda67dc5d693f85afaf01a8f2095 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:18:33 -0300 Subject: [PATCH 04/13] refactor: make each branch tip with one function that takes the work at the fork and the work of the branch, and extend it only with `Headers` `Chain::branch_tip` took the work of the branch as `work_above_fork`, the name that `extend_above` gives to the work of our chain above the fork. It now takes `work_at_fork` and `work_branch`, and `extend_above` makes its tip with it too, so that each tip has the assertion that its work is at most ours. `BranchTip::extend` takes `Headers`, so that the type bounds its loop to 2,000 headers. The test of `BranchTip::extend` now has a batch that stops being continuous at its second header: the caller already checks the first header. --- src/chain.rs | 45 ++++++++++++++++++++++++++----------------- src/net_processing.rs | 2 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index 8c563cb..832be0e 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -212,7 +212,11 @@ impl Chain { AboveFork::Empty { .. } => Extended::Kept, AboveFork::Headers { work, last_hash, .. - } => Extended::KeptOver(self.branch_tip(branch.fork_height, work, last_hash)), + } => Extended::KeptOver(self.branch_tip( + self.work_at(branch.fork_height), + work, + last_hash, + )), }); } self.extend_above(branch, batch, clock, work_min) @@ -302,6 +306,7 @@ impl Chain { } assert!(work_above_fork > crate::pow::Work::ZERO); let time_max = time_max.max(time_max_before); + let work_at_fork = self.work.minus(work_above_fork); if work_branch <= work_above_fork { if full { return Ok(Extended::Branch(Branch { @@ -315,14 +320,13 @@ impl Chain { chain_work: self.work, })); } - let work_at_fork = self.work.minus(work_above_fork); - return Ok(Extended::KeptOver(BranchTip { - hash: last_hash, - work: work_at_fork.saturating_add(work_branch), - })); + return Ok(Extended::KeptOver(self.branch_tip( + work_at_fork, + work_branch, + last_hash, + ))); } - let work_at_fork = self.work.minus(work_above_fork); let count = headers.len(); self.tip = self.below_tip[fork_height]; self.below_tip.truncate(fork_height); @@ -394,11 +398,12 @@ impl Chain { fn branch_tip( &self, - fork_height: usize, - work_above_fork: crate::pow::Work, + work_at_fork: crate::pow::Work, + work_branch: crate::pow::Work, hash: bitcoin_hashes::sha256d::Hash, ) -> BranchTip { - let work = self.work_at(fork_height).saturating_add(work_above_fork); + assert!(work_at_fork <= self.work); + let work = work_at_fork.saturating_add(work_branch); assert!(work <= self.work); BranchTip { hash, work } } @@ -708,9 +713,9 @@ impl BranchTip { pub(crate) fn extend( self, network: crate::chainparams::Network, - headers: &[crate::block::BlockHeader], + headers: &crate::protocol::Headers, ) -> Result { - headers.iter().try_fold(self, |tip, header| { + headers.as_slice().iter().try_fold(self, |tip, header| { if header.prev != tip.hash { return Err(ExtendError::NotContinuous); } @@ -1973,17 +1978,21 @@ mod tests { let branch = crate::fixtures::branch_on(&genesis(), 3); let network = crate::chainparams::Network::Regtest; let tip = tip_of_branch(&branch[..1]); - assert_eq!(tip.extend(network, &[]), Ok(tip)); - assert_eq!( - tip.extend(network, &branch[1..]), - Ok(tip_of_branch(&branch)) - ); + let extend = |headers: &[crate::block::BlockHeader]| { + tip.extend(network, &crate::fixtures::headers_batch(headers)) + }; + assert_eq!(extend(&[]), Ok(tip)); + assert_eq!(extend(&branch[1..]), Ok(tip_of_branch(&branch))); let unproven = unproven(branch[2]); for (headers, error) in [ (&branch[2..], super::ExtendError::NotContinuous), + ( + &[branch[1], branch[1]][..], + super::ExtendError::NotContinuous, + ), (&[branch[1], unproven][..], super::ExtendError::ProofOfWork), ] { - assert_eq!(tip.extend(network, headers), Err(error)); + assert_eq!(extend(headers), Err(error), "{headers:?}"); } } } diff --git a/src/net_processing.rs b/src/net_processing.rs index 9f98fee..8639a3d 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -798,7 +798,7 @@ fn receive_announcement( assert!(tip.work() <= chain.tip().work()); assert_ne!(tip.hash(), chain.tip().hash()); let tip = tip - .extend(chain.network(), headers.as_slice()) + .extend(chain.network(), &headers) .map_err(PeerError::Extend)?; if tip.work() > chain.tip().work() { return Ok(Some(Download::on_tip(chain, work_min))); From 88e9a1df492f0eaeb282e713f48534a0b2db3122 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:18:59 -0300 Subject: [PATCH 05/13] fix: keep a branch tip for a batch below the margin that is not full, with a work that is not less than the work of the branch A batch below the margin that was not full gave `Kept`, so each announcement on its branch got the branch again. The early stop of `extend_above` has the work of our chain above the fork only up to the stop, so the work of our chain less that work is not less than the work at the fork. With the work of the branch on it, that is a bound of the work of the branch tip, and it is below ours less the margin. A bound that is too high can only make elo ask before the branch has more work, never after. The `machines` model makes the same bound, and its `extend_above` now takes `full` and gives `KeptOver` itself. --- README.md | 6 +++-- src/chain.rs | 17 +++++++++++-- src/net_processing.rs | 57 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c69af2d..1a4cee7 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,10 @@ most one low-work sync, so a second one ends it, and the next connection gets a new salt. Each `getheaders` of a low-work sync or of a branch has only the hash of the last header that elo got. When a branch ends with no more work, elo keeps the hash and the work of its last header until it takes a branch or the -connection ends. 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, and asks only +connection ends. For a branch below the margin, the work that elo keeps can be +more than the true work, but not less: the true work needs the work of each +header of elo above the 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, and asks only when the branch has more work. 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 diff --git a/src/chain.rs b/src/chain.rs index 832be0e..c54d053 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -289,7 +289,14 @@ impl Chain { if work_above_fork > work_stop { assert_eq!(branch_count, 0); return Ok(match (full, shared_count) { - (false, _) => Extended::Kept, + (false, _) => { + let work_at_fork_max = self.work.minus(work_above_fork); + Extended::KeptOver(self.branch_tip( + work_at_fork_max, + work_branch, + last_hash, + )) + } (true, 0) => match batch { Batch::Answer(answer) => Extended::BelowMargin { start: self.sync_start(fork_height), @@ -1853,7 +1860,13 @@ mod tests { ); assert_eq!(work_min, regtest_with(&headers[..2_001]).tip().work()); } - _ => assert_eq!(extended, Ok(super::Extended::Kept)), + _ => assert_eq!( + extended, + Ok(super::Extended::KeptOver(super::BranchTip { + hash: branch[1_998].hash(), + work: regtest_with(&headers[..2_000]).tip().work(), + })) + ), } assert_eq!(chain.tip(), regtest_with(&headers).tip()); } diff --git a/src/net_processing.rs b/src/net_processing.rs index 8639a3d..e0052fd 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -1680,6 +1680,10 @@ pub(crate) mod fuzz { fork: usize, hashes: Vec, }, + KeptOver { + hash: bitcoin_hashes::sha256d::Hash, + work: u128, + }, } #[derive(Debug)] @@ -1909,15 +1913,15 @@ pub(crate) mod fuzz { DownloadModel::Branch { fork, hashes } => { let extended = match (headers.first(), hashes.last()) { (None, None) => ExtendedModel::Kept, - (None, Some(last)) => { - self.branch_tip = Some((*last, self.work_at(fork + hashes.len()))); - ExtendedModel::Kept - } + (None, Some(last)) => ExtendedModel::KeptOver { + hash: *last, + work: self.work_at(fork + hashes.len()), + }, (Some(_), _) => { let last = hashes.last().copied().unwrap_or(self.chain[fork]); let batch = script_hashes(self.network, last, &headers) .map_err(extend_error)?; - self.extend_above(fork, hashes, batch) + self.extend_above(fork, hashes, batch, full) } }; self.chain_next(extended, full)? @@ -1995,7 +1999,8 @@ pub(crate) mod fuzz { self.chain.extend(batch); return Ok(ExtendedModel::Taken); } - Ok(self.extend_above(fork, Vec::new(), batch)) + let full = headers.len() == HEADERS_FULL_COUNT; + Ok(self.extend_above(fork, Vec::new(), batch, full)) } fn extend_above( @@ -2003,6 +2008,7 @@ pub(crate) mod fuzz { base: usize, branch: Vec, batch: Vec, + full: bool, ) -> ExtendedModel { assert!(!batch.is_empty()); assert!(base < self.height()); @@ -2033,6 +2039,15 @@ pub(crate) mod fuzz { let branch_work = self.work_at(fork + hashes.len()); if branch_work < self.branch_work_min() { assert_eq!(branch_count, 0); + if !full { + let Some(last) = hashes.last() else { + panic!("a branch below the margin has no header above the fork"); + }; + return ExtendedModel::KeptOver { + hash: *last, + work: self.below_margin_work_max(hashes.len()), + }; + } return if shared == 0 { ExtendedModel::BelowMargin { fork, hashes } } else { @@ -2043,6 +2058,15 @@ pub(crate) mod fuzz { }; } if branch_work <= self.work() { + if !full { + let Some(last) = hashes.last() else { + panic!("a branch with no more work has no header above the fork"); + }; + return ExtendedModel::KeptOver { + hash: *last, + work: branch_work, + }; + } return ExtendedModel::Branch { fork, hashes }; } self.chain.truncate(fork + 1); @@ -2050,6 +2074,17 @@ pub(crate) mod fuzz { ExtendedModel::Taken } + fn below_margin_work_max(&self, count: usize) -> u128 { + const { assert!(usize::BITS <= 64) }; + let work_branch = count as u128 * SCRIPT_HEADER_WORK; + let work_stop = work_branch + (self.work() - self.branch_work_min()); + let work_above_fork = (work_stop / SCRIPT_HEADER_WORK + 1) * SCRIPT_HEADER_WORK; + assert!(work_above_fork > work_stop); + let work = self.work() - work_above_fork + work_branch; + assert!(work < self.branch_work_min()); + work + } + fn branch_work_min(&self) -> u128 { const { assert!(usize::BITS <= 64) }; let behind = SCRIPT_HEADER_WORK * crate::chain::BRANCH_TIP_HEADERS_BEHIND_MAX as u128; @@ -2067,10 +2102,10 @@ pub(crate) mod fuzz { Ok(full.then_some(DownloadModel::Chain)) } ExtendedModel::Kept => Ok(None), - ExtendedModel::Branch { fork, hashes } if !full => { - if let Some(last) = hashes.last() { - self.branch_tip = Some((*last, self.work_at(fork + hashes.len()))); - } + ExtendedModel::KeptOver { hash, work } => { + assert!(!full); + assert!(work <= self.work()); + self.branch_tip = Some((hash, work)); Ok(None) } _ if !full => Ok(None), @@ -2155,7 +2190,7 @@ pub(crate) mod fuzz { self.chain.extend(buffer); return Ok(full.then_some(DownloadModel::Chain)); } - let extended = self.extend_above(start, Vec::new(), buffer); + let extended = self.extend_above(start, Vec::new(), buffer, full); assert!(!matches!(extended, ExtendedModel::BelowMargin { .. })); self.chain_next(extended, full) } else if full { From 881cd9131e3ce66c88c9cec2436ecc8ca05e8945 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:20:04 -0300 Subject: [PATCH 06/13] test: make the `machines` fuzz compare the branch tip of elo with the tip of the model after each message The script reaches an announcement on a branch tip only in a corner, so the tip of the model showed only through the messages that elo sends. The driver now compares the hash and the work of the two tips after each message that elo takes. --- src/net_processing.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/net_processing.rs b/src/net_processing.rs index e0052fd..0254afe 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -1273,6 +1273,7 @@ pub(crate) mod fuzz { Ok(received) => received, Err(error) => return ScriptEnd::PeerRefused(error), }; + branch_tip_check(&peer, &model); } now = now_next(script.byte(), now, wake); assert!(model.height() <= script.headers_written_count); @@ -1370,6 +1371,16 @@ pub(crate) mod fuzz { } } + fn branch_tip_check(peer: &super::Peer<'_>, model: &PeerModel) { + let tip = peer + .branch_tip + .map(|tip| (tip.hash(), tip.work().to_string())); + let expected = model + .branch_tip + .map(|(hash, work)| (hash, format!("{work:064x}"))); + assert_eq!(tip, expected); + } + fn now_next(byte: u8, now: std::time::Instant, wake: std::time::Instant) -> std::time::Instant { assert!(now < wake); let unit_count = u64::from(byte / 4); From 980e3181ab8d12b2485419b501ea3c7d77a8f95f Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:20:47 -0300 Subject: [PATCH 07/13] fix: ask with our locator for a full announcement on the branch tip, and read the branch tip without taking it `receive_announcement` asked for an announcement on our tip when it was full, but not for one on the branch tip. A full announcement with no more work than ours left the rest of the branch unknown until the next announcement. It now asks, as for more work, and the full checks of the answer apply to the headers. On regtest each header has the minimum work, and a branch tip has at least our work less the margin, so a full announcement always has more work there, and no test reaches this. On mainnet, 2,000 headers at the minimum work do not. `BranchTip` is `Copy`, so `receive_announcement` now reads it in place. The arm that put back the tip for each other announcement is gone. --- README.md | 4 ++-- src/net_processing.rs | 43 +++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 1a4cee7..67fc737 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,8 @@ the hash and the work of its last header until it takes a branch or the connection ends. For a branch below the margin, the work that elo keeps can be more than the true work, but not less: the true work needs the work of each header of elo above the 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, and asks only -when the branch has more work. +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 handshake on, it pings the peer every 2 min. diff --git a/src/net_processing.rs b/src/net_processing.rs index 0254afe..02e546b 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -792,23 +792,23 @@ fn receive_announcement( headers: crate::protocol::Headers, clock: u64, ) -> Result, PeerError> { + let full = headers.is_full(); let first_prev = headers.first().map(|first| first.prev); - match branch_tip.take() { - Some(tip) if first_prev == Some(tip.hash()) => { - assert!(tip.work() <= chain.tip().work()); - assert_ne!(tip.hash(), chain.tip().hash()); - let tip = tip - .extend(chain.network(), &headers) - .map_err(PeerError::Extend)?; - if tip.work() > chain.tip().work() { - return Ok(Some(Download::on_tip(chain, work_min))); - } - *branch_tip = Some(tip); - return Ok(None); + if let Some(tip) = *branch_tip + && first_prev == Some(tip.hash()) + { + assert!(tip.work() <= chain.tip().work()); + assert_ne!(tip.hash(), chain.tip().hash()); + let tip = tip + .extend(chain.network(), &headers) + .map_err(PeerError::Extend)?; + if full || tip.work() > chain.tip().work() { + *branch_tip = None; + return Ok(Some(Download::on_tip(chain, work_min))); } - tip => *branch_tip = tip, + *branch_tip = Some(tip); + return Ok(None); } - let full = headers.is_full(); match chain.extend(headers, clock) { Ok(()) => { let next = Download::on_tip(chain, work_min); @@ -1970,17 +1970,20 @@ pub(crate) mod fuzz { if let Some((hash, work)) = self.branch_tip && hash == prev { - self.branch_tip = None; let hashes = script_hashes(self.network, hash, headers).map_err(extend_error)?; const { assert!(usize::BITS <= 64) }; let work = work + hashes.len() as u128 * SCRIPT_HEADER_WORK; - if work <= self.work() { - let Some(last) = hashes.last() else { - panic!("an announcement off our tip has no header"); - }; - self.branch_tip = Some((*last, work)); + let full = headers.len() == HEADERS_FULL_COUNT; + if full || work > self.work() { + self.branch_tip = None; + self.get_headers = GetHeadersModel::Due; return Ok(()); } + let Some(last) = hashes.last() else { + panic!("an announcement off our tip has no header"); + }; + self.branch_tip = Some((*last, work)); + return Ok(()); } self.get_headers = GetHeadersModel::Due; Ok(()) From d57837ec3cc4c035469e0c5d2a3e1295658260c9 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:24:19 -0300 Subject: [PATCH 08/13] test: make the `machines` model extend our tip for an announcement on it as elo does, so that the announcement keeps the branch tip The model took an announcement on our tip through `extend_on_locator` and `chain_next`, so its `Taken` cleared the branch tip. elo takes it with `chain.extend` in `receive_announcement`, and keeps the branch tip. The comparison of the two tips found it in 300 s from the corpus, with a short batch below the margin and then 2 headers on our tip. That script is now the seed `fork-below-margin-short-kept-then-announce-on-tip`. --- ...rk-below-margin-short-kept-then-announce-on-tip | Bin 0 -> 38 bytes src/net_processing.rs | 8 ++++++++ 2 files changed, 8 insertions(+) create mode 100644 fuzz/corpus/machines/fork-below-margin-short-kept-then-announce-on-tip diff --git a/fuzz/corpus/machines/fork-below-margin-short-kept-then-announce-on-tip b/fuzz/corpus/machines/fork-below-margin-short-kept-then-announce-on-tip new file mode 100644 index 0000000000000000000000000000000000000000..579fcf208377c8b13140e46f745858e0d585e0bd GIT binary patch literal 38 rcmZQzWMpJuU}E54{SPELStl|uFmSOmF)(od{lTEdz`(}}WHSH&MpXm> literal 0 HcmV?d00001 diff --git a/src/net_processing.rs b/src/net_processing.rs index 02e546b..8002c36 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -987,6 +987,7 @@ pub(crate) mod fuzz { seed!("fork-below-margin-after-our-headers", "PeerOpen { height: 2200, synced: false }"), seed!("fork-below-margin-of-the-minimum", "PeerRefused(SecondLowWorkSync)"), seed!("fork-below-margin-short-kept", "PeerOpen { height: 2200, synced: true }"), + seed!("fork-below-margin-short-kept-then-announce-on-tip", "PeerOpen { height: 2147, synced: true }"), seed!("fork-full-at-margin-then-more-work", "PeerOpen { height: 2145, synced: true }"), seed!("fork-full-in-margin-then-empty", "PeerOpen { height: 2005, synced: true }"), seed!("fork-full-in-margin-then-more-work", "PeerOpen { height: 2006, synced: true }"), @@ -1895,6 +1896,7 @@ pub(crate) mod fuzz { .map_err(super::PeerError::Headers)? .into_vec(); assert!(headers.len() <= HEADERS_FULL_COUNT); + let announced = matches!(self.get_headers, GetHeadersModel::Ended); match self.get_headers { GetHeadersModel::Outstanding(_) => {} GetHeadersModel::Ended => { @@ -1911,6 +1913,12 @@ pub(crate) mod fuzz { let full = headers.len() == HEADERS_FULL_COUNT; let download = std::mem::replace(&mut self.download, DownloadModel::Chain); let next = match download { + DownloadModel::Chain if announced => { + let batch = + script_hashes(self.network, self.tip(), &headers).map_err(extend_error)?; + self.chain.extend(batch); + full.then_some(DownloadModel::Chain) + } DownloadModel::Chain if full && self.work() < self.work_min => { assert_eq!(self.height(), 0); let hashes = From 6576235e27730088af88b8788c09cd618e3439e6 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:41:09 -0300 Subject: [PATCH 09/13] refactor: name the work of a branch tip `work_max`, because below the margin it is a bound and not the true work --- src/chain.rs | 18 +++++++++--------- src/net_processing.rs | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index c54d053..cf97606 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -39,7 +39,7 @@ pub(crate) enum Extended { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct BranchTip { hash: bitcoin_hashes::sha256d::Hash, - work: crate::pow::Work, + work_max: crate::pow::Work, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -410,9 +410,9 @@ impl Chain { hash: bitcoin_hashes::sha256d::Hash, ) -> BranchTip { assert!(work_at_fork <= self.work); - let work = work_at_fork.saturating_add(work_branch); - assert!(work <= self.work); - BranchTip { hash, work } + let work_max = work_at_fork.saturating_add(work_branch); + assert!(work_max <= self.work); + BranchTip { hash, work_max } } fn branch_on_fork(&self, fork_height: usize) -> Branch { @@ -713,8 +713,8 @@ impl BranchTip { self.hash } - pub(crate) fn work(&self) -> crate::pow::Work { - self.work + pub(crate) fn work_max(&self) -> crate::pow::Work { + self.work_max } pub(crate) fn extend( @@ -730,7 +730,7 @@ impl BranchTip { crate::pow::proven(header, network).ok_or(ExtendError::ProofOfWork)?; Ok(BranchTip { hash, - work: tip.work.saturating_add(work), + work_max: tip.work_max.saturating_add(work), }) }) } @@ -1574,7 +1574,7 @@ mod tests { let tip = regtest_with(headers).tip(); super::BranchTip { hash: tip.hash(), - work: tip.work(), + work_max: tip.work(), } } @@ -1864,7 +1864,7 @@ mod tests { extended, Ok(super::Extended::KeptOver(super::BranchTip { hash: branch[1_998].hash(), - work: regtest_with(&headers[..2_000]).tip().work(), + work_max: regtest_with(&headers[..2_000]).tip().work(), })) ), } diff --git a/src/net_processing.rs b/src/net_processing.rs index 8002c36..befc49b 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -766,7 +766,7 @@ fn chain_next( } crate::chain::Extended::KeptOver(tip) => { assert!(!full); - assert!(chain.tip().work() >= tip.work()); + assert!(chain.tip().work() >= tip.work_max()); *branch_tip = Some(tip); Ok(None) } @@ -797,12 +797,12 @@ fn receive_announcement( if let Some(tip) = *branch_tip && first_prev == Some(tip.hash()) { - assert!(tip.work() <= chain.tip().work()); + assert!(tip.work_max() <= chain.tip().work()); assert_ne!(tip.hash(), chain.tip().hash()); let tip = tip .extend(chain.network(), &headers) .map_err(PeerError::Extend)?; - if full || tip.work() > chain.tip().work() { + if full || tip.work_max() > chain.tip().work() { *branch_tip = None; return Ok(Some(Download::on_tip(chain, work_min))); } @@ -1375,7 +1375,7 @@ pub(crate) mod fuzz { fn branch_tip_check(peer: &super::Peer<'_>, model: &PeerModel) { let tip = peer .branch_tip - .map(|tip| (tip.hash(), tip.work().to_string())); + .map(|tip| (tip.hash(), tip.work_max().to_string())); let expected = model .branch_tip .map(|(hash, work)| (hash, format!("{work:064x}"))); @@ -1694,7 +1694,7 @@ pub(crate) mod fuzz { }, KeptOver { hash: bitcoin_hashes::sha256d::Hash, - work: u128, + work_max: u128, }, } @@ -1934,7 +1934,7 @@ pub(crate) mod fuzz { (None, None) => ExtendedModel::Kept, (None, Some(last)) => ExtendedModel::KeptOver { hash: *last, - work: self.work_at(fork + hashes.len()), + work_max: self.work_at(fork + hashes.len()), }, (Some(_), _) => { let last = hashes.last().copied().unwrap_or(self.chain[fork]); @@ -2067,7 +2067,7 @@ pub(crate) mod fuzz { }; return ExtendedModel::KeptOver { hash: *last, - work: self.below_margin_work_max(hashes.len()), + work_max: self.below_margin_work_max(hashes.len()), }; } return if shared == 0 { @@ -2086,7 +2086,7 @@ pub(crate) mod fuzz { }; return ExtendedModel::KeptOver { hash: *last, - work: branch_work, + work_max: branch_work, }; } return ExtendedModel::Branch { fork, hashes }; @@ -2124,10 +2124,10 @@ pub(crate) mod fuzz { Ok(full.then_some(DownloadModel::Chain)) } ExtendedModel::Kept => Ok(None), - ExtendedModel::KeptOver { hash, work } => { + ExtendedModel::KeptOver { hash, work_max } => { assert!(!full); - assert!(work <= self.work()); - self.branch_tip = Some((hash, work)); + assert!(work_max <= self.work()); + self.branch_tip = Some((hash, work_max)); Ok(None) } _ if !full => Ok(None), From fa6b9e2fc0eb570a81ecc73772d68a7346353ece Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:42:06 -0300 Subject: [PATCH 10/13] refactor: make the chain decide if an announcement on a branch tip keeps the tip or asks, so that each branch tip has no more work than ours --- src/chain.rs | 72 +++++++++++++++++++++++++++---------------- src/net_processing.rs | 25 +++++++-------- 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index cf97606..1d7e457 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -42,6 +42,12 @@ pub(crate) struct BranchTip { work_max: crate::pow::Work, } +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Announced { + Kept(BranchTip), + Ask, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct SyncStart { network: crate::chainparams::Network, @@ -415,6 +421,30 @@ impl Chain { BranchTip { hash, work_max } } + pub(crate) fn extend_branch_tip( + &self, + tip: BranchTip, + headers: &crate::protocol::Headers, + ) -> Result { + assert!(tip.work_max <= self.work); + assert_ne!(tip.hash, self.tip.hash()); + let (hash, work_max) = headers.as_slice().iter().try_fold( + (tip.hash, tip.work_max), + |(hash, work_max), header| { + if header.prev != hash { + return Err(ExtendError::NotContinuous); + } + let (hash, work) = + crate::pow::proven(header, self.network).ok_or(ExtendError::ProofOfWork)?; + Ok((hash, work_max.saturating_add(work))) + }, + )?; + if headers.is_full() || work_max > self.work { + return Ok(Announced::Ask); + } + Ok(Announced::Kept(BranchTip { hash, work_max })) + } + fn branch_on_fork(&self, fork_height: usize) -> Branch { assert!(fork_height < self.below_tip.len()); Branch { @@ -713,27 +743,10 @@ impl BranchTip { self.hash } + #[cfg(fuzzing)] pub(crate) fn work_max(&self) -> crate::pow::Work { self.work_max } - - pub(crate) fn extend( - self, - network: crate::chainparams::Network, - headers: &crate::protocol::Headers, - ) -> Result { - headers.as_slice().iter().try_fold(self, |tip, header| { - if header.prev != tip.hash { - return Err(ExtendError::NotContinuous); - } - let (hash, work) = - crate::pow::proven(header, network).ok_or(ExtendError::ProofOfWork)?; - Ok(BranchTip { - hash, - work_max: tip.work_max.saturating_add(work), - }) - }) - } } impl std::fmt::Display for ExtendError { @@ -1987,17 +2000,22 @@ mod tests { } #[test] - fn branch_tip_takes_the_work_of_headers_on_it_that_have_their_proof_of_work() { - let branch = crate::fixtures::branch_on(&genesis(), 3); - let network = crate::chainparams::Network::Regtest; + fn extend_branch_tip_keeps_a_tip_with_no_more_work_and_asks_for_more_work_or_a_full_batch() { + let headers = crate::fixtures::headers_on_genesis(2_100); + let branch = crate::fixtures::branch_on(&genesis(), 2_001); let tip = tip_of_branch(&branch[..1]); - let extend = |headers: &[crate::block::BlockHeader]| { - tip.extend(network, &crate::fixtures::headers_batch(headers)) + let extend = |count: usize, theirs: &[crate::block::BlockHeader]| { + regtest_with(&headers[..count]) + .extend_branch_tip(tip, &crate::fixtures::headers_batch(theirs)) }; - assert_eq!(extend(&[]), Ok(tip)); - assert_eq!(extend(&branch[1..]), Ok(tip_of_branch(&branch))); + let kept = |count: usize| Ok(super::Announced::Kept(tip_of_branch(&branch[..count]))); + assert_eq!(extend(3, &[]), kept(1)); + assert_eq!(extend(3, &branch[1..3]), kept(3)); + assert_eq!(extend(3, &branch[1..4]), Ok(super::Announced::Ask)); + assert_eq!(extend(2_100, &branch[1..2_000]), kept(2_000)); + assert_eq!(extend(2_100, &branch[1..]), Ok(super::Announced::Ask)); let unproven = unproven(branch[2]); - for (headers, error) in [ + for (theirs, error) in [ (&branch[2..], super::ExtendError::NotContinuous), ( &[branch[1], branch[1]][..], @@ -2005,7 +2023,7 @@ mod tests { ), (&[branch[1], unproven][..], super::ExtendError::ProofOfWork), ] { - assert_eq!(extend(headers), Err(error), "{headers:?}"); + assert_eq!(extend(3, theirs), Err(error), "{theirs:?}"); } } } diff --git a/src/net_processing.rs b/src/net_processing.rs index befc49b..9adaa5b 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -766,7 +766,6 @@ fn chain_next( } crate::chain::Extended::KeptOver(tip) => { assert!(!full); - assert!(chain.tip().work() >= tip.work_max()); *branch_tip = Some(tip); Ok(None) } @@ -797,17 +796,19 @@ fn receive_announcement( if let Some(tip) = *branch_tip && first_prev == Some(tip.hash()) { - assert!(tip.work_max() <= chain.tip().work()); - assert_ne!(tip.hash(), chain.tip().hash()); - let tip = tip - .extend(chain.network(), &headers) - .map_err(PeerError::Extend)?; - if full || tip.work_max() > chain.tip().work() { - *branch_tip = None; - return Ok(Some(Download::on_tip(chain, work_min))); - } - *branch_tip = Some(tip); - return Ok(None); + return match chain + .extend_branch_tip(tip, &headers) + .map_err(PeerError::Extend)? + { + crate::chain::Announced::Kept(tip) => { + *branch_tip = Some(tip); + Ok(None) + } + crate::chain::Announced::Ask => { + *branch_tip = None; + Ok(Some(Download::on_tip(chain, work_min))) + } + }; } match chain.extend(headers, clock) { Ok(()) => { From d1062cb95b70f5f82270efe1878f9bef787c8597 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:42:46 -0300 Subject: [PATCH 11/13] refactor: put `full` in `Extended::Taken`, the only result that reads it, so that `chain_next` has no `full` to assert against the other results --- src/chain.rs | 34 ++++++++++++++++++++-------------- src/net_processing.rs | 17 ++++++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/chain.rs b/src/chain.rs index 1d7e457..a326568 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -25,7 +25,9 @@ pub(crate) const BRANCH_TIP_HEADERS_BEHIND_MAX: usize = 144; #[derive(Debug, PartialEq, Eq)] pub(crate) enum Extended { - Taken, + Taken { + full: bool, + }, Kept, KeptOver(BranchTip), Branch(Branch), @@ -151,8 +153,9 @@ impl Chain { return Ok(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); + return Ok(Extended::Taken { full }); } let tip_height = self.below_tip.len(); let first_prev = first.prev; @@ -269,7 +272,7 @@ impl Chain { if !rest.is_empty() { self.extend_tip(rest.to_vec(), time_max)?; } - return Ok(Extended::Taken); + return Ok(Extended::Taken { full }); } if rest.is_empty() { if full { @@ -347,7 +350,7 @@ impl Chain { self.extend_store(headers, time_max, work_at_fork.saturating_add(work_branch)); assert_eq!(self.tip.hash(), last_hash); assert_eq!(self.below_tip.len(), fork_height + count); - Ok(Extended::Taken) + Ok(Extended::Taken { full }) } fn extend_tip( @@ -1684,7 +1687,7 @@ mod tests { let mut chain = regtest_with(&headers[..1]); assert_eq!( on_locator(&mut chain, &headers[1..]), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: false }) ); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } @@ -1705,7 +1708,7 @@ mod tests { let mut chain = regtest_with(&headers); let (expected, ours) = if taken { ( - super::Extended::Taken, + super::Extended::Taken { full: false }, [&headers[..1], &branch[..]].concat(), ) } else { @@ -1723,16 +1726,19 @@ mod tests { let branch = crate::fixtures::branch_on(&headers[0], 3); let mut chain = regtest_with(&headers); assert_eq!(branch[0].time, chain.sync_start_on_tip().median_time_past()); - assert_eq!(on_locator(&mut chain, &branch), Ok(super::Extended::Taken)); + assert_eq!( + on_locator(&mut chain, &branch), + Ok(super::Extended::Taken { full: false }) + ); } #[test] fn extend_on_locator_refuses_a_batch_on_a_header_that_is_not_in_the_locator() { let headers = crate::fixtures::headers_on_genesis(14); for (below, expected) in [ - (&headers[0], Ok(super::Extended::Taken)), + (&headers[0], Ok(super::Extended::Taken { full: false })), (&headers[1], Err(super::ExtendError::NotOnLocator)), - (&headers[2], Ok(super::Extended::Taken)), + (&headers[2], Ok(super::Extended::Taken { full: false })), ] { let branch = crate::fixtures::branch_on(below, 20); let mut chain = regtest_with(&headers); @@ -1765,7 +1771,7 @@ mod tests { assert_eq!(branch, chain.branch_on_fork(2_000)); assert_eq!( on_branch(&mut chain, branch, &headers[2_000..]), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: false }) ); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } @@ -1797,7 +1803,7 @@ mod tests { assert_eq!(chain.tip(), regtest_with(&headers).tip()); assert_eq!( on_branch(&mut chain, pending, &branch[2_000..]), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: false }) ); assert_eq!(chain.tip(), regtest_with(&branch).tip()); } @@ -1845,7 +1851,7 @@ mod tests { clock, crate::pow::Work::ZERO ), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: false }) ); assert_eq!( chain.tip(), @@ -1920,7 +1926,7 @@ mod tests { assert_eq!(pending, chain.branch_on_fork(1_000)); assert_eq!( on_branch(&mut chain, pending, &branch), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: true }) ); let ours = [&headers[..1_000], &branch[..]].concat(); assert_eq!(chain.tip(), regtest_with(&ours).tip()); @@ -1983,7 +1989,7 @@ mod tests { ); match (count, full) { (2_300, _) => { - assert_eq!(extended, Ok(super::Extended::Taken)); + assert_eq!(extended, Ok(super::Extended::Taken { full })); assert_eq!(chain.tip(), regtest_with(&branch).tip()); } (_, false) => { diff --git a/src/net_processing.rs b/src/net_processing.rs index 9adaa5b..547f58f 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -638,13 +638,13 @@ fn receive_answer( let extended = chain .extend_on_locator(headers, clock, work_min) .map_err(PeerError::Extend)?; - chain_next(chain, work_min, salt, branch_tip, full, extended, clock) + 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, full, extended, clock) + chain_next(chain, work_min, salt, branch_tip, extended, clock) } Download::BranchPresync(sync) => { let BranchPresync { start, presync } = *sync; @@ -676,7 +676,7 @@ fn receive_answer( 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, full, extended, clock) + chain_next(chain, work_min, salt, branch_tip, extended, clock) } } } @@ -724,7 +724,10 @@ fn receive_on_genesis( .extend_on_locator(headers, clock, work_min) .map_err(PeerError::Extend)? { - crate::chain::Extended::Taken => assert!(!empty), + crate::chain::Extended::Taken { full } => { + assert!(!full); + assert!(!empty); + } crate::chain::Extended::Kept => assert!(empty), crate::chain::Extended::KeptOver(_) | crate::chain::Extended::Branch(_) @@ -745,18 +748,16 @@ fn chain_next( work_min: crate::pow::Work, salt: &mut Option, branch_tip: &mut Option, - full: bool, extended: crate::chain::Extended, clock: u64, ) -> Result, PeerError> { match extended { - crate::chain::Extended::Taken => { + crate::chain::Extended::Taken { full } => { *branch_tip = None; let next = Download::on_tip(chain, work_min); Ok(full.then_some(next)) } crate::chain::Extended::Branch(branch) => { - assert!(full); assert!(chain.tip().work() >= work_min); Ok(Some(Download::Branch(Box::new(branch)))) } @@ -765,7 +766,6 @@ fn chain_next( Ok(None) } crate::chain::Extended::KeptOver(tip) => { - assert!(!full); *branch_tip = Some(tip); Ok(None) } @@ -774,7 +774,6 @@ fn chain_next( branch_work_min, headers, } => { - assert!(full); assert!(chain.tip().work() >= work_min); let salt = salt.take().ok_or(PeerError::SecondLowWorkSync)?; let presync = crate::headerssync::Presync::new(start, branch_work_min, clock, salt); From 8e56ba838ff9d87ab81da2ae55573bf5e8243bb8 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:45:57 -0300 Subject: [PATCH 12/13] fix: keep a branch tip while our chain has the tip that it had when elo kept the branch, so that the chain and not each caller drops a tip after a reorg --- README.md | 13 +++--- src/chain.rs | 101 ++++++++++++++++++++++++++++++++++++------ src/net_processing.rs | 47 +++++++++++++++----- 3 files changed, 132 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 67fc737..30dd066 100644 --- a/README.md +++ b/README.md @@ -39,12 +39,13 @@ is on, as from genesis, and the minimum of that sync is the work of elo less the most one low-work sync, so a second one ends it, and the next connection gets a new salt. Each `getheaders` of a low-work sync or of a branch has only the hash of the last header that elo got. When a branch ends with no more work, elo keeps -the hash and the work of its last header until it takes a branch or the -connection ends. For a branch below the margin, the work that elo keeps can be -more than the true work, but not less: the true work needs the work of each -header of elo above the 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. +the hash and the work of its last header, and the height and hash of its own +tip. It drops them when that tip leaves its chain, or when the connection ends. +For a branch below the margin, the work that elo keeps can be more than the true +work, but not less: the true work needs the work of each header of elo above the +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 handshake on, it pings the peer every 2 min. diff --git a/src/chain.rs b/src/chain.rs index a326568..d3b8449 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -42,6 +42,8 @@ pub(crate) enum Extended { pub(crate) struct BranchTip { hash: bitcoin_hashes::sha256d::Hash, work_max: crate::pow::Work, + ours_height: usize, + ours_hash: bitcoin_hashes::sha256d::Hash, } #[derive(Debug, PartialEq, Eq)] @@ -421,7 +423,21 @@ impl Chain { assert!(work_at_fork <= self.work); let work_max = work_at_fork.saturating_add(work_branch); assert!(work_max <= self.work); - BranchTip { hash, work_max } + BranchTip { + hash, + work_max, + ours_height: self.below_tip.len(), + ours_hash: self.tip.hash(), + } + } + + pub(crate) fn branch_tip_current(&self, tip: BranchTip) -> Option { + self.has_ours(tip).then_some(tip) + } + + fn has_ours(&self, tip: BranchTip) -> bool { + tip.ours_height <= self.below_tip.len() + && self.header_at(tip.ours_height).hash() == tip.ours_hash } pub(crate) fn extend_branch_tip( @@ -429,6 +445,7 @@ impl Chain { tip: BranchTip, headers: &crate::protocol::Headers, ) -> Result { + assert!(self.has_ours(tip)); assert!(tip.work_max <= self.work); assert_ne!(tip.hash, self.tip.hash()); let (hash, work_max) = headers.as_slice().iter().try_fold( @@ -445,7 +462,12 @@ impl Chain { if headers.is_full() || work_max > self.work { return Ok(Announced::Ask); } - Ok(Announced::Kept(BranchTip { hash, work_max })) + Ok(Announced::Kept(BranchTip { + hash, + work_max, + ours_height: self.below_tip.len(), + ours_hash: self.tip.hash(), + })) } fn branch_on_fork(&self, fork_height: usize) -> Branch { @@ -1586,16 +1608,24 @@ mod tests { ) } - fn tip_of_branch(headers: &[crate::block::BlockHeader]) -> super::BranchTip { + fn tip_of_branch( + headers: &[crate::block::BlockHeader], + ours: &[crate::block::BlockHeader], + ) -> super::BranchTip { let tip = regtest_with(headers).tip(); super::BranchTip { hash: tip.hash(), work_max: tip.work(), + ours_height: ours.len(), + ours_hash: regtest_with(ours).tip().hash(), } } - fn kept_over(headers: &[crate::block::BlockHeader]) -> super::Extended { - super::Extended::KeptOver(tip_of_branch(headers)) + fn kept_over( + headers: &[crate::block::BlockHeader], + ours: &[crate::block::BlockHeader], + ) -> super::Extended { + super::Extended::KeptOver(tip_of_branch(headers, ours)) } fn branch_of(extended: Result) -> super::Branch { @@ -1713,7 +1743,7 @@ mod tests { ) } else { let theirs = [&headers[..1], &branch[..]].concat(); - (kept_over(&theirs), headers.to_vec()) + (kept_over(&theirs, &headers), headers.to_vec()) }; assert_eq!(on_locator(&mut chain, &branch), Ok(expected), "{count}"); assert_eq!(chain.tip(), regtest_with(&ours).tip(), "{count}"); @@ -1815,7 +1845,10 @@ mod tests { for (rest, theirs) in [(&branch[2_000..], &branch[..]), (&[], &branch[..2_000])] { let mut chain = regtest_with(&headers); let pending = branch_of(on_locator(&mut chain, &branch[..2_000])); - assert_eq!(on_branch(&mut chain, pending, rest), Ok(kept_over(theirs))); + assert_eq!( + on_branch(&mut chain, pending, rest), + Ok(kept_over(theirs, &headers)) + ); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } } @@ -1884,6 +1917,8 @@ mod tests { Ok(super::Extended::KeptOver(super::BranchTip { hash: branch[1_998].hash(), work_max: regtest_with(&headers[..2_000]).tip().work(), + ours_height: headers.len(), + ours_hash: regtest_with(&headers).tip().hash(), })) ), } @@ -1993,7 +2028,7 @@ mod tests { assert_eq!(chain.tip(), regtest_with(&branch).tip()); } (_, false) => { - assert_eq!(extended, Ok(kept_over(&branch)), "{count}"); + assert_eq!(extended, Ok(kept_over(&branch, &headers)), "{count}"); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } (_, true) => { @@ -2009,16 +2044,21 @@ mod tests { fn extend_branch_tip_keeps_a_tip_with_no_more_work_and_asks_for_more_work_or_a_full_batch() { let headers = crate::fixtures::headers_on_genesis(2_100); let branch = crate::fixtures::branch_on(&genesis(), 2_001); - let tip = tip_of_branch(&branch[..1]); + let tip = tip_of_branch(&branch[..1], &headers[..3]); let extend = |count: usize, theirs: &[crate::block::BlockHeader]| { regtest_with(&headers[..count]) .extend_branch_tip(tip, &crate::fixtures::headers_batch(theirs)) }; - let kept = |count: usize| Ok(super::Announced::Kept(tip_of_branch(&branch[..count]))); - assert_eq!(extend(3, &[]), kept(1)); - assert_eq!(extend(3, &branch[1..3]), kept(3)); + let kept = |count: usize, ours: usize| { + Ok(super::Announced::Kept(tip_of_branch( + &branch[..count], + &headers[..ours], + ))) + }; + assert_eq!(extend(3, &[]), kept(1, 3)); + assert_eq!(extend(3, &branch[1..3]), kept(3, 3)); assert_eq!(extend(3, &branch[1..4]), Ok(super::Announced::Ask)); - assert_eq!(extend(2_100, &branch[1..2_000]), kept(2_000)); + assert_eq!(extend(2_100, &branch[1..2_000]), kept(2_000, 2_100)); assert_eq!(extend(2_100, &branch[1..]), Ok(super::Announced::Ask)); let unproven = unproven(branch[2]); for (theirs, error) in [ @@ -2032,4 +2072,39 @@ mod tests { assert_eq!(extend(3, theirs), Err(error), "{theirs:?}"); } } + + #[test] + fn branch_tip_is_current_while_our_chain_has_our_tip_of_the_time_it_was_kept() { + let headers = crate::fixtures::headers_on_genesis(4); + let mut chain = regtest_with(&headers[..3]); + let branch = crate::fixtures::branch_on(&headers[0], 1); + let Ok(super::Extended::KeptOver(tip)) = on_locator(&mut chain, &branch) else { + panic!("no branch tip"); + }; + assert_eq!( + tip, + tip_of_branch(&[&headers[..1], &branch[..]].concat(), &headers[..3]) + ); + assert_eq!(chain.branch_tip_current(tip), Some(tip)); + assert_eq!( + on_locator(&mut chain, &headers[3..]), + Ok(super::Extended::Taken { full: false }) + ); + assert_eq!(chain.branch_tip_current(tip), Some(tip)); + let above = crate::fixtures::branch_on(&headers[2], 2); + assert_eq!( + on_locator(&mut chain, &above), + Ok(super::Extended::Taken { full: false }) + ); + assert_eq!(chain.branch_tip_current(tip), Some(tip)); + let below = crate::fixtures::branch_on(&headers[1], 4); + assert_eq!( + on_locator(&mut chain, &below), + Ok(super::Extended::Taken { full: false }) + ); + assert_eq!(chain.branch_tip_current(tip), None); + let mut chain = regtest_with(&headers[..3]); + chain.restart(); + assert_eq!(chain.branch_tip_current(tip), None); + } } diff --git a/src/net_processing.rs b/src/net_processing.rs index 547f58f..19c5e3b 100644 --- a/src/net_processing.rs +++ b/src/net_processing.rs @@ -753,7 +753,6 @@ fn chain_next( ) -> Result, PeerError> { match extended { crate::chain::Extended::Taken { full } => { - *branch_tip = None; let next = Download::on_tip(chain, work_min); Ok(full.then_some(next)) } @@ -792,6 +791,7 @@ fn receive_announcement( ) -> Result, PeerError> { let full = headers.is_full(); let first_prev = headers.first().map(|first| first.prev); + *branch_tip = branch_tip.and_then(|tip| chain.branch_tip_current(tip)); if let Some(tip) = *branch_tip && first_prev == Some(tip.hash()) { @@ -1375,10 +1375,11 @@ pub(crate) mod fuzz { fn branch_tip_check(peer: &super::Peer<'_>, model: &PeerModel) { let tip = peer .branch_tip + .and_then(|tip| peer.chain.branch_tip_current(tip)) .map(|tip| (tip.hash(), tip.work_max().to_string())); let expected = model .branch_tip - .map(|(hash, work)| (hash, format!("{work:064x}"))); + .map(|(hash, work, _)| (hash, format!("{work:064x}"))); assert_eq!(tip, expected); } @@ -1660,7 +1661,7 @@ pub(crate) mod fuzz { network: crate::chainparams::Network, work_min: u128, salt_left: bool, - branch_tip: Option<(bitcoin_hashes::sha256d::Hash, u128)>, + branch_tip: Option<(bitcoin_hashes::sha256d::Hash, u128, usize)>, download: DownloadModel, } @@ -1975,7 +1976,7 @@ pub(crate) mod fuzz { prev: bitcoin_hashes::sha256d::Hash, headers: &[crate::block::BlockHeader], ) -> Result<(), super::PeerError> { - if let Some((hash, work)) = self.branch_tip + if let Some((hash, work, _)) = self.branch_tip && hash == prev { let hashes = script_hashes(self.network, hash, headers).map_err(extend_error)?; @@ -1990,7 +1991,7 @@ pub(crate) mod fuzz { let Some(last) = hashes.last() else { panic!("an announcement off our tip has no header"); }; - self.branch_tip = Some((*last, work)); + self.branch_tip = Some((*last, work, self.height())); return Ok(()); } self.get_headers = GetHeadersModel::Due; @@ -2091,6 +2092,11 @@ pub(crate) mod fuzz { } return ExtendedModel::Branch { fork, hashes }; } + if let Some((_, _, height)) = self.branch_tip + && fork < height + { + self.branch_tip = None; + } self.chain.truncate(fork + 1); self.chain.extend(hashes); ExtendedModel::Taken @@ -2119,15 +2125,12 @@ pub(crate) mod fuzz { full: bool, ) -> Result, super::PeerError> { match extended { - ExtendedModel::Taken => { - self.branch_tip = None; - Ok(full.then_some(DownloadModel::Chain)) - } + ExtendedModel::Taken => Ok(full.then_some(DownloadModel::Chain)), ExtendedModel::Kept => Ok(None), ExtendedModel::KeptOver { hash, work_max } => { assert!(!full); assert!(work_max <= self.work()); - self.branch_tip = Some((hash, work_max)); + self.branch_tip = Some((hash, work_max, self.height())); Ok(None) } _ if !full => Ok(None), @@ -3300,6 +3303,30 @@ mod tests { assert_eq!(peer.chain.tip(), tip_after(&theirs)); } + #[test] + fn branch_that_lost_to_ours_stays_a_branch_tip_after_an_answer_that_extends_our_tip() { + let start = std::time::Instant::now(); + let mut chain = regtest(); + let ours = crate::fixtures::headers_on_genesis(101); + let work_min = tip_after(&ours[..2]).work(); + let peer = synced(asked_with(&mut chain, work_min, start), &ours[..100]); + let peer = sent_send_headers(peer, start); + let branch = crate::fixtures::branch_on(&ours[97], 2); + let (peer, _) = batch_then_tick(peer, &branch, start); + let peer = synced(peer, &branch); + let (peer, _) = batch_then_tick(peer, &ours[99..], start); + let peer = synced(peer, &ours[100..]); + assert_eq!(peer.chain.tip(), tip_after(&ours)); + let more = crate::fixtures::headers_on(&branch[1], 2); + let peer = synced(peer, &more[..1]); + let (peer, _) = idle(peer, start); + let (peer, get_headers) = batch_then_tick(peer, &more[1..], start); + assert_eq!( + get_headers, + crate::protocol::GetHeaders::new(peer.chain.locator()) + ); + } + #[test] fn first_batch_on_genesis_that_starts_no_low_work_sync_keeps_the_salt_for_a_branch() { let start = std::time::Instant::now(); From 19a24db1067d88ffdad6cde2f231f5467a932190 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 17:47:40 -0300 Subject: [PATCH 13/13] test: skip the mutants of `BranchTip::work_max`, which only the fuzz build has --- src/chain.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/chain.rs b/src/chain.rs index d3b8449..3c12934 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -769,6 +769,7 @@ impl BranchTip { } #[cfg(fuzzing)] + #[cfg_attr(false, mutants::skip)] pub(crate) fn work_max(&self) -> crate::pow::Work { self.work_max }