Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/bin/taker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn main() -> Result<(), TakerError> {

println!("{txid}");

taker.get_wallet_mut().sync_no_fail();
taker.get_wallet_mut().sync_and_save()?;
}
Commands::FetchOffers => {
let all_offers = {
Expand Down
4 changes: 2 additions & 2 deletions src/maker/api2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl Maker {
config.write_to_file(&data_dir.join("config.toml"))?;

log::info!("Initializing wallet sync");
wallet.sync()?;
wallet.sync_and_save()?;
log::info!("Completed wallet sync");

let network_port = config.network_port;
Expand Down Expand Up @@ -483,7 +483,7 @@ impl Maker {
let mut wallet = self.wallet.write()?;

// Sync wallet to get latest UTXO state
wallet.sync()?;
wallet.sync_and_save()?;

let balance = wallet.get_balances()?;
if balance.spendable < connection_state.swap_amount {
Expand Down
2 changes: 1 addition & 1 deletion src/maker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl Maker {
.get(funding_output_index as usize)
.expect("funding output expected at this index");

self.wallet.write()?.sync_no_fail();
self.wallet.write()?.sync_and_save()?;

let receiver_contract_tx = create_receivers_contract_tx(
OutPoint {
Expand Down
5 changes: 2 additions & 3 deletions src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn handle_request<M: MakerRpc>(maker: &Arc<M>, socket: &mut TcpStream) -> Result

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

maker.wallet().write()?.sync_no_fail();
maker.wallet().write()?.sync_and_save()?;

RpcMsgResp::SendToAddressResp(txid.to_string())
}
Expand Down Expand Up @@ -136,11 +136,10 @@ fn handle_request<M: MakerRpc>(maker: &Arc<M>, socket: &mut TcpStream) -> Result
RpcMsgReq::SyncWallet => {
log::info!("Initializing wallet sync");
let mut wallet = maker.wallet().write()?;
if let Err(e) = wallet.sync() {
if let Err(e) = wallet.sync_and_save() {
RpcMsgResp::ServerError(format!("{e:?}"))
} else {
log::info!("Completed wallet sync");
wallet.save_to_disk()?;
RpcMsgResp::Pong
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/maker/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn setup_fidelity_bond(maker: &Maker, maker_address: &str) -> Result<FidelityPro
while !maker.shutdown.load(Relaxed) {
sleep_multiplier += 1;
// sync the wallet
maker.get_wallet().write()?.sync_no_fail();
maker.get_wallet().write()?.sync_and_save()?;

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

// sync and save the wallet data to disk
maker.get_wallet().write()?.sync_no_fail();
maker.get_wallet().read()?.save_to_disk()?;
maker.get_wallet().write()?.sync_and_save()?;
break;
}
}
Expand All @@ -217,7 +216,7 @@ fn check_swap_liquidity(maker: &Maker) -> Result<(), MakerError> {
let mut sleep_duration = 0;
let addr = maker.get_wallet().write()?.get_next_external_address()?;
while !maker.shutdown.load(Relaxed) {
maker.get_wallet().write()?.sync_no_fail();
maker.get_wallet().write()?.sync_and_save()?;
let offer_max_size = maker.get_wallet().read()?.store.offer_maxsize;

let min_required = maker.config.min_swap_amount;
Expand Down Expand Up @@ -501,9 +500,8 @@ pub fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
maker.thread_pool.join_all_threads()?;

log::info!("Shutdown wallet sync initiated.");
maker.get_wallet().write()?.sync_no_fail();
maker.get_wallet().write()?.sync_and_save()?;
log::info!("Shutdown wallet syncing completed.");
maker.get_wallet().read()?.save_to_disk()?;
log::info!("Wallet file saved to disk.");
log::info!("Maker Server is shut down successfully.");
Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/maker/server2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn setup_fidelity_bond_taproot(
while !maker.shutdown.load(Relaxed) {
sleep_multiplier += 1;
// sync the wallet
maker.wallet().write()?.sync_no_fail();
maker.wallet().write()?.sync_and_save()?;

let fidelity_result = maker.wallet().write()?.create_fidelity(
amount,
Expand Down Expand Up @@ -225,8 +225,7 @@ fn setup_fidelity_bond_taproot(
};

// sync and save the wallet data to disk
maker.wallet().write()?.sync_no_fail();
maker.wallet().read()?.save_to_disk()?;
maker.wallet().write()?.sync_and_save()?;

// Store the fidelity proof in maker
*maker.highest_fidelity_proof.write()? = Some(highest_proof.clone());
Expand Down
4 changes: 1 addition & 3 deletions src/taker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2163,9 +2163,7 @@ impl Taker {
}
}

self.wallet.sync_no_fail();

self.wallet.save_to_disk()?;
self.wallet.sync_and_save()?;

self.clear_ongoing_swaps();

Expand Down
2 changes: 1 addition & 1 deletion src/taker/api2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl Taker {
};

log::info!("Initializing wallet sync...");
wallet.sync()?;
wallet.sync_and_save()?;
log::info!("Completed wallet sync");

Ok(Self {
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ impl Wallet {
completed_swapcoins.len()
);

self.sync_no_fail();
self.sync_and_save()?;

for (multisig_redeemscript, _) in completed_swapcoins {
let utxo_info = self
Expand Down Expand Up @@ -2128,7 +2128,7 @@ impl Wallet {
}
let reedem_script = incoming.get_multisig_redeemscript();
let next_internal = &self.get_next_internal_addresses(1)?[0];
self.sync()?;
self.sync_and_save()?;

let hashlock_spend =
self.create_hashlock_spend(&incoming, next_internal, MIN_FEE_RATE)?;
Expand Down Expand Up @@ -2159,7 +2159,7 @@ impl Wallet {
let reedem_script = outgoing.get_multisig_redeemscript();
let timelock = outgoing.get_timelock()?;
let next_internal = &self.get_next_internal_addresses(1)?[0];
self.sync()?;
self.sync_and_save()?;

let timelock_spend =
self.create_timelock_spend(&outgoing, next_internal, MIN_FEE_RATE)?;
Expand Down
3 changes: 1 addition & 2 deletions src/wallet/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ impl Wallet {
store,
store_enc_material: restored_enc_material,
};
tmp_wallet.sync()?;
tmp_wallet.save_to_disk()?;
tmp_wallet.sync_and_save()?;

Ok(tmp_wallet)
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Wallet {
)?;

let txid = self.send_tx(&tx).unwrap();
self.sync_no_fail();
self.sync_and_save()?;
println!("Send to Address TxId: {txid}");

Ok(txid)
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Wallet {
"Initializing wallet sync and save for {:?}",
&self.store.file_name
);
self.sync()?;
self.sync_no_fail();
self.save_to_disk()?;
log::info!(
"Completed wallet sync and save for {:?}",
Expand All @@ -94,7 +94,7 @@ impl Wallet {
}

/// Sync the wallet with the configured Bitcoin Core RPC.
pub fn sync(&mut self) -> Result<(), WalletError> {
fn sync(&mut self) -> Result<(), WalletError> {
// Create or load the watch-only bitcoin core wallet
let wallet_name = &self.store.file_name;
if self.rpc.list_wallets()?.contains(wallet_name) {
Expand Down Expand Up @@ -175,8 +175,8 @@ impl Wallet {

/// Keep retrying sync until success and log failure.
// This is useful to handle transient RPC errors.
pub fn sync_no_fail(&mut self) {
while let Err(e) = self.sync_and_save() {
fn sync_no_fail(&mut self) {
while let Err(e) = self.sync() {
log::error!("Blockchain sync failed. Retrying. | {e:?}");
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ fn test_fidelity_spending() {
};

generate_blocks(bitcoind, 1);
maker.get_wallet().write().unwrap().sync_no_fail();
maker.get_wallet().write().unwrap().sync_and_save().unwrap();

// Make fidelity bond expire
while (bitcoind.client.get_block_count().unwrap() as u32) < short_timelock_height {
generate_blocks(bitcoind, 10);
}
generate_blocks(bitcoind, 5);
maker.get_wallet().write().unwrap().sync_no_fail();
maker.get_wallet().write().unwrap().sync_and_save().unwrap();

// Assert UTXO shows up in list and track the specific fidelity UTXO
let fidelity_utxo_info = {
Expand Down Expand Up @@ -385,7 +385,7 @@ fn test_fidelity_spending() {
Ok(Some(tx)) => {
bitcoind.client.send_raw_transaction(&tx).unwrap();
generate_blocks(bitcoind, 1);
maker.get_wallet().write().unwrap().sync_no_fail();
maker.get_wallet().write().unwrap().sync_and_save().unwrap();
log::info!("✅ Regular transaction #{} completed successfully", i + 1);
}
Ok(None) => {
Expand Down Expand Up @@ -415,7 +415,7 @@ fn test_fidelity_spending() {
}

generate_blocks(bitcoind, 1);
maker.get_wallet().write().unwrap().sync_no_fail();
maker.get_wallet().write().unwrap().sync_and_save().unwrap();

// Verify the specific UTXO is now consumed and bond is spent
{
Expand Down Expand Up @@ -463,7 +463,7 @@ fn test_fidelity_spending() {
};

generate_blocks(bitcoind, 1);
maker.get_wallet().write().unwrap().sync_no_fail();
maker.get_wallet().write().unwrap().sync_and_save().unwrap();

{
let wallet = maker.get_wallet().read().unwrap();
Expand Down
12 changes: 6 additions & 6 deletions tests/taproot_hashlock_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {

// Sync wallets after setup
for maker in &taproot_makers {
maker.wallet().write().unwrap().sync().unwrap();
maker.wallet().write().unwrap().sync_and_save().unwrap();
}

// Get balances before swap
Expand Down Expand Up @@ -129,7 +129,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {
// so contract balance may already be 0 if recovery succeeded
info!("⛏️ Mining blocks to confirm contracts...");
generate_blocks(bitcoind, 2);
taproot_taker.get_wallet_mut().sync().unwrap();
taproot_taker.get_wallet_mut().sync_and_save().unwrap();

info!("📊 Taker balance after failed swap (recovery already attempted):");
let taker_balances = taproot_taker.get_wallet().get_balances().unwrap();
Expand All @@ -152,7 +152,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {
// Mine blocks to confirm any recovery transactions
info!("⛏️ Mining blocks to confirm recovery transactions...");
generate_blocks(bitcoind, 2);
taproot_taker.get_wallet_mut().sync().unwrap();
taproot_taker.get_wallet_mut().sync_and_save().unwrap();

info!("📊 Taker balance after recovery:");
let taker_balances_after = taproot_taker.get_wallet().get_balances().unwrap();
Expand Down Expand Up @@ -184,7 +184,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {
// Verify maker recovered their incoming contract via hashlock
let maker_balance_after = {
let mut wallet = taproot_makers[0].wallet().write().unwrap();
wallet.sync().unwrap();
wallet.sync_and_save().unwrap();
let balances = wallet.get_balances().unwrap();
info!(
"📊 Maker balance after hashlock recovery: Regular: {}, Spendable: {}",
Expand Down Expand Up @@ -248,7 +248,7 @@ fn fund_taproot_makers(
}

generate_blocks(bitcoind, 1);
wallet.sync().unwrap();
wallet.sync_and_save().unwrap();

// Verify balances
let balances = wallet.get_balances().unwrap();
Expand Down Expand Up @@ -277,7 +277,7 @@ fn fund_taproot_taker(
}

generate_blocks(bitcoind, 1);
taker.get_wallet_mut().sync().unwrap();
taker.get_wallet_mut().sync_and_save().unwrap();

// Verify balances
let balances = taker.get_wallet().get_balances().unwrap();
Expand Down
10 changes: 5 additions & 5 deletions tests/taproot_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn test_taproot_coinswap() {

// Sync wallets after setup to ensure fidelity bonds are accounted for
for maker in &taproot_makers {
maker.wallet().write().unwrap().sync().unwrap();
maker.wallet().write().unwrap().sync_and_save().unwrap();
}

// Get the actual spendable balances AFTER fidelity bond creation
Expand Down Expand Up @@ -160,15 +160,15 @@ fn test_taproot_coinswap() {
log::info!("All taproot coinswaps processed successfully. Transaction complete.");

// Sync wallets and verify results
taproot_taker.get_wallet_mut().sync().unwrap();
taproot_taker.get_wallet_mut().sync_and_save().unwrap();

// Mine a block to confirm the sweep transactions
generate_blocks(bitcoind, 1);

// Synchronize each taproot maker's wallet multiple times to ensure all UTXOs are discovered
for maker in taproot_makers.iter() {
let mut wallet = maker.wallet().write().unwrap();
wallet.sync().unwrap();
wallet.sync_and_save().unwrap();
}

// Verify swap results
Expand Down Expand Up @@ -204,7 +204,7 @@ fn fund_taproot_makers(
}

generate_blocks(bitcoind, 1);
wallet.sync().unwrap();
wallet.sync_and_save().unwrap();

// Verify balances - for now just check regular balance
let balances = wallet.get_balances().unwrap();
Expand Down Expand Up @@ -242,7 +242,7 @@ fn fund_taproot_taker(
}

generate_blocks(bitcoind, 1);
taker.get_wallet_mut().sync().unwrap();
taker.get_wallet_mut().sync_and_save().unwrap();

// Verify balances
let balances = taker.get_wallet().get_balances().unwrap();
Expand Down
Loading
Loading