From 9384b0cf3c4c66017d0cd0f7e1e465dd28359b36 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 28 Oct 2020 11:46:45 +1100 Subject: [PATCH] [WIP] Swap app e2e test --- swap/Cargo.toml | 1 + swap/src/alice.rs | 1 + swap/src/alice/amounts.rs | 19 ++--------- swap/src/alice/message0.rs | 3 +- swap/src/bob.rs | 3 +- swap/tests/e2e.rs | 70 ++++++++++++++++++++++++++++++++++++++ xmr-btc/src/alice.rs | 3 +- xmr-btc/tests/on_chain.rs | 6 ---- 8 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 swap/tests/e2e.rs diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 3a2f295c..6b57107e 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -47,6 +47,7 @@ hyper = "0.13" port_check = "0.1" spectral = "0.6" tempfile = "3" +testcontainers = "0.10" [features] default = [] diff --git a/swap/src/alice.rs b/swap/src/alice.rs index b394436c..d9a48a6e 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -356,6 +356,7 @@ impl Alice { /// Message0 gets sent within the network layer using this state0. pub fn set_state0(&mut self, state: State0) { + info!("Set state 0"); let _ = self.message0.set_state(state); } diff --git a/swap/src/alice/amounts.rs b/swap/src/alice/amounts.rs index 39396861..ffa87cf7 100644 --- a/swap/src/alice/amounts.rs +++ b/swap/src/alice/amounts.rs @@ -1,11 +1,10 @@ -use anyhow::Result; use libp2p::{ request_response::{ - handler::RequestProtocol, ProtocolSupport, RequestId, RequestResponse, - RequestResponseConfig, RequestResponseEvent, RequestResponseMessage, ResponseChannel, + handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, + RequestResponseEvent, RequestResponseMessage, ResponseChannel, }, swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters}, - NetworkBehaviour, PeerId, + NetworkBehaviour, }; use std::{ collections::VecDeque, @@ -40,18 +39,6 @@ impl Amounts { self.rr.send_response(channel, msg); } - pub async fn request_amounts( - &mut self, - alice: PeerId, - btc: ::bitcoin::Amount, - ) -> Result { - let msg = BobToAlice::AmountsFromBtc(btc); - let id = self.rr.send_request(&alice, msg); - debug!("Request sent to: {}", alice); - - Ok(id) - } - fn poll( &mut self, _: &mut Context<'_>, diff --git a/swap/src/alice/message0.rs b/swap/src/alice/message0.rs index 460c63e4..1ec54365 100644 --- a/swap/src/alice/message0.rs +++ b/swap/src/alice/message0.rs @@ -13,7 +13,7 @@ use std::{ task::{Context, Poll}, time::Duration, }; -use tracing::{debug, error}; +use tracing::{debug, error, info}; use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use xmr_btc::{alice::State0, bob}; @@ -87,6 +87,7 @@ impl NetworkBehaviourEventProcess> .. } => match request { BobToAlice::Message0(msg) => { + info!("Got Alice's first message"); let response = match &self.state { None => panic!("No state, did you forget to set it?"), Some(state) => { diff --git a/swap/src/bob.rs b/swap/src/bob.rs index 2da7350c..4c01bbd4 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -306,7 +306,8 @@ impl Bob { /// Sends Bob's first message to Alice. pub fn send_message0(&mut self, alice: PeerId, msg: bob::Message0) { - self.message0.send(alice, msg) + self.message0.send(alice, msg); + info!("Sent first message to Alice"); } /// Sends Bob's second message to Alice. diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs new file mode 100644 index 00000000..8ed81970 --- /dev/null +++ b/swap/tests/e2e.rs @@ -0,0 +1,70 @@ +use bitcoin_harness::Bitcoind; +use futures::{channel::mpsc, future::try_join}; +use libp2p::Multiaddr; +use monero_harness::Monero; +use std::sync::Arc; +use swap::{alice, bob}; +use testcontainers::clients::Cli; +use tracing_subscriber::util::SubscriberInitExt; + +#[tokio::test] +async fn swap() { + let _guard = tracing_subscriber::fmt() + .with_env_filter("info") + .with_ansi(false) + .set_default(); + + let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876" + .parse() + .expect("failed to parse Alice's address"); + + let cli = Cli::default(); + let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); + let _ = bitcoind.init(5).await; + + let btc = bitcoin::Amount::ONE_BTC; + let _btc_alice = bitcoin::Amount::ZERO; + let btc_bob = btc * 10; + + let xmr = 1_000_000_000_000; + let xmr_alice = xmr * 10; + let xmr_bob = 0; + + let alice_btc_wallet = Arc::new( + swap::bitcoin::Wallet::new("alice", &bitcoind.node_url) + .await + .unwrap(), + ); + let bob_btc_wallet = Arc::new( + swap::bitcoin::Wallet::new("bob", &bitcoind.node_url) + .await + .unwrap(), + ); + bitcoind + .mint(bob_btc_wallet.0.new_address().await.unwrap(), btc_bob) + .await + .unwrap(); + + let (monero, _container) = Monero::new(&cli).unwrap(); + monero.init(xmr_alice, xmr_bob).await.unwrap(); + + let alice_xmr_wallet = Arc::new(swap::monero::Wallet(monero.alice_wallet_rpc_client())); + let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.bob_wallet_rpc_client())); + + let alice_swap = alice::swap(alice_btc_wallet, alice_xmr_wallet, alice_multiaddr.clone()); + + let (cmd_tx, mut _cmd_rx) = mpsc::channel(1); + let (mut rsp_tx, rsp_rx) = mpsc::channel(1); + let bob_swap = bob::swap( + bob_btc_wallet, + bob_xmr_wallet, + btc.as_sat(), + alice_multiaddr, + cmd_tx, + rsp_rx, + ); + + rsp_tx.try_send(swap::Rsp::VerifiedAmounts).unwrap(); + + try_join(alice_swap, bob_swap).await.unwrap(); +} diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index 2f06824b..43d79633 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -25,7 +25,7 @@ use std::{ time::Duration, }; use tokio::{sync::Mutex, time::timeout}; -use tracing::error; +use tracing::{error, info}; pub mod message; pub use message::{Message, Message0, Message1, Message2}; @@ -524,6 +524,7 @@ impl State0 { } pub fn next_message(&self, rng: &mut R) -> Message0 { + info!("Producing first message"); let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a); Message0 { diff --git a/xmr-btc/tests/on_chain.rs b/xmr-btc/tests/on_chain.rs index 89eaf1f1..e61db076 100644 --- a/xmr-btc/tests/on_chain.rs +++ b/xmr-btc/tests/on_chain.rs @@ -18,7 +18,6 @@ use std::{convert::TryInto, sync::Arc}; use testcontainers::clients::Cli; use tokio::sync::Mutex; use tracing::info; -use tracing_subscriber::util::SubscriberInitExt; use xmr_btc::{ alice::{self, ReceiveBitcoinRedeemEncsig}, bitcoin::{BroadcastSignedTransaction, EncryptedSignature, SignTxLock}, @@ -419,11 +418,6 @@ async fn on_chain_both_refund_if_alice_never_redeems() { #[tokio::test] async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() { - let _guard = tracing_subscriber::fmt() - .with_env_filter("info") - .with_ansi(false) - .set_default(); - let cli = Cli::default(); let (monero, _container) = Monero::new(&cli).unwrap(); let bitcoind = init_bitcoind(&cli).await;