mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Initial sub-protocol implementation
This commit is contained in:
@@ -16,106 +16,109 @@ pub struct RPCProtocol;
|
||||
|
||||
impl UpgradeInfo for RPCProtocol {
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
type InfoIter = iter::Iter<Self::Info>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/eth/serenity/rpc/1.0.0")
|
||||
vec![b"/eth/serenity/rpc/hello/1/ssz",
|
||||
b"/eth/serenity/rpc/goodbye/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_roots/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_headers/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_bodies/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_chain_state/1/ssz"].into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RPCProtocol {
|
||||
fn default() -> Self {
|
||||
RPCProtocol
|
||||
}
|
||||
}
|
||||
|
||||
/// A monotonic counter for ordering `RPCRequest`s.
|
||||
#[derive(Debug, Clone, Copy, 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 PartialEq for RequestId {
|
||||
fn eq(&self, other: &RequestId) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
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_encode_via_from!(RequestId, u64);
|
||||
impl_decode_via_from!(RequestId, u64);
|
||||
|
||||
/// The RPC types which are sent/received in this protocol.
|
||||
/// The outbound RPC type as well as the return type used in the behaviour.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RPCEvent {
|
||||
Request {
|
||||
id: RequestId,
|
||||
method_id: u16,
|
||||
body: RPCRequest,
|
||||
},
|
||||
Response {
|
||||
id: RequestId,
|
||||
method_id: u16, //TODO: Remove and process decoding upstream
|
||||
result: RPCResponse,
|
||||
},
|
||||
Request ( RPCRequest ),
|
||||
Response ( RPCResponse ),
|
||||
}
|
||||
|
||||
// outbound protocol supports the same as the inbound.
|
||||
impl UpgradeInfo for RPCEvent {
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
type InfoIter = iter::Iter<Self::Info>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/eth/serenity/rpc/1.0.0")
|
||||
vec![b"/eth/serenity/rpc/hello/1/ssz",
|
||||
b"/eth/serenity/rpc/goodbye/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_roots/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_headers/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_bodies/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_chain_state/1/ssz"].into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
type FnDecodeRPCEvent = fn(Vec<u8>, ()) -> Result<RPCEvent, DecodeError>;
|
||||
/* Inbound upgrade */
|
||||
|
||||
// The inbound protocol reads the request, decodes it and returns the stream to the protocol
|
||||
// handler to respond to once ready.
|
||||
|
||||
type FnDecodeRPCEvent = fn(upgrade::Negotiated<TSocket>, Vec<u8>, ()) -> Result<(upgrade::Negotiated<TSocket>,RPCEvent), DecodeError>;
|
||||
|
||||
impl<TSocket> InboundUpgrade<TSocket> for RPCProtocol
|
||||
where
|
||||
TSocket: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Output = RPCEvent;
|
||||
type Socket = upgrade::Negotiated<TSocket>,
|
||||
type Output = (Self::Socket, RPCEvent),
|
||||
type Error = DecodeError;
|
||||
type Future = upgrade::ReadOneThen<upgrade::Negotiated<TSocket>, (), FnDecodeRPCEvent>;
|
||||
type Future = upgrade::ReadRespond<Self::Socket, (), FnDecodeRPCEvent>;
|
||||
|
||||
fn upgrade_inbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
|
||||
upgrade::read_one_then(socket, MAX_READ_SIZE, (), |packet, ()| Ok(decode(packet)?))
|
||||
fn upgrade_inbound(self, socket: upgrade::Negotiated<TSocket>, protocol: Self::Info) -> Self::Future {
|
||||
upgrade::read_respond(socket, MAX_READ_SIZE, (), |socket, packet, ()| Ok((socket, decode_request(packet, protocol)?)))
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper structed used to obtain SSZ serialization for RPC messages.
|
||||
|
||||
/* Outbound upgrade */
|
||||
|
||||
impl<TSocket> OutboundUpgrade<TSocket> for RPCEvent
|
||||
where
|
||||
TSocket: AsyncWrite,
|
||||
{
|
||||
type Output = ();
|
||||
type Error = io::Error;
|
||||
type Future = upgrade::WriteOne<upgrade::Negotiated<TSocket>>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: upgrade::Negotiated<TSocket>, protocol: Self::Info) -> Self::Future {
|
||||
let bytes = ssz_encode(&self);
|
||||
upgrade::request_response(socket,
|
||||
upgrade::write_one(socket, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
// This function can be extended to provide further logic for supporting various protocol versions/encoding
|
||||
fn decode_request(packet: Vec<u8>, protocol: &'static[u8]) {
|
||||
match protocol {
|
||||
b"/eth/serenity/rpc/hello/1/ssz" => { }
|
||||
b"/eth/serenity/rpc/goodbye/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_roots/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_headers/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_block_bodies/1/ssz",
|
||||
b"/eth/serenity/rpc/beacon_chain_state/1/ssz",
|
||||
_=> { // Other protocols are not supported.
|
||||
return Err(DecodeError::UnknownProtocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// A helper struct used to obtain SSZ serialization for RPC messages.
|
||||
#[derive(Encode, Decode, Default)]
|
||||
struct SszContainer {
|
||||
/// Note: the `is_request` field is not included in the spec.
|
||||
@@ -186,20 +189,6 @@ fn decode(packet: Vec<u8>) -> Result<RPCEvent, DecodeError> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSocket> OutboundUpgrade<TSocket> for RPCEvent
|
||||
where
|
||||
TSocket: AsyncWrite,
|
||||
{
|
||||
type Output = ();
|
||||
type Error = io::Error;
|
||||
type Future = upgrade::WriteOne<upgrade::Negotiated<TSocket>>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
|
||||
let bytes = ssz_encode(&self);
|
||||
upgrade::write_one(socket, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for RPCEvent {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
|
||||
Reference in New Issue
Block a user