Previously, we had two implementations of this protocol. To reduce code size, we make Alice and Bob use the same implementation.pull/340/head
parent
9d0b9abde0
commit
9979cc9f1f
@ -0,0 +1,44 @@
|
||||
use crate::monero;
|
||||
use crate::network::request_response::CborCodec;
|
||||
use libp2p::core::ProtocolName;
|
||||
use libp2p::request_response::{
|
||||
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
|
||||
RequestResponseMessage,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub type OutEvent = RequestResponseEvent<Request, ()>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct TransferProofProtocol;
|
||||
|
||||
impl ProtocolName for TransferProofProtocol {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
b"/comit/xmr/btc/transfer_proof/1.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Request {
|
||||
pub tx_lock_proof: monero::TransferProof,
|
||||
}
|
||||
|
||||
pub type Behaviour = RequestResponse<CborCodec<TransferProofProtocol, Request, ()>>;
|
||||
|
||||
pub type Message = RequestResponseMessage<Request, ()>;
|
||||
|
||||
pub fn alice() -> Behaviour {
|
||||
Behaviour::new(
|
||||
CborCodec::default(),
|
||||
vec![(TransferProofProtocol, ProtocolSupport::Outbound)],
|
||||
RequestResponseConfig::default(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bob() -> Behaviour {
|
||||
Behaviour::new(
|
||||
CborCodec::default(),
|
||||
vec![(TransferProofProtocol, ProtocolSupport::Inbound)],
|
||||
RequestResponseConfig::default(),
|
||||
)
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
use crate::monero;
|
||||
use crate::network::request_response::{CborCodec, TransferProofProtocol, TIMEOUT};
|
||||
use anyhow::{anyhow, Error};
|
||||
use libp2p::request_response::{
|
||||
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
|
||||
RequestResponseMessage,
|
||||
};
|
||||
use libp2p::{NetworkBehaviour, PeerId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct TransferProof {
|
||||
pub tx_lock_proof: monero::TransferProof,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
Acknowledged(PeerId),
|
||||
Failure { peer: PeerId, error: Error },
|
||||
}
|
||||
|
||||
/// A `NetworkBehaviour` that represents sending the Monero transfer proof to
|
||||
/// Bob.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Behaviour {
|
||||
rr: RequestResponse<CborCodec<TransferProofProtocol, TransferProof, ()>>,
|
||||
}
|
||||
|
||||
impl Behaviour {
|
||||
pub fn send(&mut self, bob: PeerId, msg: TransferProof) {
|
||||
let _id = self.rr.send_request(&bob, msg);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Behaviour {
|
||||
fn default() -> Self {
|
||||
let timeout = Duration::from_secs(TIMEOUT);
|
||||
let mut config = RequestResponseConfig::default();
|
||||
config.set_request_timeout(timeout);
|
||||
|
||||
Self {
|
||||
rr: RequestResponse::new(
|
||||
CborCodec::default(),
|
||||
vec![(TransferProofProtocol, ProtocolSupport::Outbound)],
|
||||
config,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RequestResponseEvent<TransferProof, ()>> for OutEvent {
|
||||
fn from(event: RequestResponseEvent<TransferProof, ()>) -> Self {
|
||||
match event {
|
||||
RequestResponseEvent::Message {
|
||||
message: RequestResponseMessage::Request { .. },
|
||||
peer,
|
||||
} => OutEvent::Failure {
|
||||
peer,
|
||||
error: anyhow!("Alice should never get a transfer proof request from Bob"),
|
||||
},
|
||||
RequestResponseEvent::Message {
|
||||
message: RequestResponseMessage::Response { .. },
|
||||
peer,
|
||||
} => OutEvent::Acknowledged(peer),
|
||||
RequestResponseEvent::InboundFailure { error, peer, .. } => OutEvent::Failure {
|
||||
peer,
|
||||
error: anyhow!("Inbound failure: {:?}", error),
|
||||
},
|
||||
RequestResponseEvent::OutboundFailure { error, peer, .. } => OutEvent::Failure {
|
||||
peer,
|
||||
error: anyhow!("Outbound failure: {:?}", error),
|
||||
},
|
||||
RequestResponseEvent::ResponseSent { peer, .. } => OutEvent::Failure {
|
||||
peer,
|
||||
error: anyhow!("Alice should not send a response"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
use crate::network::request_response::{CborCodec, TransferProofProtocol, TIMEOUT};
|
||||
use crate::protocol::alice::TransferProof;
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use libp2p::request_response::{
|
||||
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
|
||||
RequestResponseMessage, ResponseChannel,
|
||||
};
|
||||
use libp2p::NetworkBehaviour;
|
||||
use std::time::Duration;
|
||||
use tracing::debug;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
MsgReceived {
|
||||
msg: TransferProof,
|
||||
channel: ResponseChannel<()>,
|
||||
},
|
||||
AckSent,
|
||||
Failure(Error),
|
||||
}
|
||||
|
||||
/// A `NetworkBehaviour` that represents receiving the transfer proof from
|
||||
/// Alice.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Behaviour {
|
||||
rr: RequestResponse<CborCodec<TransferProofProtocol, TransferProof, ()>>,
|
||||
}
|
||||
|
||||
impl Behaviour {
|
||||
pub fn send_ack(&mut self, channel: ResponseChannel<()>) -> Result<()> {
|
||||
self.rr
|
||||
.send_response(channel, ())
|
||||
.map_err(|err| anyhow!("Failed to ack transfer proof: {:?}", err))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Behaviour {
|
||||
fn default() -> Self {
|
||||
let timeout = Duration::from_secs(TIMEOUT);
|
||||
let mut config = RequestResponseConfig::default();
|
||||
config.set_request_timeout(timeout);
|
||||
|
||||
Self {
|
||||
rr: RequestResponse::new(
|
||||
CborCodec::default(),
|
||||
vec![(TransferProofProtocol, ProtocolSupport::Inbound)],
|
||||
config,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RequestResponseEvent<TransferProof, ()>> for OutEvent {
|
||||
fn from(event: RequestResponseEvent<TransferProof, ()>) -> Self {
|
||||
match event {
|
||||
RequestResponseEvent::Message {
|
||||
peer,
|
||||
message:
|
||||
RequestResponseMessage::Request {
|
||||
request, channel, ..
|
||||
},
|
||||
..
|
||||
} => {
|
||||
debug!("Received Transfer Proof from {}", peer);
|
||||
OutEvent::MsgReceived {
|
||||
msg: request,
|
||||
channel,
|
||||
}
|
||||
}
|
||||
RequestResponseEvent::Message {
|
||||
message: RequestResponseMessage::Response { .. },
|
||||
..
|
||||
} => OutEvent::Failure(anyhow!("Bob should not get a Response")),
|
||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
||||
OutEvent::Failure(anyhow!("Inbound failure: {:?}", error))
|
||||
}
|
||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
||||
OutEvent::Failure(anyhow!("Outbound failure: {:?}", error))
|
||||
}
|
||||
RequestResponseEvent::ResponseSent { .. } => OutEvent::AckSent,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue