2021-01-14 19:26:32 -05:00
|
|
|
use crate::testutils;
|
2020-12-09 22:59:09 -05:00
|
|
|
use bitcoin_harness::Bitcoind;
|
2021-01-14 19:26:32 -05:00
|
|
|
use futures::Future;
|
|
|
|
use get_port::get_port;
|
2021-01-20 05:42:35 -05:00
|
|
|
use libp2p::{core::Multiaddr, PeerId};
|
2020-12-09 22:59:09 -05:00
|
|
|
use monero_harness::{image, Monero};
|
2021-01-20 05:42:35 -05:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
2020-12-09 22:59:09 -05:00
|
|
|
use swap::{
|
2021-01-04 22:08:36 -05:00
|
|
|
bitcoin,
|
2021-01-27 01:03:52 -05:00
|
|
|
bitcoin::Timelock,
|
2021-01-29 01:27:50 -05:00
|
|
|
execution_params,
|
|
|
|
execution_params::{ExecutionParams, GetExecutionParams},
|
2021-01-18 05:57:17 -05:00
|
|
|
monero,
|
2021-01-20 21:43:25 -05:00
|
|
|
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
|
2021-01-07 20:04:48 -05:00
|
|
|
seed::Seed,
|
2020-12-09 22:59:09 -05:00
|
|
|
};
|
|
|
|
use tempfile::tempdir;
|
|
|
|
use testcontainers::{clients::Cli, Container};
|
2021-01-21 21:33:31 -05:00
|
|
|
use tokio::task::JoinHandle;
|
2020-12-09 22:59:09 -05:00
|
|
|
use tracing_core::dispatcher::DefaultGuard;
|
|
|
|
use tracing_log::LogTracer;
|
2021-01-14 19:26:32 -05:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-01-18 22:48:07 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct StartingBalances {
|
|
|
|
pub xmr: monero::Amount,
|
|
|
|
pub btc: bitcoin::Amount,
|
|
|
|
}
|
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
struct AliceParams {
|
|
|
|
seed: Seed,
|
2021-01-26 21:33:32 -05:00
|
|
|
execution_params: ExecutionParams,
|
2021-01-20 05:42:35 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
db_path: PathBuf,
|
|
|
|
listen_address: Multiaddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AliceParams {
|
2021-01-21 21:33:31 -05:00
|
|
|
pub fn builder(&self) -> alice::Builder {
|
2021-01-20 05:42:35 -05:00
|
|
|
alice::Builder::new(
|
|
|
|
self.seed,
|
2021-01-26 21:33:32 -05:00
|
|
|
self.execution_params,
|
2021-01-20 05:42:35 -05:00
|
|
|
self.swap_id,
|
|
|
|
self.bitcoin_wallet.clone(),
|
|
|
|
self.monero_wallet.clone(),
|
|
|
|
self.db_path.clone(),
|
|
|
|
self.listen_address.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
fn peer_id(&self) -> PeerId {
|
|
|
|
self.builder().peer_id()
|
2021-01-20 05:42:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2021-01-20 05:42:35 -05:00
|
|
|
struct BobParams {
|
|
|
|
seed: Seed,
|
|
|
|
db_path: PathBuf,
|
|
|
|
swap_id: Uuid,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
alice_address: Multiaddr,
|
|
|
|
alice_peer_id: PeerId,
|
2021-01-26 21:33:32 -05:00
|
|
|
execution_params: ExecutionParams,
|
2021-01-20 05:42:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BobParams {
|
|
|
|
pub fn builder(&self) -> bob::Builder {
|
|
|
|
bob::Builder::new(
|
|
|
|
self.seed,
|
|
|
|
self.db_path.clone(),
|
|
|
|
self.swap_id,
|
|
|
|
self.bitcoin_wallet.clone(),
|
|
|
|
self.monero_wallet.clone(),
|
|
|
|
self.alice_address.clone(),
|
2021-01-15 00:58:16 -05:00
|
|
|
self.alice_peer_id,
|
2021-01-26 21:33:32 -05:00
|
|
|
self.execution_params,
|
2021-01-20 05:42:35 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
pub struct BobEventLoopJoinHandle(JoinHandle<()>);
|
|
|
|
|
|
|
|
pub struct AliceEventLoopJoinHandle(JoinHandle<()>);
|
|
|
|
|
2021-01-19 18:40:40 -05:00
|
|
|
pub struct TestContext {
|
2021-01-18 03:56:43 -05:00
|
|
|
swap_amounts: SwapAmounts,
|
2021-01-18 23:03:30 -05:00
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
alice_params: AliceParams,
|
2021-01-18 22:48:07 -05:00
|
|
|
alice_starting_balances: StartingBalances,
|
2021-01-18 23:03:30 -05:00
|
|
|
alice_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
alice_monero_wallet: Arc<monero::Wallet>,
|
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
bob_params: BobParams,
|
2021-01-18 22:36:24 -05:00
|
|
|
bob_starting_balances: StartingBalances,
|
2021-01-18 23:09:05 -05:00
|
|
|
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
bob_monero_wallet: Arc<monero::Wallet>,
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 18:40:40 -05:00
|
|
|
impl TestContext {
|
2021-01-21 21:33:31 -05:00
|
|
|
pub async fn new_swap_as_alice(&mut self) -> (alice::Swap, AliceEventLoopJoinHandle) {
|
2021-01-18 03:56:43 -05:00
|
|
|
let (swap, mut event_loop) = self
|
2021-01-20 05:42:35 -05:00
|
|
|
.alice_params
|
|
|
|
.builder()
|
2021-01-18 23:21:40 -05:00
|
|
|
.with_init_params(self.swap_amounts)
|
|
|
|
.build()
|
2021-01-18 05:24:13 -05:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
let join_handle = tokio::spawn(async move { event_loop.run().await });
|
2021-01-18 03:56:43 -05:00
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
(swap, AliceEventLoopJoinHandle(join_handle))
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
pub async fn new_swap_as_bob(&mut self) -> (bob::Swap, BobEventLoopJoinHandle) {
|
2021-01-18 03:56:43 -05:00
|
|
|
let (swap, event_loop) = self
|
2021-01-20 05:42:35 -05:00
|
|
|
.bob_params
|
|
|
|
.builder()
|
2021-01-18 20:43:20 -05:00
|
|
|
.with_init_params(self.swap_amounts)
|
2021-01-18 23:21:40 -05:00
|
|
|
.build()
|
2021-01-18 05:57:17 -05:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
let join_handle = tokio::spawn(async move { event_loop.run().await });
|
2021-01-18 03:56:43 -05:00
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
(swap, BobEventLoopJoinHandle(join_handle))
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
pub async fn stop_and_resume_alice_from_db(
|
|
|
|
&mut self,
|
|
|
|
join_handle: AliceEventLoopJoinHandle,
|
|
|
|
) -> alice::Swap {
|
|
|
|
join_handle.0.abort();
|
|
|
|
|
|
|
|
let (swap, mut event_loop) = self.alice_params.builder().build().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
|
|
|
|
swap
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:33:31 -05:00
|
|
|
pub async fn stop_and_resume_bob_from_db(
|
|
|
|
&mut self,
|
|
|
|
join_handle: BobEventLoopJoinHandle,
|
|
|
|
) -> bob::Swap {
|
|
|
|
join_handle.0.abort();
|
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
let (swap, event_loop) = self.bob_params.builder().build().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
|
|
|
|
swap
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_alice_redeemed(&self, state: AliceState) {
|
|
|
|
assert!(matches!(state, AliceState::BtcRedeemed));
|
|
|
|
|
2021-01-18 23:03:30 -05:00
|
|
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
btc_balance_after_swap,
|
2021-01-18 22:48:07 -05:00
|
|
|
self.alice_starting_balances.btc + self.swap_amounts.btc
|
2021-01-18 03:56:43 -05:00
|
|
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
|
|
|
|
let xmr_balance_after_swap = self
|
2021-01-18 23:03:30 -05:00
|
|
|
.alice_monero_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.as_ref()
|
|
|
|
.get_balance()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-18 22:48:07 -05:00
|
|
|
assert!(xmr_balance_after_swap <= self.alice_starting_balances.xmr - self.swap_amounts.xmr);
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_alice_refunded(&self, state: AliceState) {
|
|
|
|
assert!(matches!(state, AliceState::XmrRefunded));
|
|
|
|
|
2021-01-18 23:03:30 -05:00
|
|
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 22:48:07 -05:00
|
|
|
assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc);
|
2021-01-18 03:56:43 -05:00
|
|
|
|
|
|
|
// Ensure that Alice's balance is refreshed as we use a newly created wallet
|
2021-01-18 23:03:30 -05:00
|
|
|
self.alice_monero_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.as_ref()
|
|
|
|
.inner
|
|
|
|
.refresh()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let xmr_balance_after_swap = self
|
2021-01-18 23:03:30 -05:00
|
|
|
.alice_monero_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.as_ref()
|
|
|
|
.get_balance()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(xmr_balance_after_swap, self.swap_amounts.xmr);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_alice_punished(&self, state: AliceState) {
|
|
|
|
assert!(matches!(state, AliceState::BtcPunished));
|
|
|
|
|
2021-01-18 23:03:30 -05:00
|
|
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
btc_balance_after_swap,
|
2021-01-18 22:48:07 -05:00
|
|
|
self.alice_starting_balances.btc + self.swap_amounts.btc
|
2021-01-18 03:56:43 -05:00
|
|
|
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
|
|
|
|
let xmr_balance_after_swap = self
|
2021-01-18 23:03:30 -05:00
|
|
|
.alice_monero_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.as_ref()
|
|
|
|
.get_balance()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-18 22:48:07 -05:00
|
|
|
assert!(xmr_balance_after_swap <= self.alice_starting_balances.xmr - self.swap_amounts.xmr);
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_bob_redeemed(&self, state: BobState) {
|
|
|
|
let lock_tx_id = if let BobState::XmrRedeemed { tx_lock_id } = state {
|
|
|
|
tx_lock_id
|
|
|
|
} else {
|
2021-01-26 21:41:08 -05:00
|
|
|
panic!("Bob in not in xmr redeemed state: {:?}", state);
|
2021-01-18 03:56:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = self
|
2021-01-18 23:09:05 -05:00
|
|
|
.bob_bitcoin_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.transaction_fee(lock_tx_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-01-18 23:09:05 -05:00
|
|
|
let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
btc_balance_after_swap,
|
2021-01-18 22:36:24 -05:00
|
|
|
self.bob_starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee
|
2021-01-18 03:56:43 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Ensure that Bob's balance is refreshed as we use a newly created wallet
|
2021-01-18 23:09:05 -05:00
|
|
|
self.bob_monero_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.as_ref()
|
|
|
|
.inner
|
|
|
|
.refresh()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-18 23:09:05 -05:00
|
|
|
let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
xmr_balance_after_swap,
|
2021-01-18 22:36:24 -05:00
|
|
|
self.bob_starting_balances.xmr + self.swap_amounts.xmr
|
2021-01-18 03:56:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_bob_refunded(&self, state: BobState) {
|
|
|
|
let lock_tx_id = if let BobState::BtcRefunded(state4) = state {
|
|
|
|
state4.tx_lock_id()
|
|
|
|
} else {
|
2021-01-26 21:41:08 -05:00
|
|
|
panic!("Bob in not in btc refunded state: {:?}", state);
|
2021-01-18 03:56:43 -05:00
|
|
|
};
|
|
|
|
let lock_tx_bitcoin_fee = self
|
2021-01-18 23:09:05 -05:00
|
|
|
.bob_bitcoin_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.transaction_fee(lock_tx_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-01-18 23:09:05 -05:00
|
|
|
let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
|
|
|
|
let alice_submitted_cancel = btc_balance_after_swap
|
2021-01-18 22:36:24 -05:00
|
|
|
== self.bob_starting_balances.btc
|
2021-01-18 03:56:43 -05:00
|
|
|
- lock_tx_bitcoin_fee
|
|
|
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE);
|
|
|
|
|
|
|
|
let bob_submitted_cancel = btc_balance_after_swap
|
2021-01-18 22:36:24 -05:00
|
|
|
== self.bob_starting_balances.btc
|
2021-01-18 03:56:43 -05:00
|
|
|
- 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);
|
|
|
|
|
2021-01-18 23:09:05 -05:00
|
|
|
let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap();
|
2021-01-18 22:36:24 -05:00
|
|
|
assert_eq!(xmr_balance_after_swap, self.bob_starting_balances.xmr);
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn assert_bob_punished(&self, state: BobState) {
|
|
|
|
let lock_tx_id = if let BobState::BtcPunished { tx_lock_id } = state {
|
|
|
|
tx_lock_id
|
|
|
|
} else {
|
2021-01-26 21:41:08 -05:00
|
|
|
panic!("Bob in not in btc punished state: {:?}", state);
|
2021-01-18 03:56:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = self
|
2021-01-18 23:09:05 -05:00
|
|
|
.bob_bitcoin_wallet
|
2021-01-18 03:56:43 -05:00
|
|
|
.transaction_fee(lock_tx_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-01-18 23:09:05 -05:00
|
|
|
let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap();
|
2021-01-18 03:56:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
btc_balance_after_swap,
|
2021-01-18 22:36:24 -05:00
|
|
|
self.bob_starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee
|
2021-01-18 03:56:43 -05:00
|
|
|
);
|
|
|
|
|
2021-01-18 23:09:05 -05:00
|
|
|
let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap();
|
2021-01-18 22:36:24 -05:00
|
|
|
assert_eq!(xmr_balance_after_swap, self.bob_starting_balances.xmr);
|
2021-01-18 03:56:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:52:05 -05:00
|
|
|
pub async fn setup_test<T, F, C>(_config: C, testfn: T)
|
2021-01-17 21:03:56 -05:00
|
|
|
where
|
2021-01-19 18:40:40 -05:00
|
|
|
T: Fn(TestContext) -> F,
|
2021-01-17 21:03:56 -05:00
|
|
|
F: Future<Output = ()>,
|
2021-01-26 21:33:32 -05:00
|
|
|
C: GetExecutionParams,
|
2021-01-17 21:03:56 -05:00
|
|
|
{
|
|
|
|
let cli = Cli::default();
|
|
|
|
|
|
|
|
let _guard = init_tracing();
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
let execution_params = C::get_execution_params();
|
2021-01-28 21:52:05 -05:00
|
|
|
|
2021-01-17 21:03:56 -05:00
|
|
|
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 alice_starting_balances = StartingBalances {
|
|
|
|
xmr: swap_amounts.xmr * 10,
|
|
|
|
btc: bitcoin::Amount::ZERO,
|
|
|
|
};
|
2021-01-18 05:24:13 -05:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
|
2021-01-18 05:24:13 -05:00
|
|
|
"alice",
|
|
|
|
&containers.bitcoind,
|
|
|
|
&monero,
|
|
|
|
alice_starting_balances.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
let alice_params = AliceParams {
|
|
|
|
seed: Seed::random().unwrap(),
|
2021-01-26 21:33:32 -05:00
|
|
|
execution_params,
|
2021-01-20 05:42:35 -05:00
|
|
|
swap_id: Uuid::new_v4(),
|
|
|
|
bitcoin_wallet: alice_bitcoin_wallet.clone(),
|
|
|
|
monero_wallet: alice_monero_wallet.clone(),
|
|
|
|
db_path: tempdir().unwrap().path().to_path_buf(),
|
2021-01-18 05:24:13 -05:00
|
|
|
listen_address,
|
2021-01-20 05:42:35 -05:00
|
|
|
};
|
2021-01-17 21:03:56 -05:00
|
|
|
|
|
|
|
let bob_starting_balances = StartingBalances {
|
|
|
|
xmr: monero::Amount::ZERO,
|
|
|
|
btc: swap_amounts.btc * 10,
|
|
|
|
};
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
let (bob_bitcoin_wallet, bob_monero_wallet) = init_test_wallets(
|
2021-01-18 05:57:17 -05:00
|
|
|
"bob",
|
|
|
|
&containers.bitcoind,
|
|
|
|
&monero,
|
|
|
|
bob_starting_balances.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2021-01-20 05:42:35 -05:00
|
|
|
let bob_params = BobParams {
|
|
|
|
seed: Seed::random().unwrap(),
|
|
|
|
db_path: tempdir().unwrap().path().to_path_buf(),
|
|
|
|
swap_id: Uuid::new_v4(),
|
|
|
|
bitcoin_wallet: bob_bitcoin_wallet.clone(),
|
|
|
|
monero_wallet: bob_monero_wallet.clone(),
|
|
|
|
alice_address: alice_params.listen_address.clone(),
|
2021-01-21 21:33:31 -05:00
|
|
|
alice_peer_id: alice_params.peer_id(),
|
2021-01-26 21:33:32 -05:00
|
|
|
execution_params,
|
2021-01-20 05:42:35 -05:00
|
|
|
};
|
2021-01-17 21:03:56 -05:00
|
|
|
|
2021-01-19 18:40:40 -05:00
|
|
|
let test = TestContext {
|
2021-01-18 03:56:43 -05:00
|
|
|
swap_amounts,
|
2021-01-20 05:42:35 -05:00
|
|
|
alice_params,
|
2021-01-18 22:48:07 -05:00
|
|
|
alice_starting_balances,
|
2021-01-18 23:03:30 -05:00
|
|
|
alice_bitcoin_wallet,
|
|
|
|
alice_monero_wallet,
|
2021-01-20 05:42:35 -05:00
|
|
|
bob_params,
|
2021-01-18 22:36:24 -05:00
|
|
|
bob_starting_balances,
|
2021-01-18 23:09:05 -05:00
|
|
|
bob_bitcoin_wallet,
|
|
|
|
bob_monero_wallet,
|
2021-01-18 03:56:43 -05:00
|
|
|
};
|
2021-01-15 02:34:46 -05:00
|
|
|
|
2021-01-18 03:56:43 -05:00
|
|
|
testfn(test).await
|
2021-01-14 19:26:32 -05:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:57:27 -05:00
|
|
|
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
2020-12-14 05:48:42 -05:00
|
|
|
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
|
|
|
let _ = bitcoind.init(5).await;
|
|
|
|
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(monero, Containers { bitcoind, monerods })
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
async fn init_test_wallets(
|
2020-12-14 05:48:42 -05:00
|
|
|
name: &str,
|
2020-12-09 22:59:09 -05:00
|
|
|
bitcoind: &Bitcoind<'_>,
|
|
|
|
monero: &Monero,
|
2021-01-15 03:03:11 -05:00
|
|
|
starting_balances: StartingBalances,
|
2020-12-14 05:48:42 -05:00
|
|
|
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
2021-01-15 03:03:11 -05:00
|
|
|
monero
|
|
|
|
.init(vec![(name, starting_balances.xmr.as_piconero())])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-12-09 22:59:09 -05:00
|
|
|
|
2021-01-04 04:29:11 -05:00
|
|
|
let xmr_wallet = Arc::new(swap::monero::Wallet {
|
|
|
|
inner: monero.wallet(name).unwrap().client(),
|
2021-01-26 21:33:32 -05:00
|
|
|
network: monero::Network::default(),
|
2021-01-04 04:29:11 -05:00
|
|
|
});
|
2020-12-09 22:59:09 -05:00
|
|
|
|
2020-12-14 05:48:42 -05:00
|
|
|
let btc_wallet = Arc::new(
|
2021-01-26 21:33:32 -05:00
|
|
|
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), bitcoin::Network::Regtest)
|
2020-12-14 05:48:42 -05:00
|
|
|
.await
|
|
|
|
.unwrap(),
|
2020-12-09 22:59:09 -05:00
|
|
|
);
|
|
|
|
|
2021-01-15 03:03:11 -05:00
|
|
|
if starting_balances.btc != bitcoin::Amount::ZERO {
|
2020-12-14 05:48:42 -05:00
|
|
|
bitcoind
|
2021-01-15 03:03:11 -05:00
|
|
|
.mint(
|
|
|
|
btc_wallet.inner.new_address().await.unwrap(),
|
|
|
|
starting_balances.btc,
|
|
|
|
)
|
2020-12-14 05:48:42 -05:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-12-09 22:59:09 -05:00
|
|
|
|
2020-12-14 05:48:42 -05:00
|
|
|
(btc_wallet, xmr_wallet)
|
2020-12-09 22:59:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is just to keep the containers alive
|
|
|
|
#[allow(dead_code)]
|
2021-01-17 20:57:27 -05:00
|
|
|
struct Containers<'a> {
|
|
|
|
bitcoind: Bitcoind<'a>,
|
|
|
|
monerods: Vec<Container<'a, Cli, image::Monero>>,
|
2020-12-09 22:59:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Utility function to initialize logging in the test environment.
|
|
|
|
/// Note that you have to keep the `_guard` in scope after calling in test:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let _guard = init_tracing();
|
|
|
|
/// ```
|
2021-01-17 20:57:27 -05:00
|
|
|
fn init_tracing() -> DefaultGuard {
|
2020-12-09 22:59:09 -05:00
|
|
|
// converts all log records into tracing events
|
|
|
|
// Note: Make sure to initialize without unwrapping, otherwise this causes
|
|
|
|
// trouble when running multiple tests.
|
|
|
|
let _ = LogTracer::init();
|
|
|
|
|
|
|
|
let global_filter = tracing::Level::WARN;
|
|
|
|
let swap_filter = tracing::Level::DEBUG;
|
|
|
|
let xmr_btc_filter = tracing::Level::DEBUG;
|
|
|
|
let monero_harness_filter = tracing::Level::INFO;
|
|
|
|
let bitcoin_harness_filter = tracing::Level::INFO;
|
|
|
|
|
|
|
|
use tracing_subscriber::util::SubscriberInitExt as _;
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(format!(
|
2020-12-21 22:11:55 -05:00
|
|
|
"{},swap={},xmr_btc={},monero_harness={},bitcoin_harness={}",
|
2020-12-09 22:59:09 -05:00
|
|
|
global_filter,
|
|
|
|
swap_filter,
|
|
|
|
xmr_btc_filter,
|
|
|
|
monero_harness_filter,
|
|
|
|
bitcoin_harness_filter,
|
|
|
|
))
|
|
|
|
.set_default()
|
|
|
|
}
|
2021-01-21 03:37:52 -05:00
|
|
|
|
|
|
|
pub mod alice_run_until {
|
|
|
|
use swap::protocol::alice::AliceState;
|
|
|
|
|
|
|
|
pub fn is_xmr_locked(state: &AliceState) -> bool {
|
2021-02-03 00:20:59 -05:00
|
|
|
matches!(state, AliceState::XmrLocked { .. })
|
2021-01-21 03:37:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_encsig_learned(state: &AliceState) -> bool {
|
2021-02-03 00:20:59 -05:00
|
|
|
matches!(state, AliceState::EncSigLearned { .. })
|
2021-01-21 03:37:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod bob_run_until {
|
|
|
|
use swap::protocol::bob::BobState;
|
|
|
|
|
|
|
|
pub fn is_btc_locked(state: &BobState) -> bool {
|
|
|
|
matches!(state, BobState::BtcLocked(..))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_lock_proof_received(state: &BobState) -> bool {
|
|
|
|
matches!(state, BobState::XmrLockProofReceived { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_xmr_locked(state: &BobState) -> bool {
|
|
|
|
matches!(state, BobState::XmrLocked(..))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_encsig_sent(state: &BobState) -> bool {
|
|
|
|
matches!(state, BobState::EncSigSent(..))
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 01:03:52 -05:00
|
|
|
|
|
|
|
pub struct SlowCancelConfig;
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
impl GetExecutionParams for SlowCancelConfig {
|
|
|
|
fn get_execution_params() -> ExecutionParams {
|
|
|
|
ExecutionParams {
|
2021-01-27 01:03:52 -05:00
|
|
|
bitcoin_cancel_timelock: Timelock::new(180),
|
2021-01-29 01:27:50 -05:00
|
|
|
..execution_params::Regtest::get_execution_params()
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FastCancelConfig;
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
impl GetExecutionParams for FastCancelConfig {
|
|
|
|
fn get_execution_params() -> ExecutionParams {
|
|
|
|
ExecutionParams {
|
2021-01-27 01:03:52 -05:00
|
|
|
bitcoin_cancel_timelock: Timelock::new(1),
|
2021-01-29 01:27:50 -05:00
|
|
|
..execution_params::Regtest::get_execution_params()
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FastPunishConfig;
|
|
|
|
|
2021-01-26 21:33:32 -05:00
|
|
|
impl GetExecutionParams for FastPunishConfig {
|
|
|
|
fn get_execution_params() -> ExecutionParams {
|
|
|
|
ExecutionParams {
|
2021-01-27 01:03:52 -05:00
|
|
|
bitcoin_cancel_timelock: Timelock::new(1),
|
|
|
|
bitcoin_punish_timelock: Timelock::new(1),
|
2021-01-29 01:27:50 -05:00
|
|
|
..execution_params::Regtest::get_execution_params()
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|