xmr-btc-swap/swap/tests
Raphael f1e5cdfbfe
fix(swap): Monero wallet thread safety (#281)
* add comment to ConfirmationListener

* swap: always wrap monero::Wallet in tokio::sync::Mutex

Before, monero::Wallet wrapped a Mutex<Client>, and locked
the mutex on each operation. This meant releasing the
lock in between operations, even though we rely on the
operations being executed in order.

To remedy this race condition, we wrap monero::Wallet itself
in a mutex, requiring any caller to hold the lock for the duration
of the operation, including any suboperations.

* work on: releasing the lock while waiting for confirmations

Due to the newly introduced thread safety, we are currently holding
lock to the monero wallet while waiting for confirmations
-- since this takes a lot of time, it starves all other tasks
that do anything with the monero wallet.

In this commit I start implementing a change that enables us to release
the lock to the wallet while waiting for confirmations and only acquire it
when necessary.

This breaks with the current system of passing just a generic client
which implements the MoneroWalletRpc trait (which we use to pass a dummy
client for testing).

This commit is the first step towards a small refactor to that system.

* always pass Wallet instead of a MoneroWalletRpc client

By always passing Arc<Mutex<Wallet>> instead of MoneroWalletRpc clients
directly we can allow the wait_for_confirmations functions to lock the
Mutex and access the client when they need to, while releasing the lock
when waiting for the next tick. This stops the current starving of other
tasks waiting for the lock.

Since we use a dummy client for testing, this required adding a generic
parameter to the Wallet. However, since we specify a default type,
this doesn't actually require generic handling anywhere.

* add warning comment to monero::wallet::Wallet::from_dummy

* add timeout when waiting for monero lock during quote

This commit adds a timeout after 60 seconds when trying to acquire
the lock on the monero wallet while making a quote.
Should a timout occur, we return an error.
This makes sure that we get _some_ return value and that
starvation is noticed.

* fix lints, don't keep lock during loop body in wait_for_confirmations

* always immediately drop lock in wait_for_transfer

* fix clippy lints

* open wallet instead of failing when we can't create from keys

When we fail to create a monero wallet from keys, we will now try
to open it instead. I also renamed the method to be more consistent
with Wallet::open_or_create.

These changes are mostly taken from #260.

* improve documentation on monero::Wallet

* use Wallet::open instead of Wallet::Client::open

* use create_from_keys_and_sweep in bob's redeem_xmr

This commit deduplicates logic by using
create_from_keys_and_sweep_to in bob's redeem_xmr
and also adds the create_from_keys_and_sweep_to
method while making create_from_keys_and_sweep a
wrapper around it.

* add error context and improve logging

* fix deadlock in wait_for_confirmation_with, add timout to test
2025-04-24 15:34:01 +02:00
..
harness fix(swap): Monero wallet thread safety (#281) 2025-04-24 15:34:01 +02:00
alice_and_bob_refund_using_cancel_and_refund_command.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_and_bob_refund_using_cancel_then_refund_command.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_manually_punishes_after_bob_dead.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_manually_punishes_after_bob_dead_and_bob_cancels.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_manually_redeems_after_enc_sig_learned.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_punishes_after_restart_bob_dead.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
alice_refunds_after_restart_bob_refunded.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
bdk.sh feat: cargo project at root 2024-08-08 00:49:04 +02:00
concurrent_bobs_after_xmr_lock_proof_sent.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
concurrent_bobs_before_xmr_lock_proof_sent.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
ensure_same_swap_id.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
happy_path.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
happy_path_bob_offline_while_alice_redeems_btc.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
happy_path_restart_alice_after_xmr_locked.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
happy_path_restart_bob_after_xmr_locked.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
happy_path_restart_bob_before_xmr_locked.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
punish.rs feat: cargo project at root 2024-08-08 00:49:04 +02:00
rpc.rs refactor(cli): Refactor RPC server and fix tests 2024-08-28 12:18:58 +02:00