aboutsummaryrefslogtreecommitdiffstats
path: root/src/simtest/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/simtest/mod.rs')
-rw-r--r--src/simtest/mod.rs78
1 files changed, 77 insertions, 1 deletions
diff --git a/src/simtest/mod.rs b/src/simtest/mod.rs
index 61c81d1..267768f 100644
--- a/src/simtest/mod.rs
+++ b/src/simtest/mod.rs
@@ -11,6 +11,8 @@
mod deltas;
mod identity;
+mod links;
+mod relay;
mod remakes;
mod soak;
mod timing;
@@ -47,7 +49,14 @@ pub(crate) const SOON: Duration = Duration::from_secs(10);
/// A simulated swarm: the net and the nodes started on it.
pub(crate) struct Sim {
+ /// The net of the transport `sim`, every node's but those started on others
+ /// (`start_on`).
pub(crate) net: SimNet,
+ /// Further nets, by the scheme of their transport: nodes on different nets can't
+ /// reach each other, as nodes on different transports (one on Tor alone, one on
+ /// libp2p alone) can't. Made when first used.
+ nets: std::sync::Mutex<std::collections::HashMap<&'static str, SimNet>>,
+ seed: u64,
/// Nodes under the identity layer (`transport::identity`), named by node IDs; else
/// by their names on the net.
identity: bool,
@@ -60,6 +69,8 @@ impl Sim {
init_logging();
Self {
net: SimNet::new(seed),
+ nets: Default::default(),
+ seed,
identity: false,
nodes: Default::default(),
}
@@ -99,8 +110,71 @@ impl Sim {
start: Start,
adjust: impl FnOnce(&mut NodeConfig),
) -> AppState {
+ self.start_node(id, self.net.transport(id), seeds, start, adjust)
+ }
+
+ /// [`start`](Self::start) under the identity layer, on the nets of `schemes`
+ /// instead of `sim`'s alone (the first preferred): it reaches only nodes on one of
+ /// them. Seeds by name are resolved on the first.
+ pub(crate) fn start_on(
+ &self,
+ id: &str,
+ schemes: &[&'static str],
+ seeds: &[&str],
+ start: Start,
+ ) -> AppState {
+ self.start_on_with(id, schemes, seeds, start, |_| {})
+ }
+
+ /// [`start_on`](Self::start_on), with the configuration adjusted first.
+ pub(crate) fn start_on_with(
+ &self,
+ id: &str,
+ schemes: &[&'static str],
+ seeds: &[&str],
+ start: Start,
+ adjust: impl FnOnce(&mut NodeConfig),
+ ) -> AppState {
+ assert!(
+ self.identity,
+ "transports by scheme need the identity layer"
+ );
+ let mut transports = schemes.iter().map(|scheme| {
+ let transport: Arc<dyn crate::transport::Transport> =
+ self.net_for(scheme).transport(id);
+ (*scheme, transport)
+ });
+ let (scheme, first) = transports.next().expect("a transport at least");
+ let more: Vec<_> = transports.collect();
+ let key = Arc::new(sim_key(id));
+ self.start_node(id, first, seeds, start, move |config| {
+ config.identity = Some(IdentityConfig { key, scheme, more });
+ adjust(config);
+ })
+ }
+
+ /// The net of the transport `scheme`.
+ pub(crate) fn net_for(&self, scheme: &'static str) -> SimNet {
+ if scheme == "sim" {
+ return self.net.clone();
+ }
+ let mut nets = self.nets.lock().unwrap();
+ let index = nets.len() as u64 + 1;
+ nets.entry(scheme)
+ .or_insert_with(|| SimNet::new(self.seed.wrapping_add(index)))
+ .clone()
+ }
+
+ fn start_node(
+ &self,
+ id: &str,
+ transport: Arc<dyn crate::transport::Transport>,
+ seeds: &[&str],
+ start: Start,
+ adjust: impl FnOnce(&mut NodeConfig),
+ ) -> AppState {
let mut config = NodeConfig {
- transport: self.net.transport(id),
+ transport,
identity: self.identity.then(|| IdentityConfig {
key: Arc::new(sim_key(id)),
scheme: "sim",
@@ -112,8 +186,10 @@ impl Sim {
timing: Timing::new(KEEPALIVE, SYNC_REPLY_TIMEOUT, DELIVERY_LIMIT),
neighbours_k: 8,
alias: Some(id.to_owned()),
+ dashboard_url: None,
electorate_window: Duration::from_secs(30 * 24 * 3600),
clock_skew_warn: Duration::from_secs(2),
+ stream_heartbeat: Duration::from_secs(30),
superseded_writes: Default::default(),
blob_chunk_bytes: 64 * 1024,
swarm_key: None,