diff options
Diffstat (limited to 'src/transport/paths.rs')
| -rw-r--r-- | src/transport/paths.rs | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/src/transport/paths.rs b/src/transport/paths.rs index f425216..e2d9ec9 100644 --- a/src/transport/paths.rs +++ b/src/transport/paths.rs @@ -58,7 +58,7 @@ use crate::clock::{Clock, UnixMillis}; use crate::locks::MutexExt; use crate::transport::stats::{PathCounts, PathInfo, PathsStats, TransportStats}; use crate::transport::traffic::Traffic; -use crate::transport::{Connection, EncodedMessage, ParsedMessage, Transport}; +use crate::transport::{Connection, EncodedMessage, ParsedMessage, Transport, wire}; /// Whether to drop what comes from or goes to an address (the debug firewall, /// `transport::firewall`; the identity layer knows which node is where). @@ -67,6 +67,12 @@ pub(crate) trait Gate: Send + Sync { fn blocks_outbound(&self, address: &str) -> bool; } +/// The wire version to write at an address (`transport::wire`): the newest the node there +/// and this build both speak, as the identity layer heard from it. +pub(crate) trait Versions: Send + Sync { + fn version_at(&self, address: &str) -> u8; +} + /// Called with the sender's address, the message and its `payload_len`. pub(crate) type PathHandler = dyn Fn(String, ParsedMessage, usize) -> std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send @@ -547,6 +553,8 @@ pub(crate) struct Paths { traffic: Option<Arc<Traffic>>, /// The debug firewall, if any. gate: Option<Weak<dyn Gate>>, + /// What each address speaks; without, every address is written `wire::OLDEST`. + versions: Option<Weak<dyn Versions>>, /// The node's wall clock, which its PONGs say the time by. clock: Arc<Clock>, /// A std mutex: never held across an await. @@ -555,12 +563,14 @@ pub(crate) struct Paths { impl Paths { /// Over `transports`, by scheme, in order of preference; its own PINGs and PONGs - /// counted in `traffic`, the firewall through `gate`, PONGs stamped by `clock`; - /// paths pinged every `keepalive` at least (`Table::probe_every`). + /// counted in `traffic`, the firewall through `gate`, each address written the wire + /// version `versions` says, PONGs stamped by `clock`; paths pinged every `keepalive` + /// at least (`Table::probe_every`). pub(crate) fn new( transports: Vec<(&'static str, Arc<dyn Transport>)>, traffic: Option<Arc<Traffic>>, gate: Option<Weak<dyn Gate>>, + versions: Option<Weak<dyn Versions>>, clock: Arc<Clock>, keepalive: Duration, ) -> Self { @@ -571,6 +581,7 @@ impl Paths { .collect(), traffic, gate, + versions, clock, table: Mutex::new(Table::new(keepalive)), } @@ -580,6 +591,15 @@ impl Paths { self.gate.as_ref()?.upgrade() } + /// The wire version to write at `address` (`Versions`): the oldest this build speaks, + /// until its node says what it speaks. + pub(crate) fn version_at(&self, address: &str) -> u8 { + self.versions + .as_ref() + .and_then(Weak::upgrade) + .map_or(wire::OLDEST, |versions| versions.version_at(address)) + } + /// The transport `address` is on, and the transport identity at it /// (`Transport::locate`, which notes where that is if the address says); None if no /// transport of ours has its scheme, or it isn't one of that transport's. @@ -825,13 +845,16 @@ impl Paths { Err(anyhow!("{}", failures.join("; "))) } - /// `message` encoded for `address`: `peer` and `address` of the result are - /// `address`, and it keeps the message (to encode it for another address). + /// `message` encoded for `address`, in the wire version it speaks (`version_at`): + /// `peer` and `address` of the result are `address`, and it keeps the message (to + /// encode it for another address). pub(crate) fn encode(&self, address: &str, message: &ParsedMessage) -> Result<EncodedMessage> { let (link, target) = self .split(address) .with_context(|| format!("no transport for {address}"))?; - let mut encoded = link.inner.encode(&target, message)?; + let mut encoded = link + .inner + .encode_in(&target, message, self.version_at(address))?; encoded.peer = address.to_owned(); encoded.address = address.to_owned(); encoded.message = Some(Arc::new(message.clone())); @@ -1195,6 +1218,7 @@ mod tests { ], None, None, + None, Arc::default(), Duration::ZERO, )) @@ -1608,6 +1632,7 @@ mod tests { vec![("sim", net.transport("b") as Arc<dyn Transport>)], None, Some(Arc::downgrade(&gate)), + None, Arc::default(), Duration::ZERO, )); @@ -1615,6 +1640,7 @@ mod tests { vec![("sim", net.transport("a") as Arc<dyn Transport>)], None, None, + None, Arc::default(), Duration::ZERO, )); @@ -1673,8 +1699,13 @@ mod tests { async fn resolve_seed(&self, seed: &str) -> Result<String> { Ok(seed.to_owned()) } - fn encode(&self, peer: &str, message: &ParsedMessage) -> Result<EncodedMessage> { - (crate::transport::sim::WIRE.encode)(peer, message) + fn encode_in( + &self, + peer: &str, + message: &ParsedMessage, + version: u8, + ) -> Result<EncodedMessage> { + (crate::transport::sim::WIRE.encode)(peer, message, version) } async fn send_encoded(&self, _message: &EncodedMessage) -> Result<()> { Err(anyhow!("broken")) @@ -1713,6 +1744,7 @@ mod tests { ], None, None, + None, Arc::default(), Duration::ZERO, ); @@ -1720,6 +1752,7 @@ mod tests { vec![("sim", net.transport("b") as Arc<dyn Transport>)], None, None, + None, Arc::default(), Duration::ZERO, )); |
