diff --git a/README.md b/README.md index 2680fcd..30dd066 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,14 @@ 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, 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/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 0000000..579fcf2 Binary files /dev/null and b/fuzz/corpus/machines/fork-below-margin-short-kept-then-announce-on-tip differ diff --git a/src/chain.rs b/src/chain.rs index 8d8c3f2..3c12934 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -25,8 +25,11 @@ 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), BelowMargin { start: SyncStart, @@ -35,6 +38,20 @@ pub(crate) enum Extended { }, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +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)] +pub(crate) enum Announced { + Kept(BranchTip), + Ask, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct SyncStart { network: crate::chainparams::Network, @@ -138,8 +155,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; @@ -201,7 +219,16 @@ 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( + self.work_at(branch.fork_height), + work, + last_hash, + )), + }); } self.extend_above(branch, batch, clock, work_min) } @@ -247,7 +274,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 { @@ -273,7 +300,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), @@ -290,6 +324,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 { @@ -303,10 +338,13 @@ impl Chain { chain_work: self.work, })); } - return Ok(Extended::Kept); + 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); @@ -314,7 +352,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( @@ -376,6 +414,62 @@ impl Chain { } } + fn branch_tip( + &self, + work_at_fork: crate::pow::Work, + work_branch: crate::pow::Work, + hash: bitcoin_hashes::sha256d::Hash, + ) -> BranchTip { + 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, + 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( + &self, + 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( + (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, + ours_height: self.below_tip.len(), + ours_hash: self.tip.hash(), + })) + } + fn branch_on_fork(&self, fork_height: usize) -> Branch { assert!(fork_height < self.below_tip.len()); Branch { @@ -669,6 +763,18 @@ impl Branch { } } +impl BranchTip { + pub(crate) fn hash(&self) -> bitcoin_hashes::sha256d::Hash { + self.hash + } + + #[cfg(fuzzing)] + #[cfg_attr(false, mutants::skip)] + pub(crate) fn work_max(&self) -> crate::pow::Work { + self.work_max + } +} + impl std::fmt::Display for ExtendError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -811,18 +917,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 +1609,26 @@ mod tests { ) } + 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], + ours: &[crate::block::BlockHeader], + ) -> super::Extended { + super::Extended::KeptOver(tip_of_branch(headers, ours)) + } + fn branch_of(extended: Result) -> super::Branch { match extended { Ok(super::Extended::Branch(branch)) => branch, @@ -1587,7 +1718,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()); } @@ -1608,11 +1739,12 @@ 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 { - (super::Extended::Kept, headers.to_vec()) + let theirs = [&headers[..1], &branch[..]].concat(); + (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}"); @@ -1625,16 +1757,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); @@ -1667,7 +1802,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()); } @@ -1699,7 +1834,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()); } @@ -1708,12 +1843,12 @@ 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) + Ok(kept_over(theirs, &headers)) ); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } @@ -1750,7 +1885,7 @@ mod tests { clock, crate::pow::Work::ZERO ), - Ok(super::Extended::Taken) + Ok(super::Extended::Taken { full: false }) ); assert_eq!( chain.tip(), @@ -1778,7 +1913,15 @@ 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_max: regtest_with(&headers[..2_000]).tip().work(), + ours_height: headers.len(), + ours_hash: regtest_with(&headers).tip().hash(), + })) + ), } assert_eq!(chain.tip(), regtest_with(&headers).tip()); } @@ -1819,7 +1962,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()); @@ -1882,11 +2025,11 @@ 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) => { - assert_eq!(extended, Ok(super::Extended::Kept), "{count}"); + assert_eq!(extended, Ok(kept_over(&branch, &headers)), "{count}"); assert_eq!(chain.tip(), regtest_with(&headers).tip()); } (_, true) => { @@ -1897,4 +2040,72 @@ mod tests { } } } + + #[test] + 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], &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, 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, 2_100)); + assert_eq!(extend(2_100, &branch[1..]), Ok(super::Announced::Ask)); + let unproven = unproven(branch[2]); + for (theirs, error) in [ + (&branch[2..], super::ExtendError::NotContinuous), + ( + &[branch[1], branch[1]][..], + super::ExtendError::NotContinuous, + ), + (&[branch[1], unproven][..], super::ExtendError::ProofOfWork), + ] { + 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 c6c8597..19c5e3b 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, 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, 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, extended, clock) } } } @@ -712,9 +724,14 @@ 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::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,17 +747,16 @@ fn chain_next( chain: &crate::chain::Chain, work_min: crate::pow::Work, salt: &mut Option, - full: bool, + branch_tip: &mut Option, extended: crate::chain::Extended, clock: u64, ) -> Result, PeerError> { match extended { - crate::chain::Extended::Taken => { + crate::chain::Extended::Taken { full } => { 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)))) } @@ -748,12 +764,15 @@ fn chain_next( assert!(chain.tip().work() >= work_min); Ok(None) } + crate::chain::Extended::KeptOver(tip) => { + *branch_tip = Some(tip); + Ok(None) + } crate::chain::Extended::BelowMargin { start, 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); @@ -766,10 +785,30 @@ 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 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()) + { + 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(()) => { let next = Download::on_tip(chain, work_min); @@ -948,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 }"), @@ -1234,6 +1274,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); @@ -1331,6 +1372,17 @@ 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}"))); + 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); @@ -1609,6 +1661,7 @@ pub(crate) mod fuzz { network: crate::chainparams::Network, work_min: u128, salt_left: bool, + branch_tip: Option<(bitcoin_hashes::sha256d::Hash, u128, usize)>, download: DownloadModel, } @@ -1640,6 +1693,10 @@ pub(crate) mod fuzz { fork: usize, hashes: Vec, }, + KeptOver { + hash: bitcoin_hashes::sha256d::Hash, + work_max: u128, + }, } #[derive(Debug)] @@ -1691,6 +1748,7 @@ pub(crate) mod fuzz { network, work_min, salt_left: true, + branch_tip: None, download: DownloadModel::Chain, } } @@ -1839,15 +1897,15 @@ 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 => { 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); } } @@ -1856,6 +1914,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 = @@ -1867,13 +1931,17 @@ 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)) => ExtendedModel::KeptOver { + hash: *last, + work_max: 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)? @@ -1903,6 +1971,33 @@ 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 + { + 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; + 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, self.height())); + return Ok(()); + } + self.get_headers = GetHeadersModel::Due; + Ok(()) + } + fn extend_on_locator( &mut self, headers: &[crate::block::BlockHeader], @@ -1927,7 +2022,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( @@ -1935,6 +2031,7 @@ pub(crate) mod fuzz { base: usize, branch: Vec, batch: Vec, + full: bool, ) -> ExtendedModel { assert!(!batch.is_empty()); assert!(base < self.height()); @@ -1965,6 +2062,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_max: self.below_margin_work_max(hashes.len()), + }; + } return if shared == 0 { ExtendedModel::BelowMargin { fork, hashes } } else { @@ -1975,13 +2081,38 @@ 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_max: branch_work, + }; + } 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 } + 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; @@ -1996,6 +2127,12 @@ pub(crate) mod fuzz { match extended { 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.height())); + Ok(None) + } _ if !full => Ok(None), ExtendedModel::Branch { fork, hashes } => { Ok(Some(DownloadModel::Branch { fork, hashes })) @@ -2078,7 +2215,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 { @@ -3088,19 +3225,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 +3254,79 @@ 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 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 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(); @@ -3123,13 +3336,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); 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 {