Skip to content

Commit 72dff95

Browse files
authored
Merge pull request cashubtc#653 from ok300/ok300-fix-update-mint-url
Wallet: fix `update_mint_url`
1 parent 158e321 commit 72dff95

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

crates/cdk-integration-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ serde.workspace = true
3636
serde_json.workspace = true
3737
# ln-regtest-rs = { path = "../../../../ln-regtest-rs" }
3838
ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "ed24716" }
39-
lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
39+
lightning-invoice.workspace = true
4040
tracing.workspace = true
4141
tracing-subscriber.workspace = true
4242
tokio-tungstenite.workspace = true

crates/cdk-integration-tests/src/init_pure_tests.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt::{Debug, Formatter};
33
use std::str::FromStr;
44
use std::sync::Arc;
55

6+
use anyhow::{anyhow, Result};
67
use async_trait::async_trait;
78
use bip39::Mnemonic;
89
use cdk::amount::SplitTarget;
@@ -21,7 +22,7 @@ use cdk::wallet::client::MintConnector;
2122
use cdk::wallet::Wallet;
2223
use cdk::{Amount, Error, Mint};
2324
use cdk_fake_wallet::FakeWallet;
24-
use tokio::sync::Notify;
25+
use tokio::sync::{Mutex, Notify};
2526
use tracing_subscriber::EnvFilter;
2627
use uuid::Uuid;
2728

@@ -142,7 +143,7 @@ impl MintConnector for DirectMintConnection {
142143
}
143144
}
144145

145-
pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
146+
pub fn setup_tracing() {
146147
let default_filter = "debug";
147148

148149
let sqlx_filter = "sqlx=warn";
@@ -153,8 +154,14 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
153154
default_filter, sqlx_filter, hyper_filter
154155
));
155156

156-
tracing_subscriber::fmt().with_env_filter(env_filter).init();
157+
// Ok if successful, Err if already initialized
158+
// Allows us to setup tracing at the start of several parallel tests
159+
let _ = tracing_subscriber::fmt()
160+
.with_env_filter(env_filter)
161+
.try_init();
162+
}
157163

164+
pub async fn create_and_start_test_mint() -> Result<Arc<Mint>> {
158165
let mut mint_builder = MintBuilder::new();
159166

160167
let database = cdk_sqlite::mint::memory::empty().await?;
@@ -188,6 +195,7 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
188195
mint_builder = mint_builder
189196
.with_name("pure test mint".to_string())
190197
.with_description("pure test mint".to_string())
198+
.with_urls(vec!["https://aaa".to_string()])
191199
.with_seed(mnemonic.to_seed_normalized("").to_vec());
192200

193201
localstore
@@ -210,23 +218,41 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
210218
Ok(mint_arc)
211219
}
212220

213-
pub async fn create_test_wallet_for_mint(mint: Arc<Mint>) -> anyhow::Result<Arc<Wallet>> {
214-
let connector = DirectMintConnection::new(mint);
221+
async fn create_test_wallet_for_mint(mint: Arc<Mint>) -> Result<Wallet> {
222+
let connector = DirectMintConnection::new(mint.clone());
223+
224+
let mint_info = mint.mint_info().await?;
225+
let mint_url = mint_info
226+
.urls
227+
.as_ref()
228+
.ok_or(anyhow!("Test mint URLs list is unset"))?
229+
.first()
230+
.ok_or(anyhow!("Test mint has empty URLs list"))?;
215231

216232
let seed = Mnemonic::generate(12)?.to_seed_normalized("");
217-
let mint_url = "http://aa".to_string();
218233
let unit = CurrencyUnit::Sat;
219234
let localstore = cdk_sqlite::wallet::memory::empty().await?;
220-
let mut wallet = Wallet::new(&mint_url, unit, Arc::new(localstore), &seed, None)?;
235+
let mut wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?;
221236

222237
wallet.set_client(connector);
223238

224-
Ok(Arc::new(wallet))
239+
Ok(wallet)
240+
}
241+
242+
pub async fn create_test_wallet_arc_for_mint(mint: Arc<Mint>) -> Result<Arc<Wallet>> {
243+
create_test_wallet_for_mint(mint).await.map(Arc::new)
244+
}
245+
246+
pub async fn create_test_wallet_arc_mut_for_mint(mint: Arc<Mint>) -> Result<Arc<Mutex<Wallet>>> {
247+
create_test_wallet_for_mint(mint)
248+
.await
249+
.map(Mutex::new)
250+
.map(Arc::new)
225251
}
226252

227253
/// Creates a mint quote for the given amount and checks its state in a loop. Returns when
228254
/// amount is minted.
229-
pub async fn fund_wallet(wallet: Arc<Wallet>, amount: u64) -> anyhow::Result<Amount> {
255+
pub async fn fund_wallet(wallet: Arc<Wallet>, amount: u64) -> Result<Amount> {
230256
let desired_amount = Amount::from(amount);
231257
let quote = wallet.mint_quote(desired_amount, None).await?;
232258

crates/cdk-integration-tests/tests/integration_tests_pure.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ use cdk::amount::SplitTarget;
44
use cdk::nuts::nut00::ProofsMethods;
55
use cdk::wallet::SendKind;
66
use cdk::Amount;
7-
use cdk_integration_tests::init_pure_tests::{
8-
create_and_start_test_mint, create_test_wallet_for_mint, fund_wallet,
9-
};
7+
use cdk_integration_tests::init_pure_tests::*;
108

119
#[tokio::test]
1210
async fn test_swap_to_send() -> anyhow::Result<()> {
11+
setup_tracing();
1312
let mint_bob = create_and_start_test_mint().await?;
14-
let wallet_alice = create_test_wallet_for_mint(mint_bob.clone()).await?;
13+
let wallet_alice = create_test_wallet_arc_for_mint(mint_bob.clone()).await?;
1514

1615
// Alice gets 64 sats
1716
fund_wallet(wallet_alice.clone(), 64).await?;
@@ -33,7 +32,7 @@ async fn test_swap_to_send() -> anyhow::Result<()> {
3332
assert_eq!(Amount::from(24), wallet_alice.total_balance().await?);
3433

3534
// Alice sends cashu, Carol receives
36-
let wallet_carol = create_test_wallet_for_mint(mint_bob.clone()).await?;
35+
let wallet_carol = create_test_wallet_arc_for_mint(mint_bob.clone()).await?;
3736
let received_amount = wallet_carol
3837
.receive_proofs(token.proofs(), SplitTarget::None, &[], &[])
3938
.await?;
@@ -43,3 +42,44 @@ async fn test_swap_to_send() -> anyhow::Result<()> {
4342

4443
Ok(())
4544
}
45+
46+
/// Pure integration tests related to NUT-06 (Mint Information)
47+
mod nut06 {
48+
use std::str::FromStr;
49+
use std::sync::Arc;
50+
51+
use anyhow::Result;
52+
use cashu::mint_url::MintUrl;
53+
use cashu::Amount;
54+
use cdk_integration_tests::init_pure_tests::*;
55+
56+
#[tokio::test]
57+
async fn test_swap_to_send() -> Result<()> {
58+
setup_tracing();
59+
let mint_bob = create_and_start_test_mint().await?;
60+
let wallet_alice_guard = create_test_wallet_arc_mut_for_mint(mint_bob.clone()).await?;
61+
let mut wallet_alice = wallet_alice_guard.lock().await;
62+
63+
// Alice gets 64 sats
64+
fund_wallet(Arc::new(wallet_alice.clone()), 64).await?;
65+
let balance_alice = wallet_alice.total_balance().await?;
66+
assert_eq!(Amount::from(64), balance_alice);
67+
68+
let initial_mint_url = wallet_alice.mint_url.clone();
69+
let mint_info_before = wallet_alice.get_mint_info().await?.unwrap();
70+
assert!(mint_info_before
71+
.urls
72+
.unwrap()
73+
.contains(&initial_mint_url.to_string()));
74+
75+
// Wallet updates mint URL
76+
let new_mint_url = MintUrl::from_str("https://new-mint-url")?;
77+
wallet_alice.update_mint_url(new_mint_url.clone()).await?;
78+
79+
// Check balance after mint URL was updated
80+
let balance_alice_after = wallet_alice.total_balance().await?;
81+
assert_eq!(Amount::from(64), balance_alice_after);
82+
83+
Ok(())
84+
}
85+
}

crates/cdk/src/mint/builder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ impl MintBuilder {
7878
self
7979
}
8080

81+
/// Set initial mint URLs
82+
pub fn with_urls(mut self, urls: Vec<String>) -> Self {
83+
self.mint_info.urls = Some(urls);
84+
self
85+
}
86+
8187
/// Set icon url
8288
pub fn with_icon_url(mut self, icon_url: String) -> Self {
8389
self.mint_info.icon_url = Some(icon_url);

crates/cdk/src/wallet/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,14 @@ impl Wallet {
218218
/// its URL
219219
#[instrument(skip(self))]
220220
pub async fn update_mint_url(&mut self, new_mint_url: MintUrl) -> Result<(), Error> {
221-
self.mint_url = new_mint_url.clone();
222-
// Where the mint_url is in the database it must be updated
221+
// Update the mint URL in the wallet DB
223222
self.localstore
224-
.update_mint_url(self.mint_url.clone(), new_mint_url)
223+
.update_mint_url(self.mint_url.clone(), new_mint_url.clone())
225224
.await?;
226225

227-
self.localstore.remove_mint(self.mint_url.clone()).await?;
226+
// Update the mint URL in the wallet struct field
227+
self.mint_url = new_mint_url;
228+
228229
Ok(())
229230
}
230231

0 commit comments

Comments
 (0)