mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Move message0 response handler out of NB
This commit is contained in:
parent
4143394767
commit
143d8bc07d
@ -17,15 +17,15 @@ use libp2p::{
|
|||||||
NetworkBehaviour, PeerId,
|
NetworkBehaviour, PeerId,
|
||||||
};
|
};
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
use xmr_btc::{alice::State0, bob};
|
use xmr_btc::bob;
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution;
|
|
||||||
mod message0;
|
mod message0;
|
||||||
mod message1;
|
mod message1;
|
||||||
mod message2;
|
mod message2;
|
||||||
mod message3;
|
mod message3;
|
||||||
|
mod negotiate;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
|
|
||||||
pub type Swarm = libp2p::Swarm<Behaviour>;
|
pub type Swarm = libp2p::Swarm<Behaviour>;
|
||||||
@ -60,7 +60,10 @@ pub enum OutEvent {
|
|||||||
// TODO (Franck): Change this to get both amounts so parties can verify the amounts are
|
// TODO (Franck): Change this to get both amounts so parties can verify the amounts are
|
||||||
// expected early on.
|
// expected early on.
|
||||||
Request(amounts::OutEvent), // Not-uniform with Bob on purpose, ready for adding Xmr event.
|
Request(amounts::OutEvent), // Not-uniform with Bob on purpose, ready for adding Xmr event.
|
||||||
Message0(bob::Message0),
|
Message0 {
|
||||||
|
msg: bob::Message0,
|
||||||
|
channel: ResponseChannel<AliceToBob>,
|
||||||
|
},
|
||||||
Message1 {
|
Message1 {
|
||||||
msg: bob::Message1,
|
msg: bob::Message1,
|
||||||
channel: ResponseChannel<AliceToBob>,
|
channel: ResponseChannel<AliceToBob>,
|
||||||
@ -91,7 +94,7 @@ impl From<amounts::OutEvent> for OutEvent {
|
|||||||
impl From<message0::OutEvent> for OutEvent {
|
impl From<message0::OutEvent> for OutEvent {
|
||||||
fn from(event: message0::OutEvent) -> Self {
|
fn from(event: message0::OutEvent) -> Self {
|
||||||
match event {
|
match event {
|
||||||
message0::OutEvent::Msg(msg) => OutEvent::Message0(msg),
|
message0::OutEvent::Msg { channel, msg } => OutEvent::Message0 { msg, channel },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,20 +139,6 @@ pub struct Behaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
pub fn new(state: State0) -> Self {
|
|
||||||
let identity = Keypair::generate_ed25519();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
pt: PeerTracker::default(),
|
|
||||||
amounts: Amounts::default(),
|
|
||||||
message0: Message0::new(state),
|
|
||||||
message1: Message1::default(),
|
|
||||||
message2: Message2::default(),
|
|
||||||
message3: Message3::default(),
|
|
||||||
identity,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn identity(&self) -> Keypair {
|
pub fn identity(&self) -> Keypair {
|
||||||
self.identity.clone()
|
self.identity.clone()
|
||||||
}
|
}
|
||||||
@ -165,6 +154,16 @@ impl Behaviour {
|
|||||||
info!("Sent amounts response");
|
info!("Sent amounts response");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send Message0 to Bob in response to receiving his Message0.
|
||||||
|
pub fn send_message0(
|
||||||
|
&mut self,
|
||||||
|
channel: ResponseChannel<AliceToBob>,
|
||||||
|
msg: xmr_btc::alice::Message0,
|
||||||
|
) {
|
||||||
|
self.message0.send(channel, msg);
|
||||||
|
debug!("Sent Message0");
|
||||||
|
}
|
||||||
|
|
||||||
/// Send Message1 to Bob in response to receiving his Message1.
|
/// Send Message1 to Bob in response to receiving his Message1.
|
||||||
pub fn send_message1(
|
pub fn send_message1(
|
||||||
&mut self,
|
&mut self,
|
||||||
@ -185,3 +184,19 @@ impl Behaviour {
|
|||||||
debug!("Sent Message2");
|
debug!("Sent Message2");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Behaviour {
|
||||||
|
fn default() -> Self {
|
||||||
|
let identity = Keypair::generate_ed25519();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
pt: PeerTracker::default(),
|
||||||
|
amounts: Amounts::default(),
|
||||||
|
message0: Message0::default(),
|
||||||
|
message1: Message1::default(),
|
||||||
|
message2: Message2::default(),
|
||||||
|
message3: Message3::default(),
|
||||||
|
identity,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -30,13 +30,14 @@ impl<T> Default for Channels<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventLoopHandle {
|
pub struct EventLoopHandle {
|
||||||
msg0: Receiver<bob::Message0>,
|
msg0: Receiver<(bob::Message0, ResponseChannel<AliceToBob>)>,
|
||||||
msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>,
|
msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>,
|
||||||
msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>,
|
msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>,
|
||||||
msg3: Receiver<bob::Message3>,
|
msg3: Receiver<bob::Message3>,
|
||||||
request: Receiver<crate::alice::amounts::OutEvent>,
|
request: Receiver<crate::alice::amounts::OutEvent>,
|
||||||
conn_established: Receiver<PeerId>,
|
conn_established: Receiver<PeerId>,
|
||||||
send_amounts: Sender<(ResponseChannel<AliceToBob>, SwapAmounts)>,
|
send_amounts: Sender<(ResponseChannel<AliceToBob>, SwapAmounts)>,
|
||||||
|
send_msg0: Sender<(ResponseChannel<AliceToBob>, alice::Message0)>,
|
||||||
send_msg1: Sender<(ResponseChannel<AliceToBob>, alice::Message1)>,
|
send_msg1: Sender<(ResponseChannel<AliceToBob>, alice::Message1)>,
|
||||||
send_msg2: Sender<(ResponseChannel<AliceToBob>, alice::Message2)>,
|
send_msg2: Sender<(ResponseChannel<AliceToBob>, alice::Message2)>,
|
||||||
}
|
}
|
||||||
@ -49,7 +50,7 @@ impl EventLoopHandle {
|
|||||||
.ok_or_else(|| anyhow!("Failed to receive connection established from Bob"))
|
.ok_or_else(|| anyhow!("Failed to receive connection established from Bob"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recv_message0(&mut self) -> Result<bob::Message0> {
|
pub async fn recv_message0(&mut self) -> Result<(bob::Message0, ResponseChannel<AliceToBob>)> {
|
||||||
self.msg0
|
self.msg0
|
||||||
.recv()
|
.recv()
|
||||||
.await
|
.await
|
||||||
@ -93,6 +94,15 @@ impl EventLoopHandle {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn send_message0(
|
||||||
|
&mut self,
|
||||||
|
channel: ResponseChannel<AliceToBob>,
|
||||||
|
msg: alice::Message0,
|
||||||
|
) -> Result<()> {
|
||||||
|
let _ = self.send_msg0.send((channel, msg)).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send_message1(
|
pub async fn send_message1(
|
||||||
&mut self,
|
&mut self,
|
||||||
channel: ResponseChannel<AliceToBob>,
|
channel: ResponseChannel<AliceToBob>,
|
||||||
@ -114,13 +124,14 @@ impl EventLoopHandle {
|
|||||||
|
|
||||||
pub struct EventLoop {
|
pub struct EventLoop {
|
||||||
swarm: libp2p::Swarm<Behaviour>,
|
swarm: libp2p::Swarm<Behaviour>,
|
||||||
msg0: Sender<bob::Message0>,
|
msg0: Sender<(bob::Message0, ResponseChannel<AliceToBob>)>,
|
||||||
msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>,
|
msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>,
|
||||||
msg2: Sender<(bob::Message2, ResponseChannel<AliceToBob>)>,
|
msg2: Sender<(bob::Message2, ResponseChannel<AliceToBob>)>,
|
||||||
msg3: Sender<bob::Message3>,
|
msg3: Sender<bob::Message3>,
|
||||||
request: Sender<crate::alice::amounts::OutEvent>,
|
request: Sender<crate::alice::amounts::OutEvent>,
|
||||||
conn_established: Sender<PeerId>,
|
conn_established: Sender<PeerId>,
|
||||||
send_amounts: Receiver<(ResponseChannel<AliceToBob>, SwapAmounts)>,
|
send_amounts: Receiver<(ResponseChannel<AliceToBob>, SwapAmounts)>,
|
||||||
|
send_msg0: Receiver<(ResponseChannel<AliceToBob>, alice::Message0)>,
|
||||||
send_msg1: Receiver<(ResponseChannel<AliceToBob>, alice::Message1)>,
|
send_msg1: Receiver<(ResponseChannel<AliceToBob>, alice::Message1)>,
|
||||||
send_msg2: Receiver<(ResponseChannel<AliceToBob>, alice::Message2)>,
|
send_msg2: Receiver<(ResponseChannel<AliceToBob>, alice::Message2)>,
|
||||||
}
|
}
|
||||||
@ -149,6 +160,7 @@ impl EventLoop {
|
|||||||
let request = Channels::new();
|
let request = Channels::new();
|
||||||
let conn_established = Channels::new();
|
let conn_established = Channels::new();
|
||||||
let send_amounts = Channels::new();
|
let send_amounts = Channels::new();
|
||||||
|
let send_msg0 = Channels::new();
|
||||||
let send_msg1 = Channels::new();
|
let send_msg1 = Channels::new();
|
||||||
let send_msg2 = Channels::new();
|
let send_msg2 = Channels::new();
|
||||||
|
|
||||||
@ -161,6 +173,7 @@ impl EventLoop {
|
|||||||
request: request.sender,
|
request: request.sender,
|
||||||
conn_established: conn_established.sender,
|
conn_established: conn_established.sender,
|
||||||
send_amounts: send_amounts.receiver,
|
send_amounts: send_amounts.receiver,
|
||||||
|
send_msg0: send_msg0.receiver,
|
||||||
send_msg1: send_msg1.receiver,
|
send_msg1: send_msg1.receiver,
|
||||||
send_msg2: send_msg2.receiver,
|
send_msg2: send_msg2.receiver,
|
||||||
};
|
};
|
||||||
@ -173,6 +186,7 @@ impl EventLoop {
|
|||||||
request: request.receiver,
|
request: request.receiver,
|
||||||
conn_established: conn_established.receiver,
|
conn_established: conn_established.receiver,
|
||||||
send_amounts: send_amounts.sender,
|
send_amounts: send_amounts.sender,
|
||||||
|
send_msg0: send_msg0.sender,
|
||||||
send_msg1: send_msg1.sender,
|
send_msg1: send_msg1.sender,
|
||||||
send_msg2: send_msg2.sender,
|
send_msg2: send_msg2.sender,
|
||||||
};
|
};
|
||||||
@ -188,8 +202,8 @@ impl EventLoop {
|
|||||||
OutEvent::ConnectionEstablished(alice) => {
|
OutEvent::ConnectionEstablished(alice) => {
|
||||||
let _ = self.conn_established.send(alice).await;
|
let _ = self.conn_established.send(alice).await;
|
||||||
}
|
}
|
||||||
OutEvent::Message0(msg) => {
|
OutEvent::Message0 { msg, channel } => {
|
||||||
let _ = self.msg0.send(msg).await;
|
let _ = self.msg0.send((msg, channel)).await;
|
||||||
}
|
}
|
||||||
OutEvent::Message1 { msg, channel } => {
|
OutEvent::Message1 { msg, channel } => {
|
||||||
let _ = self.msg1.send((msg, channel)).await;
|
let _ = self.msg1.send((msg, channel)).await;
|
||||||
@ -210,6 +224,11 @@ impl EventLoop {
|
|||||||
self.swarm.send_amounts(channel, amounts);
|
self.swarm.send_amounts(channel, amounts);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
msg0 = self.send_msg0.next().fuse() => {
|
||||||
|
if let Some((channel, msg)) = msg0 {
|
||||||
|
self.swarm.send_message0(channel, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
msg1 = self.send_msg1.next().fuse() => {
|
msg1 = self.send_msg1.next().fuse() => {
|
||||||
if let Some((channel, msg)) = msg1 {
|
if let Some((channel, msg)) = msg1 {
|
||||||
self.swarm.send_message1(channel, msg);
|
self.swarm.send_message1(channel, msg);
|
||||||
|
@ -6,7 +6,6 @@ use libp2p::{
|
|||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
||||||
NetworkBehaviour,
|
NetworkBehaviour,
|
||||||
};
|
};
|
||||||
use rand::rngs::OsRng;
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
@ -15,11 +14,15 @@ use std::{
|
|||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT};
|
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT};
|
||||||
use xmr_btc::{alice::State0, bob};
|
use libp2p::request_response::ResponseChannel;
|
||||||
|
use xmr_btc::bob;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
Msg(bob::Message0),
|
Msg {
|
||||||
|
msg: bob::Message0,
|
||||||
|
channel: ResponseChannel<AliceToBob>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents send/recv of message 0.
|
/// A `NetworkBehaviour` that represents send/recv of message 0.
|
||||||
@ -30,12 +33,28 @@ pub struct Message0 {
|
|||||||
rr: RequestResponse<Codec<Message0Protocol>>,
|
rr: RequestResponse<Codec<Message0Protocol>>,
|
||||||
#[behaviour(ignore)]
|
#[behaviour(ignore)]
|
||||||
events: VecDeque<OutEvent>,
|
events: VecDeque<OutEvent>,
|
||||||
#[behaviour(ignore)]
|
|
||||||
state: State0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message0 {
|
impl Message0 {
|
||||||
pub fn new(state: State0) -> Self {
|
pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: xmr_btc::alice::Message0) {
|
||||||
|
let msg = AliceToBob::Message0(msg);
|
||||||
|
self.rr.send_response(channel, msg);
|
||||||
|
}
|
||||||
|
fn poll(
|
||||||
|
&mut self,
|
||||||
|
_: &mut Context<'_>,
|
||||||
|
_: &mut impl PollParameters,
|
||||||
|
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message0Protocol>>, OutEvent>> {
|
||||||
|
if let Some(event) = self.events.pop_front() {
|
||||||
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Message0 {
|
||||||
|
fn default() -> Self {
|
||||||
let timeout = Duration::from_secs(TIMEOUT);
|
let timeout = Duration::from_secs(TIMEOUT);
|
||||||
let mut config = RequestResponseConfig::default();
|
let mut config = RequestResponseConfig::default();
|
||||||
config.set_request_timeout(timeout);
|
config.set_request_timeout(timeout);
|
||||||
@ -47,21 +66,8 @@ impl Message0 {
|
|||||||
config,
|
config,
|
||||||
),
|
),
|
||||||
events: Default::default(),
|
events: Default::default(),
|
||||||
state,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll(
|
|
||||||
&mut self,
|
|
||||||
_: &mut Context<'_>,
|
|
||||||
_: &mut impl PollParameters,
|
|
||||||
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message0Protocol>>, OutEvent>> {
|
|
||||||
if let Some(event) = self.events.pop_front() {
|
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
|
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
|
||||||
@ -76,13 +82,7 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
|
|||||||
} => {
|
} => {
|
||||||
if let BobToAlice::Message0(msg) = request {
|
if let BobToAlice::Message0(msg) = request {
|
||||||
debug!("Received Message0");
|
debug!("Received Message0");
|
||||||
// TODO(Franck): Move this business logic out of the network behaviour.
|
self.events.push_back(OutEvent::Msg { msg, channel });
|
||||||
let response = AliceToBob::Message0(self.state.next_message(&mut OsRng));
|
|
||||||
|
|
||||||
self.rr.send_response(channel, response);
|
|
||||||
debug!("Sent Message0");
|
|
||||||
|
|
||||||
self.events.push_back(OutEvent::Msg(msg));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RequestResponseEvent::Message {
|
RequestResponseEvent::Message {
|
||||||
|
@ -10,6 +10,7 @@ use futures::{
|
|||||||
};
|
};
|
||||||
use libp2p::request_response::ResponseChannel;
|
use libp2p::request_response::ResponseChannel;
|
||||||
|
|
||||||
|
use rand::rngs::OsRng;
|
||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
@ -59,7 +60,13 @@ pub async fn negotiate(
|
|||||||
.send_amounts(event.channel, amounts)
|
.send_amounts(event.channel, amounts)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let bob_message0 = timeout(config.bob_time_to_act, event_loop_handle.recv_message0()).await??;
|
let (bob_message0, channel) =
|
||||||
|
timeout(config.bob_time_to_act, event_loop_handle.recv_message0()).await??;
|
||||||
|
|
||||||
|
let alice_message0 = state0.next_message(&mut OsRng);
|
||||||
|
event_loop_handle
|
||||||
|
.send_message0(channel, alice_message0)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let state1 = state0.receive(bob_message0)?;
|
let state1 = state0.receive(bob_message0)?;
|
||||||
|
|
@ -3,7 +3,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
alice::{
|
alice::{
|
||||||
event_loop::EventLoopHandle,
|
event_loop::EventLoopHandle,
|
||||||
execution::{
|
negotiate::{
|
||||||
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
|
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
|
||||||
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
|
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
|
||||||
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
|
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
|
||||||
@ -110,29 +110,6 @@ impl fmt::Display for AliceState {
|
|||||||
impl From<&AliceState> for state::Alice {
|
impl From<&AliceState> for state::Alice {
|
||||||
fn from(alice_state: &AliceState) -> Self {
|
fn from(alice_state: &AliceState) -> Self {
|
||||||
match alice_state {
|
match alice_state {
|
||||||
AliceState::Started {
|
|
||||||
amounts,
|
|
||||||
state0:
|
|
||||||
State0 {
|
|
||||||
a,
|
|
||||||
s_a,
|
|
||||||
v_a,
|
|
||||||
refund_timelock,
|
|
||||||
punish_timelock,
|
|
||||||
redeem_address,
|
|
||||||
punish_address,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
} => Alice::Started {
|
|
||||||
amounts: *amounts,
|
|
||||||
a: a.clone(),
|
|
||||||
s_a: *s_a,
|
|
||||||
v_a: *v_a,
|
|
||||||
refund_timelock: *refund_timelock,
|
|
||||||
punish_timelock: *punish_timelock,
|
|
||||||
redeem_address: redeem_address.clone(),
|
|
||||||
punish_address: punish_address.clone(),
|
|
||||||
},
|
|
||||||
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.clone()),
|
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.clone()),
|
||||||
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.clone()),
|
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.clone()),
|
||||||
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.clone()),
|
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.clone()),
|
||||||
@ -153,6 +130,10 @@ impl From<&AliceState> for state::Alice {
|
|||||||
AliceState::Cancelling { state3 } => Alice::Cancelling(state3.clone()),
|
AliceState::Cancelling { state3 } => Alice::Cancelling(state3.clone()),
|
||||||
AliceState::Punished => Alice::SwapComplete,
|
AliceState::Punished => Alice::SwapComplete,
|
||||||
AliceState::SafelyAborted => Alice::SwapComplete,
|
AliceState::SafelyAborted => Alice::SwapComplete,
|
||||||
|
// todo: we may want to swap recovering from swaps that have not been negotiated
|
||||||
|
AliceState::Started { .. } => {
|
||||||
|
panic!("Alice attempted to save swap before being negotiated")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,29 +145,6 @@ impl TryFrom<state::Swap> for AliceState {
|
|||||||
use AliceState::*;
|
use AliceState::*;
|
||||||
if let Swap::Alice(state) = db_state {
|
if let Swap::Alice(state) = db_state {
|
||||||
let alice_state = match state {
|
let alice_state = match state {
|
||||||
Alice::Started {
|
|
||||||
amounts,
|
|
||||||
a,
|
|
||||||
s_a,
|
|
||||||
v_a,
|
|
||||||
refund_timelock,
|
|
||||||
punish_timelock,
|
|
||||||
redeem_address,
|
|
||||||
punish_address,
|
|
||||||
} => Started {
|
|
||||||
amounts,
|
|
||||||
state0: State0 {
|
|
||||||
a,
|
|
||||||
s_a,
|
|
||||||
v_a,
|
|
||||||
btc: amounts.btc,
|
|
||||||
xmr: amounts.xmr,
|
|
||||||
refund_timelock,
|
|
||||||
punish_timelock,
|
|
||||||
redeem_address,
|
|
||||||
punish_address,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Alice::Negotiated(state3) => Negotiated {
|
Alice::Negotiated(state3) => Negotiated {
|
||||||
channel: None,
|
channel: None,
|
||||||
amounts: SwapAmounts {
|
amounts: SwapAmounts {
|
||||||
|
@ -97,11 +97,8 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
(
|
(
|
||||||
AliceState::Started {
|
AliceState::Started { amounts, state0 },
|
||||||
amounts,
|
alice::Behaviour::default(),
|
||||||
state0: state0.clone(),
|
|
||||||
},
|
|
||||||
alice::Behaviour::new(state0),
|
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -20,11 +20,11 @@ use xmr_btc::{
|
|||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution;
|
|
||||||
mod message0;
|
mod message0;
|
||||||
mod message1;
|
mod message1;
|
||||||
mod message2;
|
mod message2;
|
||||||
mod message3;
|
mod message3;
|
||||||
|
mod negotiate;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
|
|
||||||
pub type Swarm = libp2p::Swarm<Behaviour>;
|
pub type Swarm = libp2p::Swarm<Behaviour>;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bob::{event_loop::EventLoopHandle, execution::negotiate},
|
bob::{event_loop::EventLoopHandle, negotiate::negotiate},
|
||||||
state,
|
state,
|
||||||
state::Bob,
|
state::Bob,
|
||||||
storage::Database,
|
storage::Database,
|
||||||
@ -59,15 +59,10 @@ impl fmt::Display for BobState {
|
|||||||
impl From<BobState> for state::Bob {
|
impl From<BobState> for state::Bob {
|
||||||
fn from(bob_state: BobState) -> Self {
|
fn from(bob_state: BobState) -> Self {
|
||||||
match bob_state {
|
match bob_state {
|
||||||
BobState::Started {
|
BobState::Started { .. } => {
|
||||||
state0,
|
// TODO: Do we want to resume just started swaps
|
||||||
amounts,
|
unimplemented!("Cannot save a swap that has just started")
|
||||||
addr,
|
}
|
||||||
} => Bob::Started {
|
|
||||||
state0,
|
|
||||||
amounts,
|
|
||||||
addr,
|
|
||||||
},
|
|
||||||
BobState::Negotiated(state2, peer_id) => Bob::Negotiated { state2, peer_id },
|
BobState::Negotiated(state2, peer_id) => Bob::Negotiated { state2, peer_id },
|
||||||
BobState::BtcLocked(state3, peer_id) => Bob::BtcLocked { state3, peer_id },
|
BobState::BtcLocked(state3, peer_id) => Bob::BtcLocked { state3, peer_id },
|
||||||
BobState::XmrLocked(state4, peer_id) => Bob::XmrLocked { state4, peer_id },
|
BobState::XmrLocked(state4, peer_id) => Bob::XmrLocked { state4, peer_id },
|
||||||
@ -85,15 +80,6 @@ impl From<BobState> for state::Bob {
|
|||||||
impl From<state::Bob> for BobState {
|
impl From<state::Bob> for BobState {
|
||||||
fn from(bob: Bob) -> Self {
|
fn from(bob: Bob) -> Self {
|
||||||
match bob {
|
match bob {
|
||||||
Bob::Started {
|
|
||||||
state0,
|
|
||||||
amounts,
|
|
||||||
addr,
|
|
||||||
} => BobState::Started {
|
|
||||||
state0,
|
|
||||||
amounts,
|
|
||||||
addr,
|
|
||||||
},
|
|
||||||
Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id),
|
Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id),
|
||||||
Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id),
|
Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id),
|
||||||
Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id),
|
Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id),
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
use crate::SwapAmounts;
|
use libp2p::PeerId;
|
||||||
use libp2p::{core::Multiaddr, PeerId};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use xmr_btc::{
|
use xmr_btc::{alice, bitcoin::EncryptedSignature, bob, monero, serde::monero_private_key};
|
||||||
alice, bitcoin::EncryptedSignature, bob, cross_curve_dleq, monero, serde::monero_private_key,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
@ -16,18 +13,6 @@ pub enum Swap {
|
|||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
pub enum Alice {
|
pub enum Alice {
|
||||||
Started {
|
|
||||||
amounts: SwapAmounts,
|
|
||||||
// TODO: This should not be saved, instead always derive it from a seed (and that seed file
|
|
||||||
// is the only thing that has to be kept secure)
|
|
||||||
a: crate::bitcoin::SecretKey,
|
|
||||||
s_a: cross_curve_dleq::Scalar,
|
|
||||||
v_a: monero::PrivateViewKey,
|
|
||||||
refund_timelock: u32,
|
|
||||||
punish_timelock: u32,
|
|
||||||
redeem_address: ::bitcoin::Address,
|
|
||||||
punish_address: ::bitcoin::Address,
|
|
||||||
},
|
|
||||||
Negotiated(alice::State3),
|
Negotiated(alice::State3),
|
||||||
BtcLocked(alice::State3),
|
BtcLocked(alice::State3),
|
||||||
XmrLocked(alice::State3),
|
XmrLocked(alice::State3),
|
||||||
@ -54,11 +39,6 @@ pub enum Alice {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
pub enum Bob {
|
pub enum Bob {
|
||||||
Started {
|
|
||||||
state0: bob::State0,
|
|
||||||
amounts: SwapAmounts,
|
|
||||||
addr: Multiaddr,
|
|
||||||
},
|
|
||||||
Negotiated {
|
Negotiated {
|
||||||
state2: bob::State2,
|
state2: bob::State2,
|
||||||
#[serde(with = "crate::serde::peer_id")]
|
#[serde(with = "crate::serde::peer_id")]
|
||||||
@ -108,7 +88,6 @@ impl Display for Swap {
|
|||||||
impl Display for Alice {
|
impl Display for Alice {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Alice::Started { .. } => f.write_str("Swap started"),
|
|
||||||
Alice::Negotiated(_) => f.write_str("Handshake complete"),
|
Alice::Negotiated(_) => f.write_str("Handshake complete"),
|
||||||
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
|
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
|
||||||
Alice::XmrLocked(_) => f.write_str("Monero locked"),
|
Alice::XmrLocked(_) => f.write_str("Monero locked"),
|
||||||
@ -132,7 +111,6 @@ impl Display for Bob {
|
|||||||
}
|
}
|
||||||
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
||||||
Bob::SwapComplete => f.write_str("Swap complete"),
|
Bob::SwapComplete => f.write_str("Swap complete"),
|
||||||
Bob::Started { .. } => f.write_str("Swap started"),
|
|
||||||
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,12 +123,9 @@ pub async fn init_alice_eventloop(
|
|||||||
redeem_address,
|
redeem_address,
|
||||||
punish_address,
|
punish_address,
|
||||||
);
|
);
|
||||||
let start_state = AliceState::Started {
|
let start_state = AliceState::Started { amounts, state0 };
|
||||||
amounts,
|
|
||||||
state0: state0.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let alice_behaviour = alice::Behaviour::new(state0);
|
let alice_behaviour = alice::Behaviour::default();
|
||||||
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||||
|
|
||||||
let (swarm_driver, handle) =
|
let (swarm_driver, handle) =
|
||||||
|
Loading…
Reference in New Issue
Block a user