[WIP] Swap app e2e test

This commit is contained in:
Lucas Soriano del Pino 2020-10-28 11:46:45 +11:00
parent 9e30bd5151
commit 9384b0cf3c
8 changed files with 81 additions and 25 deletions

View File

@ -47,6 +47,7 @@ hyper = "0.13"
port_check = "0.1"
spectral = "0.6"
tempfile = "3"
testcontainers = "0.10"
[features]
default = []

View File

@ -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);
}

View File

@ -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<RequestId> {
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<'_>,

View File

@ -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<RequestResponseEvent<BobToAlice, AliceToBob>>
..
} => 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) => {

View File

@ -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.

70
swap/tests/e2e.rs Normal file
View File

@ -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();
}

View File

@ -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<R: RngCore + CryptoRng>(&self, rng: &mut R) -> Message0 {
info!("Producing first message");
let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a);
Message0 {

View File

@ -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;