Skip to content

Commit 9fbb6ba

Browse files
coinage-layer Phase 7 (part 13): coin/entry_count_in_purse total aggregators
Adds total-count aggregators that don't filter by state: coin_count_in_purse(p) -> usize // all coins regardless of state entry_count_in_purse(p) -> usize // all entries regardless of state Complement the state-filtered count primitives (coin_count_available, entry_count_selectable). Useful for diagnostics ("how cluttered is this purse"), test assertions, and pre-flight checks before purse maintenance ops that don't care about state. 204 verified, 0 errors.
1 parent 8f0004a commit 9fbb6ba

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

  • rust/crates/coinage-layer/src

rust/crates/coinage-layer/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,6 +4689,57 @@ impl State {
46894689
c
46904690
}
46914691

4692+
/// Count of all coins (any state) in purse `p`. Useful diagnostic
4693+
/// for "how cluttered is this purse?". Distinguish from
4694+
/// coin_count_available which excludes locked/spent/pending.
4695+
pub fn coin_count_in_purse(&self, p: PurseId) -> (count: usize)
4696+
requires
4697+
self.invariant(),
4698+
ensures
4699+
count <= self.coins@.len(),
4700+
{
4701+
let mut c: usize = 0;
4702+
let mut j: usize = 0;
4703+
while j < self.coins.len()
4704+
invariant
4705+
0 <= j <= self.coins.len(),
4706+
c <= j,
4707+
self.invariant(),
4708+
decreases self.coins.len() - j,
4709+
{
4710+
if self.coins[j].purse == p {
4711+
c = c + 1;
4712+
}
4713+
j = j + 1;
4714+
}
4715+
c
4716+
}
4717+
4718+
/// Count of all entries (any state) in purse `p`. Entry parallel
4719+
/// of `coin_count_in_purse`.
4720+
pub fn entry_count_in_purse(&self, p: PurseId) -> (count: usize)
4721+
requires
4722+
self.invariant(),
4723+
ensures
4724+
count <= self.entries@.len(),
4725+
{
4726+
let mut c: usize = 0;
4727+
let mut j: usize = 0;
4728+
while j < self.entries.len()
4729+
invariant
4730+
0 <= j <= self.entries.len(),
4731+
c <= j,
4732+
self.invariant(),
4733+
decreases self.entries.len() - j,
4734+
{
4735+
if self.entries[j].purse == p {
4736+
c = c + 1;
4737+
}
4738+
j = j + 1;
4739+
}
4740+
c
4741+
}
4742+
46924743
/// Count of `Available` coins in purse `p`. Used by maintenance
46934744
/// triggers — e.g. "if coin_count_available(p) > threshold, run
46944745
/// rebalance to consolidate into fewer larger coins".

0 commit comments

Comments
 (0)