2019-03-21 00:02:52 +00:00
|
|
|
use super::methods::*;
|
2019-03-17 03:38:20 +00:00
|
|
|
use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
2019-03-25 05:48:44 +00:00
|
|
|
use ssz::{ssz_encode, Decodable, DecodeError as SSZDecodeError, Encodable, SszStream};
|
|
|
|
use std::hash::{Hash, Hasher};
|
2019-03-14 14:50:59 +00:00
|
|
|
use std::io;
|
|
|
|
use std::iter;
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
|
|
/// The maximum bytes that can be sent across the RPC.
|
2019-03-20 23:43:21 +00:00
|
|
|
const MAX_READ_SIZE: usize = 4_194_304; // 4M
|
2019-03-14 14:50:59 +00:00
|
|
|
|
|
|
|
/// Implementation of the `ConnectionUpgrade` for the rpc protocol.
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RPCProtocol;
|
|
|
|
|
|
|
|
impl UpgradeInfo for RPCProtocol {
|
|
|
|
type Info = &'static [u8];
|
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
|
|
iter::once(b"/eth/serenity/rpc/1.0.0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RPCProtocol {
|
|
|
|
fn default() -> Self {
|
|
|
|
RPCProtocol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 05:48:44 +00:00
|
|
|
/// A monotonic counter for ordering `RPCRequest`s.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
|
|
|
pub struct RequestId(u64);
|
|
|
|
|
|
|
|
impl RequestId {
|
|
|
|
/// Increment the request id.
|
|
|
|
pub fn increment(&mut self) {
|
|
|
|
self.0 += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the previous id.
|
|
|
|
pub fn previous(&self) -> Self {
|
|
|
|
Self(self.0 - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for RequestId {}
|
|
|
|
|
|
|
|
impl Hash for RequestId {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.0.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for RequestId {
|
|
|
|
fn from(x: u64) -> RequestId {
|
|
|
|
RequestId(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<u64> for RequestId {
|
|
|
|
fn into(self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for RequestId {
|
|
|
|
fn ssz_append(&self, s: &mut SszStream) {
|
|
|
|
self.0.ssz_append(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for RequestId {
|
|
|
|
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), SSZDecodeError> {
|
|
|
|
let (id, index) = u64::ssz_decode(bytes, index)?;
|
|
|
|
Ok((Self::from(id), index))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:50:59 +00:00
|
|
|
/// The RPC types which are sent/received in this protocol.
|
|
|
|
#[derive(Debug, Clone)]
|
2019-03-17 12:14:28 +00:00
|
|
|
pub enum RPCEvent {
|
2019-03-14 14:50:59 +00:00
|
|
|
Request {
|
2019-03-25 05:48:44 +00:00
|
|
|
id: RequestId,
|
2019-03-14 14:50:59 +00:00
|
|
|
method_id: u16,
|
|
|
|
body: RPCRequest,
|
|
|
|
},
|
|
|
|
Response {
|
2019-03-25 05:48:44 +00:00
|
|
|
id: RequestId,
|
2019-03-14 14:50:59 +00:00
|
|
|
method_id: u16, //TODO: Remove and process decoding upstream
|
|
|
|
result: RPCResponse,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
impl UpgradeInfo for RPCEvent {
|
2019-03-14 14:50:59 +00:00
|
|
|
type Info = &'static [u8];
|
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
|
|
iter::once(b"/eth/serenity/rpc/1.0.0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TSocket> InboundUpgrade<TSocket> for RPCProtocol
|
|
|
|
where
|
|
|
|
TSocket: AsyncRead + AsyncWrite,
|
|
|
|
{
|
2019-03-17 12:14:28 +00:00
|
|
|
type Output = RPCEvent;
|
2019-03-14 14:50:59 +00:00
|
|
|
type Error = DecodeError;
|
2019-03-26 04:01:05 +00:00
|
|
|
type Future = upgrade::ReadOneThen<
|
|
|
|
upgrade::Negotiated<TSocket>,
|
|
|
|
(),
|
|
|
|
fn(Vec<u8>, ()) -> Result<RPCEvent, DecodeError>,
|
|
|
|
>;
|
2019-03-14 14:50:59 +00:00
|
|
|
|
2019-03-26 04:01:05 +00:00
|
|
|
fn upgrade_inbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
|
2019-03-14 14:50:59 +00:00
|
|
|
upgrade::read_one_then(socket, MAX_READ_SIZE, (), |packet, ()| Ok(decode(packet)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
fn decode(packet: Vec<u8>) -> Result<RPCEvent, DecodeError> {
|
2019-03-14 14:50:59 +00:00
|
|
|
// decode the header of the rpc
|
|
|
|
// request/response
|
|
|
|
let (request, index) = bool::ssz_decode(&packet, 0)?;
|
2019-03-25 05:48:44 +00:00
|
|
|
let (id, index) = RequestId::ssz_decode(&packet, index)?;
|
2019-03-14 14:50:59 +00:00
|
|
|
let (method_id, index) = u16::ssz_decode(&packet, index)?;
|
|
|
|
|
|
|
|
if request {
|
|
|
|
let body = match RPCMethod::from(method_id) {
|
2019-03-17 03:38:20 +00:00
|
|
|
RPCMethod::Hello => {
|
2019-03-17 10:49:56 +00:00
|
|
|
let (hello_body, _index) = HelloMessage::ssz_decode(&packet, index)?;
|
2019-03-17 03:38:20 +00:00
|
|
|
RPCRequest::Hello(hello_body)
|
|
|
|
}
|
2019-03-20 23:53:29 +00:00
|
|
|
RPCMethod::Goodbye => {
|
2019-03-24 06:51:32 +00:00
|
|
|
let (goodbye_reason, _index) = GoodbyeReason::ssz_decode(&packet, index)?;
|
|
|
|
RPCRequest::Goodbye(goodbye_reason)
|
2019-03-20 23:53:29 +00:00
|
|
|
}
|
2019-03-21 00:02:52 +00:00
|
|
|
RPCMethod::BeaconBlockRoots => {
|
|
|
|
let (block_roots_request, _index) =
|
|
|
|
BeaconBlockRootsRequest::ssz_decode(&packet, index)?;
|
|
|
|
RPCRequest::BeaconBlockRoots(block_roots_request)
|
|
|
|
}
|
2019-03-21 00:05:26 +00:00
|
|
|
RPCMethod::BeaconBlockHeaders => {
|
|
|
|
let (block_headers_request, _index) =
|
|
|
|
BeaconBlockHeadersRequest::ssz_decode(&packet, index)?;
|
|
|
|
RPCRequest::BeaconBlockHeaders(block_headers_request)
|
|
|
|
}
|
2019-03-21 00:16:09 +00:00
|
|
|
RPCMethod::BeaconBlockBodies => {
|
|
|
|
let (block_bodies_request, _index) =
|
|
|
|
BeaconBlockBodiesRequest::ssz_decode(&packet, index)?;
|
|
|
|
RPCRequest::BeaconBlockBodies(block_bodies_request)
|
|
|
|
}
|
2019-03-21 00:18:47 +00:00
|
|
|
RPCMethod::BeaconChainState => {
|
|
|
|
let (chain_state_request, _index) =
|
|
|
|
BeaconChainStateRequest::ssz_decode(&packet, index)?;
|
|
|
|
RPCRequest::BeaconChainState(chain_state_request)
|
|
|
|
}
|
|
|
|
RPCMethod::Unknown => return Err(DecodeError::UnknownRPCMethod),
|
2019-03-14 14:50:59 +00:00
|
|
|
};
|
|
|
|
|
2019-03-19 12:20:39 +00:00
|
|
|
Ok(RPCEvent::Request {
|
2019-03-14 14:50:59 +00:00
|
|
|
id,
|
|
|
|
method_id,
|
|
|
|
body,
|
2019-03-19 12:20:39 +00:00
|
|
|
})
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
|
|
|
// we have received a response
|
|
|
|
else {
|
|
|
|
let result = match RPCMethod::from(method_id) {
|
|
|
|
RPCMethod::Hello => {
|
2019-03-17 10:49:56 +00:00
|
|
|
let (body, _index) = HelloMessage::ssz_decode(&packet, index)?;
|
2019-03-17 03:38:20 +00:00
|
|
|
RPCResponse::Hello(body)
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
2019-03-21 00:02:52 +00:00
|
|
|
RPCMethod::Goodbye => unreachable!("Should never receive a goodbye response"),
|
|
|
|
RPCMethod::BeaconBlockRoots => {
|
|
|
|
let (body, _index) = BeaconBlockRootsResponse::ssz_decode(&packet, index)?;
|
|
|
|
RPCResponse::BeaconBlockRoots(body)
|
|
|
|
}
|
2019-03-21 00:05:26 +00:00
|
|
|
RPCMethod::BeaconBlockHeaders => {
|
|
|
|
let (body, _index) = BeaconBlockHeadersResponse::ssz_decode(&packet, index)?;
|
|
|
|
RPCResponse::BeaconBlockHeaders(body)
|
|
|
|
}
|
2019-03-21 00:16:09 +00:00
|
|
|
RPCMethod::BeaconBlockBodies => {
|
|
|
|
let (body, _index) = BeaconBlockBodiesResponse::ssz_decode(&packet, index)?;
|
|
|
|
RPCResponse::BeaconBlockBodies(body)
|
|
|
|
}
|
2019-03-21 00:18:47 +00:00
|
|
|
RPCMethod::BeaconChainState => {
|
|
|
|
let (body, _index) = BeaconChainStateResponse::ssz_decode(&packet, index)?;
|
|
|
|
RPCResponse::BeaconChainState(body)
|
|
|
|
}
|
|
|
|
RPCMethod::Unknown => return Err(DecodeError::UnknownRPCMethod),
|
2019-03-14 14:50:59 +00:00
|
|
|
};
|
2019-03-19 12:20:39 +00:00
|
|
|
Ok(RPCEvent::Response {
|
2019-03-14 14:50:59 +00:00
|
|
|
id,
|
|
|
|
method_id,
|
|
|
|
result,
|
2019-03-19 12:20:39 +00:00
|
|
|
})
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
impl<TSocket> OutboundUpgrade<TSocket> for RPCEvent
|
2019-03-14 14:50:59 +00:00
|
|
|
where
|
|
|
|
TSocket: AsyncWrite,
|
|
|
|
{
|
|
|
|
type Output = ();
|
|
|
|
type Error = io::Error;
|
2019-03-26 04:01:05 +00:00
|
|
|
type Future = upgrade::WriteOne<upgrade::Negotiated<TSocket>>;
|
2019-03-14 14:50:59 +00:00
|
|
|
|
|
|
|
#[inline]
|
2019-03-26 04:01:05 +00:00
|
|
|
fn upgrade_outbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
|
2019-03-14 14:50:59 +00:00
|
|
|
let bytes = ssz_encode(&self);
|
|
|
|
upgrade::write_one(socket, bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
impl Encodable for RPCEvent {
|
2019-03-14 14:50:59 +00:00
|
|
|
fn ssz_append(&self, s: &mut SszStream) {
|
|
|
|
match self {
|
2019-03-17 12:14:28 +00:00
|
|
|
RPCEvent::Request {
|
2019-03-14 14:50:59 +00:00
|
|
|
id,
|
|
|
|
method_id,
|
|
|
|
body,
|
|
|
|
} => {
|
|
|
|
s.append(&true);
|
|
|
|
s.append(id);
|
|
|
|
s.append(method_id);
|
|
|
|
match body {
|
2019-03-19 23:54:19 +00:00
|
|
|
RPCRequest::Hello(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
2019-03-21 00:25:22 +00:00
|
|
|
RPCRequest::Goodbye(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
|
|
|
RPCRequest::BeaconBlockRoots(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
|
|
|
RPCRequest::BeaconBlockHeaders(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
|
|
|
RPCRequest::BeaconBlockBodies(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
|
|
|
RPCRequest::BeaconChainState(body) => {
|
|
|
|
s.append(body);
|
|
|
|
}
|
2019-03-19 23:54:19 +00:00
|
|
|
}
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
2019-03-17 12:14:28 +00:00
|
|
|
RPCEvent::Response {
|
2019-03-14 14:50:59 +00:00
|
|
|
id,
|
|
|
|
method_id,
|
|
|
|
result,
|
|
|
|
} => {
|
|
|
|
s.append(&false);
|
|
|
|
s.append(id);
|
|
|
|
s.append(method_id);
|
|
|
|
match result {
|
2019-03-17 03:38:20 +00:00
|
|
|
RPCResponse::Hello(response) => {
|
2019-03-14 14:50:59 +00:00
|
|
|
s.append(response);
|
|
|
|
}
|
2019-03-21 00:25:22 +00:00
|
|
|
RPCResponse::BeaconBlockRoots(response) => {
|
|
|
|
s.append(response);
|
|
|
|
}
|
|
|
|
RPCResponse::BeaconBlockHeaders(response) => {
|
|
|
|
s.append(response);
|
|
|
|
}
|
|
|
|
RPCResponse::BeaconBlockBodies(response) => {
|
|
|
|
s.append(response);
|
|
|
|
}
|
|
|
|
RPCResponse::BeaconChainState(response) => {
|
|
|
|
s.append(response);
|
|
|
|
}
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:48:09 +00:00
|
|
|
#[derive(Debug)]
|
2019-03-14 14:50:59 +00:00
|
|
|
pub enum DecodeError {
|
|
|
|
ReadError(upgrade::ReadOneError),
|
|
|
|
SSZDecodeError(ssz::DecodeError),
|
|
|
|
UnknownRPCMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<upgrade::ReadOneError> for DecodeError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: upgrade::ReadOneError) -> Self {
|
|
|
|
DecodeError::ReadError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ssz::DecodeError> for DecodeError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: ssz::DecodeError) -> Self {
|
|
|
|
DecodeError::SSZDecodeError(err)
|
|
|
|
}
|
|
|
|
}
|