mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-01 00:41:20 +00:00
Implement Goodbye and BeaconState msg handlers
This commit is contained in:
parent
f918f42b28
commit
796b68dc04
@ -1,3 +1,4 @@
|
|||||||
|
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||||
/// Available RPC methods types and ids.
|
/// Available RPC methods types and ids.
|
||||||
use ssz_derive::{Decode, Encode};
|
use ssz_derive::{Decode, Encode};
|
||||||
use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot};
|
use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot};
|
||||||
@ -53,7 +54,7 @@ impl Into<u16> for RPCMethod {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum RPCRequest {
|
pub enum RPCRequest {
|
||||||
Hello(HelloMessage),
|
Hello(HelloMessage),
|
||||||
Goodbye(u64),
|
Goodbye(GoodbyeReason),
|
||||||
BeaconBlockRoots(BeaconBlockRootsRequest),
|
BeaconBlockRoots(BeaconBlockRootsRequest),
|
||||||
BeaconBlockHeaders(BeaconBlockHeadersRequest),
|
BeaconBlockHeaders(BeaconBlockHeadersRequest),
|
||||||
BeaconBlockBodies(BeaconBlockBodiesRequest),
|
BeaconBlockBodies(BeaconBlockBodiesRequest),
|
||||||
@ -113,6 +114,55 @@ pub struct HelloMessage {
|
|||||||
pub best_slot: Slot,
|
pub best_slot: Slot,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The reason given for a `Goodbye` message.
|
||||||
|
///
|
||||||
|
/// Note: any unknown `u64::into(n)` will resolve to `GoodbyeReason::Unknown` for any unknown `n`,
|
||||||
|
/// however `GoodbyeReason::Unknown.into()` will go into `0_u64`. Therefore de-serializing then
|
||||||
|
/// re-serializing may not return the same bytes.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum GoodbyeReason {
|
||||||
|
ClientShutdown,
|
||||||
|
IrreleventNetwork,
|
||||||
|
Fault,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u64> for GoodbyeReason {
|
||||||
|
fn from(id: u64) -> GoodbyeReason {
|
||||||
|
match id {
|
||||||
|
1 => GoodbyeReason::ClientShutdown,
|
||||||
|
2 => GoodbyeReason::IrreleventNetwork,
|
||||||
|
3 => GoodbyeReason::Fault,
|
||||||
|
_ => GoodbyeReason::Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<u64> for GoodbyeReason {
|
||||||
|
fn into(self) -> u64 {
|
||||||
|
match self {
|
||||||
|
GoodbyeReason::Unknown => 0,
|
||||||
|
GoodbyeReason::ClientShutdown => 1,
|
||||||
|
GoodbyeReason::IrreleventNetwork => 2,
|
||||||
|
GoodbyeReason::Fault => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encodable for GoodbyeReason {
|
||||||
|
fn ssz_append(&self, s: &mut SszStream) {
|
||||||
|
let id: u64 = (*self).clone().into();
|
||||||
|
id.ssz_append(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decodable for GoodbyeReason {
|
||||||
|
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
|
let (id, index) = u64::ssz_decode(bytes, index)?;
|
||||||
|
Ok((Self::from(id), index))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Request a number of beacon block roots from a peer.
|
/// Request a number of beacon block roots from a peer.
|
||||||
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
|
||||||
pub struct BeaconBlockRootsRequest {
|
pub struct BeaconBlockRootsRequest {
|
||||||
|
@ -82,8 +82,8 @@ fn decode(packet: Vec<u8>) -> Result<RPCEvent, DecodeError> {
|
|||||||
RPCRequest::Hello(hello_body)
|
RPCRequest::Hello(hello_body)
|
||||||
}
|
}
|
||||||
RPCMethod::Goodbye => {
|
RPCMethod::Goodbye => {
|
||||||
let (goodbye_code, _index) = u64::ssz_decode(&packet, index)?;
|
let (goodbye_reason, _index) = GoodbyeReason::ssz_decode(&packet, index)?;
|
||||||
RPCRequest::Goodbye(goodbye_code)
|
RPCRequest::Goodbye(goodbye_reason)
|
||||||
}
|
}
|
||||||
RPCMethod::BeaconBlockRoots => {
|
RPCMethod::BeaconBlockRoots => {
|
||||||
let (block_roots_request, _index) =
|
let (block_roots_request, _index) =
|
||||||
|
@ -107,13 +107,14 @@ impl MessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A new RPC request has been received from the network.
|
/// A new RPC request has been received from the network.
|
||||||
fn handle_rpc_request(&mut self, peer_id: PeerId, id: u64, request: RPCRequest) {
|
fn handle_rpc_request(&mut self, peer_id: PeerId, _id: u64, request: RPCRequest) {
|
||||||
// TODO: ensure the id is legit
|
// TODO: process the `id`.
|
||||||
match request {
|
match request {
|
||||||
RPCRequest::Hello(hello_message) => {
|
RPCRequest::Hello(hello_message) => {
|
||||||
self.sync
|
self.sync
|
||||||
.on_hello_request(peer_id, hello_message, &mut self.network_context)
|
.on_hello_request(peer_id, hello_message, &mut self.network_context)
|
||||||
}
|
}
|
||||||
|
RPCRequest::Goodbye(goodbye_reason) => self.sync.on_goodbye(peer_id, goodbye_reason),
|
||||||
RPCRequest::BeaconBlockRoots(request) => {
|
RPCRequest::BeaconBlockRoots(request) => {
|
||||||
self.sync
|
self.sync
|
||||||
.on_beacon_block_roots_request(peer_id, request, &mut self.network_context)
|
.on_beacon_block_roots_request(peer_id, request, &mut self.network_context)
|
||||||
@ -128,8 +129,11 @@ impl MessageHandler {
|
|||||||
request,
|
request,
|
||||||
&mut self.network_context,
|
&mut self.network_context,
|
||||||
),
|
),
|
||||||
// TODO: Handle all requests
|
RPCRequest::BeaconChainState(_) => {
|
||||||
_ => panic!("Unknown request: {:?}", request),
|
// We do not implement this endpoint, it is not required and will only likely be
|
||||||
|
// useful for light-client support in later phases.
|
||||||
|
warn!(self.log, "BeaconChainState RPC call is not supported.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,8 +176,14 @@ impl MessageHandler {
|
|||||||
&mut self.network_context,
|
&mut self.network_context,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// TODO: Handle all responses
|
RPCResponse::BeaconChainState(_) => {
|
||||||
_ => panic!("Unknown response: {:?}", response),
|
// We do not implement this endpoint, it is not required and will only likely be
|
||||||
|
// useful for light-client support in later phases.
|
||||||
|
//
|
||||||
|
// Theoretically, we shouldn't reach this code because we should never send a
|
||||||
|
// beacon state RPC request.
|
||||||
|
warn!(self.log, "BeaconChainState RPC call is not supported.");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,16 @@ impl SimpleSync {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_goodbye(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
|
||||||
|
info!(
|
||||||
|
self.log, "PeerGoodbye";
|
||||||
|
"peer" => format!("{:?}", peer_id),
|
||||||
|
"reason" => format!("{:?}", reason),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.known_peers.remove(&peer_id);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) {
|
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) {
|
||||||
info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id));
|
info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user