use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use rings_core::dht::Did;
use rings_core::swarm::SuccessorLookup;
use rings_core::swarm::Swarm;
use super::evidence::ReachabilityEvidence;
use super::BootstrapPort;
use super::DialFailure;
use crate::error::Error;
use crate::error::Result;
use crate::processor::HandshakeFailure;
use crate::processor::HandshakePeer;
use crate::processor::Processor;
use crate::seed::ValidatedSeedPeer;
const LOOKUP_PROBE_TIMEOUT: Duration = Duration::from_secs(15);
const DIAL_ADMISSION_TIMEOUT: Duration = Duration::from_secs(30);
pub(crate) struct ProcessorPort {
processor: Arc<Processor>,
evidence: Arc<ReachabilityEvidence>,
}
impl ProcessorPort {
pub(crate) fn new(processor: Arc<Processor>, evidence: Arc<ReachabilityEvidence>) -> Self {
Self {
processor,
evidence,
}
}
}
impl From<HandshakeFailure> for DialFailure {
fn from(failure: HandshakeFailure) -> Self {
match failure {
HandshakeFailure::InFlight { .. } => Self::InFlight,
HandshakeFailure::Failed(error) => Self::Failed(error),
}
}
}
#[async_trait]
impl BootstrapPort for ProcessorPort {
async fn reachable(&self, target: Did) -> bool {
let swarm = &self.processor.swarm;
match swarm.is_peer_admitted(target) {
Ok(true) => true,
Ok(false) => lookup_reaches(swarm, target).await,
Err(error) => {
tracing::error!(peer = %target, %error, "connection records unavailable");
false
}
}
}
async fn dial(&self, target: &ValidatedSeedPeer) -> std::result::Result<(), DialFailure> {
let peer = target.did();
let attempt = self
.processor
.connect_peer_via_http(
target.endpoint(),
target.api_token(),
HandshakePeer::Pinned(peer),
)
.await?;
self.finish_dial(peer, attempt).await
}
}
impl ProcessorPort {
#[cfg(all(test, feature = "dummy"))]
pub(crate) async fn dial_over(
&self,
peer: Did,
remote: &impl crate::processor::handshake::RemoteHandshake,
) -> std::result::Result<(), DialFailure> {
let attempt = self
.processor
.connect_peer_over(remote, HandshakePeer::Pinned(peer))
.await?;
self.finish_dial(peer, attempt).await
}
async fn finish_dial(
&self,
peer: Did,
attempt: rings_core::swarm::ConnectionAttempt,
) -> std::result::Result<(), DialFailure> {
let admitted = self.await_admission(peer).await;
if !matches!(admitted, Ok(true)) {
self.processor.abandon_handshake(attempt).await;
}
match admitted {
Ok(true) => Ok(()),
Ok(false) => Err(DialFailure::Failed(Error::AdmissionTimedOut { peer })),
Err(error) => Err(DialFailure::Failed(error)),
}
}
async fn await_admission(&self, peer: Did) -> Result<bool> {
let waiter = self.evidence.admissions().wait_for(peer)?;
if self
.processor
.swarm
.is_peer_admitted(peer)
.map_err(Error::ConnectError)?
{
return Ok(true);
}
Ok(matches!(
tokio::time::timeout(DIAL_ADMISSION_TIMEOUT, waiter).await,
Ok(Ok(()))
))
}
}
async fn lookup_reaches(swarm: &Swarm, target: Did) -> bool {
let report = match swarm.lookup_successor(target).await {
Ok(SuccessorLookup::Routed(report)) => report,
Ok(SuccessorLookup::Local(_)) => return false,
Err(error) => {
tracing::debug!(peer = %target, %error, "bootstrap lookup could not be issued");
return false;
}
};
matches!(
tokio::time::timeout(LOOKUP_PROBE_TIMEOUT, report).await,
Ok(Ok(successor)) if successor == target
)
}