Re-registration

The fact that we have the rendezvous behaviour composed into the ASB behaviour and handle all events in one eventloop is not optimal. It would be nice if we can handle rendezvous completely separate.
This commit is contained in:
Daniel Karzel 2021-06-30 12:00:51 +10:00
parent 76c0031253
commit abde566b35
No known key found for this signature in database
GPG key ID: 30C3FC2E438ADB6E
3 changed files with 38 additions and 1 deletions

View file

@ -14,7 +14,7 @@ use futures::stream::{FuturesUnordered, StreamExt};
use libp2p::rendezvous::Namespace; use libp2p::rendezvous::Namespace;
use libp2p::request_response::{RequestId, ResponseChannel}; use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::swarm::SwarmEvent; use libp2p::swarm::SwarmEvent;
use libp2p::{PeerId, Swarm}; use libp2p::{Multiaddr, PeerId, Swarm};
use rust_decimal::Decimal; use rust_decimal::Decimal;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::Infallible; use std::convert::Infallible;
@ -68,6 +68,7 @@ where
// separate swarm? There is no dependency between swap and registration, so we could just // separate swarm? There is no dependency between swap and registration, so we could just
// build this completely separate. // build this completely separate.
rendezvous_node_peer_id: PeerId, rendezvous_node_peer_id: PeerId,
rendezvous_node_addr: Multiaddr,
rendezvous_namespace: XmrBtcNamespace, rendezvous_namespace: XmrBtcNamespace,
rendezvous_reregister_timestamp: Option<Instant>, rendezvous_reregister_timestamp: Option<Instant>,
} }
@ -87,6 +88,7 @@ where
min_buy: bitcoin::Amount, min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount, max_buy: bitcoin::Amount,
rendezvous_node_peer_id: PeerId, rendezvous_node_peer_id: PeerId,
rendezvous_node_addr: Multiaddr,
rendezvous_namespace: XmrBtcNamespace, rendezvous_namespace: XmrBtcNamespace,
) -> Result<(Self, mpsc::Receiver<Swap>)> { ) -> Result<(Self, mpsc::Receiver<Swap>)> {
let swap_channel = MpscChannels::default(); let swap_channel = MpscChannels::default();
@ -107,6 +109,7 @@ where
buffered_transfer_proofs: Default::default(), buffered_transfer_proofs: Default::default(),
inflight_transfer_proofs: Default::default(), inflight_transfer_proofs: Default::default(),
rendezvous_node_peer_id, rendezvous_node_peer_id,
rendezvous_node_addr,
rendezvous_namespace, rendezvous_namespace,
rendezvous_reregister_timestamp: None, rendezvous_reregister_timestamp: None,
}; };
@ -162,6 +165,38 @@ where
} }
loop { loop {
// rendezvous node re-registration
if let Some(rendezvous_reregister_timestamp) = self.rendezvous_reregister_timestamp {
if Instant::now() > rendezvous_reregister_timestamp {
if self.swarm.is_connected(&self.rendezvous_node_peer_id) {
match self.swarm.behaviour_mut().rendezvous.register(
Namespace::new(self.rendezvous_namespace.to_string())
.expect("our namespace to be a correct string"),
self.rendezvous_node_peer_id,
None,
) {
Ok(()) => {}
Err(err) => {
tracing::error!(
"Sending registration to rendezvous failed: {:#}",
err
);
}
}
} else {
match Swarm::dial_addr(&mut self.swarm, self.rendezvous_node_addr.clone()) {
Ok(()) => {}
Err(error) => {
tracing::error!(
"Failed to redial rendezvous node for re-registration: {:#}",
error
);
}
}
}
}
}
tokio::select! { tokio::select! {
swarm_event = self.swarm.next() => { swarm_event = self.swarm.next() => {
match swarm_event { match swarm_event {

View file

@ -188,6 +188,7 @@ async fn main() -> Result<()> {
config.maker.min_buy_btc, config.maker.min_buy_btc,
config.maker.max_buy_btc, config.maker.max_buy_btc,
config.rendezvous_node.peer_id, config.rendezvous_node.peer_id,
config.rendezvous_node.addr,
namespace, namespace,
) )
.unwrap(); .unwrap();

View file

@ -251,6 +251,7 @@ async fn start_alice(
min_buy, min_buy,
max_buy, max_buy,
PeerId::random(), PeerId::random(),
Multiaddr::empty(),
XmrBtcNamespace::Testnet, XmrBtcNamespace::Testnet,
) )
.unwrap(); .unwrap();