Fix happy path test

Poll bob swarm to send encsig after calling send message3. Comment
out unimplmented wait for transaction finality trait. Dial alice.
This commit is contained in:
rishflab 2020-12-01 14:30:02 +11:00
parent fa243e2fd7
commit 47c3ddc6f7
4 changed files with 31 additions and 14 deletions

View File

@ -142,10 +142,10 @@ where
.await .await
.context("Failed to find lock Bitcoin tx")?; .context("Failed to find lock Bitcoin tx")?;
// We saw the transaction in the mempool, waiting for it to be confirmed. // // We saw the transaction in the mempool, waiting for it to be confirmed.
bitcoin_wallet // bitcoin_wallet
.wait_for_transaction_finality(lock_bitcoin_txid) // .wait_for_transaction_finality(lock_bitcoin_txid)
.await; // .await;
Ok(()) Ok(())
} }
@ -235,8 +235,8 @@ where
.broadcast_signed_transaction(redeem_tx) .broadcast_signed_transaction(redeem_tx)
.await?; .await?;
// TODO(Franck): Not sure if we wait for finality here or just mined // // TODO(Franck): Not sure if we wait for finality here or just mined
bitcoin_wallet.wait_for_transaction_finality(tx_id).await; // bitcoin_wallet.wait_for_transaction_finality(tx_id).await;
Ok(()) Ok(())
} }
@ -370,7 +370,8 @@ where
.broadcast_signed_transaction(punish_tx) .broadcast_signed_transaction(punish_tx)
.await?; .await?;
bitcoin_wallet.wait_for_transaction_finality(txid).await; // todo: enable this once trait is implemented
// bitcoin_wallet.wait_for_transaction_finality(txid).await;
Ok(txid) Ok(txid)
} }

View File

@ -3,24 +3,25 @@ use crate::{
Cmd, Rsp, SwapAmounts, Cmd, Rsp, SwapAmounts,
}; };
use anyhow::Result; use anyhow::Result;
use libp2p::core::Multiaddr;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::sync::Arc; use std::sync::Arc;
use tokio::{stream::StreamExt, sync::mpsc}; use tokio::{stream::StreamExt, sync::mpsc};
use xmr_btc::bob::State2; use xmr_btc::bob::State2;
pub async fn negotiate<R>( pub async fn negotiate<R>(
state0: xmr_btc::bob::State0, state0: xmr_btc::bob::State0,
amounts: SwapAmounts, amounts: SwapAmounts,
swarm: &mut Swarm, swarm: &mut Swarm,
addr: Multiaddr,
mut rng: R, mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
) -> Result<(SwapAmounts, State2)> ) -> Result<(SwapAmounts, State2)>
where where
R: RngCore + CryptoRng + Send, R: RngCore + CryptoRng + Send,
{ {
// todo: dial the swarm outside libp2p::Swarm::dial_addr(swarm, addr)?;
// libp2p::Swarm::dial_addr(&mut swarm, addr)?;
let alice = match swarm.next().await { let alice = match swarm.next().await {
OutEvent::ConnectionEstablished(alice) => alice, OutEvent::ConnectionEstablished(alice) => alice,
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),

View File

@ -5,9 +5,10 @@ use crate::{
}; };
use anyhow::Result; use anyhow::Result;
use async_recursion::async_recursion; use async_recursion::async_recursion;
use libp2p::PeerId; use libp2p::{core::Multiaddr, PeerId};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::sync::Arc; use std::sync::Arc;
use tracing::debug;
use uuid::Uuid; use uuid::Uuid;
use xmr_btc::bob::{self}; use xmr_btc::bob::{self};
@ -18,6 +19,7 @@ pub enum BobState {
state0: bob::State0, state0: bob::State0,
amounts: SwapAmounts, amounts: SwapAmounts,
peer_id: PeerId, peer_id: PeerId,
addr: Multiaddr,
}, },
Negotiated(bob::State2, PeerId), Negotiated(bob::State2, PeerId),
BtcLocked(bob::State3, PeerId), BtcLocked(bob::State3, PeerId),
@ -50,11 +52,13 @@ where
state0, state0,
amounts, amounts,
peer_id, peer_id,
addr,
} => { } => {
let (swap_amounts, state2) = negotiate( let (swap_amounts, state2) = negotiate(
state0, state0,
amounts, amounts,
&mut swarm, &mut swarm,
addr,
&mut rng, &mut rng,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
) )
@ -118,6 +122,17 @@ where
// should happen in this arm? // should happen in this arm?
swarm.send_message3(alice_peer_id.clone(), tx_redeem_encsig); swarm.send_message3(alice_peer_id.clone(), tx_redeem_encsig);
// Sadly we have to poll the swarm to get make sure the message is sent?
// FIXME: Having to wait for Alice's response here is a big problem, because
// we're stuck if she doesn't send her response back. I believe this is
// currently necessary, so we may have to rework this and/or how we use libp2p
match swarm.next().await {
OutEvent::Message3 => {
debug!("Got Message3 empty response");
}
other => panic!("unexpected event: {:?}", other),
};
swap( swap(
BobState::EncSigSent(state, alice_peer_id), BobState::EncSigSent(state, alice_peer_id),
swarm, swarm,

View File

@ -179,8 +179,6 @@ async fn simple_swap_happy_path() {
)); ));
let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet("bob").unwrap().client())); let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet("bob").unwrap().client()));
// let redeem_address = bitcoin_wallet.as_ref().new_address().await?;
// let punish_address = redeem_address.clone();
let amounts = SwapAmounts { let amounts = SwapAmounts {
btc, btc,
xmr: xmr_btc::monero::Amount::from_piconero(xmr), xmr: xmr_btc::monero::Amount::from_piconero(xmr),
@ -201,7 +199,8 @@ async fn simple_swap_happy_path() {
v_a, v_a,
} }
}; };
let alice_swarm = alice::new_swarm(alice_multiaddr, alice_transport, alice_behaviour).unwrap(); let alice_swarm =
alice::new_swarm(alice_multiaddr.clone(), alice_transport, alice_behaviour).unwrap();
let alice_swap = alice::swap::swap( let alice_swap = alice::swap::swap(
alice_state, alice_state,
alice_swarm, alice_swarm,
@ -227,6 +226,7 @@ async fn simple_swap_happy_path() {
state0, state0,
amounts, amounts,
peer_id: alice_peer_id, peer_id: alice_peer_id,
addr: alice_multiaddr,
}; };
let bob_swarm = bob::new_swarm(bob_transport, bob_behaviour).unwrap(); let bob_swarm = bob::new_swarm(bob_transport, bob_behaviour).unwrap();
let bob_swap = bob::swap::swap( let bob_swap = bob::swap::swap(