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

refactor(node): short-circuit transfers verification once the first o… #924

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion sn_node/src/event.rs
Expand Up @@ -13,7 +13,8 @@ use serde::{Deserialize, Serialize};
use sn_protocol::storage::{ChunkAddress, RegisterAddress};
use sn_transfers::{Transfer, UniquePubkey};
use tokio::sync::broadcast;
const NODE_EVENT_CHANNEL_SIZE: usize = 10_000;

const NODE_EVENT_CHANNEL_SIZE: usize = 500;

/// Channel where users of the public API can listen to events broadcasted by the node.
#[derive(Clone)]
Expand Down Expand Up @@ -41,6 +42,11 @@ impl NodeEventsChannel {
trace!("Error occurred when trying to broadcast a node event ({event:?}): {err}");
}
}

/// Returns the number of active receivers
pub fn receiver_count(&self) -> usize {
self.0.receiver_count()
}
}

/// Type of events broadcasted by the node to the public API.
Expand Down
18 changes: 10 additions & 8 deletions sn_node/src/node.rs
Expand Up @@ -371,16 +371,18 @@ impl Node {
}
NetworkEvent::GossipsubMsgReceived { topic, msg }
| NetworkEvent::GossipsubMsgPublished { topic, msg } => {
if topic == TRANSFER_NOTIF_TOPIC {
// this is expected to be a notification of a transfer which we treat specially
match try_decode_transfer_notif(&msg) {
Ok(notif_event) => return self.events_channel.broadcast(notif_event),
Err(err) => warn!("GossipsubMsg matching the transfer notif. topic name, couldn't be decoded as such: {:?}", err),
if self.events_channel.receiver_count() > 0 {
if topic == TRANSFER_NOTIF_TOPIC {
// this is expected to be a notification of a transfer which we treat specially
match try_decode_transfer_notif(&msg) {
Ok(notif_event) => return self.events_channel.broadcast(notif_event),
Err(err) => warn!("GossipsubMsg matching the transfer notif. topic name, couldn't be decoded as such: {:?}", err),
}
}
}

self.events_channel
.broadcast(NodeEvent::GossipsubMsg { topic, msg });
self.events_channel
.broadcast(NodeEvent::GossipsubMsg { topic, msg });
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion sn_node/src/put_validation.rs
Expand Up @@ -429,7 +429,7 @@ impl Node {

for transfer in payment {
match transfer {
Transfer::Encrypted(_) => match self
Transfer::Encrypted(_) if cash_notes.is_empty() => match self
.network
.verify_and_unpack_transfer(transfer, wallet)
.await
Expand All @@ -441,6 +441,7 @@ impl Node {
// transfer ok
Ok(cns) => cash_notes = cns,
},
Transfer::Encrypted(_) => continue, // we already have our cash notes, so we can skip this one
Transfer::NetworkRoyalties(cashnote_redemptions) => {
if let Ok(cash_notes) = self
.network
Expand Down