Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit c6317d9

Browse files
committed
Stop payjoin session with nodemanager
1 parent 80b57db commit c6317d9

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

mutiny-core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,10 +1540,11 @@ impl<S: MutinyStorage> MutinyWallet<S> {
15401540
let pj_uri = enrolled.fallback_target();
15411541
log_debug!(self.logger, "{pj_uri}");
15421542
let wallet = self.node_manager.wallet.clone();
1543+
let stop = self.node_manager.stop.clone();
15431544
// run await payjoin task in the background as it'll keep polling the relay
15441545
let logger = self.logger.clone();
15451546
utils::spawn(async move {
1546-
match NodeManager::receive_payjoin(wallet, enrolled).await {
1547+
match NodeManager::receive_payjoin(wallet, stop, enrolled).await {
15471548
Ok(pj_txid) => log_info!(logger, "Received payjoin txid: {}", pj_txid),
15481549
Err(e) => log_error!(logger, "Payjoin error: {e}"),
15491550
}

mutiny-core/src/nodemanager.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ impl<S: MutinyStorage> NodeManager<S> {
795795
/// Poll the payjoin relay to maintain a payjoin session and create a payjoin proposal.
796796
pub async fn receive_payjoin(
797797
wallet: Arc<OnChainWallet<S>>,
798+
stop: Arc<AtomicBool>,
798799
mut enrolled: payjoin::receive::v2::Enrolled,
799800
) -> Result<Txid, MutinyError> {
800801
use crate::payjoin::Error as PayjoinError;
@@ -803,7 +804,9 @@ impl<S: MutinyStorage> NodeManager<S> {
803804
.build()
804805
.map_err(PayjoinError::Reqwest)?;
805806
let proposal: payjoin::receive::v2::UncheckedProposal =
806-
Self::poll_for_fallback_psbt(&http_client, &mut enrolled).await?;
807+
Self::poll_for_fallback_psbt(stop, &http_client, &mut enrolled)
808+
.await
809+
.map_err(|e| PayjoinError::ReceiverStateMachine(e.to_string()))?;
807810
let mut payjoin_proposal = wallet
808811
.process_payjoin_proposal(proposal)
809812
.map_err(|e| PayjoinError::ReceiverStateMachine(e.to_string()))?;
@@ -827,10 +830,14 @@ impl<S: MutinyStorage> NodeManager<S> {
827830
}
828831

829832
async fn poll_for_fallback_psbt(
833+
stop: Arc<AtomicBool>,
830834
client: &reqwest::Client,
831835
enroller: &mut payjoin::receive::v2::Enrolled,
832836
) -> Result<payjoin::receive::v2::UncheckedProposal, crate::payjoin::Error> {
833837
loop {
838+
if stop.load(Ordering::Relaxed) {
839+
return Err(crate::payjoin::Error::Shutdown);
840+
}
834841
let (req, context) = enroller.extract_req()?;
835842
let ohttp_response = client
836843
.post(req.url)

mutiny-core/src/payjoin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub enum Error {
3131
Reqwest(reqwest::Error),
3232
ReceiverStateMachine(String),
3333
Txid(bitcoin::hashes::hex::Error),
34+
Shutdown,
3435
}
3536

3637
impl std::error::Error for Error {}
@@ -41,6 +42,7 @@ impl std::fmt::Display for Error {
4142
Error::Reqwest(e) => write!(f, "Reqwest error: {}", e),
4243
Error::ReceiverStateMachine(e) => write!(f, "Payjoin state machine error: {}", e),
4344
Error::Txid(e) => write!(f, "Payjoin txid error: {}", e),
45+
Error::Shutdown => write!(f, "Payjoin stopped by application shutdown"),
4446
}
4547
}
4648
}

0 commit comments

Comments
 (0)