Move protocol definition to network module

This commit is contained in:
Thomas Eizinger 2021-06-24 19:59:58 +10:00
parent 39e0b45da9
commit 301d5be127
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 29 additions and 32 deletions

View File

@ -6,6 +6,7 @@ pub mod json_pull_codec;
pub mod quote;
pub mod redial;
pub mod spot_price;
pub mod swap_setup;
pub mod swarm;
pub mod tor_transport;
pub mod transfer_proof;

View File

@ -0,0 +1,26 @@
pub mod protocol {
use futures::future;
use libp2p::core::upgrade::{from_fn, FromFnUpgrade};
use libp2p::core::Endpoint;
use libp2p::swarm::NegotiatedSubstream;
use void::Void;
pub fn new() -> SwapSetup {
from_fn(
b"/comit/xmr/btc/swap_setup/1.0.0",
Box::new(|socket, _| future::ready(Ok(socket))),
)
}
pub type SwapSetup = FromFnUpgrade<
&'static [u8],
Box<
dyn Fn(
NegotiatedSubstream,
Endpoint,
) -> future::Ready<Result<NegotiatedSubstream, Void>>
+ Send
+ 'static,
>,
>;
}

View File

@ -1,3 +1,4 @@
use crate::network::swap_setup::protocol;
use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::alice::{State0, State3};
use crate::protocol::{alice, Message0, Message2, Message4};
@ -6,8 +7,7 @@ use anyhow::{anyhow, Context as _, Result};
use futures::future::{BoxFuture, OptionFuture};
use futures::FutureExt;
use libp2p::core::connection::ConnectionId;
use libp2p::core::upgrade::{from_fn, FromFnUpgrade};
use libp2p::core::{upgrade, Endpoint};
use libp2p::core::upgrade;
use libp2p::swarm::{
KeepAlive, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
@ -17,12 +17,10 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::fmt::Debug;
use std::future;
use std::task::{Context, Poll};
use std::time::Duration;
use uuid::Uuid;
use void::Void;
use std::time::Duration;
#[derive(Debug)]
pub enum OutEvent {
@ -532,34 +530,6 @@ where
Ok(())
}
mod protocol {
use super::*;
pub fn new() -> SwapSetup {
from_fn(
b"/comit/xmr/btc/swap_setup/1.0.0",
Box::new(|socket, endpoint| {
future::ready(match endpoint {
Endpoint::Listener => Ok(socket),
Endpoint::Dialer => todo!("return error"),
})
}),
)
}
pub type SwapSetup = FromFnUpgrade<
&'static [u8],
Box<
dyn Fn(
NegotiatedSubstream,
Endpoint,
) -> future::Ready<Result<NegotiatedSubstream, Void>>
+ Send
+ 'static,
>,
>;
}
// TODO: Differentiate between errors that we send back and shit that happens on
// our side (IO, timeout)
#[derive(Debug, thiserror::Error)]