mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 20:22:02 +00:00
Merge branch 'master' into paul-sync
This commit is contained in:
188
beacon_node/eth2-libp2p/src/rpc/methods.rs
Normal file
188
beacon_node/eth2-libp2p/src/rpc/methods.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
/// Available RPC methods types and ids.
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot};
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Available Serenity Libp2p RPC methods
|
||||
pub enum RPCMethod {
|
||||
/// Initialise handshake between connecting peers.
|
||||
Hello,
|
||||
/// Terminate a connection providing a reason.
|
||||
Goodbye,
|
||||
/// Requests a number of beacon block roots.
|
||||
BeaconBlockRoots,
|
||||
/// Requests a number of beacon block headers.
|
||||
BeaconBlockHeaders,
|
||||
/// Requests a number of beacon block bodies.
|
||||
BeaconBlockBodies,
|
||||
/// Requests values for a merkle proof for the current blocks state root.
|
||||
BeaconChainState, // Note: experimental, not complete.
|
||||
/// Unknown method received.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl From<u16> for RPCMethod {
|
||||
fn from(method_id: u16) -> Self {
|
||||
match method_id {
|
||||
0 => RPCMethod::Hello,
|
||||
1 => RPCMethod::Goodbye,
|
||||
10 => RPCMethod::BeaconBlockRoots,
|
||||
11 => RPCMethod::BeaconBlockHeaders,
|
||||
12 => RPCMethod::BeaconBlockBodies,
|
||||
13 => RPCMethod::BeaconChainState,
|
||||
|
||||
_ => RPCMethod::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u16> for RPCMethod {
|
||||
fn into(self) -> u16 {
|
||||
match self {
|
||||
RPCMethod::Hello => 0,
|
||||
RPCMethod::Goodbye => 1,
|
||||
RPCMethod::BeaconBlockRoots => 10,
|
||||
RPCMethod::BeaconBlockHeaders => 11,
|
||||
RPCMethod::BeaconBlockBodies => 12,
|
||||
RPCMethod::BeaconChainState => 13,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RPCRequest {
|
||||
Hello(HelloMessage),
|
||||
Goodbye(u64),
|
||||
BeaconBlockRoots(BeaconBlockRootsRequest),
|
||||
BeaconBlockHeaders(BeaconBlockHeadersRequest),
|
||||
BeaconBlockBodies(BeaconBlockBodiesRequest),
|
||||
BeaconChainState(BeaconChainStateRequest),
|
||||
}
|
||||
|
||||
impl RPCRequest {
|
||||
pub fn method_id(&self) -> u16 {
|
||||
let method = match self {
|
||||
RPCRequest::Hello(_) => RPCMethod::Hello,
|
||||
RPCRequest::Goodbye(_) => RPCMethod::Goodbye,
|
||||
RPCRequest::BeaconBlockRoots(_) => RPCMethod::BeaconBlockRoots,
|
||||
RPCRequest::BeaconBlockHeaders(_) => RPCMethod::BeaconBlockHeaders,
|
||||
RPCRequest::BeaconBlockBodies(_) => RPCMethod::BeaconBlockBodies,
|
||||
RPCRequest::BeaconChainState(_) => RPCMethod::BeaconChainState,
|
||||
};
|
||||
method.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RPCResponse {
|
||||
Hello(HelloMessage),
|
||||
BeaconBlockRoots(BeaconBlockRootsResponse),
|
||||
BeaconBlockHeaders(BeaconBlockHeadersResponse),
|
||||
BeaconBlockBodies(BeaconBlockBodiesResponse),
|
||||
BeaconChainState(BeaconChainStateResponse),
|
||||
}
|
||||
|
||||
impl RPCResponse {
|
||||
pub fn method_id(&self) -> u16 {
|
||||
let method = match self {
|
||||
RPCResponse::Hello(_) => RPCMethod::Hello,
|
||||
RPCResponse::BeaconBlockRoots(_) => RPCMethod::BeaconBlockRoots,
|
||||
RPCResponse::BeaconBlockHeaders(_) => RPCMethod::BeaconBlockHeaders,
|
||||
RPCResponse::BeaconBlockBodies(_) => RPCMethod::BeaconBlockBodies,
|
||||
RPCResponse::BeaconChainState(_) => RPCMethod::BeaconChainState,
|
||||
};
|
||||
method.into()
|
||||
}
|
||||
}
|
||||
|
||||
/* Request/Response data structures for RPC methods */
|
||||
|
||||
/// The HELLO request/response handshake message.
|
||||
#[derive(Encode, Decode, Clone, Debug)]
|
||||
pub struct HelloMessage {
|
||||
/// The network ID of the peer.
|
||||
pub network_id: u8,
|
||||
/// The peers last finalized root.
|
||||
pub latest_finalized_root: Hash256,
|
||||
/// The peers last finalized epoch.
|
||||
pub latest_finalized_epoch: Epoch,
|
||||
/// The peers last block root.
|
||||
pub best_root: Hash256,
|
||||
/// The peers last slot.
|
||||
pub best_slot: Slot,
|
||||
}
|
||||
|
||||
/// Request a number of beacon block roots from a peer.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockRootsRequest {
|
||||
/// The starting slot of the requested blocks.
|
||||
pub start_slot: Slot,
|
||||
/// The number of blocks from the start slot.
|
||||
pub count: u64, // this must be less than 32768. //TODO: Enforce this in the lower layers
|
||||
}
|
||||
|
||||
/// Response containing a number of beacon block roots from a peer.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockRootsResponse {
|
||||
/// List of requested blocks and associated slots.
|
||||
pub roots: Vec<BlockRootSlot>,
|
||||
}
|
||||
|
||||
/// Contains a block root and associated slot.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BlockRootSlot {
|
||||
/// The block root.
|
||||
pub block_root: Hash256,
|
||||
/// The block slot.
|
||||
pub slot: Slot,
|
||||
}
|
||||
|
||||
/// Request a number of beacon block headers from a peer.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockHeadersRequest {
|
||||
/// The starting header hash of the requested headers.
|
||||
pub start_root: Hash256,
|
||||
/// The starting slot of the requested headers.
|
||||
pub start_slot: Slot,
|
||||
/// The maximum number of headers than can be returned.
|
||||
pub max_headers: u64,
|
||||
/// The maximum number of slots to skip between blocks.
|
||||
pub skip_slots: u64,
|
||||
}
|
||||
|
||||
/// Response containing requested block headers.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockHeadersResponse {
|
||||
/// The list of requested beacon block headers.
|
||||
pub headers: Vec<BeaconBlockHeader>,
|
||||
}
|
||||
|
||||
/// Request a number of beacon block bodies from a peer.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockBodiesRequest {
|
||||
/// The list of beacon block bodies being requested.
|
||||
pub block_roots: Hash256,
|
||||
}
|
||||
|
||||
/// Response containing the list of requested beacon block bodies.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconBlockBodiesResponse {
|
||||
/// The list of beacon block bodies being requested.
|
||||
pub block_bodies: Vec<BeaconBlockBody>,
|
||||
}
|
||||
|
||||
/// Request values for tree hashes which yield a blocks `state_root`.
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconChainStateRequest {
|
||||
/// The tree hashes that a value is requested for.
|
||||
pub hashes: Vec<Hash256>,
|
||||
}
|
||||
|
||||
/// Request values for tree hashes which yield a blocks `state_root`.
|
||||
// Note: TBD
|
||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||
pub struct BeaconChainStateResponse {
|
||||
/// The values corresponding the to the requested tree hashes.
|
||||
pub values: bool, //TBD - stubbed with encodeable bool
|
||||
}
|
||||
138
beacon_node/eth2-libp2p/src/rpc/mod.rs
Normal file
138
beacon_node/eth2-libp2p/src/rpc/mod.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
/// RPC Protocol over libp2p.
|
||||
///
|
||||
/// This is purpose built for Ethereum 2.0 serenity and the protocol listens on
|
||||
/// `/eth/serenity/rpc/1.0.0`
|
||||
pub mod methods;
|
||||
mod protocol;
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::protocols_handler::{OneShotHandler, ProtocolsHandler};
|
||||
use libp2p::core::swarm::{
|
||||
ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
|
||||
};
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
pub use methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse};
|
||||
pub use protocol::{RPCEvent, RPCProtocol};
|
||||
use slog::o;
|
||||
use std::marker::PhantomData;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// The network behaviour handles RPC requests/responses as specified in the Eth 2.0 phase 0
|
||||
/// specification.
|
||||
|
||||
pub struct Rpc<TSubstream> {
|
||||
/// Queue of events to processed.
|
||||
events: Vec<NetworkBehaviourAction<RPCEvent, RPCMessage>>,
|
||||
/// Pins the generic substream.
|
||||
marker: PhantomData<TSubstream>,
|
||||
/// Slog logger for RPC behaviour.
|
||||
log: slog::Logger,
|
||||
}
|
||||
|
||||
impl<TSubstream> Rpc<TSubstream> {
|
||||
pub fn new(log: &slog::Logger) -> Self {
|
||||
let log = log.new(o!("Service" => "Libp2p-RPC"));
|
||||
Rpc {
|
||||
events: Vec::new(),
|
||||
marker: PhantomData,
|
||||
log,
|
||||
}
|
||||
}
|
||||
|
||||
/// Submits and RPC request.
|
||||
pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) {
|
||||
self.events.push(NetworkBehaviourAction::SendEvent {
|
||||
peer_id,
|
||||
event: rpc_event,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviour for Rpc<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type ProtocolsHandler = OneShotHandler<TSubstream, RPCProtocol, RPCEvent, OneShotEvent>;
|
||||
type OutEvent = RPCMessage;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, peer_id: PeerId, connected_point: ConnectedPoint) {
|
||||
// if initialised the connection, report this upwards to send the HELLO request
|
||||
if let ConnectedPoint::Dialer { address: _ } = connected_point {
|
||||
self.events.push(NetworkBehaviourAction::GenerateEvent(
|
||||
RPCMessage::PeerDialed(peer_id),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {}
|
||||
|
||||
fn inject_node_event(
|
||||
&mut self,
|
||||
source: PeerId,
|
||||
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||
) {
|
||||
// ignore successful send events
|
||||
let event = match event {
|
||||
OneShotEvent::Rx(event) => event,
|
||||
OneShotEvent::Sent => return,
|
||||
};
|
||||
|
||||
// send the event to the user
|
||||
self.events
|
||||
.push(NetworkBehaviourAction::GenerateEvent(RPCMessage::RPC(
|
||||
source, event,
|
||||
)));
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
_: &mut PollParameters<'_>,
|
||||
) -> Async<
|
||||
NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
> {
|
||||
if !self.events.is_empty() {
|
||||
return Async::Ready(self.events.remove(0));
|
||||
}
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
||||
|
||||
/// Messages sent to the user from the RPC protocol.
|
||||
pub enum RPCMessage {
|
||||
RPC(PeerId, RPCEvent),
|
||||
PeerDialed(PeerId),
|
||||
}
|
||||
|
||||
/// Transmission between the `OneShotHandler` and the `RPCEvent`.
|
||||
#[derive(Debug)]
|
||||
pub enum OneShotEvent {
|
||||
/// We received an RPC from a remote.
|
||||
Rx(RPCEvent),
|
||||
/// We successfully sent an RPC request.
|
||||
Sent,
|
||||
}
|
||||
|
||||
impl From<RPCEvent> for OneShotEvent {
|
||||
#[inline]
|
||||
fn from(rpc: RPCEvent) -> OneShotEvent {
|
||||
OneShotEvent::Rx(rpc)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<()> for OneShotEvent {
|
||||
#[inline]
|
||||
fn from(_: ()) -> OneShotEvent {
|
||||
OneShotEvent::Sent
|
||||
}
|
||||
}
|
||||
181
beacon_node/eth2-libp2p/src/rpc/protocol.rs
Normal file
181
beacon_node/eth2-libp2p/src/rpc/protocol.rs
Normal file
@@ -0,0 +1,181 @@
|
||||
use super::methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse};
|
||||
use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use ssz::{ssz_encode, Decodable, Encodable, SszStream};
|
||||
use std::io;
|
||||
use std::iter;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// The maximum bytes that can be sent across the RPC.
|
||||
const MAX_READ_SIZE: usize = 2048;
|
||||
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
/// The RPC types which are sent/received in this protocol.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RPCEvent {
|
||||
Request {
|
||||
id: u64,
|
||||
method_id: u16,
|
||||
body: RPCRequest,
|
||||
},
|
||||
Response {
|
||||
id: u64,
|
||||
method_id: u16, //TODO: Remove and process decoding upstream
|
||||
result: RPCResponse,
|
||||
},
|
||||
}
|
||||
|
||||
impl UpgradeInfo for RPCEvent {
|
||||
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,
|
||||
{
|
||||
type Output = RPCEvent;
|
||||
type Error = DecodeError;
|
||||
type Future =
|
||||
upgrade::ReadOneThen<TSocket, (), fn(Vec<u8>, ()) -> Result<RPCEvent, DecodeError>>;
|
||||
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
upgrade::read_one_then(socket, MAX_READ_SIZE, (), |packet, ()| Ok(decode(packet)?))
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(packet: Vec<u8>) -> Result<RPCEvent, DecodeError> {
|
||||
// decode the header of the rpc
|
||||
// request/response
|
||||
let (request, index) = bool::ssz_decode(&packet, 0)?;
|
||||
let (id, index) = u64::ssz_decode(&packet, index)?;
|
||||
let (method_id, index) = u16::ssz_decode(&packet, index)?;
|
||||
|
||||
if request {
|
||||
let body = match RPCMethod::from(method_id) {
|
||||
RPCMethod::Hello => {
|
||||
let (hello_body, _index) = HelloMessage::ssz_decode(&packet, index)?;
|
||||
RPCRequest::Hello(hello_body)
|
||||
}
|
||||
RPCMethod::Unknown | _ => return Err(DecodeError::UnknownRPCMethod),
|
||||
};
|
||||
|
||||
Ok(RPCEvent::Request {
|
||||
id,
|
||||
method_id,
|
||||
body,
|
||||
})
|
||||
}
|
||||
// we have received a response
|
||||
else {
|
||||
let result = match RPCMethod::from(method_id) {
|
||||
RPCMethod::Hello => {
|
||||
let (body, _index) = HelloMessage::ssz_decode(&packet, index)?;
|
||||
RPCResponse::Hello(body)
|
||||
}
|
||||
RPCMethod::Unknown | _ => return Err(DecodeError::UnknownRPCMethod),
|
||||
};
|
||||
Ok(RPCEvent::Response {
|
||||
id,
|
||||
method_id,
|
||||
result,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSocket> OutboundUpgrade<TSocket> for RPCEvent
|
||||
where
|
||||
TSocket: AsyncWrite,
|
||||
{
|
||||
type Output = ();
|
||||
type Error = io::Error;
|
||||
type Future = upgrade::WriteOne<TSocket>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
let bytes = ssz_encode(&self);
|
||||
upgrade::write_one(socket, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for RPCEvent {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
match self {
|
||||
RPCEvent::Request {
|
||||
id,
|
||||
method_id,
|
||||
body,
|
||||
} => {
|
||||
s.append(&true);
|
||||
s.append(id);
|
||||
s.append(method_id);
|
||||
match body {
|
||||
RPCRequest::Hello(body) => {
|
||||
s.append(body);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
RPCEvent::Response {
|
||||
id,
|
||||
method_id,
|
||||
result,
|
||||
} => {
|
||||
s.append(&false);
|
||||
s.append(id);
|
||||
s.append(method_id);
|
||||
match result {
|
||||
RPCResponse::Hello(response) => {
|
||||
s.append(response);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user