Skip to content
Merged
81 changes: 72 additions & 9 deletions dash-spv/src/network/broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct OnWire {
}

/// The request queue plus the registry of every keyed request in play, queued or on
/// the wire. A key stays registered until answered, so re-declaring it is a no-op.
/// the wire. A key stays registered until released, so re-declaring it is a no-op.
pub struct Broker {
queue: MsgQueue,
requests: Mutex<HashMap<RequestKey, ReqState>>,
Expand Down Expand Up @@ -110,9 +110,25 @@ impl Broker {
self.queue.push(height, msg).await;
}

pub async fn answered(&self, key: &RequestKey) {
if matches!(self.requests.lock().await.remove(key), Some(ReqState::OnWire(_))) {
self.wake();
/// Unregisters `key`. If its request was on the wire, wakes the router to send more.
/// If it was still queued, drops it unless another of its keys is still registered,
/// since sent later it would go out untracked or under a new request for the same key.
pub async fn release(&self, key: &RequestKey) {
let mut reqs = self.requests.lock().await;
match reqs.remove(key) {
Some(ReqState::OnWire(_)) => {
drop(reqs);
self.wake();
}
Some(ReqState::Queued(_)) => {
self.queue
.remove(|msg| {
let keys = request_keys(msg);
keys.contains(key) && !keys.iter().any(|k| reqs.contains_key(k))
})
.await;
}
None => {}
}
}

Expand Down Expand Up @@ -494,6 +510,17 @@ impl MsgQueue {
out
}

async fn remove(&self, matches: impl Fn(&NetworkMessage) -> bool) {
let mut removed = 0;
for class in DRAIN_PRIORITY {
let mut q = self.queue(class).lock().await;
let before = q.len();
q.retain(|_, msg| !matches(msg));
removed += before - q.len();
}
self.len.fetch_sub(removed, Ordering::SeqCst);
}

async fn push(&self, height: u32, msg: NetworkMessage) {
let key = (height, self.back_seq.fetch_add(1, Ordering::SeqCst));
self.queue(classify(&msg)).lock().await.insert(key, msg);
Expand Down Expand Up @@ -549,6 +576,14 @@ mod tests {
})
}

/// Requests the diff from block `base` to block 4.
fn get_mnlistdiff(base: u32) -> NetworkMessage {
NetworkMessage::GetMnListD(GetMnListDiff {
base_block_hash: BlockHash::dummy(base),
block_hash: BlockHash::dummy(4),
})
}

fn block(n: u32) -> Inventory {
Inventory::Block(BlockHash::dummy(n))
}
Expand All @@ -573,10 +608,7 @@ mod tests {
#[test_case(get_headers(1) => vec![RequestKey::Headers(BlockHash::dummy(1))]; "getheaders")]
#[test_case(get_cfheaders(2) => vec![RequestKey::CfHeaders(BlockHash::dummy(2))]; "getcfheaders")]
#[test_case(get_cfilters(100) => vec![RequestKey::CFilters(100)]; "getcfilters")]
#[test_case(NetworkMessage::GetMnListD(GetMnListDiff {
base_block_hash: BlockHash::dummy(3),
block_hash: BlockHash::dummy(4),
}) => vec![RequestKey::MnListDiff(BlockHash::dummy(4))]; "getmnlistd")]
#[test_case(get_mnlistdiff(3) => vec![RequestKey::MnListDiff(BlockHash::dummy(4))]; "getmnlistd")]
#[test_case(NetworkMessage::GetData(vec![block(1), tx(7), block(2)])
=> vec![RequestKey::Block(BlockHash::dummy(1)), RequestKey::Block(BlockHash::dummy(2))];
"getdata splits per block and ignores other inventory")]
Expand Down Expand Up @@ -625,12 +657,43 @@ mod tests {
broker.enqueue(0, get_cfilters(0)).await;
assert_eq!(broker.len(), 0);

broker.answered(&RequestKey::CFilters(0)).await;
broker.release(&RequestKey::CFilters(0)).await;
assert_eq!(broker.state_of(&RequestKey::CFilters(0)).await, None);
broker.enqueue(0, get_cfilters(0)).await;
assert_eq!(broker.len(), 1);
}

#[test_case(get_mnlistdiff(3), vec![RequestKey::MnListDiff(BlockHash::dummy(4))], 0; "its only key")]
#[test_case(NetworkMessage::GetData(vec![block(1), block(2)]), vec![RequestKey::Block(BlockHash::dummy(1))], 1; "one of its keys")]
#[test_case(NetworkMessage::GetData(vec![block(1), block(2)]), vec![RequestKey::Block(BlockHash::dummy(1)), RequestKey::Block(BlockHash::dummy(2))], 0; "all of its keys")]
#[tokio::test]
async fn a_released_request_is_dropped_once_none_of_its_keys_is_in_play(
msg: NetworkMessage,
released: Vec<RequestKey>,
queued: usize,
) {
let broker = Broker::new();
broker.enqueue(0, msg).await;
broker.enqueue(0, get_cfilters(0)).await;
for key in &released {
broker.release(key).await;
}
assert_eq!(broker.len(), queued + 1, "the other request stays queued");
assert_eq!(broker.queue.pop_n(broker.len()).await.len(), queued + 1);
}

#[tokio::test]
async fn a_request_released_and_declared_from_another_base_replaces_the_queued_one() {
let broker = Broker::new();
broker.enqueue(0, get_mnlistdiff(3)).await;
broker.release(&RequestKey::MnListDiff(BlockHash::dummy(4))).await;
broker.enqueue(0, get_mnlistdiff(5)).await;

let queued: Vec<NetworkMessage> =
broker.queue.pop_n(broker.len()).await.into_iter().map(|(_, msg)| msg).collect();
assert_eq!(queued, vec![get_mnlistdiff(5)]);
}

#[test_case(vec![get_cfilters(0), get_cfilters(0), get_cfilters(1000)], 2, 2; "a request in play is dropped")]
#[test_case(vec![NetworkMessage::MemPool, NetworkMessage::MemPool], 2, 0; "untracked traffic is never deduplicated")]
#[tokio::test]
Expand Down
6 changes: 3 additions & 3 deletions dash-spv/src/network/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ impl NetworkManager for PeerNetworkManager {
}
}

async fn request_answered(&self, key: RequestKey) {
self.state.broker.answered(&key).await;
async fn release_request(&self, key: RequestKey) {
self.state.broker.release(&key).await;
}

/// Broadcast a message to all connected peers
Expand Down Expand Up @@ -1227,7 +1227,7 @@ fn spawn_pump(
}

let mt = MessageType::from_cmd(msg.cmd());
// Early router wake on inbound traffic (`request_answered` notifies again for the
// Early router wake on inbound traffic (`release_request` notifies again for the
// real capacity). `cfilter` is skipped: only the last of ~1000 frees anything.
if mt != Some(MessageType::CFilter) {
broker.wake();
Expand Down
5 changes: 3 additions & 2 deletions dash-spv/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ pub trait NetworkManager: Send + Sync + 'static {
/// Inject a message into the local pump as if received from a peer.
async fn dispatch_local(&self, msg: NetworkMessage);

/// Stops timeout/retry tracking of `key`.
async fn request_answered(&self, key: RequestKey);
/// Stops tracking `key` once it is answered or no longer wanted. A request not yet
/// sent is dropped.
async fn release_request(&self, key: RequestKey);

async fn subscribe(&self, kinds: &[MessageType]) -> UnboundedReceiver<Inbound>;

Expand Down
45 changes: 45 additions & 0 deletions dash-spv/src/sml_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ pub enum FeedError<E> {
Refused(#[from] E),
}

impl FeedError<SmlError> {
/// Whether the refusal proves the peer sent a bad diff. Only a diff that contradicts itself
/// or its block header does. Any other refusal can come from our side, such as a missing or
/// stale base list, a bug, or network-specific activation heights.
pub(crate) fn blames_peer(&self) -> bool {
match self {
FeedError::Refused(
SmlError::InvalidCoinbaseProof(_) | SmlError::InvalidIndexInSignatureSet(_),
) => true,
// `IncompleteMnListDiff` also fires on honest diffs: a list from genesis to a block
// before any masternode registered is empty, and Core sends it that way.
FeedError::HeaderStorage(_)
| FeedError::Refused(
SmlError::BlockHashLookupFailed(_)
| SmlError::MissingStartMasternodeList(_)
| SmlError::BaseBlockHashMismatch {
..
}
| SmlError::BaseBlockNotGenesis(_)
| SmlError::CorruptedCodeExecution(_)
| SmlError::IncompleteMnListDiff
| SmlError::IncompleteSignatureSet
| SmlError::CoinbaseCommitmentMismatch(_),
) => false,
}
}
}

impl From<StorageError> for FeedError<SmlError> {
fn from(error: StorageError) -> Self {
FeedError::HeaderStorage(error)
Expand Down Expand Up @@ -2965,4 +2993,21 @@ mod tests {
);
}
}

#[test_case(FeedError::HeaderStorage(StorageError::ReadFailed("unreadable".into())) => false; "header storage")]
#[test_case(SmlError::BlockHashLookupFailed(BlockHash::dummy(1)).into() => false; "block hash lookup failed")]
#[test_case(SmlError::MissingStartMasternodeList(BlockHash::dummy(1)).into() => false; "missing start list")]
#[test_case(SmlError::BaseBlockHashMismatch { expected: BlockHash::dummy(1), found: BlockHash::dummy(2) }.into() => false; "base block hash mismatch")]
#[test_case(SmlError::BaseBlockNotGenesis(BlockHash::dummy(1)).into() => false; "base block not genesis")]
#[test_case(SmlError::CorruptedCodeExecution("bug".into()).into() => false; "corrupted code execution")]
#[test_case(SmlError::InvalidCoinbaseProof(BlockHash::dummy(1)).into() => true; "invalid coinbase proof")]
#[test_case(SmlError::InvalidIndexInSignatureSet(3).into() => true; "invalid signature set index")]
#[test_case(SmlError::IncompleteMnListDiff.into() => false; "incomplete diff")]
#[test_case(SmlError::IncompleteSignatureSet.into() => false; "incomplete signature set")]
#[test_case(SmlError::CoinbaseCommitmentMismatch(BlockHash::dummy(1)).into() => false; "coinbase commitment mismatch")]
fn a_refused_diff_is_blamed_only_when_our_state_cannot_explain_it(
error: FeedError<SmlError>,
) -> bool {
error.blames_peer()
}
}
2 changes: 1 addition & 1 deletion dash-spv/src/sync/block_headers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {

if matched.is_some() {
if let Some(locator) = answered_locator {
network.request_answered(RequestKey::Headers(locator)).await;
network.release_request(RequestKey::Headers(locator)).await;
}
} else if !headers.is_empty() {
if let Some(fork_height) = self.fork_height(headers).await? {
Expand Down
2 changes: 1 addition & 1 deletion dash-spv/src/sync/blocks/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface + 'static> SyncM
return Ok(vec![]);
}

network.request_answered(RequestKey::Block(*hashed_block.hash())).await;
network.release_request(RequestKey::Block(*hashed_block.hash())).await;

// Look up height for storage
let height = self
Expand Down
2 changes: 1 addition & 1 deletion dash-spv/src/sync/filter_headers/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<H: BlockHeaderStorage, FH: FilterHeaderStorage> SyncManager for FilterHeade
return Ok(vec![]);
};

network.request_answered(RequestKey::CfHeaders(cfheaders.stop_hash)).await;
network.release_request(RequestKey::CfHeaders(cfheaders.stop_hash)).await;

let mut events = Vec::new();

Expand Down
2 changes: 1 addition & 1 deletion dash-spv/src/sync/filters/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<
// One `getcfilters` is answered only once its whole batch has arrived, so
// individual `cfilter`s are never answered on their own.
if let Some(batch_start) = batch_completed {
network.request_answered(RequestKey::CFilters(batch_start)).await;
network.release_request(RequestKey::CFilters(batch_start)).await;
}

// No `send_pending` here: re-declaring per `cfilter` would re-scan every
Expand Down
Loading
Loading