Remove automatic bdk wallet sync calls

The bdk wallet was being synced before executing other calls such as getting the balance. This is poor usage of the sync functionality as it potentially long running blocking call. Instead sync is called once after the wallet is initialised.
This commit is contained in:
rishflab 2021-02-10 15:18:45 +11:00
parent 181e7c5096
commit 82d75588ba
3 changed files with 56 additions and 19 deletions

View file

@ -62,7 +62,6 @@ impl Wallet {
} }
pub async fn balance(&self) -> Result<Amount> { pub async fn balance(&self) -> Result<Amount> {
self.sync_wallet().await?;
let balance = self.inner.lock().await.get_balance()?; let balance = self.inner.lock().await.get_balance()?;
Ok(Amount::from_sat(balance)) Ok(Amount::from_sat(balance))
} }
@ -81,7 +80,6 @@ impl Wallet {
} }
pub async fn transaction_fee(&self, txid: Txid) -> Result<Amount> { pub async fn transaction_fee(&self, txid: Txid) -> Result<Amount> {
self.sync_wallet().await?;
let fees = self let fees = self
.inner .inner
.lock() .lock()
@ -111,8 +109,8 @@ impl BuildTxLockPsbt for Wallet {
output_address: Address, output_address: Address,
output_amount: Amount, output_amount: Amount,
) -> Result<PartiallySignedTransaction> { ) -> Result<PartiallySignedTransaction> {
self.sync_wallet().await?;
tracing::debug!("building tx lock"); tracing::debug!("building tx lock");
self.sync_wallet().await?;
let (psbt, _details) = self.inner.lock().await.create_tx( let (psbt, _details) = self.inner.lock().await.create_tx(
bdk::TxBuilder::with_recipients(vec![( bdk::TxBuilder::with_recipients(vec![(
output_address.script_pubkey(), output_address.script_pubkey(),
@ -129,7 +127,6 @@ impl BuildTxLockPsbt for Wallet {
#[async_trait] #[async_trait]
impl SignTxLock for Wallet { impl SignTxLock for Wallet {
async fn sign_tx_lock(&self, tx_lock: TxLock) -> Result<Transaction> { async fn sign_tx_lock(&self, tx_lock: TxLock) -> Result<Transaction> {
self.sync_wallet().await?;
tracing::debug!("signing tx lock"); tracing::debug!("signing tx lock");
let psbt = PartiallySignedTransaction::from(tx_lock); let psbt = PartiallySignedTransaction::from(tx_lock);
let (signed_psbt, finalized) = self.inner.lock().await.sign(psbt, None)?; let (signed_psbt, finalized) = self.inner.lock().await.sign(psbt, None)?;
@ -264,7 +261,6 @@ impl WaitForTransactionFinality for Wallet {
let mut interval = interval(execution_params.bitcoin_avg_block_time / 4); let mut interval = interval(execution_params.bitcoin_avg_block_time / 4);
loop { loop {
tracing::debug!("syncing wallet");
let tx_block_height = self.transaction_block_height(txid).await; let tx_block_height = self.transaction_block_height(txid).await;
let block_height = self.get_block_height().await; let block_height = self.get_block_height().await;
let confirmations = block_height - tx_block_height; let confirmations = block_height - tx_block_height;

View file

@ -356,6 +356,12 @@ async fn init_wallets(
bitcoin_wallet_data_dir, bitcoin_wallet_data_dir,
) )
.await?; .await?;
bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync btc wallet");
let bitcoin_balance = bitcoin_wallet.balance().await?; let bitcoin_balance = bitcoin_wallet.balance().await?;
info!( info!(
"Connection to Bitcoin wallet succeeded, balance: {}", "Connection to Bitcoin wallet succeeded, balance: {}",

View file

@ -24,7 +24,7 @@ use swap::{
}; };
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::{clients::Cli, Container, Docker, RunArgs}; use testcontainers::{clients::Cli, Container, Docker, RunArgs};
use tokio::task::JoinHandle; use tokio::{task::JoinHandle, time::sleep};
use tracing_core::dispatcher::DefaultGuard; use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer; use tracing_log::LogTracer;
use url::Url; use url::Url;
@ -169,6 +169,11 @@ impl TestContext {
pub async fn assert_alice_redeemed(&self, state: AliceState) { pub async fn assert_alice_redeemed(&self, state: AliceState) {
assert!(matches!(state, AliceState::BtcRedeemed)); assert!(matches!(state, AliceState::BtcRedeemed));
self.alice_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
assert_eq!( assert_eq!(
btc_balance_after_swap, btc_balance_after_swap,
@ -188,6 +193,11 @@ impl TestContext {
pub async fn assert_alice_refunded(&self, state: AliceState) { pub async fn assert_alice_refunded(&self, state: AliceState) {
assert!(matches!(state, AliceState::XmrRefunded)); assert!(matches!(state, AliceState::XmrRefunded));
self.alice_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc); assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc);
@ -210,6 +220,11 @@ impl TestContext {
pub async fn assert_alice_punished(&self, state: AliceState) { pub async fn assert_alice_punished(&self, state: AliceState) {
assert!(matches!(state, AliceState::BtcPunished)); assert!(matches!(state, AliceState::BtcPunished));
self.alice_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
assert_eq!( assert_eq!(
btc_balance_after_swap, btc_balance_after_swap,
@ -227,6 +242,11 @@ impl TestContext {
} }
pub async fn assert_bob_redeemed(&self, state: BobState) { pub async fn assert_bob_redeemed(&self, state: BobState) {
self.bob_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let lock_tx_id = if let BobState::XmrRedeemed { tx_lock_id } = state { let lock_tx_id = if let BobState::XmrRedeemed { tx_lock_id } = state {
tx_lock_id tx_lock_id
} else { } else {
@ -260,6 +280,11 @@ impl TestContext {
} }
pub async fn assert_bob_refunded(&self, state: BobState) { pub async fn assert_bob_refunded(&self, state: BobState) {
self.bob_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let lock_tx_id = if let BobState::BtcRefunded(state4) = state { let lock_tx_id = if let BobState::BtcRefunded(state4) = state {
state4.tx_lock_id() state4.tx_lock_id()
} else { } else {
@ -292,6 +317,11 @@ impl TestContext {
} }
pub async fn assert_bob_punished(&self, state: BobState) { pub async fn assert_bob_punished(&self, state: BobState) {
self.bob_bitcoin_wallet
.sync_wallet()
.await
.expect("Could not sync wallet");
let lock_tx_id = if let BobState::BtcPunished { tx_lock_id } = state { let lock_tx_id = if let BobState::BtcPunished { tx_lock_id } = state {
tx_lock_id tx_lock_id
} else { } else {
@ -580,10 +610,10 @@ async fn init_test_wallets(
.await .await
.unwrap(); .unwrap();
let xmr_wallet = Arc::new(swap::monero::Wallet { let xmr_wallet = swap::monero::Wallet {
inner: monero.wallet(name).unwrap().client(), inner: monero.wallet(name).unwrap().client(),
network: monero::Network::default(), network: monero::Network::default(),
}); };
let electrum_rpc_url = { let electrum_rpc_url = {
let input = format!("tcp://@localhost:{}", electrum_rpc_port); let input = format!("tcp://@localhost:{}", electrum_rpc_port);
@ -594,16 +624,14 @@ async fn init_test_wallets(
Url::parse(&input).unwrap() Url::parse(&input).unwrap()
}; };
let btc_wallet = Arc::new( let btc_wallet = swap::bitcoin::Wallet::new(
swap::bitcoin::Wallet::new(
electrum_rpc_url, electrum_rpc_url,
electrum_http_url, electrum_http_url,
bitcoin::Network::Regtest, bitcoin::Network::Regtest,
datadir, datadir,
) )
.await .await
.expect("could not init btc wallet"), .expect("could not init btc wallet");
);
if starting_balances.btc != bitcoin::Amount::ZERO { if starting_balances.btc != bitcoin::Amount::ZERO {
mint( mint(
@ -615,7 +643,14 @@ async fn init_test_wallets(
.expect("could not mint btc starting balance"); .expect("could not mint btc starting balance");
} }
(btc_wallet, xmr_wallet) sleep(Duration::from_secs(5)).await;
btc_wallet
.sync_wallet()
.await
.expect("Could not sync btc wallet");
(Arc::new(btc_wallet), Arc::new(xmr_wallet))
} }
// This is just to keep the containers alive // This is just to keep the containers alive