aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/events.rs15
-rw-r--r--src/federation/fold.rs27
-rw-r--r--src/federation/held.rs112
-rw-r--r--src/federation/party.rs10
-rw-r--r--src/federation/vote.rs14
5 files changed, 159 insertions, 19 deletions
diff --git a/src/api/events.rs b/src/api/events.rs
index 496a077..214882a 100644
--- a/src/api/events.rs
+++ b/src/api/events.rs
@@ -124,7 +124,8 @@ async fn event(State(state): State<AppState>, Path(id): Path<String>) -> Respons
/// `GET /events/counts`: what counts, as the store reads now: the current version, each
/// party's keys and nodes, the values at places, the members, and the proposals on the
-/// current version, each with the events proposing it, the votes on it, the parties
+/// current version, each with the events proposing it a vote may build on (`proposing`:
+/// for a checkpoint, those above its heads), the votes on it, the parties
/// voting `true` and `false`, whether it passed and whether it is dead, and why a party's
/// client backs it not, where it is no version that can be written on the current one
/// (`client::backable`).
@@ -155,13 +156,21 @@ async fn counts(State(state): State<AppState>) -> Response {
.into_iter()
.map(|(hash, ballot)| {
let tally = read.tally(current, &hash);
+ let proposing = read.proposing(&current.hash, &hash);
let none = read
.proposal(&hash)
- .and_then(|proposed| client::backable(current, proposed).err());
+ .and_then(|proposed| client::backable(current, proposed).err())
+ .or_else(|| {
+ (proposing.is_empty() && !ballot.proposals.is_empty()).then(|| {
+ "no event proposing it is above the heads it names: no version \
+ event is written on a vote on it"
+ .to_owned()
+ })
+ });
(
hash.to_string(),
json!({
- "proposals": ids(ballot.proposals),
+ "proposals": ids(proposing),
"votes": ids(ballot.votes),
"yes": tally.yes,
"no": tally.no,
diff --git a/src/federation/fold.rs b/src/federation/fold.rs
index 2e3fc56..7986a13 100644
--- a/src/federation/fold.rs
+++ b/src/federation/fold.rs
@@ -354,6 +354,33 @@ impl State {
self.proposed.get(proposal)
}
+ /// Of the events proposing the proposal of hash `proposal` on `on`, those a vote on it
+ /// may build on: where its version names a checkpoint, those above its heads, so that
+ /// a version event written on the votes is above them too. One proposing it again,
+ /// beside them, a vote doesn't build on.
+ pub(crate) fn proposing(&self, on: &Id, proposal: &Id) -> Vec<Id> {
+ let proposals = self
+ .ballots(on)
+ .remove(proposal)
+ .map(|ballot| ballot.proposals)
+ .unwrap_or_default();
+ let heads = self
+ .proposed
+ .get(proposal)
+ .and_then(|json| Version::read(json).ok())
+ .and_then(|version| version.checkpoint);
+ match heads {
+ None => proposals,
+ Some(heads) => {
+ let above = self.above(&heads.into_iter().collect());
+ proposals
+ .into_iter()
+ .filter(|id| above.contains(id))
+ .collect()
+ }
+ }
+ }
+
/// The version event writing the proposal of hash `proposal` on the current version,
/// where it passed here: on the `true` votes of the first parties by name that pass it,
/// each party's of the lowest ID, so that nodes holding those votes write the same.
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 66f20e9..93e2505 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -728,7 +728,6 @@ impl Holding {
return Ok(Vec::new());
}
let current = store.state().current().clone();
- let ballots = store.state().ballots(&current.hash);
let heads = store.heads();
let line = crate::access::sshsig::PublicKey(voter.key.verifying_key().to_bytes()).line();
let mut record = record.clone();
@@ -740,21 +739,24 @@ impl Holding {
yes,
nays,
} => {
- // On the events proposing it: above the heads it names for a
- // checkpoint wherever they are, so a version event may be written on
- // it, which the node's heads, holding what is beside them, may not be.
- let proposing = ballots
- .get(&proposal)
- .map_or_else(Vec::new, |ballot| ballot.proposals.clone());
+ // On the events proposing it, those above the heads it names for a
+ // checkpoint, so a version event may be written on it, which the
+ // node's heads, holding what is beside them, may not be.
+ let proposing = store.state().proposing(&current.hash, &proposal);
+ if proposing.is_empty() {
+ 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 cast = event::signed(
&voter.key,
party,
&line,
- if proposing.is_empty() {
- &heads
- } else {
- &proposing
- },
+ &proposing,
event::vote(current.hash, proposal, yes),
);
record.cast(&current, &cast, nays);
@@ -1949,6 +1951,92 @@ mod tests {
assert!(holding.vote_by_hand(next.hash(), true).is_ok());
}
+ /// Row 70 (`docs/federation.md`): a checkpoint's version proposed again, by an event
+ /// beside its heads. A node backs it all the same, as a proposal lapses with the move,
+ /// and votes on the event proposing it above the heads alone: a version event written
+ /// on such votes moves it; one on votes built on both events is beside the heads, and
+ /// refused.
+ #[test]
+ fn federation_attack_70() {
+ 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 head = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
+ let mut next = first.after(on);
+ next.checkpoint = Some(vec![Id::of(&head)]);
+ let above = tv.propose(&[Id::of(&head)], &next.json());
+ let beside = lass.propose(&[Id::of(&root)], &next.json());
+ let mut doc = events::started(Federation::NAME, &root, &[]).unwrap();
+ for event in [&head, &above, &beside] {
+ put(&mut doc, event);
+ }
+ let dir = crate::files::scratch_dir("held-attack-70");
+ let record = dir.join("makefu.json");
+ let version = Version::read(&first.json()).unwrap();
+ let mut begun = Record::begin(Id::of(&root), "makefu", &version);
+ begun.began = Id::of(b"before the federation").to_string();
+ begun.save(&record).unwrap();
+ let holding = Holding::open(
+ None,
+ Arc::new(Files::default()),
+ None,
+ Some("makefu".into()),
+ )
+ .unwrap()
+ .voting(Some(Voter {
+ key: makefu.key.clone(),
+ record,
+ // Everything held for a while: the proposal beside the heads too.
+ rules: Rules::default(),
+ }));
+ read(&holding, &doc, 0);
+ assert_eq!(
+ holding.with(|store| store.state().proposing(&on, &next.hash())),
+ Some(vec![Id::of(&above)])
+ );
+ let cast = holding.vote_by_hand(next.hash(), true).unwrap();
+ assert!(matches!(
+ Event::read_taken(&cast),
+ Some(Event::Signed(Signed { deps, .. })) if deps == vec![Id::of(&above)]
+ ));
+ let both = [Id::of(&above), Id::of(&beside)];
+ let beside_votes = [&tv, &lass].map(|party| party.vote(&both, on, next.hash(), true));
+ for vote in &beside_votes {
+ assert!(matches!(
+ holding.offer(vote.clone()),
+ Some(Offered::Taken(_))
+ ));
+ }
+ let refused = harness::version_event(
+ &[Id::of(&beside_votes[0]), Id::of(&beside_votes[1])],
+ next.hash(),
+ );
+ assert!(matches!(
+ holding.offer(refused),
+ Some(Offered::Refused(Refusal::Judged(_)))
+ ));
+ let tv_yes = tv.vote(&[Id::of(&above)], on, next.hash(), true);
+ assert!(matches!(
+ holding.offer(tv_yes.clone()),
+ Some(Offered::Taken(_))
+ ));
+ let written = harness::version_event(&[Id::of(&cast), Id::of(&tv_yes)], next.hash());
+ assert!(matches!(holding.offer(written), Some(Offered::Taken(_))));
+ assert_eq!(
+ holding.with(|store| store.state().current().hash),
+ Some(next.hash())
+ );
+ assert_ne!(
+ holding.with(|store| Id::of(store.checkpoint())),
+ Some(Id::of(&root))
+ );
+ }
+
// The store's attack rows (`docs/store.md`, Attacks) read from a document.
/// tv's federation, its root, and a document started from it.
diff --git a/src/federation/party.rs b/src/federation/party.rs
index 04837c3..162f8bd 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -696,6 +696,9 @@ async fn vote(
};
let proposals = ids(&proposal, "proposals");
let Some(proposing) = proposals.first().copied() else {
+ if let Some(why) = ballots[proposal.to_string()]["none"].as_str() {
+ bail!("{proposal}: {why}");
+ }
bail!("no proposal of hash {proposal} on the current version, as the node reads it");
};
let proposed = match Event::read(&node.event(&proposing).await?) {
@@ -731,8 +734,8 @@ async fn vote(
let kept = record
.may_vote(&current, proposal, &proposed, yes, &nays)
.map_err(|why| anyhow!(why))?;
- // On the events proposing it: above the heads it names for a checkpoint wherever they
- // are, so a version event may be written on it.
+ // On the events proposing it the node shows, those above the heads it names for a
+ // checkpoint, so a version event may be written on it.
let cast = signer.sign(&proposals, event::vote(current.hash, proposal, yes))?;
record.cast(&current, &cast, kept);
record.save(path).map_err(|why| anyhow!(why))?;
@@ -799,6 +802,9 @@ async fn proposals(node: &Node) -> Result<()> {
parties(&proposal["no"])
);
let Some(id) = proposal["proposals"][0].as_str() else {
+ if let Some(why) = proposal["none"].as_str() {
+ println!(" backed by no client: {why}");
+ }
continue;
};
let id = Id::from_hex(id).ok_or_else(|| anyhow!("the node names no event ID: {id}"))?;
diff --git a/src/federation/vote.rs b/src/federation/vote.rs
index d4968be..1b4de16 100644
--- a/src/federation/vote.rs
+++ b/src/federation/vote.rs
@@ -144,7 +144,7 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> {
/// Whether the node backs `version`, proposed `proposed_held` ago, by the voters' rule: one
/// naming a checkpoint only where it changes nothing of `current` but its number, what it
/// changes and its checkpoint, and its heads leave out nothing the node held for a while
-/// before it was proposed.
+/// before it was proposed, proposals and votes aside, which lapse with the move.
pub(crate) fn backs_checkpoint(
seen: &Seen<'_>,
rules: &Rules,
@@ -161,11 +161,21 @@ pub(crate) fn backs_checkpoint(
);
}
let before = proposed_held + rules.held_for;
+ // Proposals and votes lapse with the move: they count no more on the version after.
+ let lapsing = |id: &Id| {
+ matches!(
+ seen.store.get(id).and_then(Event::read_taken),
+ Some(Event::Signed(Signed {
+ does: Does::Proposal { .. } | Does::Vote { .. },
+ ..
+ }))
+ )
+ };
let held: Vec<Id> = seen
.store
.taken()
.iter()
- .filter(|id| (seen.held)(id).is_some_and(|held| held >= before))
+ .filter(|id| (seen.held)(id).is_some_and(|held| held >= before) && !lapsing(id))
.copied()
.collect();
let left_out = seen.store.left_out(heads, &held);