integration.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. #![allow(missing_docs)]
  2. use std::sync::Arc;
  3. use kuatia::ledger::Ledger;
  4. use kuatia::mem_store::InMemoryStore;
  5. use kuatia_core::*;
  6. use std::collections::BTreeMap;
  7. fn usd() -> AssetId {
  8. AssetId::new(1)
  9. }
  10. fn eur() -> AssetId {
  11. AssetId::new(2)
  12. }
  13. fn account(id: i64) -> AccountId {
  14. AccountId::new(id)
  15. }
  16. fn external() -> AccountId {
  17. AccountId::new(99)
  18. }
  19. fn make_account(id: i64, policy: AccountPolicy) -> Account {
  20. Account {
  21. id: AccountId::new(id),
  22. version: 1,
  23. policy,
  24. flags: AccountFlags::empty(),
  25. book: BookId(0),
  26. user_data: UserData::default(),
  27. metadata: BTreeMap::new(),
  28. }
  29. }
  30. async fn setup_ledger() -> Arc<Ledger> {
  31. let store = InMemoryStore::new();
  32. let ledger = Arc::new(Ledger::new(store));
  33. ledger
  34. .store()
  35. .create_account(make_account(1, AccountPolicy::NoOverdraft))
  36. .await
  37. .unwrap();
  38. ledger
  39. .store()
  40. .create_account(make_account(2, AccountPolicy::NoOverdraft))
  41. .await
  42. .unwrap();
  43. ledger
  44. .store()
  45. .create_account(make_account(3, AccountPolicy::NoOverdraft))
  46. .await
  47. .unwrap();
  48. ledger
  49. .store()
  50. .create_account(make_account(99, AccountPolicy::ExternalAccount))
  51. .await
  52. .unwrap();
  53. ledger
  54. }
  55. /// Helper: deposit via commit()
  56. async fn deposit(
  57. ledger: &Arc<Ledger>,
  58. to: AccountId,
  59. asset: AssetId,
  60. amount: Cent,
  61. ext: AccountId,
  62. ) -> Receipt {
  63. let transfer = TransferBuilder::new()
  64. .deposit(to, asset, amount, ext)
  65. .unwrap()
  66. .build();
  67. ledger.commit(transfer).await.unwrap()
  68. }
  69. /// Helper: pay via commit()
  70. async fn pay(
  71. ledger: &Arc<Ledger>,
  72. from: AccountId,
  73. to: AccountId,
  74. asset: AssetId,
  75. amount: Cent,
  76. ) -> Receipt {
  77. let transfer = TransferBuilder::new().pay(from, to, asset, amount).build();
  78. ledger.commit(transfer).await.unwrap()
  79. }
  80. /// Helper: withdraw via commit()
  81. async fn withdraw(
  82. ledger: &Arc<Ledger>,
  83. from: AccountId,
  84. asset: AssetId,
  85. amount: Cent,
  86. ext: AccountId,
  87. ) -> Receipt {
  88. let transfer = TransferBuilder::new()
  89. .withdraw(from, asset, amount, ext)
  90. .build();
  91. ledger.commit(transfer).await.unwrap()
  92. }
  93. // ---------------------------------------------------------------------------
  94. // §4.1 Deposit
  95. // ---------------------------------------------------------------------------
  96. #[tokio::test]
  97. async fn deposit_creates_balanced_postings() {
  98. let ledger = setup_ledger().await;
  99. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  100. assert_eq!(
  101. ledger.balance(&account(1), &usd()).await.unwrap(),
  102. Cent::from(100)
  103. );
  104. assert_eq!(
  105. ledger.balance(&external(), &usd()).await.unwrap(),
  106. Cent::from(-100)
  107. );
  108. }
  109. // ---------------------------------------------------------------------------
  110. // §4.2 Internal transfer with change
  111. // ---------------------------------------------------------------------------
  112. #[tokio::test]
  113. async fn pay_with_change() {
  114. let ledger = setup_ledger().await;
  115. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  116. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  117. assert_eq!(
  118. ledger.balance(&account(1), &usd()).await.unwrap(),
  119. Cent::from(50)
  120. );
  121. assert_eq!(
  122. ledger.balance(&account(2), &usd()).await.unwrap(),
  123. Cent::from(50)
  124. );
  125. assert_eq!(
  126. ledger.balance(&external(), &usd()).await.unwrap(),
  127. Cent::from(-100)
  128. );
  129. }
  130. // ---------------------------------------------------------------------------
  131. // §4.3 Multi-hop
  132. // ---------------------------------------------------------------------------
  133. #[tokio::test]
  134. async fn multi_hop_transfer() {
  135. let ledger = setup_ledger().await;
  136. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  137. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  138. pay(&ledger, account(2), account(3), usd(), Cent::from(20)).await;
  139. assert_eq!(
  140. ledger.balance(&account(1), &usd()).await.unwrap(),
  141. Cent::from(50)
  142. );
  143. assert_eq!(
  144. ledger.balance(&account(2), &usd()).await.unwrap(),
  145. Cent::from(30)
  146. );
  147. assert_eq!(
  148. ledger.balance(&account(3), &usd()).await.unwrap(),
  149. Cent::from(20)
  150. );
  151. assert_eq!(
  152. ledger.balance(&external(), &usd()).await.unwrap(),
  153. Cent::from(-100)
  154. );
  155. }
  156. // ---------------------------------------------------------------------------
  157. // §4.5 Withdrawal
  158. // ---------------------------------------------------------------------------
  159. #[tokio::test]
  160. async fn withdrawal_reduces_external_liability() {
  161. let ledger = setup_ledger().await;
  162. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  163. withdraw(&ledger, account(1), usd(), Cent::from(50), external()).await;
  164. assert_eq!(
  165. ledger.balance(&account(1), &usd()).await.unwrap(),
  166. Cent::from(50)
  167. );
  168. assert_eq!(
  169. ledger.balance(&external(), &usd()).await.unwrap(),
  170. Cent::from(-50)
  171. );
  172. }
  173. // ---------------------------------------------------------------------------
  174. // Full round-trip: deposit -> pay -> withdraw -> verify total = 0
  175. // ---------------------------------------------------------------------------
  176. #[tokio::test]
  177. async fn full_round_trip() {
  178. let ledger = setup_ledger().await;
  179. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  180. pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  181. withdraw(&ledger, account(2), usd(), Cent::from(60), external()).await;
  182. withdraw(&ledger, account(1), usd(), Cent::from(40), external()).await;
  183. assert_eq!(
  184. ledger.balance(&account(1), &usd()).await.unwrap(),
  185. Cent::ZERO
  186. );
  187. assert_eq!(
  188. ledger.balance(&account(2), &usd()).await.unwrap(),
  189. Cent::ZERO
  190. );
  191. assert_eq!(
  192. ledger.balance(&external(), &usd()).await.unwrap(),
  193. Cent::ZERO
  194. );
  195. }
  196. // ---------------------------------------------------------------------------
  197. // Idempotency -- committing same envelope twice returns same receipt
  198. // ---------------------------------------------------------------------------
  199. #[tokio::test]
  200. async fn idempotent_commit() {
  201. let ledger = setup_ledger().await;
  202. let envelope = EnvelopeBuilder::new()
  203. .creates(vec![
  204. NewPosting {
  205. owner: account(1),
  206. asset: usd(),
  207. value: Cent::from(100),
  208. payer: None,
  209. },
  210. NewPosting {
  211. owner: external(),
  212. asset: usd(),
  213. value: Cent::from(-100),
  214. payer: None,
  215. },
  216. ])
  217. .build();
  218. let r1 = ledger.commit_envelope(envelope.clone()).await.unwrap();
  219. let r2 = ledger.commit_envelope(envelope).await.unwrap();
  220. assert_eq!(r1.transfer_id, r2.transfer_id);
  221. // Balance should only be 100, not 200 (second commit was a no-op)
  222. assert_eq!(
  223. ledger.balance(&account(1), &usd()).await.unwrap(),
  224. Cent::from(100)
  225. );
  226. }
  227. // ---------------------------------------------------------------------------
  228. // Overdraft prevention
  229. // ---------------------------------------------------------------------------
  230. #[tokio::test]
  231. async fn overdraft_rejected() {
  232. let ledger = setup_ledger().await;
  233. deposit(&ledger, account(1), usd(), Cent::from(50), external()).await;
  234. let transfer = TransferBuilder::new()
  235. .pay(account(1), account(2), usd(), Cent::from(100))
  236. .build();
  237. let result = ledger.commit(transfer).await;
  238. assert!(result.is_err());
  239. // Balance unchanged
  240. assert_eq!(
  241. ledger.balance(&account(1), &usd()).await.unwrap(),
  242. Cent::from(50)
  243. );
  244. }
  245. // ---------------------------------------------------------------------------
  246. // Reverse: forward compensating transfer
  247. // ---------------------------------------------------------------------------
  248. #[tokio::test]
  249. async fn reverse_restores_balances() {
  250. let ledger = setup_ledger().await;
  251. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  252. let pay_receipt = pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  253. assert_eq!(
  254. ledger.balance(&account(1), &usd()).await.unwrap(),
  255. Cent::from(40)
  256. );
  257. assert_eq!(
  258. ledger.balance(&account(2), &usd()).await.unwrap(),
  259. Cent::from(60)
  260. );
  261. // Reverse the payment
  262. ledger.reverse(&pay_receipt.transfer_id).await.unwrap();
  263. assert_eq!(
  264. ledger.balance(&account(1), &usd()).await.unwrap(),
  265. Cent::from(100)
  266. );
  267. assert_eq!(
  268. ledger.balance(&account(2), &usd()).await.unwrap(),
  269. Cent::ZERO
  270. );
  271. }
  272. // ---------------------------------------------------------------------------
  273. // Frozen account blocks transfers
  274. // ---------------------------------------------------------------------------
  275. #[tokio::test]
  276. async fn frozen_account_rejected() {
  277. let store = InMemoryStore::new();
  278. let ledger = Arc::new(Ledger::new(store));
  279. let mut frozen = make_account(1, AccountPolicy::NoOverdraft);
  280. frozen.flags = AccountFlags::FROZEN;
  281. ledger.store().create_account(frozen).await.unwrap();
  282. ledger
  283. .store()
  284. .create_account(make_account(99, AccountPolicy::ExternalAccount))
  285. .await
  286. .unwrap();
  287. let transfer = TransferBuilder::new()
  288. .deposit(account(1), usd(), Cent::from(100), external())
  289. .unwrap()
  290. .build();
  291. let result = ledger.commit(transfer).await;
  292. assert!(result.is_err());
  293. }
  294. // ---------------------------------------------------------------------------
  295. // Multi-asset: each asset conserves independently
  296. // ---------------------------------------------------------------------------
  297. #[tokio::test]
  298. async fn multi_asset_independent_balances() {
  299. let ledger = setup_ledger().await;
  300. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  301. deposit(&ledger, account(1), eur(), Cent::from(200), external()).await;
  302. assert_eq!(
  303. ledger.balance(&account(1), &usd()).await.unwrap(),
  304. Cent::from(100)
  305. );
  306. assert_eq!(
  307. ledger.balance(&account(1), &eur()).await.unwrap(),
  308. Cent::from(200)
  309. );
  310. pay(&ledger, account(1), account(2), usd(), Cent::from(30)).await;
  311. assert_eq!(
  312. ledger.balance(&account(1), &usd()).await.unwrap(),
  313. Cent::from(70)
  314. );
  315. assert_eq!(
  316. ledger.balance(&account(1), &eur()).await.unwrap(),
  317. Cent::from(200)
  318. );
  319. assert_eq!(
  320. ledger.balance(&account(2), &usd()).await.unwrap(),
  321. Cent::from(30)
  322. );
  323. }
  324. // ---------------------------------------------------------------------------
  325. // §4.4 FX trade via market account
  326. // ---------------------------------------------------------------------------
  327. #[tokio::test]
  328. async fn fx_trade_via_market_account() {
  329. let store = InMemoryStore::new();
  330. let ledger = Arc::new(Ledger::new(store));
  331. // Setup accounts
  332. for (id, policy) in [
  333. (1, AccountPolicy::NoOverdraft),
  334. (50, AccountPolicy::SystemAccount), // FX market account
  335. (99, AccountPolicy::ExternalAccount),
  336. ] {
  337. ledger
  338. .store()
  339. .create_account(make_account(id, policy))
  340. .await
  341. .unwrap();
  342. }
  343. // Seed: account1 has 100 USD, fx has 92 EUR
  344. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  345. deposit(&ledger, account(50), eur(), Cent::from(92), external()).await;
  346. // FX trade: account1 sells 100 USD, buys 92 EUR
  347. // Build the atomic envelope manually since it spans two assets
  348. let a1_usd_postings = ledger
  349. .store()
  350. .get_postings_by_account(&account(1), Some(&usd()), Some(PostingStatus::Active))
  351. .await
  352. .unwrap();
  353. let fx_eur_postings = ledger
  354. .store()
  355. .get_postings_by_account(&account(50), Some(&eur()), Some(PostingStatus::Active))
  356. .await
  357. .unwrap();
  358. let envelope = EnvelopeBuilder::new()
  359. .consumes(vec![a1_usd_postings[0].id, fx_eur_postings[0].id])
  360. .creates(vec![
  361. NewPosting {
  362. owner: account(50),
  363. asset: usd(),
  364. value: Cent::from(100),
  365. payer: Some(account(1)),
  366. },
  367. NewPosting {
  368. owner: account(1),
  369. asset: eur(),
  370. value: Cent::from(92),
  371. payer: Some(account(50)),
  372. },
  373. ])
  374. .build();
  375. ledger.commit_envelope(envelope).await.unwrap();
  376. // Verify
  377. assert_eq!(
  378. ledger.balance(&account(1), &usd()).await.unwrap(),
  379. Cent::ZERO
  380. );
  381. assert_eq!(
  382. ledger.balance(&account(1), &eur()).await.unwrap(),
  383. Cent::from(92)
  384. );
  385. assert_eq!(
  386. ledger.balance(&account(50), &usd()).await.unwrap(),
  387. Cent::from(100)
  388. );
  389. assert_eq!(
  390. ledger.balance(&account(50), &eur()).await.unwrap(),
  391. Cent::ZERO
  392. );
  393. }
  394. // ---------------------------------------------------------------------------
  395. // Account lifecycle: freeze / unfreeze / close
  396. // ---------------------------------------------------------------------------
  397. #[tokio::test]
  398. async fn freeze_blocks_transfers() {
  399. let ledger = setup_ledger().await;
  400. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  401. ledger.freeze(&account(1)).await.unwrap();
  402. // Paying from a frozen account should fail
  403. let transfer = TransferBuilder::new()
  404. .pay(account(1), account(2), usd(), Cent::from(50))
  405. .build();
  406. let result = ledger.commit(transfer).await;
  407. assert!(result.is_err());
  408. // Balance unchanged
  409. assert_eq!(
  410. ledger.balance(&account(1), &usd()).await.unwrap(),
  411. Cent::from(100)
  412. );
  413. }
  414. #[tokio::test]
  415. async fn unfreeze_re_enables_transfers() {
  416. let ledger = setup_ledger().await;
  417. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  418. ledger.freeze(&account(1)).await.unwrap();
  419. ledger.unfreeze(&account(1)).await.unwrap();
  420. // Should work again
  421. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  422. assert_eq!(
  423. ledger.balance(&account(1), &usd()).await.unwrap(),
  424. Cent::from(50)
  425. );
  426. }
  427. #[tokio::test]
  428. async fn close_account_with_zero_balance() {
  429. let ledger = setup_ledger().await;
  430. // Account 3 has never transacted -- zero balance, no postings
  431. ledger.close(&account(3)).await.unwrap();
  432. // Closed account rejects deposits
  433. let transfer = TransferBuilder::new()
  434. .deposit(account(3), usd(), Cent::from(100), external())
  435. .unwrap()
  436. .build();
  437. let result = ledger.commit(transfer).await;
  438. assert!(result.is_err());
  439. }
  440. #[tokio::test]
  441. async fn close_account_with_balance_rejected() {
  442. let ledger = setup_ledger().await;
  443. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  444. // Should fail -- account still has active postings
  445. let result = ledger.close(&account(1)).await;
  446. assert!(result.is_err());
  447. // Balance unchanged
  448. assert_eq!(
  449. ledger.balance(&account(1), &usd()).await.unwrap(),
  450. Cent::from(100)
  451. );
  452. }
  453. #[tokio::test]
  454. async fn close_rejects_reserved_postings() {
  455. let ledger = setup_ledger().await;
  456. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  457. // Reserve the account's only posting (a transfer in flight): Active → PendingInactive.
  458. let postings = ledger
  459. .store()
  460. .get_postings_by_account(&account(1), Some(&usd()), Some(PostingStatus::Active))
  461. .await
  462. .unwrap();
  463. ledger
  464. .store()
  465. .reserve_postings(&[postings[0].id], ReservationId::new(1))
  466. .await
  467. .unwrap();
  468. // Close must reject: the posting is live (PendingInactive), not Inactive.
  469. let result = ledger.close(&account(1)).await;
  470. assert!(result.is_err());
  471. }
  472. #[tokio::test]
  473. async fn freeze_closed_account_rejected() {
  474. let ledger = setup_ledger().await;
  475. ledger.close(&account(3)).await.unwrap();
  476. let result = ledger.freeze(&account(3)).await;
  477. assert!(result.is_err());
  478. }
  479. // ---------------------------------------------------------------------------
  480. // Query layer: history, postings, list_accounts, get_account
  481. // ---------------------------------------------------------------------------
  482. #[tokio::test]
  483. async fn history_returns_transfers_for_account() {
  484. let ledger = setup_ledger().await;
  485. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  486. pay(&ledger, account(1), account(2), usd(), Cent::from(40)).await;
  487. deposit(&ledger, account(2), usd(), Cent::from(50), external()).await;
  488. let h1 = ledger.history(&account(1)).await.unwrap();
  489. // account(1) was in the deposit and the pay
  490. assert_eq!(h1.len(), 2);
  491. let h2 = ledger.history(&account(2)).await.unwrap();
  492. // account(2) was in the pay and a second deposit
  493. assert_eq!(h2.len(), 2);
  494. }
  495. #[tokio::test]
  496. async fn postings_returns_all_postings() {
  497. let ledger = setup_ledger().await;
  498. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  499. pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  500. let posts = ledger.postings(&account(1)).await.unwrap();
  501. // Original 100 posting (now consumed) + 40 change posting (active)
  502. assert_eq!(posts.len(), 2);
  503. let active: Vec<_> = posts.iter().filter(|p| p.is_active()).collect();
  504. assert_eq!(active.len(), 1);
  505. assert_eq!(active[0].value, Cent::from(40));
  506. }
  507. #[tokio::test]
  508. async fn list_accounts_returns_all() {
  509. let ledger = setup_ledger().await;
  510. let accounts = ledger.list_accounts().await.unwrap();
  511. // setup_ledger creates accounts 1, 2, 3, 99
  512. assert_eq!(accounts.len(), 4);
  513. }
  514. #[tokio::test]
  515. async fn get_account_by_id() {
  516. let ledger = setup_ledger().await;
  517. let acc = ledger.get_account(&account(1)).await.unwrap();
  518. assert_eq!(acc.id, account(1));
  519. assert_eq!(acc.policy, AccountPolicy::NoOverdraft);
  520. }
  521. #[tokio::test]
  522. async fn get_account_not_found() {
  523. let ledger = setup_ledger().await;
  524. let result = ledger.get_account(&account(999)).await;
  525. assert!(result.is_err());
  526. }
  527. // ---------------------------------------------------------------------------
  528. // Append-only accounts: version history, version conflict, account_versions
  529. // ---------------------------------------------------------------------------
  530. #[tokio::test]
  531. async fn account_history_tracks_versions() {
  532. let ledger = setup_ledger().await;
  533. // Version 1: created
  534. let history = ledger.account_history(&account(1)).await.unwrap();
  535. assert_eq!(history.len(), 1);
  536. assert_eq!(history[0].version, 1);
  537. // Version 2: frozen
  538. ledger.freeze(&account(1)).await.unwrap();
  539. let history = ledger.account_history(&account(1)).await.unwrap();
  540. assert_eq!(history.len(), 2);
  541. assert_eq!(history[1].version, 2);
  542. assert!(history[1].is_frozen());
  543. // Version 3: unfrozen
  544. ledger.unfreeze(&account(1)).await.unwrap();
  545. let history = ledger.account_history(&account(1)).await.unwrap();
  546. assert_eq!(history.len(), 3);
  547. assert_eq!(history[2].version, 3);
  548. assert!(!history[2].is_frozen());
  549. }
  550. #[tokio::test]
  551. async fn store_never_compacts() {
  552. let ledger = setup_ledger().await;
  553. // Freeze and unfreeze multiple times
  554. for _ in 0..5 {
  555. ledger.freeze(&account(1)).await.unwrap();
  556. ledger.unfreeze(&account(1)).await.unwrap();
  557. }
  558. // All 11 versions preserved (1 creation + 10 mutations)
  559. let history = ledger.account_history(&account(1)).await.unwrap();
  560. assert_eq!(history.len(), 11);
  561. // Versions are monotonically increasing
  562. for (i, acc) in history.iter().enumerate() {
  563. assert_eq!(acc.version, (i + 1) as u64);
  564. }
  565. }
  566. #[tokio::test]
  567. async fn transfer_records_account_snapshots() {
  568. let ledger = setup_ledger().await;
  569. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  570. // The envelope should have account_snapshots populated by the resolve step
  571. let transfers = ledger.history(&account(1)).await.unwrap();
  572. assert_eq!(transfers.len(), 1);
  573. assert!(!transfers[0].envelope.account_snapshots().is_empty());
  574. }
  575. #[tokio::test]
  576. async fn stale_snapshot_rejected() {
  577. let ledger = setup_ledger().await;
  578. // Get current snapshot for account(1)
  579. let acc1 = ledger.get_account(&account(1)).await.unwrap();
  580. let stale_snapshot = kuatia_core::account_snapshot_id(&acc1);
  581. // Freeze account(1) -- changes its snapshot hash
  582. ledger.freeze(&account(1)).await.unwrap();
  583. // Build an envelope with the stale snapshot
  584. let envelope = EnvelopeBuilder::new()
  585. .creates(vec![
  586. NewPosting {
  587. owner: account(1),
  588. asset: usd(),
  589. value: Cent::from(100),
  590. payer: None,
  591. },
  592. NewPosting {
  593. owner: external(),
  594. asset: usd(),
  595. value: Cent::from(-100),
  596. payer: None,
  597. },
  598. ])
  599. .account_snapshots(vec![stale_snapshot])
  600. .build();
  601. let result = ledger.commit_envelope(envelope).await;
  602. assert!(result.is_err());
  603. }
  604. #[tokio::test]
  605. async fn account_hash_deterministic() {
  606. let acc = make_account(42, AccountPolicy::NoOverdraft);
  607. let h1 = kuatia_core::account_hash(&acc);
  608. let h2 = kuatia_core::account_hash(&acc);
  609. assert_eq!(h1, h2);
  610. }
  611. #[tokio::test]
  612. async fn account_hash_changes_with_version() {
  613. let mut acc = make_account(42, AccountPolicy::NoOverdraft);
  614. let h1 = kuatia_core::account_hash(&acc);
  615. acc.version = 2;
  616. acc.flags |= AccountFlags::FROZEN;
  617. let h2 = kuatia_core::account_hash(&acc);
  618. assert_ne!(h1, h2);
  619. }
  620. // ---------------------------------------------------------------------------
  621. // Overdraft via negative postings
  622. // ---------------------------------------------------------------------------
  623. #[tokio::test]
  624. async fn capped_overdraft_creates_negative_posting() {
  625. let store = InMemoryStore::new();
  626. let ledger = Arc::new(Ledger::new(store));
  627. for (id, policy) in [
  628. (
  629. 10,
  630. AccountPolicy::CappedOverdraft {
  631. floor: Cent::from(-200),
  632. },
  633. ),
  634. (2, AccountPolicy::NoOverdraft),
  635. (99, AccountPolicy::ExternalAccount),
  636. ] {
  637. ledger
  638. .store()
  639. .create_account(make_account(id, policy))
  640. .await
  641. .unwrap();
  642. }
  643. // Fund account 10 with 50, then pay 100 — overdraft covers the 50 shortfall.
  644. deposit(&ledger, account(10), usd(), Cent::from(50), external()).await;
  645. pay(&ledger, account(10), account(2), usd(), Cent::from(100)).await;
  646. assert_eq!(
  647. ledger.balance(&account(10), &usd()).await.unwrap(),
  648. Cent::from(-50)
  649. );
  650. assert_eq!(
  651. ledger.balance(&account(2), &usd()).await.unwrap(),
  652. Cent::from(100)
  653. );
  654. // A negative posting now backs the overdraft.
  655. let postings = ledger
  656. .store()
  657. .get_postings_by_account(&account(10), Some(&usd()), Some(PostingStatus::Active))
  658. .await
  659. .unwrap();
  660. assert!(postings.iter().any(|p| p.value == Cent::from(-50)));
  661. }
  662. #[tokio::test]
  663. async fn capped_overdraft_respects_floor() {
  664. let store = InMemoryStore::new();
  665. let ledger = Arc::new(Ledger::new(store));
  666. for (id, policy) in [
  667. (
  668. 10,
  669. AccountPolicy::CappedOverdraft {
  670. floor: Cent::from(-80),
  671. },
  672. ),
  673. (2, AccountPolicy::NoOverdraft),
  674. (99, AccountPolicy::ExternalAccount),
  675. ] {
  676. ledger
  677. .store()
  678. .create_account(make_account(id, policy))
  679. .await
  680. .unwrap();
  681. }
  682. // Paying 100 from an empty account would project to -100, below the -80 floor.
  683. let transfer = TransferBuilder::new()
  684. .pay(account(10), account(2), usd(), Cent::from(100))
  685. .build();
  686. assert!(ledger.commit(transfer).await.is_err());
  687. assert_eq!(
  688. ledger.balance(&account(10), &usd()).await.unwrap(),
  689. Cent::ZERO
  690. );
  691. }
  692. #[tokio::test]
  693. async fn uncapped_overdraft_allows_arbitrary_negative() {
  694. let store = InMemoryStore::new();
  695. let ledger = Arc::new(Ledger::new(store));
  696. for (id, policy) in [
  697. (10, AccountPolicy::UncappedOverdraft),
  698. (2, AccountPolicy::NoOverdraft),
  699. (99, AccountPolicy::ExternalAccount),
  700. ] {
  701. ledger
  702. .store()
  703. .create_account(make_account(id, policy))
  704. .await
  705. .unwrap();
  706. }
  707. pay(
  708. &ledger,
  709. account(10),
  710. account(2),
  711. usd(),
  712. Cent::from(1_000_000),
  713. )
  714. .await;
  715. assert_eq!(
  716. ledger.balance(&account(10), &usd()).await.unwrap(),
  717. Cent::from(-1_000_000)
  718. );
  719. }
  720. // ---------------------------------------------------------------------------
  721. // Book policy enforcement
  722. // ---------------------------------------------------------------------------
  723. #[tokio::test]
  724. async fn book_policy_rejects_disallowed_asset() {
  725. let ledger = setup_ledger().await;
  726. // Book 5 permits only EUR.
  727. let book = BookBuilder::new("eur-only")
  728. .id(BookId::new(5))
  729. .allow_asset(eur())
  730. .build();
  731. ledger.store().create_book(book).await.unwrap();
  732. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  733. // Paying USD under a EUR-only book is rejected, balance unchanged.
  734. let transfer = TransferBuilder::new()
  735. .book(BookId::new(5))
  736. .pay(account(1), account(2), usd(), Cent::from(50))
  737. .build();
  738. assert!(ledger.commit(transfer).await.is_err());
  739. assert_eq!(
  740. ledger.balance(&account(1), &usd()).await.unwrap(),
  741. Cent::from(100)
  742. );
  743. }
  744. #[tokio::test]
  745. async fn transfer_in_missing_named_book_is_rejected() {
  746. let ledger = setup_ledger().await;
  747. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  748. let transfer = TransferBuilder::new()
  749. .book(BookId::new(404))
  750. .pay(account(1), account(2), usd(), Cent::from(50))
  751. .build();
  752. assert!(ledger.commit(transfer).await.is_err());
  753. assert_eq!(
  754. ledger.balance(&account(1), &usd()).await.unwrap(),
  755. Cent::from(100)
  756. );
  757. }
  758. // ---------------------------------------------------------------------------
  759. // Content-addressed determinism
  760. // ---------------------------------------------------------------------------
  761. #[tokio::test]
  762. async fn identical_transfers_share_envelope_id() {
  763. // Two independently-built default-book transfers must hash identically.
  764. let a = TransferBuilder::new()
  765. .pay(account(1), account(2), usd(), Cent::from(10))
  766. .build();
  767. let b = TransferBuilder::new()
  768. .pay(account(1), account(2), usd(), Cent::from(10))
  769. .build();
  770. assert_eq!(a.book, b.book, "default book must be deterministic");
  771. assert_eq!(a.book, DEFAULT_BOOK);
  772. }