diff options
| author | tv <tv@krebsco.de> | 2026-11-15 20:18:16 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-15 20:18:16 +0000 |
| commit | 2c64def2981354c341cae2bcbf871456876b8ea3 (patch) | |
| tree | 3fc136a773e94c742f36166a42fe3ad1c961a97a /src/access/rules.rs | |
| parent | 9ebdd9011d086bc74351c527f6626fe403ef3348 (diff) | |
rules: the changes that turned a swarm strict found once for its heads
Every change judged under a policy that isn't strict, in a strict
document, walked back to the changes that turned it strict, each step
reading every change the document holds. Only changes from the time
before reach it in an honest swarm, but a member can send crafted ones
at an old history, each costing every node a walk.
The changes are now read once per walk (`store::made`), and what the
walk finds is kept for the document's heads (`Reads::turned_now`), as
whether its policy is strict is.
Diffstat (limited to 'src/access/rules.rs')
| -rw-r--r-- | src/access/rules.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/access/rules.rs b/src/access/rules.rs index c84adc9..1df1d58 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -98,6 +98,9 @@ pub(crate) struct Reads { /// Whether the document's own policy, at its heads then, is strict /// (`before_strict`): kept across `seen`, read again once the document takes more. strict_now: Option<(Vec<ChangeHash>, bool)>, + /// The changes that turned the document strict, at its heads then (`turned_strict`): + /// kept as `strict_now` is. + turned_now: Option<(Vec<ChangeHash>, Vec<ChangeHash>)>, } impl Reads { @@ -107,6 +110,7 @@ impl Reads { *self = Self { seen: seen.to_vec(), strict_now: self.strict_now.take(), + turned_now: self.turned_now.take(), ..Self::default() }; } @@ -125,6 +129,15 @@ impl Reads { } } } + + /// The changes that turned `doc` strict (`turned_strict`): found once for its heads. + fn turned_now(&mut self, doc: &Document) -> &[ChangeHash] { + let heads = doc.get_heads(); + if self.turned_now.as_ref().is_none_or(|(at, _)| *at != heads) { + self.turned_now = Some((heads, turned_strict(doc))); + } + &self.turned_now.as_ref().expect("just found").1 + } } impl Reads { @@ -179,7 +192,7 @@ pub(crate) fn judge_reading( .clone(); if !policy.strict() && reads.strict_now(doc) - && let Some(why) = before_strict(doc, change) + && let Some(why) = before_strict(doc, change, reads.turned_now(doc)) { return Err(Refusal::Rule(why)); } @@ -378,10 +391,10 @@ pub(crate) fn judge_reading( /// not, is refused like any other, so a change made later to look as old can't take its /// place. A swarm strict from its genesis change (`--bootstrap-policy`) has no such /// time. Asked only where `doc`'s policy is strict: elsewhere, nothing changes. -fn before_strict(doc: &Document, change: &Change<'_>) -> Option<String> { +fn before_strict(doc: &Document, change: &Change<'_>, turned: &[ChangeHash]) -> Option<String> { let held = crate::document::changes::holds(doc, &change.hash); - let beside = turned_strict(doc).into_iter().find(|turned| { - *turned != change.hash + let beside = turned.iter().find(|turned| { + **turned != change.hash && !in_history(doc, turned, change.deps) && !(held && in_history(doc, &change.hash, std::slice::from_ref(turned))) })?; @@ -399,9 +412,10 @@ fn turned_strict(doc: &Document) -> Vec<ChangeHash> { let strict_at = |heads: &[ChangeHash]| { At::new(doc, heads).is_some_and(|at| super::policy::of(&at).strict()) }; + let made = store::made(doc); let mut turned = Vec::new(); let mut looked: HashSet<ChangeHash> = HashSet::new(); - let mut writes = store::writers(doc, doc, &path); + let mut writes = store::writers(&made, doc, &path); while let Some((hash, deps)) = writes.pop() { if !looked.insert(hash) { continue; @@ -410,7 +424,7 @@ fn turned_strict(doc: &Document) -> Vec<ChangeHash> { turned.push(hash); } if let Some(before) = At::new(doc, &deps) { - writes.extend(store::writers(doc, &before, &path)); + writes.extend(store::writers(&made, &before, &path)); } } turned |
