|
@@ -1,7 +1,7 @@
|
|
|
-//! Storage abstraction separating the pure decision logic from IO.
|
|
|
|
|
|
|
+//! The storage abstraction, which holds the pure decision logic apart from IO.
|
|
|
//!
|
|
//!
|
|
|
//! The [`Store`] trait composes focused sub-traits. Every posting and
|
|
//! The [`Store`] trait composes focused sub-traits. Every posting and
|
|
|
-//! account-version write goes through [`CommitStore`], which applies a whole
|
|
|
|
|
|
|
+//! account-version write goes through [`CommitStore`]. It applies a whole
|
|
|
//! transfer or a whole transition in one transaction (ADR-0023), so no caller
|
|
//! transfer or a whole transition in one transaction (ADR-0023), so no caller
|
|
|
//! can leave half of one applied.
|
|
//! can leave half of one applied.
|
|
|
//! - [`AccountStore`]: account creation and version reads
|
|
//! - [`AccountStore`]: account creation and version reads
|
|
@@ -28,22 +28,22 @@ use crate::events::{EventStore, LedgerEvent};
|
|
|
pub struct TransferRecord {
|
|
pub struct TransferRecord {
|
|
|
/// The envelope that was committed.
|
|
/// The envelope that was committed.
|
|
|
pub envelope: Envelope,
|
|
pub envelope: Envelope,
|
|
|
- /// The receipt proving commitment.
|
|
|
|
|
|
|
+ /// The receipt that proves the commit.
|
|
|
pub receipt: Receipt,
|
|
pub receipt: Receipt,
|
|
|
/// Unix milliseconds when this record was created.
|
|
/// Unix milliseconds when this record was created.
|
|
|
pub created_at: i64,
|
|
pub created_at: i64,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Pagination and filtering parameters for posting queries.
|
|
|
|
|
|
|
+/// The page window and the filters a posting query takes.
|
|
|
#[derive(Debug, Clone)]
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct PostingQuery {
|
|
pub struct PostingQuery {
|
|
|
- /// Filter to postings owned by this base account.
|
|
|
|
|
|
|
+ /// Keep only the postings this base account owns.
|
|
|
pub account: i64,
|
|
pub account: i64,
|
|
|
/// Restrict to one subaccount; `None` spans every subaccount of `account`.
|
|
/// Restrict to one subaccount; `None` spans every subaccount of `account`.
|
|
|
pub sub: Option<i64>,
|
|
pub sub: Option<i64>,
|
|
|
- /// Filter by asset.
|
|
|
|
|
|
|
+ /// Keep only one asset.
|
|
|
pub asset: Option<AssetId>,
|
|
pub asset: Option<AssetId>,
|
|
|
- /// Filter by derived lifecycle state.
|
|
|
|
|
|
|
+ /// Keep only one derived lifecycle state.
|
|
|
pub filter: PostingFilter,
|
|
pub filter: PostingFilter,
|
|
|
/// Max results to return.
|
|
/// Max results to return.
|
|
|
pub limit: Option<u32>,
|
|
pub limit: Option<u32>,
|
|
@@ -51,10 +51,10 @@ pub struct PostingQuery {
|
|
|
pub offset: Option<u32>,
|
|
pub offset: Option<u32>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Pagination and filtering parameters for transfer queries.
|
|
|
|
|
|
|
+/// The page window and the filters a transfer query takes.
|
|
|
#[derive(Debug, Clone, Default)]
|
|
#[derive(Debug, Clone, Default)]
|
|
|
pub struct TransferQuery {
|
|
pub struct TransferQuery {
|
|
|
- /// Filter to transfers involving this base account.
|
|
|
|
|
|
|
+ /// Keep only the transfers this base account takes part in.
|
|
|
pub account: Option<i64>,
|
|
pub account: Option<i64>,
|
|
|
/// Restrict to one subaccount; `None` spans every subaccount of `account`.
|
|
/// Restrict to one subaccount; `None` spans every subaccount of `account`.
|
|
|
pub sub: Option<i64>,
|
|
pub sub: Option<i64>,
|
|
@@ -62,13 +62,13 @@ pub struct TransferQuery {
|
|
|
pub from_ts: Option<i64>,
|
|
pub from_ts: Option<i64>,
|
|
|
/// Exclusive upper bound on commit time (unix millis).
|
|
/// Exclusive upper bound on commit time (unix millis).
|
|
|
pub to_ts: Option<i64>,
|
|
pub to_ts: Option<i64>,
|
|
|
- /// Inclusive lower bound on the value date. Separate from `from_ts`
|
|
|
|
|
- /// because an entry can be booked in one period and valued in another, and
|
|
|
|
|
- /// a report asks for one or the other, never both at once.
|
|
|
|
|
|
|
+ /// Inclusive lower bound on the value date. Separate from `from_ts`,
|
|
|
|
|
+ /// because an entry can be booked in one period and valued in another. A
|
|
|
|
|
+ /// report asks for one or the other, and never both at once.
|
|
|
pub from_value_date: Option<ValueDate>,
|
|
pub from_value_date: Option<ValueDate>,
|
|
|
/// Exclusive upper bound on the value date.
|
|
/// Exclusive upper bound on the value date.
|
|
|
pub to_value_date: Option<ValueDate>,
|
|
pub to_value_date: Option<ValueDate>,
|
|
|
- /// Filter by book.
|
|
|
|
|
|
|
+ /// Keep only one book.
|
|
|
pub book: Option<BookId>,
|
|
pub book: Option<BookId>,
|
|
|
/// Max results to return.
|
|
/// Max results to return.
|
|
|
pub limit: Option<u32>,
|
|
pub limit: Option<u32>,
|
|
@@ -78,16 +78,16 @@ pub struct TransferQuery {
|
|
|
|
|
|
|
|
/// A page of results with total count for pagination.
|
|
/// A page of results with total count for pagination.
|
|
|
///
|
|
///
|
|
|
-/// The pagination contract every backend implements: `items` is the page cut by
|
|
|
|
|
-/// the query's `(offset, limit)` window, and `total` is the count of all matching
|
|
|
|
|
-/// rows *ignoring* that window. In-memory backends satisfy it via
|
|
|
|
|
-/// [`paginate`](crate::query::paginate); a SQL backend via `LIMIT`/`OFFSET` plus a
|
|
|
|
|
-/// separate `COUNT(*)`.
|
|
|
|
|
|
|
+/// The pagination contract every backend implements. `items` is the page cut by
|
|
|
|
|
+/// the `(offset, limit)` window of the query. `total` is the count of every row
|
|
|
|
|
+/// that matches, and it takes no account of that window. An in-memory backend
|
|
|
|
|
+/// satisfies it through [`paginate`](crate::query::paginate), and a SQL backend
|
|
|
|
|
+/// through `LIMIT`/`OFFSET` plus a separate `COUNT(*)`.
|
|
|
#[derive(Debug, Clone)]
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct Page<T> {
|
|
pub struct Page<T> {
|
|
|
/// The items in this page (the `(offset, limit)` window).
|
|
/// The items in this page (the `(offset, limit)` window).
|
|
|
pub items: Vec<T>,
|
|
pub items: Vec<T>,
|
|
|
- /// Count of all matching rows, before the page window is applied.
|
|
|
|
|
|
|
+ /// Count of every row that matches, before the page window applies.
|
|
|
pub total: u64,
|
|
pub total: u64,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -103,10 +103,10 @@ pub trait AccountStore: Send + Sync {
|
|
|
async fn get_account(&self, id: &AccountId) -> Result<Account, StoreError>;
|
|
async fn get_account(&self, id: &AccountId) -> Result<Account, StoreError>;
|
|
|
/// Fetch multiple accounts by id.
|
|
/// Fetch multiple accounts by id.
|
|
|
async fn get_accounts(&self, ids: &[AccountId]) -> Result<Vec<Account>, StoreError>;
|
|
async fn get_accounts(&self, ids: &[AccountId]) -> Result<Vec<Account>, StoreError>;
|
|
|
- /// Persist a new account (version 1). A dumb instruction: insert the account
|
|
|
|
|
- /// if absent and return the **number of accounts created** — **1** if this id
|
|
|
|
|
- /// was newly created, **0** if an account with this id already existed. The
|
|
|
|
|
- /// caller decides what `0` means.
|
|
|
|
|
|
|
+ /// Persist a new account (version 1). A dumb instruction: insert the
|
|
|
|
|
+ /// account if absent, and return the **number of accounts created**. That
|
|
|
|
|
+ /// is **1** if this id was newly created, and **0** if an account with this
|
|
|
|
|
+ /// id was already there. The caller decides what `0` means.
|
|
|
async fn create_account(&self, account: Account) -> Result<u64, StoreError>;
|
|
async fn create_account(&self, account: Account) -> Result<u64, StoreError>;
|
|
|
/// Return the full version history for an account.
|
|
/// Return the full version history for an account.
|
|
|
async fn get_account_history(&self, id: &AccountId) -> Result<Vec<Account>, StoreError>;
|
|
async fn get_account_history(&self, id: &AccountId) -> Result<Vec<Account>, StoreError>;
|
|
@@ -116,20 +116,20 @@ pub trait AccountStore: Send + Sync {
|
|
|
|
|
|
|
|
/// Posting reads over an immutable posting table plus an index of the live set.
|
|
/// Posting reads over an immutable posting table plus an index of the live set.
|
|
|
///
|
|
///
|
|
|
-/// A posting is written once and never updated, so [`PostingState`] is derived
|
|
|
|
|
-/// from index membership rather than stored: in the index it is spendable, and
|
|
|
|
|
-/// out of it (but still in the immutable table) it is spent. Only
|
|
|
|
|
|
|
+/// A posting is written once and never updated, so [`PostingState`] comes from
|
|
|
|
|
+/// index membership and is not stored. In the index it is spendable. Out of the
|
|
|
|
|
+/// index, but still in the immutable table, it is spent. Only
|
|
|
/// [`CommitStore::commit_envelope`] moves a posting between the two.
|
|
/// [`CommitStore::commit_envelope`] moves a posting between the two.
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
pub trait PostingStore: Send + Sync {
|
|
pub trait PostingStore: Send + Sync {
|
|
|
- /// Fetch postings by their ids from the immutable table. Consumed (spent)
|
|
|
|
|
- /// postings still resolve here — immutable rows persist forever.
|
|
|
|
|
|
|
+ /// Fetch postings by their ids from the immutable table. A consumed (spent)
|
|
|
|
|
+ /// posting still resolves here, because an immutable row lives forever.
|
|
|
async fn get_postings(&self, ids: &[PostingId]) -> Result<Vec<Posting>, StoreError>;
|
|
async fn get_postings(&self, ids: &[PostingId]) -> Result<Vec<Posting>, StoreError>;
|
|
|
- /// Return postings owned by a base account, filtered by subaccount, asset,
|
|
|
|
|
- /// and derived lifecycle state (see [`PostingFilter`]). `sub == None` spans
|
|
|
|
|
- /// every subaccount of `id`; `sub == Some(s)` restricts to that one. Results
|
|
|
|
|
- /// are ordered by posting id, so callers and pagination built on top see a
|
|
|
|
|
- /// stable sequence.
|
|
|
|
|
|
|
+ /// Return postings owned by a base account, cut by subaccount, asset and
|
|
|
|
|
+ /// derived lifecycle state (see [`PostingFilter`]). `sub == None` spans
|
|
|
|
|
+ /// every subaccount of `id`; `sub == Some(s)` restricts to that one.
|
|
|
|
|
+ /// Results are ordered by posting id, so a caller and any pagination on top
|
|
|
|
|
+ /// see a stable sequence.
|
|
|
async fn get_postings_by_account(
|
|
async fn get_postings_by_account(
|
|
|
&self,
|
|
&self,
|
|
|
id: i64,
|
|
id: i64,
|
|
@@ -138,23 +138,23 @@ pub trait PostingStore: Send + Sync {
|
|
|
filter: PostingFilter,
|
|
filter: PostingFilter,
|
|
|
) -> Result<Vec<Posting>, StoreError>;
|
|
) -> Result<Vec<Posting>, StoreError>;
|
|
|
/// Return the derived [`PostingState`] of each id, aligned to the input
|
|
/// Return the derived [`PostingState`] of each id, aligned to the input
|
|
|
- /// order. An id absent from the immutable table is `Missing`.
|
|
|
|
|
|
|
+ /// order. An id that the immutable table does not hold is `Missing`.
|
|
|
async fn get_posting_states(&self, ids: &[PostingId]) -> Result<Vec<PostingState>, StoreError>;
|
|
async fn get_posting_states(&self, ids: &[PostingId]) -> Result<Vec<PostingState>, StoreError>;
|
|
|
|
|
|
|
|
- /// Sum the live postings of one `(account, asset)` pair: the account's
|
|
|
|
|
- /// balance in that asset.
|
|
|
|
|
|
|
+ /// Sum the live postings of one `(account, asset)` pair, which is the
|
|
|
|
|
+ /// balance of that account in that asset.
|
|
|
///
|
|
///
|
|
|
/// The one place a backend is allowed to add up money itself, and only
|
|
/// The one place a backend is allowed to add up money itself, and only
|
|
|
- /// because it can do so exactly (ADR-0024): the SQL store folds native
|
|
|
|
|
- /// amount columns inside the database rather than shipping one row per
|
|
|
|
|
- /// posting to the caller. An overflow is an error, never a wrap.
|
|
|
|
|
|
|
+ /// because it can do so exactly (ADR-0024). The SQL store folds native
|
|
|
|
|
+ /// amount columns inside the database, and sends no row per posting to the
|
|
|
|
|
+ /// caller. An overflow is an error, never a wrap.
|
|
|
async fn live_balance(
|
|
async fn live_balance(
|
|
|
&self,
|
|
&self,
|
|
|
account: &AccountId,
|
|
account: &AccountId,
|
|
|
asset: &AssetId,
|
|
asset: &AssetId,
|
|
|
) -> Result<Amount, StoreError>;
|
|
) -> Result<Amount, StoreError>;
|
|
|
|
|
|
|
|
- /// Query postings with filtering and pagination.
|
|
|
|
|
|
|
+ /// Query postings, with the filters and the page window applied.
|
|
|
async fn query_postings(&self, query: &PostingQuery) -> Result<Page<Posting>, StoreError> {
|
|
async fn query_postings(&self, query: &PostingQuery) -> Result<Page<Posting>, StoreError> {
|
|
|
// `get_postings_by_account` returns a deterministic id-ordered sequence,
|
|
// `get_postings_by_account` returns a deterministic id-ordered sequence,
|
|
|
// so paginating here is stable without an extra sort. A backend that can
|
|
// so paginating here is stable without an extra sort. A backend that can
|
|
@@ -171,23 +171,24 @@ pub trait PostingStore: Send + Sync {
|
|
|
pub trait TransferStore: Send + Sync {
|
|
pub trait TransferStore: Send + Sync {
|
|
|
/// Fetch a transfer record by its content-addressed id.
|
|
/// Fetch a transfer record by its content-addressed id.
|
|
|
async fn get_transfer(&self, id: &TransferId) -> Result<Option<TransferRecord>, StoreError>;
|
|
async fn get_transfer(&self, id: &TransferId) -> Result<Option<TransferRecord>, StoreError>;
|
|
|
- /// Return all transfers involving the given base account. `sub == None`
|
|
|
|
|
- /// spans every subaccount of `id`; `sub == Some(s)` restricts to one.
|
|
|
|
|
|
|
+ /// Return every transfer the given base account takes part in.
|
|
|
|
|
+ /// `sub == None` spans every subaccount of `id`; `sub == Some(s)` restricts
|
|
|
|
|
+ /// to one.
|
|
|
async fn get_transfers_for_account(
|
|
async fn get_transfers_for_account(
|
|
|
&self,
|
|
&self,
|
|
|
id: i64,
|
|
id: i64,
|
|
|
sub: Option<i64>,
|
|
sub: Option<i64>,
|
|
|
) -> Result<Vec<TransferRecord>, StoreError>;
|
|
) -> Result<Vec<TransferRecord>, StoreError>;
|
|
|
|
|
|
|
|
- /// Query transfers with filtering and pagination.
|
|
|
|
|
|
|
+ /// Query transfers, with the filters and the page window applied.
|
|
|
///
|
|
///
|
|
|
/// A backend loads the candidate records, then hands them to
|
|
/// A backend loads the candidate records, then hands them to
|
|
|
/// [`filter_transfers`](crate::query::filter_transfers) and
|
|
/// [`filter_transfers`](crate::query::filter_transfers) and
|
|
|
/// [`paginate`](crate::query::paginate) for the shared time-window/book and
|
|
/// [`paginate`](crate::query::paginate) for the shared time-window/book and
|
|
|
- /// page cut. There is no default because loading candidates differs by
|
|
|
|
|
- /// backend and, crucially, an `account == None` query must be answered the
|
|
|
|
|
- /// same way everywhere: a store-wide scan, not an error. Every backend does
|
|
|
|
|
- /// exactly that.
|
|
|
|
|
|
|
+ /// page cut. There is no default, because the load of the candidates
|
|
|
|
|
+ /// differs by backend. More to the point, an `account == None` query must
|
|
|
|
|
+ /// get the same answer everywhere: a store-wide scan, and not an error.
|
|
|
|
|
+ /// Every backend does exactly that.
|
|
|
async fn query_transfers(
|
|
async fn query_transfers(
|
|
|
&self,
|
|
&self,
|
|
|
query: &TransferQuery,
|
|
query: &TransferQuery,
|
|
@@ -197,17 +198,17 @@ pub trait TransferStore: Send + Sync {
|
|
|
/// The asset registry: what each [`AssetId`] in a posting means.
|
|
/// The asset registry: what each [`AssetId`] in a posting means.
|
|
|
///
|
|
///
|
|
|
/// A row is written once and never updated, so a reader can cache one for the
|
|
/// A row is written once and never updated, so a reader can cache one for the
|
|
|
-/// life of the process without going stale.
|
|
|
|
|
|
|
+/// life of the process and it never goes stale.
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
pub trait AssetStore: Send + Sync {
|
|
pub trait AssetStore: Send + Sync {
|
|
|
- /// Register an asset. A dumb instruction: insert it if absent and return
|
|
|
|
|
- /// the **number of assets created**, 1 if newly created and 0 if this id
|
|
|
|
|
- /// was already registered. The caller decides what 0 means.
|
|
|
|
|
|
|
+ /// Register an asset. A dumb instruction: insert it if absent, and return
|
|
|
|
|
+ /// the **number of assets created**. That is 1 if newly created, and 0 if
|
|
|
|
|
+ /// this id was already registered. The caller decides what 0 means.
|
|
|
async fn create_asset(&self, asset: Asset) -> Result<u64, StoreError>;
|
|
async fn create_asset(&self, asset: Asset) -> Result<u64, StoreError>;
|
|
|
/// Fetch one asset by id.
|
|
/// Fetch one asset by id.
|
|
|
async fn get_asset(&self, id: &AssetId) -> Result<Asset, StoreError>;
|
|
async fn get_asset(&self, id: &AssetId) -> Result<Asset, StoreError>;
|
|
|
/// Fetch one asset by its code, which is unique. `None` rather than
|
|
/// Fetch one asset by its code, which is unique. `None` rather than
|
|
|
- /// `NotFound`, because the caller asking by code is usually deciding
|
|
|
|
|
|
|
+ /// `NotFound`, because a caller that asks by code usually wants to know
|
|
|
/// whether to register it.
|
|
/// whether to register it.
|
|
|
async fn get_asset_by_code(&self, code: &str) -> Result<Option<Asset>, StoreError>;
|
|
async fn get_asset_by_code(&self, code: &str) -> Result<Option<Asset>, StoreError>;
|
|
|
/// List every registered asset.
|
|
/// List every registered asset.
|
|
@@ -217,9 +218,10 @@ pub trait AssetStore: Send + Sync {
|
|
|
/// Book persistence.
|
|
/// Book persistence.
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
pub trait BookStore: Send + Sync {
|
|
pub trait BookStore: Send + Sync {
|
|
|
- /// Create a new book. A dumb instruction: insert the book if absent and
|
|
|
|
|
- /// return the **number of books created** — **1** if newly created, **0** if
|
|
|
|
|
- /// a book with this id already existed. The caller decides what `0` means.
|
|
|
|
|
|
|
+ /// Create a new book. A dumb instruction: insert the book if absent, and
|
|
|
|
|
+ /// return the **number of books created**. That is **1** if newly created,
|
|
|
|
|
+ /// and **0** if a book with this id was already there. The caller decides
|
|
|
|
|
+ /// what `0` means.
|
|
|
async fn create_book(&self, book: Book) -> Result<u64, StoreError>;
|
|
async fn create_book(&self, book: Book) -> Result<u64, StoreError>;
|
|
|
/// Fetch a book by id.
|
|
/// Fetch a book by id.
|
|
|
async fn get_book(&self, id: &BookId) -> Result<Book, StoreError>;
|
|
async fn get_book(&self, id: &BookId) -> Result<Book, StoreError>;
|
|
@@ -227,19 +229,19 @@ pub trait BookStore: Send + Sync {
|
|
|
async fn list_books(&self) -> Result<Vec<Book>, StoreError>;
|
|
async fn list_books(&self) -> Result<Vec<Book>, StoreError>;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// One append-only cache point (ADR-0019): a balance snapshot for one
|
|
|
|
|
-/// `(account, asset)` and the commit-time watermark it covers, tagged with a
|
|
|
|
|
-/// monotonic `id`. Cache points are never updated; a read selects the highest
|
|
|
|
|
-/// `id`. A disposable, rebuildable accelerator, never the source of truth.
|
|
|
|
|
|
|
+/// One append-only cache point (ADR-0019). It holds a balance snapshot for one
|
|
|
|
|
+/// `(account, asset)`, the commit-time watermark it covers, and a monotonic
|
|
|
|
|
+/// `id`. A cache point is never updated, and a read selects the highest `id`. A
|
|
|
|
|
+/// disposable, rebuildable accelerator, and never the source of truth.
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
pub struct BalanceProjection {
|
|
pub struct BalanceProjection {
|
|
|
/// Monotonic cache-point id (a store-minted snowflake). Higher means newer.
|
|
/// Monotonic cache-point id (a store-minted snowflake). Higher means newer.
|
|
|
pub id: i64,
|
|
pub id: i64,
|
|
|
/// Account (base id plus subaccount) this cache point is for.
|
|
/// Account (base id plus subaccount) this cache point is for.
|
|
|
pub account: AccountId,
|
|
pub account: AccountId,
|
|
|
- /// Cached balance, carrying the asset it is in: the sum of every committed
|
|
|
|
|
- /// transfer's delta for this `(account, asset)` with commit time at or
|
|
|
|
|
- /// before `watermark`.
|
|
|
|
|
|
|
+ /// The cached balance, with the asset it is in. It is the sum of the delta
|
|
|
|
|
+ /// of every committed transfer for this `(account, asset)`, with a commit
|
|
|
|
|
+ /// time at or before `watermark`.
|
|
|
pub balance: Amount,
|
|
pub balance: Amount,
|
|
|
/// Commit-time cutoff (unix millis) the snapshot covers. A read folds in
|
|
/// Commit-time cutoff (unix millis) the snapshot covers. A read folds in
|
|
|
/// committed transfers with a commit time strictly greater than this.
|
|
/// committed transfers with a commit time strictly greater than this.
|
|
@@ -247,15 +249,15 @@ pub struct BalanceProjection {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Append-only balance cache points (ADR-0019). A disposable, rebuildable read
|
|
/// Append-only balance cache points (ADR-0019). A disposable, rebuildable read
|
|
|
-/// accelerator; the append-only postings stay the source of truth, and this is
|
|
|
|
|
-/// never consulted for the validate-time overdraft check. Cache points are only
|
|
|
|
|
-/// ever appended (never updated), and a read takes the one closest to (at or
|
|
|
|
|
-/// before) the target time, so concurrent appends need no lock.
|
|
|
|
|
|
|
+/// accelerator. The append-only postings stay the source of truth, and the
|
|
|
|
|
+/// validate-time overdraft check never reads this. A cache point is only ever
|
|
|
|
|
+/// appended and never updated. A read takes the one closest to the target time,
|
|
|
|
|
+/// at or before it, so concurrent appends need no lock.
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
pub trait BalanceProjectionStore: Send + Sync {
|
|
pub trait BalanceProjectionStore: Send + Sync {
|
|
|
- /// Append a new cache point for `(account, asset)`, minting a fresh monotonic
|
|
|
|
|
- /// `id`. A dumb instruction: it inserts one row and never updates an existing
|
|
|
|
|
- /// one.
|
|
|
|
|
|
|
+ /// Append a new cache point for `(account, asset)`, with a fresh monotonic
|
|
|
|
|
+ /// `id`. A dumb instruction: it inserts one row, and it never updates a row
|
|
|
|
|
+ /// that is already there.
|
|
|
async fn append_balance_projection(
|
|
async fn append_balance_projection(
|
|
|
&self,
|
|
&self,
|
|
|
account: &AccountId,
|
|
account: &AccountId,
|
|
@@ -263,12 +265,13 @@ pub trait BalanceProjectionStore: Send + Sync {
|
|
|
watermark: i64,
|
|
watermark: i64,
|
|
|
) -> Result<(), StoreError>;
|
|
) -> Result<(), StoreError>;
|
|
|
|
|
|
|
|
- /// Fetch the cache point for `(account, asset)` closest to `as_of`: the one
|
|
|
|
|
- /// with the largest `watermark` at or before `as_of` (tie-broken by highest
|
|
|
|
|
- /// `id`). This is the freshest snapshot a read as of `as_of` may use without
|
|
|
|
|
- /// covering transfers committed after `as_of`. Returns `None` if no cache
|
|
|
|
|
- /// point is at or before `as_of`. The caller supplies `as_of` (the store has
|
|
|
|
|
- /// no clock); the ledger passes the current time by default.
|
|
|
|
|
|
|
+ /// Fetch the cache point for `(account, asset)` closest to `as_of`. That is
|
|
|
|
|
+ /// the one with the largest `watermark` at or before `as_of`, tie-broken by
|
|
|
|
|
+ /// highest `id`. It is the freshest snapshot a read as of `as_of` can use
|
|
|
|
|
+ /// that covers no transfer committed after `as_of`. Returns `None` if no
|
|
|
|
|
+ /// cache point is at or before `as_of`. The caller supplies `as_of`,
|
|
|
|
|
+ /// because the store has no clock, and the ledger passes the current time
|
|
|
|
|
+ /// by default.
|
|
|
async fn get_closest_balance_projection(
|
|
async fn get_closest_balance_projection(
|
|
|
&self,
|
|
&self,
|
|
|
account: &AccountId,
|
|
account: &AccountId,
|
|
@@ -283,13 +286,13 @@ pub trait BalanceProjectionStore: Send + Sync {
|
|
|
|
|
|
|
|
/// Everything one transfer commits, applied in a single store transaction.
|
|
/// Everything one transfer commits, applied in a single store transaction.
|
|
|
///
|
|
///
|
|
|
-/// The pure core validates the transfer first; this request carries the
|
|
|
|
|
-/// validated effects. The store applies them atomically and re-enforces the
|
|
|
|
|
-/// three *stateful* guards inside the transaction (double-spend, freeze/close,
|
|
|
|
|
-/// overdraft floor), so a concurrent commit or freeze cannot slip between the
|
|
|
|
|
-/// check and the write.
|
|
|
|
|
|
|
+/// The pure core validates the transfer first, and this request carries the
|
|
|
|
|
+/// validated effects. The store applies them atomically. It also re-enforces
|
|
|
|
|
+/// the three *stateful* guards inside the transaction: double-spend,
|
|
|
|
|
+/// freeze/close and the overdraft floor. So a concurrent commit or freeze
|
|
|
|
|
+/// cannot slip between the check and the write.
|
|
|
pub struct CommitRequest<'a> {
|
|
pub struct CommitRequest<'a> {
|
|
|
- /// Content-addressed id of the transfer being committed.
|
|
|
|
|
|
|
+ /// Content-addressed id of the transfer to commit.
|
|
|
pub transfer_id: TransferId,
|
|
pub transfer_id: TransferId,
|
|
|
/// Postings to spend (delete from the live set). Empty for a deposit.
|
|
/// Postings to spend (delete from the live set). Empty for a deposit.
|
|
|
pub consume: &'a [PostingId],
|
|
pub consume: &'a [PostingId],
|
|
@@ -297,21 +300,23 @@ pub struct CommitRequest<'a> {
|
|
|
pub create: &'a [Posting],
|
|
pub create: &'a [Posting],
|
|
|
/// The transfer record to persist (envelope + receipt + created_at).
|
|
/// The transfer record to persist (envelope + receipt + created_at).
|
|
|
pub record: TransferRecord,
|
|
pub record: TransferRecord,
|
|
|
- /// Owners of the created and consumed postings: both the transfer's account
|
|
|
|
|
- /// index and the set whose freeze/close/floor the store re-checks.
|
|
|
|
|
|
|
+ /// Owners of the created and consumed postings. They are both the account
|
|
|
|
|
+ /// index of the transfer, and the set whose freeze, close and floor the
|
|
|
|
|
+ /// store re-checks.
|
|
|
pub involved: &'a [AccountId],
|
|
pub involved: &'a [AccountId],
|
|
|
/// The `TransferCommitted` event to append in the same transaction.
|
|
/// The `TransferCommitted` event to append in the same transaction.
|
|
|
pub event: LedgerEvent,
|
|
pub event: LedgerEvent,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Outcome of an atomic [`CommitStore::commit_envelope`]. A domain rejection is
|
|
/// Outcome of an atomic [`CommitStore::commit_envelope`]. A domain rejection is
|
|
|
-/// an `Ok(Rejected(..))` value, not a [`StoreError`]: the store reports a typed
|
|
|
|
|
-/// reason and the ledger maps it, keeping `StoreError` IO-only.
|
|
|
|
|
|
|
+/// an `Ok(Rejected(..))` value, and not a [`StoreError`]. The store reports a
|
|
|
|
|
+/// typed reason and the ledger maps it, so `StoreError` stays IO-only.
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
pub enum CommitOutcome {
|
|
pub enum CommitOutcome {
|
|
|
/// The transfer was newly applied in this call.
|
|
/// The transfer was newly applied in this call.
|
|
|
Committed(Receipt),
|
|
Committed(Receipt),
|
|
|
- /// The transfer id was already present; nothing changed (idempotent replay).
|
|
|
|
|
|
|
+ /// The transfer id was already present; nothing changed (idempotent
|
|
|
|
|
+ /// replay).
|
|
|
AlreadyCommitted(Receipt),
|
|
AlreadyCommitted(Receipt),
|
|
|
/// A stateful guard rejected the commit; nothing was applied.
|
|
/// A stateful guard rejected the commit; nothing was applied.
|
|
|
Rejected(CommitRejection),
|
|
Rejected(CommitRejection),
|
|
@@ -320,14 +325,14 @@ pub enum CommitOutcome {
|
|
|
/// Why an atomic commit was refused. Each maps to a typed ledger error.
|
|
/// Why an atomic commit was refused. Each maps to a typed ledger error.
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
pub enum CommitRejection {
|
|
pub enum CommitRejection {
|
|
|
- /// A consumed posting was not live (already spent, missing, or taken by a
|
|
|
|
|
- /// concurrent commit). Carries the first offending id.
|
|
|
|
|
|
|
+ /// A consumed posting was not live: already spent, absent, or taken by a
|
|
|
|
|
+ /// concurrent commit. Carries the first id at fault.
|
|
|
DoubleSpend(PostingId),
|
|
DoubleSpend(PostingId),
|
|
|
/// An involved account is frozen.
|
|
/// An involved account is frozen.
|
|
|
AccountFrozen(AccountId),
|
|
AccountFrozen(AccountId),
|
|
|
/// An involved account is closed.
|
|
/// An involved account is closed.
|
|
|
AccountClosed(AccountId),
|
|
AccountClosed(AccountId),
|
|
|
- /// The projected balance of an overdraft-forbidding account would go
|
|
|
|
|
|
|
+ /// The projected balance of an account that forbids overdraft would go
|
|
|
/// negative.
|
|
/// negative.
|
|
|
OverdraftExceeded {
|
|
OverdraftExceeded {
|
|
|
/// The account that would be overdrawn.
|
|
/// The account that would be overdrawn.
|
|
@@ -359,7 +364,7 @@ pub enum TransitionRejection {
|
|
|
VersionConflict {
|
|
VersionConflict {
|
|
|
/// The account whose version append was rejected.
|
|
/// The account whose version append was rejected.
|
|
|
account: AccountId,
|
|
account: AccountId,
|
|
|
- /// The version this attempt targeted.
|
|
|
|
|
|
|
+ /// The version this call aimed at.
|
|
|
expected: u64,
|
|
expected: u64,
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
@@ -368,20 +373,20 @@ pub enum TransitionRejection {
|
|
|
/// transition, applied in one store transaction.
|
|
/// transition, applied in one store transaction.
|
|
|
///
|
|
///
|
|
|
/// This is what replaces the multi-primitive reserve → finalize saga and its
|
|
/// This is what replaces the multi-primitive reserve → finalize saga and its
|
|
|
-/// write-ahead recovery: because each method is all-or-nothing, a crash leaves
|
|
|
|
|
-/// no half-applied state to reconcile, and the stateful guards are strict rather
|
|
|
|
|
-/// than best-effort (ADR-0023, superseding ADR-0003).
|
|
|
|
|
|
|
+/// write-ahead recovery. Each method is all-or-nothing, so a crash leaves no
|
|
|
|
|
+/// half-applied state to reconcile. The stateful guards are also strict rather
|
|
|
|
|
+/// than best-effort (ADR-0023, which supersedes ADR-0003).
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
pub trait CommitStore: Send + Sync {
|
|
pub trait CommitStore: Send + Sync {
|
|
|
- /// Commit a validated transfer atomically. Spends `consume`, inserts and
|
|
|
|
|
|
|
+ /// Commit a validated transfer atomically. It spends `consume`, inserts and
|
|
|
/// activates `create`, stores the transfer indexed under `involved`, and
|
|
/// activates `create`, stores the transfer indexed under `involved`, and
|
|
|
- /// appends `event` — all in one transaction, re-checking double-spend,
|
|
|
|
|
- /// freeze/close, and the overdraft floor inside it.
|
|
|
|
|
|
|
+ /// appends `event`. All of that is one transaction, which re-checks
|
|
|
|
|
+ /// double-spend, freeze/close and the overdraft floor inside itself.
|
|
|
async fn commit_envelope(&self, req: CommitRequest<'_>) -> Result<CommitOutcome, StoreError>;
|
|
async fn commit_envelope(&self, req: CommitRequest<'_>) -> Result<CommitOutcome, StoreError>;
|
|
|
|
|
|
|
|
- /// Apply one account-version transition atomically: append `next` (a version
|
|
|
|
|
- /// bump with flags flipped) and its lifecycle `event` in one transaction,
|
|
|
|
|
- /// guarding the version chain and a closed account.
|
|
|
|
|
|
|
+ /// Apply one account-version transition atomically. It appends `next` (a
|
|
|
|
|
+ /// version bump with flags flipped) and its lifecycle `event` in one
|
|
|
|
|
+ /// transaction. It also guards the version chain and a closed account.
|
|
|
async fn commit_transition(
|
|
async fn commit_transition(
|
|
|
&self,
|
|
&self,
|
|
|
next: Account,
|
|
next: Account,
|
|
@@ -393,7 +398,7 @@ pub trait CommitStore: Send + Sync {
|
|
|
// Composite trait
|
|
// Composite trait
|
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-/// Async storage abstraction composing all sub-traits.
|
|
|
|
|
|
|
+/// The async storage abstraction, which composes all the sub-traits.
|
|
|
pub trait Store:
|
|
pub trait Store:
|
|
|
AccountStore
|
|
AccountStore
|
|
|
+ PostingStore
|
|
+ PostingStore
|