2020-10-15 06:53:55 -04:00
|
|
|
pub mod harness;
|
2020-10-12 02:17:22 -04:00
|
|
|
|
|
|
|
use anyhow::Result;
|
2020-10-15 19:49:34 -04:00
|
|
|
use async_trait::async_trait;
|
2020-10-18 19:44:25 -04:00
|
|
|
use futures::{
|
2020-10-21 19:57:42 -04:00
|
|
|
channel::mpsc::{channel, Receiver, Sender},
|
2020-10-26 01:05:30 -04:00
|
|
|
future::{select, try_join},
|
|
|
|
pin_mut, SinkExt, StreamExt,
|
2020-10-18 19:44:25 -04:00
|
|
|
};
|
2020-10-12 02:17:22 -04:00
|
|
|
use genawaiter::GeneratorState;
|
2020-10-21 19:57:42 -04:00
|
|
|
use harness::{
|
|
|
|
init_bitcoind, init_test,
|
|
|
|
node::{run_alice_until, run_bob_until},
|
|
|
|
};
|
|
|
|
use monero_harness::Monero;
|
|
|
|
use rand::rngs::OsRng;
|
2020-10-26 21:11:03 -04:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2020-10-21 19:57:42 -04:00
|
|
|
use testcontainers::clients::Cli;
|
2020-10-26 21:11:03 -04:00
|
|
|
use tokio::sync::Mutex;
|
2020-10-21 19:57:42 -04:00
|
|
|
use tracing::info;
|
2020-10-12 02:17:22 -04:00
|
|
|
use xmr_btc::{
|
2020-10-25 19:59:28 -04:00
|
|
|
alice::{self, ReceiveBitcoinRedeemEncsig},
|
2020-10-18 19:44:25 -04:00
|
|
|
bitcoin::{BroadcastSignedTransaction, EncryptedSignature, SignTxLock},
|
2020-10-23 08:05:34 -04:00
|
|
|
bob::{self, ReceiveTransferProof},
|
2020-10-18 19:44:25 -04:00
|
|
|
monero::{CreateWalletForOutput, Transfer, TransferProof},
|
2020-10-12 02:17:22 -04:00
|
|
|
};
|
|
|
|
|
2020-10-25 21:03:14 -04:00
|
|
|
/// Time given to Bob to get the Bitcoin lock transaction included in a block.
|
|
|
|
const BITCOIN_TX_LOCK_TIMEOUT: u64 = 5;
|
|
|
|
|
2020-10-18 19:44:25 -04:00
|
|
|
type AliceNetwork = Network<EncryptedSignature>;
|
|
|
|
type BobNetwork = Network<TransferProof>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-10-21 19:57:42 -04:00
|
|
|
struct Network<M> {
|
2020-10-18 19:44:25 -04:00
|
|
|
// TODO: It is weird to use mpsc's in a situation where only one message is expected, but the
|
|
|
|
// ownership rules of Rust are making this painful
|
2020-10-21 19:57:42 -04:00
|
|
|
pub receiver: Receiver<M>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M> Network<M> {
|
|
|
|
pub fn new() -> (Network<M>, Sender<M>) {
|
|
|
|
let (sender, receiver) = channel(1);
|
|
|
|
|
|
|
|
(Self { receiver }, sender)
|
|
|
|
}
|
2020-10-18 19:44:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl ReceiveTransferProof for BobNetwork {
|
|
|
|
async fn receive_transfer_proof(&mut self) -> TransferProof {
|
|
|
|
self.receiver.next().await.unwrap()
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
|
2020-10-15 19:49:34 -04:00
|
|
|
#[async_trait]
|
2020-10-18 19:44:25 -04:00
|
|
|
impl ReceiveBitcoinRedeemEncsig for AliceNetwork {
|
|
|
|
async fn receive_bitcoin_redeem_encsig(&mut self) -> EncryptedSignature {
|
|
|
|
self.receiver.next().await.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
struct AliceBehaviour {
|
|
|
|
lock_xmr: bool,
|
|
|
|
redeem_btc: bool,
|
|
|
|
cancel_btc: bool,
|
|
|
|
punish_btc: bool,
|
|
|
|
create_monero_wallet_for_output: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AliceBehaviour {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
lock_xmr: true,
|
|
|
|
redeem_btc: true,
|
|
|
|
cancel_btc: true,
|
|
|
|
punish_btc: true,
|
|
|
|
create_monero_wallet_for_output: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:05:30 -04:00
|
|
|
struct BobBehaviour {
|
|
|
|
lock_btc: bool,
|
|
|
|
send_btc_redeem_encsig: bool,
|
|
|
|
create_monero_wallet_for_output: bool,
|
|
|
|
cancel_btc: bool,
|
|
|
|
refund_btc: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BobBehaviour {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
lock_btc: true,
|
|
|
|
send_btc_redeem_encsig: true,
|
|
|
|
create_monero_wallet_for_output: true,
|
|
|
|
cancel_btc: true,
|
|
|
|
refund_btc: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 19:44:25 -04:00
|
|
|
async fn swap_as_alice(
|
2020-10-26 21:11:03 -04:00
|
|
|
network: Arc<Mutex<AliceNetwork>>,
|
2020-10-18 19:44:25 -04:00
|
|
|
// FIXME: It would be more intuitive to have a single network/transport struct instead of
|
|
|
|
// splitting into two, but Rust ownership rules make this tedious
|
|
|
|
mut sender: Sender<TransferProof>,
|
2020-10-26 01:05:30 -04:00
|
|
|
monero_wallet: Arc<harness::wallet::monero::Wallet>,
|
2020-10-21 19:57:42 -04:00
|
|
|
bitcoin_wallet: Arc<harness::wallet::bitcoin::Wallet>,
|
2020-10-23 08:05:34 -04:00
|
|
|
behaviour: AliceBehaviour,
|
2020-10-18 19:44:25 -04:00
|
|
|
state: alice::State3,
|
|
|
|
) -> Result<()> {
|
2020-10-25 21:03:14 -04:00
|
|
|
let mut action_generator = alice::action_generator(
|
|
|
|
network,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
state,
|
|
|
|
BITCOIN_TX_LOCK_TIMEOUT,
|
|
|
|
);
|
2020-10-18 19:44:25 -04:00
|
|
|
|
|
|
|
loop {
|
2020-10-21 19:57:42 -04:00
|
|
|
let state = action_generator.async_resume().await;
|
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
info!("resumed execution of alice generator, got: {:?}", state);
|
2020-10-21 19:57:42 -04:00
|
|
|
|
|
|
|
match state {
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(alice::Action::LockXmr {
|
2020-10-18 19:44:25 -04:00
|
|
|
amount,
|
|
|
|
public_spend_key,
|
|
|
|
public_view_key,
|
|
|
|
}) => {
|
2020-10-23 08:05:34 -04:00
|
|
|
if behaviour.lock_xmr {
|
|
|
|
let (transfer_proof, _) = monero_wallet
|
|
|
|
.transfer(public_spend_key, public_view_key, amount)
|
|
|
|
.await?;
|
2020-10-18 19:44:25 -04:00
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
sender.send(transfer_proof).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GeneratorState::Yielded(alice::Action::RedeemBtc(tx)) => {
|
|
|
|
if behaviour.redeem_btc {
|
|
|
|
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GeneratorState::Yielded(alice::Action::CancelBtc(tx)) => {
|
|
|
|
if behaviour.cancel_btc {
|
|
|
|
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
|
|
|
}
|
2020-10-18 19:44:25 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(alice::Action::PunishBtc(tx)) => {
|
|
|
|
if behaviour.punish_btc {
|
|
|
|
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
|
|
|
}
|
2020-10-18 19:44:25 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(alice::Action::CreateMoneroWalletForOutput {
|
2020-10-18 19:44:25 -04:00
|
|
|
spend_key,
|
|
|
|
view_key,
|
|
|
|
}) => {
|
2020-10-23 08:05:34 -04:00
|
|
|
if behaviour.create_monero_wallet_for_output {
|
|
|
|
monero_wallet
|
|
|
|
.create_and_load_wallet_for_output(spend_key, view_key)
|
|
|
|
.await?;
|
|
|
|
}
|
2020-10-18 19:44:25 -04:00
|
|
|
}
|
|
|
|
GeneratorState::Complete(()) => return Ok(()),
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn swap_as_bob(
|
2020-10-27 02:18:19 -04:00
|
|
|
network: Arc<Mutex<BobNetwork>>,
|
2020-10-18 19:44:25 -04:00
|
|
|
mut sender: Sender<EncryptedSignature>,
|
2020-10-21 19:57:42 -04:00
|
|
|
monero_wallet: Arc<harness::wallet::monero::Wallet>,
|
|
|
|
bitcoin_wallet: Arc<harness::wallet::bitcoin::Wallet>,
|
2020-10-26 01:05:30 -04:00
|
|
|
behaviour: BobBehaviour,
|
2020-10-12 02:17:22 -04:00
|
|
|
state: bob::State2,
|
|
|
|
) -> Result<()> {
|
2020-10-23 08:05:34 -04:00
|
|
|
let mut action_generator = bob::action_generator(
|
2020-10-21 19:57:42 -04:00
|
|
|
network,
|
|
|
|
monero_wallet.clone(),
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
state,
|
2020-10-25 21:03:14 -04:00
|
|
|
BITCOIN_TX_LOCK_TIMEOUT,
|
2020-10-21 19:57:42 -04:00
|
|
|
);
|
2020-10-12 02:17:22 -04:00
|
|
|
|
|
|
|
loop {
|
2020-10-21 19:57:42 -04:00
|
|
|
let state = action_generator.async_resume().await;
|
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
info!("resumed execution of bob generator, got: {:?}", state);
|
2020-10-21 19:57:42 -04:00
|
|
|
|
|
|
|
match state {
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(bob::Action::LockBtc(tx_lock)) => {
|
2020-10-26 01:05:30 -04:00
|
|
|
if behaviour.lock_btc {
|
|
|
|
let signed_tx_lock = bitcoin_wallet.sign_tx_lock(tx_lock).await?;
|
|
|
|
let _ = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(signed_tx_lock)
|
|
|
|
.await?;
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(bob::Action::SendBtcRedeemEncsig(tx_redeem_encsig)) => {
|
2020-10-26 01:05:30 -04:00
|
|
|
if behaviour.send_btc_redeem_encsig {
|
|
|
|
sender.send(tx_redeem_encsig).await.unwrap();
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(bob::Action::CreateXmrWalletForOutput {
|
2020-10-12 02:17:22 -04:00
|
|
|
spend_key,
|
|
|
|
view_key,
|
|
|
|
}) => {
|
2020-10-26 01:05:30 -04:00
|
|
|
if behaviour.create_monero_wallet_for_output {
|
|
|
|
monero_wallet
|
|
|
|
.create_and_load_wallet_for_output(spend_key, view_key)
|
|
|
|
.await?;
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(bob::Action::CancelBtc(tx_cancel)) => {
|
2020-10-26 01:05:30 -04:00
|
|
|
if behaviour.cancel_btc {
|
|
|
|
let _ = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(tx_cancel)
|
|
|
|
.await?;
|
|
|
|
}
|
2020-10-15 23:06:25 -04:00
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
GeneratorState::Yielded(bob::Action::RefundBtc(tx_refund)) => {
|
2020-10-26 01:05:30 -04:00
|
|
|
if behaviour.refund_btc {
|
|
|
|
let _ = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(tx_refund)
|
|
|
|
.await?;
|
|
|
|
}
|
2020-10-12 02:17:22 -04:00
|
|
|
}
|
|
|
|
GeneratorState::Complete(()) => return Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:57:42 -04: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
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn on_chain_happy_path() {
|
|
|
|
let cli = Cli::default();
|
2020-10-22 20:28:58 -04:00
|
|
|
let (monero, _container) = Monero::new(&cli).unwrap();
|
2020-10-21 19:57:42 -04:00
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
|
|
|
init_test(&monero, &bitcoind, Some(100), Some(100)).await;
|
|
|
|
|
|
|
|
// run the handshake as part of the setup
|
|
|
|
let (alice_state, bob_state) = try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
|
|
|
harness::alice::is_state3,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state2,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let alice: alice::State3 = alice_state.try_into().unwrap();
|
|
|
|
let bob: bob::State2 = bob_state.try_into().unwrap();
|
|
|
|
let tx_lock_txid = bob.tx_lock.txid();
|
|
|
|
|
|
|
|
let alice_bitcoin_wallet = Arc::new(alice_node.bitcoin_wallet);
|
|
|
|
let bob_bitcoin_wallet = Arc::new(bob_node.bitcoin_wallet);
|
|
|
|
let alice_monero_wallet = Arc::new(alice_node.monero_wallet);
|
|
|
|
let bob_monero_wallet = Arc::new(bob_node.monero_wallet);
|
|
|
|
|
|
|
|
let (alice_network, bob_sender) = Network::<EncryptedSignature>::new();
|
|
|
|
let (bob_network, alice_sender) = Network::<TransferProof>::new();
|
|
|
|
|
|
|
|
try_join(
|
|
|
|
swap_as_alice(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(alice_network)),
|
2020-10-21 19:57:42 -04:00
|
|
|
alice_sender,
|
2020-10-26 01:05:30 -04:00
|
|
|
alice_monero_wallet.clone(),
|
2020-10-21 19:57:42 -04:00
|
|
|
alice_bitcoin_wallet.clone(),
|
2020-10-23 08:05:34 -04:00
|
|
|
AliceBehaviour::default(),
|
2020-10-21 19:57:42 -04:00
|
|
|
alice,
|
|
|
|
),
|
|
|
|
swap_as_bob(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(bob_network)),
|
2020-10-21 19:57:42 -04:00
|
|
|
bob_sender,
|
|
|
|
bob_monero_wallet.clone(),
|
|
|
|
bob_bitcoin_wallet.clone(),
|
2020-10-26 01:05:30 -04:00
|
|
|
BobBehaviour::default(),
|
2020-10-21 19:57:42 -04:00
|
|
|
bob,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = bob_bitcoin_wallet
|
|
|
|
.transaction_fee(tx_lock_txid)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
|
|
|
|
|
|
|
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
|
|
|
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
alice_final_btc_balance,
|
|
|
|
initial_balances.alice_btc + swap_amounts.btc
|
|
|
|
- bitcoin::Amount::from_sat(xmr_btc::bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
|
|
|
// Getting the Monero LockTx fee is tricky in a clean way, I think checking this
|
|
|
|
// condition is sufficient
|
2020-10-26 01:05:30 -04:00
|
|
|
assert!(alice_final_xmr_balance <= initial_balances.alice_xmr - swap_amounts.xmr);
|
2020-10-21 19:57:42 -04:00
|
|
|
assert_eq!(
|
|
|
|
bob_final_xmr_balance,
|
|
|
|
initial_balances.bob_xmr + swap_amounts.xmr
|
|
|
|
);
|
|
|
|
}
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn on_chain_both_refund_if_alice_never_redeems() {
|
|
|
|
let cli = Cli::default();
|
|
|
|
let (monero, _container) = Monero::new(&cli).unwrap();
|
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
|
|
|
init_test(&monero, &bitcoind, Some(10), Some(10)).await;
|
|
|
|
|
|
|
|
// run the handshake as part of the setup
|
|
|
|
let (alice_state, bob_state) = try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
|
|
|
harness::alice::is_state3,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state2,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let alice: alice::State3 = alice_state.try_into().unwrap();
|
|
|
|
let bob: bob::State2 = bob_state.try_into().unwrap();
|
|
|
|
let tx_lock_txid = bob.tx_lock.txid();
|
|
|
|
|
|
|
|
let alice_bitcoin_wallet = Arc::new(alice_node.bitcoin_wallet);
|
|
|
|
let bob_bitcoin_wallet = Arc::new(bob_node.bitcoin_wallet);
|
|
|
|
let alice_monero_wallet = Arc::new(alice_node.monero_wallet);
|
|
|
|
let bob_monero_wallet = Arc::new(bob_node.monero_wallet);
|
|
|
|
|
|
|
|
let (alice_network, bob_sender) = Network::<EncryptedSignature>::new();
|
|
|
|
let (bob_network, alice_sender) = Network::<TransferProof>::new();
|
|
|
|
|
|
|
|
try_join(
|
|
|
|
swap_as_alice(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(alice_network)),
|
2020-10-23 08:05:34 -04:00
|
|
|
alice_sender,
|
2020-10-26 01:05:30 -04:00
|
|
|
alice_monero_wallet.clone(),
|
2020-10-23 08:05:34 -04:00
|
|
|
alice_bitcoin_wallet.clone(),
|
|
|
|
AliceBehaviour {
|
|
|
|
redeem_btc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
alice,
|
|
|
|
),
|
|
|
|
swap_as_bob(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(bob_network)),
|
2020-10-23 08:05:34 -04:00
|
|
|
bob_sender,
|
|
|
|
bob_monero_wallet.clone(),
|
|
|
|
bob_bitcoin_wallet.clone(),
|
2020-10-26 01:05:30 -04:00
|
|
|
BobBehaviour::default(),
|
2020-10-23 08:05:34 -04:00
|
|
|
bob,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = bob_bitcoin_wallet
|
|
|
|
.transaction_fee(tx_lock_txid)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
monero.wait_for_alice_wallet_block_height().await.unwrap();
|
|
|
|
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
|
|
|
|
|
|
|
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
|
|
|
|
|
|
|
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
|
|
|
|
- bitcoin::Amount::from_sat(2 * xmr_btc::bitcoin::TX_FEE)
|
|
|
|
- lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
|
|
|
// Because we create a new wallet when claiming Monero, we can only assert on
|
|
|
|
// this new wallet owning all of `xmr_amount` after refund
|
|
|
|
assert_eq!(alice_final_xmr_balance, swap_amounts.xmr);
|
|
|
|
assert_eq!(bob_final_xmr_balance, initial_balances.bob_xmr);
|
|
|
|
}
|
2020-10-26 01:05:30 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() {
|
|
|
|
let cli = Cli::default();
|
|
|
|
let (monero, _container) = Monero::new(&cli).unwrap();
|
|
|
|
let bitcoind = init_bitcoind(&cli).await;
|
|
|
|
|
|
|
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
|
|
|
init_test(&monero, &bitcoind, Some(10), Some(10)).await;
|
|
|
|
|
|
|
|
// run the handshake as part of the setup
|
|
|
|
let (alice_state, bob_state) = try_join(
|
|
|
|
run_alice_until(
|
|
|
|
&mut alice_node,
|
|
|
|
alice_state0.into(),
|
|
|
|
harness::alice::is_state3,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
run_bob_until(
|
|
|
|
&mut bob_node,
|
|
|
|
bob_state0.into(),
|
|
|
|
harness::bob::is_state2,
|
|
|
|
&mut OsRng,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let alice: alice::State3 = alice_state.try_into().unwrap();
|
|
|
|
let bob: bob::State2 = bob_state.try_into().unwrap();
|
|
|
|
let tx_lock_txid = bob.tx_lock.txid();
|
|
|
|
|
|
|
|
let alice_bitcoin_wallet = Arc::new(alice_node.bitcoin_wallet);
|
|
|
|
let bob_bitcoin_wallet = Arc::new(bob_node.bitcoin_wallet);
|
|
|
|
let alice_monero_wallet = Arc::new(alice_node.monero_wallet);
|
|
|
|
let bob_monero_wallet = Arc::new(bob_node.monero_wallet);
|
|
|
|
|
|
|
|
let (alice_network, bob_sender) = Network::<EncryptedSignature>::new();
|
|
|
|
let (bob_network, alice_sender) = Network::<TransferProof>::new();
|
|
|
|
|
|
|
|
let alice_swap = swap_as_alice(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(alice_network)),
|
2020-10-26 01:05:30 -04:00
|
|
|
alice_sender,
|
|
|
|
alice_monero_wallet.clone(),
|
|
|
|
alice_bitcoin_wallet.clone(),
|
|
|
|
AliceBehaviour::default(),
|
|
|
|
alice,
|
|
|
|
);
|
|
|
|
let bob_swap = swap_as_bob(
|
2020-10-27 02:18:19 -04:00
|
|
|
Arc::new(Mutex::new(bob_network)),
|
2020-10-26 01:05:30 -04:00
|
|
|
bob_sender,
|
|
|
|
bob_monero_wallet.clone(),
|
|
|
|
bob_bitcoin_wallet.clone(),
|
|
|
|
BobBehaviour {
|
|
|
|
send_btc_redeem_encsig: false,
|
|
|
|
create_monero_wallet_for_output: false,
|
|
|
|
cancel_btc: false,
|
|
|
|
refund_btc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
bob,
|
|
|
|
);
|
|
|
|
|
|
|
|
pin_mut!(alice_swap);
|
|
|
|
pin_mut!(bob_swap);
|
|
|
|
|
|
|
|
// since we model Bob as inactive after locking bitcoin, his future does not
|
|
|
|
// resolve, so we wait for one of the two (Alice's) to resolve via select
|
|
|
|
select(alice_swap, bob_swap).await;
|
|
|
|
|
|
|
|
let alice_final_btc_balance = alice_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
let bob_final_btc_balance = bob_bitcoin_wallet.balance().await.unwrap();
|
|
|
|
|
|
|
|
let lock_tx_bitcoin_fee = bob_bitcoin_wallet
|
|
|
|
.transaction_fee(tx_lock_txid)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
|
|
|
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
alice_final_btc_balance,
|
|
|
|
initial_balances.alice_btc + swap_amounts.btc
|
|
|
|
- bitcoin::Amount::from_sat(2 * xmr_btc::bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
bob_final_btc_balance,
|
|
|
|
initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee
|
|
|
|
);
|
|
|
|
|
|
|
|
// Getting the Monero LockTx fee is tricky in a clean way, I think checking this
|
|
|
|
// condition is sufficient
|
|
|
|
assert!(alice_final_xmr_balance <= initial_balances.alice_xmr - swap_amounts.xmr,);
|
|
|
|
assert_eq!(bob_final_xmr_balance, initial_balances.bob_xmr);
|
|
|
|
}
|