mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 03:31:45 +00:00
Initial base codec
This commit is contained in:
@@ -212,81 +212,6 @@ impl RPCRequest {
|
||||
RPCRequest::BeaconChainState(req) => req.as_ssz_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
// This function can be extended to provide further logic for supporting various protocol versions/encoding
|
||||
/// Decodes a request received from our peer.
|
||||
pub fn decode(packet: Vec<u8>, protocol: ProtocolId) -> Result<Self, RPCError> {
|
||||
match protocol.message_name.as_str() {
|
||||
"hello" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::Hello(HelloMessage::from_ssz_bytes(&packet)?)),
|
||||
_ => Err(RPCError::InvalidProtocol("Unknown HELLO encoding")),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol("Unknown HELLO version")),
|
||||
},
|
||||
"goodbye" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::Goodbye(Goodbye::from_ssz_bytes(&packet)?)),
|
||||
_ => Err(RPCError::InvalidProtocol("Unknown GOODBYE encoding")),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown GOODBYE version.as_str()",
|
||||
)),
|
||||
},
|
||||
"beacon_block_roots" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::BeaconBlockRoots(
|
||||
BeaconBlockRootsRequest::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_ROOTS encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_ROOTS version.",
|
||||
)),
|
||||
},
|
||||
"beacon_block_headers" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::BeaconBlockHeaders(
|
||||
BeaconBlockHeadersRequest::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_HEADERS encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_HEADERS version.",
|
||||
)),
|
||||
},
|
||||
"beacon_block_bodies" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::BeaconBlockBodies(
|
||||
BeaconBlockBodiesRequest::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_BODIES encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_BODIES version.",
|
||||
)),
|
||||
},
|
||||
"beacon_chain_state" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCRequest::BeaconChainState(
|
||||
BeaconChainStateRequest::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_CHAIN_STATE encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_CHAIN_STATE version.",
|
||||
)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Response Type */
|
||||
@@ -305,163 +230,61 @@ pub enum RPCResponse {
|
||||
BeaconBlockBodies(BeaconBlockBodiesResponse),
|
||||
/// A response to a get BEACON_CHAIN_STATE request.
|
||||
BeaconChainState(BeaconChainStateResponse),
|
||||
/// The Error returned from the peer during a request.
|
||||
Error(String),
|
||||
}
|
||||
|
||||
pub enum ResponseCode {
|
||||
Success = 0,
|
||||
EncodingError = 1,
|
||||
InvalidRequest = 2,
|
||||
ServerError = 3,
|
||||
Unknown = 255,
|
||||
pub enum RPCErrorResponse {
|
||||
Success(RPCResponse),
|
||||
EncodingError,
|
||||
InvalidRequest(ErrorMessage),
|
||||
ServerError(ErrorMessage),
|
||||
Unknown(ErrorMessage),
|
||||
}
|
||||
|
||||
impl From<u8> for ResponseCode {
|
||||
fn from(val: u8) -> ResponseCode {
|
||||
match val {
|
||||
0 => ResponseCode::Success,
|
||||
1 => ResponseCode::EncodingError,
|
||||
2 => ResponseCode::InvalidRequest,
|
||||
3 => ResponseCode::ServerError,
|
||||
_ => ResponseCode::Unknown,
|
||||
impl RPCErrorResponse {
|
||||
/// If a response has no payload, returns the variant corresponding to the code.
|
||||
pub fn internal_data(response_code: u8) -> Option<RPCErrorResponse> {
|
||||
match response_code {
|
||||
// EncodingError
|
||||
1 => Some(RPCErrorResponse::EncodingError),
|
||||
// All others require further data
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u8> for ResponseCode {
|
||||
fn into(self) -> u8 {
|
||||
self as u8
|
||||
pub fn as_u8(&self) -> u8 {
|
||||
match self {
|
||||
RPCErrorResponse::Success(_) => 0,
|
||||
RPCErrorResponse::EncodingError => 1,
|
||||
RPCErrorResponse::InvalidRequest(_) => 2,
|
||||
RPCErrorResponse::ServerError(_) => 3,
|
||||
RPCErrorResponse::Unknown(_) => 255,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tells the codec whether to decode as an RPCResponse or an error.
|
||||
pub fn is_response(response_code:u8) -> bool {
|
||||
match response_code {
|
||||
0 => true,
|
||||
_ => false,
|
||||
}
|
||||
|
||||
/// Builds an RPCErrorResponse from a response code and an ErrorMessage
|
||||
pub fn from_error(response_code:u8, err: ErrorMessage) -> Self {
|
||||
match response_code {
|
||||
2 => RPCErrorResponse::InvalidRequest(err),
|
||||
3 => RPCErrorResponse::ServerError(err),
|
||||
_ => RPCErrorResponse::Unknown(err),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode)]
|
||||
struct ErrorResponse {
|
||||
error_message: String,
|
||||
struct ErrorMessage {
|
||||
/// The UTF-8 encoded Error message string.
|
||||
error_message: Vec<u8>,
|
||||
}
|
||||
|
||||
impl RPCResponse {
|
||||
/// Decodes a response that was received on the same stream as a request. The response type should
|
||||
/// therefore match the request protocol type.
|
||||
pub fn decode(
|
||||
packet: Vec<u8>,
|
||||
protocol: ProtocolId,
|
||||
response_code: ResponseCode,
|
||||
) -> Result<Self, RPCError> {
|
||||
match response_code {
|
||||
ResponseCode::EncodingError => Ok(RPCResponse::Error("Encoding error".into())),
|
||||
ResponseCode::InvalidRequest => {
|
||||
let response = match protocol.encoding.as_str() {
|
||||
"ssz" => ErrorResponse::from_ssz_bytes(&packet)?,
|
||||
_ => return Err(RPCError::InvalidProtocol("Unknown Encoding")),
|
||||
};
|
||||
Ok(RPCResponse::Error(format!(
|
||||
"Invalid Request: {}",
|
||||
response.error_message
|
||||
)))
|
||||
}
|
||||
ResponseCode::ServerError => {
|
||||
let response = match protocol.encoding.as_str() {
|
||||
"ssz" => ErrorResponse::from_ssz_bytes(&packet)?,
|
||||
_ => return Err(RPCError::InvalidProtocol("Unknown Encoding")),
|
||||
};
|
||||
Ok(RPCResponse::Error(format!(
|
||||
"Remote Server Error: {}",
|
||||
response.error_message
|
||||
)))
|
||||
}
|
||||
ResponseCode::Success => match protocol.message_name.as_str() {
|
||||
"hello" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCResponse::Hello(HelloMessage::from_ssz_bytes(&packet)?)),
|
||||
_ => Err(RPCError::InvalidProtocol("Unknown HELLO encoding")),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol("Unknown HELLO version.")),
|
||||
},
|
||||
"goodbye" => Err(RPCError::Custom(
|
||||
"GOODBYE should not have a response".into(),
|
||||
)),
|
||||
"beacon_block_roots" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCResponse::BeaconBlockRoots(
|
||||
BeaconBlockRootsResponse::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_ROOTS encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_ROOTS version.",
|
||||
)),
|
||||
},
|
||||
"beacon_block_headers" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCResponse::BeaconBlockHeaders(
|
||||
BeaconBlockHeadersResponse { headers: packet },
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_HEADERS encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_HEADERS version.",
|
||||
)),
|
||||
},
|
||||
"beacon_block_bodies" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCResponse::BeaconBlockBodies(BeaconBlockBodiesResponse {
|
||||
block_bodies: packet,
|
||||
})),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_BODIES encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_BLOCK_BODIES version.",
|
||||
)),
|
||||
},
|
||||
"beacon_chain_state" => match protocol.version.as_str() {
|
||||
"1.0.0" => match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(RPCResponse::BeaconChainState(
|
||||
BeaconChainStateResponse::from_ssz_bytes(&packet)?,
|
||||
)),
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_CHAIN_STATE encoding",
|
||||
)),
|
||||
},
|
||||
_ => Err(RPCError::InvalidProtocol(
|
||||
"Unknown BEACON_CHAIN_STATE version.",
|
||||
)),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes the Response object based on the negotiated protocol.
|
||||
pub fn encode(&self, protocol: ProtocolId) -> Result<Vec<u8>, RPCError> {
|
||||
// Match on the encoding and in the future, the version
|
||||
match protocol.encoding.as_str() {
|
||||
"ssz" => Ok(self.ssz_encode()),
|
||||
_ => {
|
||||
return Err(RPCError::Custom(format!(
|
||||
"Unknown Encoding: {}",
|
||||
protocol.encoding
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ssz_encode(&self) -> Vec<u8> {
|
||||
match self {
|
||||
RPCResponse::Hello(res) => res.as_ssz_bytes(),
|
||||
RPCResponse::Goodbye => unreachable!(),
|
||||
RPCResponse::BeaconBlockRoots(res) => res.as_ssz_bytes(),
|
||||
RPCResponse::BeaconBlockHeaders(res) => res.headers, // already raw bytes
|
||||
RPCResponse::BeaconBlockBodies(res) => res.block_bodies, // already raw bytes
|
||||
RPCResponse::BeaconChainState(res) => res.as_ssz_bytes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
// todo: SSZ-Encode
|
||||
impl RPCResponse {}
|
||||
|
||||
/* Outbound upgrades */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user