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

133 lines
3.8 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};
2019-03-19 23:36:37 +00:00
use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot};
2019-03-14 14:50:59 +00:00
#[derive(Debug)]
pub enum RPCMethod {
Hello,
2019-03-19 23:05:17 +00:00
Goodbye,
BeaconBlockRoots,
BeaconBlockHeaders,
2019-03-19 23:36:37 +00:00
BeaconBlockBodies,
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,
11 => RPCMethod::BeaconBlockHeaders,
2019-03-19 23:36:37 +00:00
12 => RPCMethod::BeaconBlockBodies,
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,
RPCMethod::BeaconBlockHeaders => 11,
2019-03-19 23:36:37 +00:00
RPCMethod::BeaconBlockBodies => 12,
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),
BeaconBlockHeaders(BeaconBlockHeadersRequest),
2019-03-19 23:36:37 +00:00
BeaconBlockBodies(BeaconBlockBodiesRequest),
2019-03-14 14:50:59 +00:00
}
#[derive(Debug, Clone)]
pub enum RPCResponse {
Hello(HelloMessage),
BeaconBlockRoots(BeaconBlockRootsResponse),
BeaconBlockHeaders(BeaconBlockHeadersResponse),
2019-03-19 23:36:37 +00:00
BeaconBlockBodies(BeaconBlockBodiesResponse),
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 containing 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,
}
/// Request a number of beacon block headers from a peer.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockHeadersRequest {
/// The starting header hash of the requested headers.
start_root: Hash256,
/// The starting slot of the requested headers.
start_slot: Slot,
/// The maximum number of headers than can be returned.
max_headers: u64,
/// The maximum number of slots to skip between blocks.
skip_slots: u64,
}
/// Response containing requested block headers.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockHeadersResponse {
/// The list of requested beacon block headers.
headers: Vec<BeaconBlockHeader>,
}
2019-03-19 23:36:37 +00:00
/// Request a number of beacon block bodies from a peer.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockBodiesRequest {
/// The list of beacon block bodies being requested.
block_roots: Hash256,
}
/// Response containing the list of requested beacon block bodies.
#[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockBodiesResponse {
/// The list of beacon block bodies being requested.
block_bodies: Vec<BeaconBlockBody>,
}