2020-10-16 09:14:39 +11:00
|
|
|
use anyhow::Result;
|
|
|
|
use libp2p::{
|
|
|
|
request_response::{
|
|
|
|
handler::RequestProtocol, ProtocolSupport, RequestId, RequestResponse,
|
|
|
|
RequestResponseConfig, RequestResponseEvent, RequestResponseMessage,
|
|
|
|
},
|
|
|
|
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
|
|
|
|
NetworkBehaviour, PeerId,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
task::{Context, Poll},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-10-16 10:43:32 +11:00
|
|
|
use tracing::error;
|
2020-10-16 09:14:39 +11:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
bitcoin,
|
|
|
|
network::request_response::{AliceToBob, BobToAlice, Codec, Protocol},
|
2020-10-20 10:10:28 +11:00
|
|
|
SwapParams,
|
2020-10-16 09:14:39 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-10-20 10:10:28 +11:00
|
|
|
pub enum OutEvent {
|
2020-10-16 09:14:39 +11:00
|
|
|
Amounts(SwapParams),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Bob.
|
|
|
|
#[derive(NetworkBehaviour)]
|
2020-10-20 10:10:28 +11:00
|
|
|
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
|
2020-10-16 09:14:39 +11:00
|
|
|
#[allow(missing_debug_implementations)]
|
2020-10-20 10:10:28 +11:00
|
|
|
pub struct Amounts {
|
2020-10-16 09:14:39 +11:00
|
|
|
rr: RequestResponse<Codec>,
|
|
|
|
#[behaviour(ignore)]
|
2020-10-20 10:10:28 +11:00
|
|
|
events: VecDeque<OutEvent>,
|
2020-10-16 09:14:39 +11:00
|
|
|
}
|
|
|
|
|
2020-10-20 10:10:28 +11:00
|
|
|
impl Amounts {
|
2020-10-16 09:14:39 +11:00
|
|
|
pub fn new(timeout: Duration) -> Self {
|
|
|
|
let mut config = RequestResponseConfig::default();
|
|
|
|
config.set_request_timeout(timeout);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
rr: RequestResponse::new(
|
|
|
|
Codec::default(),
|
|
|
|
vec![(Protocol, ProtocolSupport::Full)],
|
|
|
|
config,
|
|
|
|
),
|
|
|
|
events: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn request_amounts(
|
|
|
|
&mut self,
|
|
|
|
alice: PeerId,
|
|
|
|
btc: bitcoin::Amount,
|
|
|
|
) -> Result<RequestId> {
|
|
|
|
let msg = BobToAlice::AmountsFromBtc(btc);
|
|
|
|
let id = self.rr.send_request(&alice, msg);
|
|
|
|
|
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
_: &mut Context<'_>,
|
|
|
|
_: &mut impl PollParameters,
|
2020-10-20 10:10:28 +11:00
|
|
|
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> {
|
2020-10-16 09:14:39 +11:00
|
|
|
if let Some(event) = self.events.pop_front() {
|
|
|
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 10:10:28 +11:00
|
|
|
impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts {
|
2020-10-16 09:14:39 +11:00
|
|
|
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
|
|
|
match event {
|
|
|
|
RequestResponseEvent::Message {
|
|
|
|
peer: _,
|
|
|
|
message: RequestResponseMessage::Request { .. },
|
|
|
|
} => panic!("Bob should never get a request from Alice"),
|
|
|
|
RequestResponseEvent::Message {
|
|
|
|
peer: _,
|
|
|
|
message:
|
|
|
|
RequestResponseMessage::Response {
|
|
|
|
response,
|
|
|
|
request_id: _,
|
|
|
|
},
|
|
|
|
} => match response {
|
2020-10-20 10:10:28 +11:00
|
|
|
AliceToBob::Amounts(p) => self.events.push_back(OutEvent::Amounts(p)),
|
2020-10-16 09:14:39 +11:00
|
|
|
},
|
|
|
|
|
|
|
|
RequestResponseEvent::InboundFailure { .. } => {
|
|
|
|
panic!("Bob should never get a request from Alice, so should never get an InboundFailure");
|
|
|
|
}
|
|
|
|
RequestResponseEvent::OutboundFailure {
|
|
|
|
peer: _,
|
|
|
|
request_id: _,
|
|
|
|
error,
|
|
|
|
} => {
|
|
|
|
error!("Outbound failure: {:?}", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|