From d35a84c305fae9b88e5e6dc4d3365e9a88804b5f Mon Sep 17 00:00:00 2001 From: driftluo Date: Thu, 21 Jan 2021 11:25:49 +0800 Subject: [PATCH] chore: protocol upgrade --- multiaddr/Cargo.toml | 4 +- protocols/discovery/Cargo.toml | 23 +- protocols/discovery/src/lib.rs | 10 - protocols/discovery/src/protocol.fbs | 29 - protocols/discovery/src/protocol.rs | 109 ---- protocols/discovery/src/protocol_generated.rs | 537 ------------------ .../src/protocol_generated_verifier.rs | 480 ---------------- protocols/identify/Cargo.toml | 19 +- protocols/identify/src/lib.rs | 12 - protocols/identify/src/protocol.fbs | 14 - protocols/identify/src/protocol.rs | 83 --- protocols/identify/src/protocol_generated.rs | 206 ------- .../src/protocol_generated_verifier.rs | 225 -------- protocols/ping/Cargo.toml | 19 +- protocols/ping/src/lib.rs | 69 --- protocols/ping/src/protocol.fbs | 18 - protocols/ping/src/protocol_generated.rs | 349 ------------ .../ping/src/protocol_generated_verifier.rs | 275 --------- secio/Cargo.toml | 2 +- tentacle/tests/test_large_pending_messge.rs | 6 +- yamux/src/session.rs | 2 +- yamux/src/stream.rs | 2 +- 22 files changed, 25 insertions(+), 2468 deletions(-) delete mode 100644 protocols/discovery/src/protocol.fbs delete mode 100644 protocols/discovery/src/protocol_generated.rs delete mode 100644 protocols/discovery/src/protocol_generated_verifier.rs delete mode 100644 protocols/identify/src/protocol.fbs delete mode 100644 protocols/identify/src/protocol_generated.rs delete mode 100644 protocols/identify/src/protocol_generated_verifier.rs delete mode 100644 protocols/ping/src/protocol.fbs delete mode 100644 protocols/ping/src/protocol_generated.rs delete mode 100644 protocols/ping/src/protocol_generated_verifier.rs diff --git a/multiaddr/Cargo.toml b/multiaddr/Cargo.toml index 0ab97e44..87dae687 100644 --- a/multiaddr/Cargo.toml +++ b/multiaddr/Cargo.toml @@ -7,8 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -unsigned-varint = "0.3" -bytes = "0.5" +unsigned-varint = "0.6" +bytes = "1.0" bs58 = "0.3.0" sha2 = "0.9.0" serde = "1" diff --git a/protocols/discovery/Cargo.toml b/protocols/discovery/Cargo.toml index a8c74515..af476cc0 100644 --- a/protocols/discovery/Cargo.toml +++ b/protocols/discovery/Cargo.toml @@ -10,29 +10,20 @@ categories = ["network-programming", "asynchronous"] edition = "2018" [package.metadata.docs.rs] -features = [ "molc" ] +features = [] all-features = false no-default-features = true [dependencies] -p2p = { path = "../..", version = "0.3.0", package = "tentacle" } -bytes = "0.5.0" +p2p = { path = "../../tentacle", version = "0.3.0", package = "tentacle" } +bytes = "1.0" futures = { version = "0.3.0" } -tokio = { version = "0.2.0", features = ["time", "io-util", "tcp", "dns", "stream"] } -tokio-util = { version = "0.3.0", features = ["codec"] } +tokio = { version = "1.0.0", features = ["time", "io-util", "net"] } +tokio-util = { version = "0.6.0", features = ["codec"] } log = "0.4" -rand = "0.6.1" +rand = "0.7" bloom-filters = "0.1" -flatbuffers = { version = "0.6.0", optional = true } -flatbuffers-verifier = { version = "0.2.0", optional = true } -molecule = { version = "0.5.0", optional = true } +molecule = { version = "0.7.0" } [dev-dependencies] env_logger = "0.6" - -[features] -default = [] -# use flatbuffer to handshake -flatc = [ "flatbuffers", "flatbuffers-verifier", "p2p/flatc" ] -# use molecule to handshake -molc = [ "molecule", "p2p/molc" ] diff --git a/protocols/discovery/src/lib.rs b/protocols/discovery/src/lib.rs index 20afb510..c672b4b8 100644 --- a/protocols/discovery/src/lib.rs +++ b/protocols/discovery/src/lib.rs @@ -20,16 +20,6 @@ mod addr; mod protocol; mod state; -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -mod protocol_generated; -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -#[allow(dead_code)] -mod protocol_generated_verifier; -#[cfg(feature = "molc")] #[rustfmt::skip] #[allow(clippy::all)] #[allow(dead_code)] diff --git a/protocols/discovery/src/protocol.fbs b/protocols/discovery/src/protocol.fbs deleted file mode 100644 index 4d3ee5e8..00000000 --- a/protocols/discovery/src/protocol.fbs +++ /dev/null @@ -1,29 +0,0 @@ -namespace P2P.Discovery; - -union DiscoveryPayload { - GetNodes, - Nodes, -} - -table DiscoveryMessage { - payload: DiscoveryPayload; -} - -table GetNodes { - version: uint32; - count: uint32; - listen_port: ushort; -} - -table Nodes { - announce: bool; - items: [Node]; -} - -table Node { - addresses: [Bytes]; -} - -table Bytes { - seq: [ubyte]; -} \ No newline at end of file diff --git a/protocols/discovery/src/protocol.rs b/protocols/discovery/src/protocol.rs index 9a4d130d..8d530d85 100644 --- a/protocols/discovery/src/protocol.rs +++ b/protocols/discovery/src/protocol.rs @@ -1,8 +1,3 @@ -#[cfg(all(feature = "flatc", feature = "molc"))] -compile_error!("features `flatc` and `molc` are mutually exclusive"); -#[cfg(all(not(feature = "flatc"), not(feature = "molc")))] -compile_error!("Please choose a serialization format via feature. Possible choices: flatc, molc"); - use std::convert::TryFrom; use bytes::{Bytes, BytesMut}; @@ -11,15 +6,7 @@ use p2p::multiaddr::Multiaddr; use tokio_util::codec::length_delimited::LengthDelimitedCodec; use tokio_util::codec::{Decoder, Encoder}; -#[cfg(feature = "flatc")] -use crate::protocol_generated::p2p::discovery::{ - BytesBuilder, DiscoveryMessage as FbsDiscoveryMessage, DiscoveryMessageBuilder, - DiscoveryPayload as FbsDiscoveryPayload, GetNodes as FbsGetNodes, GetNodesBuilder, NodeBuilder, - Nodes as FbsNodes, NodesBuilder, -}; -#[cfg(feature = "molc")] use crate::protocol_mol; -#[cfg(feature = "molc")] use molecule::prelude::{Builder, Entity, Reader}; pub(crate) fn encode(data: DiscoveryMessage) -> Bytes { @@ -58,101 +45,6 @@ pub enum DiscoveryMessage { } impl DiscoveryMessage { - #[cfg(feature = "flatc")] - pub fn encode(&self) -> Bytes { - let mut fbb = flatbuffers::FlatBufferBuilder::new(); - let offset = match self { - DiscoveryMessage::GetNodes { - version, - count, - listen_port, - } => { - let mut get_nodes_builder = GetNodesBuilder::new(&mut fbb); - get_nodes_builder.add_version(*version); - get_nodes_builder.add_count(*count); - get_nodes_builder.add_listen_port(listen_port.unwrap_or(0)); - - let get_nodes = get_nodes_builder.finish(); - - let mut builder = DiscoveryMessageBuilder::new(&mut fbb); - builder.add_payload_type(FbsDiscoveryPayload::GetNodes); - builder.add_payload(get_nodes.as_union_value()); - builder.finish() - } - DiscoveryMessage::Nodes(Nodes { announce, items }) => { - let mut vec_items = Vec::new(); - for item in items { - let mut vec_addrs = Vec::new(); - for address in &item.addresses { - let seq = fbb.create_vector(address.as_ref()); - let mut bytes_builder = BytesBuilder::new(&mut fbb); - bytes_builder.add_seq(seq); - vec_addrs.push(bytes_builder.finish()); - } - let fbs_addrs = fbb.create_vector(&vec_addrs); - let mut node_builder = NodeBuilder::new(&mut fbb); - node_builder.add_addresses(fbs_addrs); - vec_items.push(node_builder.finish()); - } - let fbs_items = fbb.create_vector(&vec_items); - let mut nodes_builder = NodesBuilder::new(&mut fbb); - nodes_builder.add_announce(*announce); - nodes_builder.add_items(fbs_items); - let nodes = nodes_builder.finish(); - - let mut builder = DiscoveryMessageBuilder::new(&mut fbb); - builder.add_payload_type(FbsDiscoveryPayload::Nodes); - builder.add_payload(nodes.as_union_value()); - builder.finish() - } - }; - fbb.finish(offset, None); - Bytes::from(fbb.finished_data().to_owned()) - } - - #[cfg(feature = "flatc")] - pub fn decode(data: &[u8]) -> Option { - let fbs_message = flatbuffers_verifier::get_root::(data).ok()?; - let payload = fbs_message.payload()?; - match fbs_message.payload_type() { - FbsDiscoveryPayload::GetNodes => { - let fbs_get_nodes = FbsGetNodes::init_from_table(payload); - let listen_port = if fbs_get_nodes.listen_port() == 0 { - None - } else { - Some(fbs_get_nodes.listen_port()) - }; - Some(DiscoveryMessage::GetNodes { - version: fbs_get_nodes.version(), - count: fbs_get_nodes.count(), - listen_port, - }) - } - FbsDiscoveryPayload::Nodes => { - let fbs_nodes = FbsNodes::init_from_table(payload); - let fbs_items = fbs_nodes.items()?; - let mut items = Vec::new(); - for i in 0..fbs_items.len() { - let fbs_node = fbs_items.get(i); - let fbs_addresses = fbs_node.addresses()?; - let mut addresses = Vec::new(); - for j in 0..fbs_addresses.len() { - let address = fbs_addresses.get(j); - let multiaddr = Multiaddr::try_from(address.seq()?.to_vec()).ok()?; - addresses.push(multiaddr); - } - items.push(Node { addresses }); - } - Some(DiscoveryMessage::Nodes(Nodes { - announce: fbs_nodes.announce(), - items, - })) - } - _ => None, - } - } - - #[cfg(feature = "molc")] pub fn encode(self) -> Bytes { let playload = match self { DiscoveryMessage::GetNodes { @@ -230,7 +122,6 @@ impl DiscoveryMessage { .as_bytes() } - #[cfg(feature = "molc")] #[allow(clippy::cast_ptr_alignment)] pub fn decode(data: &[u8]) -> Option { let reader = protocol_mol::DiscoveryMessageReader::from_compatible_slice(data).ok()?; diff --git a/protocols/discovery/src/protocol_generated.rs b/protocols/discovery/src/protocol_generated.rs deleted file mode 100644 index 7ea73154..00000000 --- a/protocols/discovery/src/protocol_generated.rs +++ /dev/null @@ -1,537 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - -pub mod p2p { - #![allow(dead_code)] - #![allow(unused_imports)] - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; -pub mod discovery { - #![allow(dead_code)] - #![allow(unused_imports)] - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; - -#[allow(non_camel_case_types)] -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Debug)] -pub enum DiscoveryPayload { - NONE = 0, - GetNodes = 1, - Nodes = 2, - -} - -const ENUM_MIN_DISCOVERY_PAYLOAD: u8 = 0; -const ENUM_MAX_DISCOVERY_PAYLOAD: u8 = 2; - -impl<'a> flatbuffers::Follow<'a> for DiscoveryPayload { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::read_scalar_at::(buf, loc) - } -} - -impl flatbuffers::EndianScalar for DiscoveryPayload { - #[inline] - fn to_little_endian(self) -> Self { - let n = u8::to_le(self as u8); - let p = &n as *const u8 as *const DiscoveryPayload; - unsafe { *p } - } - #[inline] - fn from_little_endian(self) -> Self { - let n = u8::from_le(self as u8); - let p = &n as *const u8 as *const DiscoveryPayload; - unsafe { *p } - } -} - -impl flatbuffers::Push for DiscoveryPayload { - type Output = DiscoveryPayload; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - flatbuffers::emplace_scalar::(dst, *self); - } -} - -#[allow(non_camel_case_types)] -const ENUM_VALUES_DISCOVERY_PAYLOAD:[DiscoveryPayload; 3] = [ - DiscoveryPayload::NONE, - DiscoveryPayload::GetNodes, - DiscoveryPayload::Nodes -]; - -#[allow(non_camel_case_types)] -const ENUM_NAMES_DISCOVERY_PAYLOAD:[&'static str; 3] = [ - "NONE", - "GetNodes", - "Nodes" -]; - -pub fn enum_name_discovery_payload(e: DiscoveryPayload) -> &'static str { - let index: usize = e as usize; - ENUM_NAMES_DISCOVERY_PAYLOAD[index] -} - -pub struct DiscoveryPayloadUnionTableOffset {} -pub enum DiscoveryMessageOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct DiscoveryMessage<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for DiscoveryMessage<'a> { - type Inner = DiscoveryMessage<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> DiscoveryMessage<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - DiscoveryMessage { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args DiscoveryMessageArgs) -> flatbuffers::WIPOffset> { - let mut builder = DiscoveryMessageBuilder::new(_fbb); - if let Some(x) = args.payload { builder.add_payload(x); } - builder.add_payload_type(args.payload_type); - builder.finish() - } - - pub const VT_PAYLOAD_TYPE: flatbuffers::VOffsetT = 4; - pub const VT_PAYLOAD: flatbuffers::VOffsetT = 6; - - #[inline] - pub fn payload_type(&self) -> DiscoveryPayload { - self._tab.get::(DiscoveryMessage::VT_PAYLOAD_TYPE, Some(DiscoveryPayload::NONE)).unwrap() - } - #[inline] - pub fn payload(&self) -> Option> { - self._tab.get::>>(DiscoveryMessage::VT_PAYLOAD, None) - } - #[inline] - #[allow(non_snake_case)] - pub fn payload_as_get_nodes(&'a self) -> Option { - if self.payload_type() == DiscoveryPayload::GetNodes { - self.payload().map(|u| GetNodes::init_from_table(u)) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn payload_as_nodes(&'a self) -> Option { - if self.payload_type() == DiscoveryPayload::Nodes { - self.payload().map(|u| Nodes::init_from_table(u)) - } else { - None - } - } - -} - -pub struct DiscoveryMessageArgs { - pub payload_type: DiscoveryPayload, - pub payload: Option>, -} -impl<'a> Default for DiscoveryMessageArgs { - #[inline] - fn default() -> Self { - DiscoveryMessageArgs { - payload_type: DiscoveryPayload::NONE, - payload: None, - } - } -} -pub struct DiscoveryMessageBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> DiscoveryMessageBuilder<'a, 'b> { - #[inline] - pub fn add_payload_type(&mut self, payload_type: DiscoveryPayload) { - self.fbb_.push_slot::(DiscoveryMessage::VT_PAYLOAD_TYPE, payload_type, DiscoveryPayload::NONE); - } - #[inline] - pub fn add_payload(&mut self, payload: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(DiscoveryMessage::VT_PAYLOAD, payload); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> DiscoveryMessageBuilder<'a, 'b> { - let start = _fbb.start_table(); - DiscoveryMessageBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum GetNodesOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct GetNodes<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for GetNodes<'a> { - type Inner = GetNodes<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> GetNodes<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - GetNodes { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args GetNodesArgs) -> flatbuffers::WIPOffset> { - let mut builder = GetNodesBuilder::new(_fbb); - builder.add_count(args.count); - builder.add_version(args.version); - builder.add_listen_port(args.listen_port); - builder.finish() - } - - pub const VT_VERSION: flatbuffers::VOffsetT = 4; - pub const VT_COUNT: flatbuffers::VOffsetT = 6; - pub const VT_LISTEN_PORT: flatbuffers::VOffsetT = 8; - - #[inline] - pub fn version(&self) -> u32 { - self._tab.get::(GetNodes::VT_VERSION, Some(0)).unwrap() - } - #[inline] - pub fn count(&self) -> u32 { - self._tab.get::(GetNodes::VT_COUNT, Some(0)).unwrap() - } - #[inline] - pub fn listen_port(&self) -> u16 { - self._tab.get::(GetNodes::VT_LISTEN_PORT, Some(0)).unwrap() - } -} - -pub struct GetNodesArgs { - pub version: u32, - pub count: u32, - pub listen_port: u16, -} -impl<'a> Default for GetNodesArgs { - #[inline] - fn default() -> Self { - GetNodesArgs { - version: 0, - count: 0, - listen_port: 0, - } - } -} -pub struct GetNodesBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> GetNodesBuilder<'a, 'b> { - #[inline] - pub fn add_version(&mut self, version: u32) { - self.fbb_.push_slot::(GetNodes::VT_VERSION, version, 0); - } - #[inline] - pub fn add_count(&mut self, count: u32) { - self.fbb_.push_slot::(GetNodes::VT_COUNT, count, 0); - } - #[inline] - pub fn add_listen_port(&mut self, listen_port: u16) { - self.fbb_.push_slot::(GetNodes::VT_LISTEN_PORT, listen_port, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> GetNodesBuilder<'a, 'b> { - let start = _fbb.start_table(); - GetNodesBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum NodesOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Nodes<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Nodes<'a> { - type Inner = Nodes<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Nodes<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Nodes { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args NodesArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = NodesBuilder::new(_fbb); - if let Some(x) = args.items { builder.add_items(x); } - builder.add_announce(args.announce); - builder.finish() - } - - pub const VT_ANNOUNCE: flatbuffers::VOffsetT = 4; - pub const VT_ITEMS: flatbuffers::VOffsetT = 6; - - #[inline] - pub fn announce(&self) -> bool { - self._tab.get::(Nodes::VT_ANNOUNCE, Some(false)).unwrap() - } - #[inline] - pub fn items(&self) -> Option>>> { - self._tab.get::>>>>(Nodes::VT_ITEMS, None) - } -} - -pub struct NodesArgs<'a> { - pub announce: bool, - pub items: Option>>>>, -} -impl<'a> Default for NodesArgs<'a> { - #[inline] - fn default() -> Self { - NodesArgs { - announce: false, - items: None, - } - } -} -pub struct NodesBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> NodesBuilder<'a, 'b> { - #[inline] - pub fn add_announce(&mut self, announce: bool) { - self.fbb_.push_slot::(Nodes::VT_ANNOUNCE, announce, false); - } - #[inline] - pub fn add_items(&mut self, items: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Nodes::VT_ITEMS, items); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> NodesBuilder<'a, 'b> { - let start = _fbb.start_table(); - NodesBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum NodeOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Node<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Node<'a> { - type Inner = Node<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Node<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Node { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args NodeArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = NodeBuilder::new(_fbb); - if let Some(x) = args.addresses { builder.add_addresses(x); } - builder.finish() - } - - pub const VT_ADDRESSES: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn addresses(&self) -> Option>>> { - self._tab.get::>>>>(Node::VT_ADDRESSES, None) - } -} - -pub struct NodeArgs<'a> { - pub addresses: Option>>>>, -} -impl<'a> Default for NodeArgs<'a> { - #[inline] - fn default() -> Self { - NodeArgs { - addresses: None, - } - } -} -pub struct NodeBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> NodeBuilder<'a, 'b> { - #[inline] - pub fn add_addresses(&mut self, addresses: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Node::VT_ADDRESSES, addresses); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> NodeBuilder<'a, 'b> { - let start = _fbb.start_table(); - NodeBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum BytesOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Bytes<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Bytes<'a> { - type Inner = Bytes<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Bytes<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Bytes { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args BytesArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = BytesBuilder::new(_fbb); - if let Some(x) = args.seq { builder.add_seq(x); } - builder.finish() - } - - pub const VT_SEQ: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn seq(&self) -> Option<&'a [u8]> { - self._tab.get::>>(Bytes::VT_SEQ, None).map(|v| v.safe_slice()) - } -} - -pub struct BytesArgs<'a> { - pub seq: Option>>, -} -impl<'a> Default for BytesArgs<'a> { - #[inline] - fn default() -> Self { - BytesArgs { - seq: None, - } - } -} -pub struct BytesBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> BytesBuilder<'a, 'b> { - #[inline] - pub fn add_seq(&mut self, seq: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Bytes::VT_SEQ, seq); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> BytesBuilder<'a, 'b> { - let start = _fbb.start_table(); - BytesBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -} // pub mod Discovery -} // pub mod P2P - diff --git a/protocols/discovery/src/protocol_generated_verifier.rs b/protocols/discovery/src/protocol_generated_verifier.rs deleted file mode 100644 index 2c38dd85..00000000 --- a/protocols/discovery/src/protocol_generated_verifier.rs +++ /dev/null @@ -1,480 +0,0 @@ -//! This file is auto-generated by cfbc. -use super::protocol_generated as reader; - -pub mod p2p { - #![allow(unused_imports)] - - use super::reader::p2p as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - pub mod discovery { - #![allow(unused_imports)] - - use super::reader::discovery as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - - impl<'a> Verify for reader::Bytes<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_SEQ as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_SEQ) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let seq_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - seq_verifier.verify_scalar_elements(1)?; - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::DiscoveryMessage<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_PAYLOAD_TYPE as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_PAYLOAD_TYPE) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 1 { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_PAYLOAD as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_PAYLOAD) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - match self.payload_type() { - reader::DiscoveryPayload::GetNodes => self - .payload_as_get_nodes() - .ok_or(Error::UnmatchedUnion)? - .verify()?, - reader::DiscoveryPayload::Nodes => self - .payload_as_nodes() - .ok_or(Error::UnmatchedUnion)? - .verify()?, - reader::DiscoveryPayload::NONE => return Err(Error::UnmatchedUnion), - } - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::GetNodes<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_VERSION as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_VERSION) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 4 { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_COUNT as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_COUNT) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 4 { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_LISTEN_PORT as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_LISTEN_PORT) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 2 { - return Err(Error::OutOfBounds); - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::Node<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_ADDRESSES as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_ADDRESSES) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let addresses_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - addresses_verifier - .verify_reference_elements::()?; - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::Nodes<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_ANNOUNCE as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_ANNOUNCE) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 1 { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_ITEMS as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_ITEMS) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let items_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - items_verifier - .verify_reference_elements::()?; - } - } - - Ok(()) - } - } - } - -} diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index ced5f73b..dee348b0 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -10,26 +10,17 @@ categories = ["network-programming", "asynchronous"] edition = "2018" [package.metadata.docs.rs] -features = [ "molc" ] +features = [] all-features = false no-default-features = true [dependencies] -p2p = { path = "../..", version = "0.3.0", package = "tentacle" } -bytes = "0.5.0" -flatbuffers = { version = "0.6.0", optional = true } -flatbuffers-verifier = { version = "0.2.0", optional = true } +p2p = { path = "../../tentacle", version = "0.3.0", package = "tentacle" } +bytes = "1.0" log = "0.4" -molecule = { version = "0.5.0", optional = true } +molecule = { version = "0.7.0" } [dev-dependencies] env_logger = "0.6.0" futures = { version = "0.3.0" } -tokio = { version = "0.2.0", features = ["time", "io-util", "tcp", "dns", "rt-threaded", "blocking"] } - -[features] -default = [] -# use flatbuffer to handshake -flatc = [ "flatbuffers", "flatbuffers-verifier", "p2p/flatc" ] -# use molecule to handshake -molc = [ "molecule", "p2p/molc" ] +tokio = { version = "1.0.0", features = ["time", "io-util", "net", "rt-multi-thread" ] } diff --git a/protocols/identify/src/lib.rs b/protocols/identify/src/lib.rs index 5e47b6ec..274f878c 100644 --- a/protocols/identify/src/lib.rs +++ b/protocols/identify/src/lib.rs @@ -1,15 +1,3 @@ -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -#[allow(dead_code)] -#[allow(unused_imports)] -mod protocol_generated; -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -#[allow(dead_code)] -mod protocol_generated_verifier; -#[cfg(feature = "molc")] #[rustfmt::skip] #[allow(clippy::all)] #[allow(dead_code)] diff --git a/protocols/identify/src/protocol.fbs b/protocols/identify/src/protocol.fbs deleted file mode 100644 index 7fa73458..00000000 --- a/protocols/identify/src/protocol.fbs +++ /dev/null @@ -1,14 +0,0 @@ -namespace P2P.Identify; - -table Address { - bytes: [ubyte]; -} - -table IdentifyMessage { - // These are the addresses on which the peer is listening as multi-addresses. - listen_addrs: [Address]; - // Observed each other's ip - observed_addr: Address; - // Custom message to indicate self ability, such as list protocols supported - identify: [ubyte]; -} diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 6ce459b3..56927c59 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -1,16 +1,4 @@ -#[cfg(all(feature = "flatc", feature = "molc"))] -compile_error!("features `flatc` and `molc` are mutually exclusive"); -#[cfg(all(not(feature = "flatc"), not(feature = "molc")))] -compile_error!("Please choose a serialization format via feature. Possible choices: flatc, molc"); - -#[cfg(feature = "flatc")] -use crate::protocol_generated::p2p::identify::{ - Address as FbsAddress, AddressBuilder, IdentifyMessage as FbsIdentifyMessage, - IdentifyMessageBuilder, -}; -#[cfg(feature = "molc")] use crate::protocol_mol; -#[cfg(feature = "molc")] use molecule::prelude::{Builder, Entity, Reader}; use bytes::Bytes; @@ -38,60 +26,6 @@ impl<'a> IdentifyMessage<'a> { } } - #[cfg(feature = "flatc")] - pub(crate) fn encode(&self) -> Bytes { - let mut fbb = flatbuffers::FlatBufferBuilder::new(); - - let mut listens = Vec::new(); - for addr in self.listen_addrs.as_slice() { - listens.push(addr_to_offset(&mut fbb, &addr)); - } - let listens_vec = fbb.create_vector(&listens); - - let observed = addr_to_offset(&mut fbb, &self.observed_addr); - - let identify = fbb.create_vector(self.identify); - - let mut builder = IdentifyMessageBuilder::new(&mut fbb); - - builder.add_listen_addrs(listens_vec); - builder.add_observed_addr(observed); - builder.add_identify(identify); - - let data = builder.finish(); - - fbb.finish(data, None); - Bytes::from(fbb.finished_data().to_owned()) - } - - #[cfg(feature = "flatc")] - pub(crate) fn decode(data: &'a [u8]) -> Option { - let fbs_message = flatbuffers_verifier::get_root::(data).ok()?; - - match ( - fbs_message.listen_addrs(), - fbs_message.observed_addr(), - fbs_message.identify(), - ) { - (Some(raw_listens), Some(raw_observed), Some(identify)) => { - let mut listen_addrs = Vec::with_capacity(raw_listens.len()); - for i in 0..raw_listens.len() { - listen_addrs.push(fbs_to_addr(&raw_listens.get(i))?); - } - - let observed_addr = fbs_to_addr(&raw_observed)?; - - Some(IdentifyMessage { - listen_addrs, - observed_addr, - identify, - }) - } - _ => None, - } - } - - #[cfg(feature = "molc")] pub(crate) fn encode(self) -> Bytes { let identify = protocol_mol::Bytes::new_builder() .set(self.identify.to_vec().into_iter().map(Into::into).collect()) @@ -133,7 +67,6 @@ impl<'a> IdentifyMessage<'a> { .as_bytes() } - #[cfg(feature = "molc")] pub(crate) fn decode(data: &'a [u8]) -> Option { let reader = protocol_mol::IdentifyMessageReader::from_compatible_slice(data).ok()?; @@ -152,19 +85,3 @@ impl<'a> IdentifyMessage<'a> { }) } } - -#[cfg(feature = "flatc")] -fn addr_to_offset<'b>( - fbb: &mut flatbuffers::FlatBufferBuilder<'b>, - addr: &Multiaddr, -) -> flatbuffers::WIPOffset> { - let bytes = fbb.create_vector(addr.as_ref()); - let mut addr_builder = AddressBuilder::new(fbb); - addr_builder.add_bytes(bytes); - addr_builder.finish() -} - -#[cfg(feature = "flatc")] -fn fbs_to_addr(addr: &FbsAddress) -> Option { - Multiaddr::try_from(addr.bytes()?.to_vec()).ok() -} diff --git a/protocols/identify/src/protocol_generated.rs b/protocols/identify/src/protocol_generated.rs deleted file mode 100644 index 34b656c1..00000000 --- a/protocols/identify/src/protocol_generated.rs +++ /dev/null @@ -1,206 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::EndianScalar; - -#[allow(unused_imports, dead_code)] -pub mod p2p { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; -#[allow(unused_imports, dead_code)] -pub mod identify { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; - -pub enum AddressOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Address<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Address<'a> { - type Inner = Address<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Address<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Address { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args AddressArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = AddressBuilder::new(_fbb); - if let Some(x) = args.bytes { builder.add_bytes(x); } - builder.finish() - } - - pub const VT_BYTES: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn bytes(&self) -> Option<&'a [u8]> { - self._tab.get::>>(Address::VT_BYTES, None).map(|v| v.safe_slice()) - } -} - -pub struct AddressArgs<'a> { - pub bytes: Option>>, -} -impl<'a> Default for AddressArgs<'a> { - #[inline] - fn default() -> Self { - AddressArgs { - bytes: None, - } - } -} -pub struct AddressBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> AddressBuilder<'a, 'b> { - #[inline] - pub fn add_bytes(&mut self, bytes: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Address::VT_BYTES, bytes); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> AddressBuilder<'a, 'b> { - let start = _fbb.start_table(); - AddressBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum IdentifyMessageOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct IdentifyMessage<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for IdentifyMessage<'a> { - type Inner = IdentifyMessage<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> IdentifyMessage<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - IdentifyMessage { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args IdentifyMessageArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = IdentifyMessageBuilder::new(_fbb); - if let Some(x) = args.identify { builder.add_identify(x); } - if let Some(x) = args.observed_addr { builder.add_observed_addr(x); } - if let Some(x) = args.listen_addrs { builder.add_listen_addrs(x); } - builder.finish() - } - - pub const VT_LISTEN_ADDRS: flatbuffers::VOffsetT = 4; - pub const VT_OBSERVED_ADDR: flatbuffers::VOffsetT = 6; - pub const VT_IDENTIFY: flatbuffers::VOffsetT = 8; - - #[inline] - pub fn listen_addrs(&self) -> Option>>> { - self._tab.get::>>>>(IdentifyMessage::VT_LISTEN_ADDRS, None) - } - #[inline] - pub fn observed_addr(&self) -> Option> { - self._tab.get::>>(IdentifyMessage::VT_OBSERVED_ADDR, None) - } - #[inline] - pub fn identify(&self) -> Option<&'a [u8]> { - self._tab.get::>>(IdentifyMessage::VT_IDENTIFY, None).map(|v| v.safe_slice()) - } -} - -pub struct IdentifyMessageArgs<'a> { - pub listen_addrs: Option>>>>, - pub observed_addr: Option>>, - pub identify: Option>>, -} -impl<'a> Default for IdentifyMessageArgs<'a> { - #[inline] - fn default() -> Self { - IdentifyMessageArgs { - listen_addrs: None, - observed_addr: None, - identify: None, - } - } -} -pub struct IdentifyMessageBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> IdentifyMessageBuilder<'a, 'b> { - #[inline] - pub fn add_listen_addrs(&mut self, listen_addrs: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(IdentifyMessage::VT_LISTEN_ADDRS, listen_addrs); - } - #[inline] - pub fn add_observed_addr(&mut self, observed_addr: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(IdentifyMessage::VT_OBSERVED_ADDR, observed_addr); - } - #[inline] - pub fn add_identify(&mut self, identify: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(IdentifyMessage::VT_IDENTIFY, identify); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> IdentifyMessageBuilder<'a, 'b> { - let start = _fbb.start_table(); - IdentifyMessageBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -} // pub mod Identify -} // pub mod P2P - diff --git a/protocols/identify/src/protocol_generated_verifier.rs b/protocols/identify/src/protocol_generated_verifier.rs deleted file mode 100644 index bf027e81..00000000 --- a/protocols/identify/src/protocol_generated_verifier.rs +++ /dev/null @@ -1,225 +0,0 @@ -//! This file is auto-generated by cfbc. -use super::protocol_generated as reader; - -pub mod p2p { - #![allow(unused_imports)] - - use super::reader::p2p as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - pub mod identify { - #![allow(unused_imports)] - - use super::reader::identify as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - - impl<'a> Verify for reader::Address<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_BYTES as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_BYTES) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let bytes_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - bytes_verifier.verify_scalar_elements(1)?; - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::IdentifyMessage<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_LISTEN_ADDRS as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_LISTEN_ADDRS) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let listen_addrs_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - listen_addrs_verifier - .verify_reference_elements::()?; - } - } - - if Self::VT_OBSERVED_ADDR as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_OBSERVED_ADDR) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - if let Some(f) = self.observed_addr() { - f.verify()?; - } - } - } - - if Self::VT_IDENTIFY as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_IDENTIFY) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - let identify_verifier = VectorVerifier::follow( - buf, - try_follow_uoffset(buf, tab.loc + voffset)?, - ); - identify_verifier.verify_scalar_elements(1)?; - } - } - - Ok(()) - } - } - } - -} diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index 3aad377e..4b5f998b 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -10,26 +10,17 @@ description = "ping protocol implementation for tentacle" edition = "2018" [package.metadata.docs.rs] -features = [ "molc" ] +features = [] all-features = false no-default-features = true [dependencies] -p2p = { path = "../..", version = "0.3.0", package = "tentacle" } +p2p = { path = "../../tentacle", version = "0.3.0", package = "tentacle" } log = "0.4" -flatbuffers = { version = "0.6.0", optional = true } -flatbuffers-verifier = { version = "0.2.0", optional = true } futures = { version = "0.3.0" } -bytes = "0.5.0" -molecule = { version = "0.5.0", optional = true } +bytes = "1.0.0" +molecule = { version = "0.7.0" } [dev-dependencies] env_logger = "0.6.0" -tokio = { version = "0.2.0", features = ["time", "io-util", "tcp", "dns", "rt-threaded", "blocking"] } - -[features] -default = [] -# use flatbuffer to handshake -flatc = [ "flatbuffers", "flatbuffers-verifier", "p2p/flatc" ] -# use molecule to handshake -molc = [ "molecule", "p2p/molc" ] +tokio = { version = "1.0.0", features = ["time", "io-util", "net", "rt-multi-thread"] } diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 115b04b6..c2ebc7c3 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -1,24 +1,7 @@ -#[cfg(all(feature = "flatc", feature = "molc"))] -compile_error!("features `flatc` and `molc` are mutually exclusive"); -#[cfg(all(not(feature = "flatc"), not(feature = "molc")))] -compile_error!("Please choose a serialization format via feature. Possible choices: flatc, molc"); - -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -mod protocol_generated; -#[cfg(feature = "flatc")] -#[rustfmt::skip] -#[allow(clippy::all)] -#[allow(dead_code)] -mod protocol_generated_verifier; - -#[cfg(feature = "molc")] #[rustfmt::skip] #[allow(clippy::all)] #[allow(dead_code)] mod protocol_mol; -#[cfg(feature = "molc")] use molecule::prelude::{Builder, Entity, Reader}; use log::{debug, error, trace, warn}; @@ -269,56 +252,6 @@ enum PingPayload { struct PingMessage; impl PingMessage { - #[cfg(feature = "flatc")] - fn build_ping(nonce: u32) -> Bytes { - let mut fbb = flatbuffers::FlatBufferBuilder::new(); - let ping = { - let mut ping = protocol_generated::p2p::ping::PingBuilder::new(&mut fbb); - ping.add_nonce(nonce); - ping.finish() - }; - - let mut builder = protocol_generated::p2p::ping::PingMessageBuilder::new(&mut fbb); - builder.add_payload_type(protocol_generated::p2p::ping::PingPayload::Ping); - builder.add_payload(ping.as_union_value()); - let data = builder.finish(); - fbb.finish(data, None); - Bytes::from(fbb.finished_data().to_owned()) - } - - #[cfg(feature = "flatc")] - fn build_pong(nonce: u32) -> Bytes { - let mut fbb = flatbuffers::FlatBufferBuilder::new(); - let pong = { - let mut pong = protocol_generated::p2p::ping::PongBuilder::new(&mut fbb); - pong.add_nonce(nonce); - pong.finish() - }; - let mut builder = protocol_generated::p2p::ping::PingMessageBuilder::new(&mut fbb); - builder.add_payload_type(protocol_generated::p2p::ping::PingPayload::Pong); - builder.add_payload(pong.as_union_value()); - let data = builder.finish(); - fbb.finish(data, None); - Bytes::from(fbb.finished_data().to_owned()) - } - - #[cfg(feature = "flatc")] - fn decode(data: &[u8]) -> Option { - let msg = - flatbuffers_verifier::get_root::(data) - .ok()?; - match msg.payload_type() { - protocol_generated::p2p::ping::PingPayload::Ping => { - Some(PingPayload::Ping(msg.payload_as_ping().unwrap().nonce())) - } - protocol_generated::p2p::ping::PingPayload::Pong => { - Some(PingPayload::Pong(msg.payload_as_pong().unwrap().nonce())) - } - protocol_generated::p2p::ping::PingPayload::NONE => None, - } - } - - #[cfg(feature = "molc")] fn build_ping(nonce: u32) -> Bytes { let nonce_le = nonce.to_le_bytes(); let nonce = protocol_mol::Uint32::new_builder() @@ -336,7 +269,6 @@ impl PingMessage { .as_bytes() } - #[cfg(feature = "molc")] fn build_pong(nonce: u32) -> Bytes { let nonce_le = nonce.to_le_bytes(); let nonce = protocol_mol::Uint32::new_builder() @@ -354,7 +286,6 @@ impl PingMessage { .as_bytes() } - #[cfg(feature = "molc")] #[allow(clippy::cast_ptr_alignment)] fn decode(data: &[u8]) -> Option { let reader = protocol_mol::PingMessageReader::from_compatible_slice(data).ok()?; diff --git a/protocols/ping/src/protocol.fbs b/protocols/ping/src/protocol.fbs deleted file mode 100644 index 6b27a54f..00000000 --- a/protocols/ping/src/protocol.fbs +++ /dev/null @@ -1,18 +0,0 @@ -namespace P2P.Ping; - -union PingPayload { - Ping, - Pong, -} - -table PingMessage { - payload: PingPayload; -} - -table Ping { - nonce: uint32; -} - -table Pong { - nonce: uint32; -} diff --git a/protocols/ping/src/protocol_generated.rs b/protocols/ping/src/protocol_generated.rs deleted file mode 100644 index c5e40bc6..00000000 --- a/protocols/ping/src/protocol_generated.rs +++ /dev/null @@ -1,349 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - -pub mod p2p { - #![allow(dead_code)] - #![allow(unused_imports)] - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; -pub mod ping { - #![allow(dead_code)] - #![allow(unused_imports)] - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::EndianScalar; - -#[allow(non_camel_case_types)] -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Debug)] -pub enum PingPayload { - NONE = 0, - Ping = 1, - Pong = 2, - -} - -const ENUM_MIN_PING_PAYLOAD: u8 = 0; -const ENUM_MAX_PING_PAYLOAD: u8 = 2; - -impl<'a> flatbuffers::Follow<'a> for PingPayload { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::read_scalar_at::(buf, loc) - } -} - -impl flatbuffers::EndianScalar for PingPayload { - #[inline] - fn to_little_endian(self) -> Self { - let n = u8::to_le(self as u8); - let p = &n as *const u8 as *const PingPayload; - unsafe { *p } - } - #[inline] - fn from_little_endian(self) -> Self { - let n = u8::from_le(self as u8); - let p = &n as *const u8 as *const PingPayload; - unsafe { *p } - } -} - -impl flatbuffers::Push for PingPayload { - type Output = PingPayload; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - flatbuffers::emplace_scalar::(dst, *self); - } -} - -#[allow(non_camel_case_types)] -const ENUM_VALUES_PING_PAYLOAD:[PingPayload; 3] = [ - PingPayload::NONE, - PingPayload::Ping, - PingPayload::Pong -]; - -#[allow(non_camel_case_types)] -const ENUM_NAMES_PING_PAYLOAD:[&'static str; 3] = [ - "NONE", - "Ping", - "Pong" -]; - -pub fn enum_name_ping_payload(e: PingPayload) -> &'static str { - let index: usize = e as usize; - ENUM_NAMES_PING_PAYLOAD[index] -} - -pub struct PingPayloadUnionTableOffset {} -pub enum PingMessageOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct PingMessage<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for PingMessage<'a> { - type Inner = PingMessage<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> PingMessage<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - PingMessage { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args PingMessageArgs) -> flatbuffers::WIPOffset> { - let mut builder = PingMessageBuilder::new(_fbb); - if let Some(x) = args.payload { builder.add_payload(x); } - builder.add_payload_type(args.payload_type); - builder.finish() - } - - pub const VT_PAYLOAD_TYPE: flatbuffers::VOffsetT = 4; - pub const VT_PAYLOAD: flatbuffers::VOffsetT = 6; - - #[inline] - pub fn payload_type(&self) -> PingPayload { - self._tab.get::(PingMessage::VT_PAYLOAD_TYPE, Some(PingPayload::NONE)).unwrap() - } - #[inline] - pub fn payload(&self) -> Option> { - self._tab.get::>>(PingMessage::VT_PAYLOAD, None) - } - #[inline] - #[allow(non_snake_case)] - pub fn payload_as_ping(&'a self) -> Option { - if self.payload_type() == PingPayload::Ping { - self.payload().map(|u| Ping::init_from_table(u)) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn payload_as_pong(&'a self) -> Option { - if self.payload_type() == PingPayload::Pong { - self.payload().map(|u| Pong::init_from_table(u)) - } else { - None - } - } - -} - -pub struct PingMessageArgs { - pub payload_type: PingPayload, - pub payload: Option>, -} -impl<'a> Default for PingMessageArgs { - #[inline] - fn default() -> Self { - PingMessageArgs { - payload_type: PingPayload::NONE, - payload: None, - } - } -} -pub struct PingMessageBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> PingMessageBuilder<'a, 'b> { - #[inline] - pub fn add_payload_type(&mut self, payload_type: PingPayload) { - self.fbb_.push_slot::(PingMessage::VT_PAYLOAD_TYPE, payload_type, PingPayload::NONE); - } - #[inline] - pub fn add_payload(&mut self, payload: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(PingMessage::VT_PAYLOAD, payload); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> PingMessageBuilder<'a, 'b> { - let start = _fbb.start_table(); - PingMessageBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum PingOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Ping<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Ping<'a> { - type Inner = Ping<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Ping<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Ping { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args PingArgs) -> flatbuffers::WIPOffset> { - let mut builder = PingBuilder::new(_fbb); - builder.add_nonce(args.nonce); - builder.finish() - } - - pub const VT_NONCE: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn nonce(&self) -> u32 { - self._tab.get::(Ping::VT_NONCE, Some(0)).unwrap() - } -} - -pub struct PingArgs { - pub nonce: u32, -} -impl<'a> Default for PingArgs { - #[inline] - fn default() -> Self { - PingArgs { - nonce: 0, - } - } -} -pub struct PingBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> PingBuilder<'a, 'b> { - #[inline] - pub fn add_nonce(&mut self, nonce: u32) { - self.fbb_.push_slot::(Ping::VT_NONCE, nonce, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> PingBuilder<'a, 'b> { - let start = _fbb.start_table(); - PingBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -pub enum PongOffset {} -#[derive(Copy, Clone, Debug, PartialEq)] - -pub struct Pong<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Pong<'a> { - type Inner = Pong<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } - } -} - -impl<'a> Pong<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Pong { - _tab: table, - } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args PongArgs) -> flatbuffers::WIPOffset> { - let mut builder = PongBuilder::new(_fbb); - builder.add_nonce(args.nonce); - builder.finish() - } - - pub const VT_NONCE: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn nonce(&self) -> u32 { - self._tab.get::(Pong::VT_NONCE, Some(0)).unwrap() - } -} - -pub struct PongArgs { - pub nonce: u32, -} -impl<'a> Default for PongArgs { - #[inline] - fn default() -> Self { - PongArgs { - nonce: 0, - } - } -} -pub struct PongBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> PongBuilder<'a, 'b> { - #[inline] - pub fn add_nonce(&mut self, nonce: u32) { - self.fbb_.push_slot::(Pong::VT_NONCE, nonce, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> PongBuilder<'a, 'b> { - let start = _fbb.start_table(); - PongBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -} // pub mod Ping -} // pub mod P2P - diff --git a/protocols/ping/src/protocol_generated_verifier.rs b/protocols/ping/src/protocol_generated_verifier.rs deleted file mode 100644 index d888faa4..00000000 --- a/protocols/ping/src/protocol_generated_verifier.rs +++ /dev/null @@ -1,275 +0,0 @@ -//! This file is auto-generated by cfbc. -use super::protocol_generated as reader; - -pub mod p2p { - #![allow(unused_imports)] - - use super::reader::p2p as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - pub mod ping { - #![allow(unused_imports)] - - use super::reader::ping as reader; - use flatbuffers::{self, Follow}; - use flatbuffers_verifier::{ - try_follow_uoffset, Error, Result, StringVerifier, VectorVerifier, Verify, - MAX_OFFSET_LOC, - }; - - impl<'a> Verify for reader::Ping<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_NONCE as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_NONCE) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 4 { - return Err(Error::OutOfBounds); - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::PingMessage<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_PAYLOAD_TYPE as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_PAYLOAD_TYPE) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 1 { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_PAYLOAD as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_PAYLOAD) as usize; - if voffset > 0 { - if voffset + 4 > object_inline_num_bytes { - return Err(Error::OutOfBounds); - } - - match self.payload_type() { - reader::PingPayload::Ping => self - .payload_as_ping() - .ok_or(Error::UnmatchedUnion)? - .verify()?, - reader::PingPayload::Pong => self - .payload_as_pong() - .ok_or(Error::UnmatchedUnion)? - .verify()?, - reader::PingPayload::NONE => return Err(Error::UnmatchedUnion), - } - } - } - - Ok(()) - } - } - - impl<'a> Verify for reader::Pong<'a> { - fn verify(&self) -> Result { - let tab = self._tab; - let buf = tab.buf; - let buf_len = buf.len(); - - if tab.loc > MAX_OFFSET_LOC || tab.loc + flatbuffers::SIZE_SOFFSET > buf_len { - return Err(Error::OutOfBounds); - } - - let vtab_loc = { - let soffset_slice = &buf[tab.loc..]; - let soffset = flatbuffers::read_scalar::(soffset_slice); - if soffset >= 0 { - tab.loc.checked_sub(soffset as usize) - } else { - soffset - .checked_neg() - .and_then(|foffset| tab.loc.checked_add(foffset as usize)) - } - } - .ok_or(Error::OutOfBounds)?; - if vtab_loc - .checked_add(flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - let vtab = tab.vtable(); - let vtab_num_bytes = vtab.num_bytes(); - let object_inline_num_bytes = vtab.object_inline_num_bytes(); - if vtab_num_bytes < flatbuffers::SIZE_VOFFSET + flatbuffers::SIZE_VOFFSET - || object_inline_num_bytes < flatbuffers::SIZE_SOFFSET - { - return Err(Error::OutOfBounds); - } - if vtab_loc - .checked_add(vtab_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - if tab - .loc - .checked_add(object_inline_num_bytes) - .filter(|loc| *loc <= buf_len) - .is_none() - { - return Err(Error::OutOfBounds); - } - - for i in 0..vtab.num_fields() { - let voffset = vtab.get_field(i) as usize; - if (voffset > 0 && voffset < flatbuffers::SIZE_SOFFSET) - || voffset >= object_inline_num_bytes - { - return Err(Error::OutOfBounds); - } - } - - if Self::VT_NONCE as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_NONCE) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 4 { - return Err(Error::OutOfBounds); - } - } - - Ok(()) - } - } - } - -} diff --git a/secio/Cargo.toml b/secio/Cargo.toml index 9ad31491..cb6789fa 100644 --- a/secio/Cargo.toml +++ b/secio/Cargo.toml @@ -24,7 +24,7 @@ log = "0.4.1" molecule = "0.7.0" -unsigned-varint = "0.3" +unsigned-varint = "0.6" bs58 = "0.3.0" secp256k1 = "0.19" diff --git a/tentacle/tests/test_large_pending_messge.rs b/tentacle/tests/test_large_pending_messge.rs index cb6e8923..0fd4af1c 100644 --- a/tentacle/tests/test_large_pending_messge.rs +++ b/tentacle/tests/test_large_pending_messge.rs @@ -53,7 +53,7 @@ fn create_meta(id: ProtocolId) -> ProtocolMeta { .id(id) .service_handle(move || { if id == 0.into() { - ProtocolHandle::Neither + ProtocolHandle::None } else { let handle = Box::new(PHandle); ProtocolHandle::Callback(handle) @@ -79,7 +79,7 @@ fn test_large_message(secio: bool) { let (addr_sender, addr_receiver) = channel::oneshot::channel::(); thread::spawn(move || { - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); let mut service = create(secio, meta_1, SHandle); rt.block_on(async move { let listen_addr = service @@ -96,7 +96,7 @@ fn test_large_message(secio: bool) { }); let meta = create_meta(1.into()); - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); let mut service = create(secio, meta, SHandle); rt.block_on(async move { let listen_addr = addr_receiver.await.unwrap(); diff --git a/yamux/src/session.rs b/yamux/src/session.rs index 17d7d101..fa49d21b 100644 --- a/yamux/src/session.rs +++ b/yamux/src/session.rs @@ -1072,7 +1072,7 @@ mod test { #[test] fn test_open_too_many_stream() { - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { let (remote, local) = MockSocket::new(); diff --git a/yamux/src/stream.rs b/yamux/src/stream.rs index b5ed9222..e2762506 100644 --- a/yamux/src/stream.rs +++ b/yamux/src/stream.rs @@ -615,7 +615,7 @@ mod test { // the protocol cannot be opened #[test] fn test_open_stream_with_data() { - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { let (_frame_sender, frame_receiver) = channel(2); let (unbound_sender, mut unbound_receiver) = unbounded();