mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-13 08:29:35 -05:00
Merge #144
144: Test refactor r=da-kami a=da-kami This PR is pure refactoring, keeping the logic of the tests we had before. No production code is touched besides re-exports in early commits (no logic changes). In the follow ups improvements will be introduced, that touch the production code as well. All remaining tasks actioned since Friday: - [x] `happy_path_bob _restart` (trivial) - [x] add refund assertions to harnesses (trivial) - [x] convert all refund scenarios currently being tested (trivial) - [x] remove dead test init code once all old tests are converted - [ ] ~~(optional) move alice and bob harness code into separate files~~ -> might action this once re-using test code in production. Out of scope, follow up: - [x] https://github.com/comit-network/xmr-btc-swap/pull/145 - We can do exact assertions for Bob's redeem as well, but have to store Bob's `tx_lock` id in the respective final state. Make `tx_lock` available in `BtcRedeemed` and `BtcPunished` to have better assertions / harmonize test behaviour. - [ ] update the production code to use the `Alice` and `Bob` structs to bundle the params - update tests to use the production struct. - [ ] Re-use test swap setup in production (i.e. `Alice-/BobHarness::new`) to setup the swap. - [ ] add additional tests - [ ] re-try moving the tests from `test` to `src` (if the peer_id was the only problem this should be trivial now - but should be done after the refactor is finished) - [ ] creating new wallets upon restart - [ ] aborting the old event loop after restart Co-authored-by: rishflab <rishflab@hotmail.com> Co-authored-by: Daniel Karzel <daniel@comit.network>
This commit is contained in:
commit
a7f68e4aa1
@ -19,7 +19,15 @@ use crate::{
|
|||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*};
|
pub use self::{
|
||||||
|
amounts::*,
|
||||||
|
event_loop::{EventLoop, EventLoopHandle},
|
||||||
|
message0::Message0,
|
||||||
|
message1::Message1,
|
||||||
|
message2::Message2,
|
||||||
|
state::*,
|
||||||
|
swap::{run_until, swap},
|
||||||
|
};
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
|
@ -88,12 +88,11 @@ pub async fn run_until(
|
|||||||
state: AliceState,
|
state: AliceState,
|
||||||
is_target_state: fn(&AliceState) -> bool,
|
is_target_state: fn(&AliceState) -> bool,
|
||||||
mut event_loop_handle: EventLoopHandle,
|
mut event_loop_handle: EventLoopHandle,
|
||||||
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
monero_wallet: Arc<crate::monero::Wallet>,
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
config: Config,
|
config: Config,
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
db: Database,
|
db: Database,
|
||||||
// TODO: Remove EventLoopHandle!
|
|
||||||
) -> Result<AliceState> {
|
) -> Result<AliceState> {
|
||||||
info!("Current state:{}", state);
|
info!("Current state:{}", state);
|
||||||
if is_target_state(&state) {
|
if is_target_state(&state) {
|
||||||
|
@ -19,8 +19,14 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
amounts::*, message0::Message0, message1::Message1, message2::Message2, message3::Message3,
|
amounts::*,
|
||||||
|
event_loop::{EventLoop, EventLoopHandle},
|
||||||
|
message0::Message0,
|
||||||
|
message1::Message1,
|
||||||
|
message2::Message2,
|
||||||
|
message3::Message3,
|
||||||
state::*,
|
state::*,
|
||||||
|
swap::{run_until, swap},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
|
@ -7,7 +7,9 @@ use tracing::{debug, info};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
bitcoin,
|
||||||
database::{Database, Swap},
|
database::{Database, Swap},
|
||||||
|
monero,
|
||||||
protocol::bob::{self, event_loop::EventLoopHandle, state::*},
|
protocol::bob::{self, event_loop::EventLoopHandle, state::*},
|
||||||
ExpiredTimelocks, SwapAmounts,
|
ExpiredTimelocks, SwapAmounts,
|
||||||
};
|
};
|
||||||
@ -18,8 +20,8 @@ pub async fn swap<R>(
|
|||||||
state: BobState,
|
state: BobState,
|
||||||
event_loop_handle: EventLoopHandle,
|
event_loop_handle: EventLoopHandle,
|
||||||
db: Database,
|
db: Database,
|
||||||
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
monero_wallet: Arc<crate::monero::Wallet>,
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
rng: R,
|
rng: R,
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
) -> Result<BobState>
|
) -> Result<BobState>
|
||||||
@ -69,8 +71,8 @@ pub async fn run_until<R>(
|
|||||||
is_target_state: fn(&BobState) -> bool,
|
is_target_state: fn(&BobState) -> bool,
|
||||||
mut event_loop_handle: EventLoopHandle,
|
mut event_loop_handle: EventLoopHandle,
|
||||||
db: Database,
|
db: Database,
|
||||||
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
monero_wallet: Arc<crate::monero::Wallet>,
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
mut rng: R,
|
mut rng: R,
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
) -> Result<BobState>
|
) -> Result<BobState>
|
||||||
|
@ -1,21 +1,6 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use futures::{
|
|
||||||
future::{join, select},
|
|
||||||
FutureExt,
|
|
||||||
};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, bob};
|
||||||
bitcoin,
|
use tokio::join;
|
||||||
config::Config,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, bob},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
@ -23,108 +8,33 @@ pub mod testutils;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn happy_path() {
|
async fn happy_path() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let alice_swap = alice::swap(
|
||||||
let (
|
alice.state,
|
||||||
monero,
|
alice.event_loop_handle,
|
||||||
testutils::Containers {
|
alice.bitcoin_wallet.clone(),
|
||||||
bitcoind,
|
alice.monero_wallet.clone(),
|
||||||
monerods: _monerods,
|
alice.config,
|
||||||
},
|
alice.swap_id,
|
||||||
) = testutils::init_containers(&cli).await;
|
alice.db,
|
||||||
|
);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let bob_swap = bob::swap(
|
||||||
let btc_alice = bitcoin::Amount::ZERO;
|
bob.state,
|
||||||
let btc_bob = btc_to_swap * 10;
|
bob.event_loop_handle,
|
||||||
|
bob.db,
|
||||||
|
bob.bitcoin_wallet.clone(),
|
||||||
|
bob.monero_wallet.clone(),
|
||||||
|
OsRng,
|
||||||
|
bob.swap_id,
|
||||||
|
);
|
||||||
|
let (alice_state, bob_state) = join!(alice_swap, bob_swap);
|
||||||
|
|
||||||
// this xmr value matches the logic of alice::calculate_amounts i.e. btc *
|
alice_harness.assert_redeemed(alice_state.unwrap()).await;
|
||||||
// 10_000 * 100
|
bob_harness.assert_redeemed(bob_state.unwrap()).await;
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
})
|
||||||
let xmr_alice = xmr_to_swap * 10;
|
|
||||||
let xmr_bob = monero::Amount::ZERO;
|
|
||||||
|
|
||||||
let port = get_port().expect("Failed to find a free port");
|
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
|
||||||
.parse()
|
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let config = Config::regtest();
|
|
||||||
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
xmr_alice,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
config,
|
|
||||||
Seed::random().unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
alice_event_loop.peer_id(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
btc_bob,
|
|
||||||
xmr_to_swap,
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_swap_fut = alice::swap::swap(
|
|
||||||
alice_state,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
config,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
let alice_fut = select(alice_swap_fut, alice_event_loop.run().boxed());
|
|
||||||
|
|
||||||
let bob_swap_fut = bob::swap::swap(
|
|
||||||
bob_state,
|
|
||||||
bob_event_loop_handle,
|
|
||||||
bob_db,
|
|
||||||
bob_btc_wallet.clone(),
|
|
||||||
bob_xmr_wallet.clone(),
|
|
||||||
OsRng,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
let bob_fut = select(bob_swap_fut, bob_event_loop.run().boxed());
|
|
||||||
|
|
||||||
join(alice_fut, bob_fut).await;
|
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
|
|
||||||
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
bob_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
btc_alice_final,
|
|
||||||
btc_alice + btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
||||||
);
|
|
||||||
assert!(btc_bob_final <= btc_bob - btc_to_swap);
|
|
||||||
|
|
||||||
assert!(xmr_alice_final <= xmr_alice - xmr_to_swap);
|
|
||||||
assert_eq!(xmr_bob_final, xmr_bob + xmr_to_swap);
|
|
||||||
}
|
}
|
||||||
|
@ -1,166 +1,58 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, alice::AliceState, bob};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
database::Database,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, alice::AliceState, bob},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use tempfile::tempdir;
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let bob_swap = bob::swap(
|
||||||
let (
|
bob.state,
|
||||||
monero,
|
bob.event_loop_handle,
|
||||||
testutils::Containers {
|
bob.db,
|
||||||
bitcoind,
|
bob.bitcoin_wallet.clone(),
|
||||||
monerods: _monerods,
|
bob.monero_wallet.clone(),
|
||||||
},
|
OsRng,
|
||||||
) = testutils::init_containers(&cli).await;
|
bob.swap_id,
|
||||||
|
);
|
||||||
|
let bob_swap_handle = tokio::spawn(bob_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let alice_state = alice::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
alice.state,
|
||||||
|
alice::swap::is_encsig_learned,
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
alice.event_loop_handle,
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
alice.bitcoin_wallet.clone(),
|
||||||
|
alice.monero_wallet.clone(),
|
||||||
let port = get_port().expect("Failed to find a free port");
|
alice.config,
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
alice.swap_id,
|
||||||
.parse()
|
alice.db,
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let config = Config::regtest();
|
|
||||||
|
|
||||||
let alice_seed = Seed::random().unwrap();
|
|
||||||
let (
|
|
||||||
start_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
_,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
config,
|
|
||||||
alice_seed,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_peer_id = alice_event_loop.peer_id();
|
|
||||||
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
alice_peer_id.clone(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
config,
|
|
||||||
)
|
)
|
||||||
.await;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(alice_state, AliceState::EncSigLearned {..}));
|
||||||
|
|
||||||
// TODO: we are making a clone of Bob's wallets here to keep them in scope after
|
let alice = alice_harness.recover_alice_from_db().await;
|
||||||
// Bob's wallets are moved into an async task.
|
assert!(matches!(alice.state, AliceState::EncSigLearned {..}));
|
||||||
let bob_btc_wallet_clone = bob_btc_wallet.clone();
|
|
||||||
let bob_xmr_wallet_clone = bob_xmr_wallet.clone();
|
|
||||||
|
|
||||||
let bob_fut = bob::swap::swap(
|
let alice_state = alice::swap(
|
||||||
bob_state,
|
alice.state,
|
||||||
bob_event_loop_handle,
|
alice.event_loop_handle,
|
||||||
bob_db,
|
alice.bitcoin_wallet.clone(),
|
||||||
bob_btc_wallet.clone(),
|
alice.monero_wallet.clone(),
|
||||||
bob_xmr_wallet.clone(),
|
alice.config,
|
||||||
OsRng,
|
alice.swap_id,
|
||||||
Uuid::new_v4(),
|
alice.db,
|
||||||
);
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let alice_db_datadir = tempdir().unwrap();
|
alice_harness.assert_redeemed(alice_state).await;
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
||||||
|
|
||||||
tokio::spawn(async move { alice_event_loop.run().await });
|
let bob_state = bob_swap_handle.await.unwrap();
|
||||||
let bob_swap_handle = tokio::spawn(bob_fut);
|
bob_harness.assert_redeemed(bob_state.unwrap()).await
|
||||||
tokio::spawn(bob_event_loop.run());
|
})
|
||||||
|
.await;
|
||||||
let alice_swap_id = Uuid::new_v4();
|
|
||||||
|
|
||||||
let alice_state = alice::swap::run_until(
|
|
||||||
start_state,
|
|
||||||
alice::swap::is_encsig_learned,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
config,
|
|
||||||
alice_swap_id,
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::EncSigLearned {..}));
|
|
||||||
|
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
||||||
|
|
||||||
let resume_state =
|
|
||||||
if let swap::database::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() {
|
|
||||||
assert!(matches!(state, swap::database::Alice::EncSigLearned {..}));
|
|
||||||
state.into()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
let (mut event_loop_after_restart, event_loop_handle_after_restart) =
|
|
||||||
testutils::init_alice_event_loop(alice_multiaddr, alice_seed);
|
|
||||||
tokio::spawn(async move { event_loop_after_restart.run().await });
|
|
||||||
|
|
||||||
let alice_state = alice::swap::swap(
|
|
||||||
resume_state,
|
|
||||||
event_loop_handle_after_restart,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
config,
|
|
||||||
alice_swap_id,
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Wait for Bob to finish
|
|
||||||
bob_swap_handle.await.unwrap().unwrap();
|
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::BtcRedeemed {..}));
|
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
let btc_bob_final = bob_btc_wallet_clone.as_ref().balance().await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
btc_alice_final,
|
|
||||||
btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
||||||
);
|
|
||||||
assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap);
|
|
||||||
|
|
||||||
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
bob_xmr_wallet_clone.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap);
|
|
||||||
assert_eq!(xmr_bob_final, xmr_to_swap);
|
|
||||||
}
|
}
|
||||||
|
@ -1,168 +1,59 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
database::Database,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use tempfile::tempdir;
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
|
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let alice_swap = alice::swap(
|
||||||
let (
|
alice.state,
|
||||||
monero,
|
alice.event_loop_handle,
|
||||||
testutils::Containers {
|
alice.bitcoin_wallet.clone(),
|
||||||
bitcoind,
|
alice.monero_wallet.clone(),
|
||||||
monerods: _monerods,
|
alice.config,
|
||||||
},
|
alice.swap_id,
|
||||||
) = testutils::init_containers(&cli).await;
|
alice.db,
|
||||||
|
);
|
||||||
|
let alice_swap_handle = tokio::spawn(alice_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let bob_state = bob::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
bob.state,
|
||||||
|
bob::swap::is_encsig_sent,
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
bob.event_loop_handle,
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
bob.db,
|
||||||
|
bob.bitcoin_wallet.clone(),
|
||||||
let port = get_port().expect("Failed to find a free port");
|
bob.monero_wallet.clone(),
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
OsRng,
|
||||||
.parse()
|
bob.swap_id,
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let config = Config::regtest();
|
|
||||||
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
config,
|
|
||||||
Seed::random().unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_peer_id = alice_event_loop.peer_id();
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, _) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
alice_peer_id.clone(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
config,
|
|
||||||
)
|
)
|
||||||
.await;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// TODO: we are making a clone of Alices's wallets here to keep them in scope
|
assert!(matches!(bob_state, BobState::EncSigSent {..}));
|
||||||
// after Alices's wallets are moved into an async task.
|
|
||||||
let alice_btc_wallet_clone = alice_btc_wallet.clone();
|
|
||||||
let alice_xmr_wallet_clone = alice_xmr_wallet.clone();
|
|
||||||
|
|
||||||
// TODO: we are making a clone of Bob's wallets here to keep them in scope after
|
let bob = bob_harness.recover_bob_from_db().await;
|
||||||
// Bob's wallets are moved into an async task.
|
assert!(matches!(bob.state, BobState::EncSigSent {..}));
|
||||||
let bob_btc_wallet_clone = bob_btc_wallet.clone();
|
|
||||||
let bob_xmr_wallet_clone = bob_xmr_wallet.clone();
|
|
||||||
|
|
||||||
let alice_swap_handle = tokio::spawn(alice::swap::swap(
|
let bob_state = bob::swap(
|
||||||
alice_state,
|
bob.state,
|
||||||
alice_event_loop_handle,
|
bob.event_loop_handle,
|
||||||
alice_btc_wallet.clone(),
|
bob.db,
|
||||||
alice_xmr_wallet.clone(),
|
bob.bitcoin_wallet.clone(),
|
||||||
config,
|
bob.monero_wallet.clone(),
|
||||||
Uuid::new_v4(),
|
OsRng,
|
||||||
alice_db,
|
bob.swap_id,
|
||||||
));
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
tokio::spawn(async move { alice_event_loop.run().await });
|
bob_harness.assert_redeemed(bob_state).await;
|
||||||
|
|
||||||
tokio::spawn(bob_event_loop.run());
|
let alice_state = alice_swap_handle.await.unwrap();
|
||||||
|
alice_harness.assert_redeemed(alice_state.unwrap()).await;
|
||||||
let bob_swap_id = Uuid::new_v4();
|
})
|
||||||
let bob_db_datadir = tempdir().unwrap();
|
.await;
|
||||||
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
|
|
||||||
|
|
||||||
let bob_state = bob::swap::run_until(
|
|
||||||
bob_state,
|
|
||||||
bob::swap::is_encsig_sent,
|
|
||||||
bob_event_loop_handle,
|
|
||||||
bob_db,
|
|
||||||
bob_btc_wallet.clone(),
|
|
||||||
bob_xmr_wallet.clone(),
|
|
||||||
OsRng,
|
|
||||||
bob_swap_id,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert!(matches!(bob_state, BobState::EncSigSent {..}));
|
|
||||||
|
|
||||||
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
|
|
||||||
|
|
||||||
let resume_state =
|
|
||||||
if let swap::database::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() {
|
|
||||||
assert!(matches!(state, swap::database::Bob::EncSigSent {..}));
|
|
||||||
state.into()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
let (event_loop_after_restart, event_loop_handle_after_restart) =
|
|
||||||
testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);
|
|
||||||
tokio::spawn(event_loop_after_restart.run());
|
|
||||||
|
|
||||||
let bob_state = bob::swap::swap(
|
|
||||||
resume_state,
|
|
||||||
event_loop_handle_after_restart,
|
|
||||||
bob_db,
|
|
||||||
bob_btc_wallet,
|
|
||||||
bob_xmr_wallet,
|
|
||||||
OsRng,
|
|
||||||
bob_swap_id,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Wait for Alice to finish too
|
|
||||||
alice_swap_handle.await.unwrap().unwrap();
|
|
||||||
|
|
||||||
assert!(matches!(bob_state, BobState::XmrRedeemed {..}));
|
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet_clone.as_ref().balance().await.unwrap();
|
|
||||||
let btc_bob_final = bob_btc_wallet_clone.as_ref().balance().await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
btc_alice_final,
|
|
||||||
btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
||||||
);
|
|
||||||
assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap);
|
|
||||||
|
|
||||||
let xmr_alice_final = alice_xmr_wallet_clone.as_ref().get_balance().await.unwrap();
|
|
||||||
bob_xmr_wallet_clone.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap);
|
|
||||||
assert_eq!(xmr_bob_final, xmr_to_swap);
|
|
||||||
}
|
}
|
||||||
|
@ -1,159 +1,59 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
database::Database,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use tempfile::tempdir;
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use tokio::select;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
|
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let alice_swap = alice::swap(
|
||||||
let (
|
alice.state,
|
||||||
monero,
|
alice.event_loop_handle,
|
||||||
testutils::Containers {
|
alice.bitcoin_wallet.clone(),
|
||||||
bitcoind,
|
alice.monero_wallet.clone(),
|
||||||
monerods: _monerods,
|
alice.config,
|
||||||
},
|
alice.swap_id,
|
||||||
) = testutils::init_containers(&cli).await;
|
alice.db,
|
||||||
|
);
|
||||||
|
let alice_swap_handle = tokio::spawn(alice_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let bob_state = bob::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
bob.state,
|
||||||
|
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
|
||||||
let bob_xmr_starting_balance = monero::Amount::from_piconero(0);
|
|
||||||
|
|
||||||
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
|
||||||
|
|
||||||
let port = get_port().expect("Failed to find a free port");
|
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
|
||||||
.parse()
|
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
Seed::random().unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_peer_id = alice_event_loop.peer_id();
|
|
||||||
let (bob_state, bob_event_loop_1, bob_event_loop_handle_1, bob_btc_wallet, bob_xmr_wallet, _) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
alice_peer_id.clone(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
Config::regtest(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_fut = alice::swap::swap(
|
|
||||||
alice_state,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
Uuid::new_v4(),
|
|
||||||
alice_db,
|
|
||||||
);
|
|
||||||
|
|
||||||
let bob_swap_id = Uuid::new_v4();
|
|
||||||
let bob_db_datadir = tempdir().unwrap();
|
|
||||||
|
|
||||||
let bob_xmr_locked_fut = {
|
|
||||||
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
|
|
||||||
bob::swap::run_until(
|
|
||||||
bob_state,
|
|
||||||
bob::swap::is_xmr_locked,
|
bob::swap::is_xmr_locked,
|
||||||
bob_event_loop_handle_1,
|
bob.event_loop_handle,
|
||||||
bob_db,
|
bob.db,
|
||||||
bob_btc_wallet.clone(),
|
bob.bitcoin_wallet.clone(),
|
||||||
bob_xmr_wallet.clone(),
|
bob.monero_wallet.clone(),
|
||||||
OsRng,
|
OsRng,
|
||||||
bob_swap_id,
|
bob.swap_id,
|
||||||
)
|
)
|
||||||
};
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
tokio::spawn(async move { alice_event_loop.run().await });
|
assert!(matches!(bob_state, BobState::XmrLocked {..}));
|
||||||
|
|
||||||
let alice_fut_handle = tokio::spawn(alice_fut);
|
let bob = bob_harness.recover_bob_from_db().await;
|
||||||
|
assert!(matches!(bob.state, BobState::XmrLocked {..}));
|
||||||
|
|
||||||
// We are selecting with bob_event_loop_1 so that we stop polling on it once
|
let bob_state = bob::swap(
|
||||||
// bob reaches `xmr locked` state.
|
bob.state,
|
||||||
let bob_restart_state = select! {
|
bob.event_loop_handle,
|
||||||
res = bob_xmr_locked_fut => res.unwrap(),
|
bob.db,
|
||||||
_ = bob_event_loop_1.run() => panic!("The event loop should never finish")
|
bob.bitcoin_wallet.clone(),
|
||||||
};
|
bob.monero_wallet.clone(),
|
||||||
|
OsRng,
|
||||||
|
bob.swap_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let (bob_event_loop_2, bob_event_loop_handle_2) =
|
bob_harness.assert_redeemed(bob_state).await;
|
||||||
testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);
|
|
||||||
|
|
||||||
let bob_fut = bob::swap::swap(
|
let alice_state = alice_swap_handle.await.unwrap();
|
||||||
bob_restart_state,
|
alice_harness.assert_redeemed(alice_state.unwrap()).await;
|
||||||
bob_event_loop_handle_2,
|
})
|
||||||
Database::open(bob_db_datadir.path()).unwrap(),
|
.await;
|
||||||
bob_btc_wallet.clone(),
|
|
||||||
bob_xmr_wallet.clone(),
|
|
||||||
OsRng,
|
|
||||||
bob_swap_id,
|
|
||||||
);
|
|
||||||
|
|
||||||
let bob_final_state = select! {
|
|
||||||
bob_final_state = bob_fut => bob_final_state.unwrap(),
|
|
||||||
_ = bob_event_loop_2.run() => panic!("Event loop is not expected to stop")
|
|
||||||
};
|
|
||||||
|
|
||||||
assert!(matches!(bob_final_state, BobState::XmrRedeemed));
|
|
||||||
|
|
||||||
// Wait for Alice to finish too.
|
|
||||||
let alice_final_state = alice_fut_handle.await.unwrap().unwrap();
|
|
||||||
assert!(matches!(alice_final_state, AliceState::BtcRedeemed));
|
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
|
|
||||||
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
bob_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
btc_alice_final,
|
|
||||||
alice_btc_starting_balance + btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
||||||
);
|
|
||||||
assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap);
|
|
||||||
|
|
||||||
assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap);
|
|
||||||
assert_eq!(xmr_bob_final, bob_xmr_starting_balance + xmr_to_swap);
|
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,5 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use futures::{
|
|
||||||
future::{join, select, Either},
|
|
||||||
FutureExt,
|
|
||||||
};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
@ -23,128 +7,64 @@ pub mod testutils;
|
|||||||
/// the encsig and fail to refund or redeem. Alice punishes.
|
/// the encsig and fail to refund or redeem. Alice punishes.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let alice_swap = alice::swap(
|
||||||
let (
|
alice.state,
|
||||||
monero,
|
alice.event_loop_handle,
|
||||||
testutils::Containers {
|
alice.bitcoin_wallet.clone(),
|
||||||
bitcoind,
|
alice.monero_wallet.clone(),
|
||||||
monerods: _monerods,
|
alice.config,
|
||||||
},
|
alice.swap_id,
|
||||||
) = testutils::init_containers(&cli).await;
|
alice.db,
|
||||||
|
);
|
||||||
|
let alice_swap_handle = tokio::spawn(alice_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let bob_state = bob::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
bob.state,
|
||||||
|
bob::swap::is_btc_locked,
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
bob.event_loop_handle,
|
||||||
|
bob.db,
|
||||||
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
bob.bitcoin_wallet.clone(),
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
bob.monero_wallet.clone(),
|
||||||
|
OsRng,
|
||||||
let port = get_port().expect("Failed to find a free port");
|
bob.swap_id,
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
|
||||||
.parse()
|
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let config = Config::regtest();
|
|
||||||
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
config,
|
|
||||||
Seed::random().unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr,
|
|
||||||
alice_event_loop.peer_id(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
config,
|
|
||||||
)
|
)
|
||||||
.await;
|
|
||||||
|
|
||||||
let bob_btc_locked_fut = bob::swap::run_until(
|
|
||||||
bob_state,
|
|
||||||
bob::swap::is_btc_locked,
|
|
||||||
bob_event_loop_handle,
|
|
||||||
bob_db,
|
|
||||||
bob_btc_wallet.clone(),
|
|
||||||
bob_xmr_wallet.clone(),
|
|
||||||
OsRng,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
let bob_fut = select(bob_btc_locked_fut, bob_event_loop.run().boxed());
|
|
||||||
|
|
||||||
let alice_fut = alice::swap::swap(
|
|
||||||
alice_state,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
Uuid::new_v4(),
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
let alice_fut = select(alice_fut, alice_event_loop.run().boxed());
|
|
||||||
|
|
||||||
// Wait until alice has locked xmr and bob has locked btc
|
|
||||||
let (alice_state, bob_state) = join(alice_fut, bob_fut).await;
|
|
||||||
|
|
||||||
let alice_state = match alice_state {
|
|
||||||
Either::Left((state, _)) => state.unwrap(),
|
|
||||||
Either::Right(_) => panic!("Alice event loop should not terminate."),
|
|
||||||
};
|
|
||||||
|
|
||||||
let bob_state = match bob_state {
|
|
||||||
Either::Left((state, _)) => state.unwrap(),
|
|
||||||
Either::Right(_) => panic!("Bob event loop should not terminate."),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::BtcPunished));
|
|
||||||
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {
|
|
||||||
state3
|
|
||||||
} else {
|
|
||||||
panic!("Bob in unexpected state");
|
|
||||||
};
|
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
|
||||||
|
|
||||||
// lock_tx_bitcoin_fee is determined by the wallet, it is not necessarily equal
|
|
||||||
// to TX_FEE
|
|
||||||
let lock_tx_bitcoin_fee = bob_btc_wallet
|
|
||||||
.transaction_fee(bob_state3.tx_lock_id())
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert!(matches!(bob_state, BobState::BtcLocked {..}));
|
||||||
btc_alice_final,
|
|
||||||
alice_btc_starting_balance + btc_to_swap - bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
let alice_state = alice_swap_handle.await.unwrap();
|
||||||
btc_bob_final,
|
alice_harness.assert_punished(alice_state.unwrap()).await;
|
||||||
bob_btc_starting_balance - btc_to_swap - lock_tx_bitcoin_fee
|
|
||||||
);
|
// Restart Bob after Alice punished to ensure Bob transitions to
|
||||||
|
// punished and does not run indefinitely
|
||||||
|
let bob = bob_harness.recover_bob_from_db().await;
|
||||||
|
assert!(matches!(bob.state, BobState::BtcLocked {..}));
|
||||||
|
|
||||||
|
// TODO: make lock-tx-id available in final states
|
||||||
|
let lock_tx_id = if let BobState::BtcLocked(state3) = bob_state {
|
||||||
|
state3.tx_lock_id()
|
||||||
|
} else {
|
||||||
|
panic!("Bob in unexpected state");
|
||||||
|
};
|
||||||
|
|
||||||
|
let bob_state = bob::swap(
|
||||||
|
bob.state,
|
||||||
|
bob.event_loop_handle,
|
||||||
|
bob.db,
|
||||||
|
bob.bitcoin_wallet.clone(),
|
||||||
|
bob.monero_wallet.clone(),
|
||||||
|
OsRng,
|
||||||
|
bob.swap_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
bob_harness.assert_punished(bob_state, lock_tx_id).await;
|
||||||
|
})
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
@ -1,174 +1,66 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use futures::future::try_join;
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, alice::AliceState, bob};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
database::Database,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use tempfile::tempdir;
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use tokio::select;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
|
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
|
||||||
// then also refunds.
|
/// then also refunds.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let bob_swap = bob::swap(
|
||||||
let (
|
bob.state,
|
||||||
monero,
|
bob.event_loop_handle,
|
||||||
testutils::Containers {
|
bob.db,
|
||||||
bitcoind,
|
bob.bitcoin_wallet.clone(),
|
||||||
monerods: _monerods,
|
bob.monero_wallet.clone(),
|
||||||
},
|
OsRng,
|
||||||
) = testutils::init_containers(&cli).await;
|
bob.swap_id,
|
||||||
|
);
|
||||||
|
let bob_swap_handle = tokio::spawn(bob_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let alice_state = alice::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
alice.state,
|
||||||
|
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
|
||||||
let bob_xmr_starting_balance = monero::Amount::from_piconero(0);
|
|
||||||
|
|
||||||
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
|
||||||
|
|
||||||
let port = get_port().expect("Failed to find a free port");
|
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
|
||||||
.parse()
|
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let alice_seed = Seed::random().unwrap();
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop_1,
|
|
||||||
alice_event_loop_handle_1,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
_,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
alice_seed,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
alice_event_loop_1.peer_id(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
Config::regtest(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let bob_fut = bob::swap::swap(
|
|
||||||
bob_state,
|
|
||||||
bob_event_loop_handle,
|
|
||||||
bob_db,
|
|
||||||
bob_btc_wallet.clone(),
|
|
||||||
bob_xmr_wallet.clone(),
|
|
||||||
OsRng,
|
|
||||||
Uuid::new_v4(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let alice_swap_id = Uuid::new_v4();
|
|
||||||
let alice_db_datadir = tempdir().unwrap();
|
|
||||||
|
|
||||||
let alice_xmr_locked_fut = {
|
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
||||||
alice::swap::run_until(
|
|
||||||
alice_state,
|
|
||||||
alice::swap::is_xmr_locked,
|
alice::swap::is_xmr_locked,
|
||||||
alice_event_loop_handle_1,
|
alice.event_loop_handle,
|
||||||
alice_btc_wallet.clone(),
|
alice.bitcoin_wallet.clone(),
|
||||||
alice_xmr_wallet.clone(),
|
alice.monero_wallet.clone(),
|
||||||
Config::regtest(),
|
alice.config,
|
||||||
alice_swap_id,
|
alice.swap_id,
|
||||||
alice_db,
|
alice.db,
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
tokio::spawn(bob_event_loop.run());
|
|
||||||
|
|
||||||
// We are selecting with alice_event_loop_1 so that we stop polling on it once
|
|
||||||
// the try_join is finished.
|
|
||||||
let (bob_state, alice_restart_state) = select! {
|
|
||||||
res = try_join(bob_fut, alice_xmr_locked_fut) => res.unwrap(),
|
|
||||||
_ = alice_event_loop_1.run() => panic!("The event loop should never finish")
|
|
||||||
};
|
|
||||||
|
|
||||||
let tx_lock_id = if let BobState::BtcRefunded(state4) = bob_state {
|
|
||||||
state4.tx_lock_id()
|
|
||||||
} else {
|
|
||||||
panic!("Bob in unexpected state");
|
|
||||||
};
|
|
||||||
|
|
||||||
let (mut alice_event_loop_2, alice_event_loop_handle_2) =
|
|
||||||
testutils::init_alice_event_loop(alice_multiaddr, alice_seed);
|
|
||||||
|
|
||||||
let alice_final_state = {
|
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
||||||
alice::swap::swap(
|
|
||||||
alice_restart_state,
|
|
||||||
alice_event_loop_handle_2,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
alice_swap_id,
|
|
||||||
alice_db,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap();
|
||||||
};
|
assert!(matches!(alice_state, AliceState::XmrLocked {..}));
|
||||||
tokio::spawn(async move { alice_event_loop_2.run().await });
|
|
||||||
|
|
||||||
assert!(matches!(alice_final_state, AliceState::XmrRefunded));
|
// Alice does not act, Bob refunds
|
||||||
|
let bob_state = bob_swap_handle.await.unwrap();
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
// Once bob has finished Alice is restarted and refunds as well
|
||||||
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
let alice = alice_harness.recover_alice_from_db().await;
|
||||||
|
assert!(matches!(alice.state, AliceState::XmrLocked {..}));
|
||||||
|
|
||||||
let lock_tx_bitcoin_fee = bob_btc_wallet.transaction_fee(tx_lock_id).await.unwrap();
|
let alice_state = alice::swap(
|
||||||
|
alice.state,
|
||||||
|
alice.event_loop_handle,
|
||||||
|
alice.bitcoin_wallet.clone(),
|
||||||
|
alice.monero_wallet.clone(),
|
||||||
|
alice.config,
|
||||||
|
alice.swap_id,
|
||||||
|
alice.db,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(btc_alice_final, alice_btc_starting_balance);
|
// TODO: The test passes like this, but the assertion should be done after Bob
|
||||||
|
// refunded, not at the end because this can cause side-effects!
|
||||||
// Alice or Bob could publish TxCancel. This means Bob could pay tx fees for
|
// We have to properly wait for the refund tx's finality inside the assertion,
|
||||||
// TxCancel and TxRefund or only TxRefund
|
// which requires storing the refund_tx_id in the the state!
|
||||||
let btc_bob_final_alice_submitted_cancel = btc_bob_final
|
bob_harness.assert_refunded(bob_state.unwrap()).await;
|
||||||
== bob_btc_starting_balance
|
alice_harness.assert_refunded(alice_state).await;
|
||||||
- lock_tx_bitcoin_fee
|
})
|
||||||
- bitcoin::Amount::from_sat(bitcoin::TX_FEE);
|
.await;
|
||||||
|
|
||||||
let btc_bob_final_bob_submitted_cancel = btc_bob_final
|
|
||||||
== bob_btc_starting_balance
|
|
||||||
- lock_tx_bitcoin_fee
|
|
||||||
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE);
|
|
||||||
assert!(btc_bob_final_alice_submitted_cancel || btc_bob_final_bob_submitted_cancel);
|
|
||||||
|
|
||||||
alice_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
assert_eq!(xmr_alice_final, xmr_to_swap);
|
|
||||||
|
|
||||||
bob_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
|
||||||
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
||||||
assert_eq!(xmr_bob_final, bob_xmr_starting_balance);
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
use crate::testutils;
|
||||||
use bitcoin_harness::Bitcoind;
|
use bitcoin_harness::Bitcoind;
|
||||||
|
use futures::Future;
|
||||||
|
use get_port::get_port;
|
||||||
use libp2p::{core::Multiaddr, PeerId};
|
use libp2p::{core::Multiaddr, PeerId};
|
||||||
use monero_harness::{image, Monero};
|
use monero_harness::{image, Monero};
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use std::sync::Arc;
|
use std::{path::PathBuf, sync::Arc};
|
||||||
use swap::{
|
use swap::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
config::Config,
|
config::Config,
|
||||||
@ -17,8 +20,422 @@ use tempfile::tempdir;
|
|||||||
use testcontainers::{clients::Cli, Container};
|
use testcontainers::{clients::Cli, Container};
|
||||||
use tracing_core::dispatcher::DefaultGuard;
|
use tracing_core::dispatcher::DefaultGuard;
|
||||||
use tracing_log::LogTracer;
|
use tracing_log::LogTracer;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
pub async fn test<T, F>(testfn: T)
|
||||||
|
where
|
||||||
|
T: Fn(AliceHarness, BobHarness) -> F,
|
||||||
|
F: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
let cli = Cli::default();
|
||||||
|
|
||||||
|
let _guard = init_tracing();
|
||||||
|
|
||||||
|
let (monero, containers) = testutils::init_containers(&cli).await;
|
||||||
|
|
||||||
|
let swap_amounts = SwapAmounts {
|
||||||
|
btc: bitcoin::Amount::from_sat(1_000_000),
|
||||||
|
xmr: monero::Amount::from_piconero(1_000_000_000_000),
|
||||||
|
};
|
||||||
|
|
||||||
|
let config = Config::regtest();
|
||||||
|
|
||||||
|
let alice_starting_balances = StartingBalances {
|
||||||
|
xmr: swap_amounts.xmr * 10,
|
||||||
|
btc: bitcoin::Amount::ZERO,
|
||||||
|
};
|
||||||
|
let alice_harness = AliceHarness::new(
|
||||||
|
config,
|
||||||
|
swap_amounts,
|
||||||
|
Uuid::new_v4(),
|
||||||
|
&monero,
|
||||||
|
&containers.bitcoind,
|
||||||
|
alice_starting_balances,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let bob_starting_balances = StartingBalances {
|
||||||
|
xmr: monero::Amount::ZERO,
|
||||||
|
btc: swap_amounts.btc * 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
let bob_harness = BobHarness::new(
|
||||||
|
config,
|
||||||
|
swap_amounts,
|
||||||
|
Uuid::new_v4(),
|
||||||
|
&monero,
|
||||||
|
&containers.bitcoind,
|
||||||
|
bob_starting_balances,
|
||||||
|
alice_harness.listen_address(),
|
||||||
|
alice_harness.peer_id(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
testfn(alice_harness, bob_harness).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Alice {
|
||||||
|
pub state: AliceState,
|
||||||
|
pub event_loop_handle: alice::EventLoopHandle,
|
||||||
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
pub monero_wallet: Arc<monero::Wallet>,
|
||||||
|
pub config: Config,
|
||||||
|
pub swap_id: Uuid,
|
||||||
|
pub db: Database,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AliceHarness {
|
||||||
|
listen_address: Multiaddr,
|
||||||
|
peer_id: PeerId,
|
||||||
|
|
||||||
|
seed: Seed,
|
||||||
|
db_path: PathBuf,
|
||||||
|
swap_id: Uuid,
|
||||||
|
|
||||||
|
swap_amounts: SwapAmounts,
|
||||||
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
|
config: Config,
|
||||||
|
starting_balances: StartingBalances,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AliceHarness {
|
||||||
|
async fn new(
|
||||||
|
config: Config,
|
||||||
|
swap_amounts: SwapAmounts,
|
||||||
|
swap_id: Uuid,
|
||||||
|
monero: &Monero,
|
||||||
|
bitcoind: &Bitcoind<'_>,
|
||||||
|
starting_balances: StartingBalances,
|
||||||
|
) -> Self {
|
||||||
|
let port = get_port().expect("Failed to find a free port");
|
||||||
|
|
||||||
|
let listen_address: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
||||||
|
.parse()
|
||||||
|
.expect("failed to parse Alice's address");
|
||||||
|
|
||||||
|
let seed = Seed::random().unwrap();
|
||||||
|
|
||||||
|
let db_path = tempdir().unwrap().path().to_path_buf();
|
||||||
|
|
||||||
|
let (bitcoin_wallet, monero_wallet) =
|
||||||
|
init_wallets("alice", bitcoind, monero, starting_balances.clone(), config).await;
|
||||||
|
|
||||||
|
// TODO: This should be done by changing the production code
|
||||||
|
let network_seed = network::Seed::new(seed);
|
||||||
|
let identity = network_seed.derive_libp2p_identity();
|
||||||
|
let peer_id = PeerId::from(identity.public());
|
||||||
|
|
||||||
|
Self {
|
||||||
|
seed,
|
||||||
|
db_path,
|
||||||
|
listen_address,
|
||||||
|
peer_id,
|
||||||
|
swap_id,
|
||||||
|
swap_amounts,
|
||||||
|
bitcoin_wallet,
|
||||||
|
monero_wallet,
|
||||||
|
config,
|
||||||
|
starting_balances,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new_alice(&self) -> Alice {
|
||||||
|
let initial_state = init_alice_state(
|
||||||
|
self.swap_amounts.btc,
|
||||||
|
self.swap_amounts.xmr,
|
||||||
|
self.bitcoin_wallet.clone(),
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let (mut event_loop, event_loop_handle) =
|
||||||
|
init_alice_event_loop(self.listen_address.clone(), self.seed);
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
|
let db = Database::open(self.db_path.as_path()).unwrap();
|
||||||
|
|
||||||
|
Alice {
|
||||||
|
event_loop_handle,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
config: self.config,
|
||||||
|
db,
|
||||||
|
state: initial_state,
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recover_alice_from_db(&self) -> Alice {
|
||||||
|
// TODO: "simulated restart" issues:
|
||||||
|
// - create new wallets instead of reusing (hard because of container
|
||||||
|
// lifetimes)
|
||||||
|
// - consider aborting the old event loop (currently just keeps running)
|
||||||
|
|
||||||
|
// reopen the existing database
|
||||||
|
let db = Database::open(self.db_path.clone().as_path()).unwrap();
|
||||||
|
|
||||||
|
let resume_state =
|
||||||
|
if let swap::database::Swap::Alice(state) = db.get_state(self.swap_id).unwrap() {
|
||||||
|
state.into()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let (mut event_loop, event_loop_handle) =
|
||||||
|
init_alice_event_loop(self.listen_address.clone(), self.seed);
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
|
Alice {
|
||||||
|
state: resume_state,
|
||||||
|
event_loop_handle,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
config: self.config,
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_redeemed(&self, state: AliceState) {
|
||||||
|
assert!(matches!(state, AliceState::BtcRedeemed));
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
btc_balance_after_swap,
|
||||||
|
self.starting_balances.btc + self.swap_amounts.btc
|
||||||
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
||||||
|
);
|
||||||
|
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert!(xmr_balance_after_swap <= self.starting_balances.xmr - self.swap_amounts.xmr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_refunded(&self, state: AliceState) {
|
||||||
|
assert!(matches!(state, AliceState::XmrRefunded));
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(btc_balance_after_swap, self.starting_balances.btc);
|
||||||
|
|
||||||
|
// Ensure that Alice's balance is refreshed as we use a newly created wallet
|
||||||
|
self.monero_wallet.as_ref().inner.refresh().await.unwrap();
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert_eq!(xmr_balance_after_swap, self.swap_amounts.xmr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_punished(&self, state: AliceState) {
|
||||||
|
assert!(matches!(state, AliceState::BtcPunished));
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
btc_balance_after_swap,
|
||||||
|
self.starting_balances.btc + self.swap_amounts.btc
|
||||||
|
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
|
||||||
|
);
|
||||||
|
|
||||||
|
let xnr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert!(xnr_balance_after_swap <= self.starting_balances.xmr - self.swap_amounts.xmr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn peer_id(&self) -> PeerId {
|
||||||
|
self.peer_id.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn listen_address(&self) -> Multiaddr {
|
||||||
|
self.listen_address.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Bob {
|
||||||
|
pub state: BobState,
|
||||||
|
pub event_loop_handle: bob::EventLoopHandle,
|
||||||
|
pub db: Database,
|
||||||
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
pub monero_wallet: Arc<monero::Wallet>,
|
||||||
|
pub swap_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BobHarness {
|
||||||
|
db_path: PathBuf,
|
||||||
|
swap_id: Uuid,
|
||||||
|
|
||||||
|
swap_amounts: SwapAmounts,
|
||||||
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
|
config: Config,
|
||||||
|
starting_balances: StartingBalances,
|
||||||
|
|
||||||
|
alice_connect_address: Multiaddr,
|
||||||
|
alice_connect_peer_id: PeerId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BobHarness {
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
async fn new(
|
||||||
|
config: Config,
|
||||||
|
swap_amounts: SwapAmounts,
|
||||||
|
swap_id: Uuid,
|
||||||
|
monero: &Monero,
|
||||||
|
bitcoind: &Bitcoind<'_>,
|
||||||
|
starting_balances: StartingBalances,
|
||||||
|
alice_connect_address: Multiaddr,
|
||||||
|
alice_connect_peer_id: PeerId,
|
||||||
|
) -> Self {
|
||||||
|
let db_path = tempdir().unwrap().path().to_path_buf();
|
||||||
|
|
||||||
|
let (bitcoin_wallet, monero_wallet) =
|
||||||
|
init_wallets("bob", bitcoind, monero, starting_balances.clone(), config).await;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
db_path,
|
||||||
|
swap_id,
|
||||||
|
swap_amounts,
|
||||||
|
bitcoin_wallet,
|
||||||
|
monero_wallet,
|
||||||
|
config,
|
||||||
|
starting_balances,
|
||||||
|
alice_connect_address,
|
||||||
|
alice_connect_peer_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new_bob(&self) -> Bob {
|
||||||
|
let initial_state = init_bob_state(
|
||||||
|
self.swap_amounts.btc,
|
||||||
|
self.swap_amounts.xmr,
|
||||||
|
self.bitcoin_wallet.clone(),
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
||||||
|
self.alice_connect_peer_id.clone(),
|
||||||
|
self.alice_connect_address.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
|
let db = Database::open(self.db_path.as_path()).unwrap();
|
||||||
|
|
||||||
|
Bob {
|
||||||
|
state: initial_state,
|
||||||
|
event_loop_handle,
|
||||||
|
db,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recover_bob_from_db(&self) -> Bob {
|
||||||
|
// TODO: "simulated restart" issues:
|
||||||
|
// - create new wallets instead of reusing (hard because of container
|
||||||
|
// lifetimes)
|
||||||
|
// - consider aborting the old event loop (currently just keeps running)
|
||||||
|
|
||||||
|
// reopen the existing database
|
||||||
|
let db = Database::open(self.db_path.clone().as_path()).unwrap();
|
||||||
|
|
||||||
|
let resume_state =
|
||||||
|
if let swap::database::Swap::Bob(state) = db.get_state(self.swap_id).unwrap() {
|
||||||
|
state.into()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
||||||
|
self.alice_connect_peer_id.clone(),
|
||||||
|
self.alice_connect_address.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
|
Bob {
|
||||||
|
state: resume_state,
|
||||||
|
event_loop_handle,
|
||||||
|
db,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_redeemed(&self, state: BobState) {
|
||||||
|
assert!(matches!(state, BobState::XmrRedeemed));
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert!(btc_balance_after_swap <= self.starting_balances.btc - self.swap_amounts.btc);
|
||||||
|
|
||||||
|
// Ensure that Bob's balance is refreshed as we use a newly created wallet
|
||||||
|
self.monero_wallet.as_ref().inner.refresh().await.unwrap();
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
xmr_balance_after_swap,
|
||||||
|
self.starting_balances.xmr + self.swap_amounts.xmr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_refunded(&self, state: BobState) {
|
||||||
|
let lock_tx_id = if let BobState::BtcRefunded(state4) = state {
|
||||||
|
state4.tx_lock_id()
|
||||||
|
} else {
|
||||||
|
panic!("Bob in unexpected state");
|
||||||
|
};
|
||||||
|
let lock_tx_bitcoin_fee = self
|
||||||
|
.bitcoin_wallet
|
||||||
|
.transaction_fee(lock_tx_id)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
|
||||||
|
let alice_submitted_cancel = btc_balance_after_swap
|
||||||
|
== self.starting_balances.btc
|
||||||
|
- lock_tx_bitcoin_fee
|
||||||
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE);
|
||||||
|
|
||||||
|
let bob_submitted_cancel = btc_balance_after_swap
|
||||||
|
== self.starting_balances.btc
|
||||||
|
- lock_tx_bitcoin_fee
|
||||||
|
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE);
|
||||||
|
|
||||||
|
// The cancel tx can be submitted by both Alice and Bob.
|
||||||
|
// Since we cannot be sure who submitted it we have to assert accordingly
|
||||||
|
assert!(alice_submitted_cancel || bob_submitted_cancel);
|
||||||
|
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert_eq!(xmr_balance_after_swap, self.starting_balances.xmr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_punished(&self, state: BobState, lock_tx_id: ::bitcoin::Txid) {
|
||||||
|
assert!(matches!(state, BobState::BtcPunished));
|
||||||
|
|
||||||
|
let lock_tx_bitcoin_fee = self
|
||||||
|
.bitcoin_wallet
|
||||||
|
.transaction_fee(lock_tx_id)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
btc_balance_after_swap,
|
||||||
|
self.starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee
|
||||||
|
);
|
||||||
|
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert_eq!(xmr_balance_after_swap, self.starting_balances.xmr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct StartingBalances {
|
||||||
|
pub xmr: monero::Amount,
|
||||||
|
pub btc: bitcoin::Amount,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||||
let _ = bitcoind.init(5).await;
|
let _ = bitcoind.init(5).await;
|
||||||
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||||
@ -28,28 +445,17 @@ pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
|||||||
(monero, Containers { bitcoind, monerods })
|
(monero, Containers { bitcoind, monerods })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init_wallets(
|
async fn init_wallets(
|
||||||
name: &str,
|
name: &str,
|
||||||
bitcoind: &Bitcoind<'_>,
|
bitcoind: &Bitcoind<'_>,
|
||||||
monero: &Monero,
|
monero: &Monero,
|
||||||
btc_starting_balance: Option<::bitcoin::Amount>,
|
starting_balances: StartingBalances,
|
||||||
xmr_starting_balance: Option<monero::Amount>,
|
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
||||||
match xmr_starting_balance {
|
monero
|
||||||
Some(amount) => {
|
.init(vec![(name, starting_balances.xmr.as_piconero())])
|
||||||
monero
|
.await
|
||||||
.init(vec![(name, amount.as_piconero())])
|
.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
monero
|
|
||||||
.init(vec![(name, monero::Amount::ZERO.as_piconero())])
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let xmr_wallet = Arc::new(swap::monero::Wallet {
|
let xmr_wallet = Arc::new(swap::monero::Wallet {
|
||||||
inner: monero.wallet(name).unwrap().client(),
|
inner: monero.wallet(name).unwrap().client(),
|
||||||
@ -62,9 +468,12 @@ pub async fn init_wallets(
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(amount) = btc_starting_balance {
|
if starting_balances.btc != bitcoin::Amount::ZERO {
|
||||||
bitcoind
|
bitcoind
|
||||||
.mint(btc_wallet.inner.new_address().await.unwrap(), amount)
|
.mint(
|
||||||
|
btc_wallet.inner.new_address().await.unwrap(),
|
||||||
|
starting_balances.btc,
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
@ -72,7 +481,7 @@ pub async fn init_wallets(
|
|||||||
(btc_wallet, xmr_wallet)
|
(btc_wallet, xmr_wallet)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init_alice_state(
|
async fn init_alice_state(
|
||||||
btc_to_swap: bitcoin::Amount,
|
btc_to_swap: bitcoin::Amount,
|
||||||
xmr_to_swap: monero::Amount,
|
xmr_to_swap: monero::Amount,
|
||||||
alice_btc_wallet: Arc<bitcoin::Wallet>,
|
alice_btc_wallet: Arc<bitcoin::Wallet>,
|
||||||
@ -105,7 +514,7 @@ pub async fn init_alice_state(
|
|||||||
AliceState::Started { amounts, state0 }
|
AliceState::Started { amounts, state0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_alice_event_loop(
|
fn init_alice_event_loop(
|
||||||
listen: Multiaddr,
|
listen: Multiaddr,
|
||||||
seed: Seed,
|
seed: Seed,
|
||||||
) -> (
|
) -> (
|
||||||
@ -117,53 +526,7 @@ pub fn init_alice_event_loop(
|
|||||||
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap()
|
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
async fn init_bob_state(
|
||||||
pub async fn init_alice(
|
|
||||||
bitcoind: &Bitcoind<'_>,
|
|
||||||
monero: &Monero,
|
|
||||||
btc_to_swap: bitcoin::Amount,
|
|
||||||
xmr_to_swap: monero::Amount,
|
|
||||||
xmr_starting_balance: monero::Amount,
|
|
||||||
listen: Multiaddr,
|
|
||||||
config: Config,
|
|
||||||
seed: Seed,
|
|
||||||
) -> (
|
|
||||||
AliceState,
|
|
||||||
alice::event_loop::EventLoop,
|
|
||||||
alice::event_loop::EventLoopHandle,
|
|
||||||
Arc<swap::bitcoin::Wallet>,
|
|
||||||
Arc<swap::monero::Wallet>,
|
|
||||||
Database,
|
|
||||||
) {
|
|
||||||
let (alice_btc_wallet, alice_xmr_wallet) = init_wallets(
|
|
||||||
"alice",
|
|
||||||
bitcoind,
|
|
||||||
monero,
|
|
||||||
None,
|
|
||||||
Some(xmr_starting_balance),
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let alice_start_state =
|
|
||||||
init_alice_state(btc_to_swap, xmr_to_swap, alice_btc_wallet.clone(), config).await;
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = init_alice_event_loop(listen, seed);
|
|
||||||
|
|
||||||
let alice_db_datadir = tempdir().unwrap();
|
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
||||||
|
|
||||||
(
|
|
||||||
alice_start_state,
|
|
||||||
event_loop,
|
|
||||||
event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn init_bob_state(
|
|
||||||
btc_to_swap: bitcoin::Amount,
|
btc_to_swap: bitcoin::Amount,
|
||||||
xmr_to_swap: monero::Amount,
|
xmr_to_swap: monero::Amount,
|
||||||
bob_btc_wallet: Arc<bitcoin::Wallet>,
|
bob_btc_wallet: Arc<bitcoin::Wallet>,
|
||||||
@ -188,7 +551,7 @@ pub async fn init_bob_state(
|
|||||||
BobState::Started { state0, amounts }
|
BobState::Started { state0, amounts }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_bob_event_loop(
|
fn init_bob_event_loop(
|
||||||
alice_peer_id: PeerId,
|
alice_peer_id: PeerId,
|
||||||
alice_addr: Multiaddr,
|
alice_addr: Multiaddr,
|
||||||
) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) {
|
) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) {
|
||||||
@ -199,56 +562,11 @@ pub fn init_bob_event_loop(
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub async fn init_bob(
|
|
||||||
alice_multiaddr: Multiaddr,
|
|
||||||
alice_peer_id: PeerId,
|
|
||||||
bitcoind: &Bitcoind<'_>,
|
|
||||||
monero: &Monero,
|
|
||||||
btc_to_swap: bitcoin::Amount,
|
|
||||||
btc_starting_balance: bitcoin::Amount,
|
|
||||||
xmr_to_swap: monero::Amount,
|
|
||||||
config: Config,
|
|
||||||
) -> (
|
|
||||||
BobState,
|
|
||||||
bob::event_loop::EventLoop,
|
|
||||||
bob::event_loop::EventLoopHandle,
|
|
||||||
Arc<swap::bitcoin::Wallet>,
|
|
||||||
Arc<swap::monero::Wallet>,
|
|
||||||
Database,
|
|
||||||
) {
|
|
||||||
let (bob_btc_wallet, bob_xmr_wallet) = init_wallets(
|
|
||||||
"bob",
|
|
||||||
bitcoind,
|
|
||||||
monero,
|
|
||||||
Some(btc_starting_balance),
|
|
||||||
None,
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let bob_state = init_bob_state(btc_to_swap, xmr_to_swap, bob_btc_wallet.clone(), config).await;
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = init_bob_event_loop(alice_peer_id, alice_multiaddr);
|
|
||||||
|
|
||||||
let bob_db_dir = tempdir().unwrap();
|
|
||||||
let bob_db = Database::open(bob_db_dir.path()).unwrap();
|
|
||||||
|
|
||||||
(
|
|
||||||
bob_state,
|
|
||||||
event_loop,
|
|
||||||
event_loop_handle,
|
|
||||||
bob_btc_wallet,
|
|
||||||
bob_xmr_wallet,
|
|
||||||
bob_db,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is just to keep the containers alive
|
// This is just to keep the containers alive
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Containers<'a> {
|
struct Containers<'a> {
|
||||||
pub bitcoind: Bitcoind<'a>,
|
bitcoind: Bitcoind<'a>,
|
||||||
pub monerods: Vec<Container<'a, Cli, image::Monero>>,
|
monerods: Vec<Container<'a, Cli, image::Monero>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Utility function to initialize logging in the test environment.
|
/// Utility function to initialize logging in the test environment.
|
||||||
@ -257,7 +575,7 @@ pub struct Containers<'a> {
|
|||||||
/// ```rust
|
/// ```rust
|
||||||
/// let _guard = init_tracing();
|
/// let _guard = init_tracing();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn init_tracing() -> DefaultGuard {
|
fn init_tracing() -> DefaultGuard {
|
||||||
// converts all log records into tracing events
|
// converts all log records into tracing events
|
||||||
// Note: Make sure to initialize without unwrapping, otherwise this causes
|
// Note: Make sure to initialize without unwrapping, otherwise this causes
|
||||||
// trouble when running multiple tests.
|
// trouble when running multiple tests.
|
||||||
|
Loading…
Reference in New Issue
Block a user