Refactor mintd used in itest (#616)

This commit is contained in:
thesimplekid
2025-02-25 11:47:17 +00:00
committed by GitHub
parent 692e13ff16
commit 827e4aebde
17 changed files with 546 additions and 578 deletions

View File

@@ -1,24 +1,13 @@
use std::str::FromStr;
use std::sync::Arc;
use anyhow::{bail, Result};
use cdk::amount::{Amount, SplitTarget};
use cdk::dhke::construct_proofs;
use cdk::mint_url::MintUrl;
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::nut17::Params;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MintBolt11Request, MintQuoteBolt11Request, MintQuoteState,
NotificationPayload, PreMintSecrets, Proofs, State,
};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::subscription::SubscriptionManager;
use cdk::nuts::{MintQuoteState, NotificationPayload, State};
use cdk::wallet::WalletSubscription;
use cdk::Wallet;
use tokio::time::{timeout, Duration};
use tokio::time::{sleep, timeout, Duration};
pub mod init_fake_wallet;
pub mod init_mint;
pub mod init_pure_tests;
pub mod init_regtest;
@@ -30,19 +19,7 @@ pub async fn wallet_mint(
) -> Result<()> {
let quote = wallet.mint_quote(amount, description).await?;
let mut subscription = wallet
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
.id
.clone()]))
.await;
while let Some(msg) = subscription.recv().await {
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
if response.state == MintQuoteState::Paid {
break;
}
}
}
wait_for_mint_to_be_paid(&wallet, &quote.id, 60).await?;
let proofs = wallet.mint(&quote.id, split_target, None).await?;
@@ -53,70 +30,6 @@ pub async fn wallet_mint(
Ok(())
}
pub async fn mint_proofs(
mint_url: &str,
amount: Amount,
keyset_id: Id,
mint_keys: &KeySet,
description: Option<String>,
) -> anyhow::Result<Proofs> {
println!("Minting for ecash");
println!();
let wallet_client = HttpClient::new(MintUrl::from_str(mint_url)?);
let request = MintQuoteBolt11Request {
amount,
unit: CurrencyUnit::Sat,
description,
pubkey: None,
};
let mint_quote = wallet_client.post_mint_quote(request).await?;
println!("Please pay: {}", mint_quote.request);
let subscription_client = SubscriptionManager::new(Arc::new(wallet_client.clone()));
let mut subscription = subscription_client
.subscribe(
mint_url.parse()?,
Params {
filters: vec![mint_quote.quote.clone()],
kind: cdk::nuts::nut17::Kind::Bolt11MintQuote,
id: "sub".into(),
},
)
.await;
while let Some(msg) = subscription.recv().await {
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
if response.state == MintQuoteState::Paid {
break;
}
}
}
let premint_secrets = PreMintSecrets::random(keyset_id, amount, &SplitTarget::default())?;
let request = MintBolt11Request {
quote: mint_quote.quote,
outputs: premint_secrets.blinded_messages(),
signature: None,
};
let mint_response = wallet_client.post_mint(request).await?;
let pre_swap_proofs = construct_proofs(
mint_response.signatures,
premint_secrets.rs(),
premint_secrets.secrets(),
&mint_keys.clone().keys,
)?;
Ok(pre_swap_proofs)
}
// Get all pending from wallet and attempt to swap
// Will panic if there are no pending
// Will return Ok if swap fails as expected
@@ -154,6 +67,7 @@ pub async fn attempt_to_swap_pending(wallet: &Wallet) -> Result<()> {
Ok(())
}
#[allow(clippy::incompatible_msrv)]
pub async fn wait_for_mint_to_be_paid(
wallet: &Wallet,
mint_quote_id: &str,
@@ -164,7 +78,6 @@ pub async fn wait_for_mint_to_be_paid(
mint_quote_id.to_owned(),
]))
.await;
// Create the timeout future
let wait_future = async {
while let Some(msg) = subscription.recv().await {
@@ -177,9 +90,36 @@ pub async fn wait_for_mint_to_be_paid(
Ok(())
};
// Wait for either the payment to complete or timeout
match timeout(Duration::from_secs(timeout_secs), wait_future).await {
Ok(result) => result,
Err(_) => Err(anyhow::anyhow!("Timeout waiting for mint quote to be paid")),
let timeout_future = timeout(Duration::from_secs(timeout_secs), wait_future);
let check_interval = Duration::from_secs(5);
let periodic_task = async {
loop {
match wallet.mint_quote_state(mint_quote_id).await {
Ok(result) => {
if result.state == MintQuoteState::Paid {
tracing::info!("mint quote paid via poll");
return Ok(());
}
}
Err(e) => {
tracing::error!("Could not check mint quote status: {:?}", e);
}
}
sleep(check_interval).await;
}
};
tokio::select! {
result = timeout_future => {
match result {
Ok(payment_result) => payment_result,
Err(_) => Err(anyhow::anyhow!("Timeout waiting for mint quote to be paid")),
}
}
result = periodic_task => {
result // Now propagates the result from periodic checks
}
}
}