mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-24 15:09:35 -05:00
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.
This commit is contained in:
parent
84ea092a1b
commit
a0830f099f
@ -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,
|
||||
)
|
||||
|
@ -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<bitcoin::Wallet> {
|
||||
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)?,
|
||||
)
|
||||
|
@ -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<Mutex<Client>>,
|
||||
wallet: Arc<Mutex<bdk::Wallet<ElectrumBlockchain, bdk::sled::Tree>>>,
|
||||
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<Segwitv0> + Clone,
|
||||
) -> Result<Self> {
|
||||
@ -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 { "" });
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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<Wallet>,
|
||||
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();
|
||||
|
@ -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(())
|
||||
|
@ -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 {
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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"),
|
||||
|
Loading…
Reference in New Issue
Block a user