Skip to content

Commit

Permalink
feat: factor of nodes act as royalty_transfer_notif forwarder
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 7, 2023
1 parent e381bb6 commit df0a573
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use bytes::Bytes;
use libp2p::{autonat::NatStatus, identity::Keypair, Multiaddr};
#[cfg(feature = "open-metrics")]
use prometheus_client::registry::Registry;
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{
rngs::{OsRng, StdRng},
Rng, SeedableRng,
};
use sn_networking::{
MsgResponder, Network, NetworkBuilder, NetworkEvent, SwarmDriver, CLOSE_GROUP_SIZE,
};
Expand Down Expand Up @@ -44,6 +47,9 @@ use tokio::{
/// serialised transfer info encrypted against the referenced public key.
pub const ROYALTY_TRANSFER_NOTIF_TOPIC: &str = "ROYALTY_TRANSFER_NOTIFICATION";

/// Defines the percentage (1/50) of node to act as royalty_transfer_notify forwarder.
const FORWARDER_CHOOSING_FACTOR: usize = 50;

/// Interval to trigger replication on a random close_group peer
const PERIODIC_REPLICATION_INTERVAL: Duration = Duration::from_secs(10);

Expand Down Expand Up @@ -142,6 +148,16 @@ impl NodeBuilder {
// Run the node
node.run(swarm_driver, network_event_receiver);

// Having a portion of nodes (1/50) subscribe to the ROYALTY_TRANSFER_NOTIF_TOPIC
// Such nodes become `forwarder` to ensure the actual beneficary won't miss.
let index: usize = OsRng.gen_range(0..FORWARDER_CHOOSING_FACTOR);
if index == FORWARDER_CHOOSING_FACTOR / 2 {
trace!("Picked as a forwarding node to subscribe to the {ROYALTY_TRANSFER_NOTIF_TOPIC} topic");
running_node
.subscribe_to_topic(ROYALTY_TRANSFER_NOTIF_TOPIC.to_string())
.map(|()| info!("Node has been subscribed to gossipsub topic '{ROYALTY_TRANSFER_NOTIF_TOPIC}' to receive network royalties payments notifications."))?;
}

Ok(running_node)
}
}
Expand Down

0 comments on commit df0a573

Please sign in to comment.