lighthouse/beacon_node/eth2-libp2p/src/rpc/handler.rs

287 lines
9.8 KiB
Rust
Raw Normal View History

2019-07-09 05:44:23 +00:00
use super::protocol::{ProtocolId, RPCError, RPCProtocol, RPCRequest};
use super::RPCEvent;
use fnv::FnvHashMap;
use futures::prelude::*;
use libp2p::core::protocols_handler::{
2019-07-09 05:44:23 +00:00
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
};
2019-07-09 05:44:23 +00:00
use libp2p::core::upgrade::{self, InboundUpgrade, OutboundUpgrade, WriteOne};
use smallvec::SmallVec;
2019-07-09 05:44:23 +00:00
use std::time::{Duration, Instant};
use tokio_io::{AsyncRead, AsyncWrite};
/// The time (in seconds) before a substream that is awaiting a response times out.
pub const RESPONSE_TIMEOUT: u64 = 9;
/// Implementation of `ProtocolsHandler` for the RPC protocol.
2019-07-06 13:43:44 +00:00
pub struct RPCHandler<TSubstream> {
/// The upgrade for inbound substreams.
listen_protocol: SubstreamProtocol<RPCProtocol>,
/// If `Some`, something bad happened and we should shut down the handler with an error.
2019-07-09 05:44:23 +00:00
pending_error: Option<ProtocolsHandlerUpgrErr<RPCError>>,
/// Queue of events to produce in `poll()`.
2019-07-09 05:44:23 +00:00
events_out: SmallVec<[RPCEvent; 4]>,
/// Queue of outbound substreams to open.
2019-07-09 05:44:23 +00:00
dial_queue: SmallVec<[(usize, RPCRequest); 4]>,
/// Current number of concurrent outbound substreams being opened.
dial_negotiated: u32,
/// Map of current substreams awaiting a response to an RPC request.
2019-07-09 05:44:23 +00:00
waiting_substreams: FnvHashMap<usize, SubstreamState<TSubstream>>,
/// Sequential Id for waiting substreams.
current_substream_id: usize,
/// Maximum number of concurrent outbound substreams being opened. Value is never modified.
max_dial_negotiated: u32,
/// Value to return from `connection_keep_alive`.
keep_alive: KeepAlive,
/// After the given duration has elapsed, an inactive connection will shutdown.
inactive_timeout: Duration,
}
2019-07-06 13:43:44 +00:00
/// State of an outbound substream. Either waiting for a response, or in the process of sending.
pub enum SubstreamState<TSubstream> {
/// An outbound substream is waiting a response from the user.
WaitingResponse {
2019-07-09 05:44:23 +00:00
/// The negotiated substream.
substream: upgrade::Negotiated<TSubstream>,
/// The protocol that was negotiated.
negotiated_protocol: ProtocolId,
/// The time until we close the substream.
timeout: Instant,
},
2019-07-06 13:43:44 +00:00
/// A response has been sent and we are waiting for the stream to close.
2019-07-09 05:44:23 +00:00
PendingWrite(WriteOne<upgrade::Negotiated<TSubstream>, Vec<u8>>),
}
2019-07-09 05:44:23 +00:00
impl<TSubstream> RPCHandler<TSubstream> {
pub fn new(
listen_protocol: SubstreamProtocol<RPCProtocol>,
2019-07-09 05:44:23 +00:00
inactive_timeout: Duration,
) -> Self {
RPCHandler {
listen_protocol,
pending_error: None,
events_out: SmallVec::new(),
dial_queue: SmallVec::new(),
dial_negotiated: 0,
waiting_substreams: FnvHashMap::default(),
2019-07-09 05:44:23 +00:00
current_substream_id: 0,
max_dial_negotiated: 8,
keep_alive: KeepAlive::Yes,
inactive_timeout,
}
}
/// Returns the number of pending requests.
pub fn pending_requests(&self) -> u32 {
self.dial_negotiated + self.dial_queue.len() as u32
}
/// Returns a reference to the listen protocol configuration.
///
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
/// > substreams, not the ones already being negotiated.
2019-07-09 05:44:23 +00:00
pub fn listen_protocol_ref(&self) -> &SubstreamProtocol<RPCProtocol> {
&self.listen_protocol
}
/// Returns a mutable reference to the listen protocol configuration.
///
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
/// > substreams, not the ones already being negotiated.
2019-07-09 05:44:23 +00:00
pub fn listen_protocol_mut(&mut self) -> &mut SubstreamProtocol<RPCProtocol> {
&mut self.listen_protocol
}
/// Opens an outbound substream with `upgrade`.
#[inline]
2019-07-09 05:44:23 +00:00
pub fn send_request(&mut self, request_id: usize, upgrade: RPCRequest) {
self.keep_alive = KeepAlive::Yes;
2019-07-06 13:43:44 +00:00
self.dial_queue.push((request_id, upgrade));
}
}
2019-07-09 05:44:23 +00:00
impl<TSubstream> Default for RPCHandler<TSubstream> {
fn default() -> Self {
2019-07-06 13:43:44 +00:00
RPCHandler::new(SubstreamProtocol::new(RPCProtocol), Duration::from_secs(30))
}
}
2019-07-09 05:44:23 +00:00
impl<TSubstream> ProtocolsHandler for RPCHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
2019-07-06 13:43:44 +00:00
type InEvent = RPCEvent;
type OutEvent = RPCEvent;
2019-07-09 05:44:23 +00:00
type Error = ProtocolsHandlerUpgrErr<RPCError>;
type Substream = TSubstream;
type InboundProtocol = RPCProtocol;
type OutboundProtocol = RPCRequest;
2019-07-09 05:44:23 +00:00
type OutboundOpenInfo = usize; // request_id
#[inline]
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
self.listen_protocol.clone()
}
#[inline]
fn inject_fully_negotiated_inbound(
&mut self,
2019-07-09 05:44:23 +00:00
out: <RPCProtocol as InboundUpgrade<TSubstream>>::Output,
) {
2019-07-09 05:44:23 +00:00
let (substream, req, negotiated_protocol) = out;
// drop the stream and return a 0 id for goodbye "requests"
if let r @ RPCRequest::Goodbye(_) = req {
self.events_out.push(RPCEvent::Request(0, r));
return;
}
// New inbound request. Store the stream and tag the output.
2019-07-09 05:44:23 +00:00
let awaiting_stream = SubstreamState::WaitingResponse {
substream,
negotiated_protocol,
timeout: Instant::now() + Duration::from_secs(RESPONSE_TIMEOUT),
};
self.waiting_substreams
.insert(self.current_substream_id, awaiting_stream);
self.events_out
.push(RPCEvent::Request(self.current_substream_id, req));
self.current_substream_id += 1;
}
#[inline]
fn inject_fully_negotiated_outbound(
&mut self,
2019-07-09 05:44:23 +00:00
out: <RPCRequest as OutboundUpgrade<TSubstream>>::Output,
request_id: Self::OutboundOpenInfo,
) {
self.dial_negotiated -= 1;
2019-07-09 05:44:23 +00:00
if self.dial_negotiated == 0
&& self.dial_queue.is_empty()
&& self.waiting_substreams.is_empty()
{
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
2019-07-09 05:44:23 +00:00
} else {
2019-07-06 13:43:44 +00:00
self.keep_alive = KeepAlive::Yes;
}
2019-07-06 13:43:44 +00:00
self.events_out.push(RPCEvent::Response(request_id, out));
}
2019-07-06 13:43:44 +00:00
// Note: If the substream has closed due to inactivity, or the substream is in the
// wrong state a response will fail silently.
#[inline]
2019-07-06 13:43:44 +00:00
fn inject_event(&mut self, rpc_event: Self::InEvent) {
match rpc_event {
RPCEvent::Request(rpc_id, req) => self.send_request(rpc_id, req),
RPCEvent::Response(rpc_id, res) => {
// check if the stream matching the response still exists
2019-07-09 05:44:23 +00:00
if let Some(waiting_stream) = self.waiting_substreams.get_mut(&rpc_id) {
// only send one response per stream. This must be in the waiting state.
if let SubstreamState::WaitingResponse {
substream,
negotiated_protocol,
..
} = *waiting_stream
{
*waiting_stream = SubstreamState::PendingWrite(upgrade::write_one(
substream,
res.encode(negotiated_protocol)
.expect("Response should always be encodeable"),
));
2019-07-06 13:43:44 +00:00
}
}
}
}
}
#[inline]
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
error: ProtocolsHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error,
>,
) {
2019-07-09 05:44:23 +00:00
dbg!(error);
if self.pending_error.is_none() {
self.pending_error = Some(error);
}
}
#[inline]
fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive
}
fn poll(
&mut self,
) -> Poll<
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
Self::Error,
> {
if let Some(err) = self.pending_error.take() {
return Err(err);
}
2019-07-06 13:43:44 +00:00
// prioritise sending responses for waiting substreams
self.waiting_substreams.retain(|_k, mut waiting_stream| {
2019-07-09 05:44:23 +00:00
match waiting_stream {
2019-07-06 13:43:44 +00:00
SubstreamState::PendingWrite(write_one) => {
2019-07-09 05:44:23 +00:00
match write_one.poll() {
2019-07-06 13:43:44 +00:00
Ok(Async::Ready(_socket)) => false,
2019-07-09 05:44:23 +00:00
Ok(Async::NotReady) => true,
Err(_e) => {
2019-07-06 13:43:44 +00:00
//TODO: Add logging
// throw away streams that error
2019-07-09 05:44:23 +00:00
false
}
}
}
SubstreamState::WaitingResponse { timeout, .. } => {
if Instant::now() > *timeout {
false
} else {
true
2019-07-06 13:43:44 +00:00
}
}
}
});
if !self.events_out.is_empty() {
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(
self.events_out.remove(0),
)));
} else {
self.events_out.shrink_to_fit();
}
2019-07-06 13:43:44 +00:00
// establish outbound substreams
if !self.dial_queue.is_empty() {
if self.dial_negotiated < self.max_dial_negotiated {
self.dial_negotiated += 1;
2019-07-06 13:43:44 +00:00
let (request_id, req) = self.dial_queue.remove(0);
return Ok(Async::Ready(
ProtocolsHandlerEvent::OutboundSubstreamRequest {
2019-07-06 13:43:44 +00:00
protocol: SubstreamProtocol::new(req),
info: request_id,
},
));
}
} else {
self.dial_queue.shrink_to_fit();
}
Ok(Async::NotReady)
}
}