Skip to content

Commit

Permalink
Add StorageUsed::compute
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Apr 12, 2024
1 parent 6ea03bc commit 8680d2b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/models/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ impl StorageUsed {
bits: VarUint56::ZERO,
public_cells: VarUint56::ZERO,
};

/// Computes a total storage usage stats.
///
/// `cell_limit` is the maximum number of unique cells to visit.
/// If the limit is reached, the function will return [`Error::Cancelled`].
pub fn compute(account: &Account, cell_limit: usize) -> Result<Self, Error> {
let cell = {
let cx = &mut Cell::empty_context();
let mut storage = CellBuilder::new();
storage.store_u64(account.last_trans_lt)?;
account.balance.store_into(&mut storage, cx)?;
account.state.store_into(&mut storage, cx)?;
if account.init_code_hash.is_some() {
account.init_code_hash.store_into(&mut storage, cx)?;
}
storage.build_ext(cx)?
};

let Some(res) = cell.compute_unique_stats(cell_limit) else {
return Err(Error::Cancelled);
};

let res = Self {
cells: VarUint56::new(res.cell_count),
bits: VarUint56::new(res.bit_count),
public_cells: Default::default(),
};

if res.cells.is_valid() || !res.bits.is_valid() {
return Err(Error::IntOverflow);
}

Ok(res)
}
}

/// Amount of unique cells and bits.
Expand Down

0 comments on commit 8680d2b

Please sign in to comment.