Implement RequestBeaconBlockRoots RPC method

This commit is contained in:
Age Manning 2019-03-20 10:18:04 +11:00
parent 0e8b174770
commit 8acfb260d1
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93

View File

@ -6,6 +6,7 @@ use types::{Epoch, Hash256, Slot};
pub enum RPCMethod { pub enum RPCMethod {
Hello, Hello,
Goodbye, Goodbye,
RequestBeaconBlockRoots,
Unknown, Unknown,
} }
@ -14,6 +15,7 @@ impl From<u16> for RPCMethod {
match method_id { match method_id {
0 => RPCMethod::Hello, 0 => RPCMethod::Hello,
1 => RPCMethod::Goodbye, 1 => RPCMethod::Goodbye,
10 => RPCMethod::RequestBeaconBlockRoots,
_ => RPCMethod::Unknown, _ => RPCMethod::Unknown,
} }
} }
@ -24,6 +26,7 @@ impl Into<u16> for RPCMethod {
match self { match self {
RPCMethod::Hello => 0, RPCMethod::Hello => 0,
RPCMethod::Goodbye => 1, RPCMethod::Goodbye => 1,
RPCMethod::RequestBeaconBlockRoots => 10,
_ => 0, _ => 0,
} }
} }
@ -33,19 +36,53 @@ impl Into<u16> for RPCMethod {
pub enum RPCRequest { pub enum RPCRequest {
Hello(HelloMessage), Hello(HelloMessage),
Goodbye(u64), Goodbye(u64),
RequestBeaconBlockRoots(BeaconBlockRootsRequest),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum RPCResponse { pub enum RPCResponse {
Hello(HelloMessage), Hello(HelloMessage),
RequestBeaconBlockRoots(BeaconBlockRootsResponse),
} }
// request/response structs for RPC methods /* Request/Response data structures for RPC methods */
/// The HELLO request/response handshake message.
#[derive(Encode, Decode, Clone, Debug)] #[derive(Encode, Decode, Clone, Debug)]
pub struct HelloMessage { pub struct HelloMessage {
/// The network ID of the peer.
pub network_id: u8, pub network_id: u8,
/// The peers last finalized root.
pub latest_finalized_root: Hash256, pub latest_finalized_root: Hash256,
/// The peers last finalized epoch.
pub latest_finalized_epoch: Epoch, pub latest_finalized_epoch: Epoch,
/// The peers last block root.
pub best_root: Hash256, pub best_root: Hash256,
/// The peers last slot.
pub best_slot: Slot, 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,
}