test_swap_flow.rs 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. //! Comprehensive tests for the current swap flow
  2. //!
  3. //! These tests validate the swap operation's behavior including:
  4. //! - Happy path: successful token swaps
  5. //! - Error handling: validation failures, rollback scenarios
  6. //! - Edge cases: concurrent operations, double-spending
  7. //! - State management: proof states, blinded message tracking
  8. //!
  9. //! The tests focus on the current implementation using ProofWriter and BlindedMessageWriter
  10. //! patterns to ensure proper cleanup and rollback behavior.
  11. use std::collections::HashMap;
  12. use std::sync::Arc;
  13. use cashu::amount::SplitTarget;
  14. use cashu::dhke::construct_proofs;
  15. use cashu::{CurrencyUnit, Id, PreMintSecrets, SecretKey, SpendingConditions, State, SwapRequest};
  16. use cdk::mint::Mint;
  17. use cdk::nuts::nut00::ProofsMethods;
  18. use cdk::Amount;
  19. use cdk_fake_wallet::create_fake_invoice;
  20. use cdk_integration_tests::init_pure_tests::*;
  21. /// Helper to get the active keyset ID from a mint
  22. async fn get_keyset_id(mint: &Mint) -> Id {
  23. let keys = mint.pubkeys().keysets.first().unwrap().clone();
  24. keys.verify_id()
  25. .expect("Keyset ID generation is successful");
  26. keys.id
  27. }
  28. /// Tests the complete happy path of a swap operation:
  29. /// 1. Wallet is funded with tokens
  30. /// 2. Blinded messages are added to database
  31. /// 3. Outputs are signed by mint
  32. /// 4. Input proofs are verified
  33. /// 5. Transaction is balanced
  34. /// 6. Proofs are added and marked as spent
  35. /// 7. Blind signatures are saved
  36. /// All steps should succeed and database should be in consistent state.
  37. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  38. async fn test_swap_happy_path() {
  39. setup_tracing();
  40. let mint = create_and_start_test_mint()
  41. .await
  42. .expect("Failed to create test mint");
  43. let wallet = create_test_wallet_for_mint(mint.clone())
  44. .await
  45. .expect("Failed to create test wallet");
  46. // Fund wallet with 100 sats
  47. fund_wallet(wallet.clone(), 100, None)
  48. .await
  49. .expect("Failed to fund wallet");
  50. let proofs = wallet
  51. .get_unspent_proofs()
  52. .await
  53. .expect("Could not get proofs");
  54. let keyset_id = get_keyset_id(&mint).await;
  55. // Check initial amounts after minting
  56. let total_issued = mint.total_issued().await.unwrap();
  57. let total_redeemed = mint.total_redeemed().await.unwrap();
  58. let initial_issued = total_issued
  59. .get(&keyset_id)
  60. .copied()
  61. .unwrap_or(Amount::ZERO);
  62. let initial_redeemed = total_redeemed
  63. .get(&keyset_id)
  64. .copied()
  65. .unwrap_or(Amount::ZERO);
  66. assert_eq!(
  67. initial_issued,
  68. Amount::from(100),
  69. "Should have issued 100 sats"
  70. );
  71. assert_eq!(
  72. initial_redeemed,
  73. Amount::ZERO,
  74. "Should have redeemed 0 sats initially"
  75. );
  76. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  77. // Create swap request for same amount (100 sats)
  78. let preswap = PreMintSecrets::random(
  79. keyset_id,
  80. 100.into(),
  81. &SplitTarget::default(),
  82. &fee_and_amounts,
  83. )
  84. .expect("Failed to create preswap");
  85. let swap_request = SwapRequest::new(proofs.clone(), preswap.blinded_messages());
  86. // Execute swap
  87. let swap_response = mint
  88. .process_swap_request(swap_request)
  89. .await
  90. .expect("Swap should succeed");
  91. // Verify response contains correct number of signatures
  92. assert_eq!(
  93. swap_response.signatures.len(),
  94. preswap.blinded_messages().len(),
  95. "Should receive signature for each blinded message"
  96. );
  97. // Verify input proofs are marked as spent
  98. let states = mint
  99. .localstore()
  100. .get_proofs_states(&proofs.iter().map(|p| p.y().unwrap()).collect::<Vec<_>>())
  101. .await
  102. .expect("Failed to get proof states");
  103. for state in states {
  104. assert_eq!(
  105. State::Spent,
  106. state.expect("State should be known"),
  107. "All input proofs should be marked as spent"
  108. );
  109. }
  110. // Verify blind signatures were saved
  111. let saved_signatures = mint
  112. .localstore()
  113. .get_blind_signatures(
  114. &preswap
  115. .blinded_messages()
  116. .iter()
  117. .map(|bm| bm.blinded_secret)
  118. .collect::<Vec<_>>(),
  119. )
  120. .await
  121. .expect("Failed to get blind signatures");
  122. assert_eq!(
  123. saved_signatures.len(),
  124. swap_response.signatures.len(),
  125. "All signatures should be saved"
  126. );
  127. // Check keyset amounts after swap
  128. // Swap redeems old proofs (100 sats) and issues new proofs (100 sats)
  129. let total_issued = mint.total_issued().await.unwrap();
  130. let total_redeemed = mint.total_redeemed().await.unwrap();
  131. let after_issued = total_issued
  132. .get(&keyset_id)
  133. .copied()
  134. .unwrap_or(Amount::ZERO);
  135. let after_redeemed = total_redeemed
  136. .get(&keyset_id)
  137. .copied()
  138. .unwrap_or(Amount::ZERO);
  139. assert_eq!(
  140. after_issued,
  141. Amount::from(200),
  142. "Should have issued 200 sats total (initial 100 + swap 100)"
  143. );
  144. assert_eq!(
  145. after_redeemed,
  146. Amount::from(100),
  147. "Should have redeemed 100 sats from the swap"
  148. );
  149. }
  150. /// Tests that duplicate blinded messages are rejected:
  151. /// 1. First swap with blinded messages succeeds
  152. /// 2. Second swap attempt with same blinded messages fails
  153. /// 3. BlindedMessageWriter should prevent reuse
  154. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  155. async fn test_swap_duplicate_blinded_messages() {
  156. setup_tracing();
  157. let mint = create_and_start_test_mint()
  158. .await
  159. .expect("Failed to create test mint");
  160. let wallet = create_test_wallet_for_mint(mint.clone())
  161. .await
  162. .expect("Failed to create test wallet");
  163. // Fund wallet with 200 sats (enough for two swaps)
  164. fund_wallet(wallet.clone(), 200, None)
  165. .await
  166. .expect("Failed to fund wallet");
  167. let all_proofs = wallet
  168. .get_unspent_proofs()
  169. .await
  170. .expect("Could not get proofs");
  171. // Split proofs into two sets
  172. let mid = all_proofs.len() / 2;
  173. let proofs1: Vec<_> = all_proofs.iter().take(mid).cloned().collect();
  174. let proofs2: Vec<_> = all_proofs.iter().skip(mid).cloned().collect();
  175. let keyset_id = get_keyset_id(&mint).await;
  176. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  177. // Create blinded messages for first swap
  178. let preswap = PreMintSecrets::random(
  179. keyset_id,
  180. proofs1.total_amount().unwrap(),
  181. &SplitTarget::default(),
  182. &fee_and_amounts,
  183. )
  184. .expect("Failed to create preswap");
  185. let blinded_messages = preswap.blinded_messages();
  186. // First swap should succeed
  187. let swap_request1 = SwapRequest::new(proofs1, blinded_messages.clone());
  188. mint.process_swap_request(swap_request1)
  189. .await
  190. .expect("First swap should succeed");
  191. // Second swap with SAME blinded messages should fail
  192. let swap_request2 = SwapRequest::new(proofs2, blinded_messages.clone());
  193. let result = mint.process_swap_request(swap_request2).await;
  194. assert!(
  195. result.is_err(),
  196. "Second swap with duplicate blinded messages should fail"
  197. );
  198. }
  199. /// Tests that swap correctly rejects double-spending attempts:
  200. /// 1. First swap with proofs succeeds
  201. /// 2. Second swap with same proofs fails with TokenAlreadySpent
  202. /// 3. ProofWriter should detect already-spent proofs
  203. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  204. async fn test_swap_double_spend_detection() {
  205. setup_tracing();
  206. let mint = create_and_start_test_mint()
  207. .await
  208. .expect("Failed to create test mint");
  209. let wallet = create_test_wallet_for_mint(mint.clone())
  210. .await
  211. .expect("Failed to create test wallet");
  212. // Fund wallet with 100 sats
  213. fund_wallet(wallet.clone(), 100, None)
  214. .await
  215. .expect("Failed to fund wallet");
  216. let proofs = wallet
  217. .get_unspent_proofs()
  218. .await
  219. .expect("Could not get proofs");
  220. let keyset_id = get_keyset_id(&mint).await;
  221. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  222. // First swap
  223. let preswap1 = PreMintSecrets::random(
  224. keyset_id,
  225. 100.into(),
  226. &SplitTarget::default(),
  227. &fee_and_amounts,
  228. )
  229. .expect("Failed to create preswap");
  230. let swap_request1 = SwapRequest::new(proofs.clone(), preswap1.blinded_messages());
  231. mint.process_swap_request(swap_request1)
  232. .await
  233. .expect("First swap should succeed");
  234. // Second swap with same proofs should fail
  235. let preswap2 = PreMintSecrets::random(
  236. keyset_id,
  237. 100.into(),
  238. &SplitTarget::default(),
  239. &fee_and_amounts,
  240. )
  241. .expect("Failed to create preswap");
  242. let swap_request2 = SwapRequest::new(proofs.clone(), preswap2.blinded_messages());
  243. let result = mint.process_swap_request(swap_request2).await;
  244. match result {
  245. Err(cdk::Error::TokenAlreadySpent) => {
  246. // Expected error
  247. }
  248. Err(err) => panic!("Wrong error type: {:?}", err),
  249. Ok(_) => panic!("Double spend should not succeed"),
  250. }
  251. }
  252. /// Tests that unbalanced swap requests are rejected:
  253. /// Case 1: Output amount < Input amount (trying to steal from mint)
  254. /// Case 2: Output amount > Input amount (trying to create tokens)
  255. /// Both should fail with TransactionUnbalanced error.
  256. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  257. async fn test_swap_unbalanced_transaction_detection() {
  258. setup_tracing();
  259. let mint = create_and_start_test_mint()
  260. .await
  261. .expect("Failed to create test mint");
  262. let wallet = create_test_wallet_for_mint(mint.clone())
  263. .await
  264. .expect("Failed to create test wallet");
  265. // Fund wallet with 100 sats
  266. fund_wallet(wallet.clone(), 100, None)
  267. .await
  268. .expect("Failed to fund wallet");
  269. let proofs = wallet
  270. .get_unspent_proofs()
  271. .await
  272. .expect("Could not get proofs");
  273. let keyset_id = get_keyset_id(&mint).await;
  274. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  275. // Case 1: Try to swap for LESS (95 < 100) - underpaying
  276. let preswap_less = PreMintSecrets::random(
  277. keyset_id,
  278. 95.into(),
  279. &SplitTarget::default(),
  280. &fee_and_amounts,
  281. )
  282. .expect("Failed to create preswap");
  283. let swap_request_less = SwapRequest::new(proofs.clone(), preswap_less.blinded_messages());
  284. match mint.process_swap_request(swap_request_less).await {
  285. Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  286. // Expected error
  287. }
  288. Err(err) => panic!("Wrong error type for underpay: {:?}", err),
  289. Ok(_) => panic!("Unbalanced swap (underpay) should not succeed"),
  290. }
  291. // Case 2: Try to swap for MORE (105 > 100) - overpaying/creating tokens
  292. let preswap_more = PreMintSecrets::random(
  293. keyset_id,
  294. 105.into(),
  295. &SplitTarget::default(),
  296. &fee_and_amounts,
  297. )
  298. .expect("Failed to create preswap");
  299. let swap_request_more = SwapRequest::new(proofs.clone(), preswap_more.blinded_messages());
  300. match mint.process_swap_request(swap_request_more).await {
  301. Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  302. // Expected error
  303. }
  304. Err(err) => panic!("Wrong error type for overpay: {:?}", err),
  305. Ok(_) => panic!("Unbalanced swap (overpay) should not succeed"),
  306. }
  307. }
  308. /// Tests that swap requests with empty inputs or outputs are rejected:
  309. /// Case 1: Empty outputs (inputs without outputs)
  310. /// Case 2: Empty inputs (outputs without inputs)
  311. /// Both should fail. Currently returns UnitMismatch (11010) instead of
  312. /// TransactionUnbalanced (11002) because there are no keyset IDs to determine units.
  313. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  314. async fn test_swap_empty_inputs_or_outputs() {
  315. setup_tracing();
  316. let mint = create_and_start_test_mint()
  317. .await
  318. .expect("Failed to create test mint");
  319. let wallet = create_test_wallet_for_mint(mint.clone())
  320. .await
  321. .expect("Failed to create test wallet");
  322. // Fund wallet with 100 sats
  323. fund_wallet(wallet.clone(), 100, None)
  324. .await
  325. .expect("Failed to fund wallet");
  326. let proofs = wallet
  327. .get_unspent_proofs()
  328. .await
  329. .expect("Could not get proofs");
  330. // Case 1: Swap request with inputs but empty outputs
  331. // This represents trying to destroy tokens (inputs with no outputs)
  332. let swap_request_empty_outputs = SwapRequest::new(proofs.clone(), vec![]);
  333. match mint.process_swap_request(swap_request_empty_outputs).await {
  334. Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  335. // This would be the more appropriate error
  336. }
  337. Err(err) => panic!("Wrong error type for empty outputs: {:?}", err),
  338. Ok(_) => panic!("Swap with empty outputs should not succeed"),
  339. }
  340. // Case 2: Swap request with empty inputs but with outputs
  341. // This represents trying to create tokens from nothing
  342. let keyset_id = get_keyset_id(&mint).await;
  343. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  344. let preswap = PreMintSecrets::random(
  345. keyset_id,
  346. 100.into(),
  347. &SplitTarget::default(),
  348. &fee_and_amounts,
  349. )
  350. .expect("Failed to create preswap");
  351. let swap_request_empty_inputs = SwapRequest::new(vec![], preswap.blinded_messages());
  352. match mint.process_swap_request(swap_request_empty_inputs).await {
  353. Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  354. // This would be the more appropriate error
  355. }
  356. Err(err) => panic!("Wrong error type for empty inputs: {:?}", err),
  357. Ok(_) => panic!("Swap with empty inputs should not succeed"),
  358. }
  359. }
  360. /// Tests P2PK (Pay-to-Public-Key) spending conditions:
  361. /// 1. Create proofs locked to a public key
  362. /// 2. Attempt swap without signature - should fail
  363. /// 3. Attempt swap with valid signature - should succeed
  364. /// Validates NUT-11 signature enforcement.
  365. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  366. async fn test_swap_p2pk_signature_validation() {
  367. setup_tracing();
  368. let mint = create_and_start_test_mint()
  369. .await
  370. .expect("Failed to create test mint");
  371. let wallet = create_test_wallet_for_mint(mint.clone())
  372. .await
  373. .expect("Failed to create test wallet");
  374. // Fund wallet with 100 sats
  375. fund_wallet(wallet.clone(), 100, None)
  376. .await
  377. .expect("Failed to fund wallet");
  378. let input_proofs = wallet
  379. .get_unspent_proofs()
  380. .await
  381. .expect("Could not get proofs");
  382. let keyset_id = get_keyset_id(&mint).await;
  383. let secret_key = SecretKey::generate();
  384. // Create P2PK locked outputs
  385. let spending_conditions = SpendingConditions::new_p2pk(secret_key.public_key(), None);
  386. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  387. let pre_swap = PreMintSecrets::with_conditions(
  388. keyset_id,
  389. 100.into(),
  390. &SplitTarget::default(),
  391. &spending_conditions,
  392. &fee_and_amounts,
  393. )
  394. .expect("Failed to create P2PK preswap");
  395. let swap_request = SwapRequest::new(input_proofs.clone(), pre_swap.blinded_messages());
  396. // First swap to get P2PK locked proofs
  397. let keys = mint.pubkeys().keysets.first().cloned().unwrap().keys;
  398. let post_swap = mint
  399. .process_swap_request(swap_request)
  400. .await
  401. .expect("Initial swap should succeed");
  402. // Construct proofs from swap response
  403. let mut p2pk_proofs = construct_proofs(
  404. post_swap.signatures,
  405. pre_swap.rs(),
  406. pre_swap.secrets(),
  407. &keys,
  408. )
  409. .expect("Failed to construct proofs");
  410. // Try to spend P2PK proofs WITHOUT signature - should fail
  411. let preswap_unsigned = PreMintSecrets::random(
  412. keyset_id,
  413. 100.into(),
  414. &SplitTarget::default(),
  415. &fee_and_amounts,
  416. )
  417. .expect("Failed to create preswap");
  418. let swap_request_unsigned =
  419. SwapRequest::new(p2pk_proofs.clone(), preswap_unsigned.blinded_messages());
  420. match mint.process_swap_request(swap_request_unsigned).await {
  421. Err(cdk::Error::NUT11(cdk::nuts::nut11::Error::SignaturesNotProvided)) => {
  422. // Expected error
  423. }
  424. Err(err) => panic!("Wrong error type: {:?}", err),
  425. Ok(_) => panic!("Unsigned P2PK spend should fail"),
  426. }
  427. // Sign the proofs with correct key
  428. for proof in &mut p2pk_proofs {
  429. proof
  430. .sign_p2pk(secret_key.clone())
  431. .expect("Failed to sign proof");
  432. }
  433. // Try again WITH signature - should succeed
  434. let preswap_signed = PreMintSecrets::random(
  435. keyset_id,
  436. 100.into(),
  437. &SplitTarget::default(),
  438. &fee_and_amounts,
  439. )
  440. .expect("Failed to create preswap");
  441. let swap_request_signed = SwapRequest::new(p2pk_proofs, preswap_signed.blinded_messages());
  442. mint.process_swap_request(swap_request_signed)
  443. .await
  444. .expect("Signed P2PK spend should succeed");
  445. }
  446. /// Tests rollback behavior when duplicate blinded messages are used:
  447. /// This validates that the BlindedMessageWriter prevents reuse of blinded messages.
  448. /// 1. First swap with blinded messages succeeds
  449. /// 2. Second swap with same blinded messages fails
  450. /// 3. The failure should happen early (during blinded message addition)
  451. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  452. async fn test_swap_rollback_on_duplicate_blinded_message() {
  453. setup_tracing();
  454. let mint = create_and_start_test_mint()
  455. .await
  456. .expect("Failed to create test mint");
  457. let wallet = create_test_wallet_for_mint(mint.clone())
  458. .await
  459. .expect("Failed to create test wallet");
  460. // Fund with enough for multiple swaps
  461. fund_wallet(wallet.clone(), 200, None)
  462. .await
  463. .expect("Failed to fund wallet");
  464. let all_proofs = wallet
  465. .get_unspent_proofs()
  466. .await
  467. .expect("Could not get proofs");
  468. let mid = all_proofs.len() / 2;
  469. let proofs1: Vec<_> = all_proofs.iter().take(mid).cloned().collect();
  470. let proofs2: Vec<_> = all_proofs.iter().skip(mid).cloned().collect();
  471. let keyset_id = get_keyset_id(&mint).await;
  472. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  473. // Create shared blinded messages
  474. let preswap = PreMintSecrets::random(
  475. keyset_id,
  476. proofs1.total_amount().unwrap(),
  477. &SplitTarget::default(),
  478. &fee_and_amounts,
  479. )
  480. .expect("Failed to create preswap");
  481. let blinded_messages = preswap.blinded_messages();
  482. // Extract proof2 ys before moving proofs2
  483. let proof2_ys: Vec<_> = proofs2.iter().map(|p| p.y().unwrap()).collect();
  484. // First swap succeeds
  485. let swap1 = SwapRequest::new(proofs1, blinded_messages.clone());
  486. mint.process_swap_request(swap1)
  487. .await
  488. .expect("First swap should succeed");
  489. // Second swap with duplicate blinded messages should fail early
  490. // The BlindedMessageWriter should detect duplicate and prevent the swap
  491. let swap2 = SwapRequest::new(proofs2, blinded_messages.clone());
  492. let result = mint.process_swap_request(swap2).await;
  493. assert!(
  494. result.is_err(),
  495. "Duplicate blinded messages should cause failure"
  496. );
  497. // Verify the second set of proofs are NOT marked as spent
  498. // (since the swap failed before processing them)
  499. let states = mint
  500. .localstore()
  501. .get_proofs_states(&proof2_ys)
  502. .await
  503. .expect("Failed to get proof states");
  504. for state in states {
  505. assert!(
  506. state.is_none(),
  507. "Proofs from failed swap should not be marked as spent"
  508. );
  509. }
  510. }
  511. /// Tests concurrent swap attempts with same proofs:
  512. /// Spawns 3 concurrent tasks trying to swap the same proofs.
  513. /// Only one should succeed, others should fail with TokenAlreadySpent or TokenPending.
  514. /// Validates that concurrent access is properly handled.
  515. #[tokio::test(flavor = "multi_thread", worker_threads = 3)]
  516. async fn test_swap_concurrent_double_spend_prevention() {
  517. setup_tracing();
  518. let mint = create_and_start_test_mint()
  519. .await
  520. .expect("Failed to create test mint");
  521. let wallet = create_test_wallet_for_mint(mint.clone())
  522. .await
  523. .expect("Failed to create test wallet");
  524. // Fund wallet
  525. fund_wallet(wallet.clone(), 100, None)
  526. .await
  527. .expect("Failed to fund wallet");
  528. let proofs = wallet
  529. .get_unspent_proofs()
  530. .await
  531. .expect("Could not get proofs");
  532. let keyset_id = get_keyset_id(&mint).await;
  533. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  534. // Create 3 different swap requests with SAME proofs but different outputs
  535. let preswap1 = PreMintSecrets::random(
  536. keyset_id,
  537. 100.into(),
  538. &SplitTarget::default(),
  539. &fee_and_amounts,
  540. )
  541. .expect("Failed to create preswap 1");
  542. let preswap2 = PreMintSecrets::random(
  543. keyset_id,
  544. 100.into(),
  545. &SplitTarget::default(),
  546. &fee_and_amounts,
  547. )
  548. .expect("Failed to create preswap 2");
  549. let preswap3 = PreMintSecrets::random(
  550. keyset_id,
  551. 100.into(),
  552. &SplitTarget::default(),
  553. &fee_and_amounts,
  554. )
  555. .expect("Failed to create preswap 3");
  556. let swap_request1 = SwapRequest::new(proofs.clone(), preswap1.blinded_messages());
  557. let swap_request2 = SwapRequest::new(proofs.clone(), preswap2.blinded_messages());
  558. let swap_request3 = SwapRequest::new(proofs.clone(), preswap3.blinded_messages());
  559. // Spawn concurrent tasks
  560. let mint1 = mint.clone();
  561. let mint2 = mint.clone();
  562. let mint3 = mint.clone();
  563. let task1 = tokio::spawn(async move { mint1.process_swap_request(swap_request1).await });
  564. let task2 = tokio::spawn(async move { mint2.process_swap_request(swap_request2).await });
  565. let task3 = tokio::spawn(async move { mint3.process_swap_request(swap_request3).await });
  566. // Wait for all tasks
  567. let results = tokio::try_join!(task1, task2, task3).expect("Tasks should complete");
  568. // Count successes and failures
  569. let mut success_count = 0;
  570. let mut failure_count = 0;
  571. for result in [results.0, results.1, results.2] {
  572. match result {
  573. Ok(_) => success_count += 1,
  574. Err(cdk::Error::TokenAlreadySpent) | Err(cdk::Error::TokenPending) => {
  575. failure_count += 1
  576. }
  577. Err(err) => panic!("Unexpected error: {:?}", err),
  578. }
  579. }
  580. assert_eq!(
  581. success_count, 1,
  582. "Exactly one swap should succeed in concurrent scenario"
  583. );
  584. assert_eq!(
  585. failure_count, 2,
  586. "Exactly two swaps should fail in concurrent scenario"
  587. );
  588. // Verify all proofs are marked as spent
  589. let states = mint
  590. .localstore()
  591. .get_proofs_states(&proofs.iter().map(|p| p.y().unwrap()).collect::<Vec<_>>())
  592. .await
  593. .expect("Failed to get proof states");
  594. for state in states {
  595. assert_eq!(
  596. State::Spent,
  597. state.expect("State should be known"),
  598. "All proofs should be marked as spent after concurrent attempts"
  599. );
  600. }
  601. }
  602. /// Tests swap with fees enabled:
  603. /// 1. Create mint with keyset that has fees (1 sat per proof)
  604. /// 2. Fund wallet with many small proofs
  605. /// 3. Attempt swap without paying fee - should fail
  606. /// 4. Attempt swap with correct fee deduction - should succeed
  607. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  608. async fn test_swap_with_fees() {
  609. setup_tracing();
  610. let mint = create_and_start_test_mint()
  611. .await
  612. .expect("Failed to create test mint");
  613. let wallet = create_test_wallet_for_mint(mint.clone())
  614. .await
  615. .expect("Failed to create test wallet");
  616. // Rotate to keyset with 1 sat per proof fee
  617. mint.rotate_keyset(
  618. CurrencyUnit::Sat,
  619. cdk_integration_tests::standard_keyset_amounts(32),
  620. 100,
  621. )
  622. .await
  623. .expect("Failed to rotate keyset");
  624. // Fund with 1000 sats as individual 1-sat proofs using the fee-based keyset
  625. // Wait a bit for keyset to be available
  626. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  627. fund_wallet(wallet.clone(), 1000, Some(SplitTarget::Value(Amount::ONE)))
  628. .await
  629. .expect("Failed to fund wallet");
  630. let proofs = wallet
  631. .get_unspent_proofs()
  632. .await
  633. .expect("Could not get proofs");
  634. // Take 100 proofs (100 sats total, will need to pay fee)
  635. let hundred_proofs: Vec<_> = proofs.iter().take(100).cloned().collect();
  636. // Get the keyset ID from the proofs (which will be the fee-based keyset)
  637. let keyset_id = hundred_proofs[0].keyset_id;
  638. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  639. // Try to swap for 100 outputs (same as input) - should fail due to unpaid fee
  640. let preswap_no_fee = PreMintSecrets::random(
  641. keyset_id,
  642. 100.into(),
  643. &SplitTarget::default(),
  644. &fee_and_amounts,
  645. )
  646. .expect("Failed to create preswap");
  647. let swap_no_fee = SwapRequest::new(hundred_proofs.clone(), preswap_no_fee.blinded_messages());
  648. match mint.process_swap_request(swap_no_fee).await {
  649. Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  650. // Expected - didn't pay the fee
  651. }
  652. Err(err) => panic!("Wrong error type: {:?}", err),
  653. Ok(_) => panic!("Should fail when fee not paid"),
  654. }
  655. // Calculate correct fee (1 sat per input proof in this keyset)
  656. let fee = hundred_proofs.len() as u64; // 1 sat per proof = 100 sats fee
  657. let output_amount = 100 - fee;
  658. // Swap with correct fee deduction - should succeed if output_amount > 0
  659. if output_amount > 0 {
  660. let preswap_with_fee = PreMintSecrets::random(
  661. keyset_id,
  662. output_amount.into(),
  663. &SplitTarget::default(),
  664. &fee_and_amounts,
  665. )
  666. .expect("Failed to create preswap with fee");
  667. let swap_with_fee =
  668. SwapRequest::new(hundred_proofs.clone(), preswap_with_fee.blinded_messages());
  669. mint.process_swap_request(swap_with_fee)
  670. .await
  671. .expect("Swap with correct fee should succeed");
  672. }
  673. }
  674. /// Tests melt with fees enabled and swap-before-melt optimization:
  675. /// 1. Create mint with keyset that has fees (1000 ppk = 1 sat per proof)
  676. /// 2. Fund wallet with proofs using default split (optimal denominations)
  677. /// 3. Call melt() - should automatically swap if proofs don't match exactly
  678. /// 4. Verify fee calculations are reasonable
  679. ///
  680. /// Fee calculation:
  681. /// - Initial: 4096 sats in optimal denominations
  682. /// - Melt: 1000 sats, fee_reserve = 20 sats (2%)
  683. /// - inputs_needed = 1020 sats
  684. /// - Target split for 1020: [512, 256, 128, 64, 32, 16, 8, 4] = 8 proofs
  685. /// - target_fee = 8 sats
  686. /// - inputs_total_needed = 1028 sats
  687. ///
  688. /// The wallet uses two-step selection:
  689. /// - Step 1: Try to find exact proofs for inputs_needed (no swap fee)
  690. /// - Step 2: If not exact, select proofs for inputs_total_needed and swap
  691. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  692. async fn test_melt_with_fees_swap_before_melt() {
  693. setup_tracing();
  694. let mint = create_and_start_test_mint()
  695. .await
  696. .expect("Failed to create test mint");
  697. let wallet = create_test_wallet_for_mint(mint.clone())
  698. .await
  699. .expect("Failed to create test wallet");
  700. // Rotate to keyset with 1000 ppk = 1 sat per proof fee
  701. mint.rotate_keyset(
  702. CurrencyUnit::Sat,
  703. cdk_integration_tests::standard_keyset_amounts(32),
  704. 1000, // 1 sat per proof
  705. )
  706. .await
  707. .expect("Failed to rotate keyset");
  708. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  709. // Fund with default split target to get optimal denominations
  710. // Use larger amount to ensure enough margin for swap fees
  711. let initial_amount = 4096u64;
  712. fund_wallet(wallet.clone(), initial_amount, None)
  713. .await
  714. .expect("Failed to fund wallet");
  715. let initial_balance: u64 = wallet.total_balance().await.unwrap().into();
  716. assert_eq!(initial_balance, initial_amount);
  717. let proofs = wallet.get_unspent_proofs().await.unwrap();
  718. let proof_amounts: Vec<u64> = proofs.iter().map(|p| u64::from(p.amount)).collect();
  719. tracing::info!("Proofs after funding: {:?}", proof_amounts);
  720. let proofs_total: u64 = proof_amounts.iter().sum();
  721. assert_eq!(
  722. proofs_total, initial_amount,
  723. "Total proofs should equal funded amount"
  724. );
  725. // Create melt quote for 1000 sats (1_000_000 msats)
  726. // Fake wallet: fee_reserve = max(1, amount * 2%) = 20 sats
  727. let invoice = create_fake_invoice(1_000_000, "".to_string()); // 1000 sats in msats
  728. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  729. let quote_amount: u64 = melt_quote.amount.into();
  730. let fee_reserve: u64 = melt_quote.fee_reserve.into();
  731. tracing::info!(
  732. "Melt quote: amount={}, fee_reserve={}",
  733. quote_amount,
  734. fee_reserve
  735. );
  736. let initial_proof_count = proofs.len();
  737. tracing::info!(
  738. "Initial state: {} proofs, {} sats",
  739. initial_proof_count,
  740. proofs_total
  741. );
  742. // Perform melt
  743. let melted = wallet.melt(&melt_quote.id).await.unwrap();
  744. let melt_amount: u64 = melted.amount.into();
  745. let ln_fee_paid: u64 = melted.fee_paid.into();
  746. tracing::info!(
  747. "Melt completed: amount={}, ln_fee_paid={}",
  748. melt_amount,
  749. ln_fee_paid
  750. );
  751. assert_eq!(melt_amount, quote_amount, "Melt amount should match quote");
  752. // Get final balance and calculate fees
  753. let final_balance: u64 = wallet.total_balance().await.unwrap().into();
  754. let total_spent = initial_amount - final_balance;
  755. let total_fees = total_spent - melt_amount;
  756. tracing::info!(
  757. "Balance: initial={}, final={}, total_spent={}, melt_amount={}, total_fees={}",
  758. initial_amount,
  759. final_balance,
  760. total_spent,
  761. melt_amount,
  762. total_fees
  763. );
  764. // Calculate input fees (swap + melt)
  765. let input_fees = total_fees - ln_fee_paid;
  766. tracing::info!(
  767. "Fee breakdown: total_fees={}, ln_fee={}, input_fees (swap+melt)={}",
  768. total_fees,
  769. ln_fee_paid,
  770. input_fees
  771. );
  772. // Verify input fees are reasonable
  773. // With swap-before-melt optimization, we use fewer proofs for the melt
  774. // Melt uses ~8 proofs for optimal split of 1028, so input_fee ~= 8
  775. // Swap (if any) also has fees, but the optimization minimizes total fees
  776. assert!(
  777. input_fees > 0,
  778. "Should have some input fees with fee-enabled keyset"
  779. );
  780. assert!(
  781. input_fees <= 20,
  782. "Input fees {} should be reasonable (not too high)",
  783. input_fees
  784. );
  785. // Verify we have change remaining
  786. assert!(final_balance > 0, "Should have change remaining after melt");
  787. tracing::info!(
  788. "Test passed: spent {} sats, fees {} (ln={}, input={}), remaining {}",
  789. total_spent,
  790. total_fees,
  791. ln_fee_paid,
  792. input_fees,
  793. final_balance
  794. );
  795. }
  796. /// Tests the "exact match" early return path in melt_with_metadata.
  797. /// When proofs already exactly match inputs_needed_amount, no swap is required.
  798. ///
  799. /// This tests Step 1 of the two-step selection:
  800. /// - Select proofs for inputs_needed_amount
  801. /// - If exact match, use proofs directly without swap
  802. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  803. async fn test_melt_exact_match_no_swap() {
  804. setup_tracing();
  805. let mint = create_and_start_test_mint()
  806. .await
  807. .expect("Failed to create test mint");
  808. let wallet = create_test_wallet_for_mint(mint.clone())
  809. .await
  810. .expect("Failed to create test wallet");
  811. // Use keyset WITHOUT fees to make exact match easier
  812. // (default keyset has no fees)
  813. // Fund with exactly inputs_needed_amount to trigger the exact match path
  814. // For a 1000 sat melt, fee_reserve = max(1, 1000 * 2%) = 20 sats
  815. // inputs_needed = 1000 + 20 = 1020 sats
  816. let initial_amount = 1020u64;
  817. fund_wallet(wallet.clone(), initial_amount, None)
  818. .await
  819. .expect("Failed to fund wallet");
  820. let initial_balance: u64 = wallet.total_balance().await.unwrap().into();
  821. assert_eq!(initial_balance, initial_amount);
  822. let proofs_before = wallet.get_unspent_proofs().await.unwrap();
  823. tracing::info!(
  824. "Proofs before melt: {:?}",
  825. proofs_before
  826. .iter()
  827. .map(|p| u64::from(p.amount))
  828. .collect::<Vec<_>>()
  829. );
  830. // Create melt quote for 1000 sats
  831. // fee_reserve = max(1, 1000 * 2%) = 20 sats
  832. // inputs_needed = 1000 + 20 = 1020 sats = our exact balance
  833. let invoice = create_fake_invoice(1_000_000, "".to_string());
  834. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  835. let quote_amount: u64 = melt_quote.amount.into();
  836. let fee_reserve: u64 = melt_quote.fee_reserve.into();
  837. let inputs_needed = quote_amount + fee_reserve;
  838. tracing::info!(
  839. "Melt quote: amount={}, fee_reserve={}, inputs_needed={}",
  840. quote_amount,
  841. fee_reserve,
  842. inputs_needed
  843. );
  844. // Perform melt
  845. let melted = wallet.melt(&melt_quote.id).await.unwrap();
  846. let melt_amount: u64 = melted.amount.into();
  847. let ln_fee_paid: u64 = melted.fee_paid.into();
  848. tracing::info!(
  849. "Melt completed: amount={}, ln_fee_paid={}",
  850. melt_amount,
  851. ln_fee_paid
  852. );
  853. assert_eq!(melt_amount, quote_amount, "Melt amount should match quote");
  854. // Get final balance
  855. let final_balance: u64 = wallet.total_balance().await.unwrap().into();
  856. let total_spent = initial_amount - final_balance;
  857. let total_fees = total_spent - melt_amount;
  858. tracing::info!(
  859. "Balance: initial={}, final={}, total_spent={}, total_fees={}",
  860. initial_amount,
  861. final_balance,
  862. total_spent,
  863. total_fees
  864. );
  865. // With no keyset fees and no swap needed, total fees should just be ln_fee
  866. // (no input fees since default keyset has 0 ppk)
  867. assert_eq!(
  868. total_fees, ln_fee_paid,
  869. "Total fees should equal LN fee (no swap or input fees with 0 ppk keyset)"
  870. );
  871. tracing::info!("Test passed: exact match path used, no swap needed");
  872. }
  873. /// Tests melt with small amounts where swap margin is too tight.
  874. /// When fees are high relative to the melt amount, the swap-before-melt
  875. /// optimization may not have enough margin to cover both input and output fees.
  876. /// In this case, the wallet should fall back to using proofs directly.
  877. ///
  878. /// Scenario:
  879. /// - Fund with 8 sats
  880. /// - Melt 5 sats (with 2% fee_reserve = 1 sat min, so inputs_needed = 6)
  881. /// - With 1 sat per proof fee, the swap margin becomes too tight
  882. /// - Should still succeed by falling back to direct melt
  883. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  884. async fn test_melt_small_amount_tight_margin() {
  885. setup_tracing();
  886. let mint = create_and_start_test_mint()
  887. .await
  888. .expect("Failed to create test mint");
  889. let wallet = create_test_wallet_for_mint(mint.clone())
  890. .await
  891. .expect("Failed to create test wallet");
  892. // Rotate to keyset with 1000 ppk = 1 sat per proof fee
  893. mint.rotate_keyset(
  894. CurrencyUnit::Sat,
  895. cdk_integration_tests::standard_keyset_amounts(32),
  896. 1000,
  897. )
  898. .await
  899. .expect("Failed to rotate keyset");
  900. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  901. // Fund with enough to cover melt + fees, but amounts that will trigger swap
  902. // 32 sats gives us enough margin even with 1 sat/proof fees
  903. let initial_amount = 32;
  904. fund_wallet(wallet.clone(), initial_amount, None)
  905. .await
  906. .expect("Failed to fund wallet");
  907. let initial_balance: u64 = wallet.total_balance().await.unwrap().into();
  908. assert_eq!(initial_balance, initial_amount);
  909. let proofs = wallet.get_unspent_proofs().await.unwrap();
  910. tracing::info!(
  911. "Proofs after funding: {:?}",
  912. proofs
  913. .iter()
  914. .map(|p| u64::from(p.amount))
  915. .collect::<Vec<_>>()
  916. );
  917. // Create melt quote for 5 sats
  918. // fee_reserve = max(1, 5 * 2%) = 1 sat
  919. // inputs_needed = 5 + 1 = 6 sats
  920. let invoice = create_fake_invoice(5_000, "".to_string()); // 5 sats in msats
  921. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  922. let quote_amount: u64 = melt_quote.amount.into();
  923. let fee_reserve: u64 = melt_quote.fee_reserve.into();
  924. tracing::info!(
  925. "Melt quote: amount={}, fee_reserve={}, inputs_needed={}",
  926. quote_amount,
  927. fee_reserve,
  928. quote_amount + fee_reserve
  929. );
  930. // This should succeed even with tight margins
  931. let melted = wallet
  932. .melt(&melt_quote.id)
  933. .await
  934. .expect("Melt should succeed even with tight swap margin");
  935. let melt_amount: u64 = melted.amount.into();
  936. assert_eq!(melt_amount, quote_amount, "Melt amount should match quote");
  937. let final_balance: u64 = wallet.total_balance().await.unwrap().into();
  938. tracing::info!(
  939. "Melt completed: amount={}, fee_paid={}, final_balance={}",
  940. melted.amount,
  941. melted.fee_paid,
  942. final_balance
  943. );
  944. // Verify balance decreased appropriately
  945. assert!(
  946. final_balance < initial_balance,
  947. "Balance should decrease after melt"
  948. );
  949. }
  950. /// Tests melt where swap proofs barely cover swap_amount + input_fee.
  951. ///
  952. /// This is a regression test for a bug where the swap-before-melt was called
  953. /// with include_fees=true, causing it to try to add output fees on top of
  954. /// swap_amount + input_fee. When proofs_to_swap had just barely enough value,
  955. /// this caused InsufficientFunds error.
  956. ///
  957. /// Scenario (from the bug):
  958. /// - Balance: proofs like [4, 2, 1, 1] = 8 sats
  959. /// - Melt: 5 sats + 1 fee_reserve = 6 inputs_needed
  960. /// - target_fee = 1 (for optimal output split)
  961. /// - inputs_total_needed = 7
  962. /// - proofs_to_send = [4, 2] = 6, proofs_to_swap = [1, 1] = 2
  963. /// - swap_amount = 1 sat (7 - 6)
  964. /// - swap input_fee = 1 sat (2 proofs)
  965. /// - Before fix: include_fees=true tried to add output fee, causing failure
  966. /// - After fix: include_fees=false, swap succeeds
  967. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  968. async fn test_melt_swap_tight_margin_regression() {
  969. setup_tracing();
  970. let mint = create_and_start_test_mint()
  971. .await
  972. .expect("Failed to create test mint");
  973. let wallet = create_test_wallet_for_mint(mint.clone())
  974. .await
  975. .expect("Failed to create test wallet");
  976. // Rotate to keyset with 250 ppk = 0.25 sat per proof fee (same as original bug scenario)
  977. // This means 4 proofs = 1 sat fee
  978. mint.rotate_keyset(
  979. CurrencyUnit::Sat,
  980. cdk_integration_tests::standard_keyset_amounts(32),
  981. 250,
  982. )
  983. .await
  984. .expect("Failed to rotate keyset");
  985. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  986. // Fund with 100 sats using default split to get optimal denominations
  987. // This should give us proofs like [64, 32, 4] or similar power-of-2 split
  988. let initial_amount = 100;
  989. fund_wallet(wallet.clone(), initial_amount, None)
  990. .await
  991. .expect("Failed to fund wallet");
  992. let initial_balance: u64 = wallet.total_balance().await.unwrap().into();
  993. assert_eq!(initial_balance, initial_amount);
  994. let proofs = wallet.get_unspent_proofs().await.unwrap();
  995. let proof_amounts: Vec<u64> = proofs.iter().map(|p| u64::from(p.amount)).collect();
  996. tracing::info!("Proofs after funding: {:?}", proof_amounts);
  997. // Create melt quote for 5 sats (5000 msats)
  998. // fee_reserve = max(1, 5 * 2%) = 1 sat
  999. // inputs_needed = 5 + 1 = 6 sats
  1000. // The optimal split for 6 sats is [4, 2] (2 proofs)
  1001. // target_fee = 1 sat (2 proofs * 0.25, rounded up)
  1002. // inputs_total_needed = 7 sats
  1003. //
  1004. // If we don't have exact [4, 2] proofs, we'll need to swap.
  1005. // The swap path is what triggered the original bug when proofs_to_swap
  1006. // had tight margins and include_fees=true was incorrectly used.
  1007. let invoice = create_fake_invoice(5_000, "".to_string());
  1008. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  1009. let quote_amount: u64 = melt_quote.amount.into();
  1010. let fee_reserve: u64 = melt_quote.fee_reserve.into();
  1011. tracing::info!(
  1012. "Melt quote: amount={}, fee_reserve={}, inputs_needed={}",
  1013. quote_amount,
  1014. fee_reserve,
  1015. quote_amount + fee_reserve
  1016. );
  1017. // This is the key test: melt should succeed even when swap is needed
  1018. // Before the fix, include_fees=true in swap caused InsufficientFunds
  1019. // After the fix, include_fees=false allows the swap to succeed
  1020. let melted = wallet
  1021. .melt(&melt_quote.id)
  1022. .await
  1023. .expect("Melt should succeed with swap-before-melt (regression test)");
  1024. let melt_amount: u64 = melted.amount.into();
  1025. assert_eq!(melt_amount, quote_amount, "Melt amount should match quote");
  1026. let final_balance: u64 = wallet.total_balance().await.unwrap().into();
  1027. tracing::info!(
  1028. "Melt completed: amount={}, fee_paid={}, final_balance={}",
  1029. melted.amount,
  1030. melted.fee_paid,
  1031. final_balance
  1032. );
  1033. // Should have change remaining
  1034. assert!(
  1035. final_balance < initial_balance,
  1036. "Balance should decrease after melt"
  1037. );
  1038. assert!(final_balance > 0, "Should have change remaining");
  1039. }
  1040. /// Tests that swap correctly handles amount overflow:
  1041. /// Attempts to create outputs that would overflow u64 when summed.
  1042. /// This should be rejected before any database operations occur.
  1043. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  1044. async fn test_swap_amount_overflow_protection() {
  1045. setup_tracing();
  1046. let mint = create_and_start_test_mint()
  1047. .await
  1048. .expect("Failed to create test mint");
  1049. let wallet = create_test_wallet_for_mint(mint.clone())
  1050. .await
  1051. .expect("Failed to create test wallet");
  1052. // Fund wallet
  1053. fund_wallet(wallet.clone(), 100, None)
  1054. .await
  1055. .expect("Failed to fund wallet");
  1056. let proofs = wallet
  1057. .get_unspent_proofs()
  1058. .await
  1059. .expect("Could not get proofs");
  1060. let keyset_id = get_keyset_id(&mint).await;
  1061. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  1062. // Try to create outputs that would overflow
  1063. // 2^63 + 2^63 + small amount would overflow u64
  1064. let large_amount = 2_u64.pow(63);
  1065. let pre_mint1 = PreMintSecrets::random(
  1066. keyset_id,
  1067. large_amount.into(),
  1068. &SplitTarget::default(),
  1069. &fee_and_amounts,
  1070. )
  1071. .expect("Failed to create pre_mint1");
  1072. let pre_mint2 = PreMintSecrets::random(
  1073. keyset_id,
  1074. large_amount.into(),
  1075. &SplitTarget::default(),
  1076. &fee_and_amounts,
  1077. )
  1078. .expect("Failed to create pre_mint2");
  1079. let mut combined_pre_mint = PreMintSecrets::random(
  1080. keyset_id,
  1081. 1.into(),
  1082. &SplitTarget::default(),
  1083. &fee_and_amounts,
  1084. )
  1085. .expect("Failed to create combined_pre_mint");
  1086. combined_pre_mint.combine(pre_mint1);
  1087. combined_pre_mint.combine(pre_mint2);
  1088. let swap_request = SwapRequest::new(proofs, combined_pre_mint.blinded_messages());
  1089. // Should fail with overflow/amount error
  1090. match mint.process_swap_request(swap_request).await {
  1091. Err(cdk::Error::NUT03(cdk::nuts::nut03::Error::Amount(_)))
  1092. | Err(cdk::Error::AmountOverflow)
  1093. | Err(cdk::Error::AmountError(_))
  1094. | Err(cdk::Error::TransactionUnbalanced(_, _, _)) => {
  1095. // Any of these errors are acceptable for overflow
  1096. }
  1097. Err(err) => panic!("Unexpected error type: {:?}", err),
  1098. Ok(_) => panic!("Overflow swap should not succeed"),
  1099. }
  1100. }
  1101. /// Tests swap state transitions through pubsub notifications:
  1102. /// 1. Subscribe to proof state changes
  1103. /// 2. Execute swap
  1104. /// 3. Verify Pending then Spent state transitions are received
  1105. /// Validates NUT-17 notification behavior.
  1106. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  1107. async fn test_swap_state_transition_notifications() {
  1108. setup_tracing();
  1109. let mint = create_and_start_test_mint()
  1110. .await
  1111. .expect("Failed to create test mint");
  1112. let wallet = create_test_wallet_for_mint(mint.clone())
  1113. .await
  1114. .expect("Failed to create test wallet");
  1115. // Fund wallet
  1116. fund_wallet(wallet.clone(), 100, None)
  1117. .await
  1118. .expect("Failed to fund wallet");
  1119. let proofs = wallet
  1120. .get_unspent_proofs()
  1121. .await
  1122. .expect("Could not get proofs");
  1123. let keyset_id = get_keyset_id(&mint).await;
  1124. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  1125. let preswap = PreMintSecrets::random(
  1126. keyset_id,
  1127. 100.into(),
  1128. &SplitTarget::default(),
  1129. &fee_and_amounts,
  1130. )
  1131. .expect("Failed to create preswap");
  1132. let swap_request = SwapRequest::new(proofs.clone(), preswap.blinded_messages());
  1133. // Subscribe to proof state changes
  1134. let proof_ys: Vec<String> = proofs.iter().map(|p| p.y().unwrap().to_string()).collect();
  1135. let mut listener = mint
  1136. .pubsub_manager()
  1137. .subscribe(cdk::subscription::Params {
  1138. kind: cdk::nuts::nut17::Kind::ProofState,
  1139. filters: proof_ys.clone(),
  1140. id: Arc::new("test_swap_notifications".into()),
  1141. })
  1142. .expect("Should subscribe successfully");
  1143. // Execute swap
  1144. mint.process_swap_request(swap_request)
  1145. .await
  1146. .expect("Swap should succeed");
  1147. // Give pubsub time to deliver messages
  1148. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  1149. // Collect all state transition notifications
  1150. let mut state_transitions: HashMap<String, Vec<State>> = HashMap::new();
  1151. while let Some(msg) = listener.try_recv() {
  1152. match msg.into_inner() {
  1153. cashu::NotificationPayload::ProofState(cashu::ProofState { y, state, .. }) => {
  1154. state_transitions
  1155. .entry(y.to_string())
  1156. .or_default()
  1157. .push(state);
  1158. }
  1159. _ => panic!("Unexpected notification type"),
  1160. }
  1161. }
  1162. // Verify each proof went through Pending -> Spent transition
  1163. for y in proof_ys {
  1164. let transitions = state_transitions
  1165. .get(&y)
  1166. .expect("Should have transitions for proof");
  1167. assert_eq!(
  1168. transitions,
  1169. &vec![State::Pending, State::Spent],
  1170. "Proof should transition from Pending to Spent"
  1171. );
  1172. }
  1173. }
  1174. /// Tests that swap fails gracefully when proof states cannot be updated:
  1175. /// This would test the rollback path where proofs are added but state update fails.
  1176. /// In the current implementation, this should trigger rollback of both proofs and blinded messages.
  1177. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  1178. async fn test_swap_proof_state_consistency() {
  1179. setup_tracing();
  1180. let mint = create_and_start_test_mint()
  1181. .await
  1182. .expect("Failed to create test mint");
  1183. let wallet = create_test_wallet_for_mint(mint.clone())
  1184. .await
  1185. .expect("Failed to create test wallet");
  1186. // Fund wallet
  1187. fund_wallet(wallet.clone(), 100, None)
  1188. .await
  1189. .expect("Failed to fund wallet");
  1190. let proofs = wallet
  1191. .get_unspent_proofs()
  1192. .await
  1193. .expect("Could not get proofs");
  1194. let keyset_id = get_keyset_id(&mint).await;
  1195. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  1196. // Execute successful swap
  1197. let preswap = PreMintSecrets::random(
  1198. keyset_id,
  1199. 100.into(),
  1200. &SplitTarget::default(),
  1201. &fee_and_amounts,
  1202. )
  1203. .expect("Failed to create preswap");
  1204. let swap_request = SwapRequest::new(proofs.clone(), preswap.blinded_messages());
  1205. mint.process_swap_request(swap_request)
  1206. .await
  1207. .expect("Swap should succeed");
  1208. // Verify all proofs have consistent state (Spent)
  1209. let proof_ys: Vec<_> = proofs.iter().map(|p| p.y().unwrap()).collect();
  1210. let states = mint
  1211. .localstore()
  1212. .get_proofs_states(&proof_ys)
  1213. .await
  1214. .expect("Failed to get proof states");
  1215. // All states should be Some(Spent) - none should be None or Pending
  1216. for (i, state) in states.iter().enumerate() {
  1217. match state {
  1218. Some(State::Spent) => {
  1219. // Expected state
  1220. }
  1221. Some(other_state) => {
  1222. panic!("Proof {} in unexpected state: {:?}", i, other_state)
  1223. }
  1224. None => {
  1225. panic!("Proof {} has no state (should be Spent)", i)
  1226. }
  1227. }
  1228. }
  1229. }
  1230. /// Tests that wallet correctly increments keyset counters when receiving proofs
  1231. /// from multiple keysets and then performing operations with them.
  1232. ///
  1233. /// This test validates:
  1234. /// 1. Wallet can receive proofs from multiple different keysets
  1235. /// 2. Counter is correctly incremented for the target keyset during swap
  1236. /// 3. Database maintains separate counters for each keyset
  1237. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  1238. async fn test_wallet_multi_keyset_counter_updates() {
  1239. setup_tracing();
  1240. let mint = create_and_start_test_mint()
  1241. .await
  1242. .expect("Failed to create test mint");
  1243. let wallet = create_test_wallet_for_mint(mint.clone())
  1244. .await
  1245. .expect("Failed to create test wallet");
  1246. // Fund wallet with initial 100 sats using first keyset
  1247. fund_wallet(wallet.clone(), 100, None)
  1248. .await
  1249. .expect("Failed to fund wallet");
  1250. let first_keyset_id = get_keyset_id(&mint).await;
  1251. // Rotate to a second keyset
  1252. mint.rotate_keyset(
  1253. CurrencyUnit::Sat,
  1254. cdk_integration_tests::standard_keyset_amounts(32),
  1255. 0,
  1256. )
  1257. .await
  1258. .expect("Failed to rotate keyset");
  1259. // Wait for keyset rotation to propagate
  1260. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  1261. // Refresh wallet keysets to know about the new keyset
  1262. wallet
  1263. .refresh_keysets()
  1264. .await
  1265. .expect("Failed to refresh wallet keysets");
  1266. // Fund wallet again with 100 sats using second keyset
  1267. fund_wallet(wallet.clone(), 100, None)
  1268. .await
  1269. .expect("Failed to fund wallet with second keyset");
  1270. let second_keyset_id = mint
  1271. .pubkeys()
  1272. .keysets
  1273. .iter()
  1274. .find(|k| k.id != first_keyset_id)
  1275. .expect("Should have second keyset")
  1276. .id;
  1277. // Verify we now have proofs from two different keysets
  1278. let all_proofs = wallet
  1279. .get_unspent_proofs()
  1280. .await
  1281. .expect("Could not get proofs");
  1282. let keysets_in_use: std::collections::HashSet<_> =
  1283. all_proofs.iter().map(|p| p.keyset_id).collect();
  1284. assert_eq!(
  1285. keysets_in_use.len(),
  1286. 2,
  1287. "Should have proofs from 2 different keysets"
  1288. );
  1289. assert!(
  1290. keysets_in_use.contains(&first_keyset_id),
  1291. "Should have proofs from first keyset"
  1292. );
  1293. assert!(
  1294. keysets_in_use.contains(&second_keyset_id),
  1295. "Should have proofs from second keyset"
  1296. );
  1297. // Get initial total issued and redeemed for both keysets before swap
  1298. let total_issued_before = mint.total_issued().await.unwrap();
  1299. let total_redeemed_before = mint.total_redeemed().await.unwrap();
  1300. let first_keyset_issued_before = total_issued_before
  1301. .get(&first_keyset_id)
  1302. .copied()
  1303. .unwrap_or(Amount::ZERO);
  1304. let first_keyset_redeemed_before = total_redeemed_before
  1305. .get(&first_keyset_id)
  1306. .copied()
  1307. .unwrap_or(Amount::ZERO);
  1308. let second_keyset_issued_before = total_issued_before
  1309. .get(&second_keyset_id)
  1310. .copied()
  1311. .unwrap_or(Amount::ZERO);
  1312. let second_keyset_redeemed_before = total_redeemed_before
  1313. .get(&second_keyset_id)
  1314. .copied()
  1315. .unwrap_or(Amount::ZERO);
  1316. tracing::info!(
  1317. "Before swap - First keyset: issued={}, redeemed={}",
  1318. first_keyset_issued_before,
  1319. first_keyset_redeemed_before
  1320. );
  1321. tracing::info!(
  1322. "Before swap - Second keyset: issued={}, redeemed={}",
  1323. second_keyset_issued_before,
  1324. second_keyset_redeemed_before
  1325. );
  1326. // Both keysets should have issued 100 sats
  1327. assert_eq!(
  1328. first_keyset_issued_before,
  1329. Amount::from(100),
  1330. "First keyset should have issued 100 sats"
  1331. );
  1332. assert_eq!(
  1333. second_keyset_issued_before,
  1334. Amount::from(100),
  1335. "Second keyset should have issued 100 sats"
  1336. );
  1337. // Neither should have redeemed anything yet
  1338. assert_eq!(
  1339. first_keyset_redeemed_before,
  1340. Amount::ZERO,
  1341. "First keyset should have redeemed 0 sats before swap"
  1342. );
  1343. assert_eq!(
  1344. second_keyset_redeemed_before,
  1345. Amount::ZERO,
  1346. "Second keyset should have redeemed 0 sats before swap"
  1347. );
  1348. // Now perform a swap with all proofs - this should only increment the counter
  1349. // for the active (second) keyset, not for the first keyset
  1350. let fee_and_amounts = (0, ((0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>())).into();
  1351. let total_amount = all_proofs.total_amount().expect("Should get total amount");
  1352. // Create swap using the active (second) keyset
  1353. let preswap = PreMintSecrets::random(
  1354. second_keyset_id,
  1355. total_amount,
  1356. &SplitTarget::default(),
  1357. &fee_and_amounts,
  1358. )
  1359. .expect("Failed to create preswap");
  1360. let swap_request = SwapRequest::new(all_proofs.clone(), preswap.blinded_messages());
  1361. // Execute the swap
  1362. let swap_response = mint
  1363. .process_swap_request(swap_request)
  1364. .await
  1365. .expect("Swap should succeed");
  1366. // Verify response
  1367. assert_eq!(
  1368. swap_response.signatures.len(),
  1369. preswap.blinded_messages().len(),
  1370. "Should receive signature for each blinded message"
  1371. );
  1372. // All the new proofs should be from the second (active) keyset
  1373. let keys = mint
  1374. .pubkeys()
  1375. .keysets
  1376. .iter()
  1377. .find(|k| k.id == second_keyset_id)
  1378. .expect("Should find second keyset")
  1379. .keys
  1380. .clone();
  1381. let new_proofs = construct_proofs(
  1382. swap_response.signatures,
  1383. preswap.rs(),
  1384. preswap.secrets(),
  1385. &keys,
  1386. )
  1387. .expect("Failed to construct proofs");
  1388. // Verify all new proofs use the second keyset
  1389. for proof in &new_proofs {
  1390. assert_eq!(
  1391. proof.keyset_id, second_keyset_id,
  1392. "All new proofs should use the active (second) keyset"
  1393. );
  1394. }
  1395. // Verify total issued and redeemed after swap
  1396. let total_issued_after = mint.total_issued().await.unwrap();
  1397. let total_redeemed_after = mint.total_redeemed().await.unwrap();
  1398. let first_keyset_issued_after = total_issued_after
  1399. .get(&first_keyset_id)
  1400. .copied()
  1401. .unwrap_or(Amount::ZERO);
  1402. let first_keyset_redeemed_after = total_redeemed_after
  1403. .get(&first_keyset_id)
  1404. .copied()
  1405. .unwrap_or(Amount::ZERO);
  1406. let second_keyset_issued_after = total_issued_after
  1407. .get(&second_keyset_id)
  1408. .copied()
  1409. .unwrap_or(Amount::ZERO);
  1410. let second_keyset_redeemed_after = total_redeemed_after
  1411. .get(&second_keyset_id)
  1412. .copied()
  1413. .unwrap_or(Amount::ZERO);
  1414. tracing::info!(
  1415. "After swap - First keyset: issued={}, redeemed={}",
  1416. first_keyset_issued_after,
  1417. first_keyset_redeemed_after
  1418. );
  1419. tracing::info!(
  1420. "After swap - Second keyset: issued={}, redeemed={}",
  1421. second_keyset_issued_after,
  1422. second_keyset_redeemed_after
  1423. );
  1424. // After swap:
  1425. // - First keyset: issued stays 100, redeemed increases by 100 (all its proofs were spent in swap)
  1426. // - Second keyset: issued increases by 200 (original 100 + new 100 from swap output),
  1427. // redeemed increases by 100 (its proofs from first funding were spent)
  1428. assert_eq!(
  1429. first_keyset_issued_after,
  1430. Amount::from(100),
  1431. "First keyset issued should stay 100 sats (no new issuance)"
  1432. );
  1433. assert_eq!(
  1434. first_keyset_redeemed_after,
  1435. Amount::from(100),
  1436. "First keyset should have redeemed 100 sats (all its proofs spent in swap)"
  1437. );
  1438. assert_eq!(
  1439. second_keyset_issued_after,
  1440. Amount::from(300),
  1441. "Second keyset should have issued 300 sats total (100 initial + 100 the second funding + 100 from swap output from the old keyset)"
  1442. );
  1443. assert_eq!(
  1444. second_keyset_redeemed_after,
  1445. Amount::from(100),
  1446. "Second keyset should have redeemed 100 sats (its proofs from initial funding spent in swap)"
  1447. );
  1448. // The test verifies that:
  1449. // 1. We can have proofs from multiple keysets in a wallet
  1450. // 2. Swap operation processes inputs from any keyset but creates outputs using active keyset
  1451. // 3. The keyset_counter table correctly handles counters for different keysets independently
  1452. // 4. The database upsert logic in increment_keyset_counter works for multiple keysets
  1453. // 5. Total issued and redeemed are tracked correctly per keyset during multi-keyset swaps
  1454. }