aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/federation/fold.rs53
-rw-r--r--src/federation/held.rs23
-rw-r--r--src/federation/vote.rs99
3 files changed, 133 insertions, 42 deletions
diff --git a/src/federation/fold.rs b/src/federation/fold.rs
index b96cf1f..b590f07 100644
--- a/src/federation/fold.rs
+++ b/src/federation/fold.rs
@@ -559,33 +559,44 @@ impl State {
return Err(Refused::new("its proposal isn't proposed on that version"));
}
if *yes {
- let backed: BTreeSet<Id> = self
- .signed()
- .filter_map(|(_, other, _)| match &other.does {
- Does::Vote {
- on: voted_on,
- proposal: voted,
- yes: true,
- } if other.party == signed.party
- && voted_on == on
- && voted != proposal =>
- {
- Some(*voted)
- }
- _ => None,
- })
- .collect();
- if backed.iter().any(|other| !self.tally(current, other).dead) {
- return Err(Refused::new(
- "its party backs another proposal on that version, not dead",
- ));
- }
+ self.may_back(&signed.party, proposal, current)?;
}
Ok(())
}
}
}
+ /// Whether a `true` of `party` on the proposal of hash `proposal` on `current` is
+ /// taken here, as far as its party's other votes go: none of its `true`s on another
+ /// proposal on that version but on one dead here. A node asks it at the history its
+ /// vote would build on (`Store::state_at`), before it casts one.
+ pub(crate) fn may_back(
+ &self,
+ party: &str,
+ proposal: &Id,
+ current: &Version,
+ ) -> Result<(), Refused> {
+ let backed: BTreeSet<Id> = self
+ .signed()
+ .filter_map(|(_, other, _)| match &other.does {
+ Does::Vote {
+ on,
+ proposal: voted,
+ yes: true,
+ } if other.party == party && *on == current.hash && voted != proposal => {
+ Some(*voted)
+ }
+ _ => None,
+ })
+ .collect();
+ if backed.iter().any(|other| !self.tally(current, other).dead) {
+ return Err(Refused::new(
+ "its party backs another proposal on that version, not dead",
+ ));
+ }
+ Ok(())
+ }
+
/// Whether a version event is taken here, at its history, `current` the version current
/// there. One whose proposal passed but doesn't read as a version, it doesn't refuse:
/// a later continuum may read it (`Fold::stops`).
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 02b94d2..1e1e0d4 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -29,7 +29,7 @@ use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use tracing::{error, info, warn};
-use super::client::{self, Record};
+use super::client::Record;
use super::event::{self, Does, Event, Signed};
use super::fold::Federation;
use super::revision::Feature;
@@ -756,25 +756,8 @@ impl Holding {
// On an event proposing it, of a checkpoint one above the heads it
// names, so a version event may be written on it, which the node's
// heads, holding what is beside them, may not be; and on the nays that
- // freed its party (`client::building`).
- let state = store.state();
- let Some(proposing) =
- state.proposing(&current.hash, &proposal).first().copied()
- else {
- if hand.is_some() {
- return Err(format!(
- "no event proposing {proposal} is above the heads it names: \
- no version event is written on a vote on it"
- ));
- }
- continue;
- };
- let checkpoint = state
- .proposal(&proposal)
- .and_then(|json| Version::read(json).ok())
- .and_then(|version| version.checkpoint)
- .is_some();
- let on = match client::building(proposing, checkpoint, &nays) {
+ // freed its party: where its store takes it (`vote::builds_on`).
+ let on = match vote::builds_on(store, party, &proposal, yes, &nays) {
Ok(on) => on,
Err(why) if hand.is_some() => return Err(why),
Err(_) => continue,
diff --git a/src/federation/vote.rs b/src/federation/vote.rs
index e96cb22..baf8dd0 100644
--- a/src/federation/vote.rs
+++ b/src/federation/vote.rs
@@ -236,6 +236,47 @@ pub(crate) fn backs_checkpoint(
}
}
+/// What a vote of `party` on the proposal of hash `proposal` builds on in `store`: an
+/// event proposing it, of a checkpoint one above the heads it names, so that a version
+/// event may be written on the vote; and, but for a checkpoint, the nays that freed its
+/// party (`client::building`). Why not, where there is none, or where the store refuses a
+/// `true` built so: the party backs another proposal that isn't dead at that history, its
+/// nays cast beside a checkpoint's heads, say. Such a vote counts nowhere, so a node
+/// casts and records none.
+pub(crate) fn builds_on(
+ store: &Store<Federation>,
+ party: &str,
+ proposal: &Id,
+ yes: bool,
+ nays: &[Vec<u8>],
+) -> Result<Vec<Id>, String> {
+ let state = store.state();
+ let current = state.current();
+ let proposing = state
+ .proposing(&current.hash, proposal)
+ .first()
+ .copied()
+ .ok_or_else(|| {
+ format!(
+ "no event proposing {proposal} is above the heads it names: no version \
+ event is written on a vote on it"
+ )
+ })?;
+ let checkpoint = state
+ .proposal(proposal)
+ .and_then(|json| Version::read(json).ok())
+ .and_then(|version| version.checkpoint)
+ .is_some();
+ let on = super::client::building(proposing, checkpoint, nays)?;
+ if yes {
+ store
+ .state_at(&on)
+ .may_back(party, proposal, current)
+ .map_err(|why| format!("no store takes that vote: {}", why.0))?;
+ }
+ Ok(on)
+}
+
/// Whether a proposal can still pass: not dead by the votes cast.
fn alive(current: &Version, proposed: &Proposed) -> bool {
let parties = current.parties.len();
@@ -330,7 +371,9 @@ pub(crate) fn decide(seen: &Seen<'_>, rules: &Rules, record: &Record) -> Vec<Act
{
continue;
}
- if let Ok(kept) = record.may_vote(&current, proposed.hash, &proposed.json, true, &nays) {
+ if let Ok(kept) = record.may_vote(&current, proposed.hash, &proposed.json, true, &nays)
+ && builds_on(seen.store, &record.party, &proposed.hash, true, &kept).is_ok()
+ {
backed = true;
acts.push(Act::Vote {
proposal: proposed.hash,
@@ -437,6 +480,7 @@ pub(crate) fn by_hand(
.flat_map(|proposed| proposed.votes.iter().cloned())
.collect();
let nays = record.may_vote(&current, proposal, &proposed.json, yes, &nays)?;
+ builds_on(seen.store, &record.party, &proposal, yes, &nays)?;
Ok(Act::Vote {
proposal,
yes,
@@ -684,6 +728,59 @@ mod tests {
assert!(backs_checkpoint(&running, &rules, &current, &version, hour).is_ok());
}
+ /// A node casts no `true` its own store refuses. Its party's earlier `true` is in the
+ /// history of a checkpoint's heads, and the nays that freed it came after they were
+ /// named, beside them: a vote on the checkpoint builds on its proposal alone, where
+ /// the earlier proposal isn't dead, so every store refuses it. Cast and recorded, it
+ /// would bind the party by a vote no node holds.
+ #[test]
+ fn a_node_casts_no_true_its_store_refuses() {
+ let (tv, lass, makefu, first, mut store) = three();
+ let root = store.id();
+ let on = first.hash();
+ let mut change = first.after(on);
+ change
+ .rules
+ .push(("names/x".to_owned(), harness::str("tv")));
+ let asked = taken(&mut store, tv.propose(&[root], &change.json()));
+ let earlier = makefu.vote(&[asked], on, change.hash(), true);
+ let mut record = record(&store, "makefu");
+ record.cast(store.state().current(), &earlier, Vec::new());
+ let earlier = taken(&mut store, earlier);
+ let head = taken(
+ &mut store,
+ tv.write(&[earlier], "home/tv/a", harness::str("1")),
+ );
+ let mut checkpoint = first.after(on);
+ checkpoint.checkpoint = Some(vec![head]);
+ let proposal = taken(&mut store, tv.propose(&[head], &checkpoint.json()));
+ taken(
+ &mut store,
+ tv.vote(&[proposal], on, checkpoint.hash(), true),
+ );
+ for party in [&tv, &lass] {
+ taken(&mut store, party.vote(&[asked], on, change.hash(), false));
+ }
+ let rules = Rules {
+ follow: vec!["tv".to_owned()],
+ held_for: Duration::from_secs(60),
+ ..Rules::default()
+ };
+ let long = |_: &Id| Some(Duration::from_secs(3600));
+ let acts = decide(&seen(&store, &long), &rules, &record);
+ assert!(
+ !acts
+ .iter()
+ .any(|act| matches!(act, Act::Vote { yes: true, .. })),
+ "{acts:?}"
+ );
+ // What it would have cast, its store refuses.
+ assert!(matches!(
+ store.offer(makefu.vote(&[proposal], on, checkpoint.hash(), true)),
+ Offered::Refused(_)
+ ));
+ }
+
#[test]
fn a_node_follows_a_party_on_checkpoints() {
let (tv, lass, _, first, mut store) = three();