Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[p2p] cbfmgr: improve error on cbfmgr #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions p2p/src/protocol/cbfmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,11 @@ pub trait Events {
#[derive(Error, Debug)]
pub enum GetFiltersError {
/// The specified range is invalid, eg. it is out of bounds.
#[error("the specified range is invalid")]
InvalidRange,
#[error("the specified range is invalid, current tip is {tip}")]
InvalidRange {
/// Filter header tip.
tip: Height,
},
/// Not connected to any compact filter peer.
#[error("not connected to any peer with compact filters support")]
NotConnected,
Expand Down Expand Up @@ -494,13 +497,18 @@ impl<F: Filters, U: SyncFilters + Events + Wakeup + Disconnect, C: Clock> Filter
range: RangeInclusive<Height>,
tree: &T,
) -> Result<(), GetFiltersError> {
let tip = self.filters.height();

if self.peers.is_empty() {
return Err(GetFiltersError::NotConnected);
}
if range.is_empty() {
return Err(GetFiltersError::InvalidRange);
return Err(GetFiltersError::InvalidRange { tip });
}

if *range.end() > tip {
return Err(GetFiltersError::InvalidRange { tip });
}
metaclips marked this conversation as resolved.
Show resolved Hide resolved
assert!(*range.end() <= self.filters.height());

// TODO: Only ask peers synced to a certain height.
// Choose a different peer for each requested range.
Expand All @@ -512,7 +520,7 @@ impl<F: Filters, U: SyncFilters + Events + Wakeup + Disconnect, C: Clock> Filter
{
let stop_hash = tree
.get_block_by_height(*range.end())
.ok_or(GetFiltersError::InvalidRange)?
.ok_or(GetFiltersError::InvalidRange { tip })?
.block_hash();
let timeout = self.config.request_timeout;

Expand Down
50 changes: 50 additions & 0 deletions p2p/src/protocol/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::net;
use std::ops::{Bound, Range};
use std::sync::Arc;

use cbfmgr::GetFiltersError;
use log::*;
use nakamoto_common::bitcoin::network::message_blockdata::GetHeadersMessage;

Expand Down Expand Up @@ -190,6 +191,55 @@ fn test_inv_getheaders() {
.expect("a timer should be returned");
}

#[test]
fn test_get_filter_with_no_header() {
let mut rng = fastrand::Rng::new();
let height = 16;
let network = Network::Regtest;
let remote: PeerId = ([88, 88, 88, 88], 8333).into();

logger::init(log::Level::Debug);

let genesis = network.genesis_block();
let chain = gen::blockchain(genesis, height, &mut rng);
let headers = NonEmpty::from_vec(chain.iter().map(|b| b.header).collect()).unwrap();
let cfheader_genesis = FilterHeader::genesis(network);
let cfheaders = gen::cfheaders_from_blocks(cfheader_genesis, chain.iter())
.into_iter()
.skip(1) // Skip genesis
.collect::<Vec<_>>();

let mut peer = Peer::new(
"alice",
[48, 48, 48, 48],
network,
headers.tail,
cfheaders,
vec![],
rng,
);

// Connect to a peer with cfilter support.
peer.connect(
&PeerDummy {
addr: remote,
height,
protocol_version: PROTOCOL_VERSION,
services: cbfmgr::REQUIRED_SERVICES | syncmgr::REQUIRED_SERVICES,
relay: false,
time: peer.local_time(),
},
Link::Outbound,
);

let (transmit, receiver) = chan::unbounded();
peer.command(Command::GetFilters(17..=100, transmit));

let err = receiver.recv().unwrap().err().unwrap();

assert!(matches!(err, GetFiltersError::InvalidRange { tip: 16 }));
}

#[test]
fn test_bad_magic() {
let rng = fastrand::Rng::new();
Expand Down