From 5899900544bd7ac7bcea31bf1dd13a225b82256e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 12:15:40 -0600 Subject: [PATCH] fix(id): reject same-account conversion legs --- pkgs/id/src/currency_poc/executor.rs | 26 ++++++++- pkgs/id/src/currency_poc/tests.rs | 86 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/currency_poc/executor.rs b/pkgs/id/src/currency_poc/executor.rs index 778c0a46eb..0582e8eacb 100644 --- a/pkgs/id/src/currency_poc/executor.rs +++ b/pkgs/id/src/currency_poc/executor.rs @@ -134,10 +134,30 @@ impl ConversionExecutor { } /// Pre-flight (ยง4 of #2063): every account exists on the right - /// ledger, the user can cover amount + fee, and the LP holds - /// enough destination inventory. Failing here means no intent is - /// written and nothing is submitted. + /// ledger, every transfer has distinct debit/credit accounts, the + /// user can cover amount + fee, and the LP holds enough destination + /// inventory. Failing here means no intent is written and nothing + /// is submitted. pub fn preflight(&self, request: &ConversionRequest) -> Result<()> { + if request.user_source == request.lp_source { + bail!( + "preflight: user source and LP source accounts must differ (account {})", + request.user_source + ); + } + if request.fee_amount > 0 && request.fee_account == Some(request.user_source) { + bail!( + "preflight: user source and fee accounts must differ (account {})", + request.user_source + ); + } + if request.lp_dest == request.user_dest { + bail!( + "preflight: LP destination and user destination accounts must differ (account {})", + request.lp_dest + ); + } + let mut transfer_ids = HashSet::with_capacity(request.transfer_ids.len()); for id in &request.transfer_ids { if !transfer_ids.insert(*id) { diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index f04c7f8eac..a085c2a5a6 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -320,6 +320,92 @@ fn insufficient_lp_inventory_fails_preflight() { ); } +#[test] +fn user_and_lp_source_must_be_distinct_before_persisting_intent() { + let mut executor = executor(); + // Give the aliased source enough funds so an ordinary balance + // failure cannot mask the same-account transfer defect. + let funding = funding_transfer(902, ISSUER_SRC, LP_SRC, 10_000, SRC); + assert_eq!(executor.backend.create_transfers(&[funding]), vec![Ok(())]); + + let mut aliased = request(); + aliased.user_source = LP_SRC; + + let error = executor + .prepare(&aliased) + .expect_err("T1 must reject a same-account transfer before writing an intent"); + assert!(error.to_string().contains("user source and LP source")); + assert!(executor.store.get(777).unwrap().is_none()); + assert!( + executor + .backend + .transfers_with_user_data_128(777) + .is_empty() + ); +} + +#[test] +fn fee_and_user_source_must_be_distinct_before_persisting_intent() { + let mut executor = executor(); + let mut aliased = request(); + aliased.fee_account = Some(USER_SRC); + + let error = executor + .prepare(&aliased) + .expect_err("the fee leg must not debit and credit the same account"); + assert!(error.to_string().contains("user source and fee")); + assert!(executor.store.get(777).unwrap().is_none()); + assert!( + executor + .backend + .transfers_with_user_data_128(777) + .is_empty() + ); +} + +#[test] +fn lp_and_user_destination_must_be_distinct_before_persisting_intent() { + let mut executor = executor(); + // Fund the aliased destination so the ordinary LP inventory check + // cannot mask the same-account transfer defect. + let funding = funding_transfer(901, ISSUER_DST, USER_DST, 50_000, DST); + assert_eq!(executor.backend.create_transfers(&[funding]), vec![Ok(())]); + + let mut aliased = request(); + aliased.lp_dest = USER_DST; + let error = executor + .prepare(&aliased) + .expect_err("T3 must reject a same-account transfer before writing an intent"); + assert!( + error + .to_string() + .contains("LP destination and user destination") + ); + assert!(executor.store.get(777).unwrap().is_none()); + assert!( + executor + .backend + .transfers_with_user_data_128(777) + .is_empty() + ); +} + +#[test] +fn conversion_without_fee_still_posts() { + let mut executor = executor(); + let mut no_fee = request(); + no_fee.transfer_ids = vec![604, 605]; + no_fee.fee_account = None; + no_fee.fee_amount = 0; + + let record = executor + .execute(&no_fee) + .expect("distinct accounts must still allow a conversion without a fee leg"); + assert_eq!(record.state, IntentState::Posted); + assert_eq!(record.legs.len(), 2); + assert_eq!(executor.backend.transfers_with_user_data_128(777).len(), 2); +} + #[test] fn duplicate_t1_and_fee_transfer_ids_fail_preflight_before_persisting_intent() { let mut executor = executor();