lighthouse/beacon_node/libp2p/src/rpc/methods.rs

89 lines
2.2 KiB
Rust
Raw Normal View History

2019-03-14 14:50:59 +00:00
/// Available RPC methods types and ids.
use ssz_derive::{Decode, Encode};
use types::{Epoch, Hash256, Slot};
#[derive(Debug)]
pub enum RPCMethod {
Hello,
2019-03-19 23:05:17 +00:00
Goodbye,
BeaconBlockRoots,
2019-03-14 14:50:59 +00:00
Unknown,
}
impl From<u16> for RPCMethod {
fn from(method_id: u16) -> Self {
match method_id {
0 => RPCMethod::Hello,
2019-03-19 23:05:17 +00:00
1 => RPCMethod::Goodbye,
10 => RPCMethod::BeaconBlockRoots,
2019-03-14 14:50:59 +00:00
_ => RPCMethod::Unknown,
}
}
}
2019-03-19 01:19:07 +00:00
impl Into<u16> for RPCMethod {
fn into(self) -> u16 {
match self {
RPCMethod::Hello => 0,
2019-03-19 23:05:17 +00:00
RPCMethod::Goodbye => 1,
RPCMethod::BeaconBlockRoots => 10,
2019-03-19 01:19:07 +00:00
_ => 0,
}
}
}
2019-03-14 14:50:59 +00:00
#[derive(Debug, Clone)]
pub enum RPCRequest {
Hello(HelloMessage),
2019-03-19 23:05:17 +00:00
Goodbye(u64),
BeaconBlockRoots(BeaconBlockRootsRequest),
2019-03-14 14:50:59 +00:00
}
#[derive(Debug, Clone)]
pub enum RPCResponse {
Hello(HelloMessage),
BeaconBlockRoots(BeaconBlockRootsResponse),
2019-03-14 14:50:59 +00:00
}
/* Request/Response data structures for RPC methods */
/// The HELLO request/response handshake message.
2019-03-14 14:50:59 +00:00
#[derive(Encode, Decode, Clone, Debug)]
pub struct HelloMessage {
/// The network ID of the peer.
2019-03-14 14:50:59 +00:00
pub network_id: u8,
/// The peers last finalized root.
2019-03-14 14:50:59 +00:00
pub latest_finalized_root: Hash256,
/// The peers last finalized epoch.
2019-03-14 14:50:59 +00:00
pub latest_finalized_epoch: Epoch,
/// The peers last block root.
2019-03-14 14:50:59 +00:00
pub best_root: Hash256,
/// The peers last slot.
2019-03-14 14:50:59 +00:00
pub best_slot: Slot,
}
/// Request a number of beacon block roots from a peer.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockRootsRequest {
/// The starting slot of the requested blocks.
start_slot: Slot,
/// The number of blocks from the start slot.
count: u64, // this must be less than 32768. //TODO: Enforce this in the lower layers
}
/// Response a number of beacon block roots from a peer.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockRootsResponse {
/// List of requested blocks and associated slots.
roots: Vec<BlockRootSlot>,
}
/// Contains a block root and associated slot.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BlockRootSlot {
/// The block root.
block_root: Hash256,
/// The block slot.
slot: Slot,
}