Update tokio::codec to futures_codec (#1128)

This commit is contained in:
Pawan Dhananjay
2020-05-11 17:53:53 +05:30
committed by GitHub
parent 1635ae8666
commit e57aea924a
6 changed files with 58 additions and 39 deletions

21
Cargo.lock generated
View File

@@ -1239,6 +1239,7 @@ dependencies = [
"eth2_ssz_types", "eth2_ssz_types",
"fnv", "fnv",
"futures 0.3.4", "futures 0.3.4",
"futures_codec 0.4.0",
"hashset_delay", "hashset_delay",
"hex 0.4.2", "hex 0.4.2",
"lazy_static", "lazy_static",
@@ -1259,7 +1260,6 @@ dependencies = [
"tiny-keccak 2.0.2", "tiny-keccak 2.0.2",
"tokio 0.2.20", "tokio 0.2.20",
"tokio-io-timeout", "tokio-io-timeout",
"tokio-util",
"types", "types",
"unsigned-varint 0.3.3 (git+https://github.com/sigp/unsigned-varint?branch=latest-codecs)", "unsigned-varint 0.3.3 (git+https://github.com/sigp/unsigned-varint?branch=latest-codecs)",
"version", "version",
@@ -1611,6 +1611,18 @@ dependencies = [
"pin-project", "pin-project",
] ]
[[package]]
name = "futures_codec"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe8859feb7140742ed1a2a85a07941100ad2b5f98a421b353931d718a34144d1"
dependencies = [
"bytes 0.5.4",
"futures 0.3.4",
"memchr",
"pin-project",
]
[[package]] [[package]]
name = "gcc" name = "gcc"
version = "0.3.55" version = "0.3.55"
@@ -2302,7 +2314,7 @@ dependencies = [
"bytes 0.5.4", "bytes 0.5.4",
"fnv", "fnv",
"futures 0.3.4", "futures 0.3.4",
"futures_codec", "futures_codec 0.3.4",
"libp2p-core", "libp2p-core",
"libp2p-swarm", "libp2p-swarm",
"log 0.4.8", "log 0.4.8",
@@ -2341,7 +2353,7 @@ dependencies = [
"bytes 0.5.4", "bytes 0.5.4",
"fnv", "fnv",
"futures 0.3.4", "futures 0.3.4",
"futures_codec", "futures_codec 0.3.4",
"libp2p-core", "libp2p-core",
"log 0.4.8", "log 0.4.8",
"parking_lot 0.10.2", "parking_lot 0.10.2",
@@ -4770,7 +4782,6 @@ checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499"
dependencies = [ dependencies = [
"bytes 0.5.4", "bytes 0.5.4",
"futures-core", "futures-core",
"futures-io",
"futures-sink", "futures-sink",
"log 0.4.8", "log 0.4.8",
"pin-project-lite", "pin-project-lite",
@@ -4990,7 +5001,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f67332660eb59a6f1eb24ff1220c9e8d01738a8503c6002e30bcfe4bd9f2b4a9" checksum = "f67332660eb59a6f1eb24ff1220c9e8d01738a8503c6002e30bcfe4bd9f2b4a9"
dependencies = [ dependencies = [
"bytes 0.5.4", "bytes 0.5.4",
"futures_codec", "futures_codec 0.3.4",
] ]
[[package]] [[package]]

View File

@@ -31,9 +31,9 @@ base64 = "0.12.0"
snap = "1.0.0" snap = "1.0.0"
void = "1.0.2" void = "1.0.2"
tokio-io-timeout = "0.4.0" tokio-io-timeout = "0.4.0"
tokio-util = { version = "0.3.1", features = ["codec", "compat"] }
discv5 = "0.1.0-alpha.1" discv5 = "0.1.0-alpha.1"
tiny-keccak = "2.0.2" tiny-keccak = "2.0.2"
futures_codec = "0.4.0"
[dependencies.libp2p] [dependencies.libp2p]
version = "0.18.1" version = "0.18.1"

View File

@@ -4,10 +4,10 @@ use crate::rpc::{ErrorMessage, RPCCodedResponse, RPCRequest, RPCResponse};
use libp2p::bytes::BufMut; use libp2p::bytes::BufMut;
use libp2p::bytes::BytesMut; use libp2p::bytes::BytesMut;
use std::marker::PhantomData; use std::marker::PhantomData;
use tokio_util::codec::{Decoder, Encoder}; use futures_codec::{Decoder, Encoder};
use types::EthSpec; use types::EthSpec;
pub trait OutboundCodec<TItem>: Encoder<TItem> + Decoder { pub trait OutboundCodec: Encoder + Decoder {
type ErrorType; type ErrorType;
fn decode_error( fn decode_error(
@@ -21,7 +21,7 @@ pub trait OutboundCodec<TItem>: Encoder<TItem> + Decoder {
pub struct BaseInboundCodec<TCodec, TSpec> pub struct BaseInboundCodec<TCodec, TSpec>
where where
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder, TCodec: Encoder + Decoder,
TSpec: EthSpec, TSpec: EthSpec,
{ {
/// Inner codec for handling various encodings /// Inner codec for handling various encodings
@@ -31,7 +31,7 @@ where
impl<TCodec, TSpec> BaseInboundCodec<TCodec, TSpec> impl<TCodec, TSpec> BaseInboundCodec<TCodec, TSpec>
where where
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder, TCodec: Encoder + Decoder,
TSpec: EthSpec, TSpec: EthSpec,
{ {
pub fn new(codec: TCodec) -> Self { pub fn new(codec: TCodec) -> Self {
@@ -46,7 +46,7 @@ where
// This deals with Decoding RPC Responses from other peers and encoding our requests // This deals with Decoding RPC Responses from other peers and encoding our requests
pub struct BaseOutboundCodec<TOutboundCodec, TSpec> pub struct BaseOutboundCodec<TOutboundCodec, TSpec>
where where
TOutboundCodec: OutboundCodec<RPCRequest<TSpec>>, TOutboundCodec: OutboundCodec,
TSpec: EthSpec, TSpec: EthSpec,
{ {
/// Inner codec for handling various encodings. /// Inner codec for handling various encodings.
@@ -59,7 +59,7 @@ where
impl<TOutboundCodec, TSpec> BaseOutboundCodec<TOutboundCodec, TSpec> impl<TOutboundCodec, TSpec> BaseOutboundCodec<TOutboundCodec, TSpec>
where where
TSpec: EthSpec, TSpec: EthSpec,
TOutboundCodec: OutboundCodec<RPCRequest<TSpec>>, TOutboundCodec: OutboundCodec,
{ {
pub fn new(codec: TOutboundCodec) -> Self { pub fn new(codec: TOutboundCodec) -> Self {
BaseOutboundCodec { BaseOutboundCodec {
@@ -75,16 +75,17 @@ where
/* Base Inbound Codec */ /* Base Inbound Codec */
// This Encodes RPC Responses sent to external peers // This Encodes RPC Responses sent to external peers
impl<TCodec, TSpec> Encoder<RPCCodedResponse<TSpec>> for BaseInboundCodec<TCodec, TSpec> impl<TCodec, TSpec> Encoder for BaseInboundCodec<TCodec, TSpec>
where where
TSpec: EthSpec, TSpec: EthSpec,
TCodec: Decoder + Encoder<RPCCodedResponse<TSpec>>, TCodec: Decoder + Encoder,
{ {
type Error = <TCodec as Encoder<RPCCodedResponse<TSpec>>>::Error; type Item = RPCCodedResponse<TSpec>;
type Error = <TCodec as Encoder>::Error;
fn encode( fn encode(
&mut self, &mut self,
item: RPCCodedResponse<TSpec>, item: Self::Item,
dst: &mut BytesMut, dst: &mut BytesMut,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
dst.clear(); dst.clear();
@@ -101,7 +102,7 @@ where
impl<TCodec, TSpec> Decoder for BaseInboundCodec<TCodec, TSpec> impl<TCodec, TSpec> Decoder for BaseInboundCodec<TCodec, TSpec>
where where
TSpec: EthSpec, TSpec: EthSpec,
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder<Item = RPCRequest<TSpec>>, TCodec: Encoder + Decoder<Item = RPCRequest<TSpec>>,
{ {
type Item = RPCRequest<TSpec>; type Item = RPCRequest<TSpec>;
type Error = <TCodec as Decoder>::Error; type Error = <TCodec as Decoder>::Error;
@@ -114,14 +115,15 @@ where
/* Base Outbound Codec */ /* Base Outbound Codec */
// This Encodes RPC Requests sent to external peers // This Encodes RPC Requests sent to external peers
impl<TCodec, TSpec> Encoder<RPCRequest<TSpec>> for BaseOutboundCodec<TCodec, TSpec> impl<TCodec, TSpec> Encoder for BaseOutboundCodec<TCodec, TSpec>
where where
TSpec: EthSpec, TSpec: EthSpec,
TCodec: OutboundCodec<RPCRequest<TSpec>> + Encoder<RPCRequest<TSpec>>, TCodec: OutboundCodec + Encoder<Item = RPCRequest<TSpec>>,
{ {
type Error = <TCodec as Encoder<RPCRequest<TSpec>>>::Error; type Item = RPCRequest<TSpec>;
type Error = <TCodec as Encoder>::Error;
fn encode(&mut self, item: RPCRequest<TSpec>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.inner.encode(item, dst) self.inner.encode(item, dst)
} }
} }
@@ -130,7 +132,7 @@ where
impl<TCodec, TSpec> Decoder for BaseOutboundCodec<TCodec, TSpec> impl<TCodec, TSpec> Decoder for BaseOutboundCodec<TCodec, TSpec>
where where
TSpec: EthSpec, TSpec: EthSpec,
TCodec: OutboundCodec<RPCRequest<TSpec>, ErrorType = ErrorMessage> TCodec: OutboundCodec<ErrorType = ErrorMessage>
+ Decoder<Item = RPCResponse<TSpec>>, + Decoder<Item = RPCResponse<TSpec>>,
{ {
type Item = RPCCodedResponse<TSpec>; type Item = RPCCodedResponse<TSpec>;

View File

@@ -8,7 +8,7 @@ use self::ssz_snappy::{SSZSnappyInboundCodec, SSZSnappyOutboundCodec};
use crate::rpc::protocol::RPCError; use crate::rpc::protocol::RPCError;
use crate::rpc::{RPCCodedResponse, RPCRequest}; use crate::rpc::{RPCCodedResponse, RPCRequest};
use libp2p::bytes::BytesMut; use libp2p::bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder}; use futures_codec::{Encoder, Decoder};
use types::EthSpec; use types::EthSpec;
// Known types of codecs // Known types of codecs
@@ -22,10 +22,11 @@ pub enum OutboundCodec<TSpec: EthSpec> {
SSZ(BaseOutboundCodec<SSZOutboundCodec<TSpec>, TSpec>), SSZ(BaseOutboundCodec<SSZOutboundCodec<TSpec>, TSpec>),
} }
impl<T: EthSpec> Encoder<RPCCodedResponse<T>> for InboundCodec<T> { impl<T: EthSpec> Encoder for InboundCodec<T> {
type Item = RPCCodedResponse<T>;
type Error = RPCError; type Error = RPCError;
fn encode(&mut self, item: RPCCodedResponse<T>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
match self { match self {
InboundCodec::SSZ(codec) => codec.encode(item, dst), InboundCodec::SSZ(codec) => codec.encode(item, dst),
InboundCodec::SSZSnappy(codec) => codec.encode(item, dst), InboundCodec::SSZSnappy(codec) => codec.encode(item, dst),
@@ -45,10 +46,11 @@ impl<TSpec: EthSpec> Decoder for InboundCodec<TSpec> {
} }
} }
impl<TSpec: EthSpec> Encoder<RPCRequest<TSpec>> for OutboundCodec<TSpec> { impl<TSpec: EthSpec> Encoder for OutboundCodec<TSpec> {
type Item = RPCRequest<TSpec>;
type Error = RPCError; type Error = RPCError;
fn encode(&mut self, item: RPCRequest<TSpec>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
match self { match self {
OutboundCodec::SSZ(codec) => codec.encode(item, dst), OutboundCodec::SSZ(codec) => codec.encode(item, dst),
OutboundCodec::SSZSnappy(codec) => codec.encode(item, dst), OutboundCodec::SSZSnappy(codec) => codec.encode(item, dst),

View File

@@ -7,7 +7,7 @@ use crate::rpc::{ErrorMessage, RPCCodedResponse, RPCRequest, RPCResponse};
use libp2p::bytes::{BufMut, Bytes, BytesMut}; use libp2p::bytes::{BufMut, Bytes, BytesMut};
use ssz::{Decode, Encode}; use ssz::{Decode, Encode};
use std::marker::PhantomData; use std::marker::PhantomData;
use tokio_util::codec::{Decoder, Encoder}; use futures_codec::{Decoder, Encoder};
use types::{EthSpec, SignedBeaconBlock}; use types::{EthSpec, SignedBeaconBlock};
use unsigned_varint::codec::UviBytes; use unsigned_varint::codec::UviBytes;
@@ -36,12 +36,13 @@ impl<TSpec: EthSpec> SSZInboundCodec<TSpec> {
} }
// Encoder for inbound streams: Encodes RPC Responses sent to peers. // Encoder for inbound streams: Encodes RPC Responses sent to peers.
impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZInboundCodec<TSpec> { impl<TSpec: EthSpec> Encoder for SSZInboundCodec<TSpec> {
type Item = RPCCodedResponse<TSpec>;
type Error = RPCError; type Error = RPCError;
fn encode( fn encode(
&mut self, &mut self,
item: RPCCodedResponse<TSpec>, item: Self::Item,
dst: &mut BytesMut, dst: &mut BytesMut,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
let bytes = match item { let bytes = match item {
@@ -148,10 +149,11 @@ impl<TSpec: EthSpec> SSZOutboundCodec<TSpec> {
} }
// Encoder for outbound streams: Encodes RPC Requests to peers // Encoder for outbound streams: Encodes RPC Requests to peers
impl<TSpec: EthSpec> Encoder<RPCRequest<TSpec>> for SSZOutboundCodec<TSpec> { impl<TSpec: EthSpec> Encoder for SSZOutboundCodec<TSpec> {
type Item = RPCRequest<TSpec>;
type Error = RPCError; type Error = RPCError;
fn encode(&mut self, item: RPCRequest<TSpec>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
let bytes = match item { let bytes = match item {
RPCRequest::Status(req) => req.as_ssz_bytes(), RPCRequest::Status(req) => req.as_ssz_bytes(),
RPCRequest::Goodbye(req) => req.as_ssz_bytes(), RPCRequest::Goodbye(req) => req.as_ssz_bytes(),
@@ -241,7 +243,7 @@ impl<TSpec: EthSpec> Decoder for SSZOutboundCodec<TSpec> {
} }
} }
impl<TSpec: EthSpec> OutboundCodec<RPCRequest<TSpec>> for SSZOutboundCodec<TSpec> { impl<TSpec: EthSpec> OutboundCodec for SSZOutboundCodec<TSpec> {
type ErrorType = ErrorMessage; type ErrorType = ErrorMessage;
fn decode_error(&mut self, src: &mut BytesMut) -> Result<Option<Self::ErrorType>, RPCError> { fn decode_error(&mut self, src: &mut BytesMut) -> Result<Option<Self::ErrorType>, RPCError> {

View File

@@ -12,7 +12,7 @@ use std::io::Cursor;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
use tokio_util::codec::{Decoder, Encoder}; use futures_codec::{Decoder, Encoder};
use types::{EthSpec, SignedBeaconBlock}; use types::{EthSpec, SignedBeaconBlock};
use unsigned_varint::codec::Uvi; use unsigned_varint::codec::Uvi;
@@ -44,12 +44,13 @@ impl<T: EthSpec> SSZSnappyInboundCodec<T> {
} }
// Encoder for inbound streams: Encodes RPC Responses sent to peers. // Encoder for inbound streams: Encodes RPC Responses sent to peers.
impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZSnappyInboundCodec<TSpec> { impl<TSpec: EthSpec> Encoder for SSZSnappyInboundCodec<TSpec> {
type Item = RPCCodedResponse<TSpec>;
type Error = RPCError; type Error = RPCError;
fn encode( fn encode(
&mut self, &mut self,
item: RPCCodedResponse<TSpec>, item: Self::Item,
dst: &mut BytesMut, dst: &mut BytesMut,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
let bytes = match item { let bytes = match item {
@@ -196,10 +197,11 @@ impl<TSpec: EthSpec> SSZSnappyOutboundCodec<TSpec> {
} }
// Encoder for outbound streams: Encodes RPC Requests to peers // Encoder for outbound streams: Encodes RPC Requests to peers
impl<TSpec: EthSpec> Encoder<RPCRequest<TSpec>> for SSZSnappyOutboundCodec<TSpec> { impl<TSpec: EthSpec> Encoder for SSZSnappyOutboundCodec<TSpec> {
type Item = RPCRequest<TSpec>;
type Error = RPCError; type Error = RPCError;
fn encode(&mut self, item: RPCRequest<TSpec>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
let bytes = match item { let bytes = match item {
RPCRequest::Status(req) => req.as_ssz_bytes(), RPCRequest::Status(req) => req.as_ssz_bytes(),
RPCRequest::Goodbye(req) => req.as_ssz_bytes(), RPCRequest::Goodbye(req) => req.as_ssz_bytes(),
@@ -309,7 +311,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
} }
} }
impl<TSpec: EthSpec> OutboundCodec<RPCRequest<TSpec>> for SSZSnappyOutboundCodec<TSpec> { impl<TSpec: EthSpec> OutboundCodec for SSZSnappyOutboundCodec<TSpec> {
type ErrorType = ErrorMessage; type ErrorType = ErrorMessage;
fn decode_error(&mut self, src: &mut BytesMut) -> Result<Option<Self::ErrorType>, RPCError> { fn decode_error(&mut self, src: &mut BytesMut) -> Result<Option<Self::ErrorType>, RPCError> {