Implement BeaconBlockHeaders RPC method

This commit is contained in:
Age Manning 2019-03-20 10:28:05 +11:00
parent 450b2cfb81
commit 8fa70f64ec
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93

View File

@ -1,12 +1,13 @@
/// Available RPC methods types and ids. /// Available RPC methods types and ids.
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use types::{Epoch, Hash256, Slot}; use types::{BeaconBlockHeader, Epoch, Hash256, Slot};
#[derive(Debug)] #[derive(Debug)]
pub enum RPCMethod { pub enum RPCMethod {
Hello, Hello,
Goodbye, Goodbye,
BeaconBlockRoots, BeaconBlockRoots,
BeaconBlockHeaders,
Unknown, Unknown,
} }
@ -16,6 +17,7 @@ impl From<u16> for RPCMethod {
0 => RPCMethod::Hello, 0 => RPCMethod::Hello,
1 => RPCMethod::Goodbye, 1 => RPCMethod::Goodbye,
10 => RPCMethod::BeaconBlockRoots, 10 => RPCMethod::BeaconBlockRoots,
11 => RPCMethod::BeaconBlockHeaders,
_ => RPCMethod::Unknown, _ => RPCMethod::Unknown,
} }
} }
@ -27,6 +29,7 @@ impl Into<u16> for RPCMethod {
RPCMethod::Hello => 0, RPCMethod::Hello => 0,
RPCMethod::Goodbye => 1, RPCMethod::Goodbye => 1,
RPCMethod::BeaconBlockRoots => 10, RPCMethod::BeaconBlockRoots => 10,
RPCMethod::BeaconBlockHeaders => 11,
_ => 0, _ => 0,
} }
} }
@ -37,12 +40,14 @@ pub enum RPCRequest {
Hello(HelloMessage), Hello(HelloMessage),
Goodbye(u64), Goodbye(u64),
BeaconBlockRoots(BeaconBlockRootsRequest), BeaconBlockRoots(BeaconBlockRootsRequest),
BeaconBlockHeaders(BeaconBlockHeadersRequest),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum RPCResponse { pub enum RPCResponse {
Hello(HelloMessage), Hello(HelloMessage),
BeaconBlockRoots(BeaconBlockRootsResponse), BeaconBlockRoots(BeaconBlockRootsResponse),
BeaconBlockHeaders(BeaconBlockHeadersResponse),
} }
/* Request/Response data structures for RPC methods */ /* Request/Response data structures for RPC methods */
@ -71,7 +76,7 @@ pub struct BeaconBlockRootsRequest {
count: u64, // this must be less than 32768. //TODO: Enforce this in the lower layers 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. /// Response containing a number of beacon block roots from a peer.
#[derive(Encode, Decode, Clone, Debug)] #[derive(Encode, Decode, Clone, Debug)]
pub struct BeaconBlockRootsResponse { pub struct BeaconBlockRootsResponse {
/// List of requested blocks and associated slots. /// List of requested blocks and associated slots.
@ -86,3 +91,23 @@ pub struct BlockRootSlot {
/// The block slot. /// The block slot.
slot: 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>,
}