Shift changes into message handler and simple sync for rpc-rewrite

This commit is contained in:
Age Manning
2019-07-16 22:32:37 +10:00
parent 704263e35f
commit 414d41cb57
11 changed files with 486 additions and 227 deletions

View File

@@ -5,7 +5,7 @@ use bytes::BufMut;
use bytes::BytesMut;
use tokio::codec::{Decoder, Encoder};
pub(crate) trait OutboundCodec: Encoder + Decoder {
pub trait OutboundCodec: Encoder + Decoder {
type ErrorType;
fn decode_error(
@@ -14,7 +14,7 @@ pub(crate) trait OutboundCodec: Encoder + Decoder {
) -> Result<Option<Self::ErrorType>, <Self as Decoder>::Error>;
}
pub(crate) struct BaseInboundCodec<TCodec>
pub struct BaseInboundCodec<TCodec>
where
TCodec: Encoder + Decoder,
{
@@ -31,7 +31,7 @@ where
}
}
pub(crate) struct BaseOutboundCodec<TOutboundCodec>
pub struct BaseOutboundCodec<TOutboundCodec>
where
TOutboundCodec: OutboundCodec,
{
@@ -109,7 +109,7 @@ where
debug_assert!(!src.is_empty());
let resp_byte = src.split_to(1);
let resp_code_byte = [0; 1];
let mut resp_code_byte = [0; 1];
resp_code_byte.copy_from_slice(&resp_byte);
let resp_code = u8::from_be_bytes(resp_code_byte);

View File

@@ -18,7 +18,7 @@ pub struct SSZInboundCodec {
impl SSZInboundCodec {
pub fn new(protocol: ProtocolId, max_packet_size: usize) -> Self {
let uvi_codec = UviBytes::default();
let mut uvi_codec = UviBytes::default();
uvi_codec.set_max_len(max_packet_size);
// this encoding only applies to ssz.
@@ -41,7 +41,6 @@ impl Encoder for SSZInboundCodec {
RPCErrorResponse::Success(resp) => {
match resp {
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
@@ -80,7 +79,9 @@ impl Decoder for SSZInboundCodec {
_ => Err(RPCError::InvalidProtocol("Unknown HELLO version")),
},
"goodbye" => match self.protocol.version.as_str() {
"1.0.0" => Ok(Some(RPCRequest::Goodbye(Goodbye::from_ssz_bytes(&packet)?))),
"1.0.0" => Ok(Some(RPCRequest::Goodbye(GoodbyeReason::from_ssz_bytes(
&packet,
)?))),
_ => Err(RPCError::InvalidProtocol(
"Unknown GOODBYE version.as_str()",
)),
@@ -117,6 +118,7 @@ impl Decoder for SSZInboundCodec {
"Unknown BEACON_CHAIN_STATE version.",
)),
},
_ => Err(RPCError::InvalidProtocol("Unknown message name.")),
},
Ok(None) => Ok(None),
Err(e) => Err(e),
@@ -133,7 +135,7 @@ pub struct SSZOutboundCodec {
impl SSZOutboundCodec {
pub fn new(protocol: ProtocolId, max_packet_size: usize) -> Self {
let uvi_codec = UviBytes::default();
let mut uvi_codec = UviBytes::default();
uvi_codec.set_max_len(max_packet_size);
// this encoding only applies to ssz.
@@ -204,6 +206,8 @@ impl Decoder for SSZOutboundCodec {
"1.0.0" => Ok(Some(RPCResponse::BeaconBlockBodies(
BeaconBlockBodiesResponse {
block_bodies: packet.to_vec(),
// this gets filled in the protocol handler
block_roots: None,
},
))),
_ => Err(RPCError::InvalidProtocol(