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

Commit 0f600ee

Browse files
committed
Stop payjoin session with nodemanager
1 parent 542fdb5 commit 0f600ee

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

mutiny-core/src/nodemanager.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,10 @@ impl<S: MutinyStorage> NodeManager<S> {
771771

772772
pub fn spawn_payjoin_receiver(&self, enrolled: Enrolled) {
773773
let logger = self.logger.clone();
774+
let stop = self.stop.clone();
774775
let wallet = self.wallet.clone();
775776
utils::spawn(async move {
776-
match Self::receive_payjoin(wallet, enrolled).await {
777+
match Self::receive_payjoin(wallet, stop, enrolled).await {
777778
Ok(txid) => log_info!(logger, "Received payjoin txid: {txid}"),
778779
Err(e) => log_error!(logger, "Error receiving payjoin: {e}"),
779780
};
@@ -783,13 +784,14 @@ impl<S: MutinyStorage> NodeManager<S> {
783784
/// Poll the payjoin relay to maintain a payjoin session and create a payjoin proposal.
784785
async fn receive_payjoin(
785786
wallet: Arc<OnChainWallet<S>>,
787+
stop: Arc<AtomicBool>,
786788
mut enrolled: payjoin::receive::v2::Enrolled,
787789
) -> Result<Txid, MutinyError> {
788790
let http_client = reqwest::Client::builder()
789791
.build()
790792
.map_err(PayjoinError::Reqwest)?;
791793
let proposal: payjoin::receive::v2::UncheckedProposal =
792-
Self::poll_for_fallback_psbt(&http_client, &mut enrolled).await?;
794+
Self::poll_for_fallback_psbt(stop, &http_client, &mut enrolled).await?;
793795
let original_tx = proposal.extract_tx_to_schedule_broadcast();
794796
let mut payjoin_proposal = match wallet.process_payjoin_proposal(proposal) {
795797
Ok(p) => p,
@@ -818,10 +820,14 @@ impl<S: MutinyStorage> NodeManager<S> {
818820
}
819821

820822
async fn poll_for_fallback_psbt(
823+
stop: Arc<AtomicBool>,
821824
client: &reqwest::Client,
822825
enroller: &mut payjoin::receive::v2::Enrolled,
823826
) -> Result<payjoin::receive::v2::UncheckedProposal, PayjoinError> {
824827
loop {
828+
if stop.load(Ordering::Relaxed) {
829+
return Err(crate::payjoin::Error::Shutdown);
830+
}
825831
let (req, context) = enroller.extract_req()?;
826832
let ohttp_response = client
827833
.post(req.url)

mutiny-core/src/payjoin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum Error {
3535
ReceiverStateMachine(String),
3636
Txid(bitcoin::hashes::hex::Error),
3737
OhttpDecodeFailed,
38+
Shutdown,
3839
}
3940

4041
impl std::error::Error for Error {}
@@ -46,6 +47,7 @@ impl std::fmt::Display for Error {
4647
Error::ReceiverStateMachine(e) => write!(f, "Payjoin state machine error: {}", e),
4748
Error::Txid(e) => write!(f, "Payjoin txid error: {}", e),
4849
Error::OhttpDecodeFailed => write!(f, "Failed to decode ohttp keys"),
50+
Error::Shutdown => write!(f, "Payjoin stopped by application shutdown"),
4951
}
5052
}
5153
}

0 commit comments

Comments
 (0)