Skip to content

Commit aea2792

Browse files
committed
joinstr: add Joinstr::start_coinjoin_blocking() and make Joinstr::start_coinjoin() non blocking
1 parent dd4d5b7 commit aea2792

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

rust/joinstr/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn initiate_coinjoin(config: PoolConfig, peer: PeerConfig) -> Result<Txid, E
140140
initiator.set_coin(coin)?;
141141
initiator.set_address(addr)?;
142142

143-
initiator.start_coinjoin(None, Some(&signer))?;
143+
initiator.start_coinjoin_blocking(None, Some(signer.clone()))?;
144144

145145
let txid = initiator
146146
.final_tx()
@@ -200,7 +200,7 @@ pub fn join_coinjoin(pool: Pool, peer: PeerConfig) -> Result<String /* Txid */,
200200
let client = Client::new(&url, port)?;
201201
signer.set_client(client);
202202

203-
joinstr_peer.start_coinjoin(None, Some(&signer))?;
203+
joinstr_peer.start_coinjoin_blocking(None, Some(signer.clone()))?;
204204

205205
let txid = joinstr_peer
206206
.final_tx()

rust/joinstr/src/joinstr/mod.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub use error::Error;
55
use std::{
66
collections::HashSet,
77
sync::{Arc, Mutex},
8+
thread,
89
time::{SystemTime, UNIX_EPOCH},
910
};
1011

@@ -28,9 +29,9 @@ use crate::{
2829
// delay we wait between (non-blocking) polls of a channel
2930
pub const WAIT: u64 = 50;
3031

31-
#[derive(Debug)]
32+
#[derive(Debug, Clone)]
3233
pub struct Joinstr<'a> {
33-
inner: Arc<Mutex<JoinstrInner<'a>>>,
34+
pub inner: Arc<Mutex<JoinstrInner<'a>>>,
3435
}
3536

3637
#[derive(Debug, Default, Clone, Copy)]
@@ -62,6 +63,7 @@ pub struct Status {
6263
registered_outputs: usize,
6364
registered_inputs: usize,
6465
confirmations: usize,
66+
error: Option<String>,
6567
}
6668

6769
#[derive(Debug)]
@@ -72,6 +74,7 @@ pub struct JoinstrInner<'a> {
7274
registered_outputs: usize,
7375
registered_inputs: usize,
7476
confirmations: usize,
77+
error: Option<String>,
7578
pub client: NostrClient,
7679
pub pool: Option<Pool>,
7780
pub denomination: Option<Amount>,
@@ -96,6 +99,7 @@ impl Default for JoinstrInner<'_> {
9699
registered_outputs: 0,
97100
registered_inputs: 0,
98101
confirmations: 0,
102+
error: None,
99103
client: Default::default(),
100104
pool: Default::default(),
101105
denomination: Default::default(),
@@ -729,9 +733,29 @@ impl Joinstr<'_> {
729733
/// # Errors
730734
///
731735
/// This function will return an error if any step return an error.
732-
pub fn start_coinjoin<S>(&mut self, pool: Option<Pool>, signer: Option<&S>) -> Result<(), Error>
736+
pub fn start_coinjoin<S>(&mut self, pool: Option<Pool>, signer: Option<S>)
733737
where
734-
S: JoinstrSigner,
738+
S: JoinstrSigner + Sized + Sync + Clone + Send + 'static,
739+
Self: Sized + Send + 'static,
740+
{
741+
let mut cloned = self.clone();
742+
let signer = signer.clone();
743+
thread::spawn(move || {
744+
if let Err(e) = cloned.start_coinjoin_blocking(pool, signer) {
745+
let mut inner = cloned.inner.lock().expect("poisoned");
746+
inner.error = Some(format!("{:?}", e));
747+
inner.step = Step::Failed;
748+
}
749+
});
750+
}
751+
752+
pub fn start_coinjoin_blocking<S>(
753+
&mut self,
754+
pool: Option<Pool>,
755+
signer: Option<S>,
756+
) -> Result<(), Error>
757+
where
758+
S: JoinstrSigner + Sync + Clone + Send + 'static,
735759
{
736760
let mut inner = self.inner.lock().expect("poisoned");
737761

@@ -762,7 +786,7 @@ impl Joinstr<'_> {
762786
let mut inner = self.inner.lock().expect("poisoned");
763787
if inner.input.is_some() {
764788
if let Some(s) = signer {
765-
inner.register_input(s)?;
789+
inner.register_input(&s)?;
766790
} else {
767791
return Err(Error::SignerMissing);
768792
}
@@ -1207,6 +1231,7 @@ impl<'a> JoinstrInner<'a> {
12071231
registered_outputs: self.registered_outputs,
12081232
registered_inputs: self.registered_inputs,
12091233
confirmations: self.confirmations,
1234+
error: self.error.clone(),
12101235
}
12111236
}
12121237
}

rust/joinstr/src/signer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait JoinstrSigner {
2424
fn sign_input(&self, tx: &Transaction, input_data: Coin) -> Result<InputDataSigned, String>;
2525
}
2626

27+
// S: JoinstrSigner + Sync + Clone + Send + 'static,
2728
#[derive(Clone)]
2829
pub struct WpkhHotSigner {
2930
#[allow(unused)]

rust/joinstr/tests/joinstr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn simple_coinjoin() {
139139

140140
let coordinator_handle = thread::spawn(move || {
141141
coordinator
142-
.start_coinjoin(None, Option::<&WpkhHotSigner>::None)
142+
.start_coinjoin_blocking(None, Option::<WpkhHotSigner>::None)
143143
.unwrap();
144144
coordinator.final_tx()
145145
});
@@ -226,11 +226,11 @@ fn simple_coinjoin() {
226226
let signer_a = signer.clone();
227227
let pool_a = pool.clone();
228228
let _peer_a = thread::spawn(move || {
229-
let _ = peer_a.start_coinjoin(Some(pool_a), Some(&signer_a));
229+
let _ = peer_a.start_coinjoin_blocking(Some(pool_a), Some(signer_a));
230230
});
231231

232232
let _peer_b = thread::spawn(move || {
233-
let _ = peer_b.start_coinjoin(Some(pool), Some(&signer));
233+
let _ = peer_b.start_coinjoin_blocking(Some(pool), Some(signer));
234234
});
235235

236236
let final_tx = coordinator_handle.join().unwrap().unwrap();

0 commit comments

Comments
 (0)