Skip to content

Commit

Permalink
fix: also sweep from currency specific wallets
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Sep 13, 2023
1 parent 156c964 commit c0c94fa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
7 changes: 5 additions & 2 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,11 @@ impl BitcoinCoreApi for BitcoinCore {
.rpc
.list_transactions(Some(SWEEP_ADDRESS), Some(DEFAULT_MAX_TX_COUNT), None, None)?
.into_iter()
.filter_map(|tx| tx.info.blockheight)
// we want to return None if there is no sweep tx for full nodes or new
// pruned nodes and we should return an error if any tx is still in the mempool
.map(|tx| tx.info.blockheight.ok_or(Error::ConfirmationError))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.min())
}

Expand Down Expand Up @@ -1080,7 +1084,6 @@ impl BitcoinCoreApi for BitcoinCore {
}

log::info!("Sweeping {} from {} utxos", amount, utxos.len());

let mut outputs = serde_json::Map::<String, serde_json::Value>::new();
outputs.insert(address.to_string(), serde_json::Value::from(amount.to_btc()));

Expand Down
4 changes: 2 additions & 2 deletions vault/src/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub async fn add_keys_from_past_issue_request(
// privkey
let btc_end_height = bitcoin_core.get_block_count().await? as usize;
let btc_pruned_start_height = bitcoin_core.get_pruned_height().await? as usize;
let btc_max_sweep_height = bitcoin_core.get_last_sweep_height().await?;
let btc_last_sweep_height = bitcoin_core.get_last_sweep_height().await?;

let issues = issue_requests.clone().into_iter().map(|(_key, issue)| issue).collect();
scanning_status.update(issues, btc_end_height);
Expand All @@ -184,7 +184,7 @@ pub async fn add_keys_from_past_issue_request(
.into_iter()
.filter_map(|(_, request)| {
// only import if address is AFTER last sweep height and BEFORE current pruning height
if btc_max_sweep_height.is_some_and(|sweep_height| request.btc_height > sweep_height)
if btc_last_sweep_height.map_or(true, |sweep_height| request.btc_height > sweep_height)
&& (request.btc_height as usize) < btc_pruned_start_height
{
Some(request.btc_address.to_address(bitcoin_core.network()).ok()?)
Expand Down
1 change: 0 additions & 1 deletion vault/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![recursion_limit = "256"]
#![feature(array_zip)]
#![feature(is_some_and)]

mod cancellation;
mod cli;
Expand Down
14 changes: 11 additions & 3 deletions vault/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ impl VaultIdManager {
btc_rpc_shared.import_private_key(&private_key, false)?;
}

// only sweep if using pruned node and there is no sweep tx yet to shared-v2
if btc_rpc_shared.get_pruned_height().await? != 0 && self.btc_rpc_shared_wallet_v2.get_last_sweep_height().await?.is_none() {
// sweep to old shared wallet which will then sweep again to the v2 wallet
let shared_wallet_address = btc_rpc_shared.get_new_address().await?;
let txid = btc_rpc.sweep_funds(shared_wallet_address).await?;
tracing::info!("Sent sweep tx: {txid}");
}

tracing::info!("Initializing metrics...");
let metrics = PerCurrencyMetrics::new(&vault_id);
let data = VaultData {
Expand Down Expand Up @@ -435,7 +443,7 @@ impl VaultIdManager {
pub struct VaultService {
btc_parachain: InterBtcParachain,
btc_rpc_master_wallet: DynBitcoinCoreApi,
btc_rpc_shared_wallet: DynBitcoinCoreApi,
btc_rpc_shared_wallet_v2: DynBitcoinCoreApi,
config: VaultServiceConfig,
monitoring_config: MonitoringConfig,
shutdown: ShutdownSender,
Expand Down Expand Up @@ -547,7 +555,7 @@ impl VaultService {
Self {
btc_parachain: btc_parachain.clone(),
btc_rpc_master_wallet: btc_rpc_master_wallet.clone(),
btc_rpc_shared_wallet: btc_rpc_shared_wallet.clone(),
btc_rpc_shared_wallet_v2: btc_rpc_shared_wallet_v2.clone(),
config,
monitoring_config,
shutdown,
Expand Down Expand Up @@ -673,7 +681,7 @@ impl VaultService {

tracing::info!("Adding keys from past issues...");
issue::add_keys_from_past_issue_request(
&self.btc_rpc_shared_wallet,
&self.btc_rpc_shared_wallet_v2,
&self.btc_parachain,
&self.vault_id_manager.db,
)
Expand Down

0 comments on commit c0c94fa

Please sign in to comment.