From d4d42ae3ae8e19d0e2e185b2a19c5eb1aabd437f Mon Sep 17 00:00:00 2001 From: rishflab Date: Wed, 10 Feb 2021 12:31:16 +1100 Subject: [PATCH] Debugging: Checked sub to calculate tx confirmation --- swap/src/bitcoin/timelocks.rs | 13 ++----------- swap/src/bitcoin/wallet.rs | 12 +++++++----- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/swap/src/bitcoin/timelocks.rs b/swap/src/bitcoin/timelocks.rs index c850906a..c0c8da4e 100644 --- a/swap/src/bitcoin/timelocks.rs +++ b/swap/src/bitcoin/timelocks.rs @@ -1,6 +1,5 @@ -use std::ops::{Add, Sub}; - use serde::{Deserialize, Serialize}; +use std::ops::Add; /// Represent a timelock, expressed in relative block height as defined in /// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki). @@ -27,7 +26,7 @@ impl From for u32 { /// after the genesis block. #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)] #[serde(transparent)] -pub struct BlockHeight(u32); +pub struct BlockHeight(pub u32); impl From for u32 { fn from(height: BlockHeight) -> Self { @@ -49,14 +48,6 @@ impl Add for BlockHeight { } } -impl Sub for BlockHeight { - type Output = BlockHeight; - - fn sub(self, rhs: BlockHeight) -> Self::Output { - BlockHeight(self.0 - rhs.0) - } -} - #[derive(Debug, Clone, Copy)] pub enum ExpiredTimelocks { None, diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 7873b7ac..1d75d404 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -264,13 +264,15 @@ impl WaitForTransactionFinality for Wallet { let mut interval = interval(execution_params.bitcoin_avg_block_time / 4); loop { - tracing::debug!("syncing wallet"); let tx_block_height = self.transaction_block_height(txid).await; + tracing::debug!("tx_block_height: {}", tx_block_height.0); let block_height = self.get_block_height().await; - let confirmations = block_height - tx_block_height; - tracing::debug!("confirmations: {:?}", confirmations); - if confirmations >= BlockHeight::new(execution_params.bitcoin_finality_confirmations) { - break; + tracing::debug!("block_height: {}", block_height.0); + if let Some(confirmations) = block_height.0.checked_sub(tx_block_height.0) { + tracing::debug!("confirmations: {:?}", confirmations); + if confirmations >= execution_params.bitcoin_finality_confirmations { + break; + } } interval.tick().await; }