ledger.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //! The async ledger resource -- the primary entry point for callers.
  2. //!
  3. //! [`Ledger`] is one type over a single [`Store`]. Its methods are grouped into
  4. //! sibling modules by concern, so the deep commit engine is not buried among
  5. //! shallow query re-exports:
  6. //!
  7. //! - `commit`: the write-ahead saga/commit engine (resolve, commit, reverse,
  8. //! recover, finalize). This is what a ledger fundamentally *is*.
  9. //! - `lifecycle`: account create, freeze, unfreeze, close.
  10. //! - `balance`: per-subaccount balance queries.
  11. //! - `query`: read-only queries and book CRUD (thin `Store` pass-throughs that
  12. //! relabel `StoreError` as [`LedgerError`]).
  13. //!
  14. //! The inflight-hold API is a further cluster, in [`crate::inflight`].
  15. use std::sync::Arc;
  16. use kuatia_storage::store::Store;
  17. use crate::error::LedgerError;
  18. mod balance;
  19. mod commit;
  20. mod lifecycle;
  21. mod query;
  22. pub use balance::SubAccountBalance;
  23. pub use commit::LoadedState;
  24. /// Return the current time as Unix milliseconds.
  25. pub(crate) fn now_millis() -> Result<i64, LedgerError> {
  26. Ok(std::time::SystemTime::now()
  27. .duration_since(std::time::UNIX_EPOCH)
  28. .map_err(|_| LedgerError::Overflow)?
  29. .as_millis() as i64)
  30. }
  31. /// Async ledger resource composing the commit pipeline.
  32. pub struct Ledger {
  33. store: Arc<dyn Store>,
  34. }
  35. impl Ledger {
  36. /// Create a new ledger backed by the given store.
  37. pub fn new(store: impl Store + 'static) -> Self {
  38. Self {
  39. store: Arc::new(store),
  40. }
  41. }
  42. /// Returns a reference to the underlying store.
  43. pub fn store(&self) -> &dyn Store {
  44. self.store.as_ref()
  45. }
  46. }