2020-10-15 10:53:55 +00:00
|
|
|
pub mod harness;
|
|
|
|
|
2020-09-29 05:36:50 +00:00
|
|
|
mod tests {
|
2020-10-21 04:52:51 +00:00
|
|
|
// NOTE: For some reason running these tests overflows the stack. In order to
|
|
|
|
// mitigate this run them with:
|
|
|
|
//
|
|
|
|
// RUST_MIN_STACK=100000000 cargo test
|
|
|
|
|
2020-09-29 05:36:50 +00:00
|
|
|
use crate::{
|
2020-10-08 22:28:51 +00:00
|
|
|
harness,
|
2020-10-22 03:42:36 +00:00
|
|
|
harness::{
|
|
|
|
init_bitcoind, init_test,
|
|
|
|
node::{run_alice_until, run_bob_until},
|
|
|
|
ALICE_TEST_DB_FOLDER, BOB_TEST_DB_FOLDER,
|
|
|
|
},
|
2020-09-29 05:36:50 +00:00
|
|
|
};
|
2020-10-08 22:28:51 +00:00
|
|
|
use futures::future;
|
2020-09-29 05:36:50 +00:00
|
|
|
use monero_harness::Monero;
|
|
|
|
use rand::rngs::OsRng;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
use std::{convert::TryInto, path::Path};
|
2020-10-08 22:28:51 +00:00
|
|
|
use testcontainers::clients::Cli;
|
2020-09-29 05:36:50 +00:00
|
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
use xmr_btc::{
|
|
|
|
alice, bitcoin,
|
|
|
|
bitcoin::{Amount, TX_FEE},
|
|
|
|
bob,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn happy_path() {
|
|
|
|
let _guard = tracing_subscriber::fmt()
|
|
|
|
.with_env_filter("info")
|
|
|
|
.set_default();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-10-20 01:18:27 +00:00
|
|
|
let (monero, _container) = Monero::new(&cli);
|
2020-09-29 05:36:50 +00:00
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (
|
|
|
|
alice_state0,
|
|
|
|
bob_state0,
|
|
|
|
mut alice_node,
|
|
|
|
mut bob_node,
|
|
|
|
initial_balances,
|
|
|
|
swap_amounts,
|
2020-10-21 23:57:42 +00:00
|
|
|
) = init_test(&monero, &bitcoind, None, None).await;
|
2020-09-29 05:36:50 +00:00
|
|
|
|
|
|
|
let (alice_state, bob_state) = future::try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
2020-10-08 22:28:51 +00:00
|
|
|
harness::alice::is_state6,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state5,
|
2020-09-29 05:36:50 +00:00
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_state6: alice::State6 = alice_state.try_into().unwrap();
|
|
|
|
let bob_state5: bob::State5 = bob_state.try_into().unwrap();
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = bob_node
|
|
|
|
.bitcoin_wallet
|
|
|
|
.transaction_fee(bob_state5.tx_lock_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-10-21 23:57:42 +00:00
|
|
|
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
2020-09-29 05:36:50 +00:00
|
|
|
|
2020-10-20 01:18:27 +00:00
|
|
|
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
2020-09-29 05:36:50 +00:00
|
|
|
|
2020-10-21 23:57:42 +00:00
|
|
|
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
2020-09-29 05:36:50 +00:00
|
|
|
|
2020-10-08 21:56:20 +00:00
|
|
|
assert_eq!(
|
|
|
|
alice_final_btc_balance,
|
|
|
|
initial_balances.alice_btc + swap_amounts.btc
|
|
|
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
2020-09-29 05:36:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
alice_final_xmr_balance,
|
2020-10-21 23:57:42 +00:00
|
|
|
initial_balances.alice_xmr - swap_amounts.xmr - alice_state6.lock_xmr_fee()
|
2020-09-29 05:36:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_xmr_balance,
|
2020-10-21 23:57:42 +00:00
|
|
|
initial_balances.bob_xmr + swap_amounts.xmr
|
2020-09-29 05:36:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn both_refund() {
|
|
|
|
let _guard = tracing_subscriber::fmt()
|
|
|
|
.with_env_filter("info")
|
|
|
|
.set_default();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-10-20 01:18:27 +00:00
|
|
|
let (monero, _container) = Monero::new(&cli);
|
2020-09-29 05:36:50 +00:00
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (
|
|
|
|
alice_state0,
|
|
|
|
bob_state0,
|
|
|
|
mut alice_node,
|
|
|
|
mut bob_node,
|
|
|
|
initial_balances,
|
|
|
|
swap_amounts,
|
2020-10-21 23:57:42 +00:00
|
|
|
) = init_test(&monero, &bitcoind, None, None).await;
|
2020-09-29 05:36:50 +00:00
|
|
|
|
|
|
|
let (alice_state, bob_state) = future::try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
2020-10-08 22:28:51 +00:00
|
|
|
harness::alice::is_state5,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state3,
|
2020-09-29 05:36:50 +00:00
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_state5: alice::State5 = alice_state.try_into().unwrap();
|
|
|
|
let bob_state3: bob::State3 = bob_state.try_into().unwrap();
|
|
|
|
|
|
|
|
bob_state3
|
|
|
|
.refund_btc(&bob_node.bitcoin_wallet)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
alice_state5
|
|
|
|
.refund_xmr(&alice_node.bitcoin_wallet, &alice_node.monero_wallet)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_node.bitcoin_wallet.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_node
|
|
|
|
.bitcoin_wallet
|
|
|
|
.transaction_fee(bob_state3.tx_lock_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-10-20 01:18:27 +00:00
|
|
|
monero.wait_for_alice_wallet_block_height().await.unwrap();
|
2020-10-21 23:57:42 +00:00
|
|
|
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
|
|
|
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
2020-09-29 05:36:50 +00:00
|
|
|
|
2020-10-08 21:56:20 +00:00
|
|
|
assert_eq!(alice_final_btc_balance, initial_balances.alice_btc);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
// The 2 * TX_FEE corresponds to tx_refund and tx_cancel.
|
|
|
|
initial_balances.bob_btc - Amount::from_sat(2 * TX_FEE) - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
2020-09-29 05:36:50 +00:00
|
|
|
// Because we create a new wallet when claiming Monero, we can only assert on
|
|
|
|
// this new wallet owning all of `xmr_amount` after refund
|
2020-10-21 23:57:42 +00:00
|
|
|
assert_eq!(alice_final_xmr_balance, swap_amounts.xmr);
|
2020-09-29 05:36:50 +00:00
|
|
|
assert_eq!(bob_final_xmr_balance, initial_balances.bob_xmr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn alice_punishes() {
|
|
|
|
let _guard = tracing_subscriber::fmt()
|
|
|
|
.with_env_filter("info")
|
|
|
|
.set_default();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-10-20 01:18:27 +00:00
|
|
|
let (monero, _container) = Monero::new(&cli);
|
2020-09-29 05:36:50 +00:00
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (
|
|
|
|
alice_state0,
|
|
|
|
bob_state0,
|
|
|
|
mut alice_node,
|
|
|
|
mut bob_node,
|
|
|
|
initial_balances,
|
|
|
|
swap_amounts,
|
2020-10-21 23:57:42 +00:00
|
|
|
) = init_test(&monero, &bitcoind, None, None).await;
|
2020-09-29 05:36:50 +00:00
|
|
|
|
|
|
|
let (alice_state, bob_state) = future::try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
2020-10-08 22:28:51 +00:00
|
|
|
harness::alice::is_state4,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state3,
|
2020-09-29 05:36:50 +00:00
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_state4: alice::State4 = alice_state.try_into().unwrap();
|
|
|
|
let bob_state3: bob::State3 = bob_state.try_into().unwrap();
|
|
|
|
|
|
|
|
alice_state4
|
|
|
|
.punish(&alice_node.bitcoin_wallet)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_node.bitcoin_wallet.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_node
|
|
|
|
.bitcoin_wallet
|
|
|
|
.transaction_fee(bob_state3.tx_lock_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
alice_final_btc_balance,
|
|
|
|
initial_balances.alice_btc + swap_amounts.btc - Amount::from_sat(2 * TX_FEE)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
}
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn recover_protocol_state_from_db() {
|
|
|
|
let _guard = tracing_subscriber::fmt()
|
|
|
|
.with_env_filter("info")
|
|
|
|
.set_default();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-10-21 21:28:51 +00:00
|
|
|
let (monero, _container) = Monero::new(&cli);
|
2020-10-13 22:32:25 +00:00
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
let alice_db = harness::storage::Database::open(Path::new(ALICE_TEST_DB_FOLDER)).unwrap();
|
|
|
|
let bob_db = harness::storage::Database::open(Path::new(BOB_TEST_DB_FOLDER)).unwrap();
|
|
|
|
|
|
|
|
let (
|
|
|
|
alice_state0,
|
|
|
|
bob_state0,
|
|
|
|
mut alice_node,
|
|
|
|
mut bob_node,
|
|
|
|
initial_balances,
|
|
|
|
swap_amounts,
|
2020-10-22 03:42:36 +00:00
|
|
|
) = init_test(&monero, &bitcoind, None, None).await;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let (alice_state, bob_state) = future::try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
|
|
|
harness::alice::is_state5,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state3,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_state5: alice::State5 = alice_state.try_into().unwrap();
|
|
|
|
let bob_state3: bob::State3 = bob_state.try_into().unwrap();
|
|
|
|
|
|
|
|
// save state to db
|
|
|
|
alice_db.insert_latest_state(&alice_state5).await.unwrap();
|
|
|
|
bob_db.insert_latest_state(&bob_state3).await.unwrap();
|
|
|
|
};
|
|
|
|
|
|
|
|
let (alice_state6, bob_state5) = {
|
|
|
|
// recover state from db
|
|
|
|
let alice_state5: alice::State5 = alice_db.get_latest_state().unwrap();
|
|
|
|
let bob_state3: bob::State3 = bob_db.get_latest_state().unwrap();
|
|
|
|
|
|
|
|
let (alice_state, bob_state) = future::try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state5.into(),
|
|
|
|
harness::alice::is_state6,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state3.into(),
|
|
|
|
harness::bob::is_state5,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_state: alice::State6 = alice_state.try_into().unwrap();
|
|
|
|
let bob_state: bob::State5 = bob_state.try_into().unwrap();
|
|
|
|
|
|
|
|
(alice_state, bob_state)
|
|
|
|
};
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_node.bitcoin_wallet.balance().await.unwrap();
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = bob_node
|
|
|
|
.bitcoin_wallet
|
|
|
|
.transaction_fee(bob_state5.tx_lock_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-10-21 21:28:51 +00:00
|
|
|
let alice_final_xmr_balance = alice_node.monero_wallet.0.get_balance(0).await.unwrap();
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-10-21 21:28:51 +00:00
|
|
|
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-10-21 21:28:51 +00:00
|
|
|
let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance(0).await.unwrap();
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
alice_final_btc_balance,
|
|
|
|
initial_balances.alice_btc + swap_amounts.btc
|
|
|
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
alice_final_xmr_balance,
|
2020-10-22 03:42:36 +00:00
|
|
|
initial_balances.alice_xmr.as_piconero()
|
|
|
|
- swap_amounts.xmr.as_piconero()
|
|
|
|
- alice_state6.lock_xmr_fee().as_piconero()
|
2020-10-13 22:32:25 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_xmr_balance,
|
2020-10-22 03:42:36 +00:00
|
|
|
initial_balances.bob_xmr.as_piconero() + swap_amounts.xmr.as_piconero()
|
2020-10-13 22:32:25 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-29 05:36:50 +00:00
|
|
|
}
|