wip: fixed message0

This commit is contained in:
Lucas Soriano del Pino 2020-10-29 14:00:00 +11:00
parent 119f2a7c54
commit ae87c10cae
14 changed files with 130 additions and 78 deletions

View File

@ -98,6 +98,7 @@ pub async fn swap(
let mut swarm = new_swarm(listen, local_port)?;
let message0: bob::Message0;
let mut state0: Option<alice::State0> = None;
let mut last_amounts: Option<SwapAmounts> = None;
loop {
@ -112,6 +113,29 @@ pub async fn swap(
// verification of message 0.
last_amounts = Some(amounts);
swarm.send_amounts(channel, amounts);
let (xmr, btc) = match last_amounts {
Some(p) => (p.xmr, p.btc),
None => unreachable!("should have amounts by here"),
};
let redeem_address = bitcoin_wallet.as_ref().new_address().await?;
let punish_address = redeem_address.clone();
// TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng;
let state = State0::new(
rng,
btc,
xmr,
REFUND_TIMELOCK,
PUNISH_TIMELOCK,
redeem_address,
punish_address,
);
swarm.set_state0(state.clone());
state0 = Some(state)
}
OutEvent::Message0(msg) => {
debug!("got message 0 from Bob");
@ -127,28 +151,10 @@ pub async fn swap(
};
}
let (xmr, btc) = match last_amounts {
Some(p) => (p.xmr, p.btc),
None => unreachable!("should have amounts by here"),
};
let redeem_address = bitcoin_wallet.as_ref().new_address().await?;
let punish_address = redeem_address.clone();
// TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng;
let state0 = State0::new(
rng,
btc,
xmr,
REFUND_TIMELOCK,
PUNISH_TIMELOCK,
redeem_address,
punish_address,
);
swarm.set_state0(state0.clone());
let state1 = state0.receive(message0).expect("failed to receive msg 0");
let state1 = state0
.expect("to be set")
.receive(message0)
.expect("failed to receive msg 0");
let (state2, channel) = match swarm.next().await {
OutEvent::Message1 { msg, channel } => {

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, AmountsProtocol, BobToAlice, Codec, TIMEOUT};
#[derive(Debug)]
pub enum OutEvent {
@ -28,7 +28,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Amounts {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<AmountsProtocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -43,7 +43,7 @@ impl Amounts {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<AmountsProtocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -62,7 +62,7 @@ impl Default for Amounts {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(AmountsProtocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -15,7 +15,7 @@ use std::{
};
use tracing::{error, info};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT};
use xmr_btc::{alice::State0, bob};
#[derive(Debug)]
@ -28,7 +28,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message0 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message0Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
#[behaviour(ignore)]
@ -49,7 +49,7 @@ impl Message0 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message0Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -67,7 +67,7 @@ impl Default for Message0 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message0Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT};
use xmr_btc::bob;
#[derive(Debug)]
@ -31,7 +31,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message1 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message1Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -46,7 +46,7 @@ impl Message1 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message1Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -64,7 +64,7 @@ impl Default for Message1 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message1Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT};
use xmr_btc::bob;
#[derive(Debug)]
@ -31,7 +31,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message2 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message2Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -46,7 +46,7 @@ impl Message2 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message2Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -64,7 +64,7 @@ impl Default for Message2 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message2Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT};
use xmr_btc::bob;
#[derive(Debug)]
@ -26,7 +26,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message3 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message3Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -36,7 +36,7 @@ impl Message3 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message3Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -54,7 +54,7 @@ impl Default for Message3 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message3Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -90,7 +90,7 @@ pub async fn swap(
swarm.request_amounts(alice.clone(), btc);
let (btc, xmr) = match swarm.next().await {
let (btc_amount, xmr) = match swarm.next().await {
OutEvent::Amounts(amounts) => {
debug!("Got amounts from Alice: {:?}", amounts);
let cmd = Cmd::VerifyAmounts(amounts);
@ -107,13 +107,22 @@ pub async fn swap(
other => panic!("unexpected event: {:?}", other),
};
swarm.request_amounts(alice.clone(), btc);
match swarm.next().await {
OutEvent::Amounts(amounts) => {
debug!("Got amounts from Alice: {:?}", amounts);
}
other => panic!("unexpected event: {:?}", other),
};
let refund_address = bitcoin_wallet.new_address().await?;
// TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng;
let state0 = State0::new(
rng,
btc,
btc_amount,
xmr,
REFUND_TIMELOCK,
PUNISH_TIMELOCK,

View File

@ -15,7 +15,7 @@ use std::{
use tracing::error;
use crate::{
network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT},
network::request_response::{AliceToBob, AmountsProtocol, BobToAlice, Codec, TIMEOUT},
SwapAmounts,
};
@ -29,7 +29,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Amounts {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<AmountsProtocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -46,7 +46,7 @@ impl Amounts {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<AmountsProtocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -65,7 +65,7 @@ impl Default for Amounts {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(AmountsProtocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT};
use xmr_btc::{alice, bob};
#[derive(Debug)]
@ -26,7 +26,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message0 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message0Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -41,7 +41,7 @@ impl Message0 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message0Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -59,7 +59,7 @@ impl Default for Message0 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message0Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT};
use xmr_btc::{alice, bob};
#[derive(Debug)]
@ -26,7 +26,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message1 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message1Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -41,7 +41,7 @@ impl Message1 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message1Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -59,7 +59,7 @@ impl Default for Message1 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message1Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),

View File

@ -13,7 +13,7 @@ use std::{
};
use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT};
use xmr_btc::{alice, bob};
#[derive(Debug)]
@ -26,7 +26,7 @@ pub enum OutEvent {
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message2 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message2Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
@ -41,7 +41,7 @@ impl Message2 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message2Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
@ -59,7 +59,7 @@ impl Default for Message2 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message2Protocol, ProtocolSupport::Full)],
config,
),
events: VecDeque::default(),

View File

@ -13,7 +13,7 @@ use std::{
use tracing::{debug, error};
use crate::{
network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT},
network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT},
Never,
};
use xmr_btc::bob;
@ -23,7 +23,7 @@ use xmr_btc::bob;
#[behaviour(out_event = "Never", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Message3 {
rr: RequestResponse<Codec>,
rr: RequestResponse<Codec<Message3Protocol>>,
}
impl Message3 {
@ -38,7 +38,7 @@ impl Message3 {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, Never>> {
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message3Protocol>>, Never>> {
Poll::Pending
}
}
@ -52,7 +52,7 @@ impl Default for Message3 {
Self {
rr: RequestResponse::new(
Codec::default(),
vec![(Protocol, ProtocolSupport::Full)],
vec![(Message3Protocol, ProtocolSupport::Full)],
config,
),
}

View File

@ -5,8 +5,7 @@ use libp2p::{
request_response::{ProtocolName, RequestResponseCodec},
};
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, io};
use tracing::debug;
use std::{fmt::Debug, io, marker::PhantomData};
use crate::SwapAmounts;
use xmr_btc::{alice, bob, monero};
@ -45,20 +44,61 @@ pub enum AliceToBob {
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Protocol;
pub struct AmountsProtocol;
impl ProtocolName for Protocol {
#[derive(Debug, Clone, Copy, Default)]
pub struct Message0Protocol;
#[derive(Debug, Clone, Copy, Default)]
pub struct Message1Protocol;
#[derive(Debug, Clone, Copy, Default)]
pub struct Message2Protocol;
#[derive(Debug, Clone, Copy, Default)]
pub struct Message3Protocol;
impl ProtocolName for AmountsProtocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/1.0.0"
b"/xmr/btc/amounts/1.0.0"
}
}
impl ProtocolName for Message0Protocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message0/1.0.0"
}
}
impl ProtocolName for Message1Protocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message1/1.0.0"
}
}
impl ProtocolName for Message2Protocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message2/1.0.0"
}
}
impl ProtocolName for Message3Protocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message3/1.0.0"
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Codec;
pub struct Codec<P> {
phantom: PhantomData<P>,
}
#[async_trait]
impl RequestResponseCodec for Codec {
type Protocol = Protocol;
impl<P> RequestResponseCodec for Codec<P>
where
P: Send + Sync + Clone + ProtocolName,
{
type Protocol = P;
type Request = BobToAlice;
type Response = AliceToBob;
@ -109,11 +149,8 @@ impl RequestResponseCodec for Codec {
where
T: AsyncWrite + Unpin + Send,
{
debug!("enter write_request");
let bytes = serde_cbor::to_vec(&req).map_err(|e| {
tracing::debug!("serde write_request error: {:?}", e);
io::Error::new(io::ErrorKind::InvalidData, e)
})?;
let bytes =
serde_cbor::to_vec(&req).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
upgrade::write_one(io, &bytes).await?;

View File

@ -13,7 +13,7 @@ async fn swap() {
.with_env_filter(
"swap=debug,hyper=off,reqwest=off,monero-harness=info,testcontainers=info,libp2p=debug",
)
.with_ansi(true)
.with_ansi(false)
.set_default();
let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876"