Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fork. Headers that the peer announces on that header do not make elo ask. elo
adds the work of each one that has its proof of work. It asks when the branch
has more work, or when the announcement is a full batch.
A peer announces a block with an `inv` when its headers do not connect to the
last header it sent, so an `inv` of a block makes elo ask too. From the
last header it sent, so an `inv` of a block makes elo ask too. While a
`getheaders` of elo is out, a `headers` that does not start on one of its hashes
is an announcement. elo skips it, and the answer or the next `getheaders` gets
those headers again. From the
handshake on, it pings the peer every 2 min.
It drops a peer that does not answer a `ping` within 20 min, or a `getheaders`
within 2 min. It also drops a peer that sends `wtxidrelay` or `sendaddrv2` after
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
72 changes: 43 additions & 29 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ enum AboveFork {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExtendError {
NotOnTip,
NotOnLocator,
NotContinuous,
ProofOfWork,
WrongBits,
Expand Down Expand Up @@ -150,24 +149,26 @@ impl Chain {
headers: crate::protocol::Headers,
clock: u64,
work_min: crate::pow::Work,
) -> Result<Extended, ExtendError> {
) -> Result<Option<Extended>, ExtendError> {
let Some(first) = headers.first() else {
return Ok(Extended::Kept);
return Ok(Some(Extended::Kept));
};
if first.prev == self.tip.hash() {
let tip_height = self.below_tip.len();
let first_prev = first.prev;
let Some(fork_height) =
locator_heights(tip_height).find(|height| self.header_at(*height).hash() == first_prev)
else {
return Ok(None);
};
if fork_height == tip_height {
let full = headers.is_full();
self.extend_tip(headers.into_vec(), time_max(clock))?;
return Ok(Extended::Taken { full });
return Ok(Some(Extended::Taken { full }));
}
let tip_height = self.below_tip.len();
let first_prev = first.prev;
let fork_height = locator_heights(tip_height)
.skip(1)
.find(|height| self.header_at(*height).hash() == first_prev)
.ok_or(ExtendError::NotOnLocator)?;
assert!(fork_height < tip_height);
let branch = self.branch_on_fork(fork_height);
self.extend_above(branch, Batch::Answer(headers), clock, work_min)
.map(Some)
}

pub(crate) fn extend_branch(
Expand Down Expand Up @@ -779,9 +780,6 @@ impl std::fmt::Display for ExtendError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExtendError::NotOnTip => write!(f, "headers do not start at our tip"),
ExtendError::NotOnLocator => {
write!(f, "headers do not start at a header of our locator")
}
ExtendError::NotContinuous => write!(f, "headers do not form a chain"),
ExtendError::ProofOfWork => write!(f, "a header fails proof of work"),
ExtendError::WrongBits => write!(f, "a header has the wrong bits"),
Expand Down Expand Up @@ -1589,11 +1587,14 @@ mod tests {
chain: &mut super::Chain,
headers: &[crate::block::BlockHeader],
) -> Result<super::Extended, super::ExtendError> {
chain.extend_on_locator(
crate::fixtures::headers_batch(headers),
crate::fixtures::CLOCK,
crate::pow::Work::ZERO,
)
chain
.extend_on_locator(
crate::fixtures::headers_batch(headers),
crate::fixtures::CLOCK,
crate::pow::Work::ZERO,
)
.transpose()
.unwrap()
}

fn on_branch(
Expand Down Expand Up @@ -1764,16 +1765,26 @@ mod tests {
}

#[test]
fn extend_on_locator_refuses_a_batch_on_a_header_that_is_not_in_the_locator() {
fn extend_on_locator_takes_no_batch_on_a_header_that_is_not_in_the_locator() {
let headers = crate::fixtures::headers_on_genesis(14);
for (below, expected) in [
(&headers[0], Ok(super::Extended::Taken { full: false })),
(&headers[1], Err(super::ExtendError::NotOnLocator)),
(&headers[2], Ok(super::Extended::Taken { full: false })),
(&headers[0], Some(super::Extended::Taken { full: false })),
(&headers[1], None),
(&headers[2], Some(super::Extended::Taken { full: false })),
] {
let branch = crate::fixtures::branch_on(below, 20);
let mut chain = regtest_with(&headers);
assert_eq!(on_locator(&mut chain, &branch), expected);
let tip = chain.tip();
let extended = chain.extend_on_locator(
crate::fixtures::headers_batch(&branch),
crate::fixtures::CLOCK,
crate::pow::Work::ZERO,
);
let skipped = expected.is_none();
assert_eq!(extended, Ok(expected));
if skipped {
assert_eq!(chain.tip(), tip);
}
}
}

Expand Down Expand Up @@ -1934,11 +1945,14 @@ mod tests {
for (count, goes_on) in [(2_000, true), (2_001, false)] {
let work_min = regtest_with(&headers[..count]).tip().work();
let mut chain = regtest_with(&headers);
let extended = chain.extend_on_locator(
crate::fixtures::headers_batch(&branch),
crate::fixtures::CLOCK,
work_min,
);
let extended = chain
.extend_on_locator(
crate::fixtures::headers_batch(&branch),
crate::fixtures::CLOCK,
work_min,
)
.transpose()
.unwrap();
assert_eq!(
matches!(extended, Ok(super::Extended::Branch(_))),
goes_on,
Expand Down
2 changes: 1 addition & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ async fn handshake_stream_exchange(
crate::net_processing::Step::Done(peer, theirs) => {
let connection = Connection {
socket,
peer,
peer: *peer,
address,
};
return Ok((connection, theirs));
Expand Down
Loading