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

Commit 9e736db

Browse files
committed
Stop payjoin session with nodemanager
1 parent 2ca4379 commit 9e736db

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
@@ -774,9 +774,10 @@ impl<S: MutinyStorage> NodeManager<S> {
774774

775775
pub fn spawn_payjoin_receiver(&self, enrolled: Enrolled) {
776776
let logger = self.logger.clone();
777+
let stop = self.stop.clone();
777778
let wallet = self.wallet.clone();
778779
utils::spawn(async move {
779-
match Self::receive_payjoin(wallet, enrolled).await {
780+
match Self::receive_payjoin(wallet, stop, enrolled).await {
780781
Ok(txid) => log_info!(logger, "Received payjoin txid: {txid}"),
781782
Err(e) => log_error!(logger, "Error receiving payjoin: {e}"),
782783
};
@@ -786,13 +787,14 @@ impl<S: MutinyStorage> NodeManager<S> {
786787
/// Poll the payjoin relay to maintain a payjoin session and create a payjoin proposal.
787788
async fn receive_payjoin(
788789
wallet: Arc<OnChainWallet<S>>,
790+
stop: Arc<AtomicBool>,
789791
mut enrolled: payjoin::receive::v2::Enrolled,
790792
) -> Result<Txid, MutinyError> {
791793
let http_client = reqwest::Client::builder()
792794
.build()
793795
.map_err(PayjoinError::Reqwest)?;
794796
let proposal: payjoin::receive::v2::UncheckedProposal =
795-
Self::poll_for_fallback_psbt(&http_client, &mut enrolled).await?;
797+
Self::poll_for_fallback_psbt(stop, &http_client, &mut enrolled).await?;
796798
let original_tx = proposal.extract_tx_to_schedule_broadcast();
797799
let mut payjoin_proposal = match wallet
798800
.process_payjoin_proposal(proposal)
@@ -824,10 +826,14 @@ impl<S: MutinyStorage> NodeManager<S> {
824826
}
825827

826828
async fn poll_for_fallback_psbt(
829+
stop: Arc<AtomicBool>,
827830
client: &reqwest::Client,
828831
enroller: &mut payjoin::receive::v2::Enrolled,
829832
) -> Result<payjoin::receive::v2::UncheckedProposal, PayjoinError> {
830833
loop {
834+
if stop.load(Ordering::Relaxed) {
835+
return Err(crate::payjoin::Error::Shutdown);
836+
}
831837
let (req, context) = enroller.extract_req()?;
832838
let ohttp_response = client
833839
.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)