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,
|
2019-03-19 23:18:04 +00:00
|
|
|
RequestBeaconBlockRoots,
|
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:18:04 +00:00
|
|
|
10 => RPCMethod::RequestBeaconBlockRoots,
|
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:18:04 +00:00
|
|
|
RPCMethod::RequestBeaconBlockRoots => 10,
|
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:18:04 +00:00
|
|
|
RequestBeaconBlockRoots(BeaconBlockRootsRequest),
|
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:18:04 +00:00
|
|
|
RequestBeaconBlockRoots(BeaconBlockRootsResponse),
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|