Skip to content

Commit

Permalink
Add compute_unique_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 1, 2023
1 parent 2452d93 commit ffebd52
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
45 changes: 40 additions & 5 deletions src/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ pub trait CellImpl {

/// Returns the sum of all bits and cells of all elements in the cell tree
/// (including this cell).
///
/// NOTE: identical cells are counted each time they occur in the tree.
#[cfg(feature = "stats")]
fn stats(&self) -> CellTreeStats;
}
Expand Down Expand Up @@ -235,6 +237,44 @@ impl DynCell {
CellSlice::new_unchecked(self)
}

/// Recursively computes the count of distinct cells returning
/// the total storage used by this dag taking into account the
/// identification of equal cells.
pub fn compute_unique_stats(&self, limit: usize) -> Option<CellTreeStats> {
let mut visited = ahash::HashSet::<&HashBytes>::default();
let mut stack = vec![self.references()];

let mut stats = CellTreeStats {
bit_count: self.bit_len() as u64,
cell_count: 1,
};

'outer: while let Some(item) = stack.last_mut() {
for cell in item.by_ref() {
if !visited.insert(cell.repr_hash()) {
continue;
}

if stats.cell_count >= limit as u64 {
return None;
}

stats.bit_count += cell.bit_len() as u64;
stats.cell_count += 1;

let next = cell.references();
if next.max > 0 {
stack.push(next);
continue 'outer;
}
}

stack.pop();
}

Some(stats)
}

/// Returns an object that implements [`Debug`] for printing only
/// the root cell of the cell tree.
///
Expand Down Expand Up @@ -1062,18 +1102,14 @@ impl Iterator for LevelMaskIter {
}

/// Cell tree storage stats.
///
/// NOTE: identical cells are counted each time they occur in the tree.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(feature = "stats")]
pub struct CellTreeStats {
/// Total number of bits in tree.
pub bit_count: u64,
/// Total number of cells in tree.
pub cell_count: u64,
}

#[cfg(feature = "stats")]
impl std::ops::Add for CellTreeStats {
type Output = Self;

Expand All @@ -1086,7 +1122,6 @@ impl std::ops::Add for CellTreeStats {
}
}

#[cfg(feature = "stats")]
impl std::ops::AddAssign for CellTreeStats {
#[inline]
fn add_assign(&mut self, rhs: Self) {
Expand Down
24 changes: 21 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub mod error;

#[cfg(test)]
mod tests {
use crate::cell::MAX_BIT_LEN;
use crate::cell::{CellTreeStats, MAX_BIT_LEN};
use crate::prelude::*;
use crate::util::decode_base64;

Expand All @@ -174,7 +174,16 @@ mod tests {
#[test]
fn big_cell_deserialization() {
let data = decode_base64("te6ccgIDAAwAAQAAAACIAAAEBAABAAEAAQABAAEEBAACAAIAAgACAAIEBAADAAMAAwADAAMEBAAEAAQABAAEAAQEBAAFAAUABQAFAAUEBAAGAAYABgAGAAYEBAAHAAcABwAHAAcEBAAIAAgACAAIAAgEBAAJAAkACQAJAAkEBAAKAAoACgAKAAoEBAALAAsACwALAAsABAAA").unwrap();
_ = Boc::decode(data).unwrap();
let cell = Boc::decode(data).unwrap();

let stats = cell.compute_unique_stats(1 << 22).unwrap();
assert_eq!(
stats,
CellTreeStats {
bit_count: 192,
cell_count: 12
}
);
}

#[test]
Expand Down Expand Up @@ -250,6 +259,15 @@ mod tests {

#[test]
fn test_tx() {
Boc::decode_base64("te6ccgICAQoAAQAADGkAAAO3ea37gczcXLp00bkP3eA1txaTwX6TyzGtowSuHiFwobmgAAF3fHG0RBrAoqQhyfVHKxY+b4xigHnXHqftp9X5vfYVKuY58i4/cAABd3p8EkwWJgK1gAA0gEVmAigABQAEAAECEQyBbEYb1mwEQAADAAIAb8mHoSBMFFhAAAAAAAACAAAAAAADMQg15pv/2PjjbqZFi59+K/39f1kPXUGLckkscjpa2sJAUBYMAJ1D7gMTiAAAAAAAAAAANAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAIJyl+oF61WYJFz0URNA5vMfkcc7dxHYfH6w0cmoXG2Ro2za6+U+LR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DmBAAA5wDnAOcA5wQAAOgA6ADoAOgEAADpAOkA6QDpBAAA6gDqAOoA6gQAAOsA6wDrAOsEAADsAOwA7ADsBAAA7QDtAO0A7QQAAO4A7gDuAO4EAADvAO8A7wDvBAAA8ADwAPAA8AQAAPEA8QDxAPEEAADyAPIA8gDyBAAA8wDzAPMA8wQAAPQA9AD0APQEAAD1APUA9QD1BAAA9gD2APYA9gQAAPcA9wD3APcEAAD4APgA+AD4BAAA+QD5APkA+QQAAPoA+gD6APoEAAD7APsA+wD7BAAA/AD8APwA/AQAAP0A/QD9AP0EAAD+AP4A/gD+BAAA/wD/AP8A/wQAAQABAAEAAQAEAAEBAQEBAQEBBAABAgECAQIBAgQAAQMBAwEDAQMEAAEEAQQBBAEEBAABBQEFAQUBBQQAAQYBBgEGAQYEAAEHAQcBBwEHBAABCAEIAQgBCAQAAQkBCQEJAQkAAA==").unwrap();

let stats = cell.compute_unique_stats(1 << 22).unwrap();
assert_eq!(
stats,
CellTreeStats {
bit_count: 4681,
cell_count: 266
}
);
}
}
11 changes: 10 additions & 1 deletion src/merkle/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::cell::EMPTY_CELL_HASH;
use crate::cell::{CellTreeStats, EMPTY_CELL_HASH};
use crate::error::Error;
use crate::prelude::*;

Expand Down Expand Up @@ -39,6 +39,15 @@ fn create_proof_for_deep_cell() {
cell = builder.build().unwrap();
}

let stats = cell.compute_unique_stats(1 << 22).unwrap();
assert_eq!(
stats,
CellTreeStats {
bit_count: 65000 * 32,
cell_count: 65001
}
);

{
let encoded = Boc::encode_base64(cell.as_ref());
let decoded = Boc::decode_base64(encoded).unwrap();
Expand Down

0 comments on commit ffebd52

Please sign in to comment.