parent
ecb54958ee
commit
081237bb6f
@ -1,119 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
bitcoin, monero,
|
|
||||||
network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT},
|
|
||||||
protocol::bob,
|
|
||||||
};
|
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage, ResponseChannel,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg {
|
|
||||||
msg: bob::Message0,
|
|
||||||
channel: ResponseChannel<AliceToBob>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Message0 {
|
|
||||||
pub(crate) A: bitcoin::PublicKey,
|
|
||||||
pub(crate) S_a_monero: monero::PublicKey,
|
|
||||||
pub(crate) S_a_bitcoin: bitcoin::PublicKey,
|
|
||||||
pub(crate) dleq_proof_s_a: cross_curve_dleq::Proof,
|
|
||||||
pub(crate) v_a: monero::PrivateViewKey,
|
|
||||||
pub(crate) redeem_address: bitcoin::Address,
|
|
||||||
pub(crate) punish_address: bitcoin::Address,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents send/recv of message 0.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message0Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: Message0) -> Result<()> {
|
|
||||||
let msg = AliceToBob::Message0(Box::new(msg));
|
|
||||||
self.rr
|
|
||||||
.send_response(channel, msg)
|
|
||||||
.map_err(|alice_to_bob| anyhow!("Could not send response {:?}", alice_to_bob))
|
|
||||||
}
|
|
||||||
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 Behaviour {
|
|
||||||
fn default() -> Self {
|
|
||||||
let timeout = Duration::from_secs(TIMEOUT);
|
|
||||||
let mut config = RequestResponseConfig::default();
|
|
||||||
config.set_request_timeout(timeout);
|
|
||||||
|
|
||||||
Self {
|
|
||||||
rr: RequestResponse::new(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message0Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message:
|
|
||||||
RequestResponseMessage::Request {
|
|
||||||
request, channel, ..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let BobToAlice::Message0(msg) = request {
|
|
||||||
debug!("Received Message0");
|
|
||||||
self.events.push_back(OutEvent::Msg { msg: *msg, channel });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Alice should not get a Response"),
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
debug!("Alice has sent Message0 as response to Bob");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT},
|
|
||||||
protocol::bob,
|
|
||||||
};
|
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
use ecdsa_fun::{adaptor::EncryptedSignature, Signature};
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage, ResponseChannel,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg {
|
|
||||||
/// Received message from Bob.
|
|
||||||
msg: bob::Message1,
|
|
||||||
/// Channel to send back Alice's message 1.
|
|
||||||
channel: ResponseChannel<AliceToBob>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Message1 {
|
|
||||||
pub(crate) tx_cancel_sig: Signature,
|
|
||||||
pub(crate) tx_refund_encsig: EncryptedSignature,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents send/recv of message 1.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message1Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: Message1) -> Result<()> {
|
|
||||||
let msg = AliceToBob::Message1(Box::new(msg));
|
|
||||||
self.rr
|
|
||||||
.send_response(channel, msg)
|
|
||||||
.map_err(|alice_to_bob| anyhow!("Could not send response {:?}", alice_to_bob))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll(
|
|
||||||
&mut self,
|
|
||||||
_: &mut Context<'_>,
|
|
||||||
_: &mut impl PollParameters,
|
|
||||||
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message1Protocol>>, OutEvent>> {
|
|
||||||
if let Some(event) = self.events.pop_front() {
|
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message1Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message:
|
|
||||||
RequestResponseMessage::Request {
|
|
||||||
request, channel, ..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let BobToAlice::Message1(msg) = request {
|
|
||||||
debug!("Received Message1");
|
|
||||||
self.events.push_back(OutEvent::Msg { msg: *msg, channel });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Alice should not get a Response"),
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
debug!("Alice has sent an Message1 response to Bob");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT},
|
|
||||||
protocol::bob,
|
|
||||||
};
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour, PeerId,
|
|
||||||
};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg {
|
|
||||||
msg: bob::Message2,
|
|
||||||
bob_peer_id: PeerId,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents receiving of message 2 from Bob.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message2Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
fn poll(
|
|
||||||
&mut self,
|
|
||||||
_: &mut Context<'_>,
|
|
||||||
_: &mut impl PollParameters,
|
|
||||||
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message2Protocol>>, OutEvent>> {
|
|
||||||
if let Some(event) = self.events.pop_front() {
|
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message2Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
peer,
|
|
||||||
message:
|
|
||||||
RequestResponseMessage::Request {
|
|
||||||
request, channel, ..
|
|
||||||
},
|
|
||||||
} => {
|
|
||||||
if let BobToAlice::Message2(msg) = request {
|
|
||||||
debug!("Received Message 2");
|
|
||||||
self.events.push_back(OutEvent::Msg {
|
|
||||||
msg: *msg,
|
|
||||||
bob_peer_id: peer,
|
|
||||||
});
|
|
||||||
// Send back empty response so that the request/response protocol completes.
|
|
||||||
let _ = self.rr.send_response(channel, AliceToBob::Message2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Alice should not get a Response"),
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
debug!("Alice has sent a Message2 response to Bob");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,110 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
bitcoin, monero,
|
|
||||||
network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT},
|
|
||||||
protocol::{alice, bob},
|
|
||||||
};
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour, PeerId,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Message0 {
|
|
||||||
pub(crate) B: bitcoin::PublicKey,
|
|
||||||
pub(crate) S_b_monero: monero::PublicKey,
|
|
||||||
pub(crate) S_b_bitcoin: bitcoin::PublicKey,
|
|
||||||
pub(crate) dleq_proof_s_b: cross_curve_dleq::Proof,
|
|
||||||
pub(crate) v_b: monero::PrivateViewKey,
|
|
||||||
pub(crate) refund_address: bitcoin::Address,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg(alice::Message0),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents send/recv of message 0.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message0Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
pub fn send(&mut self, alice: PeerId, msg: bob::Message0) {
|
|
||||||
let msg = BobToAlice::Message0(Box::new(msg));
|
|
||||||
let _id = self.rr.send_request(&alice, 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 Behaviour {
|
|
||||||
fn default() -> Self {
|
|
||||||
let timeout = Duration::from_secs(TIMEOUT);
|
|
||||||
let mut config = RequestResponseConfig::default();
|
|
||||||
config.set_request_timeout(timeout);
|
|
||||||
|
|
||||||
Self {
|
|
||||||
rr: RequestResponse::new(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message0Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Request { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Bob should never get a request from Alice"),
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { response, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let AliceToBob::Message0(msg) = response {
|
|
||||||
debug!("Received Message0");
|
|
||||||
self.events.push_back(OutEvent::Msg(*msg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
unreachable!("Bob does not send a message0 response to Alice");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,105 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
bitcoin,
|
|
||||||
network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT},
|
|
||||||
protocol::alice,
|
|
||||||
};
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour, PeerId,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Message1 {
|
|
||||||
pub(crate) tx_lock: bitcoin::TxLock,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg(alice::Message1),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents send/recv of message 1.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message1Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
pub fn send(&mut self, alice: PeerId, msg: Message1) {
|
|
||||||
let msg = BobToAlice::Message1(Box::new(msg));
|
|
||||||
let _id = self.rr.send_request(&alice, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll(
|
|
||||||
&mut self,
|
|
||||||
_: &mut Context<'_>,
|
|
||||||
_: &mut impl PollParameters,
|
|
||||||
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message1Protocol>>, OutEvent>> {
|
|
||||||
if let Some(event) = self.events.pop_front() {
|
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message1Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Request { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Bob should never get a request from Alice"),
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { response, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let AliceToBob::Message1(msg) = response {
|
|
||||||
debug!("Received Message1");
|
|
||||||
self.events.push_back(OutEvent::Msg(*msg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
unreachable!("Bob does not send a message 1 response to Alice");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT};
|
|
||||||
use ecdsa_fun::Signature;
|
|
||||||
use libp2p::{
|
|
||||||
request_response::{
|
|
||||||
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
|
|
||||||
RequestResponseEvent, RequestResponseMessage,
|
|
||||||
},
|
|
||||||
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
||||||
NetworkBehaviour, PeerId,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::VecDeque,
|
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Message2 {
|
|
||||||
pub(crate) tx_punish_sig: Signature,
|
|
||||||
pub(crate) tx_cancel_sig: Signature,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
Msg,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents sending message 2 to Alice.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
rr: RequestResponse<Codec<Message2Protocol>>,
|
|
||||||
#[behaviour(ignore)]
|
|
||||||
events: VecDeque<OutEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
pub fn send(&mut self, alice: PeerId, msg: Message2) {
|
|
||||||
let msg = BobToAlice::Message2(Box::new(msg));
|
|
||||||
let _id = self.rr.send_request(&alice, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll(
|
|
||||||
&mut self,
|
|
||||||
_: &mut Context<'_>,
|
|
||||||
_: &mut impl PollParameters,
|
|
||||||
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message2Protocol>>, OutEvent>> {
|
|
||||||
if let Some(event) = self.events.pop_front() {
|
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
|
||||||
Codec::default(),
|
|
||||||
vec![(Message2Protocol, ProtocolSupport::Full)],
|
|
||||||
config,
|
|
||||||
),
|
|
||||||
events: VecDeque::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Behaviour {
|
|
||||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
||||||
match event {
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Request { .. },
|
|
||||||
..
|
|
||||||
} => panic!("Bob should never get a request from Alice"),
|
|
||||||
RequestResponseEvent::Message {
|
|
||||||
message: RequestResponseMessage::Response { response, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let AliceToBob::Message2 = response {
|
|
||||||
debug!("Received Message 2 acknowledgement");
|
|
||||||
self.events.push_back(OutEvent::Msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
|
||||||
error!("Inbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::OutboundFailure { error, .. } => {
|
|
||||||
error!("Outbound failure: {:?}", error);
|
|
||||||
}
|
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
|
||||||
unreachable!("Bob does not send a Message2 response to Alice");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue