aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs88
1 files changed, 84 insertions, 4 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index ae2264b..6e075aa 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -546,6 +546,34 @@ impl Holding {
Ok(signed)
}
+ /// The version events writing what passed on the current version, as its store reads
+ /// now, offered to it: those it takes, to put into the document. A node writes a version
+ /// as soon as it holds it passed (`docs/federation.md`, Changing the policy).
+ pub(crate) fn passed(&self) -> Vec<Vec<u8>> {
+ let mut inner = self.inner.lock_or_recover();
+ let inner = &mut *inner;
+ let Some(store) = inner.store.as_mut() else {
+ return Vec::new();
+ };
+ let state = store.state();
+ let writing: Vec<Vec<u8>> = state
+ .ballots(&state.current().hash)
+ .keys()
+ .filter_map(|proposal| state.version_event(proposal))
+ .collect();
+ let mut written = Vec::new();
+ for bytes in writing {
+ if let Offered::Taken(_) = store.offer(bytes.clone()) {
+ info!(event = %Id::of(&bytes), "Writing a version that passed");
+ written.push(bytes);
+ }
+ }
+ if !written.is_empty() {
+ self.settle(inner);
+ }
+ written
+ }
+
/// What it signs now, voting by itself by its operator's rules: to put into the
/// document.
pub(crate) fn act(&self) -> Vec<Vec<u8>> {
@@ -629,8 +657,8 @@ fn found(pin: &Id, root: &[u8], events: &BTreeMap<Id, Vec<u8>>) -> Option<Built>
}
/// Reads the store from the document whenever it changes, and from the changes the node
-/// refused now and then; puts again what the document lacks, a random wait after it
-/// first lacked it.
+/// refused now and then; writes what passed and what it signs voting by itself; puts again
+/// what the document lacks, a random wait after it first lacked it.
pub(crate) async fn run_worker(state: crate::state::AppState) {
let mut changed = state.change_tx.subscribe();
let mut due: Option<tokio::time::Instant> = None;
@@ -663,20 +691,27 @@ pub(crate) async fn run_worker(state: crate::state::AppState) {
holding.read(unread);
}
let signed = holding.act();
+ let passed = holding.passed();
(
!holding.lacking(&doc.blocking_read().doc).is_empty(),
signed,
+ passed,
)
});
- let (lacking, signed) = read.await.unwrap_or_else(|error| {
+ let (lacking, signed, passed) = read.await.unwrap_or_else(|error| {
error!(error = %error, "Reading the store from the document failed");
- (false, Vec::new())
+ (false, Vec::new(), Vec::new())
});
for bytes in signed {
if let Err(error) = write(&state, None, bytes).await {
warn!(error = %error, "Couldn't put what it signed for its party: put again later");
}
}
+ for bytes in passed {
+ if let Err(error) = write(&state, None, bytes).await {
+ warn!(error = %error, "Couldn't write a version that passed: put again later");
+ }
+ }
let member = state
.holding
.member(state.local_id(), state.holding.party());
@@ -1166,6 +1201,51 @@ mod tests {
assert!(events::ids(&doc).contains(&Id::of(&beside)));
}
+ /// Two proposals on the first version passing at once, tv backing both: a node writes
+ /// the one it takes, and refuses the other, of its own.
+ #[test]
+ fn a_node_writes_what_passed() {
+ let (tv, lass, makefu) = (
+ Party::new("tv", 1),
+ Party::new("lass", 2),
+ Party::new("makefu", 3),
+ );
+ let first = Spec::first(&[&tv, &lass, &makefu], &[]);
+ let root = first.root();
+ let on = first.hash();
+ let one = first.after(on);
+ let mut two = first.after(on);
+ two.rules.push(("names/x".to_owned(), harness::str("tv")));
+ let p1 = tv.propose(&[Id::of(&root)], &one.json());
+ let p2 = lass.propose(&[Id::of(&root)], &two.json());
+ let mut doc = events::started(Federation::NAME, &root).unwrap();
+ for event in [
+ &p1,
+ &p2,
+ &tv.vote(&[Id::of(&p1)], on, one.hash(), true),
+ &lass.vote(&[Id::of(&p1)], on, one.hash(), true),
+ &tv.vote(&[Id::of(&p2)], on, two.hash(), true),
+ &makefu.vote(&[Id::of(&p2)], on, two.hash(), true),
+ ] {
+ put(&mut doc, event);
+ }
+ let holding = Holding::default();
+ assert_eq!(holding.passed(), Vec::<Vec<u8>>::new());
+ read(&holding, &doc, 0);
+ let written = holding.passed();
+ assert_eq!(written.len(), 1);
+ let current = holding.with(|store| store.state().current().hash).unwrap();
+ assert!(current == one.hash() || current == two.hash());
+ let other = if current == one.hash() { &two } else { &one };
+ let refused = holding.with(|store| store.own().0).unwrap();
+ assert_eq!(refused.len(), 1);
+ assert_eq!(
+ holding.with(|store| store.state().version_event(&other.hash())),
+ Some(None)
+ );
+ assert_eq!(holding.passed(), Vec::<Vec<u8>>::new());
+ }
+
/// Rows the next stages test.
const LATER: &[u32] = &[21, 22];