mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04: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(
|
let bitcoin_wallet = bitcoin::Wallet::new(
|
||||||
config.bitcoin.electrum_rpc_url,
|
config.bitcoin.electrum_rpc_url,
|
||||||
BITCOIN_NETWORK,
|
BITCOIN_NETWORK,
|
||||||
|
execution_params.bitcoin_finality_confirmations,
|
||||||
bitcoin_wallet_data_dir,
|
bitcoin_wallet_data_dir,
|
||||||
key,
|
key,
|
||||||
)
|
)
|
||||||
|
@ -103,9 +103,14 @@ async fn main() -> Result<()> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let bitcoin_wallet =
|
let bitcoin_wallet = init_bitcoin_wallet(
|
||||||
init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir.clone())
|
bitcoin_network,
|
||||||
.await?;
|
electrum_rpc_url,
|
||||||
|
seed,
|
||||||
|
data_dir.clone(),
|
||||||
|
execution_params,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
let (monero_wallet, _process) = init_monero_wallet(
|
let (monero_wallet, _process) = init_monero_wallet(
|
||||||
monero_network,
|
monero_network,
|
||||||
data_dir,
|
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)
|
bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network)
|
||||||
}
|
}
|
||||||
|
|
||||||
let bitcoin_wallet =
|
let bitcoin_wallet = init_bitcoin_wallet(
|
||||||
init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir.clone())
|
bitcoin_network,
|
||||||
.await?;
|
electrum_rpc_url,
|
||||||
|
seed,
|
||||||
|
data_dir.clone(),
|
||||||
|
execution_params,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
let (monero_wallet, _process) = init_monero_wallet(
|
let (monero_wallet, _process) = init_monero_wallet(
|
||||||
monero_network,
|
monero_network,
|
||||||
data_dir,
|
data_dir,
|
||||||
@ -237,8 +247,14 @@ async fn main() -> Result<()> {
|
|||||||
force,
|
force,
|
||||||
electrum_rpc_url,
|
electrum_rpc_url,
|
||||||
} => {
|
} => {
|
||||||
let bitcoin_wallet =
|
let bitcoin_wallet = init_bitcoin_wallet(
|
||||||
init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir).await?;
|
bitcoin_network,
|
||||||
|
electrum_rpc_url,
|
||||||
|
seed,
|
||||||
|
data_dir,
|
||||||
|
execution_params,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
||||||
let cancel =
|
let cancel =
|
||||||
@ -262,20 +278,18 @@ async fn main() -> Result<()> {
|
|||||||
force,
|
force,
|
||||||
electrum_rpc_url,
|
electrum_rpc_url,
|
||||||
} => {
|
} => {
|
||||||
let bitcoin_wallet =
|
let bitcoin_wallet = init_bitcoin_wallet(
|
||||||
init_bitcoin_wallet(bitcoin_network, electrum_rpc_url, seed, data_dir).await?;
|
bitcoin_network,
|
||||||
|
electrum_rpc_url,
|
||||||
|
seed,
|
||||||
|
data_dir,
|
||||||
|
execution_params,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
||||||
|
|
||||||
bob::refund(
|
bob::refund(swap_id, resume_state, Arc::new(bitcoin_wallet), db, force).await??;
|
||||||
swap_id,
|
|
||||||
resume_state,
|
|
||||||
execution_params,
|
|
||||||
Arc::new(bitcoin_wallet),
|
|
||||||
db,
|
|
||||||
force,
|
|
||||||
)
|
|
||||||
.await??;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -286,12 +300,14 @@ async fn init_bitcoin_wallet(
|
|||||||
electrum_rpc_url: Url,
|
electrum_rpc_url: Url,
|
||||||
seed: Seed,
|
seed: Seed,
|
||||||
data_dir: PathBuf,
|
data_dir: PathBuf,
|
||||||
|
execution_params: ExecutionParams,
|
||||||
) -> Result<bitcoin::Wallet> {
|
) -> Result<bitcoin::Wallet> {
|
||||||
let wallet_dir = data_dir.join("wallet");
|
let wallet_dir = data_dir.join("wallet");
|
||||||
|
|
||||||
let wallet = bitcoin::Wallet::new(
|
let wallet = bitcoin::Wallet::new(
|
||||||
electrum_rpc_url.clone(),
|
electrum_rpc_url.clone(),
|
||||||
network,
|
network,
|
||||||
|
execution_params.bitcoin_finality_confirmations,
|
||||||
&wallet_dir,
|
&wallet_dir,
|
||||||
seed.derive_extended_private_key(network)?,
|
seed.derive_extended_private_key(network)?,
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::bitcoin::timelocks::BlockHeight;
|
use crate::bitcoin::timelocks::BlockHeight;
|
||||||
use crate::bitcoin::{Address, Amount, Transaction};
|
use crate::bitcoin::{Address, Amount, Transaction};
|
||||||
use crate::execution_params::ExecutionParams;
|
|
||||||
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
use ::bitcoin::Txid;
|
use ::bitcoin::Txid;
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
@ -24,12 +23,14 @@ const SLED_TREE_NAME: &str = "default_tree";
|
|||||||
pub struct Wallet {
|
pub struct Wallet {
|
||||||
client: Arc<Mutex<Client>>,
|
client: Arc<Mutex<Client>>,
|
||||||
wallet: Arc<Mutex<bdk::Wallet<ElectrumBlockchain, bdk::sled::Tree>>>,
|
wallet: Arc<Mutex<bdk::Wallet<ElectrumBlockchain, bdk::sled::Tree>>>,
|
||||||
|
bitcoin_finality_confirmations: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wallet {
|
impl Wallet {
|
||||||
pub async fn new(
|
pub async fn new(
|
||||||
electrum_rpc_url: Url,
|
electrum_rpc_url: Url,
|
||||||
network: bitcoin::Network,
|
network: bitcoin::Network,
|
||||||
|
bitcoin_finality_confirmations: u32,
|
||||||
wallet_dir: &Path,
|
wallet_dir: &Path,
|
||||||
key: impl DerivableKey<Segwitv0> + Clone,
|
key: impl DerivableKey<Segwitv0> + Clone,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
@ -58,6 +59,7 @@ impl Wallet {
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
wallet: Arc::new(Mutex::new(bdk_wallet)),
|
wallet: Arc::new(Mutex::new(bdk_wallet)),
|
||||||
client: Arc::new(Mutex::new(Client::new(electrum, interval)?)),
|
client: Arc::new(Mutex::new(Client::new(electrum, interval)?)),
|
||||||
|
bitcoin_finality_confirmations,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,9 +227,8 @@ impl Wallet {
|
|||||||
&self,
|
&self,
|
||||||
txid: Txid,
|
txid: Txid,
|
||||||
script_to_watch: Script,
|
script_to_watch: Script,
|
||||||
execution_params: ExecutionParams,
|
|
||||||
) -> Result<()> {
|
) -> 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 { "" });
|
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(
|
.wait_for_transaction_finality(
|
||||||
state3.tx_lock.txid(),
|
state3.tx_lock.txid(),
|
||||||
state3.tx_lock.script_pubkey(),
|
state3.tx_lock.script_pubkey(),
|
||||||
execution_params,
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@ -224,7 +223,6 @@ async fn run_until_internal(
|
|||||||
.wait_for_transaction_finality(
|
.wait_for_transaction_finality(
|
||||||
txid,
|
txid,
|
||||||
state3.redeem_address.script_pubkey(),
|
state3.redeem_address.script_pubkey(),
|
||||||
execution_params,
|
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@ -413,7 +411,7 @@ async fn run_until_internal(
|
|||||||
let txid = bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?;
|
let txid = bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?;
|
||||||
|
|
||||||
bitcoin_wallet
|
bitcoin_wallet
|
||||||
.wait_for_transaction_finality(txid, punish_script_pubkey, execution_params)
|
.wait_for_transaction_finality(txid, punish_script_pubkey)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Result::<_, anyhow::Error>::Ok(txid)
|
Result::<_, anyhow::Error>::Ok(txid)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::bitcoin::Wallet;
|
use crate::bitcoin::Wallet;
|
||||||
use crate::database::{Database, Swap};
|
use crate::database::{Database, Swap};
|
||||||
use crate::execution_params::ExecutionParams;
|
|
||||||
use crate::protocol::bob::BobState;
|
use crate::protocol::bob::BobState;
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -13,7 +12,6 @@ pub struct SwapNotCancelledYet(Uuid);
|
|||||||
pub async fn refund(
|
pub async fn refund(
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
state: BobState,
|
state: BobState,
|
||||||
execution_params: ExecutionParams,
|
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Database,
|
db: Database,
|
||||||
force: bool,
|
force: bool,
|
||||||
@ -41,9 +39,7 @@ pub async fn refund(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
state4
|
state4.refund_btc(bitcoin_wallet.as_ref()).await?;
|
||||||
.refund_btc(bitcoin_wallet.as_ref(), execution_params)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let state = BobState::BtcRefunded(state4);
|
let state = BobState::BtcRefunded(state4);
|
||||||
let db_state = state.clone().into();
|
let db_state = state.clone().into();
|
||||||
|
@ -2,7 +2,6 @@ use crate::bitcoin::{
|
|||||||
self, current_epoch, CancelTimelock, ExpiredTimelocks, PunishTimelock, Transaction, TxCancel,
|
self, current_epoch, CancelTimelock, ExpiredTimelocks, PunishTimelock, Transaction, TxCancel,
|
||||||
TxLock, Txid,
|
TxLock, Txid,
|
||||||
};
|
};
|
||||||
use crate::execution_params::ExecutionParams;
|
|
||||||
use crate::monero;
|
use crate::monero;
|
||||||
use crate::monero::{monero_private_key, InsufficientFunds, TransferProof};
|
use crate::monero::{monero_private_key, InsufficientFunds, TransferProof};
|
||||||
use crate::monero_ext::ScalarExt;
|
use crate::monero_ext::ScalarExt;
|
||||||
@ -531,11 +530,7 @@ impl State4 {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refund_btc(
|
pub async fn refund_btc(&self, bitcoin_wallet: &bitcoin::Wallet) -> Result<()> {
|
||||||
&self,
|
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
|
||||||
execution_params: ExecutionParams,
|
|
||||||
) -> Result<()> {
|
|
||||||
let tx_cancel =
|
let tx_cancel =
|
||||||
bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.A, self.b.public());
|
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);
|
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?;
|
let txid = bitcoin_wallet.broadcast(signed_tx_refund, "refund").await?;
|
||||||
|
|
||||||
bitcoin_wallet
|
bitcoin_wallet
|
||||||
.wait_for_transaction_finality(
|
.wait_for_transaction_finality(txid, self.refund_address.script_pubkey())
|
||||||
txid,
|
|
||||||
self.refund_address.script_pubkey(),
|
|
||||||
execution_params,
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -382,9 +382,7 @@ async fn run_until_internal(
|
|||||||
bail!("Internal error: canceled state reached before cancel timelock was expired");
|
bail!("Internal error: canceled state reached before cancel timelock was expired");
|
||||||
}
|
}
|
||||||
ExpiredTimelocks::Cancel => {
|
ExpiredTimelocks::Cancel => {
|
||||||
state
|
state.refund_btc(bitcoin_wallet.as_ref()).await?;
|
||||||
.refund_btc(bitcoin_wallet.as_ref(), execution_params)
|
|
||||||
.await?;
|
|
||||||
BobState::BtcRefunded(state)
|
BobState::BtcRefunded(state)
|
||||||
}
|
}
|
||||||
ExpiredTimelocks::Punish => BobState::BtcPunished {
|
ExpiredTimelocks::Punish => BobState::BtcPunished {
|
||||||
|
@ -48,7 +48,6 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
|||||||
let bob_state = bob::refund(
|
let bob_state = bob::refund(
|
||||||
bob_swap.swap_id,
|
bob_swap.swap_id,
|
||||||
bob_swap.state,
|
bob_swap.state,
|
||||||
bob_swap.execution_params,
|
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
false,
|
false,
|
||||||
|
@ -39,7 +39,6 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
|||||||
bob::refund(
|
bob::refund(
|
||||||
bob_swap.swap_id,
|
bob_swap.swap_id,
|
||||||
bob_swap.state,
|
bob_swap.state,
|
||||||
bob_swap.execution_params,
|
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
false,
|
false,
|
||||||
|
@ -36,7 +36,6 @@ async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() {
|
|||||||
let is_error = bob::refund(
|
let is_error = bob::refund(
|
||||||
bob_swap.swap_id,
|
bob_swap.swap_id,
|
||||||
bob_swap.state,
|
bob_swap.state,
|
||||||
bob_swap.execution_params,
|
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
true,
|
true,
|
||||||
|
@ -605,6 +605,7 @@ async fn init_test_wallets(
|
|||||||
let btc_wallet = swap::bitcoin::Wallet::new(
|
let btc_wallet = swap::bitcoin::Wallet::new(
|
||||||
electrum_rpc_url,
|
electrum_rpc_url,
|
||||||
bitcoin::Network::Regtest,
|
bitcoin::Network::Regtest,
|
||||||
|
execution_params.bitcoin_finality_confirmations,
|
||||||
datadir,
|
datadir,
|
||||||
seed.derive_extended_private_key(bitcoin::Network::Regtest)
|
seed.derive_extended_private_key(bitcoin::Network::Regtest)
|
||||||
.expect("Could not create extended private key from seed"),
|
.expect("Could not create extended private key from seed"),
|
||||||
|
Loading…
Reference in New Issue
Block a user