aetherlight: apply_operations judges a holder restored to its start value
The conservation sum covers the union of the holders written at entry and the holders the log reports, so a holder written before the operations and returned to its transaction-start value (absent from changes()) still counts. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
aac972260f
commit
1541cb6c4f
2 changed files with 42 additions and 2 deletions
|
|
@ -2086,3 +2086,30 @@ fn views_equal_their_rebuild_and_the_scans_over_random_commands() {
|
|||
assert!(kept.get(name).copied().unwrap_or(0) > 0, "no {name} ever landed: {kept:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A holder written before the operations begin and returned by them to the
|
||||
/// value it had when the transaction opened is no longer in the log, yet it
|
||||
/// moved: conservation reads it from the entry record, so the transfer that
|
||||
/// restores it is judged as conserving.
|
||||
#[test]
|
||||
fn apply_conserves_a_holder_restored_to_its_start_value() {
|
||||
use crate::patch::ResolvedOp;
|
||||
use crate::{Quantity, apply_operations};
|
||||
|
||||
let (mut kernel, people, _) = clockwork_world(20);
|
||||
let grain = *kernel
|
||||
.state
|
||||
.entities
|
||||
.iter()
|
||||
.find(|(_, record)| record.label == "Grain 1")
|
||||
.expect("declared")
|
||||
.0;
|
||||
let revision = kernel.state.revision + 1;
|
||||
let state = &mut kernel.state;
|
||||
state.begin();
|
||||
crate::set_held(state, people[0], grain, 5);
|
||||
let transfer = ResolvedOp::Transfer { from: people[0], to: people[1], resource: grain, qty: Quantity(5) };
|
||||
apply_operations(state, std::slice::from_ref(&transfer), &[], revision)
|
||||
.expect("moving the pre-written five on conserves it");
|
||||
state.rollback();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1742,14 +1742,27 @@ fn apply_operations(
|
|||
let held_of = |held: Option<&BTreeMap<EntityId, Quantity>>, resource: &EntityId| {
|
||||
held.and_then(|held| held.get(resource)).map_or(0, |quantity| u128::from(quantity.0))
|
||||
};
|
||||
// The holders judged are the union of those written at entry and those the
|
||||
// log now reports: a holder written before the operations and returned to
|
||||
// its transaction-start value is absent from `changes()` but still moved.
|
||||
type Held<'a> = Option<&'a BTreeMap<EntityId, Quantity>>;
|
||||
let mut judged: BTreeMap<SubjectId, (Held<'_>, Held<'_>)> = BTreeMap::new();
|
||||
for (holder, logged, now) in state.holdings.changes() {
|
||||
let before = match at_entry.get(holder) {
|
||||
Some(recorded) => recorded.as_ref(),
|
||||
None => logged,
|
||||
};
|
||||
judged.insert(*holder, (before, now));
|
||||
}
|
||||
for (holder, recorded) in &at_entry {
|
||||
judged
|
||||
.entry(*holder)
|
||||
.or_insert_with(|| (recorded.as_ref(), state.holdings.get(holder)));
|
||||
}
|
||||
for (before, now) in judged.values() {
|
||||
for (resource, delta) in &mut deltas {
|
||||
delta.before += held_of(before, resource);
|
||||
delta.after += held_of(now, resource);
|
||||
delta.before += held_of(*before, resource);
|
||||
delta.after += held_of(*now, resource);
|
||||
}
|
||||
}
|
||||
if patch::check_ledger(&deltas).is_some() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue