From 84ea092a1b354dac186b72f15795f0bc96720999 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 16 Mar 2021 18:31:13 +1100 Subject: [PATCH] Remove unnecessary state variables by constructing TXs on demand --- swap/src/database/alice.rs | 37 ++++++++------------------------ swap/src/protocol/alice/state.rs | 19 ++++++++++------ swap/src/protocol/alice/steps.rs | 5 ++--- swap/src/protocol/alice/swap.rs | 17 ++++++--------- 4 files changed, 31 insertions(+), 47 deletions(-) diff --git a/swap/src/database/alice.rs b/swap/src/database/alice.rs index 6c1a19ab..cb6c30b6 100644 --- a/swap/src/database/alice.rs +++ b/swap/src/database/alice.rs @@ -1,4 +1,4 @@ -use crate::bitcoin::{EncryptedSignature, TxCancel, TxRefund}; +use crate::bitcoin::EncryptedSignature; use crate::monero; use crate::monero::monero_private_key; use crate::protocol::alice; @@ -177,37 +177,18 @@ impl From for AliceState { Alice::BtcCancelled { monero_wallet_restore_blockheight, state3, - } => { - let tx_cancel = TxCancel::new( - &state3.tx_lock, - state3.cancel_timelock, - state3.a.public(), - state3.B, - ); + } => AliceState::BtcCancelled { + monero_wallet_restore_blockheight, + state3: Box::new(state3), + }, - AliceState::BtcCancelled { - monero_wallet_restore_blockheight, - state3: Box::new(state3), - tx_cancel: Box::new(tx_cancel), - } - } Alice::BtcPunishable { monero_wallet_restore_blockheight, state3, - } => { - let tx_cancel = TxCancel::new( - &state3.tx_lock, - state3.cancel_timelock, - state3.a.public(), - state3.B, - ); - let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address); - AliceState::BtcPunishable { - monero_wallet_restore_blockheight, - tx_refund: Box::new(tx_refund), - state3: Box::new(state3), - } - } + } => AliceState::BtcPunishable { + monero_wallet_restore_blockheight, + state3: Box::new(state3), + }, Alice::BtcRefunded { monero_wallet_restore_blockheight, state3, diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index bbfa0e2d..f3b83881 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -36,7 +36,6 @@ pub enum AliceState { BtcRedeemed, BtcCancelled { monero_wallet_restore_blockheight: BlockHeight, - tx_cancel: Box, state3: Box, }, BtcRefunded { @@ -46,7 +45,6 @@ pub enum AliceState { }, BtcPunishable { monero_wallet_restore_blockheight: BlockHeight, - tx_refund: Box, state3: Box, }, XmrRefunded, @@ -337,7 +335,7 @@ impl State3 { &self, bitcoin_wallet: &bitcoin::Wallet, ) -> Result { - let tx_cancel = TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B); + let tx_cancel = self.tx_cancel(); let tx_lock_status = bitcoin_wallet .status_of_script(&self.tx_lock.script_pubkey(), &self.tx_lock.txid()) @@ -354,10 +352,19 @@ impl State3 { )) } + pub fn tx_cancel(&self) -> TxCancel { + TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B) + } + pub fn tx_punish(&self) -> TxPunish { - let tx_cancel = - bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B); + bitcoin::TxPunish::new( + &self.tx_cancel(), + &self.punish_address, + self.punish_timelock, + ) + } - bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock) + pub fn tx_refund(&self) -> TxRefund { + bitcoin::TxRefund::new(&self.tx_cancel(), &self.refund_address) } } diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 43732012..d4516a07 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -59,7 +59,7 @@ pub async fn publish_cancel_transaction( cancel_timelock: CancelTimelock, tx_cancel_sig_bob: bitcoin::Signature, bitcoin_wallet: &bitcoin::Wallet, -) -> Result { +) -> Result<()> { bitcoin_wallet .watch_until_status(tx_lock.txid(), tx_lock.script_pubkey(), |status| { status.is_confirmed_with(cancel_timelock) @@ -81,7 +81,6 @@ pub async fn publish_cancel_transaction( let sig_b = tx_cancel_sig_bob.clone(); let tx_cancel = tx_cancel - .clone() .add_signatures((a.public(), sig_a), (B, sig_b)) .expect("sig_{a,b} to be valid signatures for tx_cancel"); @@ -92,7 +91,7 @@ pub async fn publish_cancel_transaction( // block height } - Ok(tx_cancel) + Ok(()) } pub async fn wait_for_bitcoin_refund( diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index e15ade0d..e041f37a 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -285,7 +285,7 @@ async fn run_until_internal( state3, monero_wallet_restore_blockheight, } => { - let tx_cancel = publish_cancel_transaction( + publish_cancel_transaction( state3.tx_lock.clone(), state3.a.clone(), state3.B, @@ -297,7 +297,6 @@ async fn run_until_internal( let state = AliceState::BtcCancelled { state3, - tx_cancel: Box::new(tx_cancel), monero_wallet_restore_blockheight, }; let db_state = (&state).into(); @@ -317,11 +316,10 @@ async fn run_until_internal( } AliceState::BtcCancelled { state3, - tx_cancel, monero_wallet_restore_blockheight, } => { let (tx_refund, published_refund_tx) = wait_for_bitcoin_refund( - &tx_cancel, + &state3.tx_cancel(), state3.punish_timelock, &state3.refund_address, &bitcoin_wallet, @@ -332,7 +330,6 @@ async fn run_until_internal( match published_refund_tx { None => { let state = AliceState::BtcPunishable { - tx_refund: Box::new(tx_refund), state3, monero_wallet_restore_blockheight, }; @@ -401,7 +398,6 @@ async fn run_until_internal( Ok(state) } AliceState::BtcPunishable { - tx_refund, state3, monero_wallet_restore_blockheight, } => { @@ -424,7 +420,7 @@ async fn run_until_internal( }; let refund_tx_seen = bitcoin_wallet.watch_until_status( - tx_refund.txid(), + state3.tx_refund().txid(), state3.refund_address.script_pubkey(), |status| status.has_been_seen(), ); @@ -434,12 +430,13 @@ async fn run_until_internal( match select(refund_tx_seen, punish_tx_finalised).await { Either::Left((Ok(()), _)) => { - let published_refund_tx = - bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?; + let published_refund_tx = bitcoin_wallet + .get_raw_transaction(state3.tx_refund().txid()) + .await?; let spend_key = extract_monero_private_key( published_refund_tx, - &tx_refund, + &state3.tx_refund(), state3.s_a, state3.a.clone(), state3.S_b_bitcoin,