diff --git a/core/continuum-kernel/src/resources/governor.rs b/core/continuum-kernel/src/resources/governor.rs index b1e2dafce4..a5f66ea188 100644 --- a/core/continuum-kernel/src/resources/governor.rs +++ b/core/continuum-kernel/src/resources/governor.rs @@ -392,7 +392,7 @@ impl ResourceGovernor { } /// The live lease by id, from the ledger the board is built from (card 26df). - pub fn lease(&self, lease_id: &str) -> Option<&ResourceLease> { + pub fn lease(&self, lease_id: &str) -> Option { self.ledger.lease(lease_id) } diff --git a/core/continuum-kernel/src/resources/lease.rs b/core/continuum-kernel/src/resources/lease.rs index d33182910c..918ba7097a 100644 --- a/core/continuum-kernel/src/resources/lease.rs +++ b/core/continuum-kernel/src/resources/lease.rs @@ -95,9 +95,14 @@ pub struct ResourceLease { pub reclaim_policy: ReclaimPolicy, } +/// The one expiry rule: a lease is past its deadline from `expires_at_ms` on. +pub(crate) fn expired(expires_at_ms: u64, now_ms: u64) -> bool { + now_ms >= expires_at_ms +} + impl ResourceLease { pub fn is_expired(&self, now_ms: u64) -> bool { - now_ms >= self.expires_at_ms + expired(self.expires_at_ms, now_ms) } /// Can pressure take this back right now? Expired leases always yes (the diff --git a/core/continuum-kernel/src/resources/ledger.rs b/core/continuum-kernel/src/resources/ledger.rs index bb73dd719e..613ac1ef40 100644 --- a/core/continuum-kernel/src/resources/ledger.rs +++ b/core/continuum-kernel/src/resources/ledger.rs @@ -209,14 +209,36 @@ impl LeaseView { impl Held { /// This lease's bytes still owed on top of physical use, given the oldest of the accepted observations that /// must show it (its kind's, and its backing pool's on unified memory). - fn pending(&self, accepted: Option) -> u64 { + fn pending(&self, accepted: Option, bytes: u64) -> u64 { match (self.landed_from, accepted) { (Some(from), Some(sample)) if sample >= from => 0, - _ => self.lease.bytes, + _ => bytes, } } } +impl ResourceLeaseLedger { + /// What the core holds for `held` now: its one record of the lease's size. A lease the core holds + /// nothing for holds nothing (the core is the record, so this is its answer, not a substitute). + fn held_bytes(&self, held: &Held) -> u64 { + match self.core.holding(held.ask(), held.meta.kind) { + Some(Bytes(bytes)) => bytes, + None => 0, + } + } + + /// `held` as a caller sees it: its terms at the size the core holds now (zero when the core holds + /// nothing for it, so a lease whose claim is gone still expires and is reclaimed; Fable on #6308). + fn snapshot(&self, held: &Held) -> ResourceLease { + held.meta.with_bytes(self.held_bytes(held)) + } + + /// Every held lease, as callers see them, in lease-id order: the same set `lease_count` counts. + fn snapshots(&self) -> impl Iterator + '_ { + self.leases.values().map(|held| self.snapshot(held)) + } +} + impl ResourceLeaseLedger { pub fn new() -> Self { Self::default() @@ -241,9 +263,8 @@ impl ResourceLeaseLedger { pub fn granted(&self, kind: ResourceKind) -> u64 { self.leases .values() - .map(|held| &held.lease) - .filter(|l| l.kind == kind) - .map(|l| l.bytes) + .filter(|held| held.meta.kind == kind) + .map(|held| self.held_bytes(held)) .fold(0u64, |acc, b| acc.saturating_add(b)) } @@ -350,9 +371,8 @@ impl ResourceLeaseLedger { pub fn granted_to(&self, consumer_id: &str, kind: ResourceKind) -> u64 { self.leases .values() - .map(|held| &held.lease) - .filter(|l| l.kind == kind && l.consumer_id == consumer_id) - .map(|l| l.bytes) + .filter(|held| held.meta.kind == kind && held.meta.consumer_id == consumer_id) + .map(|held| self.held_bytes(held)) .fold(0u64, |acc, b| acc.saturating_add(b)) } @@ -525,7 +545,7 @@ impl ResourceLeaseLedger { .keys() .filter(|(k, _)| *k == kind) .map(|(_, c)| c.as_str()) - .chain(self.leases.values().map(|held| &held.lease).filter(|l| l.kind == kind).map(|l| l.consumer_id.as_str())) + .chain(self.leases.values().map(|held| &held.meta).filter(|l| l.kind == kind).map(|l| l.consumer_id.as_str())) .chain(self.measured.iter().filter(|(_, fs)| fs.iter().any(|f| f.kind == kind)).map(|(c, _)| c.as_str())) .filter(|c| *c != consumer_id) .collect(); @@ -617,7 +637,7 @@ impl ResourceLeaseLedger { .ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_string(), })?; - let lease = &mut held.lease; + let lease = &mut held.meta; if lease.is_expired(now_ms) { return Err(LeaseError::ExpiredLease { lease_id: lease_id.to_string(), @@ -634,7 +654,9 @@ impl ResourceLeaseLedger { pub fn release(&mut self, lease_id: &str) -> Result { let held = self.leases.remove(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_string() })?; let ask = held.ask(); - if let Some(view) = held.view { view.bytes.store(0, Ordering::Release); } + // what it held, read from the core before the release frees it + let released = held.meta.with_bytes(self.held_bytes(&held)); + if let Some(view) = &held.view { view.bytes.store(0, Ordering::Release); } self.core.apply(&Act::Release(ask)); if self.core.status(ask).is_some() { // still in the table: the core refused the release (its claim already gone); it leaves anyway @@ -642,7 +664,7 @@ impl ResourceLeaseLedger { self.core.drop_ask(ask); } self.tell(); - Ok(held.lease) + Ok(released) } /// Reduce a lease's footprint by `freed_bytes` without ending it — the @@ -654,27 +676,26 @@ impl ResourceLeaseLedger { /// lease is fail-loud (a stale id is a real bug). Returns the bytes still /// held by the lease (0 if it was removed). pub fn shrink(&mut self, lease_id: &str, freed_bytes: u64) -> Result { - let held = self - .leases - .get_mut(lease_id) - .ok_or_else(|| LeaseError::MissingLease { - lease_id: lease_id.to_string(), - })?; - let lease = &mut held.lease; + let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_string() })?; + let (kind, ask, had) = (held.meta.kind, held.ask(), self.held_bytes(held)); // never more than the lease held: a holder that over-reports must not tell the core it freed more // than the lease was (Fable on #6287) - let freed_bytes = freed_bytes.min(lease.bytes); - lease.bytes -= freed_bytes; - if let Some(view) = &held.view { view.bytes.store(lease.bytes, Ordering::Release); } - let (remaining, kind, ask) = (lease.bytes, lease.kind, held.ask()); - if remaining == 0 { - self.leases.remove(lease_id); - } + let freed_bytes = freed_bytes.min(had); if freed_bytes > 0 { self.core.apply(&Act::Shrunk { ask, place: kind, by: Bytes(freed_bytes) }); } + // the core moved the bytes; what remains is its record, never a subtraction kept here + let remaining = match self.core.holding(ask, kind) { + Some(Bytes(left)) => left, + None => 0, + }; + if let Some(view) = self.leases.get(lease_id).and_then(|held| held.view.as_ref()) { + view.bytes.store(remaining, Ordering::Release); + } if remaining == 0 { - self.core.drop_ask(ask); // shrunk to nothing: its claim is gone, and so is its entry + // shrunk to nothing: its claim is gone, and so are its lease and its entry + self.leases.remove(lease_id); + self.core.drop_ask(ask); } self.tell(); Ok(remaining) @@ -684,12 +705,7 @@ impl ResourceLeaseLedger { /// uses this list to drive reclaim callbacks; the bytes free only when the /// holder confirms via `release`. Returned in stable id order. pub fn expire(&self, now_ms: u64) -> Vec { - self.leases - .values() - .map(|held| &held.lease) - .filter(|l| l.is_expired(now_ms)) - .cloned() - .collect() + self.snapshots().filter(|l| l.is_expired(now_ms)).collect() } /// Pick leases to reclaim to free at least `target_bytes` of `kind`, @@ -727,10 +743,8 @@ impl ResourceLeaseLedger { return Some(Vec::new()); } let ctx = ArbiterContext { now_ms, pressure }; - let mut candidates: Vec<&ResourceLease> = self - .leases - .values() - .map(|held| &held.lease) + let mut candidates: Vec = self + .snapshots() .filter(|l| l.kind == kind && l.reclaim_rank(now_ms).is_some()) .filter(|l| { // Dwell protection — expired bytes are always fair game; a @@ -750,7 +764,7 @@ impl ResourceLeaseLedger { let mut remaining_per_consumer: BTreeMap<&str, u64> = BTreeMap::new(); let mut freed = 0u64; let mut chosen = Vec::new(); - for lease in candidates { + for lease in &candidates { if freed >= target_bytes { break; } @@ -774,16 +788,19 @@ impl ResourceLeaseLedger { } pub(super) fn lease_view(&mut self, id: &str) -> Option { + let bytes = self.held_bytes(self.leases.get(id)?); let held = self.leases.get_mut(id)?; let coverage = held.view.get_or_insert_with(|| Arc::new(LeaseCoverage { - bytes: AtomicU64::new(held.lease.bytes), - expires_at_ms: AtomicU64::new(held.lease.expires_at_ms), + bytes: AtomicU64::new(bytes), + expires_at_ms: AtomicU64::new(held.meta.expires_at_ms), })); - Some(LeaseView { coverage: Arc::downgrade(coverage), kind: held.lease.kind }) + Some(LeaseView { coverage: Arc::downgrade(coverage), kind: held.meta.kind }) } - pub fn lease(&self, lease_id: &str) -> Option<&ResourceLease> { - self.leases.get(lease_id).map(|held| &held.lease) + /// `lease_id` as it stands: its terms at the size the core holds now. A snapshot, never a reference + /// into a second record. + pub fn lease(&self, lease_id: &str) -> Option { + self.leases.get(lease_id).map(|held| self.snapshot(held)) } /// The published board. Omits a kind only when it is uncapacitied AND @@ -795,7 +812,7 @@ impl ResourceLeaseLedger { let mut kinds = Vec::new(); for kind in ResourceKind::ALL { let capacity_bytes = self.capacity(kind); - let lease_count = self.leases.values().map(|held| &held.lease).filter(|l| l.kind == kind).count() as u32; + let lease_count = self.leases.values().filter(|held| held.meta.kind == kind).count() as u32; let measured_bytes = self.measured(kind); let physical_used_bytes = self.physical_used(kind); if capacity_bytes == 0 @@ -821,7 +838,7 @@ impl ResourceLeaseLedger { lease_count, }); } - let mut leases: Vec = self.leases.values().map(|held| held.lease.clone()).collect(); + let mut leases: Vec = self.snapshots().collect(); leases.sort_by(|a, b| a.lease_id.cmp(&b.lease_id)); // Flatten every consumer's latest footprint into stable-ordered diff --git a/core/continuum-kernel/src/resources/ledger/asks.rs b/core/continuum-kernel/src/resources/ledger/asks.rs index 21e6c5a929..4070217852 100644 --- a/core/continuum-kernel/src/resources/ledger/asks.rs +++ b/core/continuum-kernel/src/resources/ledger/asks.rs @@ -126,8 +126,8 @@ impl ResourceLeaseLedger { pub fn carve(&mut self, reserve: &str, req: &LeaseRequest, lease_id: &str, now_ms: u64) -> Result { let (kind, bytes, from) = match self.leases.get(reserve) { // a lapsed reserve stays charged for its fenced release; nothing is carved from it into a fresh term - Some(held) if held.lease.is_expired(now_ms) => return Err(LeaseError::ExpiredLease { lease_id: reserve.to_owned() }.into()), - Some(held) => (held.lease.kind, held.lease.bytes, held.ask()), + Some(held) if held.meta.is_expired(now_ms) => return Err(LeaseError::ExpiredLease { lease_id: reserve.to_owned() }.into()), + Some(held) => (held.meta.kind, self.held_bytes(held), held.ask()), None => return Err(LeaseError::MissingLease { lease_id: reserve.to_owned() }.into()), }; if kind != req.kind || bytes < req.bytes { @@ -139,14 +139,17 @@ impl ResourceLeaseLedger { let into = self.ask_id(&req.consumer_id); let split = SplitAsk { from, moved: Need { place: kind, amount: Bytes(req.bytes) }, into, generation: self.generation() }; self.core.split(split).map_err(CarveError::Place)?; - let left = bytes - req.bytes; + // the core moved the bytes; what the reserve keeps is its record, never a subtraction kept here + let left = match self.core.holding(from, kind) { + Some(Bytes(left)) => left, + None => 0, + }; if left == 0 { if let Some(held) = self.leases.remove(reserve) { if let Some(view) = held.view { view.bytes.store(0, std::sync::atomic::Ordering::Release); } } - } else if let Some(held) = self.leases.get_mut(reserve) { - held.lease.bytes = left; - if let Some(view) = &held.view { view.bytes.store(left, std::sync::atomic::Ordering::Release); } + } else if let Some(view) = self.leases.get(reserve).and_then(|held| held.view.as_ref()) { + view.bytes.store(left, std::sync::atomic::Ordering::Release); } let lease = ResourceLease { lease_id: lease_id.to_owned(), diff --git a/core/continuum-kernel/src/resources/ledger/held.rs b/core/continuum-kernel/src/resources/ledger/held.rs index f5fcfe0774..b6f4c32980 100644 --- a/core/continuum-kernel/src/resources/ledger/held.rs +++ b/core/continuum-kernel/src/resources/ledger/held.rs @@ -17,7 +17,9 @@ use super::LeaseCoverage; /// include the bytes). A holder that never declares is over-counted (refuses), never under-counted. #[derive(Debug, Clone)] pub(super) struct Held { - pub(super) lease: ResourceLease, + /// Everything of the lease but its size: the size is the core's (`PlacerCore::holding`), read on demand, + /// never kept here as a second record (step 1, ONE-FRAME-ONE-MEMORY.md). + pub(super) meta: LeaseMeta, pub(super) view: Option>, /// When its holder declared it landed: only a reading observed at or after this can show it. None while /// undeclared. @@ -27,10 +29,48 @@ pub(super) struct Held { ask: AskId, } +/// A lease's identity, owner and terms: what the core does not hold. Its bytes are never here. +#[derive(Debug, Clone)] +pub(super) struct LeaseMeta { + pub(super) lease_id: String, + pub(super) consumer_id: String, + pub(super) kind: super::super::lease::ResourceKind, + pub(super) acquired_at_ms: u64, + pub(super) expires_at_ms: u64, + pub(super) reclaim_policy: super::super::lease::ReclaimPolicy, +} + +impl LeaseMeta { + pub(super) fn is_expired(&self, now_ms: u64) -> bool { + super::super::lease::expired(self.expires_at_ms, now_ms) + } + + /// The lease as a caller sees it, at the size the core holds for it now. + pub(super) fn with_bytes(&self, bytes: u64) -> ResourceLease { + ResourceLease { + lease_id: self.lease_id.clone(), + consumer_id: self.consumer_id.clone(), + kind: self.kind, + bytes, + acquired_at_ms: self.acquired_at_ms, + expires_at_ms: self.expires_at_ms, + reclaim_policy: self.reclaim_policy, + } + } +} + impl Held { - /// A lease just granted under core entry `ask`: no view yet, not landed. + /// A lease just granted under core entry `ask`: no view yet, not landed. Its bytes are the core's. pub(super) fn new(lease: &ResourceLease, ask: AskId) -> Self { - Self { lease: lease.clone(), view: None, landed_from: None, ask } + let meta = LeaseMeta { + lease_id: lease.lease_id.clone(), + consumer_id: lease.consumer_id.clone(), + kind: lease.kind, + acquired_at_ms: lease.acquired_at_ms, + expires_at_ms: lease.expires_at_ms, + reclaim_policy: lease.reclaim_policy, + }; + Self { meta, view: None, landed_from: None, ask } } pub(super) fn ask(&self) -> AskId { diff --git a/core/continuum-kernel/src/resources/ledger/shrinks.rs b/core/continuum-kernel/src/resources/ledger/shrinks.rs index 09b655c1a2..b07db07d73 100644 --- a/core/continuum-kernel/src/resources/ledger/shrinks.rs +++ b/core/continuum-kernel/src/resources/ledger/shrinks.rs @@ -31,7 +31,7 @@ impl ResourceLeaseLedger { /// Mark `lease_id` elastic: the law may ask it to give back down to `elastic.floor`. pub fn make_elastic(&mut self, lease_id: &str, elastic: Elastic) -> Result<(), LeaseError> { let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_owned() })?; - let (ask, kind) = (held.ask(), held.lease.kind); + let (ask, kind) = (held.ask(), held.meta.kind); self.core.apply(&Act::Elastic { ask, place: kind, floor: Bytes(elastic.floor), priority: elastic.priority }); self.tell(); Ok(()) @@ -42,7 +42,7 @@ impl ResourceLeaseLedger { /// next settle shares the gap among the elastic leases left (Fable on #6287 at d8fbe6bc5c). pub fn refused(&mut self, lease_id: &str) -> Result<(), LeaseError> { let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_owned() })?; - let (ask, kind, bytes) = (held.ask(), held.lease.kind, held.lease.bytes); + let (ask, kind, bytes) = (held.ask(), held.meta.kind, self.held_bytes(held)); self.core.apply(&Act::Elastic { ask, place: kind, floor: Bytes(bytes), priority: u8::MAX }); self.tell(); Ok(()) @@ -58,7 +58,7 @@ impl ResourceLeaseLedger { for (seq, status) in entries { let Status::Shrink { place, to } = status else { continue }; if let Some((lease_id, held)) = self.leases.by_ask(continuum_sage::place::AskId { owner, seq }) { - out.push(ShrinkDue { ask: held.ask(), lease_id: lease_id.clone(), kind: place, held: held.lease.bytes, to: to.0 }); + out.push(ShrinkDue { ask: held.ask(), lease_id: lease_id.clone(), kind: place, held: self.held_bytes(held), to: to.0 }); } } } diff --git a/core/continuum-kernel/src/resources/ledger/tests/asks.rs b/core/continuum-kernel/src/resources/ledger/tests/asks.rs index 6720d80f18..a313cd61af 100644 --- a/core/continuum-kernel/src/resources/ledger/tests/asks.rs +++ b/core/continuum-kernel/src/resources/ledger/tests/asks.rs @@ -129,3 +129,54 @@ fn churn_leaves_nothing_behind_in_the_core() { assert_eq!(l.available(ResourceKind::Vram), 1_000_000); assert!(l.leases.is_empty() && l.asking.is_empty()); } + +// what this catches (step 1, ONE-FRAME-ONE-MEMORY.md; Codex on the design): a lease's size kept as a second +// record beside the core's claim, drifting after a carve, a shrink, a release or a move still in flight. At +// every step the lease a caller reads equals what the core holds for its entry. +#[test] +fn a_lease_is_what_the_core_holds_for_it_through_carve_shrink_release_and_a_move_in_flight() { + let agrees = |l: &ResourceLeaseLedger, id: &str| { + let core = l.ask_of(id).and_then(|ask| l.core.holding(ask, ResourceKind::Vram)).map(|Bytes(bytes)| bytes); + assert_eq!(l.lease(id).map(|lease| lease.bytes), core, "{id}: the lease is the core's record"); + core + }; + let mut l = ResourceLeaseLedger::new(); + l.set_capacity(ResourceKind::Vram, 10_000); + l.acquire(&req("learning:her", ResourceKind::Vram, 6_000, ReclaimPolicy::Graceful), "reserve".into(), 0).expect("test: the reserve fits"); + assert_eq!(agrees(&l, "reserve"), Some(6_000)); + + l.carve("reserve", &req("learning:her", ResourceKind::Vram, 2_000, ReclaimPolicy::Graceful), "weights", 1).expect("test: carved"); + assert_eq!((agrees(&l, "reserve"), agrees(&l, "weights")), (Some(4_000), Some(2_000)), "a carve moves bytes in the core, and both leases read it"); + + assert_eq!(l.shrink("reserve", 1_000).expect("test: shrunk"), 3_000); + assert_eq!(agrees(&l, "reserve"), Some(3_000), "a shrink is the core's, never a subtraction kept beside it"); + + // a move in flight: the law asks the elastic reserve to give back, and nothing has landed yet + l.make_elastic("reserve", Elastic { floor: 0, priority: 1 }).expect("test: elastic"); + let Ok(Asked::Waiting { ask, .. }) = l.ask(&req("serving", ResourceKind::Vram, 7_500, ReclaimPolicy::Pinned), "turn", 2) else { + panic!("test: the turn waits") + }; + l.prioritize(ask, 200); + let mut due = Vec::new(); + l.shrinks_due(&mut due); + assert!(due.iter().any(|shrink| shrink.lease_id == "reserve" && shrink.held == 3_000), "the reserve is asked from what the core holds: {due:?}"); + assert_eq!(agrees(&l, "reserve"), Some(3_000), "asked, not yet given: the lease still reads what the core holds"); + + l.release("weights").expect("test: released"); + assert_eq!(agrees(&l, "weights"), None, "released: neither the lease nor the core holds it"); + assert_eq!(l.granted(ResourceKind::Vram), 3_000, "granted sums the core's holdings"); +} + +// what this catches (Fable on #6308 at 4330ca90e): a lease whose claim the core no longer holds, skipped by +// expiry, so no reclaim ever fires and its entry is never released. It still expires, at zero bytes. +#[test] +fn a_lease_the_core_holds_nothing_for_still_expires_and_is_reclaimed() { + let mut l = ResourceLeaseLedger::new(); + l.set_capacity(ResourceKind::Vram, 10_000); + l.acquire(&req("learning:her", ResourceKind::Vram, 2_000, ReclaimPolicy::Graceful), "orphan".into(), 0).expect("test: fits"); + let ask = l.ask_of("orphan").expect("test: held"); + l.core.drop_ask(ask); + let deadline = l.lease("orphan").expect("test: still held").expires_at_ms; + let expired: Vec<(String, u64)> = l.expire(deadline + 1).into_iter().map(|lease| (lease.lease_id, lease.bytes)).collect(); + assert_eq!(expired, [("orphan".to_owned(), 0)], "an orphaned lease expires at what the core holds for it"); +} diff --git a/core/continuum-kernel/src/resources/ledger/told.rs b/core/continuum-kernel/src/resources/ledger/told.rs index fc08f13ebb..bc6c21d27d 100644 --- a/core/continuum-kernel/src/resources/ledger/told.rs +++ b/core/continuum-kernel/src/resources/ledger/told.rs @@ -71,8 +71,8 @@ impl ResourceLeaseLedger { let its_landed = self .leases .values() - .filter(|held| held.lease.kind == kind && held.lease.consumer_id == consumer) - .map(|held| held.lease.bytes.saturating_sub(held.pending(self.observed_for(kind)))) + .filter(|held| held.meta.kind == kind && held.meta.consumer_id == consumer) + .map(|held| { let bytes = self.held_bytes(held); bytes.saturating_sub(held.pending(self.observed_for(kind), bytes)) }) .fold(0u64, u64::saturating_add); let axes = std::iter::once(kind).chain(self.backing.get(&kind).copied()); axes.map(|axis| { @@ -106,8 +106,8 @@ impl ResourceLeaseLedger { fn landed_on(&self, kind: ResourceKind) -> u64 { self.leases .values() - .filter(|held| held.lease.kind == kind) - .map(|held| held.lease.bytes.saturating_sub(held.pending(self.observed_for(kind)))) + .filter(|held| held.meta.kind == kind) + .map(|held| { let bytes = self.held_bytes(held); bytes.saturating_sub(held.pending(self.observed_for(kind), bytes)) }) .fold(0u64, u64::saturating_add) } } diff --git a/core/continuum-sage/src/place/core/door.rs b/core/continuum-sage/src/place/core/door.rs index 4484d9b8e2..009bfe78b0 100644 --- a/core/continuum-sage/src/place/core/door.rs +++ b/core/continuum-sage/src/place/core/door.rs @@ -64,6 +64,12 @@ impl PlacerCore { self.ledger.floors(floors); } + /// What `ask` holds on `place` now (its claim's committed or retired holding): the one record a lease's + /// size is read from. + pub fn holding(&self, ask: AskId, place: D::Place) -> Option { + self.ledger.holding(self.claim_of(ask)?, place) + } + fn claim_of(&self, ask: AskId) -> Option { self.owners.get(&ask.owner)?.entries.iter().find(|entry| entry.seq == ask.seq)?.claim } diff --git a/core/continuum-sage/src/place/ledger.rs b/core/continuum-sage/src/place/ledger.rs index 48277670c0..81565d3e3a 100644 --- a/core/continuum-sage/src/place/ledger.rs +++ b/core/continuum-sage/src/place/ledger.rs @@ -168,6 +168,17 @@ impl Ledger { self.claims.contains_key(&id) } + /// What claim `id` holds on `place` now: a committed holding, or a retired one still charged. `None` + /// for a claim not holding there (prepared, gone, or on another place). The one record of a holding's + /// size: a view over the claim never keeps its own copy. + pub fn holding(&self, id: ClaimId, place: P) -> Option { + let held = match self.claims.get(&id)? { + Stage::Committed { held, .. } | Stage::Retired { held } => held, + Stage::Prepared(_) => return None, + }; + held.iter().find(|need| need.place == place).map(|need| need.amount) + } + pub fn accounts(&self, place: P) -> Option> { self.places.iter().find(|(at, _)| *at == place).map(|(_, accounts)| *accounts) }