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,
|
2019-03-19 23:19:45 +00:00
|
|
|
BeaconBlockRoots,
|
2019-03-19 23:28:05 +00:00
|
|
|
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,
|
2019-03-19 23:19:45 +00:00
|
|
|
10 => RPCMethod::BeaconBlockRoots,
|
2019-03-19 23:28:05 +00:00
|
|
|
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,
|
2019-03-19 23:19:45 +00:00
|
|
|
RPCMethod::BeaconBlockRoots => 10,
|
2019-03-19 23:28:05 +00:00
|
|
|
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 {
|
2019-03-17 10:49:56 +00:00
|
|
|
Hello(HelloMessage),
|
2019-03-19 23:05:17 +00:00
|
|
|
Goodbye(u64),
|
2019-03-19 23:19:45 +00:00
|
|
|
BeaconBlockRoots(BeaconBlockRootsRequest),
|
2019-03-19 23:28:05 +00:00
|
|
|
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 {
|
2019-03-17 10:49:56 +00:00
|
|
|
Hello(HelloMessage),
|
2019-03-19 23:19:45 +00:00
|
|
|
BeaconBlockRoots(BeaconBlockRootsResponse),
|
2019-03-19 23:28:05 +00:00
|
|
|
BeaconBlockHeaders(BeaconBlockHeadersResponse),
|
2019-03-19 23:36:37 +00:00
|
|
|
BeaconBlockBodies(BeaconBlockBodiesResponse),
|
2019-03-14 14:50:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 23:18:04 +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)]
|
2019-03-17 10:49:56 +00:00
|
|
|
pub struct HelloMessage {
|
2019-03-19 23:18:04 +00:00
|
|
|
/// The network ID of the peer.
|
2019-03-14 14:50:59 +00:00
|
|
|
pub network_id: u8,
|
2019-03-19 23:18:04 +00:00
|
|
|
/// The peers last finalized root.
|
2019-03-14 14:50:59 +00:00
|
|
|
pub latest_finalized_root: Hash256,
|
2019-03-19 23:18:04 +00:00
|
|
|
/// The peers last finalized epoch.
|
2019-03-14 14:50:59 +00:00
|
|
|
pub latest_finalized_epoch: Epoch,
|
2019-03-19 23:18:04 +00:00
|
|
|
/// The peers last block root.
|
2019-03-14 14:50:59 +00:00
|
|
|
pub best_root: Hash256,
|
2019-03-19 23:18:04 +00:00
|
|
|
/// The peers last slot.
|
2019-03-14 14:50:59 +00:00
|
|
|
pub best_slot: Slot,
|
|
|
|
}
|
2019-03-19 23:18:04 +00:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2019-03-19 23:28:05 +00:00
|
|
|
/// Response containing a number of beacon block roots from a peer.
|
2019-03-19 23:18:04 +00:00
|
|
|
#[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,
|
|
|
|
}
|
2019-03-19 23:28:05 +00:00
|
|
|
|
|
|
|
/// 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>,
|
|
|
|
}
|