|
@@ -10,10 +10,31 @@ use kuatia::ledger::{Ledger, ProjectionConfig};
|
|
|
use kuatia::mem_store::InMemoryStore;
|
|
use kuatia::mem_store::InMemoryStore;
|
|
|
use kuatia_core::*;
|
|
use kuatia_core::*;
|
|
|
|
|
|
|
|
|
|
+/// An amount of `asset` in minor units. An amount compares by asset id, and the
|
|
|
|
|
+/// tests register these same ids, so building the row here is equivalent to
|
|
|
|
|
+/// reading it back from the registry.
|
|
|
|
|
+fn money<V>(asset: AssetId, value: V) -> Amount
|
|
|
|
|
+where
|
|
|
|
|
+ Amount: From<(V, Arc<Asset>)>,
|
|
|
|
|
+{
|
|
|
|
|
+ Amount::from((value, Arc::new(Asset::new(asset, "TEST", "$", 2))))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn usd() -> AssetId {
|
|
fn usd() -> AssetId {
|
|
|
AssetId::new(1)
|
|
AssetId::new(1)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// Register the assets these tests read. A balance read hydrates its asset from
|
|
|
|
|
+/// the registry, so an unregistered one has no amount to report.
|
|
|
|
|
+async fn register_assets(ledger: &Arc<Ledger>) {
|
|
|
|
|
+ for (id, code, decimals) in [(1, "USD", 2), (2, "EUR", 2)] {
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .create_asset(Asset::new(AssetId::new(id), code, "$", decimals))
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn account(id: i64) -> AccountId {
|
|
fn account(id: i64) -> AccountId {
|
|
|
AccountId::new(id)
|
|
AccountId::new(id)
|
|
|
}
|
|
}
|
|
@@ -40,9 +61,9 @@ async fn overdraft(ledger: &Arc<Ledger>, id: i64) {
|
|
|
|
|
|
|
|
async fn deposit(ledger: &Arc<Ledger>, to: i64, amount: i64) {
|
|
async fn deposit(ledger: &Arc<Ledger>, to: i64, amount: i64) {
|
|
|
let transfer = TransferBuilder::new()
|
|
let transfer = TransferBuilder::new()
|
|
|
- .deposit(account(to), usd(), Cent::from(amount), external())
|
|
|
|
|
- .unwrap()
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .deposit(account(to), money(usd(), amount), external())
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
ledger.commit(transfer).await.unwrap();
|
|
ledger.commit(transfer).await.unwrap();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -57,8 +78,9 @@ fn unix_millis_now() -> i64 {
|
|
|
|
|
|
|
|
async fn pay(ledger: &Arc<Ledger>, from: i64, to: i64, amount: i64) {
|
|
async fn pay(ledger: &Arc<Ledger>, from: i64, to: i64, amount: i64) {
|
|
|
let transfer = TransferBuilder::new()
|
|
let transfer = TransferBuilder::new()
|
|
|
- .pay(account(from), account(to), usd(), Cent::from(amount))
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .pay(account(from), account(to), money(usd(), amount))
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
ledger.commit(transfer).await.unwrap();
|
|
ledger.commit(transfer).await.unwrap();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -66,8 +88,16 @@ async fn pay(ledger: &Arc<Ledger>, from: i64, to: i64, amount: i64) {
|
|
|
/// for every account, at every asset it might hold.
|
|
/// for every account, at every asset it might hold.
|
|
|
async fn assert_projection_matches(ledger: &Arc<Ledger>, ids: &[i64]) {
|
|
async fn assert_projection_matches(ledger: &Arc<Ledger>, ids: &[i64]) {
|
|
|
for &id in ids {
|
|
for &id in ids {
|
|
|
- let authoritative = ledger.balance_exact(&account(id), &usd()).await.unwrap();
|
|
|
|
|
- let projected = ledger.balance(&account(id), &usd()).await.unwrap();
|
|
|
|
|
|
|
+ let authoritative = ledger
|
|
|
|
|
+ .balance_exact(&account(id), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
|
|
+ let projected = ledger
|
|
|
|
|
+ .balance(&account(id), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
projected, authoritative,
|
|
projected, authoritative,
|
|
|
"projected balance for account {id} diverged from the live sum"
|
|
"projected balance for account {id} diverged from the live sum"
|
|
@@ -81,6 +111,7 @@ async fn assert_projection_matches(ledger: &Arc<Ledger>, ids: &[i64]) {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn projected_balance_matches_live_sum_without_refresh() {
|
|
async fn projected_balance_matches_live_sum_without_refresh() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 10).await;
|
|
overdraft(&ledger, 10).await;
|
|
@@ -110,6 +141,7 @@ async fn append_cache_point_snapshots_balance_and_preserves_answer() {
|
|
|
..ProjectionConfig::default()
|
|
..ProjectionConfig::default()
|
|
|
},
|
|
},
|
|
|
));
|
|
));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -131,10 +163,14 @@ async fn append_cache_point_snapshots_balance_and_preserves_answer() {
|
|
|
.await
|
|
.await
|
|
|
.unwrap()
|
|
.unwrap()
|
|
|
.expect("a cache point exists after append");
|
|
.expect("a cache point exists after append");
|
|
|
- assert_eq!(cache_point.balance, Cent::from(650));
|
|
|
|
|
|
|
+ assert_eq!(cache_point.balance, money(usd(), 650));
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
- ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
|
|
- Cent::from(650)
|
|
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 650
|
|
|
);
|
|
);
|
|
|
assert_projection_matches(&ledger, &[1, 2]).await;
|
|
assert_projection_matches(&ledger, &[1, 2]).await;
|
|
|
}
|
|
}
|
|
@@ -143,6 +179,7 @@ async fn append_cache_point_snapshots_balance_and_preserves_answer() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn commit_does_not_write_cache_point() {
|
|
async fn commit_does_not_write_cache_point() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -172,6 +209,7 @@ async fn read_appends_cache_point_after_interval() {
|
|
|
snapshot_interval: 1,
|
|
snapshot_interval: 1,
|
|
|
},
|
|
},
|
|
|
));
|
|
));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -180,7 +218,11 @@ async fn read_appends_cache_point_after_interval() {
|
|
|
pay(&ledger, 1, 2, 250).await;
|
|
pay(&ledger, 1, 2, 250).await;
|
|
|
|
|
|
|
|
// This read folds >= 1 credit/debit for account 1, so it spawns an append.
|
|
// This read folds >= 1 credit/debit for account 1, so it spawns an append.
|
|
|
- let _ = ledger.balance(&account(1), &usd()).await.unwrap();
|
|
|
|
|
|
|
+ let _ = ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
|
|
|
|
|
// Let the background append run, then confirm a cache point exists.
|
|
// Let the background append run, then confirm a cache point exists.
|
|
|
let mut appeared = false;
|
|
let mut appeared = false;
|
|
@@ -202,8 +244,16 @@ async fn read_appends_cache_point_after_interval() {
|
|
|
"a read past the interval should append a cache point"
|
|
"a read past the interval should append a cache point"
|
|
|
);
|
|
);
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
- ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
|
|
- ledger.balance_exact(&account(1), &usd()).await.unwrap()
|
|
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance_exact(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units()
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -218,6 +268,7 @@ async fn read_below_interval_appends_nothing() {
|
|
|
snapshot_interval: 1_000,
|
|
snapshot_interval: 1_000,
|
|
|
},
|
|
},
|
|
|
));
|
|
));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -226,10 +277,18 @@ async fn read_below_interval_appends_nothing() {
|
|
|
pay(&ledger, 1, 2, 250).await;
|
|
pay(&ledger, 1, 2, 250).await;
|
|
|
|
|
|
|
|
// A handful of credits/debits, far below the 1000 interval.
|
|
// A handful of credits/debits, far below the 1000 interval.
|
|
|
- let bal = ledger.balance(&account(1), &usd()).await.unwrap();
|
|
|
|
|
|
|
+ let bal = ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
bal,
|
|
bal,
|
|
|
- ledger.balance_exact(&account(1), &usd()).await.unwrap()
|
|
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance_exact(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units()
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
// Even after the background task has every chance to run, no cache point was
|
|
// Even after the background task has every chance to run, no cache point was
|
|
@@ -258,6 +317,7 @@ async fn read_below_interval_appends_nothing() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn snapshot_plus_nonempty_tail_matches_live_sum() {
|
|
async fn snapshot_plus_nonempty_tail_matches_live_sum() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -274,7 +334,7 @@ async fn snapshot_plus_nonempty_tail_matches_live_sum() {
|
|
|
let watermark = unix_millis_now();
|
|
let watermark = unix_millis_now();
|
|
|
ledger
|
|
ledger
|
|
|
.store()
|
|
.store()
|
|
|
- .append_balance_projection(&account(1), &usd(), snapshot, watermark)
|
|
|
|
|
|
|
+ .append_balance_projection(&account(1), snapshot.clone(), watermark)
|
|
|
.await
|
|
.await
|
|
|
.unwrap();
|
|
.unwrap();
|
|
|
|
|
|
|
@@ -289,8 +349,16 @@ async fn snapshot_plus_nonempty_tail_matches_live_sum() {
|
|
|
deposit(&ledger, 1, 500).await;
|
|
deposit(&ledger, 1, 500).await;
|
|
|
|
|
|
|
|
// The read is snapshot + folded tail, and still equals the live sum.
|
|
// The read is snapshot + folded tail, and still equals the live sum.
|
|
|
- let projected = ledger.balance(&account(1), &usd()).await.unwrap();
|
|
|
|
|
- let authoritative = ledger.balance_exact(&account(1), &usd()).await.unwrap();
|
|
|
|
|
|
|
+ let projected = ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
|
|
+ let authoritative = ledger
|
|
|
|
|
+ .balance_exact(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
projected, authoritative,
|
|
projected, authoritative,
|
|
|
"snapshot + non-empty tail diverged from the live sum"
|
|
"snapshot + non-empty tail diverged from the live sum"
|
|
@@ -299,7 +367,8 @@ async fn snapshot_plus_nonempty_tail_matches_live_sum() {
|
|
|
// actually carried value. Otherwise this silently degrades to the empty-tail
|
|
// actually carried value. Otherwise this silently degrades to the empty-tail
|
|
|
// case the other tests already cover.
|
|
// case the other tests already cover.
|
|
|
assert_ne!(
|
|
assert_ne!(
|
|
|
- snapshot, authoritative,
|
|
|
|
|
|
|
+ snapshot.minor_units(),
|
|
|
|
|
+ authoritative,
|
|
|
"the snapshot already held the full balance; the tail was empty"
|
|
"the snapshot already held the full balance; the tail was empty"
|
|
|
);
|
|
);
|
|
|
assert_projection_matches(&ledger, &[1, 2]).await;
|
|
assert_projection_matches(&ledger, &[1, 2]).await;
|
|
@@ -343,6 +412,7 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
..ProjectionConfig::default()
|
|
..ProjectionConfig::default()
|
|
|
},
|
|
},
|
|
|
));
|
|
));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
|
|
|
|
|
// Accounts under test, including two subaccounts and both overdraft kinds.
|
|
// Accounts under test, including two subaccounts and both overdraft kinds.
|
|
|
let no_overdraft_ids = [
|
|
let no_overdraft_ids = [
|
|
@@ -395,9 +465,9 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
let to = accounts[rng.below(accounts.len() as u64) as usize];
|
|
let to = accounts[rng.below(accounts.len() as u64) as usize];
|
|
|
let amount = 1 + rng.below(500) as i64;
|
|
let amount = 1 + rng.below(500) as i64;
|
|
|
let t = TransferBuilder::new()
|
|
let t = TransferBuilder::new()
|
|
|
- .deposit(to, asset, Cent::from(amount), ext)
|
|
|
|
|
- .unwrap()
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .deposit(to, money(asset, amount), ext)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
|
receipts.push(r.transfer_id);
|
|
receipts.push(r.transfer_id);
|
|
|
}
|
|
}
|
|
@@ -407,8 +477,9 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
let from = accounts[rng.below(accounts.len() as u64) as usize];
|
|
let from = accounts[rng.below(accounts.len() as u64) as usize];
|
|
|
let amount = 1 + rng.below(200) as i64;
|
|
let amount = 1 + rng.below(200) as i64;
|
|
|
let t = TransferBuilder::new()
|
|
let t = TransferBuilder::new()
|
|
|
- .withdraw(from, asset, Cent::from(amount), ext)
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .withdraw(from, money(asset, amount), ext)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
|
receipts.push(r.transfer_id);
|
|
receipts.push(r.transfer_id);
|
|
|
}
|
|
}
|
|
@@ -429,8 +500,9 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
}
|
|
}
|
|
|
let amount = 1 + rng.below(300) as i64;
|
|
let amount = 1 + rng.below(300) as i64;
|
|
|
let t = TransferBuilder::new()
|
|
let t = TransferBuilder::new()
|
|
|
- .pay(from, to, asset, Cent::from(amount))
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .pay(from, to, money(asset, amount))
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
if let Ok(r) = ledger.commit(t).await {
|
|
|
receipts.push(r.transfer_id);
|
|
receipts.push(r.transfer_id);
|
|
|
}
|
|
}
|
|
@@ -441,8 +513,12 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
// the authoritative live-posting sum for every (account, asset).
|
|
// the authoritative live-posting sum for every (account, asset).
|
|
|
for account in accounts.iter().chain(std::iter::once(&ext)) {
|
|
for account in accounts.iter().chain(std::iter::once(&ext)) {
|
|
|
for asset in &assets {
|
|
for asset in &assets {
|
|
|
- let authoritative = ledger.balance_exact(account, asset).await.unwrap();
|
|
|
|
|
- let projected = ledger.balance(account, asset).await.unwrap();
|
|
|
|
|
|
|
+ let authoritative = ledger
|
|
|
|
|
+ .balance_exact(account, asset)
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
|
|
+ let projected = ledger.balance(account, asset).await.unwrap().minor_units();
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
projected, authoritative,
|
|
projected, authoritative,
|
|
|
"projected != live sum for {account:?} / {asset:?}"
|
|
"projected != live sum for {account:?} / {asset:?}"
|
|
@@ -457,6 +533,7 @@ async fn projection_matches_live_sum_across_random_utxo_history() {
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
|
|
async fn projection_matches_under_concurrent_commits() {
|
|
async fn projection_matches_under_concurrent_commits() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
for id in 2..=9 {
|
|
for id in 2..=9 {
|
|
|
no_overdraft(&ledger, id).await;
|
|
no_overdraft(&ledger, id).await;
|
|
@@ -469,8 +546,9 @@ async fn projection_matches_under_concurrent_commits() {
|
|
|
let ledger = Arc::clone(&ledger);
|
|
let ledger = Arc::clone(&ledger);
|
|
|
handles.push(tokio::spawn(async move {
|
|
handles.push(tokio::spawn(async move {
|
|
|
let transfer = TransferBuilder::new()
|
|
let transfer = TransferBuilder::new()
|
|
|
- .pay(account(1), account(payee), usd(), Cent::from(10))
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .pay(account(1), account(payee), money(usd(), 10))
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .unwrap();
|
|
|
let _ = ledger.commit(transfer).await;
|
|
let _ = ledger.commit(transfer).await;
|
|
|
}));
|
|
}));
|
|
|
}
|
|
}
|
|
@@ -488,6 +566,7 @@ async fn projection_matches_under_concurrent_commits() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn balance_as_of_excludes_later_commits() {
|
|
async fn balance_as_of_excludes_later_commits() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -503,13 +582,18 @@ async fn balance_as_of_excludes_later_commits() {
|
|
|
ledger
|
|
ledger
|
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
|
.await
|
|
.await
|
|
|
- .unwrap(),
|
|
|
|
|
- Cent::from(750),
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 750,
|
|
|
"the as-of read picked up a transfer committed after it"
|
|
"the as-of read picked up a transfer committed after it"
|
|
|
);
|
|
);
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
- ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
|
|
- Cent::from(1250)
|
|
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 1250
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -522,6 +606,7 @@ async fn balance_as_of_excludes_later_commits() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -533,7 +618,7 @@ async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
|
let watermark = unix_millis_now();
|
|
let watermark = unix_millis_now();
|
|
|
ledger
|
|
ledger
|
|
|
.store()
|
|
.store()
|
|
|
- .append_balance_projection(&account(1), &usd(), snapshot, watermark)
|
|
|
|
|
|
|
+ .append_balance_projection(&account(1), snapshot.clone(), watermark)
|
|
|
.await
|
|
.await
|
|
|
.unwrap();
|
|
.unwrap();
|
|
|
|
|
|
|
@@ -548,17 +633,22 @@ async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
|
ledger
|
|
ledger
|
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
|
.await
|
|
.await
|
|
|
- .unwrap(),
|
|
|
|
|
- Cent::from(1250),
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 1250,
|
|
|
"snapshot plus bounded tail diverged from the total at as_of"
|
|
"snapshot plus bounded tail diverged from the total at as_of"
|
|
|
);
|
|
);
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
- ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
|
|
- Cent::from(1550)
|
|
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 1550
|
|
|
);
|
|
);
|
|
|
assert_ne!(
|
|
assert_ne!(
|
|
|
- snapshot,
|
|
|
|
|
- Cent::from(1250),
|
|
|
|
|
|
|
+ snapshot.minor_units(),
|
|
|
|
|
+ 1250,
|
|
|
"the snapshot already held the as-of balance; the tail was empty"
|
|
"the snapshot already held the as-of balance; the tail was empty"
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
@@ -568,6 +658,7 @@ async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn balance_as_of_ignores_a_cache_point_above_it() {
|
|
async fn balance_as_of_ignores_a_cache_point_above_it() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -582,7 +673,7 @@ async fn balance_as_of_ignores_a_cache_point_above_it() {
|
|
|
let full = ledger.balance_exact(&account(1), &usd()).await.unwrap();
|
|
let full = ledger.balance_exact(&account(1), &usd()).await.unwrap();
|
|
|
ledger
|
|
ledger
|
|
|
.store()
|
|
.store()
|
|
|
- .append_balance_projection(&account(1), &usd(), full, unix_millis_now())
|
|
|
|
|
|
|
+ .append_balance_projection(&account(1), full.clone(), unix_millis_now())
|
|
|
.await
|
|
.await
|
|
|
.unwrap();
|
|
.unwrap();
|
|
|
|
|
|
|
@@ -590,11 +681,19 @@ async fn balance_as_of_ignores_a_cache_point_above_it() {
|
|
|
ledger
|
|
ledger
|
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
.balance_as_of(&account(1), &usd(), as_of)
|
|
|
.await
|
|
.await
|
|
|
- .unwrap(),
|
|
|
|
|
- Cent::from(750),
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 750,
|
|
|
"the as-of read used a cache point covering later commits"
|
|
"the as-of read used a cache point covering later commits"
|
|
|
);
|
|
);
|
|
|
- assert_eq!(ledger.balance(&account(1), &usd()).await.unwrap(), full);
|
|
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ full.minor_units()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// An as-of read appends no cache point, even past the interval that makes the
|
|
/// An as-of read appends no cache point, even past the interval that makes the
|
|
@@ -608,6 +707,7 @@ async fn balance_as_of_appends_no_cache_point() {
|
|
|
snapshot_interval: 1,
|
|
snapshot_interval: 1,
|
|
|
},
|
|
},
|
|
|
));
|
|
));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -618,7 +718,8 @@ async fn balance_as_of_appends_no_cache_point() {
|
|
|
let _ = ledger
|
|
let _ = ledger
|
|
|
.balance_as_of(&account(1), &usd(), unix_millis_now())
|
|
.balance_as_of(&account(1), &usd(), unix_millis_now())
|
|
|
.await
|
|
.await
|
|
|
- .unwrap();
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units();
|
|
|
|
|
|
|
|
for _ in 0..1000 {
|
|
for _ in 0..1000 {
|
|
|
tokio::task::yield_now().await;
|
|
tokio::task::yield_now().await;
|
|
@@ -639,6 +740,7 @@ async fn balance_as_of_appends_no_cache_point() {
|
|
|
#[tokio::test]
|
|
#[tokio::test]
|
|
|
async fn balance_as_of_outside_the_committed_history() {
|
|
async fn balance_as_of_outside_the_committed_history() {
|
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
|
|
+ register_assets(&ledger).await;
|
|
|
no_overdraft(&ledger, 1).await;
|
|
no_overdraft(&ledger, 1).await;
|
|
|
no_overdraft(&ledger, 2).await;
|
|
no_overdraft(&ledger, 2).await;
|
|
|
overdraft(&ledger, 99).await;
|
|
overdraft(&ledger, 99).await;
|
|
@@ -652,14 +754,20 @@ async fn balance_as_of_outside_the_committed_history() {
|
|
|
ledger
|
|
ledger
|
|
|
.balance_as_of(&account(1), &usd(), before)
|
|
.balance_as_of(&account(1), &usd(), before)
|
|
|
.await
|
|
.await
|
|
|
- .unwrap(),
|
|
|
|
|
- Cent::from(0)
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ 0
|
|
|
);
|
|
);
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
|
ledger
|
|
ledger
|
|
|
.balance_as_of(&account(1), &usd(), i64::MAX)
|
|
.balance_as_of(&account(1), &usd(), i64::MAX)
|
|
|
.await
|
|
.await
|
|
|
- .unwrap(),
|
|
|
|
|
- ledger.balance(&account(1), &usd()).await.unwrap()
|
|
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units(),
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .balance(&account(1), &usd())
|
|
|
|
|
+ .await
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .minor_units()
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|