Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
feat: Expose replication/publication/ttl intervals to NameSystemBuild…
Browse files Browse the repository at this point in the history
…er (#130)
  • Loading branch information
jsantell authored Nov 9, 2022
1 parent ba5560c commit e20680e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
41 changes: 30 additions & 11 deletions rust/noosphere-ns/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ where
dht_config: DHTConfig,
key_material: Option<Ed25519KeyMaterial>,
store: Option<SphereDb<S>>,
propagation_interval: u64,
}

impl<S> NameSystemBuilder<S>
Expand Down Expand Up @@ -88,21 +87,39 @@ where
self
}

/// How long, in seconds, published records are replicated to
/// peers. Should be significantly shorter than `record_ttl`.
/// See [KademliaConfig::set_publication_interval](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_publication_interval).
pub fn publication_interval(mut self, interval: u32) -> Self {
self.dht_config.publication_interval = interval;
self
}

/// How long, in seconds, until a network query times out.
pub fn query_timeout(mut self, timeout: u32) -> Self {
self.dht_config.query_timeout = timeout;
self
}

/// The Noosphere Store to use for reading and writing sphere data.
pub fn store(mut self, store: &SphereDb<S>) -> Self {
self.store = Some(store.to_owned());
/// How long, in seconds, records remain valid for. Should be significantly
/// longer than `publication_interval`.
/// See [KademliaConfig::set_record_ttl](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_record_ttl).
pub fn record_ttl(mut self, interval: u32) -> Self {
self.dht_config.record_ttl = interval;
self
}

/// Default interval for hosted records to be propagated to the network.
pub fn propagation_interval(mut self, propagation_interval: u64) -> Self {
self.propagation_interval = propagation_interval;
/// How long, in seconds, stored records are replicated to
/// peers. Should be significantly shorter than `publication_interval`.
/// See [KademliaConfig::set_replication_interval](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_replication_interval).
pub fn replication_interval(mut self, interval: u32) -> Self {
self.dht_config.replication_interval = interval;
self
}

/// The Noosphere Store to use for reading and writing sphere data.
pub fn store(mut self, store: &SphereDb<S>) -> Self {
self.store = Some(store.to_owned());
self
}

Expand All @@ -121,7 +138,6 @@ where
store,
self.bootstrap_peers.take(),
self.dht_config,
self.propagation_interval,
))
}
}
Expand All @@ -136,7 +152,6 @@ where
dht_config: DHTConfig::default(),
key_material: None,
store: None,
propagation_interval: 60 * 60 * 24, // 1 day
}
}
}
Expand Down Expand Up @@ -171,11 +186,12 @@ mod tests {
.bootstrap_interval(33)
.peer_dialing_interval(11)
.query_timeout(22)
.propagation_interval(3600)
.publication_interval(60 * 60 * 24 + 1)
.replication_interval(60 * 60 + 1)
.record_ttl(60 * 60 * 24 * 3 + 1)
.build()?;

assert_eq!(ns.key_material.0.as_ref(), key_material.0.as_ref());
assert_eq!(ns._propagation_interval, 3600);
assert_eq!(ns.bootstrap_peers.as_ref().unwrap().len(), 2);
assert_eq!(ns.bootstrap_peers.as_ref().unwrap()[0], bootstrap_peers[0],);
assert_eq!(ns.bootstrap_peers.as_ref().unwrap()[1], bootstrap_peers[1]);
Expand All @@ -186,6 +202,9 @@ mod tests {
assert_eq!(ns.dht_config.bootstrap_interval, 33);
assert_eq!(ns.dht_config.peer_dialing_interval, 11);
assert_eq!(ns.dht_config.query_timeout, 22);
assert_eq!(ns.dht_config.publication_interval, 60 * 60 * 24 + 1);
assert_eq!(ns.dht_config.replication_interval, 60 * 60 + 1);
assert_eq!(ns.dht_config.record_ttl, 60 * 60 * 24 * 3 + 1);

if NameSystemBuilder::default().store(&store).build().is_ok() {
panic!("key_material required.");
Expand Down
20 changes: 18 additions & 2 deletions rust/noosphere-ns/src/dht/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,35 @@ pub struct DHTConfig {
/// dial peers found in its kbucket. Outside of tests,
/// should not be lower than 5 seconds.
pub peer_dialing_interval: u64,
/// How long, in seconds, published records are replicated to
/// peers. Should be significantly shorter than `record_ttl`.
/// See [KademliaConfig::set_publication_interval](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_publication_interval).
pub publication_interval: u32,
/// How long, in seconds, until an unsuccessful
/// DHT query times out.
pub query_timeout: u32,
/// How long, in seconds, stored records are replicated to
/// peers. Should be significantly shorter than `publication_interval`.
/// See [KademliaConfig::set_replication_interval](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_replication_interval).
/// Only applies to value records.
pub replication_interval: u32,
/// How long, in seconds, records remain valid for. Should be significantly
/// longer than `publication_interval`.
/// See [KademliaConfig::set_record_ttl](https://docs.rs/libp2p/latest/libp2p/kad/struct.KademliaConfig.html#method.set_record_ttl).
pub record_ttl: u32,
}

impl Default for DHTConfig {
/// Creates a new [DHTConfig] with defaults applied.
fn default() -> Self {
Self {
bootstrap_interval: 5 * 60,
bootstrap_interval: 5 * 60, // 5 mins
listening_address: None,
peer_dialing_interval: 5,
query_timeout: 5 * 60,
publication_interval: 60 * 60 * 24, // 1 day
query_timeout: 5 * 60, // 5 mins
replication_interval: 60 * 60, // 1 hour
record_ttl: 60 * 60 * 24 * 3, // 3 days
}
}
}
15 changes: 15 additions & 0 deletions rust/noosphere-ns/src/dht/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ impl DHTBehaviour {
// where we implement logic to validate/prune incoming records.
cfg.set_record_filtering(KademliaStoreInserts::FilterBoth);

// These configurations only apply to Value records.
cfg.set_record_ttl(Some(Duration::from_secs(config.record_ttl.into())));
cfg.set_publication_interval(Some(Duration::from_secs(
config.publication_interval.into(),
)));
cfg.set_replication_interval(Some(Duration::from_secs(
config.replication_interval.into(),
)));

// These configurations are for Provider records. No replication interval available.
cfg.set_provider_record_ttl(Some(Duration::from_secs(config.record_ttl.into())));
cfg.set_provider_publication_interval(Some(Duration::from_secs(
config.publication_interval.into(),
)));

// TODO(#99): Use SphereFS storage
let store = kad::record::store::MemoryStore::new(local_peer_id.to_owned());
Kademlia::with_config(local_peer_id.to_owned(), store, cfg)
Expand Down
7 changes: 1 addition & 6 deletions rust/noosphere-ns/src/name_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ where
pub(crate) dht_config: DHTConfig,
/// Key of the NameSystem's sphere.
pub(crate) key_material: Ed25519KeyMaterial,
/// In seconds, the interval that hosted records are propagated on the network.
pub(crate) _propagation_interval: u64,
pub(crate) store: SphereDb<S>,
/// Map of sphere DIDs to [NSRecord] hosted/propagated by this name system.
hosted_records: HashMap<String, NSRecord>,
Expand All @@ -54,14 +52,12 @@ where
store: SphereDb<S>,
bootstrap_peers: Option<Vec<Multiaddr>>,
dht_config: DHTConfig,
_propagation_interval: u64,
) -> Self {
NameSystem {
key_material,
store,
bootstrap_peers,
dht_config,
_propagation_interval,
dht: None,
hosted_records: HashMap::new(),
resolved_records: HashMap::new(),
Expand Down Expand Up @@ -95,8 +91,7 @@ where
}

/// Propagates all hosted records on nearby peers in the DHT network.
/// Automatically called every `crate::NameSystemBuilder::propagation_interval` seconds (TBD),
/// but can be manually called to republish records to the network.
/// Automatically propagated by the intervals configured in [crate::NameSystemBuilder].
///
/// Can fail if NameSystem is not connected or if no peers can be found.
pub async fn propagate_records(&self) -> Result<()> {
Expand Down
1 change: 0 additions & 1 deletion rust/noosphere-ns/tests/ns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ async fn generate_name_systems_network(
let ns: NameSystem<MemoryStore> = NameSystemBuilder::default()
.key_material(&ns_key)
.store(&store)
.propagation_interval(3600)
.peer_dialing_interval(1)
.bootstrap_peers(&bootstrap_addresses)
.build()?;
Expand Down

0 comments on commit e20680e

Please sign in to comment.