Merge pull request #28 from comit-network/re-work-swap-functions

Re work swap functions
This commit is contained in:
Lucas Soriano 2020-10-27 13:27:52 +11:00 committed by GitHub
commit cf8accf30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 45 deletions

View File

@ -28,6 +28,9 @@ use xmr_btc::{alice::State0, bob, monero};
pub type Swarm = libp2p::Swarm<Alice>; pub type Swarm = libp2p::Swarm<Alice>;
// TODO: After we have done some testing replace all the 'panic's with log
// statements or error returns.
// FIXME: This whole function is horrible, needs total re-write. // FIXME: This whole function is horrible, needs total re-write.
pub async fn swap( pub async fn swap(
listen: Multiaddr, listen: Multiaddr,
@ -46,15 +49,20 @@ pub async fn swap(
} }
OutEvent::Request(amounts::OutEvent::Btc { btc, channel }) => { OutEvent::Request(amounts::OutEvent::Btc { btc, channel }) => {
debug!("Got request from Bob to swap {}", btc); debug!("Got request from Bob to swap {}", btc);
let p = calculate_amounts(btc); let amounts = calculate_amounts(btc);
last_amounts = Some(p); // TODO: We cache the last amounts returned, this needs improving along with
swarm.send_amounts(channel, p); // verification of message 0.
last_amounts = Some(amounts);
swarm.send_amounts(channel, amounts);
} }
OutEvent::Message0(msg) => { OutEvent::Message0(msg) => {
debug!("Got message0 from Bob"); // We don't want Bob to be able to crash us by sending an out of
// TODO: Do this in a more Rusty/functional way. // order message. Keep looping if Bob has not requested amounts.
message0 = msg; if last_amounts.is_some() {
break; // TODO: We should verify the amounts and notify Bob if they have changed.
message0 = msg;
break;
}
} }
other => panic!("Unexpected event: {:?}", other), other => panic!("Unexpected event: {:?}", other),
}; };
@ -65,10 +73,6 @@ pub async fn swap(
None => unreachable!("should have amounts by here"), None => unreachable!("should have amounts by here"),
}; };
let xmr = monero::Amount::from_piconero(xmr.as_piconero());
// TODO: This should be the Amount exported by xmr_btc.
let btc = ::bitcoin::Amount::from_sat(btc.as_sat());
// TODO: Pass this in using <R: RngCore + CryptoRng> // TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng; let rng = &mut OsRng;
let state0 = State0::new( let state0 = State0::new(
@ -82,6 +86,7 @@ pub async fn swap(
); );
swarm.set_state0(state0.clone()); swarm.set_state0(state0.clone());
// TODO: Can we verify message 0 before calling this so we never fail?
let state1 = state0.receive(message0).expect("failed to receive msg 0"); let state1 = state0.receive(message0).expect("failed to receive msg 0");
let (state2, channel) = match swarm.next().await { let (state2, channel) = match swarm.next().await {

View File

@ -71,11 +71,17 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {
RequestResponseEvent::Message { RequestResponseEvent::Message {
message: RequestResponseMessage::Request { request, .. }, message:
RequestResponseMessage::Request {
request, channel, ..
},
.. ..
} => match request { } => match request {
BobToAlice::Message2(msg) => { BobToAlice::Message2(msg) => {
self.events.push_back(OutEvent::Msg(msg)); self.events.push_back(OutEvent::Msg(msg));
// Send back empty response so that the request/response protocol completes.
let msg = AliceToBob::EmptyResponse;
self.rr.send_response(channel, msg);
} }
other => debug!("got request: {:?}", other), other => debug!("got request: {:?}", other),
}, },

View File

@ -21,7 +21,7 @@ use crate::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
transport, TokioExecutor, transport, TokioExecutor,
}, },
Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, Cmd, Never, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
}; };
use xmr_btc::{ use xmr_btc::{
alice, alice,
@ -69,10 +69,6 @@ where
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
}; };
// FIXME: Too many `bitcoin` crates/modules.
let xmr = xmr_btc::monero::Amount::from_piconero(xmr.as_piconero());
let btc = ::bitcoin::Amount::from_sat(btc.as_sat());
// TODO: Pass this in using <R: RngCore + CryptoRng> // TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng; let rng = &mut OsRng;
let state0 = State0::new( let state0 = State0::new(
@ -87,8 +83,9 @@ where
swarm.send_message0(alice.clone(), state0.next_message(rng)); swarm.send_message0(alice.clone(), state0.next_message(rng));
let state1 = match swarm.next().await { let state1 = match swarm.next().await {
OutEvent::Message0(msg) => { OutEvent::Message0(msg) => {
state0.receive(&wallet, msg).await? // TODO: More graceful error // TODO: Verify the response message before calling receive() and handle any
// handling. // error gracefully.
state0.receive(&wallet, msg).await?
} }
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
}; };
@ -96,7 +93,7 @@ where
swarm.send_message1(alice.clone(), state1.next_message()); swarm.send_message1(alice.clone(), state1.next_message());
let state2 = match swarm.next().await { let state2 = match swarm.next().await {
OutEvent::Message1(msg) => { OutEvent::Message1(msg) => {
state1.receive(msg)? // TODO: More graceful error handling. state1.receive(msg)? // TODO: Same as above.
} }
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
}; };
@ -137,7 +134,6 @@ pub enum OutEvent {
Amounts(SwapAmounts), Amounts(SwapAmounts),
Message0(alice::Message0), Message0(alice::Message0),
Message1(alice::Message1), Message1(alice::Message1),
Message2(alice::Message2),
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -174,11 +170,9 @@ impl From<message1::OutEvent> for OutEvent {
} }
} }
impl From<message2::OutEvent> for OutEvent { impl From<Never> for OutEvent {
fn from(event: message2::OutEvent) -> Self { fn from(_: Never) -> Self {
match event { panic!("this never happens")
message2::OutEvent::Msg(msg) => OutEvent::Message2(msg),
}
} }
} }

View File

@ -7,28 +7,23 @@ use libp2p::{
NetworkBehaviour, PeerId, NetworkBehaviour, PeerId,
}; };
use std::{ use std::{
collections::VecDeque,
task::{Context, Poll}, task::{Context, Poll},
time::Duration, time::Duration,
}; };
use tracing::{debug, error}; use tracing::{debug, error};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use crate::{
use xmr_btc::{alice, bob}; network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT},
Never,
#[derive(Debug)] };
pub enum OutEvent { use xmr_btc::bob;
Msg(alice::Message2),
}
/// A `NetworkBehaviour` that represents sending message 2 to Alice. /// A `NetworkBehaviour` that represents sending message 2 to Alice.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", poll_method = "poll")] #[behaviour(out_event = "Never", poll_method = "poll")]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Message2 { pub struct Message2 {
rr: RequestResponse<Codec>, rr: RequestResponse<Codec>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
} }
impl Message2 { impl Message2 {
@ -37,15 +32,13 @@ impl Message2 {
let _id = self.rr.send_request(&alice, msg); let _id = self.rr.send_request(&alice, msg);
} }
// TODO: Do we need a custom implementation if we are not bubbling any out
// events?
fn poll( fn poll(
&mut self, &mut self,
_: &mut Context<'_>, _: &mut Context<'_>,
_: &mut impl PollParameters, _: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> { ) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, Never>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
Poll::Pending Poll::Pending
} }
} }
@ -62,7 +55,6 @@ impl Default for Message2 {
vec![(Protocol, ProtocolSupport::Full)], vec![(Protocol, ProtocolSupport::Full)],
config, config,
), ),
events: Default::default(),
} }
} }
} }
@ -78,8 +70,8 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
message: RequestResponseMessage::Response { response, .. }, message: RequestResponseMessage::Response { response, .. },
.. ..
} => match response { } => match response {
AliceToBob::Message2(msg) => self.events.push_back(OutEvent::Msg(msg)), AliceToBob::EmptyResponse => debug!("Alice correctly responded to message 2"),
other => debug!("got response: {:?}", other), other => debug!("unexpected response: {:?}", other),
}, },
RequestResponseEvent::InboundFailure { error, .. } => { RequestResponseEvent::InboundFailure { error, .. } => {
error!("Inbound failure: {:?}", error); error!("Inbound failure: {:?}", error);

View File

@ -35,6 +35,7 @@ pub enum AliceToBob {
Amounts(SwapAmounts), Amounts(SwapAmounts),
Message0(alice::Message0), Message0(alice::Message0),
Message1(alice::Message1), Message1(alice::Message1),
EmptyResponse, // This is sent back as response to Message2 from Bob.
Message2(alice::Message2), Message2(alice::Message2),
} }