mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 04:01:51 +00:00
add a unique integer id to Rpc requests (#6444)
* add id to rpc requests * rename rpc request and response types for more accurate meaning * remove unrequired build_request function * remove unirequired Request wrapper types and unify Outbound and Inbound Request * add RequestId to NetworkMessage::SendResponse ,NetworkMessage::SendErrorResponse to be passed to Rpc::send_response
This commit is contained in:
@@ -481,7 +481,7 @@ impl DataColumnsByRootRequest {
|
||||
// Collection of enums and structs used by the Codecs to encode/decode RPC messages
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum RPCResponse<E: EthSpec> {
|
||||
pub enum RpcSuccessResponse<E: EthSpec> {
|
||||
/// A HELLO message.
|
||||
Status(StatusMessage),
|
||||
|
||||
@@ -545,11 +545,11 @@ pub enum ResponseTermination {
|
||||
/// The structured response containing a result/code indicating success or failure
|
||||
/// and the contents of the response
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RPCCodedResponse<E: EthSpec> {
|
||||
pub enum RpcResponse<E: EthSpec> {
|
||||
/// The response is a successful.
|
||||
Success(RPCResponse<E>),
|
||||
Success(RpcSuccessResponse<E>),
|
||||
|
||||
Error(RPCResponseErrorCode, ErrorType),
|
||||
Error(RpcErrorResponse, ErrorType),
|
||||
|
||||
/// Received a stream termination indicating which response is being terminated.
|
||||
StreamTermination(ResponseTermination),
|
||||
@@ -564,7 +564,7 @@ pub struct LightClientBootstrapRequest {
|
||||
/// The code assigned to an erroneous `RPCResponse`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, IntoStaticStr)]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum RPCResponseErrorCode {
|
||||
pub enum RpcErrorResponse {
|
||||
RateLimited,
|
||||
BlobsNotFoundForBlock,
|
||||
InvalidRequest,
|
||||
@@ -574,13 +574,13 @@ pub enum RPCResponseErrorCode {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> RPCCodedResponse<E> {
|
||||
impl<E: EthSpec> RpcResponse<E> {
|
||||
/// Used to encode the response in the codec.
|
||||
pub fn as_u8(&self) -> Option<u8> {
|
||||
match self {
|
||||
RPCCodedResponse::Success(_) => Some(0),
|
||||
RPCCodedResponse::Error(code, _) => Some(code.as_u8()),
|
||||
RPCCodedResponse::StreamTermination(_) => None,
|
||||
RpcResponse::Success(_) => Some(0),
|
||||
RpcResponse::Error(code, _) => Some(code.as_u8()),
|
||||
RpcResponse::StreamTermination(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -592,64 +592,66 @@ impl<E: EthSpec> RPCCodedResponse<E> {
|
||||
/// Builds an RPCCodedResponse from a response code and an ErrorMessage
|
||||
pub fn from_error(response_code: u8, err: ErrorType) -> Self {
|
||||
let code = match response_code {
|
||||
1 => RPCResponseErrorCode::InvalidRequest,
|
||||
2 => RPCResponseErrorCode::ServerError,
|
||||
3 => RPCResponseErrorCode::ResourceUnavailable,
|
||||
139 => RPCResponseErrorCode::RateLimited,
|
||||
140 => RPCResponseErrorCode::BlobsNotFoundForBlock,
|
||||
_ => RPCResponseErrorCode::Unknown,
|
||||
1 => RpcErrorResponse::InvalidRequest,
|
||||
2 => RpcErrorResponse::ServerError,
|
||||
3 => RpcErrorResponse::ResourceUnavailable,
|
||||
139 => RpcErrorResponse::RateLimited,
|
||||
140 => RpcErrorResponse::BlobsNotFoundForBlock,
|
||||
_ => RpcErrorResponse::Unknown,
|
||||
};
|
||||
RPCCodedResponse::Error(code, err)
|
||||
RpcResponse::Error(code, err)
|
||||
}
|
||||
|
||||
/// Returns true if this response always terminates the stream.
|
||||
pub fn close_after(&self) -> bool {
|
||||
!matches!(self, RPCCodedResponse::Success(_))
|
||||
!matches!(self, RpcResponse::Success(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl RPCResponseErrorCode {
|
||||
impl RpcErrorResponse {
|
||||
fn as_u8(&self) -> u8 {
|
||||
match self {
|
||||
RPCResponseErrorCode::InvalidRequest => 1,
|
||||
RPCResponseErrorCode::ServerError => 2,
|
||||
RPCResponseErrorCode::ResourceUnavailable => 3,
|
||||
RPCResponseErrorCode::Unknown => 255,
|
||||
RPCResponseErrorCode::RateLimited => 139,
|
||||
RPCResponseErrorCode::BlobsNotFoundForBlock => 140,
|
||||
RpcErrorResponse::InvalidRequest => 1,
|
||||
RpcErrorResponse::ServerError => 2,
|
||||
RpcErrorResponse::ResourceUnavailable => 3,
|
||||
RpcErrorResponse::Unknown => 255,
|
||||
RpcErrorResponse::RateLimited => 139,
|
||||
RpcErrorResponse::BlobsNotFoundForBlock => 140,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use super::Protocol;
|
||||
impl<E: EthSpec> RPCResponse<E> {
|
||||
impl<E: EthSpec> RpcSuccessResponse<E> {
|
||||
pub fn protocol(&self) -> Protocol {
|
||||
match self {
|
||||
RPCResponse::Status(_) => Protocol::Status,
|
||||
RPCResponse::BlocksByRange(_) => Protocol::BlocksByRange,
|
||||
RPCResponse::BlocksByRoot(_) => Protocol::BlocksByRoot,
|
||||
RPCResponse::BlobsByRange(_) => Protocol::BlobsByRange,
|
||||
RPCResponse::BlobsByRoot(_) => Protocol::BlobsByRoot,
|
||||
RPCResponse::DataColumnsByRoot(_) => Protocol::DataColumnsByRoot,
|
||||
RPCResponse::DataColumnsByRange(_) => Protocol::DataColumnsByRange,
|
||||
RPCResponse::Pong(_) => Protocol::Ping,
|
||||
RPCResponse::MetaData(_) => Protocol::MetaData,
|
||||
RPCResponse::LightClientBootstrap(_) => Protocol::LightClientBootstrap,
|
||||
RPCResponse::LightClientOptimisticUpdate(_) => Protocol::LightClientOptimisticUpdate,
|
||||
RPCResponse::LightClientFinalityUpdate(_) => Protocol::LightClientFinalityUpdate,
|
||||
RpcSuccessResponse::Status(_) => Protocol::Status,
|
||||
RpcSuccessResponse::BlocksByRange(_) => Protocol::BlocksByRange,
|
||||
RpcSuccessResponse::BlocksByRoot(_) => Protocol::BlocksByRoot,
|
||||
RpcSuccessResponse::BlobsByRange(_) => Protocol::BlobsByRange,
|
||||
RpcSuccessResponse::BlobsByRoot(_) => Protocol::BlobsByRoot,
|
||||
RpcSuccessResponse::DataColumnsByRoot(_) => Protocol::DataColumnsByRoot,
|
||||
RpcSuccessResponse::DataColumnsByRange(_) => Protocol::DataColumnsByRange,
|
||||
RpcSuccessResponse::Pong(_) => Protocol::Ping,
|
||||
RpcSuccessResponse::MetaData(_) => Protocol::MetaData,
|
||||
RpcSuccessResponse::LightClientBootstrap(_) => Protocol::LightClientBootstrap,
|
||||
RpcSuccessResponse::LightClientOptimisticUpdate(_) => {
|
||||
Protocol::LightClientOptimisticUpdate
|
||||
}
|
||||
RpcSuccessResponse::LightClientFinalityUpdate(_) => Protocol::LightClientFinalityUpdate,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for RPCResponseErrorCode {
|
||||
impl std::fmt::Display for RpcErrorResponse {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let repr = match self {
|
||||
RPCResponseErrorCode::InvalidRequest => "The request was invalid",
|
||||
RPCResponseErrorCode::ResourceUnavailable => "Resource unavailable",
|
||||
RPCResponseErrorCode::ServerError => "Server error occurred",
|
||||
RPCResponseErrorCode::Unknown => "Unknown error occurred",
|
||||
RPCResponseErrorCode::RateLimited => "Rate limited",
|
||||
RPCResponseErrorCode::BlobsNotFoundForBlock => "No blobs for the given root",
|
||||
RpcErrorResponse::InvalidRequest => "The request was invalid",
|
||||
RpcErrorResponse::ResourceUnavailable => "Resource unavailable",
|
||||
RpcErrorResponse::ServerError => "Server error occurred",
|
||||
RpcErrorResponse::Unknown => "Unknown error occurred",
|
||||
RpcErrorResponse::RateLimited => "Rate limited",
|
||||
RpcErrorResponse::BlobsNotFoundForBlock => "No blobs for the given root",
|
||||
};
|
||||
f.write_str(repr)
|
||||
}
|
||||
@@ -661,45 +663,47 @@ impl std::fmt::Display for StatusMessage {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> std::fmt::Display for RPCResponse<E> {
|
||||
impl<E: EthSpec> std::fmt::Display for RpcSuccessResponse<E> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
RPCResponse::Status(status) => write!(f, "{}", status),
|
||||
RPCResponse::BlocksByRange(block) => {
|
||||
RpcSuccessResponse::Status(status) => write!(f, "{}", status),
|
||||
RpcSuccessResponse::BlocksByRange(block) => {
|
||||
write!(f, "BlocksByRange: Block slot: {}", block.slot())
|
||||
}
|
||||
RPCResponse::BlocksByRoot(block) => {
|
||||
RpcSuccessResponse::BlocksByRoot(block) => {
|
||||
write!(f, "BlocksByRoot: Block slot: {}", block.slot())
|
||||
}
|
||||
RPCResponse::BlobsByRange(blob) => {
|
||||
RpcSuccessResponse::BlobsByRange(blob) => {
|
||||
write!(f, "BlobsByRange: Blob slot: {}", blob.slot())
|
||||
}
|
||||
RPCResponse::BlobsByRoot(sidecar) => {
|
||||
RpcSuccessResponse::BlobsByRoot(sidecar) => {
|
||||
write!(f, "BlobsByRoot: Blob slot: {}", sidecar.slot())
|
||||
}
|
||||
RPCResponse::DataColumnsByRoot(sidecar) => {
|
||||
RpcSuccessResponse::DataColumnsByRoot(sidecar) => {
|
||||
write!(f, "DataColumnsByRoot: Data column slot: {}", sidecar.slot())
|
||||
}
|
||||
RPCResponse::DataColumnsByRange(sidecar) => {
|
||||
RpcSuccessResponse::DataColumnsByRange(sidecar) => {
|
||||
write!(
|
||||
f,
|
||||
"DataColumnsByRange: Data column slot: {}",
|
||||
sidecar.slot()
|
||||
)
|
||||
}
|
||||
RPCResponse::Pong(ping) => write!(f, "Pong: {}", ping.data),
|
||||
RPCResponse::MetaData(metadata) => write!(f, "Metadata: {}", metadata.seq_number()),
|
||||
RPCResponse::LightClientBootstrap(bootstrap) => {
|
||||
RpcSuccessResponse::Pong(ping) => write!(f, "Pong: {}", ping.data),
|
||||
RpcSuccessResponse::MetaData(metadata) => {
|
||||
write!(f, "Metadata: {}", metadata.seq_number())
|
||||
}
|
||||
RpcSuccessResponse::LightClientBootstrap(bootstrap) => {
|
||||
write!(f, "LightClientBootstrap Slot: {}", bootstrap.get_slot())
|
||||
}
|
||||
RPCResponse::LightClientOptimisticUpdate(update) => {
|
||||
RpcSuccessResponse::LightClientOptimisticUpdate(update) => {
|
||||
write!(
|
||||
f,
|
||||
"LightClientOptimisticUpdate Slot: {}",
|
||||
update.signature_slot()
|
||||
)
|
||||
}
|
||||
RPCResponse::LightClientFinalityUpdate(update) => {
|
||||
RpcSuccessResponse::LightClientFinalityUpdate(update) => {
|
||||
write!(
|
||||
f,
|
||||
"LightClientFinalityUpdate Slot: {}",
|
||||
@@ -710,12 +714,12 @@ impl<E: EthSpec> std::fmt::Display for RPCResponse<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> std::fmt::Display for RPCCodedResponse<E> {
|
||||
impl<E: EthSpec> std::fmt::Display for RpcResponse<E> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
RPCCodedResponse::Success(res) => write!(f, "{}", res),
|
||||
RPCCodedResponse::Error(code, err) => write!(f, "{}: {}", code, err),
|
||||
RPCCodedResponse::StreamTermination(_) => write!(f, "Stream Termination"),
|
||||
RpcResponse::Success(res) => write!(f, "{}", res),
|
||||
RpcResponse::Error(code, err) => write!(f, "{}: {}", code, err),
|
||||
RpcResponse::StreamTermination(_) => write!(f, "Stream Termination"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user