From a0830f099f46b487f738e1ab2476b05dcb1dc062 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 16 Mar 2021 18:51:29 +1100 Subject: [PATCH] Pass relevant execution params into wallet instead of via functions The execution params don't change throughout the lifetime of the program. They can be set in the wallet at the very beginning. This simplifies the interface of the wallet functions. --- swap/src/bin/asb.rs | 1 + swap/src/bin/swap.rs | 54 ++++++++++++------- swap/src/bitcoin/wallet.rs | 7 +-- swap/src/protocol/alice/swap.rs | 4 +- swap/src/protocol/bob/refund.rs | 6 +-- swap/src/protocol/bob/state.rs | 13 +---- swap/src/protocol/bob/swap.rs | 4 +- ...refunds_using_cancel_and_refund_command.rs | 1 - ...and_refund_command_timelock_not_expired.rs | 1 - ...fund_command_timelock_not_expired_force.rs | 1 - swap/tests/testutils/mod.rs | 1 + 11 files changed, 46 insertions(+), 47 deletions(-) diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index e618404b..6a494bba 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -137,6 +137,7 @@ async fn init_wallets( let bitcoin_wallet = bitcoin::Wallet::new( config.bitcoin.electrum_rpc_url, BITCOIN_NETWORK, + execution_params.bitcoin_finality_confirmations, bitcoin_wallet_data_dir, key, ) diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index 9c6be51f..dabed242 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -103,9 +103,14 @@ async fn main() -> Result<()> { ) } - let bitcoin_wallet = - init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir.clone()) - .await?; + let bitcoin_wallet = init_bitcoin_wallet( + bitcoin_network, + electrum_rpc_url, + seed, + data_dir.clone(), + execution_params, + ) + .await?; let (monero_wallet, _process) = init_monero_wallet( monero_network, data_dir, @@ -191,9 +196,14 @@ async fn main() -> Result<()> { bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network) } - let bitcoin_wallet = - init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir.clone()) - .await?; + let bitcoin_wallet = init_bitcoin_wallet( + bitcoin_network, + electrum_rpc_url, + seed, + data_dir.clone(), + execution_params, + ) + .await?; let (monero_wallet, _process) = init_monero_wallet( monero_network, data_dir, @@ -237,8 +247,14 @@ async fn main() -> Result<()> { force, electrum_rpc_url, } => { - let bitcoin_wallet = - init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir).await?; + let bitcoin_wallet = init_bitcoin_wallet( + bitcoin_network, + electrum_rpc_url, + seed, + data_dir, + execution_params, + ) + .await?; let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); let cancel = @@ -262,20 +278,18 @@ async fn main() -> Result<()> { force, electrum_rpc_url, } => { - let bitcoin_wallet = - init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir).await?; + let bitcoin_wallet = init_bitcoin_wallet( + bitcoin_network, + electrum_rpc_url, + seed, + data_dir, + execution_params, + ) + .await?; let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); - bob::refund( - swap_id, - resume_state, - execution_params, - Arc::new(bitcoin_wallet), - db, - force, - ) - .await??; + bob::refund(swap_id, resume_state, Arc::new(bitcoin_wallet), db, force).await??; } }; Ok(()) @@ -286,12 +300,14 @@ async fn init_bitcoin_wallet( electrum_rpc_url: Url, seed: Seed, data_dir: PathBuf, + execution_params: ExecutionParams, ) -> Result { let wallet_dir = data_dir.join("wallet"); let wallet = bitcoin::Wallet::new( electrum_rpc_url.clone(), network, + execution_params.bitcoin_finality_confirmations, &wallet_dir, seed.derive_extended_private_key(network)?, ) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 7093743b..f3306d25 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -1,6 +1,5 @@ use crate::bitcoin::timelocks::BlockHeight; use crate::bitcoin::{Address, Amount, Transaction}; -use crate::execution_params::ExecutionParams; use ::bitcoin::util::psbt::PartiallySignedTransaction; use ::bitcoin::Txid; use anyhow::{anyhow, bail, Context, Result}; @@ -24,12 +23,14 @@ const SLED_TREE_NAME: &str = "default_tree"; pub struct Wallet { client: Arc>, wallet: Arc>>, + bitcoin_finality_confirmations: u32, } impl Wallet { pub async fn new( electrum_rpc_url: Url, network: bitcoin::Network, + bitcoin_finality_confirmations: u32, wallet_dir: &Path, key: impl DerivableKey + Clone, ) -> Result { @@ -58,6 +59,7 @@ impl Wallet { Ok(Self { wallet: Arc::new(Mutex::new(bdk_wallet)), client: Arc::new(Mutex::new(Client::new(electrum, interval)?)), + bitcoin_finality_confirmations, }) } @@ -225,9 +227,8 @@ impl Wallet { &self, txid: Txid, script_to_watch: Script, - execution_params: ExecutionParams, ) -> Result<()> { - let conf_target = execution_params.bitcoin_finality_confirmations; + let conf_target = self.bitcoin_finality_confirmations; tracing::info!(%txid, "Waiting for {} confirmation{} of Bitcoin transaction", conf_target, if conf_target > 1 { "s" } else { "" }); diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index e041f37a..b59a2fd8 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -95,7 +95,6 @@ async fn run_until_internal( .wait_for_transaction_finality( state3.tx_lock.txid(), state3.tx_lock.script_pubkey(), - execution_params, ) .await?; @@ -224,7 +223,6 @@ async fn run_until_internal( .wait_for_transaction_finality( txid, state3.redeem_address.script_pubkey(), - execution_params, ) .await; @@ -413,7 +411,7 @@ async fn run_until_internal( let txid = bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?; bitcoin_wallet - .wait_for_transaction_finality(txid, punish_script_pubkey, execution_params) + .wait_for_transaction_finality(txid, punish_script_pubkey) .await?; Result::<_, anyhow::Error>::Ok(txid) diff --git a/swap/src/protocol/bob/refund.rs b/swap/src/protocol/bob/refund.rs index ffbfe458..b1390045 100644 --- a/swap/src/protocol/bob/refund.rs +++ b/swap/src/protocol/bob/refund.rs @@ -1,6 +1,5 @@ use crate::bitcoin::Wallet; use crate::database::{Database, Swap}; -use crate::execution_params::ExecutionParams; use crate::protocol::bob::BobState; use anyhow::{bail, Result}; use std::sync::Arc; @@ -13,7 +12,6 @@ pub struct SwapNotCancelledYet(Uuid); pub async fn refund( swap_id: Uuid, state: BobState, - execution_params: ExecutionParams, bitcoin_wallet: Arc, db: Database, force: bool, @@ -41,9 +39,7 @@ pub async fn refund( } }; - state4 - .refund_btc(bitcoin_wallet.as_ref(), execution_params) - .await?; + state4.refund_btc(bitcoin_wallet.as_ref()).await?; let state = BobState::BtcRefunded(state4); let db_state = state.clone().into(); diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index d743c0f5..d723b9c2 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -2,7 +2,6 @@ use crate::bitcoin::{ self, current_epoch, CancelTimelock, ExpiredTimelocks, PunishTimelock, Transaction, TxCancel, TxLock, Txid, }; -use crate::execution_params::ExecutionParams; use crate::monero; use crate::monero::{monero_private_key, InsufficientFunds, TransferProof}; use crate::monero_ext::ScalarExt; @@ -531,11 +530,7 @@ impl State4 { )) } - pub async fn refund_btc( - &self, - bitcoin_wallet: &bitcoin::Wallet, - execution_params: ExecutionParams, - ) -> Result<()> { + pub async fn refund_btc(&self, bitcoin_wallet: &bitcoin::Wallet) -> Result<()> { let tx_cancel = bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.A, self.b.public()); let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &self.refund_address); @@ -552,11 +547,7 @@ impl State4 { let txid = bitcoin_wallet.broadcast(signed_tx_refund, "refund").await?; bitcoin_wallet - .wait_for_transaction_finality( - txid, - self.refund_address.script_pubkey(), - execution_params, - ) + .wait_for_transaction_finality(txid, self.refund_address.script_pubkey()) .await?; Ok(()) diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index b051923a..75db537d 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -382,9 +382,7 @@ async fn run_until_internal( bail!("Internal error: canceled state reached before cancel timelock was expired"); } ExpiredTimelocks::Cancel => { - state - .refund_btc(bitcoin_wallet.as_ref(), execution_params) - .await?; + state.refund_btc(bitcoin_wallet.as_ref()).await?; BobState::BtcRefunded(state) } ExpiredTimelocks::Punish => BobState::BtcPunished { diff --git a/swap/tests/bob_refunds_using_cancel_and_refund_command.rs b/swap/tests/bob_refunds_using_cancel_and_refund_command.rs index 1f464adb..44e05385 100644 --- a/swap/tests/bob_refunds_using_cancel_and_refund_command.rs +++ b/swap/tests/bob_refunds_using_cancel_and_refund_command.rs @@ -48,7 +48,6 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() { let bob_state = bob::refund( bob_swap.swap_id, bob_swap.state, - bob_swap.execution_params, bob_swap.bitcoin_wallet, bob_swap.db, false, diff --git a/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired.rs b/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired.rs index d5f23aad..42c747f7 100644 --- a/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired.rs +++ b/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired.rs @@ -39,7 +39,6 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() { bob::refund( bob_swap.swap_id, bob_swap.state, - bob_swap.execution_params, bob_swap.bitcoin_wallet, bob_swap.db, false, diff --git a/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force.rs b/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force.rs index 9ae6bf38..255fa007 100644 --- a/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force.rs +++ b/swap/tests/bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force.rs @@ -36,7 +36,6 @@ async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() { let is_error = bob::refund( bob_swap.swap_id, bob_swap.state, - bob_swap.execution_params, bob_swap.bitcoin_wallet, bob_swap.db, true, diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index ce480ed8..bd051ca4 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -605,6 +605,7 @@ async fn init_test_wallets( let btc_wallet = swap::bitcoin::Wallet::new( electrum_rpc_url, bitcoin::Network::Regtest, + execution_params.bitcoin_finality_confirmations, datadir, seed.derive_extended_private_key(bitcoin::Network::Regtest) .expect("Could not create extended private key from seed"),