Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl Maker {
}
config.write_to_file(&data_dir.join("config.toml"))?;

log::info!("Sync at:----Maker init----");
wallet.sync_and_save()?;

let network_port = config.network_port;
Expand Down
11 changes: 8 additions & 3 deletions src/maker/api2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@ impl Maker {

config.write_to_file(&data_dir.join("config.toml"))?;

log::info!("Initializing wallet sync");
log::info!("Sync at:----Maker init----");
wallet.sync_and_save()?;
log::info!("Completed wallet sync");

let network_port = config.network_port;

Expand Down Expand Up @@ -491,6 +490,7 @@ impl Maker {
let mut wallet = self.wallet.write()?;

// Sync wallet to get latest UTXO state
log::info!("Sync at:----verify_and_process_senders_contract----");
wallet.sync_and_save()?;

let balance = wallet.get_balances()?;
Expand Down Expand Up @@ -954,7 +954,8 @@ impl Maker {
self.config.network_port,
incoming_txid
);
wallet.save_to_disk()?;
// Sync to update utxo_cache with the swept UTXO so it's classified as SweptCoin
wallet.sync_and_save()?;
}

let outgoing_privkey_handover_message = PrivateKeyHandover {
Expand Down Expand Up @@ -991,6 +992,10 @@ impl Maker {
// Get the contract output value (assuming it's the first output)
let contract_value = incoming_contract_tx.output[0].value;

// sync wallet to update utxo cache before getting destination address
// ensuring we get a fresh address different from any change address
self.wallet.write()?.sync_and_save()?;

// Get a fresh internal address for the destination
let destination_address = {
let wallet = self.wallet.read()?;
Expand Down
3 changes: 3 additions & 0 deletions src/maker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl Maker {
.get(funding_output_index as usize)
.expect("funding output expected at this index");

log::info!("Sync at:----handle_proof_of_funding----");
self.wallet.write()?.sync_and_save()?;

let receiver_contract_tx = create_receivers_contract_tx(
Expand Down Expand Up @@ -706,6 +707,7 @@ impl Maker {
}
}

log::info!("Sync at:----handle_private_key_handover----");
self.wallet.write()?.sync_and_save()?;

log::info!("Successfully Completed Coinswap");
Expand All @@ -725,6 +727,7 @@ impl Maker {
swept_txids
);
}
log::info!("Sync at:----sweep_after_successful_coinswap----");
self.wallet.write()?.sync_and_save()?;
// For tests, terminate the maker at this stage.
#[cfg(feature = "integration-test")]
Expand Down
1 change: 1 addition & 0 deletions src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fn handle_request<M: MakerRpc>(maker: &Arc<M>, socket: &mut TcpStream) -> Result

let txid = maker.wallet().read()?.send_tx(&tx)?;

log::info!("Sync at:----handle_request----");
maker.wallet().write()?.sync_and_save()?;

RpcMsgResp::SendToAddressResp(txid.to_string())
Expand Down
7 changes: 4 additions & 3 deletions src/maker/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ fn setup_fidelity_bond(maker: &Maker, maker_address: &str) -> Result<FidelityPro
while !maker.shutdown.load(Relaxed) {
sleep_multiplier += 1;
// sync the wallet
log::info!("Sync at:----setup_fidelity_bond----");
maker.get_wallet().write()?.sync_and_save()?;

let fidelity_result = maker.get_wallet().write()?.create_fidelity(
Expand Down Expand Up @@ -358,6 +359,7 @@ fn setup_fidelity_bond(maker: &Maker, maker_address: &str) -> Result<FidelityPro
*proof = Some(highest_proof);

// sync and save the wallet data to disk
log::info!(" Sync at end:----setup_fidelity_bond----");
maker.get_wallet().write()?.sync_and_save()?;
break;
}
Expand All @@ -381,6 +383,7 @@ fn check_swap_liquidity(maker: &Maker) -> Result<(), MakerError> {
.write()?
.get_next_external_address(AddressType::P2WPKH)?;
while !maker.shutdown.load(Relaxed) {
log::info!("Sync at:----check_swap_liquidity----");
maker.get_wallet().write()?.sync_and_save()?;
let offer_max_size = maker.get_wallet().read()?.store.offer_maxsize;

Expand Down Expand Up @@ -662,10 +665,8 @@ pub fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
log::info!("[{network_port}] Maker is shutting down.");
maker.thread_pool.join_all_threads()?;

log::info!("Shutdown wallet sync initiated.");
log::info!("sync at:----Shutdown wallet----");
maker.get_wallet().write()?.sync_and_save()?;
log::info!("Shutdown wallet syncing completed.");
log::info!("Wallet file saved to disk.");
log::info!("Maker Server is shut down successfully.");
Ok(())
}
18 changes: 10 additions & 8 deletions src/maker/server2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ fn setup_fidelity_bond_taproot(
while !maker.shutdown.load(Relaxed) {
sleep_multiplier += 1;
// sync the wallet
log::info!("Sync at:----setup_fidelity_bond----");
maker.wallet().write()?.sync_and_save()?;

let fidelity_result = maker.wallet().write()?.create_fidelity(
Expand Down Expand Up @@ -384,6 +385,7 @@ fn setup_fidelity_bond_taproot(
};

// sync and save the wallet data to disk
log::info!("Sync at end:----setup_fidelity_bond----");
maker.wallet().write()?.sync_and_save()?;

// Store the fidelity proof in maker
Expand All @@ -401,6 +403,10 @@ fn setup_fidelity_bond_taproot(

/// Checks swap liquidity for taproot swaps
fn check_swap_liquidity_taproot(maker: &Maker) -> Result<(), MakerError> {
{
let mut wallet = maker.wallet().write()?;
wallet.refresh_offer_maxsize_cache()?;
}
let wallet_read = maker.wallet().read()?;
let balances = wallet_read.get_balances()?;

Expand Down Expand Up @@ -500,11 +506,7 @@ fn handle_client_taproot(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(
);
let message = match serde_cbor::from_slice::<TakerToMakerMessage>(&message_bytes) {
Ok(msg) => {
log::debug!(
"[{}] Successfully decoded message: {:?}",
maker.config.network_port,
msg
);
log::info!("[{}] <=== {}", maker.config.network_port, msg);
msg
}
Err(e) => {
Expand Down Expand Up @@ -596,7 +598,7 @@ fn handle_client_taproot(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(
}
// Send response if we have one (only applies to taker messages)
if let Some(response_msg) = response {
log::info!("[{}] Sending response", maker.config.network_port,);
log::info!("[{}] ===> {}", maker.config.network_port, response_msg);

if let Err(e) = send_message(stream, &response_msg) {
log::error!(
Expand Down Expand Up @@ -778,8 +780,6 @@ pub fn start_maker_server_taproot(maker: Arc<Maker>) -> Result<(), MakerError> {
while !maker.shutdown.load(Relaxed) {
match listener.accept() {
Ok((mut stream, _)) => {
log::info!("[{network_port}] Received incoming connection");

if let Err(e) = handle_client_taproot(&maker, &mut stream) {
log::error!("[{network_port}] Error Handling client request {e:?}");
}
Expand All @@ -803,6 +803,8 @@ pub fn start_maker_server_taproot(maker: Arc<Maker>) -> Result<(), MakerError> {
// Join all threads
maker.thread_pool.join_all_threads()?;

log::info!("sync at:----Taproot server shutdown----");
maker.wallet().write()?.sync_and_save()?;
log::info!(
"[{}] Taproot maker server stopped",
maker.config.network_port
Expand Down
4 changes: 4 additions & 0 deletions src/taker/api2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,10 @@ impl Taker {
.checked_sub(fee)
.ok_or_else(|| TakerError::General("Insufficient amount for fee".to_string()))?;

// sync wallet to update utxo cache before getting destination address
// ensuring we get a fresh address different from any change address
self.wallet.sync_and_save()?;

let destination_address = self
.wallet
.get_next_internal_addresses(1, AddressType::P2TR)
Expand Down
22 changes: 19 additions & 3 deletions src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! In the future, takers might adopt alternative synchronization methods, such as lightweight wallet solutions.

use std::{
convert::TryFrom, fmt::Display, path::PathBuf, str::FromStr, thread, thread::sleep,
cmp::max, convert::TryFrom, fmt::Display, path::PathBuf, str::FromStr, thread, thread::sleep,
time::Duration,
};

Expand Down Expand Up @@ -292,6 +292,16 @@ impl Wallet {
Some(wallet_birthday),
&store_enc_material,
)?;
let last_synced_height_val = match store.last_synced_height {
Some(height) => height.to_string(),
None => "None".to_string(),
};

log::info!(
"Wallet birth_height = {}, wallet last_sync_height = {}",
wallet_birthday,
last_synced_height_val
);

Ok(Self {
rpc,
Expand Down Expand Up @@ -1270,8 +1280,8 @@ impl Wallet {

/// Refreshes the offer maximum size cache based on the current wallet's unspent transaction outputs (UTXOs).
pub(crate) fn refresh_offer_maxsize_cache(&mut self) -> Result<(), WalletError> {
let balance = self.get_balances()?.spendable;
self.store.offer_maxsize = balance.to_sat();
let Balances { swap, regular, .. } = self.get_balances()?;
self.store.offer_maxsize = max(swap, regular).to_sat();
Ok(())
}

Expand Down Expand Up @@ -2483,6 +2493,9 @@ impl Wallet {
witness: Witness::new(), // Will be filled later
};

// sync wallet to update utxo cache before getting destination address
self.sync_and_save()?;

// Get destination address
let destination = self.get_next_internal_addresses(1, AddressType::P2WPKH)?[0].clone();

Expand Down Expand Up @@ -2628,6 +2641,9 @@ impl Wallet {
witness: Witness::new(),
};

// sync wallet to update utxo cache before getting destination address
self.sync_and_save()?;

// Get destination
let destination = self.get_next_internal_addresses(1, AddressType::P2WPKH)?[0].clone();

Expand Down
2 changes: 0 additions & 2 deletions src/wallet/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,6 @@ impl Wallet {
bond.cert_expiry = Some(cert_expiry);
bond.conf_height = Some(conf_height);

self.sync_and_save()?;

Ok(())
}

Expand Down
69 changes: 35 additions & 34 deletions src/wallet/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//!
use std::{convert::TryFrom, thread};

use bitcoind::bitcoincore_rpc::{json::ListUnspentResultEntry, Auth, Client, RpcApi};
use bitcoind::bitcoincore_rpc::{
json::{ListUnspentResultEntry, ScanningDetails},
Auth, Client, RpcApi,
};
use serde_json::{json, Value};

use crate::{utill::HEART_BEAT_INTERVAL, wallet::api::KeychainKind};
Expand Down Expand Up @@ -70,16 +73,10 @@ impl Wallet {
/// This method first synchronizes the wallet with the Bitcoin Core node,
/// then persists the wallet state in the disk.
pub fn sync_and_save(&mut self) -> Result<(), WalletError> {
log::info!(
"Initializing wallet sync and save for {:?}",
&self.store.file_name
);
log::info!("Sync Started for {:?}", &self.store.file_name);
self.sync_no_fail();
self.save_to_disk()?;
log::info!(
"Completed wallet sync and save for {:?}",
&self.store.file_name
);
log::info!("Synced & Saved {:?}", &self.store.file_name);
Ok(())
}

Expand Down Expand Up @@ -130,40 +127,43 @@ impl Wallet {
return Ok(());
}

log::debug!("Importing Wallet spks/descriptors");

self.import_descriptors(&descriptors_to_import, None)?;

// Now run the scan
log::debug!("Initializing TxOut scan. This may take a while.");

// Sometimes in test multiple wallet scans can occur at same time, resulting in error.
// Just retry after 3 sec.
loop {
let last_synced_height = self
.store
.last_synced_height
.unwrap_or(0)
.max(self.store.wallet_birthday.unwrap_or(0));
let node_synced = self.rpc.get_block_count()?;
log::debug!("Re-scanning Blockchain from:{last_synced_height} to:{node_synced}");
match self.rpc.rescan_blockchain(
Some(last_synced_height as usize),
Some(node_synced as usize),
) {
Ok(_) => {
self.store.last_synced_height = Some(node_synced);
break;
}
let last_synced_height = self
.store
.last_synced_height
.unwrap_or(0)
.max(self.store.wallet_birthday.unwrap_or(0));
let node_synced = self.rpc.get_block_count()?;
log::info!("Re-scanning Blockchain from:{last_synced_height} to:{node_synced}");

let _ = self.rpc.rescan_blockchain(
Some(last_synced_height as usize),
Some(node_synced as usize),
);

Err(e) => {
log::warn!("Sync Error, Retrying: {e}");
// Returns when the scanning is completed
loop {
let wallet_info = self.rpc.get_wallet_info()?;
match wallet_info.scanning {
Some(ScanningDetails::Scanning { duration, .. }) => {
// Todo: Show scan progress
log::info!("Scanning for {}s", duration);
thread::sleep(HEART_BEAT_INTERVAL);
continue;
}
Some(ScanningDetails::NotScanning(_)) => {
log::info!("Scanning completed");
break;
}
None => {
log::info!("No scan is in progress or Scanning completed");
break;
}
}
}

self.store.last_synced_height = Some(node_synced);
self.update_utxo_cache(self.get_all_utxo_from_rpc()?);

let max_external_index = self.find_hd_next_index(KeychainKind::External)?;
Expand All @@ -177,6 +177,7 @@ impl Wallet {
fn sync_no_fail(&mut self) {
while let Err(e) = self.sync() {
log::error!("Blockchain sync failed. Retrying. | {e:?}");
thread::sleep(HEART_BEAT_INTERVAL);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ fn test_fidelity() {
log::info!("📊 Verifying balances with both fidelity bonds");
// Verify balances
{
maker.get_wallet().write().unwrap().sync_and_save().unwrap();
let wallet_read = maker.get_wallet().read().unwrap();

let balances = wallet_read.get_balances().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions tests/taproot_hashlock_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {
14999518, // No fund loss (with slight fee variance)
15020989, // 1st Maker completed the swap via hashlock path spending and earned some sats.
15021003, // 1st Maker (with slight fee variance)
15031710, // 2nd Maker (with fee variance after sync fix)
15032496, // 2nd Maker completed the swap via hashlock path spending and earned some sats.
],
"Taproot Maker after hashlock recovery balance check."
Expand All @@ -225,6 +226,8 @@ fn test_taproot_hashlock_recovery_end_to_end() {
18, // No fund gain/lost (with slight fee variance)
21485, // 1st Maker gained fee (with slight variance)
21489, // 1st Maker gained fee after completing the swap via hashlock path spending.
31710, // 2nd Maker gained fee (with fee variance after sync fix)
32192, // 1st Maker gained fee (with fee variance)
32996 // 2nd Maker gained fee after completing the swap via hashlock path spending.
],
"Taproot Maker fee gained by recovering via hashlock"
Expand Down
Loading