From fa7147f7c5cc0fcf289bcd0602b9d18ee269509c Mon Sep 17 00:00:00 2001 From: divma Date: Mon, 20 Apr 2020 08:15:08 -0500 Subject: [PATCH] Clean RPC names versions and encodings (#1024) * cleanup RPC protocol names * add rpc encodings * Add RPC Versions as an enum --- beacon_node/eth2-libp2p/src/rpc/codec/ssz.rs | 112 +++++------- .../eth2-libp2p/src/rpc/codec/ssz_snappy.rs | 80 ++++----- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 160 ++++++++++++------ 3 files changed, 183 insertions(+), 169 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/rpc/codec/ssz.rs b/beacon_node/eth2-libp2p/src/rpc/codec/ssz.rs index b3c7f2ce7b..bd8cbfd9f8 100644 --- a/beacon_node/eth2-libp2p/src/rpc/codec/ssz.rs +++ b/beacon_node/eth2-libp2p/src/rpc/codec/ssz.rs @@ -1,10 +1,7 @@ use crate::rpc::methods::*; use crate::rpc::{ codec::base::OutboundCodec, - protocol::{ - ProtocolId, RPCError, RPC_BLOCKS_BY_RANGE, RPC_BLOCKS_BY_ROOT, RPC_GOODBYE, RPC_META_DATA, - RPC_PING, RPC_STATUS, - }, + protocol::{Encoding, Protocol, ProtocolId, RPCError, Version}, }; use crate::rpc::{ErrorMessage, RPCErrorResponse, RPCRequest, RPCResponse}; use libp2p::bytes::{BufMut, Bytes, BytesMut}; @@ -28,7 +25,7 @@ impl SSZInboundCodec { uvi_codec.set_max_len(max_packet_size); // this encoding only applies to ssz. - debug_assert!(protocol.encoding.as_str() == "ssz"); + debug_assert_eq!(protocol.encoding, Encoding::SSZ); SSZInboundCodec { inner: uvi_codec, @@ -81,39 +78,34 @@ impl Decoder for SSZInboundCodec { fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { match self.inner.decode(src).map_err(RPCError::from) { - Ok(Some(packet)) => match self.protocol.message_name.as_str() { - RPC_STATUS => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Status(StatusMessage::from_ssz_bytes( + Ok(Some(packet)) => match self.protocol.message_name { + Protocol::Status => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Status(StatusMessage::from_ssz_bytes( &packet, )?))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_GOODBYE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Goodbye(GoodbyeReason::from_ssz_bytes( + Protocol::Goodbye => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Goodbye(GoodbyeReason::from_ssz_bytes( &packet, )?))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_RANGE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::BlocksByRange( + Protocol::BlocksByRange => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::BlocksByRange( BlocksByRangeRequest::from_ssz_bytes(&packet)?, ))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_ROOT => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::BlocksByRoot(BlocksByRootRequest { + Protocol::BlocksByRoot => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::BlocksByRoot(BlocksByRootRequest { block_roots: Vec::from_ssz_bytes(&packet)?, }))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_PING => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Ping(Ping { + Protocol::Ping => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Ping(Ping { data: u64::from_ssz_bytes(&packet)?, }))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_META_DATA => match self.protocol.version.as_str() { - "1" => { + Protocol::MetaData => match self.protocol.version { + Version::V1 => { if packet.len() > 0 { Err(RPCError::Custom( "Get metadata request should be empty".into(), @@ -122,9 +114,7 @@ impl Decoder for SSZInboundCodec { Ok(Some(RPCRequest::MetaData(PhantomData))) } } - _ => unreachable!("Cannot negotiate an unknown version"), }, - _ => unreachable!("Cannot negotiate an unknown protocol"), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -146,7 +136,7 @@ impl SSZOutboundCodec { uvi_codec.set_max_len(max_packet_size); // this encoding only applies to ssz. - debug_assert!(protocol.encoding.as_str() == "ssz"); + debug_assert_eq!(protocol.encoding, Encoding::SSZ); SSZOutboundCodec { inner: uvi_codec, @@ -191,39 +181,35 @@ impl Decoder for SSZOutboundCodec { // the object is empty. We return the empty object if this is the case // clear the buffer and return an empty object src.clear(); - match self.protocol.message_name.as_str() { - RPC_STATUS => match self.protocol.version.as_str() { - "1" => Err(RPCError::Custom( + match self.protocol.message_name { + Protocol::Status => match self.protocol.version { + Version::V1 => Err(RPCError::Custom( "Status stream terminated unexpectedly".into(), )), // cannot have an empty HELLO message. The stream has terminated unexpectedly - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_GOODBYE => Err(RPCError::InvalidProtocol("GOODBYE doesn't have a response")), - RPC_BLOCKS_BY_RANGE => match self.protocol.version.as_str() { - "1" => Err(RPCError::Custom( + Protocol::Goodbye => { + Err(RPCError::InvalidProtocol("GOODBYE doesn't have a response")) + } + Protocol::BlocksByRange => match self.protocol.version { + Version::V1 => Err(RPCError::Custom( "Status stream terminated unexpectedly, empty block".into(), )), // cannot have an empty block message. - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_ROOT => match self.protocol.version.as_str() { - "1" => Err(RPCError::Custom( + Protocol::BlocksByRoot => match self.protocol.version { + Version::V1 => Err(RPCError::Custom( "Status stream terminated unexpectedly, empty block".into(), )), // cannot have an empty block message. - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_PING => match self.protocol.version.as_str() { - "1" => Err(RPCError::Custom( + Protocol::Ping => match self.protocol.version { + Version::V1 => Err(RPCError::Custom( "PING stream terminated unexpectedly".into(), )), // cannot have an empty block message. - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_META_DATA => match self.protocol.version.as_str() { - "1" => Err(RPCError::Custom( + Protocol::MetaData => match self.protocol.version { + Version::V1 => Err(RPCError::Custom( "Metadata stream terminated unexpectedly".into(), )), // cannot have an empty block message. - _ => unreachable!("Cannot negotiate an unknown version"), }, - _ => unreachable!("Cannot negotiate an unknown protocol"), } } else { match self.inner.decode(src).map_err(RPCError::from) { @@ -231,41 +217,35 @@ impl Decoder for SSZOutboundCodec { // take the bytes from the buffer let raw_bytes = packet.take(); - match self.protocol.message_name.as_str() { - RPC_STATUS => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::Status(StatusMessage::from_ssz_bytes( - &raw_bytes, - )?))), - _ => unreachable!("Cannot negotiate an unknown version"), + match self.protocol.message_name { + Protocol::Status => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::Status( + StatusMessage::from_ssz_bytes(&raw_bytes)?, + ))), }, - RPC_GOODBYE => { + Protocol::Goodbye => { Err(RPCError::InvalidProtocol("GOODBYE doesn't have a response")) } - RPC_BLOCKS_BY_RANGE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::BlocksByRange(Box::new( + Protocol::BlocksByRange => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::BlocksByRange(Box::new( SignedBeaconBlock::from_ssz_bytes(&raw_bytes)?, )))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_ROOT => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::BlocksByRoot(Box::new( + Protocol::BlocksByRoot => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::BlocksByRoot(Box::new( SignedBeaconBlock::from_ssz_bytes(&raw_bytes)?, )))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_PING => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::Pong(Ping { + Protocol::Ping => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::Pong(Ping { data: u64::from_ssz_bytes(&raw_bytes)?, }))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_META_DATA => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::MetaData(MetaData::from_ssz_bytes( - &raw_bytes, - )?))), - _ => unreachable!("Cannot negotiate an unknown version"), + Protocol::MetaData => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::MetaData( + MetaData::from_ssz_bytes(&raw_bytes)?, + ))), }, - _ => unreachable!("Cannot negotiate an unknown protocol"), } } Ok(None) => Ok(None), // waiting for more bytes diff --git a/beacon_node/eth2-libp2p/src/rpc/codec/ssz_snappy.rs b/beacon_node/eth2-libp2p/src/rpc/codec/ssz_snappy.rs index c345a71451..e2f0db1ff4 100644 --- a/beacon_node/eth2-libp2p/src/rpc/codec/ssz_snappy.rs +++ b/beacon_node/eth2-libp2p/src/rpc/codec/ssz_snappy.rs @@ -1,10 +1,7 @@ use crate::rpc::methods::*; use crate::rpc::{ codec::base::OutboundCodec, - protocol::{ - ProtocolId, RPCError, RPC_BLOCKS_BY_RANGE, RPC_BLOCKS_BY_ROOT, RPC_GOODBYE, RPC_META_DATA, - RPC_PING, RPC_STATUS, - }, + protocol::{Encoding, Protocol, ProtocolId, RPCError, Version}, }; use crate::rpc::{ErrorMessage, RPCErrorResponse, RPCRequest, RPCResponse}; use libp2p::bytes::BytesMut; @@ -34,7 +31,7 @@ impl SSZSnappyInboundCodec { pub fn new(protocol: ProtocolId, max_packet_size: usize) -> Self { let uvi_codec = Uvi::default(); // this encoding only applies to ssz_snappy. - debug_assert!(protocol.encoding.as_str() == "ssz_snappy"); + debug_assert_eq!(protocol.encoding, Encoding::SSZSnappy); SSZSnappyInboundCodec { inner: uvi_codec, @@ -122,39 +119,34 @@ impl Decoder for SSZSnappyInboundCodec { let n = reader.get_ref().position(); self.len = None; src.split_to(n as usize); - match self.protocol.message_name.as_str() { - RPC_STATUS => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Status(StatusMessage::from_ssz_bytes( + match self.protocol.message_name { + Protocol::Status => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Status(StatusMessage::from_ssz_bytes( &decoded_buffer, )?))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_GOODBYE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Goodbye(GoodbyeReason::from_ssz_bytes( - &decoded_buffer, - )?))), - _ => unreachable!("Cannot negotiate an unknown version"), + Protocol::Goodbye => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Goodbye( + GoodbyeReason::from_ssz_bytes(&decoded_buffer)?, + ))), }, - RPC_BLOCKS_BY_RANGE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::BlocksByRange( + Protocol::BlocksByRange => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::BlocksByRange( BlocksByRangeRequest::from_ssz_bytes(&decoded_buffer)?, ))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_ROOT => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::BlocksByRoot(BlocksByRootRequest { + Protocol::BlocksByRoot => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::BlocksByRoot(BlocksByRootRequest { block_roots: Vec::from_ssz_bytes(&decoded_buffer)?, }))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_PING => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCRequest::Ping(Ping::from_ssz_bytes( + Protocol::Ping => match self.protocol.version { + Version::V1 => Ok(Some(RPCRequest::Ping(Ping::from_ssz_bytes( &decoded_buffer, )?))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_META_DATA => match self.protocol.version.as_str() { - "1" => { + Protocol::MetaData => match self.protocol.version { + Version::V1 => { if decoded_buffer.len() > 0 { Err(RPCError::Custom( "Get metadata request should be empty".into(), @@ -163,9 +155,7 @@ impl Decoder for SSZSnappyInboundCodec { Ok(Some(RPCRequest::MetaData(PhantomData))) } } - _ => unreachable!("Cannot negotiate an unknown version"), }, - _ => unreachable!("Cannot negotiate an unknown protocol"), } } Err(e) => match e.kind() { @@ -194,7 +184,7 @@ impl SSZSnappyOutboundCodec { pub fn new(protocol: ProtocolId, max_packet_size: usize) -> Self { let uvi_codec = Uvi::default(); // this encoding only applies to ssz_snappy. - debug_assert!(protocol.encoding.as_str() == "ssz_snappy"); + debug_assert_eq!(protocol.encoding, Encoding::SSZSnappy); SSZSnappyOutboundCodec { inner: uvi_codec, @@ -279,41 +269,35 @@ impl Decoder for SSZSnappyOutboundCodec { let n = reader.get_ref().position(); self.len = None; src.split_to(n as usize); - match self.protocol.message_name.as_str() { - RPC_STATUS => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::Status(StatusMessage::from_ssz_bytes( - &decoded_buffer, - )?))), - _ => unreachable!("Cannot negotiate an unknown version"), + match self.protocol.message_name { + Protocol::Status => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::Status( + StatusMessage::from_ssz_bytes(&decoded_buffer)?, + ))), }, - RPC_GOODBYE => { + Protocol::Goodbye => { Err(RPCError::InvalidProtocol("GOODBYE doesn't have a response")) } - RPC_BLOCKS_BY_RANGE => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::BlocksByRange(Box::new( + Protocol::BlocksByRange => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::BlocksByRange(Box::new( SignedBeaconBlock::from_ssz_bytes(&decoded_buffer)?, )))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_BLOCKS_BY_ROOT => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::BlocksByRoot(Box::new( + Protocol::BlocksByRoot => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::BlocksByRoot(Box::new( SignedBeaconBlock::from_ssz_bytes(&decoded_buffer)?, )))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_PING => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::Pong(Ping { + Protocol::Ping => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::Pong(Ping { data: u64::from_ssz_bytes(&decoded_buffer)?, }))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - RPC_META_DATA => match self.protocol.version.as_str() { - "1" => Ok(Some(RPCResponse::MetaData(MetaData::from_ssz_bytes( + Protocol::MetaData => match self.protocol.version { + Version::V1 => Ok(Some(RPCResponse::MetaData(MetaData::from_ssz_bytes( &decoded_buffer, )?))), - _ => unreachable!("Cannot negotiate an unknown version"), }, - _ => unreachable!("Cannot negotiate an unknown protocol"), } } Err(e) => match e.kind() { diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index 058ea78e75..76567cf466 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -34,18 +34,68 @@ const TTFB_TIMEOUT: u64 = 5; const REQUEST_TIMEOUT: u64 = 15; /// Protocol names to be used. -/// The Status protocol name. -pub const RPC_STATUS: &str = "status"; -/// The Goodbye protocol name. -pub const RPC_GOODBYE: &str = "goodbye"; -/// The `BlocksByRange` protocol name. -pub const RPC_BLOCKS_BY_RANGE: &str = "beacon_blocks_by_range"; -/// The `BlocksByRoot` protocol name. -pub const RPC_BLOCKS_BY_ROOT: &str = "beacon_blocks_by_root"; -/// The `Ping` protocol name. -pub const RPC_PING: &str = "ping"; -/// The `MetaData` protocol name. -pub const RPC_META_DATA: &str = "metadata"; +#[derive(Debug, Clone)] +pub enum Protocol { + /// The Status protocol name. + Status, + /// The Goodbye protocol name. + Goodbye, + /// The `BlocksByRange` protocol name. + BlocksByRange, + /// The `BlocksByRoot` protocol name. + BlocksByRoot, + /// The `Ping` protocol name. + Ping, + /// The `MetaData` protocol name. + MetaData, +} + +/// RPC Versions +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Version { + /// Version 1 of RPC + V1, +} + +/// RPC Encondings supported. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Encoding { + SSZ, + SSZSnappy, +} + +impl std::fmt::Display for Protocol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let repr = match self { + Protocol::Status => "status", + Protocol::Goodbye => "goodbye", + Protocol::BlocksByRange => "beacon_blocks_by_range", + Protocol::BlocksByRoot => "beacon_blocks_by_root", + Protocol::Ping => "ping", + Protocol::MetaData => "metadata", + }; + f.write_str(repr) + } +} + +impl std::fmt::Display for Encoding { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let repr = match self { + Encoding::SSZ => "ssz", + Encoding::SSZSnappy => "ssz_snappy", + }; + f.write_str(repr) + } +} + +impl std::fmt::Display for Version { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let repr = match self { + Version::V1 => "1", + }; + f.write_str(repr) + } +} #[derive(Debug, Clone)] pub struct RPCProtocol { @@ -59,18 +109,18 @@ impl UpgradeInfo for RPCProtocol { /// The list of supported RPC protocols for Lighthouse. fn protocol_info(&self) -> Self::InfoIter { vec![ - ProtocolId::new(RPC_STATUS, "1", "ssz_snappy"), - ProtocolId::new(RPC_STATUS, "1", "ssz"), - ProtocolId::new(RPC_GOODBYE, "1", "ssz_snappy"), - ProtocolId::new(RPC_GOODBYE, "1", "ssz"), - ProtocolId::new(RPC_BLOCKS_BY_RANGE, "1", "ssz_snappy"), - ProtocolId::new(RPC_BLOCKS_BY_RANGE, "1", "ssz"), - ProtocolId::new(RPC_BLOCKS_BY_ROOT, "1", "ssz_snappy"), - ProtocolId::new(RPC_BLOCKS_BY_ROOT, "1", "ssz"), - ProtocolId::new(RPC_PING, "1", "ssz_snappy"), - ProtocolId::new(RPC_PING, "1", "ssz"), - ProtocolId::new(RPC_META_DATA, "1", "ssz_snappy"), - ProtocolId::new(RPC_META_DATA, "1", "ssz"), + ProtocolId::new(Protocol::Status, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Status, Version::V1, Encoding::SSZ), + ProtocolId::new(Protocol::Goodbye, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Goodbye, Version::V1, Encoding::SSZ), + ProtocolId::new(Protocol::BlocksByRange, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::BlocksByRange, Version::V1, Encoding::SSZ), + ProtocolId::new(Protocol::BlocksByRoot, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::BlocksByRoot, Version::V1, Encoding::SSZ), + ProtocolId::new(Protocol::Ping, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Ping, Version::V1, Encoding::SSZ), + ProtocolId::new(Protocol::MetaData, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::MetaData, Version::V1, Encoding::SSZ), ] } } @@ -79,13 +129,13 @@ impl UpgradeInfo for RPCProtocol { #[derive(Clone, Debug)] pub struct ProtocolId { /// The rpc message type/name. - pub message_name: String, + pub message_name: Protocol, /// The version of the RPC. - pub version: String, + pub version: Version, /// The encoding of the RPC. - pub encoding: String, + pub encoding: Encoding, /// The protocol id that is formed from the above fields. protocol_id: String, @@ -93,16 +143,16 @@ pub struct ProtocolId { /// An RPC protocol ID. impl ProtocolId { - pub fn new(message_name: &str, version: &str, encoding: &str) -> Self { + pub fn new(message_name: Protocol, version: Version, encoding: Encoding) -> Self { let protocol_id = format!( "{}/{}/{}/{}", PROTOCOL_PREFIX, message_name, version, encoding ); ProtocolId { - message_name: message_name.into(), - version: version.into(), - encoding: encoding.into(), + message_name, + version: version, + encoding, protocol_id, } } @@ -154,13 +204,13 @@ where protocol: ProtocolId, ) -> Self::Future { let protocol_name = protocol.message_name.clone(); - let codec = match protocol.encoding.as_str() { - "ssz_snappy" => { + let codec = match protocol.encoding { + Encoding::SSZSnappy => { let ssz_snappy_codec = BaseInboundCodec::new(SSZSnappyInboundCodec::new(protocol, MAX_RPC_SIZE)); InboundCodec::SSZSnappy(ssz_snappy_codec) } - "ssz" | _ => { + Encoding::SSZ => { let ssz_codec = BaseInboundCodec::new(SSZInboundCodec::new(protocol, MAX_RPC_SIZE)); InboundCodec::SSZ(ssz_codec) } @@ -171,13 +221,13 @@ where let socket = Framed::new(timed_socket, codec); // MetaData requests should be empty, return the stream - if protocol_name == RPC_META_DATA { - futures::future::Either::A(futures::future::ok(( + match protocol_name { + Protocol::MetaData => futures::future::Either::A(futures::future::ok(( RPCRequest::MetaData(PhantomData), socket, - ))) - } else { - futures::future::Either::B( + ))), + + _ => futures::future::Either::B( socket .into_future() .timeout(Duration::from_secs(REQUEST_TIMEOUT)) @@ -190,7 +240,7 @@ where )), } } as FnAndThen), - ) + ), } } } @@ -226,28 +276,28 @@ impl RPCRequest { match self { // add more protocols when versions/encodings are supported RPCRequest::Status(_) => vec![ - ProtocolId::new(RPC_STATUS, "1", "ssz_snappy"), - ProtocolId::new(RPC_STATUS, "1", "ssz"), + ProtocolId::new(Protocol::Status, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Status, Version::V1, Encoding::SSZ), ], RPCRequest::Goodbye(_) => vec![ - ProtocolId::new(RPC_GOODBYE, "1", "ssz_snappy"), - ProtocolId::new(RPC_GOODBYE, "1", "ssz"), + ProtocolId::new(Protocol::Goodbye, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Goodbye, Version::V1, Encoding::SSZ), ], RPCRequest::BlocksByRange(_) => vec![ - ProtocolId::new(RPC_BLOCKS_BY_RANGE, "1", "ssz_snappy"), - ProtocolId::new(RPC_BLOCKS_BY_RANGE, "1", "ssz"), + ProtocolId::new(Protocol::BlocksByRange, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::BlocksByRange, Version::V1, Encoding::SSZ), ], RPCRequest::BlocksByRoot(_) => vec![ - ProtocolId::new(RPC_BLOCKS_BY_ROOT, "1", "ssz_snappy"), - ProtocolId::new(RPC_BLOCKS_BY_ROOT, "1", "ssz"), + ProtocolId::new(Protocol::BlocksByRoot, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::BlocksByRoot, Version::V1, Encoding::SSZ), ], RPCRequest::Ping(_) => vec![ - ProtocolId::new(RPC_PING, "1", "ssz_snappy"), - ProtocolId::new(RPC_PING, "1", "ssz"), + ProtocolId::new(Protocol::Ping, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::Ping, Version::V1, Encoding::SSZ), ], RPCRequest::MetaData(_) => vec![ - ProtocolId::new(RPC_META_DATA, "1", "ssz_snappy"), - ProtocolId::new(RPC_META_DATA, "1", "ssz"), + ProtocolId::new(Protocol::MetaData, Version::V1, Encoding::SSZSnappy), + ProtocolId::new(Protocol::MetaData, Version::V1, Encoding::SSZ), ], } } @@ -316,13 +366,13 @@ where socket: upgrade::Negotiated, protocol: Self::Info, ) -> Self::Future { - let codec = match protocol.encoding.as_str() { - "ssz_snappy" => { + let codec = match protocol.encoding { + Encoding::SSZSnappy => { let ssz_snappy_codec = BaseOutboundCodec::new(SSZSnappyOutboundCodec::new(protocol, MAX_RPC_SIZE)); OutboundCodec::SSZSnappy(ssz_snappy_codec) } - "ssz" | _ => { + Encoding::SSZ => { let ssz_codec = BaseOutboundCodec::new(SSZOutboundCodec::new(protocol, MAX_RPC_SIZE)); OutboundCodec::SSZ(ssz_codec)