aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/swarm/set.rs')
-rw-r--r--src/swarm/set.rs59
1 files changed, 53 insertions, 6 deletions
diff --git a/src/swarm/set.rs b/src/swarm/set.rs
index ec2415e..6df78ba 100644
--- a/src/swarm/set.rs
+++ b/src/swarm/set.rs
@@ -25,10 +25,15 @@ use crate::transport::{self, HeadsSummary, ParsedMessage, SyncBody};
/// Where this node's set stands, as its STATUSes say it; none, where no set carries its
/// store.
pub(crate) fn stamp(state: &AppState) -> Option<HeadsSummary> {
- let stamp = state.holding.with_set(passing::stamp)?;
- Some(HeadsSummary::Kind(
- rmp_serde::to_vec(&stamp).expect("a stamp encodes"),
- ))
+ if !state.holding.of_a_set() {
+ return None;
+ }
+ // A node still gathering what its set begins from stands nowhere yet.
+ let stamp = state
+ .holding
+ .with_set(|set| rmp_serde::to_vec(&passing::stamp(set)).expect("a stamp encodes"))
+ .unwrap_or_default();
+ Some(HeadsSummary::Kind(stamp))
}
/// What a STATUS of `peer` says, where a set carries this node's store: where the
@@ -56,10 +61,52 @@ pub(crate) fn heard_status(
/// What a SYNC of `peer` carries of its set, where a set carries this node's store.
pub(crate) async fn received(state: &AppState, peer: &str, body: Vec<u8>) {
- match rmp_serde::from_slice::<Message>(&body) {
- Ok(message) => handle(state, peer, message).await,
+ let message = match rmp_serde::from_slice::<Message>(&body) {
+ Ok(message) => message,
Err(error) => {
debug!(peer = %peer, error = %error, "What a peer says of its set doesn't read");
+ return;
+ }
+ };
+ // Started with nothing: what was asked for, to begin the set from (`ask_to_start`).
+ if !state.holding.start_wants().is_empty() {
+ if let Message::Units(units) = message {
+ let holding = std::sync::Arc::clone(&state.holding);
+ let begun = tokio::task::spawn_blocking(move || holding.start_take(units))
+ .await
+ .unwrap_or(false);
+ if begun {
+ state.notify_store_changed();
+ state.topology_changed.notify_one();
+ } else {
+ ask(state, peer).await;
+ }
+ }
+ return;
+ }
+ handle(state, peer, message).await;
+}
+
+/// Asks `peer` for what this node's set begins from, where the node started with
+/// nothing and still gathers it.
+async fn ask(state: &AppState, peer: &str) {
+ let wants = state.holding.start_wants();
+ if !wants.is_empty() {
+ send(state, peer, &Message::Wants(wants)).await;
+ }
+}
+
+/// Asks every peer whose handshake this node took for what its set begins from, where
+/// the node started with nothing and still gathers it.
+pub(crate) async fn ask_to_start(state: &AppState) {
+ if state.holding.start_wants().is_empty() {
+ return;
+ }
+ for (peer, peer_state) in state.peers_snapshot().await {
+ let established =
+ peer_state.lock().await.hello_state == crate::state::HelloState::Established;
+ if established {
+ ask(state, &peer).await;
}
}
}