mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-05-29 11:47:28 -04:00
Introduce a more flexible transaction subscription system
Instead of watching for status changes directly on bitcoin::Wallet, we return a Subscription object back to the caller. This subscription object can be re-used multiple times. Among other things, this now allows callers of `broadcast` to decide on what to wait for given the returned Subscription object. The new API is also more concise which allows us to remove some of the functions on the actor states in favor of simple inline calls. Co-authored-by: rishflab <rishflab@hotmail.com>
This commit is contained in:
parent
6fb495b6ab
commit
01739eddb1
7 changed files with 185 additions and 160 deletions
|
@ -295,11 +295,11 @@ pub struct State3 {
|
|||
S_a_bitcoin: bitcoin::PublicKey,
|
||||
v: monero::PrivateViewKey,
|
||||
xmr: monero::Amount,
|
||||
cancel_timelock: CancelTimelock,
|
||||
pub cancel_timelock: CancelTimelock,
|
||||
punish_timelock: PunishTimelock,
|
||||
refund_address: bitcoin::Address,
|
||||
redeem_address: bitcoin::Address,
|
||||
tx_lock: bitcoin::TxLock,
|
||||
pub tx_lock: bitcoin::TxLock,
|
||||
tx_cancel_sig_a: Signature,
|
||||
tx_refund_encsig: bitcoin::EncryptedSignature,
|
||||
min_monero_confirmations: u64,
|
||||
|
@ -338,18 +338,6 @@ impl State3 {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn wait_for_cancel_timelock_to_expire(
|
||||
&self,
|
||||
bitcoin_wallet: &bitcoin::Wallet,
|
||||
) -> Result<()> {
|
||||
bitcoin_wallet
|
||||
.watch_until_status(&self.tx_lock, |status| {
|
||||
status.is_confirmed_with(self.cancel_timelock)
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cancel(&self) -> State6 {
|
||||
State6 {
|
||||
A: self.A,
|
||||
|
@ -393,11 +381,11 @@ pub struct State4 {
|
|||
s_b: monero::Scalar,
|
||||
S_a_bitcoin: bitcoin::PublicKey,
|
||||
v: monero::PrivateViewKey,
|
||||
cancel_timelock: CancelTimelock,
|
||||
pub cancel_timelock: CancelTimelock,
|
||||
punish_timelock: PunishTimelock,
|
||||
refund_address: bitcoin::Address,
|
||||
redeem_address: bitcoin::Address,
|
||||
tx_lock: bitcoin::TxLock,
|
||||
pub tx_lock: bitcoin::TxLock,
|
||||
tx_cancel_sig_a: Signature,
|
||||
tx_refund_encsig: bitcoin::EncryptedSignature,
|
||||
monero_wallet_restore_blockheight: BlockHeight,
|
||||
|
@ -414,7 +402,9 @@ impl State4 {
|
|||
let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin, tx_redeem.digest());
|
||||
|
||||
bitcoin_wallet
|
||||
.watch_until_status(&tx_redeem, |status| status.has_been_seen())
|
||||
.subscribe_to(tx_redeem.clone())
|
||||
.await
|
||||
.wait_until_seen()
|
||||
.await?;
|
||||
|
||||
let tx_redeem_candidate = bitcoin_wallet.get_raw_transaction(tx_redeem.txid()).await?;
|
||||
|
@ -433,19 +423,6 @@ impl State4 {
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn wait_for_cancel_timelock_to_expire(
|
||||
&self,
|
||||
bitcoin_wallet: &bitcoin::Wallet,
|
||||
) -> Result<()> {
|
||||
bitcoin_wallet
|
||||
.watch_until_status(&self.tx_lock, |status| {
|
||||
status.is_confirmed_with(self.cancel_timelock)
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn expired_timelock(
|
||||
&self,
|
||||
bitcoin_wallet: &bitcoin::Wallet,
|
||||
|
@ -569,9 +546,9 @@ impl State6 {
|
|||
let signed_tx_refund =
|
||||
tx_refund.add_signatures((self.A, sig_a), (self.b.public(), sig_b))?;
|
||||
|
||||
let (_, finality) = bitcoin_wallet.broadcast(signed_tx_refund, "refund").await?;
|
||||
let (_, subscription) = bitcoin_wallet.broadcast(signed_tx_refund, "refund").await?;
|
||||
|
||||
finality.await?;
|
||||
subscription.wait_until_final().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue