From c250f684376d1ff7cd1ea13e2e741c53e99de027 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 6 Jan 2023 19:13:33 -0500 Subject: [PATCH 01/66] Add generated directory --- core/src/generated/envelope.proto | 30 +++++++ core/src/generated/keys.proto | 20 +++++ core/src/generated/peer_record.proto | 27 ++++++ protocols/autonat/src/generated/structs.proto | 37 ++++++++ protocols/dcutr/src/generated/message.proto | 19 ++++ protocols/floodsub/src/generated/rpc.proto | 20 +++++ .../gossipsub/src/generated/compat.proto | 12 +++ protocols/gossipsub/src/generated/rpc.proto | 84 +++++++++++++++++ .../identify/src/generated/structs.proto | 27 ++++++ protocols/kad/src/generated/dht.proto | 89 +++++++++++++++++++ protocols/relay/src/generated/message.proto | 59 ++++++++++++ protocols/rendezvous/src/generated/rpc.proto | 61 +++++++++++++ transports/noise/src/generated/payload.proto | 11 +++ .../plaintext/src/generated/structs.proto | 8 ++ transports/webrtc/src/generated/message.proto | 20 +++++ 15 files changed, 524 insertions(+) create mode 100644 core/src/generated/envelope.proto create mode 100644 core/src/generated/keys.proto create mode 100644 core/src/generated/peer_record.proto create mode 100644 protocols/autonat/src/generated/structs.proto create mode 100644 protocols/dcutr/src/generated/message.proto create mode 100644 protocols/floodsub/src/generated/rpc.proto create mode 100644 protocols/gossipsub/src/generated/compat.proto create mode 100644 protocols/gossipsub/src/generated/rpc.proto create mode 100644 protocols/identify/src/generated/structs.proto create mode 100644 protocols/kad/src/generated/dht.proto create mode 100644 protocols/relay/src/generated/message.proto create mode 100644 protocols/rendezvous/src/generated/rpc.proto create mode 100644 transports/noise/src/generated/payload.proto create mode 100644 transports/plaintext/src/generated/structs.proto create mode 100644 transports/webrtc/src/generated/message.proto diff --git a/core/src/generated/envelope.proto b/core/src/generated/envelope.proto new file mode 100644 index 00000000000..9ab3e6fd256 --- /dev/null +++ b/core/src/generated/envelope.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package envelope_proto; + +import "keys.proto"; + +// Envelope encloses a signed payload produced by a peer, along with the public +// key of the keypair it was signed with so that it can be statelessly validated +// by the receiver. +// +// The payload is prefixed with a byte string that determines the type, so it +// can be deserialized deterministically. Often, this byte string is a +// multicodec. +message Envelope { + // public_key is the public key of the keypair the enclosed payload was + // signed with. + keys_proto.PublicKey public_key = 1; + + // payload_type encodes the type of payload, so that it can be deserialized + // deterministically. + bytes payload_type = 2; + + // payload is the actual payload carried inside this envelope. + bytes payload = 3; + + // signature is the signature produced by the private key corresponding to + // the enclosed public key, over the payload, prefixing a domain string for + // additional security. + bytes signature = 5; +} diff --git a/core/src/generated/keys.proto b/core/src/generated/keys.proto new file mode 100644 index 00000000000..5fbeaf8f6e0 --- /dev/null +++ b/core/src/generated/keys.proto @@ -0,0 +1,20 @@ +syntax = "proto2"; + +package keys_proto; + +enum KeyType { + RSA = 0; + Ed25519 = 1; + Secp256k1 = 2; + ECDSA = 3; +} + +message PublicKey { + required KeyType Type = 1; + required bytes Data = 2; +} + +message PrivateKey { + required KeyType Type = 1; + required bytes Data = 2; +} diff --git a/core/src/generated/peer_record.proto b/core/src/generated/peer_record.proto new file mode 100644 index 00000000000..69bb345e02f --- /dev/null +++ b/core/src/generated/peer_record.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +package peer_record_proto; + +// PeerRecord messages contain information that is useful to share with other peers. +// Currently, a PeerRecord contains the public listen addresses for a peer, but this +// is expected to expand to include other information in the future. +// +// PeerRecords are designed to be serialized to bytes and placed inside of +// SignedEnvelopes before sharing with other peers. +message PeerRecord { + + // AddressInfo is a wrapper around a binary multiaddr. It is defined as a + // separate message to allow us to add per-address metadata in the future. + message AddressInfo { + bytes multiaddr = 1; + } + + // peer_id contains a libp2p peer id in its binary representation. + bytes peer_id = 1; + + // seq contains a monotonically-increasing sequence counter to order PeerRecords in time. + uint64 seq = 2; + + // addresses is a list of public listen addresses for the peer. + repeated AddressInfo addresses = 3; +} diff --git a/protocols/autonat/src/generated/structs.proto b/protocols/autonat/src/generated/structs.proto new file mode 100644 index 00000000000..19e27abd36a --- /dev/null +++ b/protocols/autonat/src/generated/structs.proto @@ -0,0 +1,37 @@ +syntax = "proto2"; + +package structs; + +message Message { + enum MessageType { + DIAL = 0; + DIAL_RESPONSE = 1; + } + + enum ResponseStatus { + OK = 0; + E_DIAL_ERROR = 100; + E_DIAL_REFUSED = 101; + E_BAD_REQUEST = 200; + E_INTERNAL_ERROR = 300; + } + + message PeerInfo { + optional bytes id = 1; + repeated bytes addrs = 2; + } + + message Dial { + optional PeerInfo peer = 1; + } + + message DialResponse { + optional ResponseStatus status = 1; + optional string statusText = 2; + optional bytes addr = 3; + } + + optional MessageType type = 1; + optional Dial dial = 2; + optional DialResponse dialResponse = 3; +} diff --git a/protocols/dcutr/src/generated/message.proto b/protocols/dcutr/src/generated/message.proto new file mode 100644 index 00000000000..ab5b220f2ea --- /dev/null +++ b/protocols/dcutr/src/generated/message.proto @@ -0,0 +1,19 @@ +syntax = "proto2"; + +package holepunch.pb; + +message HolePunch { + enum Type { + CONNECT = 100; + SYNC = 300; + } + + required Type type=1; + + // For hole punching, we'll send some additional observed addresses to the remote peer + // that could have been filtered by the Host address factory (for example: AutoRelay removes all public addresses if peer has private reachability). + // This is a hack! + // We plan to have a better address discovery and advertisement mechanism in the future. + // See https://github.com/libp2p/go-libp2p-autonat/pull/98 + repeated bytes ObsAddrs = 2; +} diff --git a/protocols/floodsub/src/generated/rpc.proto b/protocols/floodsub/src/generated/rpc.proto new file mode 100644 index 00000000000..84f0ea51795 --- /dev/null +++ b/protocols/floodsub/src/generated/rpc.proto @@ -0,0 +1,20 @@ +syntax = "proto2"; + +package floodsub.pb; + +message RPC { + repeated SubOpts subscriptions = 1; + repeated Message publish = 2; + + message SubOpts { + optional bool subscribe = 1; // subscribe or unsubcribe + optional string topic_id = 2; + } +} + +message Message { + optional bytes from = 1; + optional bytes data = 2; + optional bytes seqno = 3; + repeated string topic_ids = 4; +} diff --git a/protocols/gossipsub/src/generated/compat.proto b/protocols/gossipsub/src/generated/compat.proto new file mode 100644 index 00000000000..b2753bf7e41 --- /dev/null +++ b/protocols/gossipsub/src/generated/compat.proto @@ -0,0 +1,12 @@ +syntax = "proto2"; + +package compat.pb; + +message Message { + optional bytes from = 1; + optional bytes data = 2; + optional bytes seqno = 3; + repeated string topic_ids = 4; + optional bytes signature = 5; + optional bytes key = 6; +} \ No newline at end of file diff --git a/protocols/gossipsub/src/generated/rpc.proto b/protocols/gossipsub/src/generated/rpc.proto new file mode 100644 index 00000000000..2ce12f3f37f --- /dev/null +++ b/protocols/gossipsub/src/generated/rpc.proto @@ -0,0 +1,84 @@ +syntax = "proto2"; + +package gossipsub.pb; + +message RPC { + repeated SubOpts subscriptions = 1; + repeated Message publish = 2; + + message SubOpts { + optional bool subscribe = 1; // subscribe or unsubscribe + optional string topic_id = 2; + } + + optional ControlMessage control = 3; +} + +message Message { + optional bytes from = 1; + optional bytes data = 2; + optional bytes seqno = 3; + required string topic = 4; + optional bytes signature = 5; + optional bytes key = 6; +} + +message ControlMessage { + repeated ControlIHave ihave = 1; + repeated ControlIWant iwant = 2; + repeated ControlGraft graft = 3; + repeated ControlPrune prune = 4; +} + +message ControlIHave { + optional string topic_id = 1; + repeated bytes message_ids = 2; +} + +message ControlIWant { + repeated bytes message_ids= 1; +} + +message ControlGraft { + optional string topic_id = 1; +} + +message ControlPrune { + optional string topic_id = 1; + repeated PeerInfo peers = 2; // gossipsub v1.1 PX + optional uint64 backoff = 3; // gossipsub v1.1 backoff time (in seconds) +} + +message PeerInfo { + optional bytes peer_id = 1; + optional bytes signed_peer_record = 2; +} + +// topicID = hash(topicDescriptor); (not the topic.name) +message TopicDescriptor { + optional string name = 1; + optional AuthOpts auth = 2; + optional EncOpts enc = 3; + + message AuthOpts { + optional AuthMode mode = 1; + repeated bytes keys = 2; // root keys to trust + + enum AuthMode { + NONE = 0; // no authentication, anyone can publish + KEY = 1; // only messages signed by keys in the topic descriptor are accepted + WOT = 2; // web of trust, certificates can allow publisher set to grow + } + } + + message EncOpts { + optional EncMode mode = 1; + repeated bytes key_hashes = 2; // the hashes of the shared keys used (salted) + + enum EncMode { + NONE = 0; // no encryption, anyone can read + SHAREDKEY = 1; // messages are encrypted with shared key + WOT = 2; // web of trust, certificates can allow publisher set to grow + } + } +} diff --git a/protocols/identify/src/generated/structs.proto b/protocols/identify/src/generated/structs.proto new file mode 100644 index 00000000000..fbe6836bfdb --- /dev/null +++ b/protocols/identify/src/generated/structs.proto @@ -0,0 +1,27 @@ +syntax = "proto2"; + +package structs; + +message Identify { + // protocolVersion determines compatibility between peers + optional string protocolVersion = 5; // e.g. ipfs/1.0.0 + + // agentVersion is like a UserAgent string in browsers, or client version in bittorrent + // includes the client name and client. + optional string agentVersion = 6; // e.g. go-ipfs/0.1.0 + + // publicKey is this node's public key (which also gives its node.ID) + // - may not need to be sent, as secure channel implies it has been sent. + // - then again, if we change / disable secure channel, may still want it. + optional bytes publicKey = 1; + + // listenAddrs are the multiaddrs the sender node listens for open connections on + repeated bytes listenAddrs = 2; + + // oservedAddr is the multiaddr of the remote endpoint that the sender node perceives + // this is useful information to convey to the other side, as it helps the remote endpoint + // determine whether its connection to the local peer goes through NAT. + optional bytes observedAddr = 4; + + repeated string protocols = 3; +} diff --git a/protocols/kad/src/generated/dht.proto b/protocols/kad/src/generated/dht.proto new file mode 100644 index 00000000000..4ff2f6e265b --- /dev/null +++ b/protocols/kad/src/generated/dht.proto @@ -0,0 +1,89 @@ +syntax = "proto3"; +package dht.pb; + +// Record represents a dht record that contains a value +// for a key value pair +message Record { + // The key that references this record + bytes key = 1; + + // The actual value this record is storing + bytes value = 2; + + // Note: These fields were removed from the Record message + // hash of the authors public key + //optional string author = 3; + // A PKI signature for the key+value+author + //optional bytes signature = 4; + + // Time the record was received, set by receiver + string timeReceived = 5; + + // The original publisher of the record. + // Currently specific to rust-libp2p. + bytes publisher = 666; + + // The remaining TTL of the record, in seconds. + // Currently specific to rust-libp2p. + uint32 ttl = 777; +}; + +message Message { + enum MessageType { + PUT_VALUE = 0; + GET_VALUE = 1; + ADD_PROVIDER = 2; + GET_PROVIDERS = 3; + FIND_NODE = 4; + PING = 5; + } + + enum ConnectionType { + // sender does not have a connection to peer, and no extra information (default) + NOT_CONNECTED = 0; + + // sender has a live connection to peer + CONNECTED = 1; + + // sender recently connected to peer + CAN_CONNECT = 2; + + // sender recently tried to connect to peer repeatedly but failed to connect + // ("try" here is loose, but this should signal "made strong effort, failed") + CANNOT_CONNECT = 3; + } + + message Peer { + // ID of a given peer. + bytes id = 1; + + // multiaddrs for a given peer + repeated bytes addrs = 2; + + // used to signal the sender's connection capabilities to the peer + ConnectionType connection = 3; + } + + // defines what type of message it is. + MessageType type = 1; + + // defines what coral cluster level this query/response belongs to. + // in case we want to implement coral's cluster rings in the future. + int32 clusterLevelRaw = 10; // NOT USED + + // Used to specify the key associated with this message. + // PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS + bytes key = 2; + + // Used to return a value + // PUT_VALUE, GET_VALUE + Record record = 3; + + // Used to return peers closer to a key in a query + // GET_VALUE, GET_PROVIDERS, FIND_NODE + repeated Peer closerPeers = 8; + + // Used to return Providers + // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS + repeated Peer providerPeers = 9; +} diff --git a/protocols/relay/src/generated/message.proto b/protocols/relay/src/generated/message.proto new file mode 100644 index 00000000000..0a42ce3d773 --- /dev/null +++ b/protocols/relay/src/generated/message.proto @@ -0,0 +1,59 @@ +syntax = "proto2"; + +package message_v2.pb; + +message HopMessage { + enum Type { + RESERVE = 0; + CONNECT = 1; + STATUS = 2; + } + + required Type type = 1; + + optional Peer peer = 2; + optional Reservation reservation = 3; + optional Limit limit = 4; + + optional Status status = 5; +} + +message StopMessage { + enum Type { + CONNECT = 0; + STATUS = 1; + } + + required Type type = 1; + + optional Peer peer = 2; + optional Limit limit = 3; + + optional Status status = 4; +} + +message Peer { + required bytes id = 1; + repeated bytes addrs = 2; +} + +message Reservation { + required uint64 expire = 1; // Unix expiration time (UTC) + repeated bytes addrs = 2; // relay addrs for reserving peer + optional bytes voucher = 3; // reservation voucher +} +message Limit { + optional uint32 duration = 1; // seconds + optional uint64 data = 2; // bytes +} + +enum Status { + OK = 100; + RESERVATION_REFUSED = 200; + RESOURCE_LIMIT_EXCEEDED = 201; + PERMISSION_DENIED = 202; + CONNECTION_FAILED = 203; + NO_RESERVATION = 204; + MALFORMED_MESSAGE = 400; + UNEXPECTED_MESSAGE = 401; +} \ No newline at end of file diff --git a/protocols/rendezvous/src/generated/rpc.proto b/protocols/rendezvous/src/generated/rpc.proto new file mode 100644 index 00000000000..d4e388ca96e --- /dev/null +++ b/protocols/rendezvous/src/generated/rpc.proto @@ -0,0 +1,61 @@ +syntax = "proto2"; + +package rendezvous.pb; + +message Message { + enum MessageType { + REGISTER = 0; + REGISTER_RESPONSE = 1; + UNREGISTER = 2; + DISCOVER = 3; + DISCOVER_RESPONSE = 4; + } + + enum ResponseStatus { + OK = 0; + E_INVALID_NAMESPACE = 100; + E_INVALID_SIGNED_PEER_RECORD = 101; + E_INVALID_TTL = 102; + E_INVALID_COOKIE = 103; + E_NOT_AUTHORIZED = 200; + E_INTERNAL_ERROR = 300; + E_UNAVAILABLE = 400; + } + + message Register { + optional string ns = 1; + optional bytes signedPeerRecord = 2; + optional uint64 ttl = 3; // in seconds + } + + message RegisterResponse { + optional ResponseStatus status = 1; + optional string statusText = 2; + optional uint64 ttl = 3; // in seconds + } + + message Unregister { + optional string ns = 1; + optional bytes id = 2; + } + + message Discover { + optional string ns = 1; + optional uint64 limit = 2; + optional bytes cookie = 3; + } + + message DiscoverResponse { + repeated Register registrations = 1; + optional bytes cookie = 2; + optional ResponseStatus status = 3; + optional string statusText = 4; + } + + optional MessageType type = 1; + optional Register register = 2; + optional RegisterResponse registerResponse = 3; + optional Unregister unregister = 4; + optional Discover discover = 5; + optional DiscoverResponse discoverResponse = 6; +} diff --git a/transports/noise/src/generated/payload.proto b/transports/noise/src/generated/payload.proto new file mode 100644 index 00000000000..1893dc55037 --- /dev/null +++ b/transports/noise/src/generated/payload.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package payload.proto; + +// Payloads for Noise handshake messages. + +message NoiseHandshakePayload { + bytes identity_key = 1; + bytes identity_sig = 2; + bytes data = 3; +} diff --git a/transports/plaintext/src/generated/structs.proto b/transports/plaintext/src/generated/structs.proto new file mode 100644 index 00000000000..acabedce459 --- /dev/null +++ b/transports/plaintext/src/generated/structs.proto @@ -0,0 +1,8 @@ +syntax = "proto2"; + +package structs; + +message Exchange { + optional bytes id = 1; + optional bytes pubkey = 2; +} diff --git a/transports/webrtc/src/generated/message.proto b/transports/webrtc/src/generated/message.proto new file mode 100644 index 00000000000..eab3ceb720b --- /dev/null +++ b/transports/webrtc/src/generated/message.proto @@ -0,0 +1,20 @@ +syntax = "proto2"; + +package webrtc.pb; + +message Message { + enum Flag { + // The sender will no longer send messages on the stream. + FIN = 0; + // The sender will no longer read messages on the stream. Incoming data is + // being discarded on receipt. + STOP_SENDING = 1; + // The sender abruptly terminates the sending part of the stream. The + // receiver can discard any data that it already received on that stream. + RESET = 2; + } + + optional Flag flag=1; + + optional bytes message = 2; +} From cac57a049b075762c576add201aa03b8d441d6e0 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 6 Jan 2023 19:22:21 -0500 Subject: [PATCH 02/66] Add generated code --- core/src/generated/envelope_proto.rs | 60 ++ core/src/generated/keys_proto.rs | 126 ++++ core/src/generated/mod.rs | 4 + core/src/generated/peer_record_proto.rs | 95 +++ protocols/autonat/src/generated/mod.rs | 2 + protocols/autonat/src/generated/structs.rs | 243 ++++++++ .../dcutr/src/generated/holepunch/mod.rs | 2 + protocols/dcutr/src/generated/holepunch/pb.rs | 89 +++ protocols/dcutr/src/generated/mod.rs | 2 + .../floodsub/src/generated/floodsub/mod.rs | 2 + .../floodsub/src/generated/floodsub/pb.rs | 139 +++++ protocols/floodsub/src/generated/mod.rs | 2 + .../gossipsub/src/generated/compat/mod.rs | 2 + .../gossipsub/src/generated/compat/pb.rs | 68 +++ .../gossipsub/src/generated/gossipsub/mod.rs | 2 + .../gossipsub/src/generated/gossipsub/pb.rs | 570 ++++++++++++++++++ protocols/gossipsub/src/generated/mod.rs | 3 + protocols/identify/src/generated/mod.rs | 2 + protocols/identify/src/generated/structs.rs | 68 +++ protocols/kad/src/generated/dht/mod.rs | 2 + protocols/kad/src/generated/dht/pb.rs | 245 ++++++++ protocols/kad/src/generated/mod.rs | 2 + .../relay/src/generated/message_v2/mod.rs | 2 + .../relay/src/generated/message_v2/pb.rs | 347 +++++++++++ protocols/relay/src/generated/mod.rs | 2 + protocols/rendezvous/src/generated/mod.rs | 2 + .../src/generated/rendezvous/mod.rs | 2 + .../rendezvous/src/generated/rendezvous/pb.rs | 365 +++++++++++ transports/noise/src/generated/mod.rs | 2 + transports/noise/src/generated/payload/mod.rs | 2 + .../noise/src/generated/payload/proto.rs | 56 ++ transports/plaintext/src/generated/mod.rs | 2 + transports/plaintext/src/generated/structs.rs | 52 ++ transports/webrtc/src/generated/mod.rs | 2 + transports/webrtc/src/generated/webrtc/mod.rs | 2 + transports/webrtc/src/generated/webrtc/pb.rs | 92 +++ 36 files changed, 2660 insertions(+) create mode 100644 core/src/generated/envelope_proto.rs create mode 100644 core/src/generated/keys_proto.rs create mode 100644 core/src/generated/mod.rs create mode 100644 core/src/generated/peer_record_proto.rs create mode 100644 protocols/autonat/src/generated/mod.rs create mode 100644 protocols/autonat/src/generated/structs.rs create mode 100644 protocols/dcutr/src/generated/holepunch/mod.rs create mode 100644 protocols/dcutr/src/generated/holepunch/pb.rs create mode 100644 protocols/dcutr/src/generated/mod.rs create mode 100644 protocols/floodsub/src/generated/floodsub/mod.rs create mode 100644 protocols/floodsub/src/generated/floodsub/pb.rs create mode 100644 protocols/floodsub/src/generated/mod.rs create mode 100644 protocols/gossipsub/src/generated/compat/mod.rs create mode 100644 protocols/gossipsub/src/generated/compat/pb.rs create mode 100644 protocols/gossipsub/src/generated/gossipsub/mod.rs create mode 100644 protocols/gossipsub/src/generated/gossipsub/pb.rs create mode 100644 protocols/gossipsub/src/generated/mod.rs create mode 100644 protocols/identify/src/generated/mod.rs create mode 100644 protocols/identify/src/generated/structs.rs create mode 100644 protocols/kad/src/generated/dht/mod.rs create mode 100644 protocols/kad/src/generated/dht/pb.rs create mode 100644 protocols/kad/src/generated/mod.rs create mode 100644 protocols/relay/src/generated/message_v2/mod.rs create mode 100644 protocols/relay/src/generated/message_v2/pb.rs create mode 100644 protocols/relay/src/generated/mod.rs create mode 100644 protocols/rendezvous/src/generated/mod.rs create mode 100644 protocols/rendezvous/src/generated/rendezvous/mod.rs create mode 100644 protocols/rendezvous/src/generated/rendezvous/pb.rs create mode 100644 transports/noise/src/generated/mod.rs create mode 100644 transports/noise/src/generated/payload/mod.rs create mode 100644 transports/noise/src/generated/payload/proto.rs create mode 100644 transports/plaintext/src/generated/mod.rs create mode 100644 transports/plaintext/src/generated/structs.rs create mode 100644 transports/webrtc/src/generated/mod.rs create mode 100644 transports/webrtc/src/generated/webrtc/mod.rs create mode 100644 transports/webrtc/src/generated/webrtc/pb.rs diff --git a/core/src/generated/envelope_proto.rs b/core/src/generated/envelope_proto.rs new file mode 100644 index 00000000000..32cfc7ed207 --- /dev/null +++ b/core/src/generated/envelope_proto.rs @@ -0,0 +1,60 @@ +// Automatically generated rust module for 'envelope.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Envelope<'a> { + pub public_key: Option>, + pub payload_type: Cow<'a, [u8]>, + pub payload: Cow<'a, [u8]>, + pub signature: Cow<'a, [u8]>, +} + +impl<'a> MessageRead<'a> for Envelope<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.public_key = Some(r.read_message::(bytes)?), + Ok(18) => msg.payload_type = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(26) => msg.payload = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(42) => msg.signature = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Envelope<'a> { + fn get_size(&self) -> usize { + 0 + + self.public_key.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + if self.payload_type == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.payload_type).len()) } + + if self.payload == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.payload).len()) } + + if self.signature == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.signature).len()) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.public_key { w.write_with_tag(10, |w| w.write_message(s))?; } + if self.payload_type != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.payload_type))?; } + if self.payload != Cow::Borrowed(b"") { w.write_with_tag(26, |w| w.write_bytes(&**&self.payload))?; } + if self.signature != Cow::Borrowed(b"") { w.write_with_tag(42, |w| w.write_bytes(&**&self.signature))?; } + Ok(()) + } +} + diff --git a/core/src/generated/keys_proto.rs b/core/src/generated/keys_proto.rs new file mode 100644 index 00000000000..2d7a022760b --- /dev/null +++ b/core/src/generated/keys_proto.rs @@ -0,0 +1,126 @@ +// Automatically generated rust module for 'keys.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum KeyType { + RSA = 0, + Ed25519 = 1, + Secp256k1 = 2, + ECDSA = 3, +} + +impl Default for KeyType { + fn default() -> Self { + KeyType::RSA + } +} + +impl From for KeyType { + fn from(i: i32) -> Self { + match i { + 0 => KeyType::RSA, + 1 => KeyType::Ed25519, + 2 => KeyType::Secp256k1, + 3 => KeyType::ECDSA, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for KeyType { + fn from(s: &'a str) -> Self { + match s { + "RSA" => KeyType::RSA, + "Ed25519" => KeyType::Ed25519, + "Secp256k1" => KeyType::Secp256k1, + "ECDSA" => KeyType::ECDSA, + _ => Self::default(), + } + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PublicKey<'a> { + pub Type: keys_proto::KeyType, + pub Data: Cow<'a, [u8]>, +} + +impl<'a> MessageRead<'a> for PublicKey<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.Type = r.read_enum(bytes)?, + Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for PublicKey<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.Type) as u64) + + 1 + sizeof_len((&self.Data).len()) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.Type as i32))?; + w.write_with_tag(18, |w| w.write_bytes(&**&self.Data))?; + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PrivateKey<'a> { + pub Type: keys_proto::KeyType, + pub Data: Cow<'a, [u8]>, +} + +impl<'a> MessageRead<'a> for PrivateKey<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.Type = r.read_enum(bytes)?, + Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for PrivateKey<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.Type) as u64) + + 1 + sizeof_len((&self.Data).len()) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.Type as i32))?; + w.write_with_tag(18, |w| w.write_bytes(&**&self.Data))?; + Ok(()) + } +} + diff --git a/core/src/generated/mod.rs b/core/src/generated/mod.rs new file mode 100644 index 00000000000..964d125e02f --- /dev/null +++ b/core/src/generated/mod.rs @@ -0,0 +1,4 @@ +// Automatically generated mod.rs +pub mod envelope_proto; +pub mod keys_proto; +pub mod peer_record_proto; diff --git a/core/src/generated/peer_record_proto.rs b/core/src/generated/peer_record_proto.rs new file mode 100644 index 00000000000..b76f2f5277c --- /dev/null +++ b/core/src/generated/peer_record_proto.rs @@ -0,0 +1,95 @@ +// Automatically generated rust module for 'peer_record.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PeerRecord<'a> { + pub peer_id: Cow<'a, [u8]>, + pub seq: u64, + pub addresses: Vec>, +} + +impl<'a> MessageRead<'a> for PeerRecord<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.peer_id = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(16) => msg.seq = r.read_uint64(bytes)?, + Ok(26) => msg.addresses.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for PeerRecord<'a> { + fn get_size(&self) -> usize { + 0 + + if self.peer_id == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.peer_id).len()) } + + if self.seq == 0u64 { 0 } else { 1 + sizeof_varint(*(&self.seq) as u64) } + + self.addresses.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.peer_id != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.peer_id))?; } + if self.seq != 0u64 { w.write_with_tag(16, |w| w.write_uint64(*&self.seq))?; } + for s in &self.addresses { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_PeerRecord { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct AddressInfo<'a> { + pub multiaddr: Cow<'a, [u8]>, +} + +impl<'a> MessageRead<'a> for AddressInfo<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.multiaddr = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for AddressInfo<'a> { + fn get_size(&self) -> usize { + 0 + + if self.multiaddr == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.multiaddr).len()) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.multiaddr != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.multiaddr))?; } + Ok(()) + } +} + +} + diff --git a/protocols/autonat/src/generated/mod.rs b/protocols/autonat/src/generated/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/protocols/autonat/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/protocols/autonat/src/generated/structs.rs b/protocols/autonat/src/generated/structs.rs new file mode 100644 index 00000000000..28cb742e26c --- /dev/null +++ b/protocols/autonat/src/generated/structs.rs @@ -0,0 +1,243 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub type_pb: Option, + pub dial: Option>, + pub dialResponse: Option>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = Some(r.read_enum(bytes)?), + Ok(18) => msg.dial = Some(r.read_message::(bytes)?), + Ok(26) => msg.dialResponse = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.dial.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.dialResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.type_pb { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.dial { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.dialResponse { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_Message { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PeerInfo<'a> { + pub id: Option>, + pub addrs: Vec>, +} + +impl<'a> MessageRead<'a> for PeerInfo<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for PeerInfo<'a> { + fn get_size(&self) -> usize { + 0 + + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Dial<'a> { + pub peer: Option>, +} + +impl<'a> MessageRead<'a> for Dial<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.peer = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Dial<'a> { + fn get_size(&self) -> usize { + 0 + + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.peer { w.write_with_tag(10, |w| w.write_message(s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialResponse<'a> { + pub status: Option, + pub statusText: Option>, + pub addr: Option>, +} + +impl<'a> MessageRead<'a> for DialResponse<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.status = Some(r.read_enum(bytes)?), + Ok(18) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.addr = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for DialResponse<'a> { + fn get_size(&self) -> usize { + 0 + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.addr.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.status { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.statusText { w.write_with_tag(18, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.addr { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MessageType { + DIAL = 0, + DIAL_RESPONSE = 1, +} + +impl Default for MessageType { + fn default() -> Self { + MessageType::DIAL + } +} + +impl From for MessageType { + fn from(i: i32) -> Self { + match i { + 0 => MessageType::DIAL, + 1 => MessageType::DIAL_RESPONSE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for MessageType { + fn from(s: &'a str) -> Self { + match s { + "DIAL" => MessageType::DIAL, + "DIAL_RESPONSE" => MessageType::DIAL_RESPONSE, + _ => Self::default(), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ResponseStatus { + OK = 0, + E_DIAL_ERROR = 100, + E_DIAL_REFUSED = 101, + E_BAD_REQUEST = 200, + E_INTERNAL_ERROR = 300, +} + +impl Default for ResponseStatus { + fn default() -> Self { + ResponseStatus::OK + } +} + +impl From for ResponseStatus { + fn from(i: i32) -> Self { + match i { + 0 => ResponseStatus::OK, + 100 => ResponseStatus::E_DIAL_ERROR, + 101 => ResponseStatus::E_DIAL_REFUSED, + 200 => ResponseStatus::E_BAD_REQUEST, + 300 => ResponseStatus::E_INTERNAL_ERROR, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ResponseStatus { + fn from(s: &'a str) -> Self { + match s { + "OK" => ResponseStatus::OK, + "E_DIAL_ERROR" => ResponseStatus::E_DIAL_ERROR, + "E_DIAL_REFUSED" => ResponseStatus::E_DIAL_REFUSED, + "E_BAD_REQUEST" => ResponseStatus::E_BAD_REQUEST, + "E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR, + _ => Self::default(), + } + } +} + +} + diff --git a/protocols/dcutr/src/generated/holepunch/mod.rs b/protocols/dcutr/src/generated/holepunch/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/dcutr/src/generated/holepunch/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/dcutr/src/generated/holepunch/pb.rs b/protocols/dcutr/src/generated/holepunch/pb.rs new file mode 100644 index 00000000000..de44ccf7cd4 --- /dev/null +++ b/protocols/dcutr/src/generated/holepunch/pb.rs @@ -0,0 +1,89 @@ +// Automatically generated rust module for 'message.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct HolePunch<'a> { + pub type_pb: holepunch::pb::mod_HolePunch::Type, + pub ObsAddrs: Vec>, +} + +impl<'a> MessageRead<'a> for HolePunch<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(18) => msg.ObsAddrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for HolePunch<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.type_pb) as u64) + + self.ObsAddrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; + for s in &self.ObsAddrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_HolePunch { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Type { + CONNECT = 100, + SYNC = 300, +} + +impl Default for Type { + fn default() -> Self { + Type::CONNECT + } +} + +impl From for Type { + fn from(i: i32) -> Self { + match i { + 100 => Type::CONNECT, + 300 => Type::SYNC, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Type { + fn from(s: &'a str) -> Self { + match s { + "CONNECT" => Type::CONNECT, + "SYNC" => Type::SYNC, + _ => Self::default(), + } + } +} + +} + diff --git a/protocols/dcutr/src/generated/mod.rs b/protocols/dcutr/src/generated/mod.rs new file mode 100644 index 00000000000..89023be5535 --- /dev/null +++ b/protocols/dcutr/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod holepunch; diff --git a/protocols/floodsub/src/generated/floodsub/mod.rs b/protocols/floodsub/src/generated/floodsub/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/floodsub/src/generated/floodsub/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/floodsub/src/generated/floodsub/pb.rs b/protocols/floodsub/src/generated/floodsub/pb.rs new file mode 100644 index 00000000000..967e89add39 --- /dev/null +++ b/protocols/floodsub/src/generated/floodsub/pb.rs @@ -0,0 +1,139 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RPC<'a> { + pub subscriptions: Vec>, + pub publish: Vec>, +} + +impl<'a> MessageRead<'a> for RPC<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), + Ok(18) => msg.publish.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for RPC<'a> { + fn get_size(&self) -> usize { + 0 + + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_RPC { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct SubOpts<'a> { + pub subscribe: Option, + pub topic_id: Option>, +} + +impl<'a> MessageRead<'a> for SubOpts<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for SubOpts<'a> { + fn get_size(&self) -> usize { + 0 + + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } + if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(34) => msg.topic_ids.push(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/floodsub/src/generated/mod.rs b/protocols/floodsub/src/generated/mod.rs new file mode 100644 index 00000000000..1cef0041eb6 --- /dev/null +++ b/protocols/floodsub/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod floodsub; diff --git a/protocols/gossipsub/src/generated/compat/mod.rs b/protocols/gossipsub/src/generated/compat/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/gossipsub/src/generated/compat/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/gossipsub/src/generated/compat/pb.rs b/protocols/gossipsub/src/generated/compat/pb.rs new file mode 100644 index 00000000000..8f048013532 --- /dev/null +++ b/protocols/gossipsub/src/generated/compat/pb.rs @@ -0,0 +1,68 @@ +// Automatically generated rust module for 'compat.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec>, + pub signature: Option>, + pub key: Option>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(34) => msg.topic_ids.push(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(42) => msg.signature = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(50) => msg.key = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/gossipsub/src/generated/gossipsub/mod.rs b/protocols/gossipsub/src/generated/gossipsub/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/gossipsub/src/generated/gossipsub/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/gossipsub/src/generated/gossipsub/pb.rs b/protocols/gossipsub/src/generated/gossipsub/pb.rs new file mode 100644 index 00000000000..793a1175570 --- /dev/null +++ b/protocols/gossipsub/src/generated/gossipsub/pb.rs @@ -0,0 +1,570 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RPC<'a> { + pub subscriptions: Vec>, + pub publish: Vec>, + pub control: Option>, +} + +impl<'a> MessageRead<'a> for RPC<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), + Ok(18) => msg.publish.push(r.read_message::(bytes)?), + Ok(26) => msg.control = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for RPC<'a> { + fn get_size(&self) -> usize { + 0 + + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.control.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.control { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_RPC { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct SubOpts<'a> { + pub subscribe: Option, + pub topic_id: Option>, +} + +impl<'a> MessageRead<'a> for SubOpts<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for SubOpts<'a> { + fn get_size(&self) -> usize { + 0 + + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } + if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic: Cow<'a, str>, + pub signature: Option>, + pub key: Option>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(34) => msg.topic = r.read_string(bytes).map(Cow::Borrowed)?, + Ok(42) => msg.signature = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(50) => msg.key = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + 1 + sizeof_len((&self.topic).len()) + + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + w.write_with_tag(34, |w| w.write_string(&**&self.topic))?; + if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlMessage<'a> { + pub ihave: Vec>, + pub iwant: Vec>, + pub graft: Vec>, + pub prune: Vec>, +} + +impl<'a> MessageRead<'a> for ControlMessage<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ihave.push(r.read_message::(bytes)?), + Ok(18) => msg.iwant.push(r.read_message::(bytes)?), + Ok(26) => msg.graft.push(r.read_message::(bytes)?), + Ok(34) => msg.prune.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for ControlMessage<'a> { + fn get_size(&self) -> usize { + 0 + + self.ihave.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.iwant.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.graft.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.prune.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.ihave { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.iwant { w.write_with_tag(18, |w| w.write_message(s))?; } + for s in &self.graft { w.write_with_tag(26, |w| w.write_message(s))?; } + for s in &self.prune { w.write_with_tag(34, |w| w.write_message(s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlIHave<'a> { + pub topic_id: Option>, + pub message_ids: Vec>, +} + +impl<'a> MessageRead<'a> for ControlIHave<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.message_ids.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for ControlIHave<'a> { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + for s in &self.message_ids { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlIWant<'a> { + pub message_ids: Vec>, +} + +impl<'a> MessageRead<'a> for ControlIWant<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.message_ids.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for ControlIWant<'a> { + fn get_size(&self) -> usize { + 0 + + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.message_ids { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlGraft<'a> { + pub topic_id: Option>, +} + +impl<'a> MessageRead<'a> for ControlGraft<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for ControlGraft<'a> { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlPrune<'a> { + pub topic_id: Option>, + pub peers: Vec>, + pub backoff: Option, +} + +impl<'a> MessageRead<'a> for ControlPrune<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.peers.push(r.read_message::(bytes)?), + Ok(24) => msg.backoff = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for ControlPrune<'a> { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.peers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.backoff.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + for s in &self.peers { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.backoff { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PeerInfo<'a> { + pub peer_id: Option>, + pub signed_peer_record: Option>, +} + +impl<'a> MessageRead<'a> for PeerInfo<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.peer_id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.signed_peer_record = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for PeerInfo<'a> { + fn get_size(&self) -> usize { + 0 + + self.peer_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.signed_peer_record.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.peer_id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.signed_peer_record { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct TopicDescriptor<'a> { + pub name: Option>, + pub auth: Option>, + pub enc: Option>, +} + +impl<'a> MessageRead<'a> for TopicDescriptor<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.name = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.auth = Some(r.read_message::(bytes)?), + Ok(26) => msg.enc = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for TopicDescriptor<'a> { + fn get_size(&self) -> usize { + 0 + + self.name.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.auth.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.enc.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.name { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.auth { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.enc { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_TopicDescriptor { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct AuthOpts<'a> { + pub mode: Option, + pub keys: Vec>, +} + +impl<'a> MessageRead<'a> for AuthOpts<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.mode = Some(r.read_enum(bytes)?), + Ok(18) => msg.keys.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for AuthOpts<'a> { + fn get_size(&self) -> usize { + 0 + + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.keys.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + for s in &self.keys { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_AuthOpts { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum AuthMode { + NONE = 0, + KEY = 1, + WOT = 2, +} + +impl Default for AuthMode { + fn default() -> Self { + AuthMode::NONE + } +} + +impl From for AuthMode { + fn from(i: i32) -> Self { + match i { + 0 => AuthMode::NONE, + 1 => AuthMode::KEY, + 2 => AuthMode::WOT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for AuthMode { + fn from(s: &'a str) -> Self { + match s { + "NONE" => AuthMode::NONE, + "KEY" => AuthMode::KEY, + "WOT" => AuthMode::WOT, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct EncOpts<'a> { + pub mode: Option, + pub key_hashes: Vec>, +} + +impl<'a> MessageRead<'a> for EncOpts<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.mode = Some(r.read_enum(bytes)?), + Ok(18) => msg.key_hashes.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for EncOpts<'a> { + fn get_size(&self) -> usize { + 0 + + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.key_hashes.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + for s in &self.key_hashes { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_EncOpts { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum EncMode { + NONE = 0, + SHAREDKEY = 1, + WOT = 2, +} + +impl Default for EncMode { + fn default() -> Self { + EncMode::NONE + } +} + +impl From for EncMode { + fn from(i: i32) -> Self { + match i { + 0 => EncMode::NONE, + 1 => EncMode::SHAREDKEY, + 2 => EncMode::WOT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for EncMode { + fn from(s: &'a str) -> Self { + match s { + "NONE" => EncMode::NONE, + "SHAREDKEY" => EncMode::SHAREDKEY, + "WOT" => EncMode::WOT, + _ => Self::default(), + } + } +} + +} + +} + diff --git a/protocols/gossipsub/src/generated/mod.rs b/protocols/gossipsub/src/generated/mod.rs new file mode 100644 index 00000000000..7ac564f3c36 --- /dev/null +++ b/protocols/gossipsub/src/generated/mod.rs @@ -0,0 +1,3 @@ +// Automatically generated mod.rs +pub mod compat; +pub mod gossipsub; diff --git a/protocols/identify/src/generated/mod.rs b/protocols/identify/src/generated/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/protocols/identify/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/protocols/identify/src/generated/structs.rs b/protocols/identify/src/generated/structs.rs new file mode 100644 index 00000000000..99ca25c0dbc --- /dev/null +++ b/protocols/identify/src/generated/structs.rs @@ -0,0 +1,68 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Identify<'a> { + pub protocolVersion: Option>, + pub agentVersion: Option>, + pub publicKey: Option>, + pub listenAddrs: Vec>, + pub observedAddr: Option>, + pub protocols: Vec>, +} + +impl<'a> MessageRead<'a> for Identify<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(42) => msg.protocolVersion = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(50) => msg.agentVersion = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.publicKey = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.listenAddrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(34) => msg.observedAddr = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.protocols.push(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Identify<'a> { + fn get_size(&self) -> usize { + 0 + + self.protocolVersion.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.agentVersion.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.publicKey.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.listenAddrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.observedAddr.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.protocols.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.protocolVersion { w.write_with_tag(42, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.agentVersion { w.write_with_tag(50, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.publicKey { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + for s in &self.listenAddrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.observedAddr { w.write_with_tag(34, |w| w.write_bytes(&**s))?; } + for s in &self.protocols { w.write_with_tag(26, |w| w.write_string(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/kad/src/generated/dht/mod.rs b/protocols/kad/src/generated/dht/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/kad/src/generated/dht/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/kad/src/generated/dht/pb.rs b/protocols/kad/src/generated/dht/pb.rs new file mode 100644 index 00000000000..0ec5993a05b --- /dev/null +++ b/protocols/kad/src/generated/dht/pb.rs @@ -0,0 +1,245 @@ +// Automatically generated rust module for 'dht.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Record<'a> { + pub key: Cow<'a, [u8]>, + pub value: Cow<'a, [u8]>, + pub timeReceived: Cow<'a, str>, + pub publisher: Cow<'a, [u8]>, + pub ttl: u32, +} + +impl<'a> MessageRead<'a> for Record<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.key = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.value = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(42) => msg.timeReceived = r.read_string(bytes).map(Cow::Borrowed)?, + Ok(5330) => msg.publisher = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(6216) => msg.ttl = r.read_uint32(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Record<'a> { + fn get_size(&self) -> usize { + 0 + + if self.key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.key).len()) } + + if self.value == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.value).len()) } + + if self.timeReceived == "" { 0 } else { 1 + sizeof_len((&self.timeReceived).len()) } + + if self.publisher == Cow::Borrowed(b"") { 0 } else { 2 + sizeof_len((&self.publisher).len()) } + + if self.ttl == 0u32 { 0 } else { 2 + sizeof_varint(*(&self.ttl) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.key != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.key))?; } + if self.value != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.value))?; } + if self.timeReceived != "" { w.write_with_tag(42, |w| w.write_string(&**&self.timeReceived))?; } + if self.publisher != Cow::Borrowed(b"") { w.write_with_tag(5330, |w| w.write_bytes(&**&self.publisher))?; } + if self.ttl != 0u32 { w.write_with_tag(6216, |w| w.write_uint32(*&self.ttl))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub type_pb: dht::pb::mod_Message::MessageType, + pub clusterLevelRaw: i32, + pub key: Cow<'a, [u8]>, + pub record: Option>, + pub closerPeers: Vec>, + pub providerPeers: Vec>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(80) => msg.clusterLevelRaw = r.read_int32(bytes)?, + Ok(18) => msg.key = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(26) => msg.record = Some(r.read_message::(bytes)?), + Ok(66) => msg.closerPeers.push(r.read_message::(bytes)?), + Ok(74) => msg.providerPeers.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + if self.type_pb == dht::pb::mod_Message::MessageType::PUT_VALUE { 0 } else { 1 + sizeof_varint(*(&self.type_pb) as u64) } + + if self.clusterLevelRaw == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.clusterLevelRaw) as u64) } + + if self.key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.key).len()) } + + self.record.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.closerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.providerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.type_pb != dht::pb::mod_Message::MessageType::PUT_VALUE { w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; } + if self.clusterLevelRaw != 0i32 { w.write_with_tag(80, |w| w.write_int32(*&self.clusterLevelRaw))?; } + if self.key != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.key))?; } + if let Some(ref s) = self.record { w.write_with_tag(26, |w| w.write_message(s))?; } + for s in &self.closerPeers { w.write_with_tag(66, |w| w.write_message(s))?; } + for s in &self.providerPeers { w.write_with_tag(74, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_Message { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Peer<'a> { + pub id: Cow<'a, [u8]>, + pub addrs: Vec>, + pub connection: dht::pb::mod_Message::ConnectionType, +} + +impl<'a> MessageRead<'a> for Peer<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(24) => msg.connection = r.read_enum(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Peer<'a> { + fn get_size(&self) -> usize { + 0 + + if self.id == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.id).len()) } + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + if self.connection == dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { 0 } else { 1 + sizeof_varint(*(&self.connection) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.id != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; } + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if self.connection != dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { w.write_with_tag(24, |w| w.write_enum(*&self.connection as i32))?; } + Ok(()) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MessageType { + PUT_VALUE = 0, + GET_VALUE = 1, + ADD_PROVIDER = 2, + GET_PROVIDERS = 3, + FIND_NODE = 4, + PING = 5, +} + +impl Default for MessageType { + fn default() -> Self { + MessageType::PUT_VALUE + } +} + +impl From for MessageType { + fn from(i: i32) -> Self { + match i { + 0 => MessageType::PUT_VALUE, + 1 => MessageType::GET_VALUE, + 2 => MessageType::ADD_PROVIDER, + 3 => MessageType::GET_PROVIDERS, + 4 => MessageType::FIND_NODE, + 5 => MessageType::PING, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for MessageType { + fn from(s: &'a str) -> Self { + match s { + "PUT_VALUE" => MessageType::PUT_VALUE, + "GET_VALUE" => MessageType::GET_VALUE, + "ADD_PROVIDER" => MessageType::ADD_PROVIDER, + "GET_PROVIDERS" => MessageType::GET_PROVIDERS, + "FIND_NODE" => MessageType::FIND_NODE, + "PING" => MessageType::PING, + _ => Self::default(), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ConnectionType { + NOT_CONNECTED = 0, + CONNECTED = 1, + CAN_CONNECT = 2, + CANNOT_CONNECT = 3, +} + +impl Default for ConnectionType { + fn default() -> Self { + ConnectionType::NOT_CONNECTED + } +} + +impl From for ConnectionType { + fn from(i: i32) -> Self { + match i { + 0 => ConnectionType::NOT_CONNECTED, + 1 => ConnectionType::CONNECTED, + 2 => ConnectionType::CAN_CONNECT, + 3 => ConnectionType::CANNOT_CONNECT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ConnectionType { + fn from(s: &'a str) -> Self { + match s { + "NOT_CONNECTED" => ConnectionType::NOT_CONNECTED, + "CONNECTED" => ConnectionType::CONNECTED, + "CAN_CONNECT" => ConnectionType::CAN_CONNECT, + "CANNOT_CONNECT" => ConnectionType::CANNOT_CONNECT, + _ => Self::default(), + } + } +} + +} + diff --git a/protocols/kad/src/generated/mod.rs b/protocols/kad/src/generated/mod.rs new file mode 100644 index 00000000000..6ffc566d1a0 --- /dev/null +++ b/protocols/kad/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod dht; diff --git a/protocols/relay/src/generated/message_v2/mod.rs b/protocols/relay/src/generated/message_v2/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/relay/src/generated/message_v2/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/relay/src/generated/message_v2/pb.rs b/protocols/relay/src/generated/message_v2/pb.rs new file mode 100644 index 00000000000..ef10b27329f --- /dev/null +++ b/protocols/relay/src/generated/message_v2/pb.rs @@ -0,0 +1,347 @@ +// Automatically generated rust module for 'message.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Status { + OK = 100, + RESERVATION_REFUSED = 200, + RESOURCE_LIMIT_EXCEEDED = 201, + PERMISSION_DENIED = 202, + CONNECTION_FAILED = 203, + NO_RESERVATION = 204, + MALFORMED_MESSAGE = 400, + UNEXPECTED_MESSAGE = 401, +} + +impl Default for Status { + fn default() -> Self { + Status::OK + } +} + +impl From for Status { + fn from(i: i32) -> Self { + match i { + 100 => Status::OK, + 200 => Status::RESERVATION_REFUSED, + 201 => Status::RESOURCE_LIMIT_EXCEEDED, + 202 => Status::PERMISSION_DENIED, + 203 => Status::CONNECTION_FAILED, + 204 => Status::NO_RESERVATION, + 400 => Status::MALFORMED_MESSAGE, + 401 => Status::UNEXPECTED_MESSAGE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Status { + fn from(s: &'a str) -> Self { + match s { + "OK" => Status::OK, + "RESERVATION_REFUSED" => Status::RESERVATION_REFUSED, + "RESOURCE_LIMIT_EXCEEDED" => Status::RESOURCE_LIMIT_EXCEEDED, + "PERMISSION_DENIED" => Status::PERMISSION_DENIED, + "CONNECTION_FAILED" => Status::CONNECTION_FAILED, + "NO_RESERVATION" => Status::NO_RESERVATION, + "MALFORMED_MESSAGE" => Status::MALFORMED_MESSAGE, + "UNEXPECTED_MESSAGE" => Status::UNEXPECTED_MESSAGE, + _ => Self::default(), + } + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct HopMessage<'a> { + pub type_pb: message_v2::pb::mod_HopMessage::Type, + pub peer: Option>, + pub reservation: Option>, + pub limit: Option, + pub status: Option, +} + +impl<'a> MessageRead<'a> for HopMessage<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(18) => msg.peer = Some(r.read_message::(bytes)?), + Ok(26) => msg.reservation = Some(r.read_message::(bytes)?), + Ok(34) => msg.limit = Some(r.read_message::(bytes)?), + Ok(40) => msg.status = Some(r.read_enum(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for HopMessage<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.type_pb) as u64) + + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.reservation.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; + if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.reservation { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(34, |w| w.write_message(s))?; } + if let Some(ref s) = self.status { w.write_with_tag(40, |w| w.write_enum(*s as i32))?; } + Ok(()) + } +} + +pub mod mod_HopMessage { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Type { + RESERVE = 0, + CONNECT = 1, + STATUS = 2, +} + +impl Default for Type { + fn default() -> Self { + Type::RESERVE + } +} + +impl From for Type { + fn from(i: i32) -> Self { + match i { + 0 => Type::RESERVE, + 1 => Type::CONNECT, + 2 => Type::STATUS, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Type { + fn from(s: &'a str) -> Self { + match s { + "RESERVE" => Type::RESERVE, + "CONNECT" => Type::CONNECT, + "STATUS" => Type::STATUS, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct StopMessage<'a> { + pub type_pb: message_v2::pb::mod_StopMessage::Type, + pub peer: Option>, + pub limit: Option, + pub status: Option, +} + +impl<'a> MessageRead<'a> for StopMessage<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(18) => msg.peer = Some(r.read_message::(bytes)?), + Ok(26) => msg.limit = Some(r.read_message::(bytes)?), + Ok(32) => msg.status = Some(r.read_enum(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for StopMessage<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.type_pb) as u64) + + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; + if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.status { w.write_with_tag(32, |w| w.write_enum(*s as i32))?; } + Ok(()) + } +} + +pub mod mod_StopMessage { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Type { + CONNECT = 0, + STATUS = 1, +} + +impl Default for Type { + fn default() -> Self { + Type::CONNECT + } +} + +impl From for Type { + fn from(i: i32) -> Self { + match i { + 0 => Type::CONNECT, + 1 => Type::STATUS, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Type { + fn from(s: &'a str) -> Self { + match s { + "CONNECT" => Type::CONNECT, + "STATUS" => Type::STATUS, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Peer<'a> { + pub id: Cow<'a, [u8]>, + pub addrs: Vec>, +} + +impl<'a> MessageRead<'a> for Peer<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Peer<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_len((&self.id).len()) + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Reservation<'a> { + pub expire: u64, + pub addrs: Vec>, + pub voucher: Option>, +} + +impl<'a> MessageRead<'a> for Reservation<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.expire = r.read_uint64(bytes)?, + Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.voucher = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Reservation<'a> { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.expire) as u64) + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.voucher.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_uint64(*&self.expire))?; + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.voucher { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Limit { + pub duration: Option, + pub data: Option, +} + +impl<'a> MessageRead<'a> for Limit { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.duration = Some(r.read_uint32(bytes)?), + Ok(16) => msg.data = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Limit { + fn get_size(&self) -> usize { + 0 + + self.duration.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.duration { w.write_with_tag(8, |w| w.write_uint32(*s))?; } + if let Some(ref s) = self.data { w.write_with_tag(16, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + diff --git a/protocols/relay/src/generated/mod.rs b/protocols/relay/src/generated/mod.rs new file mode 100644 index 00000000000..18bb20c7eb0 --- /dev/null +++ b/protocols/relay/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod message_v2; diff --git a/protocols/rendezvous/src/generated/mod.rs b/protocols/rendezvous/src/generated/mod.rs new file mode 100644 index 00000000000..448a0207b2d --- /dev/null +++ b/protocols/rendezvous/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod rendezvous; diff --git a/protocols/rendezvous/src/generated/rendezvous/mod.rs b/protocols/rendezvous/src/generated/rendezvous/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/rendezvous/src/generated/rendezvous/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/rendezvous/src/generated/rendezvous/pb.rs b/protocols/rendezvous/src/generated/rendezvous/pb.rs new file mode 100644 index 00000000000..45a84862e1a --- /dev/null +++ b/protocols/rendezvous/src/generated/rendezvous/pb.rs @@ -0,0 +1,365 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub type_pb: Option, + pub register: Option>, + pub registerResponse: Option>, + pub unregister: Option>, + pub discover: Option>, + pub discoverResponse: Option>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = Some(r.read_enum(bytes)?), + Ok(18) => msg.register = Some(r.read_message::(bytes)?), + Ok(26) => msg.registerResponse = Some(r.read_message::(bytes)?), + Ok(34) => msg.unregister = Some(r.read_message::(bytes)?), + Ok(42) => msg.discover = Some(r.read_message::(bytes)?), + Ok(50) => msg.discoverResponse = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.register.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.registerResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.unregister.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.discover.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.discoverResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.type_pb { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.register { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.registerResponse { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.unregister { w.write_with_tag(34, |w| w.write_message(s))?; } + if let Some(ref s) = self.discover { w.write_with_tag(42, |w| w.write_message(s))?; } + if let Some(ref s) = self.discoverResponse { w.write_with_tag(50, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_Message { + +use std::borrow::Cow; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Register<'a> { + pub ns: Option>, + pub signedPeerRecord: Option>, + pub ttl: Option, +} + +impl<'a> MessageRead<'a> for Register<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.signedPeerRecord = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Register<'a> { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.signedPeerRecord.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.signedPeerRecord { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RegisterResponse<'a> { + pub status: Option, + pub statusText: Option>, + pub ttl: Option, +} + +impl<'a> MessageRead<'a> for RegisterResponse<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.status = Some(r.read_enum(bytes)?), + Ok(18) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for RegisterResponse<'a> { + fn get_size(&self) -> usize { + 0 + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.status { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.statusText { w.write_with_tag(18, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Unregister<'a> { + pub ns: Option>, + pub id: Option>, +} + +impl<'a> MessageRead<'a> for Unregister<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Unregister<'a> { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.id { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Discover<'a> { + pub ns: Option>, + pub limit: Option, + pub cookie: Option>, +} + +impl<'a> MessageRead<'a> for Discover<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(16) => msg.limit = Some(r.read_uint64(bytes)?), + Ok(26) => msg.cookie = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Discover<'a> { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(16, |w| w.write_uint64(*s))?; } + if let Some(ref s) = self.cookie { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DiscoverResponse<'a> { + pub registrations: Vec>, + pub cookie: Option>, + pub status: Option, + pub statusText: Option>, +} + +impl<'a> MessageRead<'a> for DiscoverResponse<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.registrations.push(r.read_message::(bytes)?), + Ok(18) => msg.cookie = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(24) => msg.status = Some(r.read_enum(bytes)?), + Ok(34) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for DiscoverResponse<'a> { + fn get_size(&self) -> usize { + 0 + + self.registrations.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.registrations { w.write_with_tag(10, |w| w.write_message(s))?; } + if let Some(ref s) = self.cookie { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.status { w.write_with_tag(24, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.statusText { w.write_with_tag(34, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MessageType { + REGISTER = 0, + REGISTER_RESPONSE = 1, + UNREGISTER = 2, + DISCOVER = 3, + DISCOVER_RESPONSE = 4, +} + +impl Default for MessageType { + fn default() -> Self { + MessageType::REGISTER + } +} + +impl From for MessageType { + fn from(i: i32) -> Self { + match i { + 0 => MessageType::REGISTER, + 1 => MessageType::REGISTER_RESPONSE, + 2 => MessageType::UNREGISTER, + 3 => MessageType::DISCOVER, + 4 => MessageType::DISCOVER_RESPONSE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for MessageType { + fn from(s: &'a str) -> Self { + match s { + "REGISTER" => MessageType::REGISTER, + "REGISTER_RESPONSE" => MessageType::REGISTER_RESPONSE, + "UNREGISTER" => MessageType::UNREGISTER, + "DISCOVER" => MessageType::DISCOVER, + "DISCOVER_RESPONSE" => MessageType::DISCOVER_RESPONSE, + _ => Self::default(), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ResponseStatus { + OK = 0, + E_INVALID_NAMESPACE = 100, + E_INVALID_SIGNED_PEER_RECORD = 101, + E_INVALID_TTL = 102, + E_INVALID_COOKIE = 103, + E_NOT_AUTHORIZED = 200, + E_INTERNAL_ERROR = 300, + E_UNAVAILABLE = 400, +} + +impl Default for ResponseStatus { + fn default() -> Self { + ResponseStatus::OK + } +} + +impl From for ResponseStatus { + fn from(i: i32) -> Self { + match i { + 0 => ResponseStatus::OK, + 100 => ResponseStatus::E_INVALID_NAMESPACE, + 101 => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, + 102 => ResponseStatus::E_INVALID_TTL, + 103 => ResponseStatus::E_INVALID_COOKIE, + 200 => ResponseStatus::E_NOT_AUTHORIZED, + 300 => ResponseStatus::E_INTERNAL_ERROR, + 400 => ResponseStatus::E_UNAVAILABLE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ResponseStatus { + fn from(s: &'a str) -> Self { + match s { + "OK" => ResponseStatus::OK, + "E_INVALID_NAMESPACE" => ResponseStatus::E_INVALID_NAMESPACE, + "E_INVALID_SIGNED_PEER_RECORD" => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, + "E_INVALID_TTL" => ResponseStatus::E_INVALID_TTL, + "E_INVALID_COOKIE" => ResponseStatus::E_INVALID_COOKIE, + "E_NOT_AUTHORIZED" => ResponseStatus::E_NOT_AUTHORIZED, + "E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR, + "E_UNAVAILABLE" => ResponseStatus::E_UNAVAILABLE, + _ => Self::default(), + } + } +} + +} + diff --git a/transports/noise/src/generated/mod.rs b/transports/noise/src/generated/mod.rs new file mode 100644 index 00000000000..c2153dd9066 --- /dev/null +++ b/transports/noise/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod payload; diff --git a/transports/noise/src/generated/payload/mod.rs b/transports/noise/src/generated/payload/mod.rs new file mode 100644 index 00000000000..5e3889634fb --- /dev/null +++ b/transports/noise/src/generated/payload/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod proto; diff --git a/transports/noise/src/generated/payload/proto.rs b/transports/noise/src/generated/payload/proto.rs new file mode 100644 index 00000000000..5481910f796 --- /dev/null +++ b/transports/noise/src/generated/payload/proto.rs @@ -0,0 +1,56 @@ +// Automatically generated rust module for 'payload.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct NoiseHandshakePayload<'a> { + pub identity_key: Cow<'a, [u8]>, + pub identity_sig: Cow<'a, [u8]>, + pub data: Cow<'a, [u8]>, +} + +impl<'a> MessageRead<'a> for NoiseHandshakePayload<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.identity_key = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.identity_sig = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(26) => msg.data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for NoiseHandshakePayload<'a> { + fn get_size(&self) -> usize { + 0 + + if self.identity_key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.identity_key).len()) } + + if self.identity_sig == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.identity_sig).len()) } + + if self.data == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.data).len()) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.identity_key != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.identity_key))?; } + if self.identity_sig != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.identity_sig))?; } + if self.data != Cow::Borrowed(b"") { w.write_with_tag(26, |w| w.write_bytes(&**&self.data))?; } + Ok(()) + } +} + diff --git a/transports/plaintext/src/generated/mod.rs b/transports/plaintext/src/generated/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/transports/plaintext/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/transports/plaintext/src/generated/structs.rs b/transports/plaintext/src/generated/structs.rs new file mode 100644 index 00000000000..306d78db920 --- /dev/null +++ b/transports/plaintext/src/generated/structs.rs @@ -0,0 +1,52 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Exchange<'a> { + pub id: Option>, + pub pubkey: Option>, +} + +impl<'a> MessageRead<'a> for Exchange<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.pubkey = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Exchange<'a> { + fn get_size(&self) -> usize { + 0 + + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.pubkey.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.pubkey { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + diff --git a/transports/webrtc/src/generated/mod.rs b/transports/webrtc/src/generated/mod.rs new file mode 100644 index 00000000000..5e9f6373b12 --- /dev/null +++ b/transports/webrtc/src/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod webrtc; diff --git a/transports/webrtc/src/generated/webrtc/mod.rs b/transports/webrtc/src/generated/webrtc/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/transports/webrtc/src/generated/webrtc/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/transports/webrtc/src/generated/webrtc/pb.rs b/transports/webrtc/src/generated/webrtc/pb.rs new file mode 100644 index 00000000000..ac6943dcf59 --- /dev/null +++ b/transports/webrtc/src/generated/webrtc/pb.rs @@ -0,0 +1,92 @@ +// Automatically generated rust module for 'message.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use std::borrow::Cow; +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message<'a> { + pub flag: Option, + pub message: Option>, +} + +impl<'a> MessageRead<'a> for Message<'a> { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.flag = Some(r.read_enum(bytes)?), + Ok(18) => msg.message = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl<'a> MessageWrite for Message<'a> { + fn get_size(&self) -> usize { + 0 + + self.flag.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.message.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.flag { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.message { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_Message { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Flag { + FIN = 0, + STOP_SENDING = 1, + RESET = 2, +} + +impl Default for Flag { + fn default() -> Self { + Flag::FIN + } +} + +impl From for Flag { + fn from(i: i32) -> Self { + match i { + 0 => Flag::FIN, + 1 => Flag::STOP_SENDING, + 2 => Flag::RESET, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Flag { + fn from(s: &'a str) -> Self { + match s { + "FIN" => Flag::FIN, + "STOP_SENDING" => Flag::STOP_SENDING, + "RESET" => Flag::RESET, + _ => Self::default(), + } + } +} + +} + From 44b346503320a9de424d74a7584c67efd7ded1d0 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 6 Jan 2023 20:20:41 -0500 Subject: [PATCH 03/66] Add ci --- .github/workflows/ci.yml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ce4fc5fdea..40e49f8b5d7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -305,4 +305,22 @@ jobs: then echo "PR title is too long (greater than 72 characters)" exit 1 - fi \ No newline at end of file + fi + + check-proto-files: + name: Check for changes in proto files + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7 + with: + profile: minimal + toolchain: stable + override: true + + - name: Generated proto files and diff + run: | + cargo install pb-rs + pb-rs **/generated/**.proto + git diff --exit-code ${{ github.ref }} From 4dca4d0fb9a2eb9cb4c3afe866259a873ebd99a5 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 6 Jan 2023 21:05:14 -0500 Subject: [PATCH 04/66] Try adding quotes --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40e49f8b5d7..44507588cbd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -322,5 +322,5 @@ jobs: - name: Generated proto files and diff run: | cargo install pb-rs - pb-rs **/generated/**.proto + pb-rs "**/generated/**.proto" git diff --exit-code ${{ github.ref }} From 6b19385133c0729ad3acfbfdd5a8ea0b356dc1cc Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 21:30:55 -0500 Subject: [PATCH 05/66] Add input variable --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 44507588cbd..6a6bc0e88cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -320,7 +320,11 @@ jobs: override: true - name: Generated proto files and diff + inputs: + proto-files: + description: "Proto files" + default: "**/generated/**.proto" run: | cargo install pb-rs - pb-rs "**/generated/**.proto" + pb-rs "${{ inputs.proto-files }}" git diff --exit-code ${{ github.ref }} From 9d4b8640e09e6e9550038bd9570dc0767e853d97 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 21:40:51 -0500 Subject: [PATCH 06/66] Move inputs --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6a6bc0e88cc..2bee7f2bcfd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -310,6 +310,10 @@ jobs: check-proto-files: name: Check for changes in proto files runs-on: ubuntu-latest + inputs: + proto-files: + description: "Proto files" + default: "**/generated/**.proto" steps: - uses: actions/checkout@v3 @@ -320,10 +324,6 @@ jobs: override: true - name: Generated proto files and diff - inputs: - proto-files: - description: "Proto files" - default: "**/generated/**.proto" run: | cargo install pb-rs pb-rs "${{ inputs.proto-files }}" From c9853f746e35b52408440163c82820163a82cf12 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 21:45:13 -0500 Subject: [PATCH 07/66] Move inputs 2 --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2bee7f2bcfd..9f3cd2b77fe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,10 @@ on: push: branches: - master + inputs: + proto-files: + description: "Proto files" + default: "**/generated/**.proto" concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -310,10 +314,6 @@ jobs: check-proto-files: name: Check for changes in proto files runs-on: ubuntu-latest - inputs: - proto-files: - description: "Proto files" - default: "**/generated/**.proto" steps: - uses: actions/checkout@v3 From 9716fe5b41314b61bff92dff04f3a29599cd18e9 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 21:49:45 -0500 Subject: [PATCH 08/66] Glob into variable --- .github/workflows/ci.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f3cd2b77fe..2e8d26ea147 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,10 +5,6 @@ on: push: branches: - master - inputs: - proto-files: - description: "Proto files" - default: "**/generated/**.proto" concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -326,5 +322,6 @@ jobs: - name: Generated proto files and diff run: | cargo install pb-rs - pb-rs "${{ inputs.proto-files }}" + protofiles="**/generated/**.proto" + pb-rs $protofiles git diff --exit-code ${{ github.ref }} From 1d1dff0070c685842a84b38687faabcc100ffe2e Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 21:58:35 -0500 Subject: [PATCH 09/66] Glob into variable 2 --- .github/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e8d26ea147..ce808fb83ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -322,6 +322,7 @@ jobs: - name: Generated proto files and diff run: | cargo install pb-rs - protofiles="**/generated/**.proto" - pb-rs $protofiles + PROTOFILES=(**/generated/**.proto) + echo $PROTOFILES + pb-rs $PROTOFILES git diff --exit-code ${{ github.ref }} From b2aabcdd531f4217335059e246fbb9f7b8f143c7 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 22:09:01 -0500 Subject: [PATCH 10/66] Use glob --- .github/workflows/ci.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce808fb83ea..6eaec499598 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -319,10 +319,16 @@ jobs: toolchain: stable override: true + - name: Glob match + uses: tj-actions/glob@v16 + id: glob + with: + files: | + **/generated/**.proto + - name: Generated proto files and diff run: | cargo install pb-rs - PROTOFILES=(**/generated/**.proto) - echo $PROTOFILES - pb-rs $PROTOFILES + echo "${{ steps.glob.outputs.paths }}" + pb-rs "${{ steps.glob.outputs.paths }}" git diff --exit-code ${{ github.ref }} From 559a1d3aacd43abccb35b2945fd2a733c33f88d2 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 22:14:27 -0500 Subject: [PATCH 11/66] Remove quotes --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6eaec499598..0854ba2f034 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -329,6 +329,6 @@ jobs: - name: Generated proto files and diff run: | cargo install pb-rs - echo "${{ steps.glob.outputs.paths }}" - pb-rs "${{ steps.glob.outputs.paths }}" + echo ${{ steps.glob.outputs.paths }} + pb-rs ${{ steps.glob.outputs.paths }} git diff --exit-code ${{ github.ref }} From d62b50d1a863e6ab911130209b0ad9f10aa11ec8 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 22:17:16 -0500 Subject: [PATCH 12/66] Remove refs --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0854ba2f034..a5890812a87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -331,4 +331,4 @@ jobs: cargo install pb-rs echo ${{ steps.glob.outputs.paths }} pb-rs ${{ steps.glob.outputs.paths }} - git diff --exit-code ${{ github.ref }} + git diff --exit-code From b894ac7b6398f5643061be8e2ad85bcbfe75b7cf Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 11 Jan 2023 22:20:26 -0500 Subject: [PATCH 13/66] Test glob pattern --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5890812a87..00580654a1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -324,7 +324,7 @@ jobs: id: glob with: files: | - **/generated/**.proto + **/**.proto - name: Generated proto files and diff run: | From 391a892a8a1b30105493580fbd1afd3dd68f091e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:01:53 +1100 Subject: [PATCH 14/66] Bad commit --- core/src/generated/envelope_proto.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/generated/envelope_proto.rs b/core/src/generated/envelope_proto.rs index 32cfc7ed207..d53ae0b6a7d 100644 --- a/core/src/generated/envelope_proto.rs +++ b/core/src/generated/envelope_proto.rs @@ -19,7 +19,7 @@ use super::*; pub struct Envelope<'a> { pub public_key: Option>, pub payload_type: Cow<'a, [u8]>, - pub payload: Cow<'a, [u8]>, + pub payload: Cow<'a, [u8]>, pub signature: Cow<'a, [u8]>, } From 29919943e6b4fc0b6f2c3203980632414b46ab24 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:04:48 +1100 Subject: [PATCH 15/66] Remove explicit echo This is unnecessary as we can see the command being executed in the action log. See https://github.com/libp2p/rust-libp2p/actions/runs/3898847742/jobs/6657969252#step:5:4. --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00580654a1f..d27ceb4f7c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -329,6 +329,5 @@ jobs: - name: Generated proto files and diff run: | cargo install pb-rs - echo ${{ steps.glob.outputs.paths }} pb-rs ${{ steps.glob.outputs.paths }} git diff --exit-code From 8a25c844f79e8286fbf3f2bba2375abfa1a7b45b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:14:02 +1100 Subject: [PATCH 16/66] Fail CI on untracked files --- .github/workflows/ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d27ceb4f7c9..391c8da5139 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -326,8 +326,10 @@ jobs: files: | **/**.proto - - name: Generated proto files and diff + - name: Generated proto files run: | - cargo install pb-rs + cargo install pb-rs pb-rs ${{ steps.glob.outputs.paths }} - git diff --exit-code + + - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 + run: test -z "$(git status --porcelain)" From ccc4541e57c704855dcb4d011b0f38a677faa433 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:14:21 +1100 Subject: [PATCH 17/66] Revert "Bad commit" This reverts commit 391a892a8a1b30105493580fbd1afd3dd68f091e. --- core/src/generated/envelope_proto.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/generated/envelope_proto.rs b/core/src/generated/envelope_proto.rs index d53ae0b6a7d..32cfc7ed207 100644 --- a/core/src/generated/envelope_proto.rs +++ b/core/src/generated/envelope_proto.rs @@ -19,7 +19,7 @@ use super::*; pub struct Envelope<'a> { pub public_key: Option>, pub payload_type: Cow<'a, [u8]>, - pub payload: Cow<'a, [u8]>, + pub payload: Cow<'a, [u8]>, pub signature: Cow<'a, [u8]>, } From f464e3b4d1de10974a874ef7938145f8f2f0dc0c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:17:21 +1100 Subject: [PATCH 18/66] Cache installed `pb-rs` binary --- .github/workflows/ci.yml | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 391c8da5139..8b205370e6f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -313,11 +313,9 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7 - with: - profile: minimal - toolchain: stable - override: true + - uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0 + + - run: cargo install pb-rs --locked - name: Glob match uses: tj-actions/glob@v16 @@ -326,10 +324,8 @@ jobs: files: | **/**.proto - - name: Generated proto files - run: | - cargo install pb-rs - pb-rs ${{ steps.glob.outputs.paths }} + - name: Generate proto files + run: pb-rs ${{ steps.glob.outputs.paths }} - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 run: test -z "$(git status --porcelain)" From d2ed2c6ca90c839834d65322014bdea8fcc28ded Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:19:13 +1100 Subject: [PATCH 19/66] Show bad proto files to the user --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b205370e6f..f1d22bdf285 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -328,4 +328,6 @@ jobs: run: pb-rs ${{ steps.glob.outputs.paths }} - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 - run: test -z "$(git status --porcelain)" + run: | + git status --porcelain # Show output to the user + test -z "$(git status --porcelain)" # Fail CI if there are any modifications From c6dd4e4fea4d38edb5293b45cf2850c8a7126583 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 10:31:44 +1100 Subject: [PATCH 20/66] Don't duplicate command --- .github/workflows/ci.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1d22bdf285..257c1f42432 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -329,5 +329,7 @@ jobs: - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 run: | - git status --porcelain # Show output to the user - test -z "$(git status --porcelain)" # Fail CI if there are any modifications + git_status=$(git status --porcelain) + + echo $git_status + test -z "$git_status" From 367a1a489f5a92c8a8d1c94dec7685b4357ca2d1 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 11:05:59 +1100 Subject: [PATCH 21/66] Only run on files in `generated/` directory --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 257c1f42432..76199f93873 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -322,7 +322,7 @@ jobs: id: glob with: files: | - **/**.proto + **/generated/*.proto - name: Generate proto files run: pb-rs ${{ steps.glob.outputs.paths }} From 78a483f1382fb93f99abcc0e31529229d5d270ce Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 19:04:34 -0500 Subject: [PATCH 22/66] Remove prost from core --- core/Cargo.toml | 5 +- core/build.rs | 31 ---------- core/src/envelope.proto | 30 --------- core/src/identity.rs | 117 +++++++++++++++++++----------------- core/src/identity/error.rs | 7 --- core/src/keys.proto | 20 ------ core/src/lib.rs | 17 ++---- core/src/peer_record.proto | 27 --------- core/src/peer_record.rs | 30 +++++---- core/src/signed_envelope.rs | 33 +++++----- 10 files changed, 101 insertions(+), 216 deletions(-) delete mode 100644 core/build.rs delete mode 100644 core/src/envelope.proto delete mode 100644 core/src/keys.proto delete mode 100644 core/src/peer_record.proto diff --git a/core/Cargo.toml b/core/Cargo.toml index d52fa05805c..474ee1102d4 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -27,7 +27,7 @@ multistream-select = { version = "0.12.1", path = "../misc/multistream-select" } p256 = { version = "0.11.1", default-features = false, features = ["ecdsa", "std"], optional = true } parking_lot = "0.12.0" pin-project = "1.0.0" -prost = "0.11" +quick-protobuf = "0.8" once_cell = "1.16.0" rand = "0.8" rw-stream-sink = { version = "0.3.0", path = "../misc/rw-stream-sink" } @@ -54,9 +54,6 @@ quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" } rmp-serde = "1.0" serde_json = "1.0" -[build-dependencies] -prost-build = "0.11" - [features] secp256k1 = [ "libsecp256k1" ] ecdsa = [ "p256" ] diff --git a/core/build.rs b/core/build.rs deleted file mode 100644 index f0c09f93abf..00000000000 --- a/core/build.rs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos( - &[ - "src/keys.proto", - "src/envelope.proto", - "src/peer_record.proto", - ], - &["src"], - ) - .unwrap(); -} diff --git a/core/src/envelope.proto b/core/src/envelope.proto deleted file mode 100644 index 9ab3e6fd256..00000000000 --- a/core/src/envelope.proto +++ /dev/null @@ -1,30 +0,0 @@ -syntax = "proto3"; - -package envelope_proto; - -import "keys.proto"; - -// Envelope encloses a signed payload produced by a peer, along with the public -// key of the keypair it was signed with so that it can be statelessly validated -// by the receiver. -// -// The payload is prefixed with a byte string that determines the type, so it -// can be deserialized deterministically. Often, this byte string is a -// multicodec. -message Envelope { - // public_key is the public key of the keypair the enclosed payload was - // signed with. - keys_proto.PublicKey public_key = 1; - - // payload_type encodes the type of payload, so that it can be deserialized - // deterministically. - bytes payload_type = 2; - - // payload is the actual payload carried inside this envelope. - bytes payload = 3; - - // signature is the signature produced by the private key corresponding to - // the enclosed public key, over the payload, prefixing a domain string for - // additional security. - bytes signature = 5; -} diff --git a/core/src/identity.rs b/core/src/identity.rs index 45ed6be2db9..6466f316da6 100644 --- a/core/src/identity.rs +++ b/core/src/identity.rs @@ -42,9 +42,11 @@ pub mod secp256k1; pub mod error; +use std::borrow::Cow; use self::error::*; -use crate::{keys_proto, PeerId}; +use crate::{PeerId, proto}; use std::convert::{TryFrom, TryInto}; +use quick_protobuf::{BytesReader, Writer}; /// Identity keypair of a node. /// @@ -147,12 +149,12 @@ impl Keypair { /// Encode a private key as protobuf structure. pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { - use prost::Message; + use quick_protobuf::MessageWrite; let pk = match self { - Self::Ed25519(data) => keys_proto::PrivateKey { - r#type: keys_proto::KeyType::Ed25519.into(), - data: data.encode().into(), + Self::Ed25519(data) => proto::PrivateKey { + Type: proto::KeyType::Ed25519, + Data: Cow::from(data.encode().to_vec()), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] Self::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), @@ -162,35 +164,38 @@ impl Keypair { Self::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), }; - Ok(pk.encode_to_vec()) + let mut buf = Vec::with_capacity(pk.get_size()); + let mut writer = Writer::new(&mut buf); + pk.write_message(&mut writer).expect("Encoding to succeed"); + + Ok(buf) } /// Decode a private key from a protobuf structure and parse it as a [`Keypair`]. pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - use prost::Message; + use quick_protobuf::MessageRead; - let mut private_key = keys_proto::PrivateKey::decode(bytes) + let mut reader = BytesReader::from_bytes(bytes); + let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) .map(zeroize::Zeroizing::new)?; - let key_type = keys_proto::KeyType::from_i32(private_key.r#type) - .ok_or_else(|| DecodingError::unknown_key_type(private_key.r#type))?; - match key_type { - keys_proto::KeyType::Ed25519 => { - ed25519::Keypair::decode(&mut private_key.data).map(Keypair::Ed25519) + match private_key.Type { + proto::KeyType::Ed25519 => { + ed25519::Keypair::decode(private_key.Data.to_mut()).map(Keypair::Ed25519) } - keys_proto::KeyType::Rsa => Err(DecodingError::decoding_unsupported("RSA")), - keys_proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), - keys_proto::KeyType::Ecdsa => Err(DecodingError::decoding_unsupported("ECDSA")), + proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), + proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), + proto::KeyType::ECDSA => Err(DecodingError::decoding_unsupported("ECDSA")), } } } -impl zeroize::Zeroize for keys_proto::PrivateKey { +impl zeroize::Zeroize for proto::PrivateKey<'_> { fn zeroize(&mut self) { - self.r#type.zeroize(); - self.data.zeroize(); + self.Type = proto::KeyType::default(); + self.Data = Cow::default(); } } @@ -232,23 +237,25 @@ impl PublicKey { /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. pub fn to_protobuf_encoding(&self) -> Vec { - use prost::Message; + use quick_protobuf::MessageWrite; + + let public_key = proto::PublicKey::from(self); - let public_key = keys_proto::PublicKey::from(self); + let mut buf = Vec::with_capacity(public_key.get_size()); + let mut writer = Writer::new(&mut buf); + public_key.write_message(&mut writer).expect("Encoding to succeed"); - let mut buf = Vec::with_capacity(public_key.encoded_len()); - public_key - .encode(&mut buf) - .expect("Vec provides capacity as needed"); buf } /// Decode a public key from a protobuf structure, e.g. read from storage /// or received from another node. pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - use prost::Message; + use quick_protobuf::MessageRead; + + let mut reader = BytesReader::from_bytes(bytes); - let pubkey = keys_proto::PublicKey::decode(bytes) + let pubkey = proto::PublicKey::from_reader(&mut reader, bytes) .map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?; pubkey.try_into() @@ -260,67 +267,65 @@ impl PublicKey { } } -impl From<&PublicKey> for keys_proto::PublicKey { +impl From<&PublicKey> for proto::PublicKey<'_> { fn from(key: &PublicKey) -> Self { match key { - PublicKey::Ed25519(key) => keys_proto::PublicKey { - r#type: keys_proto::KeyType::Ed25519 as i32, - data: key.encode().to_vec(), + PublicKey::Ed25519(key) => proto::PublicKey { + Type: proto::KeyType::Ed25519, + Data: Cow::from(key.encode().to_vec()), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKey::Rsa(key) => keys_proto::PublicKey { - r#type: keys_proto::KeyType::Rsa as i32, - data: key.encode_x509(), + PublicKey::Rsa(key) => proto::PublicKey { + Type: proto::KeyType::RSA, + Data: Cow::from(key.encode_x509()), }, #[cfg(feature = "secp256k1")] - PublicKey::Secp256k1(key) => keys_proto::PublicKey { - r#type: keys_proto::KeyType::Secp256k1 as i32, - data: key.encode().to_vec(), + PublicKey::Secp256k1(key) => proto::PublicKey { + Type: proto::KeyType::Secp256k1, + Data: Cow::from(key.encode().to_vec()), }, #[cfg(feature = "ecdsa")] - PublicKey::Ecdsa(key) => keys_proto::PublicKey { - r#type: keys_proto::KeyType::Ecdsa as i32, - data: key.encode_der(), + PublicKey::Ecdsa(key) => proto::PublicKey { + Type: proto::KeyType::ECDSA, + Data: Cow::from(key.encode_der()), }, } } } -impl TryFrom for PublicKey { +impl TryFrom> for PublicKey { type Error = DecodingError; - fn try_from(pubkey: keys_proto::PublicKey) -> Result { - let key_type = keys_proto::KeyType::from_i32(pubkey.r#type) - .ok_or_else(|| DecodingError::unknown_key_type(pubkey.r#type))?; + fn try_from(pubkey: proto::PublicKey) -> Result { - match key_type { - keys_proto::KeyType::Ed25519 => { - ed25519::PublicKey::decode(&pubkey.data).map(PublicKey::Ed25519) + match pubkey.Type { + proto::KeyType::Ed25519 => { + ed25519::PublicKey::decode(&pubkey.Data).map(PublicKey::Ed25519) } #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - keys_proto::KeyType::Rsa => { - rsa::PublicKey::decode_x509(&pubkey.data).map(PublicKey::Rsa) + proto::KeyType::RSA => { + rsa::PublicKey::decode_x509(&pubkey.Data).map(PublicKey::Rsa) } #[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))] - keys_proto::KeyType::Rsa => { + proto::KeyType::RSA => { log::debug!("support for RSA was disabled at compile-time"); Err(DecodingError::missing_feature("rsa")) } #[cfg(feature = "secp256k1")] - keys_proto::KeyType::Secp256k1 => { - secp256k1::PublicKey::decode(&pubkey.data).map(PublicKey::Secp256k1) + proto::KeyType::Secp256k1 => { + secp256k1::PublicKey::decode(&pubkey.Data).map(PublicKey::Secp256k1) } #[cfg(not(feature = "secp256k1"))] - keys_proto::KeyType::Secp256k1 => { + proto::KeyType::Secp256k1 => { log::debug!("support for secp256k1 was disabled at compile-time"); Err(DecodingError::missing_feature("secp256k1")) } #[cfg(feature = "ecdsa")] - keys_proto::KeyType::Ecdsa => { - ecdsa::PublicKey::decode_der(&pubkey.data).map(PublicKey::Ecdsa) + proto::KeyType::ECDSA => { + ecdsa::PublicKey::decode_der(&pubkey.Data).map(PublicKey::Ecdsa) } #[cfg(not(feature = "ecdsa"))] - keys_proto::KeyType::Ecdsa => { + proto::KeyType::ECDSA => { log::debug!("support for ECDSA was disabled at compile-time"); Err(DecodingError::missing_feature("ecdsa")) } diff --git a/core/src/identity/error.rs b/core/src/identity/error.rs index 522c21e4bf0..fb5d8eb2006 100644 --- a/core/src/identity/error.rs +++ b/core/src/identity/error.rs @@ -68,13 +68,6 @@ impl DecodingError { } } - pub(crate) fn unknown_key_type(key_type: i32) -> Self { - Self { - msg: format!("unknown key-type {key_type}"), - source: None, - } - } - pub(crate) fn decoding_unsupported(key_type: &'static str) -> Self { Self { msg: format!("decoding {key_type} key from Protobuf is unsupported"), diff --git a/core/src/keys.proto b/core/src/keys.proto deleted file mode 100644 index 5fbeaf8f6e0..00000000000 --- a/core/src/keys.proto +++ /dev/null @@ -1,20 +0,0 @@ -syntax = "proto2"; - -package keys_proto; - -enum KeyType { - RSA = 0; - Ed25519 = 1; - Secp256k1 = 2; - ECDSA = 3; -} - -message PublicKey { - required KeyType Type = 1; - required bytes Data = 2; -} - -message PrivateKey { - required KeyType Type = 1; - required bytes Data = 2; -} diff --git a/core/src/lib.rs b/core/src/lib.rs index 2b20f5156e4..304c6b255b1 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -37,18 +37,8 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -#[allow(clippy::derive_partial_eq_without_eq)] -mod keys_proto { - include!(concat!(env!("OUT_DIR"), "/keys_proto.rs")); -} - -mod envelope_proto { - include!(concat!(env!("OUT_DIR"), "/envelope_proto.rs")); -} - -#[allow(clippy::derive_partial_eq_without_eq)] -mod peer_record_proto { - include!(concat!(env!("OUT_DIR"), "/peer_record_proto.rs")); +mod proto { + pub use crate::generated::{keys_proto::*, envelope_proto::*, peer_record_proto::*, peer_record_proto::mod_PeerRecord::*}; } /// Multi-address re-export. @@ -57,6 +47,7 @@ pub type Negotiated = multistream_select::Negotiated; mod peer_id; mod translation; +mod generated; pub mod connection; pub mod either; @@ -82,4 +73,4 @@ pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, U #[derive(thiserror::Error, Debug)] #[error(transparent)] -pub struct DecodeError(prost::DecodeError); +pub struct DecodeError(quick_protobuf::Error); diff --git a/core/src/peer_record.proto b/core/src/peer_record.proto deleted file mode 100644 index 69bb345e02f..00000000000 --- a/core/src/peer_record.proto +++ /dev/null @@ -1,27 +0,0 @@ -syntax = "proto3"; - -package peer_record_proto; - -// PeerRecord messages contain information that is useful to share with other peers. -// Currently, a PeerRecord contains the public listen addresses for a peer, but this -// is expected to expand to include other information in the future. -// -// PeerRecords are designed to be serialized to bytes and placed inside of -// SignedEnvelopes before sharing with other peers. -message PeerRecord { - - // AddressInfo is a wrapper around a binary multiaddr. It is defined as a - // separate message to allow us to add per-address metadata in the future. - message AddressInfo { - bytes multiaddr = 1; - } - - // peer_id contains a libp2p peer id in its binary representation. - bytes peer_id = 1; - - // seq contains a monotonically-increasing sequence counter to order PeerRecords in time. - uint64 seq = 2; - - // addresses is a list of public listen addresses for the peer. - repeated AddressInfo addresses = 3; -} diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 39cf985ee36..dca725a694f 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -1,9 +1,12 @@ +use std::borrow::Cow; use crate::identity::error::SigningError; use crate::identity::Keypair; +use crate::proto; use crate::signed_envelope::SignedEnvelope; -use crate::{peer_record_proto, signed_envelope, DecodeError, Multiaddr, PeerId}; +use crate::{signed_envelope, DecodeError, Multiaddr, PeerId}; use instant::SystemTime; use std::convert::TryInto; +use quick_protobuf::{BytesReader, Writer}; const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record"; const DOMAIN_SEP: &str = "libp2p-routing-state"; @@ -29,11 +32,12 @@ impl PeerRecord { /// /// If this function succeeds, the [`SignedEnvelope`] contained a peer record with a valid signature and can hence be considered authenticated. pub fn from_signed_envelope(envelope: SignedEnvelope) -> Result { - use prost::Message; + use quick_protobuf::MessageRead; let (payload, signing_key) = envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?; - let record = peer_record_proto::PeerRecord::decode(payload).map_err(DecodeError)?; + let mut reader = BytesReader::from_bytes(payload); + let record = proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError)?; let peer_id = PeerId::from_bytes(&record.peer_id)?; @@ -45,7 +49,7 @@ impl PeerRecord { let addresses = record .addresses .into_iter() - .map(|a| a.multiaddr.try_into()) + .map(|a| a.multiaddr.to_vec().try_into()) .collect::, _>>()?; Ok(Self { @@ -60,7 +64,7 @@ impl PeerRecord { /// /// This is the same key that is used for authenticating every libp2p connection of your application, i.e. what you use when setting up your [`crate::transport::Transport`]. pub fn new(key: &Keypair, addresses: Vec) -> Result { - use prost::Message; + use quick_protobuf::MessageWrite; let seq = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -69,21 +73,21 @@ impl PeerRecord { let peer_id = key.public().to_peer_id(); let payload = { - let record = peer_record_proto::PeerRecord { - peer_id: peer_id.to_bytes(), + let record = proto::PeerRecord { + peer_id: Cow::from(peer_id.to_bytes()), seq, addresses: addresses .iter() - .map(|m| peer_record_proto::peer_record::AddressInfo { - multiaddr: m.to_vec(), + .map(|m| proto::AddressInfo { + multiaddr: Cow::Borrowed(m.as_ref()), }) .collect(), }; - let mut buf = Vec::with_capacity(record.encoded_len()); - record - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + let mut buf = Vec::with_capacity(record.get_size()); + let mut writer = Writer::new(&mut buf); + record.write_message(&mut writer).expect("Encoding to succeed"); + buf }; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index d290354fa9c..80e233c6e83 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -1,8 +1,10 @@ +use std::borrow::Cow; use crate::identity::error::SigningError; use crate::identity::Keypair; -use crate::{identity, DecodeError, PublicKey}; +use crate::{identity, DecodeError, PublicKey, proto}; use std::convert::TryInto; use std::fmt; +use quick_protobuf::{BytesReader, Writer}; use unsigned_varint::encode::usize_buffer; /// A signed envelope contains an arbitrary byte string payload, a signature of the payload, and the public key that can be used to verify the signature. @@ -73,37 +75,38 @@ impl SignedEnvelope { /// Encode this [`SignedEnvelope`] using the protobuf encoding specified in the RFC. pub fn into_protobuf_encoding(self) -> Vec { - use prost::Message; + use quick_protobuf::MessageWrite; - let envelope = crate::envelope_proto::Envelope { + let envelope = proto::Envelope { public_key: Some((&self.key).into()), - payload_type: self.payload_type, - payload: self.payload, - signature: self.signature, + payload_type: Cow::Borrowed(&self.payload_type), + payload: Cow::Borrowed(&self.payload), + signature: Cow::Borrowed(&self.signature), }; - let mut buf = Vec::with_capacity(envelope.encoded_len()); - envelope - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + let mut buf = Vec::with_capacity(envelope.get_size()); + let mut writer = Writer::new(&mut buf); + + envelope.write_message(&mut writer).expect("Encoding to succeed"); buf } /// Decode a [`SignedEnvelope`] using the protobuf encoding specified in the RFC. pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - use prost::Message; + use quick_protobuf::MessageRead; - let envelope = crate::envelope_proto::Envelope::decode(bytes).map_err(DecodeError)?; + let mut reader = BytesReader::from_bytes(bytes); + let envelope = proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError)?; Ok(Self { key: envelope .public_key .ok_or(DecodingError::MissingPublicKey)? .try_into()?, - payload_type: envelope.payload_type, - payload: envelope.payload, - signature: envelope.signature, + payload_type: envelope.payload_type.to_vec(), + payload: envelope.payload.to_vec(), + signature: envelope.signature.to_vec(), }) } } From 78be5d51550bfddcb568b42845a2786fbf54ea97 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 19:09:24 -0500 Subject: [PATCH 23/66] Fix core tests --- core/src/peer_record.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index dca725a694f..50a6f44b761 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -166,7 +166,7 @@ mod tests { #[test] fn mismatched_signature() { - use prost::Message; + use quick_protobuf::MessageWrite; let addr: Multiaddr = HOME.parse().unwrap(); @@ -175,18 +175,18 @@ mod tests { let identity_b = Keypair::generate_ed25519(); let payload = { - let record = peer_record_proto::PeerRecord { - peer_id: identity_a.public().to_peer_id().to_bytes(), + let record = proto::PeerRecord { + peer_id: Cow::from(identity_a.public().to_peer_id().to_bytes()), seq: 0, - addresses: vec![peer_record_proto::peer_record::AddressInfo { - multiaddr: addr.to_vec(), + addresses: vec![proto::AddressInfo { + multiaddr: Cow::from(addr.to_vec()), }], }; - let mut buf = Vec::with_capacity(record.encoded_len()); - record - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + let mut buf = Vec::with_capacity(record.get_size()); + let mut writer = Writer::new(&mut buf); + record.write_message(&mut writer).expect("Encoding to succeed"); + buf }; From 430a9fb4d285d096df75988c69ac88aa8a176adb Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 19:10:57 -0500 Subject: [PATCH 24/66] Fmt code --- core/src/identity.rs | 16 +++++++--------- core/src/lib.rs | 7 +++++-- core/src/peer_record.rs | 12 ++++++++---- core/src/signed_envelope.rs | 10 ++++++---- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/src/identity.rs b/core/src/identity.rs index 6466f316da6..d38a2af0e91 100644 --- a/core/src/identity.rs +++ b/core/src/identity.rs @@ -42,11 +42,11 @@ pub mod secp256k1; pub mod error; -use std::borrow::Cow; use self::error::*; -use crate::{PeerId, proto}; -use std::convert::{TryFrom, TryInto}; +use crate::{proto, PeerId}; use quick_protobuf::{BytesReader, Writer}; +use std::borrow::Cow; +use std::convert::{TryFrom, TryInto}; /// Identity keypair of a node. /// @@ -180,7 +180,6 @@ impl Keypair { .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) .map(zeroize::Zeroizing::new)?; - match private_key.Type { proto::KeyType::Ed25519 => { ed25519::Keypair::decode(private_key.Data.to_mut()).map(Keypair::Ed25519) @@ -243,7 +242,9 @@ impl PublicKey { let mut buf = Vec::with_capacity(public_key.get_size()); let mut writer = Writer::new(&mut buf); - public_key.write_message(&mut writer).expect("Encoding to succeed"); + public_key + .write_message(&mut writer) + .expect("Encoding to succeed"); buf } @@ -297,15 +298,12 @@ impl TryFrom> for PublicKey { type Error = DecodingError; fn try_from(pubkey: proto::PublicKey) -> Result { - match pubkey.Type { proto::KeyType::Ed25519 => { ed25519::PublicKey::decode(&pubkey.Data).map(PublicKey::Ed25519) } #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - proto::KeyType::RSA => { - rsa::PublicKey::decode_x509(&pubkey.Data).map(PublicKey::Rsa) - } + proto::KeyType::RSA => rsa::PublicKey::decode_x509(&pubkey.Data).map(PublicKey::Rsa), #[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))] proto::KeyType::RSA => { log::debug!("support for RSA was disabled at compile-time"); diff --git a/core/src/lib.rs b/core/src/lib.rs index 304c6b255b1..f0d40254a42 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -38,16 +38,19 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] mod proto { - pub use crate::generated::{keys_proto::*, envelope_proto::*, peer_record_proto::*, peer_record_proto::mod_PeerRecord::*}; + pub use crate::generated::{ + envelope_proto::*, keys_proto::*, peer_record_proto::mod_PeerRecord::*, + peer_record_proto::*, + }; } /// Multi-address re-export. pub use multiaddr; pub type Negotiated = multistream_select::Negotiated; +mod generated; mod peer_id; mod translation; -mod generated; pub mod connection; pub mod either; diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 50a6f44b761..56e3fb48ce5 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -1,12 +1,12 @@ -use std::borrow::Cow; use crate::identity::error::SigningError; use crate::identity::Keypair; use crate::proto; use crate::signed_envelope::SignedEnvelope; use crate::{signed_envelope, DecodeError, Multiaddr, PeerId}; use instant::SystemTime; -use std::convert::TryInto; use quick_protobuf::{BytesReader, Writer}; +use std::borrow::Cow; +use std::convert::TryInto; const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record"; const DOMAIN_SEP: &str = "libp2p-routing-state"; @@ -86,7 +86,9 @@ impl PeerRecord { let mut buf = Vec::with_capacity(record.get_size()); let mut writer = Writer::new(&mut buf); - record.write_message(&mut writer).expect("Encoding to succeed"); + record + .write_message(&mut writer) + .expect("Encoding to succeed"); buf }; @@ -185,7 +187,9 @@ mod tests { let mut buf = Vec::with_capacity(record.get_size()); let mut writer = Writer::new(&mut buf); - record.write_message(&mut writer).expect("Encoding to succeed"); + record + .write_message(&mut writer) + .expect("Encoding to succeed"); buf }; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 80e233c6e83..32af34fdfed 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -1,10 +1,10 @@ -use std::borrow::Cow; use crate::identity::error::SigningError; use crate::identity::Keypair; -use crate::{identity, DecodeError, PublicKey, proto}; +use crate::{identity, proto, DecodeError, PublicKey}; +use quick_protobuf::{BytesReader, Writer}; +use std::borrow::Cow; use std::convert::TryInto; use std::fmt; -use quick_protobuf::{BytesReader, Writer}; use unsigned_varint::encode::usize_buffer; /// A signed envelope contains an arbitrary byte string payload, a signature of the payload, and the public key that can be used to verify the signature. @@ -87,7 +87,9 @@ impl SignedEnvelope { let mut buf = Vec::with_capacity(envelope.get_size()); let mut writer = Writer::new(&mut buf); - envelope.write_message(&mut writer).expect("Encoding to succeed"); + envelope + .write_message(&mut writer) + .expect("Encoding to succeed"); buf } From f1967b8a56d0bb79896154398a2f025446c9c4ee Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 19:39:08 -0500 Subject: [PATCH 25/66] Include generated in proto mod --- core/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index f0d40254a42..e911ad52531 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -38,9 +38,10 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] mod proto { - pub use crate::generated::{ + include!("generated/mod.rs"); + pub use self::{ envelope_proto::*, keys_proto::*, peer_record_proto::mod_PeerRecord::*, - peer_record_proto::*, + peer_record_proto::PeerRecord, }; } @@ -48,7 +49,6 @@ mod proto { pub use multiaddr; pub type Negotiated = multistream_select::Negotiated; -mod generated; mod peer_id; mod translation; From 76dd1d1d0cf697361245db863ad9d5b0063b0e0c Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 20:19:50 -0500 Subject: [PATCH 26/66] Remove prost from autonat --- protocols/autonat/Cargo.toml | 5 +- protocols/autonat/build.rs | 23 ----- protocols/autonat/src/lib.rs | 5 +- protocols/autonat/src/protocol.rs | 133 ++++++++++++++-------------- protocols/autonat/src/structs.proto | 37 -------- 5 files changed, 72 insertions(+), 131 deletions(-) delete mode 100644 protocols/autonat/build.rs delete mode 100644 protocols/autonat/src/structs.proto diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index 07e83adb066..b8ef491b20f 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -10,9 +10,6 @@ repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] -[build-dependencies] -prost-build = "0.11" - [dependencies] async-trait = "0.1" futures = "0.3" @@ -23,7 +20,7 @@ libp2p-swarm = { version = "0.42.0", path = "../../swarm" } libp2p-request-response = { version = "0.24.0", path = "../request-response" } log = "0.4" rand = "0.8" -prost = "0.11" +quick-protobuf = "0.8" [dev-dependencies] async-std = { version = "1.10", features = ["attributes"] } diff --git a/protocols/autonat/build.rs b/protocols/autonat/build.rs deleted file mode 100644 index d3714fdec14..00000000000 --- a/protocols/autonat/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2021 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/structs.proto"], &["src"]).unwrap(); -} diff --git a/protocols/autonat/src/lib.rs b/protocols/autonat/src/lib.rs index 07771fe0615..5a32dc50181 100644 --- a/protocols/autonat/src/lib.rs +++ b/protocols/autonat/src/lib.rs @@ -35,6 +35,7 @@ pub use self::{ pub use libp2p_request_response::{InboundFailure, OutboundFailure}; #[allow(clippy::derive_partial_eq_without_eq)] -mod structs_proto { - include!(concat!(env!("OUT_DIR"), "/structs.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::structs::{mod_Message::*, Message}; } diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/protocol.rs index 08ae415342f..eaa0a9dcde9 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/protocol.rs @@ -18,12 +18,13 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::structs_proto; +use crate::proto; use async_trait::async_trait; use futures::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; use libp2p_core::{upgrade, Multiaddr, PeerId}; use libp2p_request_response::{self as request_response, ProtocolName}; -use prost::Message; +use quick_protobuf::{BytesReader, Writer}; +use std::borrow::Cow; use std::{convert::TryFrom, io}; #[derive(Clone, Debug)] @@ -108,14 +109,17 @@ pub struct DialRequest { impl DialRequest { pub fn from_bytes(bytes: &[u8]) -> Result { - let msg = structs_proto::Message::decode(bytes) + use quick_protobuf::MessageRead; + + let mut reader = BytesReader::from_bytes(bytes); + let msg = proto::Message::from_reader(&mut reader, bytes) .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; - if msg.r#type != Some(structs_proto::message::MessageType::Dial as _) { + if msg.type_pb != Some(proto::MessageType::DIAL) { return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid type")); } - let (peer_id, addrs) = if let Some(structs_proto::message::Dial { + let (peer_id, addrs) = if let Some(proto::Dial { peer: - Some(structs_proto::message::PeerInfo { + Some(proto::PeerInfo { id: Some(peer_id), addrs, }), @@ -131,13 +135,13 @@ impl DialRequest { }; let peer_id = { - PeerId::try_from(peer_id) + PeerId::try_from(peer_id.to_vec()) .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid peer id"))? }; let addrs = { let mut maddrs = vec![]; for addr in addrs.into_iter() { - let maddr = Multiaddr::try_from(addr) + let maddr = Multiaddr::try_from(addr.to_vec()) .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; maddrs.push(maddr); } @@ -150,28 +154,30 @@ impl DialRequest { } pub fn into_bytes(self) -> Vec { + use quick_protobuf::MessageWrite; + let peer_id = self.peer_id.to_bytes(); let addrs = self .addresses .into_iter() - .map(|addr| addr.to_vec()) + .map(|addr| Cow::from(addr.to_vec())) .collect(); - let msg = structs_proto::Message { - r#type: Some(structs_proto::message::MessageType::Dial as _), - dial: Some(structs_proto::message::Dial { - peer: Some(structs_proto::message::PeerInfo { - id: Some(peer_id), + let msg = proto::Message { + type_pb: Some(proto::MessageType::DIAL), + dial: Some(proto::Dial { + peer: Some(proto::PeerInfo { + id: Some(Cow::from(peer_id)), addrs, }), }), - dial_response: None, + dialResponse: None, }; - let mut bytes = Vec::with_capacity(msg.encoded_len()); - msg.encode(&mut bytes) - .expect("Vec provides capacity as needed"); - bytes + let mut buf = Vec::with_capacity(msg.get_size()); + let mut writer = Writer::new(&mut buf); + msg.write_message(&mut writer).expect("Encoding to succeed"); + buf } } @@ -183,29 +189,27 @@ pub enum ResponseError { InternalError, } -impl From for i32 { +impl From for proto::ResponseStatus { fn from(t: ResponseError) -> Self { match t { - ResponseError::DialError => 100, - ResponseError::DialRefused => 101, - ResponseError::BadRequest => 200, - ResponseError::InternalError => 300, + ResponseError::DialError => proto::ResponseStatus::E_DIAL_ERROR, + ResponseError::DialRefused => proto::ResponseStatus::E_DIAL_REFUSED, + ResponseError::BadRequest => proto::ResponseStatus::E_BAD_REQUEST, + ResponseError::InternalError => proto::ResponseStatus::E_INTERNAL_ERROR, } } } -impl TryFrom for ResponseError { +impl TryFrom for ResponseError { type Error = io::Error; - fn try_from(value: structs_proto::message::ResponseStatus) -> Result { + fn try_from(value: proto::ResponseStatus) -> Result { match value { - structs_proto::message::ResponseStatus::EDialError => Ok(ResponseError::DialError), - structs_proto::message::ResponseStatus::EDialRefused => Ok(ResponseError::DialRefused), - structs_proto::message::ResponseStatus::EBadRequest => Ok(ResponseError::BadRequest), - structs_proto::message::ResponseStatus::EInternalError => { - Ok(ResponseError::InternalError) - } - structs_proto::message::ResponseStatus::Ok => { + proto::ResponseStatus::E_DIAL_ERROR => Ok(ResponseError::DialError), + proto::ResponseStatus::E_DIAL_REFUSED => Ok(ResponseError::DialRefused), + proto::ResponseStatus::E_BAD_REQUEST => Ok(ResponseError::BadRequest), + proto::ResponseStatus::E_INTERNAL_ERROR => Ok(ResponseError::InternalError), + proto::ResponseStatus::OK => { log::debug!("Received response with status code OK but expected error."); Err(io::Error::new( io::ErrorKind::InvalidData, @@ -224,38 +228,35 @@ pub struct DialResponse { impl DialResponse { pub fn from_bytes(bytes: &[u8]) -> Result { - let msg = structs_proto::Message::decode(bytes) + use quick_protobuf::MessageRead; + + let mut reader = BytesReader::from_bytes(bytes); + let msg = proto::Message::from_reader(&mut reader, bytes) .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; - if msg.r#type != Some(structs_proto::message::MessageType::DialResponse as _) { + if msg.type_pb != Some(proto::MessageType::DIAL_RESPONSE) { return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid type")); } - Ok(match msg.dial_response { - Some(structs_proto::message::DialResponse { - status: Some(status), - status_text, + Ok(match msg.dialResponse { + Some(proto::DialResponse { + status: Some(proto::ResponseStatus::OK), + statusText, addr: Some(addr), - }) if structs_proto::message::ResponseStatus::from_i32(status) - == Some(structs_proto::message::ResponseStatus::Ok) => - { - let addr = Multiaddr::try_from(addr) + }) => { + let addr = Multiaddr::try_from(addr.to_vec()) .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; Self { - status_text, + status_text: statusText.map(|status| status.to_string()), result: Ok(addr), } } - Some(structs_proto::message::DialResponse { + Some(proto::DialResponse { status: Some(status), - status_text, + statusText, addr: None, }) => Self { - status_text, - result: Err(ResponseError::try_from( - structs_proto::message::ResponseStatus::from_i32(status).ok_or_else(|| { - io::Error::new(io::ErrorKind::InvalidData, "invalid response status code") - })?, - )?), + status_text: statusText.map(|status| status.to_string()), + result: Err(ResponseError::try_from(status)?), }, _ => { log::debug!("Received malformed response message."); @@ -268,29 +269,31 @@ impl DialResponse { } pub fn into_bytes(self) -> Vec { + use quick_protobuf::MessageWrite; + let dial_response = match self.result { - Ok(addr) => structs_proto::message::DialResponse { - status: Some(0), - status_text: self.status_text, - addr: Some(addr.to_vec()), + Ok(addr) => proto::DialResponse { + status: Some(proto::ResponseStatus::OK), + statusText: self.status_text.map(Cow::from), + addr: Some(Cow::from(addr.to_vec())), }, - Err(error) => structs_proto::message::DialResponse { + Err(error) => proto::DialResponse { status: Some(error.into()), - status_text: self.status_text, + statusText: self.status_text.map(Cow::from), addr: None, }, }; - let msg = structs_proto::Message { - r#type: Some(structs_proto::message::MessageType::DialResponse as _), + let msg = proto::Message { + type_pb: Some(proto::MessageType::DIAL_RESPONSE), dial: None, - dial_response: Some(dial_response), + dialResponse: Some(dial_response), }; - let mut bytes = Vec::with_capacity(msg.encoded_len()); - msg.encode(&mut bytes) - .expect("Vec provides capacity as needed"); - bytes + let mut buf = Vec::with_capacity(msg.get_size()); + let mut writer = Writer::new(&mut buf); + msg.write_message(&mut writer).expect("Encoding to succeed"); + buf } } diff --git a/protocols/autonat/src/structs.proto b/protocols/autonat/src/structs.proto deleted file mode 100644 index 19e27abd36a..00000000000 --- a/protocols/autonat/src/structs.proto +++ /dev/null @@ -1,37 +0,0 @@ -syntax = "proto2"; - -package structs; - -message Message { - enum MessageType { - DIAL = 0; - DIAL_RESPONSE = 1; - } - - enum ResponseStatus { - OK = 0; - E_DIAL_ERROR = 100; - E_DIAL_REFUSED = 101; - E_BAD_REQUEST = 200; - E_INTERNAL_ERROR = 300; - } - - message PeerInfo { - optional bytes id = 1; - repeated bytes addrs = 2; - } - - message Dial { - optional PeerInfo peer = 1; - } - - message DialResponse { - optional ResponseStatus status = 1; - optional string statusText = 2; - optional bytes addr = 3; - } - - optional MessageType type = 1; - optional Dial dial = 2; - optional DialResponse dialResponse = 3; -} From 840887a04eff1191d55f319651eb83349f125a5a Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 21 Jan 2023 20:22:41 -0500 Subject: [PATCH 27/66] Update changelog --- core/CHANGELOG.md | 2 ++ protocols/autonat/CHANGELOG.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index f9c45f5f4da..1ecd328d275 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -2,7 +2,9 @@ - Move `ConnectionId` to `libp2p-swarm`. See [PR 3221]. - Move `PendingPoint` to `libp2p-swarm` and make it crate-private. See [PR 3221]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 [PR 3221]: https://github.com/libp2p/rust-libp2p/pull/3221 # 0.38.0 diff --git a/protocols/autonat/CHANGELOG.md b/protocols/autonat/CHANGELOG.md index a39c6b2b14a..a40f880729d 100644 --- a/protocols/autonat/CHANGELOG.md +++ b/protocols/autonat/CHANGELOG.md @@ -8,6 +8,9 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 [PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153 # 0.9.0 From 448765ac99889014bf02adc3a34f613da3e608e1 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 28 Jan 2023 16:53:52 -0500 Subject: [PATCH 28/66] Rename to quick-protobuf-codec --- Cargo.toml | 2 +- .../CHANGELOG.md | 6 ++++++ .../Cargo.toml | 5 +---- .../src/lib.rs | 16 ++++++++-------- protocols/dcutr/Cargo.toml | 2 +- protocols/floodsub/Cargo.toml | 2 +- protocols/gossipsub/Cargo.toml | 2 +- protocols/identify/Cargo.toml | 2 +- protocols/relay/Cargo.toml | 2 +- protocols/rendezvous/Cargo.toml | 2 +- transports/webrtc/Cargo.toml | 2 +- 11 files changed, 23 insertions(+), 20 deletions(-) rename misc/{prost-codec => quick-protobuf-codec}/CHANGELOG.md (80%) rename misc/{prost-codec => quick-protobuf-codec}/Cargo.toml (93%) rename misc/{prost-codec => quick-protobuf-codec}/src/lib.rs (79%) diff --git a/Cargo.toml b/Cargo.toml index 4e678204d3b..8cda8b60d48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -148,7 +148,7 @@ members = [ "misc/multistream-select", "misc/rw-stream-sink", "misc/keygen", - "misc/prost-codec", + "misc/quick-protobuf-codec", "misc/quickcheck-ext", "muxers/mplex", "muxers/yamux", diff --git a/misc/prost-codec/CHANGELOG.md b/misc/quick-protobuf-codec/CHANGELOG.md similarity index 80% rename from misc/prost-codec/CHANGELOG.md rename to misc/quick-protobuf-codec/CHANGELOG.md index f21b9d5bc2f..8e0f4d845db 100644 --- a/misc/prost-codec/CHANGELOG.md +++ b/misc/quick-protobuf-codec/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.1.0 [unreleased] + +- Update to `quick-protobuf`. See [PR 3312]. + +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 + # 0.3.0 - Implement `From` trait for `std::io::Error`. See [PR 2622]. diff --git a/misc/prost-codec/Cargo.toml b/misc/quick-protobuf-codec/Cargo.toml similarity index 93% rename from misc/prost-codec/Cargo.toml rename to misc/quick-protobuf-codec/Cargo.toml index c0446db8139..b3efdfa90ba 100644 --- a/misc/prost-codec/Cargo.toml +++ b/misc/quick-protobuf-codec/Cargo.toml @@ -13,12 +13,9 @@ categories = ["asynchronous"] [dependencies] asynchronous-codec = { version = "0.6" } bytes = { version = "1" } -prost = "0.11" thiserror = "1.0" unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] } - -[dev-dependencies] -prost-build = "0.11" +quick-protobuf = "0.8" # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/misc/prost-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs similarity index 79% rename from misc/prost-codec/src/lib.rs rename to misc/quick-protobuf-codec/src/lib.rs index dc5714d742f..c70c70a594d 100644 --- a/misc/prost-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -2,8 +2,7 @@ use asynchronous_codec::{Decoder, Encoder}; use bytes::BytesMut; -use prost::Message; -use std::io::Cursor; +use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use std::marker::PhantomData; use unsigned_varint::codec::UviBytes; @@ -31,21 +30,22 @@ impl Codec { } } -impl Encoder for Codec { +impl Encoder for Codec { type Item = In; type Error = Error; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { let mut encoded_msg = BytesMut::new(); - item.encode(&mut encoded_msg) - .expect("BytesMut to have sufficient capacity."); + let mut writer = Writer::new(encoded_msg.as_mut()); + item.write_message(&mut writer) + .expect("Encoding to succeed"); self.uvi.encode(encoded_msg.freeze(), dst)?; Ok(()) } } -impl Decoder for Codec { +impl MessageRead<'a> + Default> Decoder for Codec { type Item = Out; type Error = Error; @@ -55,9 +55,9 @@ impl Decoder for Codec { Some(msg) => msg, }; - let message = Message::decode(Cursor::new(msg)) + let mut reader = BytesReader::from_bytes(&msg); + let message = Self::Item::from_reader(&mut reader, &msg) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; - Ok(Some(message)) } } diff --git a/protocols/dcutr/Cargo.toml b/protocols/dcutr/Cargo.toml index 1a7dcaf1a06..772f79f9e14 100644 --- a/protocols/dcutr/Cargo.toml +++ b/protocols/dcutr/Cargo.toml @@ -20,7 +20,7 @@ instant = "0.1.11" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" -prost-codec = { version = "0.3", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } prost = "0.11" thiserror = "1.0" void = "1" diff --git a/protocols/floodsub/Cargo.toml b/protocols/floodsub/Cargo.toml index 8843edad65d..d1fd91ee459 100644 --- a/protocols/floodsub/Cargo.toml +++ b/protocols/floodsub/Cargo.toml @@ -19,7 +19,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" prost = "0.11" -prost-codec = { version = "0.3", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } rand = "0.8" smallvec = "1.6.1" thiserror = "1.0.37" diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 02234babe27..3fd190fc900 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -25,7 +25,7 @@ sha2 = "0.10.0" base64 = "0.20.0" smallvec = "1.6.1" prost = "0.11" -prost-codec = { version = "0.3", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } hex_fmt = "0.3.0" regex = "1.5.5" serde = { version = "1", optional = true, features = ["derive"] } diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 3e844a2c8d0..7fc875736b8 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -18,7 +18,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4.1" lru = "0.9.0" -prost-codec = { version = "0.3", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } prost = "0.11" smallvec = "1.6.1" thiserror = "1.0" diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 1ff515800d5..b517cc0e145 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -21,7 +21,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" pin-project = "1" -prost-codec = { version = "0.3", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } prost = "0.11" rand = "0.8.4" smallvec = "1.6.1" diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index 8238eaa433e..e2a37bd686c 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -20,7 +20,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" prost = "0.11" -prost-codec = { version = "0.3.0", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3.0", path = "../../misc/quick-protobuf-codec" } rand = "0.8" sha2 = "0.10" thiserror = "1" diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 990d09101a7..e167f097404 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -23,7 +23,7 @@ libp2p-noise = { version = "0.42.0", path = "../../transports/noise" } log = "0.4" multihash = { version = "0.17.0", default-features = false, features = ["sha2"] } prost = "0.11" -prost-codec = { version = "0.3.0", path = "../../misc/prost-codec" } +prost-codec = { version = "0.3.0", path = "../../misc/quick-protobuf-codec" } rand = "0.8" rcgen = "0.9.3" serde = { version = "1.0", features = ["derive"] } From 651b9c92aa841dc4d95a861164f6a5369ff771e2 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 28 Jan 2023 18:49:16 -0500 Subject: [PATCH 29/66] Remove prost-codec --- misc/quick-protobuf-codec/Cargo.toml | 4 +- protocols/dcutr/Cargo.toml | 7 +--- protocols/dcutr/build.rs | 23 ----------- protocols/dcutr/src/lib.rs | 7 ++-- protocols/dcutr/src/message.proto | 19 --------- protocols/dcutr/src/protocol/inbound.rs | 39 +++++++++---------- protocols/dcutr/src/protocol/outbound.rs | 34 ++++++++-------- protocols/floodsub/Cargo.toml | 2 +- protocols/floodsub/src/protocol.rs | 6 +-- protocols/gossipsub/Cargo.toml | 2 +- protocols/gossipsub/src/error.rs | 4 +- protocols/gossipsub/src/protocol.rs | 4 +- protocols/identify/Cargo.toml | 2 +- protocols/identify/src/protocol.rs | 6 +-- protocols/relay/Cargo.toml | 2 +- protocols/relay/src/protocol/inbound_hop.rs | 14 +++---- protocols/relay/src/protocol/inbound_stop.rs | 12 +++--- protocols/relay/src/protocol/outbound_hop.rs | 8 ++-- protocols/relay/src/protocol/outbound_stop.rs | 8 ++-- protocols/rendezvous/Cargo.toml | 2 +- protocols/rendezvous/src/codec.rs | 6 +-- transports/webrtc/Cargo.toml | 2 +- transports/webrtc/src/tokio/substream.rs | 2 +- .../webrtc/src/tokio/substream/framed_dc.rs | 4 +- 24 files changed, 87 insertions(+), 132 deletions(-) delete mode 100644 protocols/dcutr/build.rs delete mode 100644 protocols/dcutr/src/message.proto diff --git a/misc/quick-protobuf-codec/Cargo.toml b/misc/quick-protobuf-codec/Cargo.toml index b3efdfa90ba..eab4d073faf 100644 --- a/misc/quick-protobuf-codec/Cargo.toml +++ b/misc/quick-protobuf-codec/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "prost-codec" +name = "quick-protobuf-codec" edition = "2021" rust-version = "1.60.0" description = "Asynchronous de-/encoding of Protobuf structs using asynchronous-codec, unsigned-varint and prost." -version = "0.3.0" +version = "0.1.0" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/dcutr/Cargo.toml b/protocols/dcutr/Cargo.toml index 772f79f9e14..b49c98bf1ca 100644 --- a/protocols/dcutr/Cargo.toml +++ b/protocols/dcutr/Cargo.toml @@ -20,14 +20,11 @@ instant = "0.1.11" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" -prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } -prost = "0.11" +quick-protobuf = "0.8" +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } thiserror = "1.0" void = "1" -[build-dependencies] -prost-build = "0.11" - [dev-dependencies] clap = { version = "4.0.13", features = ["derive"] } env_logger = "0.10.0" diff --git a/protocols/dcutr/build.rs b/protocols/dcutr/build.rs deleted file mode 100644 index b159bb4c817..00000000000 --- a/protocols/dcutr/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2021 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/message.proto"], &["src"]).unwrap(); -} diff --git a/protocols/dcutr/src/lib.rs b/protocols/dcutr/src/lib.rs index d32ad011b5b..8d5db45000c 100644 --- a/protocols/dcutr/src/lib.rs +++ b/protocols/dcutr/src/lib.rs @@ -26,9 +26,10 @@ mod behaviour_impl; // TODO: Rename back `behaviour` once deprecation symbols are removed. mod handler; mod protocol; -#[allow(clippy::derive_partial_eq_without_eq)] -mod message_proto { - include!(concat!(env!("OUT_DIR"), "/holepunch.pb.rs")); + +mod proto { + include!("generated/mod.rs"); + pub use self::holepunch::pb::{mod_HolePunch::*, HolePunch}; } pub use behaviour_impl::Behaviour; diff --git a/protocols/dcutr/src/message.proto b/protocols/dcutr/src/message.proto deleted file mode 100644 index ab5b220f2ea..00000000000 --- a/protocols/dcutr/src/message.proto +++ /dev/null @@ -1,19 +0,0 @@ -syntax = "proto2"; - -package holepunch.pb; - -message HolePunch { - enum Type { - CONNECT = 100; - SYNC = 300; - } - - required Type type=1; - - // For hole punching, we'll send some additional observed addresses to the remote peer - // that could have been filtered by the Host address factory (for example: AutoRelay removes all public addresses if peer has private reachability). - // This is a hack! - // We plan to have a better address discovery and advertisement mechanism in the future. - // See https://github.com/libp2p/go-libp2p-autonat/pull/98 - repeated bytes ObsAddrs = 2; -} diff --git a/protocols/dcutr/src/protocol/inbound.rs b/protocols/dcutr/src/protocol/inbound.rs index 05037da34dd..b47a9eb2899 100644 --- a/protocols/dcutr/src/protocol/inbound.rs +++ b/protocols/dcutr/src/protocol/inbound.rs @@ -18,7 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{hole_punch, HolePunch}; +use std::borrow::Cow; +use crate::proto; use asynchronous_codec::Framed; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{multiaddr::Protocol, upgrade, Multiaddr}; @@ -46,19 +47,19 @@ impl upgrade::InboundUpgrade for Upgrade { fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { let mut substream = Framed::new( substream, - prost_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES), ); async move { - let HolePunch { r#type, obs_addrs } = + let proto::HolePunch { type_pb, ObsAddrs } = substream.next().await.ok_or(UpgradeError::StreamClosed)??; - let obs_addrs = if obs_addrs.is_empty() { + let obs_addrs = if ObsAddrs.is_empty() { return Err(UpgradeError::NoAddresses); } else { - obs_addrs + ObsAddrs .into_iter() - .filter_map(|a| match Multiaddr::try_from(a) { + .filter_map(|a| match Multiaddr::try_from(a.to_vec()) { Ok(a) => Some(a), Err(e) => { log::debug!("Unable to parse multiaddr: {e}"); @@ -77,11 +78,10 @@ impl upgrade::InboundUpgrade for Upgrade { .collect::>() }; - let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?; - match r#type { - hole_punch::Type::Connect => {} - hole_punch::Type::Sync => return Err(UpgradeError::UnexpectedTypeSync), + match type_pb { + proto::Type::CONNECT => {} + proto::Type::SYNC => return Err(UpgradeError::UnexpectedTypeSync), } Ok(PendingConnect { @@ -94,7 +94,7 @@ impl upgrade::InboundUpgrade for Upgrade { } pub struct PendingConnect { - substream: Framed>, + substream: Framed>>, remote_obs_addrs: Vec, } @@ -103,22 +103,21 @@ impl PendingConnect { mut self, local_obs_addrs: Vec, ) -> Result, UpgradeError> { - let msg = HolePunch { - r#type: hole_punch::Type::Connect.into(), - obs_addrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).collect(), + let msg = proto::HolePunch { + type_pb: proto::Type::CONNECT, + ObsAddrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).map(|a| Cow::from(a)).collect(), }; self.substream.send(msg).await?; - let HolePunch { r#type, .. } = self + let proto::HolePunch { type_pb, .. } = self .substream .next() .await .ok_or(UpgradeError::StreamClosed)??; - let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?; - match r#type { - hole_punch::Type::Connect => return Err(UpgradeError::UnexpectedTypeConnect), - hole_punch::Type::Sync => {} + match type_pb { + proto::Type::CONNECT => return Err(UpgradeError::UnexpectedTypeConnect), + proto::Type::SYNC => {} } Ok(self.remote_obs_addrs) @@ -128,7 +127,7 @@ impl PendingConnect { #[derive(Debug, Error)] pub enum UpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Expected at least one address in reservation.")] diff --git a/protocols/dcutr/src/protocol/outbound.rs b/protocols/dcutr/src/protocol/outbound.rs index 20b3a781155..f4d714c1dc3 100644 --- a/protocols/dcutr/src/protocol/outbound.rs +++ b/protocols/dcutr/src/protocol/outbound.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{hole_punch, HolePunch}; +use std::borrow::Cow; use asynchronous_codec::Framed; use futures::{future::BoxFuture, prelude::*}; use futures_timer::Delay; @@ -28,6 +28,7 @@ use libp2p_swarm::NegotiatedSubstream; use std::convert::TryFrom; use std::iter; use thiserror::Error; +use crate::proto; pub struct Upgrade { obs_addrs: Vec, @@ -56,12 +57,12 @@ impl upgrade::OutboundUpgrade for Upgrade { fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { let mut substream = Framed::new( substream, - prost_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES), ); - let msg = HolePunch { - r#type: hole_punch::Type::Connect.into(), - obs_addrs: self.obs_addrs.into_iter().map(|a| a.to_vec()).collect(), + let msg = proto::HolePunch { + type_pb: proto::Type::CONNECT, + ObsAddrs: self.obs_addrs.into_iter().map(|a| Cow::from(a.to_vec())).collect(), }; async move { @@ -69,23 +70,22 @@ impl upgrade::OutboundUpgrade for Upgrade { let sent_time = Instant::now(); - let HolePunch { r#type, obs_addrs } = + let proto::HolePunch { type_pb, ObsAddrs } = substream.next().await.ok_or(UpgradeError::StreamClosed)??; let rtt = sent_time.elapsed(); - let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?; - match r#type { - hole_punch::Type::Connect => {} - hole_punch::Type::Sync => return Err(UpgradeError::UnexpectedTypeSync), + match type_pb { + proto::Type::CONNECT => {} + proto::Type::SYNC => return Err(UpgradeError::UnexpectedTypeSync), } - let obs_addrs = if obs_addrs.is_empty() { + let obs_addrs = if ObsAddrs.is_empty() { return Err(UpgradeError::NoAddresses); } else { - obs_addrs + ObsAddrs .into_iter() - .filter_map(|a| match Multiaddr::try_from(a) { + .filter_map(|a| match Multiaddr::try_from(a.to_vec()) { Ok(a) => Some(a), Err(e) => { log::debug!("Unable to parse multiaddr: {e}"); @@ -104,9 +104,9 @@ impl upgrade::OutboundUpgrade for Upgrade { .collect::>() }; - let msg = HolePunch { - r#type: hole_punch::Type::Sync.into(), - obs_addrs: vec![], + let msg = proto::HolePunch { + type_pb: proto::Type::SYNC, + ObsAddrs: vec![], }; substream.send(msg).await?; @@ -126,7 +126,7 @@ pub struct Connect { #[derive(Debug, Error)] pub enum UpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Expected 'status' field to be set.")] diff --git a/protocols/floodsub/Cargo.toml b/protocols/floodsub/Cargo.toml index d1fd91ee459..2c09335f688 100644 --- a/protocols/floodsub/Cargo.toml +++ b/protocols/floodsub/Cargo.toml @@ -19,7 +19,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" prost = "0.11" -prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" smallvec = "1.6.1" thiserror = "1.0.37" diff --git a/protocols/floodsub/src/protocol.rs b/protocols/floodsub/src/protocol.rs index fe3f2859437..c92b89991b8 100644 --- a/protocols/floodsub/src/protocol.rs +++ b/protocols/floodsub/src/protocol.rs @@ -65,7 +65,7 @@ where Box::pin(async move { let mut framed = Framed::new( socket, - prost_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), ); let rpc = framed @@ -120,7 +120,7 @@ pub enum FloodsubError { #[derive(thiserror::Error, Debug)] #[error(transparent)] -pub struct CodecError(#[from] prost_codec::Error); +pub struct CodecError(#[from] quick_protobuf_codec::Error); /// An RPC received by the floodsub system. #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -152,7 +152,7 @@ where Box::pin(async move { let mut framed = Framed::new( socket, - prost_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), ); framed.send(self.into_rpc()).await?; framed.close().await?; diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 3fd190fc900..185f4b1ab3a 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -25,7 +25,7 @@ sha2 = "0.10.0" base64 = "0.20.0" smallvec = "1.6.1" prost = "0.11" -prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } hex_fmt = "0.3.0" regex = "1.5.5" serde = { version = "1", optional = true, features = ["derive"] } diff --git a/protocols/gossipsub/src/error.rs b/protocols/gossipsub/src/error.rs index feb9231b662..66bc05dbc86 100644 --- a/protocols/gossipsub/src/error.rs +++ b/protocols/gossipsub/src/error.rs @@ -106,7 +106,7 @@ pub enum HandlerError { #[error("Protocol negotiation failed.")] NegotiationProtocolError(ProtocolError), #[error("Failed to encode or decode")] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), } #[derive(Debug, Clone, Copy)] @@ -142,7 +142,7 @@ impl std::error::Error for ValidationError {} impl From for HandlerError { fn from(error: std::io::Error) -> HandlerError { - HandlerError::Codec(prost_codec::Error::from(error)) + HandlerError::Codec(quick_protobuf_codec::Error::from(error)) } } diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index 3524fc85854..7926c5ff8d2 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -191,12 +191,12 @@ pub struct GossipsubCodec { /// Determines the level of validation performed on incoming messages. validation_mode: ValidationMode, /// The codec to handle common encoding/decoding of protobuf messages - codec: prost_codec::Codec, + codec: quick_protobuf_codec::Codec, } impl GossipsubCodec { pub fn new(length_codec: codec::UviBytes, validation_mode: ValidationMode) -> GossipsubCodec { - let codec = prost_codec::Codec::new(length_codec.max_len()); + let codec = quick_protobuf_codec::Codec::new(length_codec.max_len()); GossipsubCodec { validation_mode, codec, diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 7fc875736b8..35e6417058a 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -18,7 +18,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4.1" lru = "0.9.0" -prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } prost = "0.11" smallvec = "1.6.1" thiserror = "1.0" diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 9ef1067a49a..96365497552 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -180,7 +180,7 @@ where let mut framed_io = FramedWrite::new( io, - prost_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), ); framed_io.send(message).await?; @@ -197,7 +197,7 @@ where let info = FramedRead::new( socket, - prost_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), ) .next() .await @@ -255,7 +255,7 @@ impl TryFrom for Info { #[derive(Debug, Error)] pub enum UpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("I/O interaction failed")] Io(#[from] io::Error), #[error("Stream closed")] diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index b517cc0e145..57e98c4e17d 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -21,7 +21,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" pin-project = "1" -prost-codec = { version = "0.3", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } prost = "0.11" rand = "0.8.4" smallvec = "1.6.1" diff --git a/protocols/relay/src/protocol/inbound_hop.rs b/protocols/relay/src/protocol/inbound_hop.rs index dec290e0e6c..23e376fb11d 100644 --- a/protocols/relay/src/protocol/inbound_hop.rs +++ b/protocols/relay/src/protocol/inbound_hop.rs @@ -51,7 +51,7 @@ impl upgrade::InboundUpgrade for Upgrade { type Future = BoxFuture<'static, Result>; fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { - let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); async move { let HopMessage { @@ -96,8 +96,8 @@ pub enum UpgradeError { Fatal(#[from] FatalUpgradeError), } -impl From for UpgradeError { - fn from(error: prost_codec::Error) -> Self { +impl From for UpgradeError { + fn from(error: quick_protobuf_codec::Error) -> Self { Self::Fatal(error.into()) } } @@ -105,7 +105,7 @@ impl From for UpgradeError { #[derive(Debug, Error)] pub enum FatalUpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Failed to parse response type field.")] @@ -124,7 +124,7 @@ pub enum Req { } pub struct ReservationReq { - substream: Framed>, + substream: Framed>, reservation_duration: Duration, max_circuit_duration: Duration, max_circuit_bytes: u64, @@ -181,7 +181,7 @@ impl ReservationReq { pub struct CircuitReq { dst: PeerId, - substream: Framed>, + substream: Framed>, } impl CircuitReq { @@ -226,7 +226,7 @@ impl CircuitReq { self.substream.close().await.map_err(Into::into) } - async fn send(&mut self, msg: HopMessage) -> Result<(), prost_codec::Error> { + async fn send(&mut self, msg: HopMessage) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; diff --git a/protocols/relay/src/protocol/inbound_stop.rs b/protocols/relay/src/protocol/inbound_stop.rs index d3cad292bd7..30e3d57de4b 100644 --- a/protocols/relay/src/protocol/inbound_stop.rs +++ b/protocols/relay/src/protocol/inbound_stop.rs @@ -45,7 +45,7 @@ impl upgrade::InboundUpgrade for Upgrade { type Future = BoxFuture<'static, Result>; fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { - let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); async move { let StopMessage { @@ -84,8 +84,8 @@ pub enum UpgradeError { Fatal(#[from] FatalUpgradeError), } -impl From for UpgradeError { - fn from(error: prost_codec::Error) -> Self { +impl From for UpgradeError { + fn from(error: quick_protobuf_codec::Error) -> Self { Self::Fatal(error.into()) } } @@ -93,7 +93,7 @@ impl From for UpgradeError { #[derive(Debug, Error)] pub enum FatalUpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Failed to parse response type field.")] @@ -107,7 +107,7 @@ pub enum FatalUpgradeError { } pub struct Circuit { - substream: Framed>, + substream: Framed>, src_peer_id: PeerId, limit: Option, } @@ -156,7 +156,7 @@ impl Circuit { self.send(msg).await.map_err(Into::into) } - async fn send(&mut self, msg: StopMessage) -> Result<(), prost_codec::Error> { + async fn send(&mut self, msg: StopMessage) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; diff --git a/protocols/relay/src/protocol/outbound_hop.rs b/protocols/relay/src/protocol/outbound_hop.rs index c692c7a2596..229be977a52 100644 --- a/protocols/relay/src/protocol/outbound_hop.rs +++ b/protocols/relay/src/protocol/outbound_hop.rs @@ -71,7 +71,7 @@ impl upgrade::OutboundUpgrade for Upgrade { }, }; - let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); async move { substream.send(msg).await?; @@ -205,8 +205,8 @@ pub enum UpgradeError { Fatal(#[from] FatalUpgradeError), } -impl From for UpgradeError { - fn from(error: prost_codec::Error) -> Self { +impl From for UpgradeError { + fn from(error: quick_protobuf_codec::Error) -> Self { Self::Fatal(error.into()) } } @@ -234,7 +234,7 @@ pub enum ReservationFailedReason { #[derive(Debug, Error)] pub enum FatalUpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Expected 'status' field to be set.")] diff --git a/protocols/relay/src/protocol/outbound_stop.rs b/protocols/relay/src/protocol/outbound_stop.rs index 08056412d53..e42b8eefe74 100644 --- a/protocols/relay/src/protocol/outbound_stop.rs +++ b/protocols/relay/src/protocol/outbound_stop.rs @@ -69,7 +69,7 @@ impl upgrade::OutboundUpgrade for Upgrade { status: None, }; - let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); async move { substream.send(msg).await?; @@ -130,8 +130,8 @@ pub enum UpgradeError { Fatal(#[from] FatalUpgradeError), } -impl From for UpgradeError { - fn from(error: prost_codec::Error) -> Self { +impl From for UpgradeError { + fn from(error: quick_protobuf_codec::Error) -> Self { Self::Fatal(error.into()) } } @@ -147,7 +147,7 @@ pub enum CircuitFailedReason { #[derive(Debug, Error)] pub enum FatalUpgradeError { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Stream closed")] StreamClosed, #[error("Expected 'status' field to be set.")] diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index e2a37bd686c..9be32554df1 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -20,7 +20,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" prost = "0.11" -prost-codec = { version = "0.3.0", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" sha2 = "0.10" thiserror = "1" diff --git a/protocols/rendezvous/src/codec.rs b/protocols/rendezvous/src/codec.rs index b14795417e0..d28a01f77b1 100644 --- a/protocols/rendezvous/src/codec.rs +++ b/protocols/rendezvous/src/codec.rs @@ -200,13 +200,13 @@ pub enum ErrorCode { } pub struct RendezvousCodec { - inner: prost_codec::Codec, + inner: quick_protobuf_codec::Codec, } impl Default for RendezvousCodec { fn default() -> Self { Self { - inner: prost_codec::Codec::new(1024 * 1024), // 1MB + inner: quick_protobuf_codec::Codec::new(1024 * 1024), // 1MB } } } @@ -239,7 +239,7 @@ impl Decoder for RendezvousCodec { #[derive(Debug, thiserror::Error)] pub enum Error { #[error(transparent)] - Codec(#[from] prost_codec::Error), + Codec(#[from] quick_protobuf_codec::Error), #[error("Failed to read/write")] Io(#[from] std::io::Error), #[error("Failed to convert wire message to internal data model")] diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index e167f097404..b2a132b1d68 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -23,7 +23,7 @@ libp2p-noise = { version = "0.42.0", path = "../../transports/noise" } log = "0.4" multihash = { version = "0.17.0", default-features = false, features = ["sha2"] } prost = "0.11" -prost-codec = { version = "0.3.0", path = "../../misc/quick-protobuf-codec" } +quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" rcgen = "0.9.3" serde = { version = "1.0", features = ["derive"] } diff --git a/transports/webrtc/src/tokio/substream.rs b/transports/webrtc/src/tokio/substream.rs index 8622719a37c..eadaee4d989 100644 --- a/transports/webrtc/src/tokio/substream.rs +++ b/transports/webrtc/src/tokio/substream.rs @@ -238,7 +238,7 @@ impl AsyncWrite for Substream { } fn io_poll_next( - io: &mut Framed, prost_codec::Codec>, + io: &mut Framed, quick_protobuf_codec::Codec>, cx: &mut Context<'_>, ) -> Poll, Option>)>>> { match ready!(io.poll_next_unpin(cx)) diff --git a/transports/webrtc/src/tokio/substream/framed_dc.rs b/transports/webrtc/src/tokio/substream/framed_dc.rs index 39bd117f1f1..bd9c2eff373 100644 --- a/transports/webrtc/src/tokio/substream/framed_dc.rs +++ b/transports/webrtc/src/tokio/substream/framed_dc.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use super::{MAX_DATA_LEN, MAX_MSG_LEN, VARINT_LEN}; use crate::message_proto::Message; -pub type FramedDc = Framed, prost_codec::Codec>; +pub type FramedDc = Framed, quick_protobuf_codec::Codec>; pub fn new(data_channel: Arc) -> FramedDc { let mut inner = PollDataChannel::new(data_channel); @@ -36,7 +36,7 @@ pub fn new(data_channel: Arc) -> FramedDc { let mut framed = Framed::new( inner.compat(), - prost_codec::Codec::new(MAX_MSG_LEN - VARINT_LEN), + quick_protobuf_codec::Codec::new(MAX_MSG_LEN - VARINT_LEN), ); // If not set, `Framed` buffers up to 131kB of data before sending, which leads to "outbound // packet larger than maximum message size" error in webrtc-rs. From a6b63ac504df04c570bb7e27cc784e5dc31adb4d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 14:00:51 +1100 Subject: [PATCH 30/66] Don't use `Cow` --- core/src/generated/envelope_proto.rs | 33 +- core/src/generated/keys_proto.rs | 21 +- core/src/generated/peer_record_proto.rs | 32 +- core/src/identity.rs | 21 +- core/src/peer_record.rs | 5 +- core/src/signed_envelope.rs | 6 +- misc/quick-protobuf-codec/src/lib.rs | 5 +- protocols/autonat/src/generated/structs.rs | 47 +- protocols/autonat/src/protocol.rs | 4 +- protocols/dcutr/src/generated/holepunch/pb.rs | 11 +- protocols/dcutr/src/protocol/inbound.rs | 9 +- protocols/dcutr/src/protocol/outbound.rs | 9 +- protocols/floodsub/src/floodsub/mod.rs | 2 + protocols/floodsub/src/floodsub/pb.rs | 137 +++++ .../floodsub/src/generated/floodsub/pb.rs | 44 +- protocols/floodsub/src/mod.rs | 2 + protocols/gossipsub/src/compat/mod.rs | 2 + protocols/gossipsub/src/compat/pb.rs | 67 +++ .../gossipsub/src/generated/compat/pb.rs | 31 +- .../gossipsub/src/generated/gossipsub/pb.rs | 163 +++-- protocols/gossipsub/src/gossipsub/mod.rs | 2 + protocols/gossipsub/src/gossipsub/pb.rs | 567 ++++++++++++++++++ protocols/gossipsub/src/mod.rs | 3 + protocols/identify/src/generated/structs.rs | 31 +- protocols/identify/src/mod.rs | 2 + protocols/identify/src/structs.rs | 67 +++ protocols/kad/src/dht/mod.rs | 2 + protocols/kad/src/dht/pb.rs | 243 ++++++++ protocols/kad/src/generated/dht/pb.rs | 78 ++- protocols/kad/src/mod.rs | 2 + protocols/relay/Cargo.toml | 1 + protocols/relay/build.rs | 23 - protocols/relay/src/behaviour.rs | 8 +- protocols/relay/src/behaviour/handler.rs | 26 +- .../relay/src/generated/message_v2/pb.rs | 47 +- protocols/relay/src/lib.rs | 8 +- protocols/relay/src/message_v2/mod.rs | 2 + protocols/relay/src/message_v2/pb.rs | 346 +++++++++++ protocols/relay/src/mod.rs | 2 + protocols/relay/src/priv_client/handler.rs | 4 +- protocols/relay/src/protocol.rs | 6 +- protocols/relay/src/protocol/inbound_hop.rs | 64 +- protocols/relay/src/protocol/inbound_stop.rs | 43 +- protocols/relay/src/protocol/outbound_hop.rs | 62 +- protocols/relay/src/protocol/outbound_stop.rs | 39 +- .../rendezvous/src/generated/rendezvous/pb.rs | 85 ++- protocols/rendezvous/src/mod.rs | 2 + protocols/rendezvous/src/rendezvous/mod.rs | 2 + protocols/rendezvous/src/rendezvous/pb.rs | 364 +++++++++++ transports/noise/build.rs | 23 - .../noise/src/generated/payload/proto.rs | 31 +- transports/noise/src/io/handshake.rs | 5 - .../noise/src/io/handshake/payload.proto | 11 - transports/plaintext/src/generated/structs.rs | 15 +- transports/plaintext/src/mod.rs | 2 + transports/plaintext/src/structs.rs | 51 ++ transports/webrtc/src/generated/webrtc/pb.rs | 11 +- transports/webrtc/src/mod.rs | 2 + transports/webrtc/src/webrtc/mod.rs | 2 + transports/webrtc/src/webrtc/pb.rs | 91 +++ 60 files changed, 2465 insertions(+), 561 deletions(-) create mode 100644 protocols/floodsub/src/floodsub/mod.rs create mode 100644 protocols/floodsub/src/floodsub/pb.rs create mode 100644 protocols/floodsub/src/mod.rs create mode 100644 protocols/gossipsub/src/compat/mod.rs create mode 100644 protocols/gossipsub/src/compat/pb.rs create mode 100644 protocols/gossipsub/src/gossipsub/mod.rs create mode 100644 protocols/gossipsub/src/gossipsub/pb.rs create mode 100644 protocols/gossipsub/src/mod.rs create mode 100644 protocols/identify/src/mod.rs create mode 100644 protocols/identify/src/structs.rs create mode 100644 protocols/kad/src/dht/mod.rs create mode 100644 protocols/kad/src/dht/pb.rs create mode 100644 protocols/kad/src/mod.rs delete mode 100644 protocols/relay/build.rs create mode 100644 protocols/relay/src/message_v2/mod.rs create mode 100644 protocols/relay/src/message_v2/pb.rs create mode 100644 protocols/relay/src/mod.rs create mode 100644 protocols/rendezvous/src/mod.rs create mode 100644 protocols/rendezvous/src/rendezvous/mod.rs create mode 100644 protocols/rendezvous/src/rendezvous/pb.rs delete mode 100644 transports/noise/build.rs delete mode 100644 transports/noise/src/io/handshake/payload.proto create mode 100644 transports/plaintext/src/mod.rs create mode 100644 transports/plaintext/src/structs.rs create mode 100644 transports/webrtc/src/mod.rs create mode 100644 transports/webrtc/src/webrtc/mod.rs create mode 100644 transports/webrtc/src/webrtc/pb.rs diff --git a/core/src/generated/envelope_proto.rs b/core/src/generated/envelope_proto.rs index 32cfc7ed207..268b6a334f3 100644 --- a/core/src/generated/envelope_proto.rs +++ b/core/src/generated/envelope_proto.rs @@ -9,29 +9,28 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Envelope<'a> { - pub public_key: Option>, - pub payload_type: Cow<'a, [u8]>, - pub payload: Cow<'a, [u8]>, - pub signature: Cow<'a, [u8]>, +pub struct Envelope { + pub public_key: Option, + pub payload_type: Vec, + pub payload: Vec, + pub signature: Vec, } -impl<'a> MessageRead<'a> for Envelope<'a> { +impl<'a> MessageRead<'a> for Envelope { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(10) => msg.public_key = Some(r.read_message::(bytes)?), - Ok(18) => msg.payload_type = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(26) => msg.payload = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(42) => msg.signature = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.payload_type = r.read_bytes(bytes)?.to_owned(), + Ok(26) => msg.payload = r.read_bytes(bytes)?.to_owned(), + Ok(42) => msg.signature = r.read_bytes(bytes)?.to_owned(), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -40,20 +39,20 @@ impl<'a> MessageRead<'a> for Envelope<'a> { } } -impl<'a> MessageWrite for Envelope<'a> { +impl MessageWrite for Envelope { fn get_size(&self) -> usize { 0 + self.public_key.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + if self.payload_type == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.payload_type).len()) } - + if self.payload == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.payload).len()) } - + if self.signature == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.signature).len()) } + + if self.payload_type.is_empty() { 0 } else { 1 + sizeof_len((&self.payload_type).len()) } + + if self.payload.is_empty() { 0 } else { 1 + sizeof_len((&self.payload).len()) } + + if self.signature.is_empty() { 0 } else { 1 + sizeof_len((&self.signature).len()) } } fn write_message(&self, w: &mut Writer) -> Result<()> { if let Some(ref s) = self.public_key { w.write_with_tag(10, |w| w.write_message(s))?; } - if self.payload_type != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.payload_type))?; } - if self.payload != Cow::Borrowed(b"") { w.write_with_tag(26, |w| w.write_bytes(&**&self.payload))?; } - if self.signature != Cow::Borrowed(b"") { w.write_with_tag(42, |w| w.write_bytes(&**&self.signature))?; } + if !self.payload_type.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.payload_type))?; } + if !self.payload.is_empty() { w.write_with_tag(26, |w| w.write_bytes(&**&self.payload))?; } + if !self.signature.is_empty() { w.write_with_tag(42, |w| w.write_bytes(&**&self.signature))?; } Ok(()) } } diff --git a/core/src/generated/keys_proto.rs b/core/src/generated/keys_proto.rs index 2d7a022760b..ba15fed5004 100644 --- a/core/src/generated/keys_proto.rs +++ b/core/src/generated/keys_proto.rs @@ -9,7 +9,6 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::*; @@ -54,18 +53,18 @@ impl<'a> From<&'a str> for KeyType { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct PublicKey<'a> { +pub struct PublicKey { pub Type: keys_proto::KeyType, - pub Data: Cow<'a, [u8]>, + pub Data: Vec, } -impl<'a> MessageRead<'a> for PublicKey<'a> { +impl<'a> MessageRead<'a> for PublicKey { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.Type = r.read_enum(bytes)?, - Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.Data = r.read_bytes(bytes)?.to_owned(), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -74,7 +73,7 @@ impl<'a> MessageRead<'a> for PublicKey<'a> { } } -impl<'a> MessageWrite for PublicKey<'a> { +impl MessageWrite for PublicKey { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.Type) as u64) @@ -90,18 +89,18 @@ impl<'a> MessageWrite for PublicKey<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct PrivateKey<'a> { +pub struct PrivateKey { pub Type: keys_proto::KeyType, - pub Data: Cow<'a, [u8]>, + pub Data: Vec, } -impl<'a> MessageRead<'a> for PrivateKey<'a> { +impl<'a> MessageRead<'a> for PrivateKey { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.Type = r.read_enum(bytes)?, - Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.Data = r.read_bytes(bytes)?.to_owned(), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -110,7 +109,7 @@ impl<'a> MessageRead<'a> for PrivateKey<'a> { } } -impl<'a> MessageWrite for PrivateKey<'a> { +impl MessageWrite for PrivateKey { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.Type) as u64) diff --git a/core/src/generated/peer_record_proto.rs b/core/src/generated/peer_record_proto.rs index b76f2f5277c..c771caacd2e 100644 --- a/core/src/generated/peer_record_proto.rs +++ b/core/src/generated/peer_record_proto.rs @@ -9,25 +9,24 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct PeerRecord<'a> { - pub peer_id: Cow<'a, [u8]>, +pub struct PeerRecord { + pub peer_id: Vec, pub seq: u64, - pub addresses: Vec>, + pub addresses: Vec, } -impl<'a> MessageRead<'a> for PeerRecord<'a> { +impl<'a> MessageRead<'a> for PeerRecord { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.peer_id = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(10) => msg.peer_id = r.read_bytes(bytes)?.to_owned(), Ok(16) => msg.seq = r.read_uint64(bytes)?, Ok(26) => msg.addresses.push(r.read_message::(bytes)?), Ok(t) => { r.read_unknown(bytes, t)?; } @@ -38,16 +37,16 @@ impl<'a> MessageRead<'a> for PeerRecord<'a> { } } -impl<'a> MessageWrite for PeerRecord<'a> { +impl MessageWrite for PeerRecord { fn get_size(&self) -> usize { 0 - + if self.peer_id == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.peer_id).len()) } + + if self.peer_id.is_empty() { 0 } else { 1 + sizeof_len((&self.peer_id).len()) } + if self.seq == 0u64 { 0 } else { 1 + sizeof_varint(*(&self.seq) as u64) } + self.addresses.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() } fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.peer_id != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.peer_id))?; } + if !self.peer_id.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.peer_id))?; } if self.seq != 0u64 { w.write_with_tag(16, |w| w.write_uint64(*&self.seq))?; } for s in &self.addresses { w.write_with_tag(26, |w| w.write_message(s))?; } Ok(()) @@ -56,21 +55,20 @@ impl<'a> MessageWrite for PeerRecord<'a> { pub mod mod_PeerRecord { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct AddressInfo<'a> { - pub multiaddr: Cow<'a, [u8]>, +pub struct AddressInfo { + pub multiaddr: Vec, } -impl<'a> MessageRead<'a> for AddressInfo<'a> { +impl<'a> MessageRead<'a> for AddressInfo { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.multiaddr = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(10) => msg.multiaddr = r.read_bytes(bytes)?.to_owned(), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -79,14 +77,14 @@ impl<'a> MessageRead<'a> for AddressInfo<'a> { } } -impl<'a> MessageWrite for AddressInfo<'a> { +impl MessageWrite for AddressInfo { fn get_size(&self) -> usize { 0 - + if self.multiaddr == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.multiaddr).len()) } + + if self.multiaddr.is_empty() { 0 } else { 1 + sizeof_len((&self.multiaddr).len()) } } fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.multiaddr != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.multiaddr))?; } + if !self.multiaddr.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.multiaddr))?; } Ok(()) } } diff --git a/core/src/identity.rs b/core/src/identity.rs index d38a2af0e91..07eacccf25d 100644 --- a/core/src/identity.rs +++ b/core/src/identity.rs @@ -45,7 +45,6 @@ pub mod error; use self::error::*; use crate::{proto, PeerId}; use quick_protobuf::{BytesReader, Writer}; -use std::borrow::Cow; use std::convert::{TryFrom, TryInto}; /// Identity keypair of a node. @@ -154,7 +153,7 @@ impl Keypair { let pk = match self { Self::Ed25519(data) => proto::PrivateKey { Type: proto::KeyType::Ed25519, - Data: Cow::from(data.encode().to_vec()), + Data: data.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] Self::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), @@ -182,7 +181,7 @@ impl Keypair { match private_key.Type { proto::KeyType::Ed25519 => { - ed25519::Keypair::decode(private_key.Data.to_mut()).map(Keypair::Ed25519) + ed25519::Keypair::decode(&mut private_key.Data).map(Keypair::Ed25519) } proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), @@ -191,10 +190,10 @@ impl Keypair { } } -impl zeroize::Zeroize for proto::PrivateKey<'_> { +impl zeroize::Zeroize for proto::PrivateKey { fn zeroize(&mut self) { self.Type = proto::KeyType::default(); - self.Data = Cow::default(); + self.Data = Vec::default(); } } @@ -268,33 +267,33 @@ impl PublicKey { } } -impl From<&PublicKey> for proto::PublicKey<'_> { +impl From<&PublicKey> for proto::PublicKey { fn from(key: &PublicKey) -> Self { match key { PublicKey::Ed25519(key) => proto::PublicKey { Type: proto::KeyType::Ed25519, - Data: Cow::from(key.encode().to_vec()), + Data: key.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] PublicKey::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, - Data: Cow::from(key.encode_x509()), + Data: key.encode_x509(), }, #[cfg(feature = "secp256k1")] PublicKey::Secp256k1(key) => proto::PublicKey { Type: proto::KeyType::Secp256k1, - Data: Cow::from(key.encode().to_vec()), + Data: key.encode().to_vec(), }, #[cfg(feature = "ecdsa")] PublicKey::Ecdsa(key) => proto::PublicKey { Type: proto::KeyType::ECDSA, - Data: Cow::from(key.encode_der()), + Data: key.encode_der(), }, } } } -impl TryFrom> for PublicKey { +impl TryFrom for PublicKey { type Error = DecodingError; fn try_from(pubkey: proto::PublicKey) -> Result { diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 56e3fb48ce5..dc7fe826f42 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -5,7 +5,6 @@ use crate::signed_envelope::SignedEnvelope; use crate::{signed_envelope, DecodeError, Multiaddr, PeerId}; use instant::SystemTime; use quick_protobuf::{BytesReader, Writer}; -use std::borrow::Cow; use std::convert::TryInto; const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record"; @@ -74,12 +73,12 @@ impl PeerRecord { let payload = { let record = proto::PeerRecord { - peer_id: Cow::from(peer_id.to_bytes()), + peer_id: peer_id.to_bytes(), seq, addresses: addresses .iter() .map(|m| proto::AddressInfo { - multiaddr: Cow::Borrowed(m.as_ref()), + multiaddr: m.to_vec(), }) .collect(), }; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 32af34fdfed..78e2e18b67e 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -79,9 +79,9 @@ impl SignedEnvelope { let envelope = proto::Envelope { public_key: Some((&self.key).into()), - payload_type: Cow::Borrowed(&self.payload_type), - payload: Cow::Borrowed(&self.payload), - signature: Cow::Borrowed(&self.signature), + payload_type: self.payload_type, + payload: self.payload, + signature: self.signature, }; let mut buf = Vec::with_capacity(envelope.get_size()); diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index c70c70a594d..9648ae08d15 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -45,7 +45,10 @@ impl Encoder for Codec { } } -impl MessageRead<'a> + Default> Decoder for Codec { +impl Decoder for Codec +where + Out: for<'a> MessageRead<'a>, +{ type Item = Out; type Error = Error; diff --git a/protocols/autonat/src/generated/structs.rs b/protocols/autonat/src/generated/structs.rs index 28cb742e26c..3a6d416b2b1 100644 --- a/protocols/autonat/src/generated/structs.rs +++ b/protocols/autonat/src/generated/structs.rs @@ -15,13 +15,13 @@ use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { +pub struct Message { pub type_pb: Option, - pub dial: Option>, - pub dialResponse: Option>, + pub dial: Option, + pub dialResponse: Option, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -37,7 +37,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -55,23 +55,22 @@ impl<'a> MessageWrite for Message<'a> { pub mod mod_Message { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct PeerInfo<'a> { - pub id: Option>, - pub addrs: Vec>, +pub struct PeerInfo { + pub id: Option>, + pub addrs: Vec>, } -impl<'a> MessageRead<'a> for PeerInfo<'a> { +impl<'a> MessageRead<'a> for PeerInfo { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -80,7 +79,7 @@ impl<'a> MessageRead<'a> for PeerInfo<'a> { } } -impl<'a> MessageWrite for PeerInfo<'a> { +impl MessageWrite for PeerInfo { fn get_size(&self) -> usize { 0 + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -96,11 +95,11 @@ impl<'a> MessageWrite for PeerInfo<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Dial<'a> { - pub peer: Option>, +pub struct Dial { + pub peer: Option, } -impl<'a> MessageRead<'a> for Dial<'a> { +impl<'a> MessageRead<'a> for Dial { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -114,7 +113,7 @@ impl<'a> MessageRead<'a> for Dial<'a> { } } -impl<'a> MessageWrite for Dial<'a> { +impl MessageWrite for Dial { fn get_size(&self) -> usize { 0 + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) @@ -128,20 +127,20 @@ impl<'a> MessageWrite for Dial<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct DialResponse<'a> { +pub struct DialResponse { pub status: Option, - pub statusText: Option>, - pub addr: Option>, + pub statusText: Option, + pub addr: Option>, } -impl<'a> MessageRead<'a> for DialResponse<'a> { +impl<'a> MessageRead<'a> for DialResponse { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.status = Some(r.read_enum(bytes)?), - Ok(18) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.addr = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), + Ok(26) => msg.addr = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -150,7 +149,7 @@ impl<'a> MessageRead<'a> for DialResponse<'a> { } } -impl<'a> MessageWrite for DialResponse<'a> { +impl MessageWrite for DialResponse { fn get_size(&self) -> usize { 0 + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/protocol.rs index 4d53b2d420c..288877e745a 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/protocol.rs @@ -162,14 +162,14 @@ impl DialRequest { let addrs = self .addresses .into_iter() - .map(|addr| Cow::from(addr.to_vec())) + .map(|addr| addr.to_vec()) .collect(); let msg = proto::Message { type_pb: Some(proto::MessageType::DIAL), dial: Some(proto::Dial { peer: Some(proto::PeerInfo { - id: Some(Cow::from(peer_id)), + id: Some(peer_id.to_vec()), addrs, }), }), diff --git a/protocols/dcutr/src/generated/holepunch/pb.rs b/protocols/dcutr/src/generated/holepunch/pb.rs index de44ccf7cd4..59f37040f14 100644 --- a/protocols/dcutr/src/generated/holepunch/pb.rs +++ b/protocols/dcutr/src/generated/holepunch/pb.rs @@ -9,25 +9,24 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct HolePunch<'a> { +pub struct HolePunch { pub type_pb: holepunch::pb::mod_HolePunch::Type, - pub ObsAddrs: Vec>, + pub ObsAddrs: Vec>, } -impl<'a> MessageRead<'a> for HolePunch<'a> { +impl<'a> MessageRead<'a> for HolePunch { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.type_pb = r.read_enum(bytes)?, - Ok(18) => msg.ObsAddrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.ObsAddrs.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -36,7 +35,7 @@ impl<'a> MessageRead<'a> for HolePunch<'a> { } } -impl<'a> MessageWrite for HolePunch<'a> { +impl MessageWrite for HolePunch { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.type_pb) as u64) diff --git a/protocols/dcutr/src/protocol/inbound.rs b/protocols/dcutr/src/protocol/inbound.rs index b47a9eb2899..96bee210e2a 100644 --- a/protocols/dcutr/src/protocol/inbound.rs +++ b/protocols/dcutr/src/protocol/inbound.rs @@ -18,7 +18,6 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::borrow::Cow; use crate::proto; use asynchronous_codec::Framed; use futures::{future::BoxFuture, prelude::*}; @@ -78,7 +77,6 @@ impl upgrade::InboundUpgrade for Upgrade { .collect::>() }; - match type_pb { proto::Type::CONNECT => {} proto::Type::SYNC => return Err(UpgradeError::UnexpectedTypeSync), @@ -94,7 +92,7 @@ impl upgrade::InboundUpgrade for Upgrade { } pub struct PendingConnect { - substream: Framed>>, + substream: Framed>, remote_obs_addrs: Vec, } @@ -105,7 +103,10 @@ impl PendingConnect { ) -> Result, UpgradeError> { let msg = proto::HolePunch { type_pb: proto::Type::CONNECT, - ObsAddrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).map(|a| Cow::from(a)).collect(), + ObsAddrs: local_obs_addrs + .into_iter() + .map(|a| a.to_vec()) + .collect(), }; self.substream.send(msg).await?; diff --git a/protocols/dcutr/src/protocol/outbound.rs b/protocols/dcutr/src/protocol/outbound.rs index f4d714c1dc3..7bebcd260a4 100644 --- a/protocols/dcutr/src/protocol/outbound.rs +++ b/protocols/dcutr/src/protocol/outbound.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::borrow::Cow; +use crate::proto; use asynchronous_codec::Framed; use futures::{future::BoxFuture, prelude::*}; use futures_timer::Delay; @@ -28,7 +28,6 @@ use libp2p_swarm::NegotiatedSubstream; use std::convert::TryFrom; use std::iter; use thiserror::Error; -use crate::proto; pub struct Upgrade { obs_addrs: Vec, @@ -62,7 +61,11 @@ impl upgrade::OutboundUpgrade for Upgrade { let msg = proto::HolePunch { type_pb: proto::Type::CONNECT, - ObsAddrs: self.obs_addrs.into_iter().map(|a| Cow::from(a.to_vec())).collect(), + ObsAddrs: self + .obs_addrs + .into_iter() + .map(|a| a.to_vec()) + .collect(), }; async move { diff --git a/protocols/floodsub/src/floodsub/mod.rs b/protocols/floodsub/src/floodsub/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/floodsub/src/floodsub/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/floodsub/src/floodsub/pb.rs b/protocols/floodsub/src/floodsub/pb.rs new file mode 100644 index 00000000000..a11135ad053 --- /dev/null +++ b/protocols/floodsub/src/floodsub/pb.rs @@ -0,0 +1,137 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RPC { + pub subscriptions: Vec, + pub publish: Vec, +} + +impl<'a> MessageRead<'a> for RPC { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), + Ok(18) => msg.publish.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for RPC { + fn get_size(&self) -> usize { + 0 + + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_RPC { + +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct SubOpts { + pub subscribe: Option, + pub topic_id: Option, +} + +impl<'a> MessageRead<'a> for SubOpts { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for SubOpts { + fn get_size(&self) -> usize { + 0 + + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } + if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/floodsub/src/generated/floodsub/pb.rs b/protocols/floodsub/src/generated/floodsub/pb.rs index 967e89add39..a11135ad053 100644 --- a/protocols/floodsub/src/generated/floodsub/pb.rs +++ b/protocols/floodsub/src/generated/floodsub/pb.rs @@ -9,19 +9,18 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct RPC<'a> { - pub subscriptions: Vec>, - pub publish: Vec>, +pub struct RPC { + pub subscriptions: Vec, + pub publish: Vec, } -impl<'a> MessageRead<'a> for RPC<'a> { +impl<'a> MessageRead<'a> for RPC { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -36,7 +35,7 @@ impl<'a> MessageRead<'a> for RPC<'a> { } } -impl<'a> MessageWrite for RPC<'a> { +impl MessageWrite for RPC { fn get_size(&self) -> usize { 0 + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() @@ -52,23 +51,22 @@ impl<'a> MessageWrite for RPC<'a> { pub mod mod_RPC { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct SubOpts<'a> { +pub struct SubOpts { pub subscribe: Option, - pub topic_id: Option>, + pub topic_id: Option, } -impl<'a> MessageRead<'a> for SubOpts<'a> { +impl<'a> MessageRead<'a> for SubOpts { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), - Ok(18) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -77,7 +75,7 @@ impl<'a> MessageRead<'a> for SubOpts<'a> { } } -impl<'a> MessageWrite for SubOpts<'a> { +impl MessageWrite for SubOpts { fn get_size(&self) -> usize { 0 + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -95,22 +93,22 @@ impl<'a> MessageWrite for SubOpts<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic_ids: Vec>, +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(34) => msg.topic_ids.push(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -119,7 +117,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) diff --git a/protocols/floodsub/src/mod.rs b/protocols/floodsub/src/mod.rs new file mode 100644 index 00000000000..1cef0041eb6 --- /dev/null +++ b/protocols/floodsub/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod floodsub; diff --git a/protocols/gossipsub/src/compat/mod.rs b/protocols/gossipsub/src/compat/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/gossipsub/src/compat/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/gossipsub/src/compat/pb.rs b/protocols/gossipsub/src/compat/pb.rs new file mode 100644 index 00000000000..fd59c38e2b4 --- /dev/null +++ b/protocols/gossipsub/src/compat/pb.rs @@ -0,0 +1,67 @@ +// Automatically generated rust module for 'compat.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec, + pub signature: Option>, + pub key: Option>, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), + Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), + Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/gossipsub/src/generated/compat/pb.rs b/protocols/gossipsub/src/generated/compat/pb.rs index 8f048013532..fd59c38e2b4 100644 --- a/protocols/gossipsub/src/generated/compat/pb.rs +++ b/protocols/gossipsub/src/generated/compat/pb.rs @@ -9,33 +9,32 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic_ids: Vec>, - pub signature: Option>, - pub key: Option>, +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic_ids: Vec, + pub signature: Option>, + pub key: Option>, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(34) => msg.topic_ids.push(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(42) => msg.signature = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(50) => msg.key = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), + Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), + Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -44,7 +43,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) diff --git a/protocols/gossipsub/src/generated/gossipsub/pb.rs b/protocols/gossipsub/src/generated/gossipsub/pb.rs index 793a1175570..9a074fd61fc 100644 --- a/protocols/gossipsub/src/generated/gossipsub/pb.rs +++ b/protocols/gossipsub/src/generated/gossipsub/pb.rs @@ -9,20 +9,19 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct RPC<'a> { - pub subscriptions: Vec>, - pub publish: Vec>, - pub control: Option>, +pub struct RPC { + pub subscriptions: Vec, + pub publish: Vec, + pub control: Option, } -impl<'a> MessageRead<'a> for RPC<'a> { +impl<'a> MessageRead<'a> for RPC { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -38,7 +37,7 @@ impl<'a> MessageRead<'a> for RPC<'a> { } } -impl<'a> MessageWrite for RPC<'a> { +impl MessageWrite for RPC { fn get_size(&self) -> usize { 0 + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() @@ -56,23 +55,22 @@ impl<'a> MessageWrite for RPC<'a> { pub mod mod_RPC { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct SubOpts<'a> { +pub struct SubOpts { pub subscribe: Option, - pub topic_id: Option>, + pub topic_id: Option, } -impl<'a> MessageRead<'a> for SubOpts<'a> { +impl<'a> MessageRead<'a> for SubOpts { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), - Ok(18) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -81,7 +79,7 @@ impl<'a> MessageRead<'a> for SubOpts<'a> { } } -impl<'a> MessageWrite for SubOpts<'a> { +impl MessageWrite for SubOpts { fn get_size(&self) -> usize { 0 + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -99,26 +97,26 @@ impl<'a> MessageWrite for SubOpts<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic: Cow<'a, str>, - pub signature: Option>, - pub key: Option>, +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic: String, + pub signature: Option>, + pub key: Option>, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.data = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(34) => msg.topic = r.read_string(bytes).map(Cow::Borrowed)?, - Ok(42) => msg.signature = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(50) => msg.key = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic = r.read_string(bytes)?.to_owned(), + Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), + Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -127,7 +125,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -151,14 +149,14 @@ impl<'a> MessageWrite for Message<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlMessage<'a> { - pub ihave: Vec>, - pub iwant: Vec>, - pub graft: Vec>, - pub prune: Vec>, +pub struct ControlMessage { + pub ihave: Vec, + pub iwant: Vec, + pub graft: Vec, + pub prune: Vec, } -impl<'a> MessageRead<'a> for ControlMessage<'a> { +impl<'a> MessageRead<'a> for ControlMessage { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -175,7 +173,7 @@ impl<'a> MessageRead<'a> for ControlMessage<'a> { } } -impl<'a> MessageWrite for ControlMessage<'a> { +impl MessageWrite for ControlMessage { fn get_size(&self) -> usize { 0 + self.ihave.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() @@ -195,18 +193,18 @@ impl<'a> MessageWrite for ControlMessage<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlIHave<'a> { - pub topic_id: Option>, - pub message_ids: Vec>, +pub struct ControlIHave { + pub topic_id: Option, + pub message_ids: Vec>, } -impl<'a> MessageRead<'a> for ControlIHave<'a> { +impl<'a> MessageRead<'a> for ControlIHave { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.message_ids.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -215,7 +213,7 @@ impl<'a> MessageRead<'a> for ControlIHave<'a> { } } -impl<'a> MessageWrite for ControlIHave<'a> { +impl MessageWrite for ControlIHave { fn get_size(&self) -> usize { 0 + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -231,16 +229,16 @@ impl<'a> MessageWrite for ControlIHave<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlIWant<'a> { - pub message_ids: Vec>, +pub struct ControlIWant { + pub message_ids: Vec>, } -impl<'a> MessageRead<'a> for ControlIWant<'a> { +impl<'a> MessageRead<'a> for ControlIWant { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.message_ids.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -249,7 +247,7 @@ impl<'a> MessageRead<'a> for ControlIWant<'a> { } } -impl<'a> MessageWrite for ControlIWant<'a> { +impl MessageWrite for ControlIWant { fn get_size(&self) -> usize { 0 + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() @@ -263,16 +261,16 @@ impl<'a> MessageWrite for ControlIWant<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlGraft<'a> { - pub topic_id: Option>, +pub struct ControlGraft { + pub topic_id: Option, } -impl<'a> MessageRead<'a> for ControlGraft<'a> { +impl<'a> MessageRead<'a> for ControlGraft { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -281,7 +279,7 @@ impl<'a> MessageRead<'a> for ControlGraft<'a> { } } -impl<'a> MessageWrite for ControlGraft<'a> { +impl MessageWrite for ControlGraft { fn get_size(&self) -> usize { 0 + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -295,18 +293,18 @@ impl<'a> MessageWrite for ControlGraft<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlPrune<'a> { - pub topic_id: Option>, - pub peers: Vec>, +pub struct ControlPrune { + pub topic_id: Option, + pub peers: Vec, pub backoff: Option, } -impl<'a> MessageRead<'a> for ControlPrune<'a> { +impl<'a> MessageRead<'a> for ControlPrune { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), Ok(18) => msg.peers.push(r.read_message::(bytes)?), Ok(24) => msg.backoff = Some(r.read_uint64(bytes)?), Ok(t) => { r.read_unknown(bytes, t)?; } @@ -317,7 +315,7 @@ impl<'a> MessageRead<'a> for ControlPrune<'a> { } } -impl<'a> MessageWrite for ControlPrune<'a> { +impl MessageWrite for ControlPrune { fn get_size(&self) -> usize { 0 + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -335,18 +333,18 @@ impl<'a> MessageWrite for ControlPrune<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct PeerInfo<'a> { - pub peer_id: Option>, - pub signed_peer_record: Option>, +pub struct PeerInfo { + pub peer_id: Option>, + pub signed_peer_record: Option>, } -impl<'a> MessageRead<'a> for PeerInfo<'a> { +impl<'a> MessageRead<'a> for PeerInfo { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.peer_id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.signed_peer_record = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.peer_id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.signed_peer_record = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -355,7 +353,7 @@ impl<'a> MessageRead<'a> for PeerInfo<'a> { } } -impl<'a> MessageWrite for PeerInfo<'a> { +impl MessageWrite for PeerInfo { fn get_size(&self) -> usize { 0 + self.peer_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -371,18 +369,18 @@ impl<'a> MessageWrite for PeerInfo<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct TopicDescriptor<'a> { - pub name: Option>, - pub auth: Option>, - pub enc: Option>, +pub struct TopicDescriptor { + pub name: Option, + pub auth: Option, + pub enc: Option, } -impl<'a> MessageRead<'a> for TopicDescriptor<'a> { +impl<'a> MessageRead<'a> for TopicDescriptor { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.name = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.name = Some(r.read_string(bytes)?.to_owned()), Ok(18) => msg.auth = Some(r.read_message::(bytes)?), Ok(26) => msg.enc = Some(r.read_message::(bytes)?), Ok(t) => { r.read_unknown(bytes, t)?; } @@ -393,7 +391,7 @@ impl<'a> MessageRead<'a> for TopicDescriptor<'a> { } } -impl<'a> MessageWrite for TopicDescriptor<'a> { +impl MessageWrite for TopicDescriptor { fn get_size(&self) -> usize { 0 + self.name.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -411,23 +409,22 @@ impl<'a> MessageWrite for TopicDescriptor<'a> { pub mod mod_TopicDescriptor { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct AuthOpts<'a> { +pub struct AuthOpts { pub mode: Option, - pub keys: Vec>, + pub keys: Vec>, } -impl<'a> MessageRead<'a> for AuthOpts<'a> { +impl<'a> MessageRead<'a> for AuthOpts { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.mode = Some(r.read_enum(bytes)?), - Ok(18) => msg.keys.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.keys.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -436,7 +433,7 @@ impl<'a> MessageRead<'a> for AuthOpts<'a> { } } -impl<'a> MessageWrite for AuthOpts<'a> { +impl MessageWrite for AuthOpts { fn get_size(&self) -> usize { 0 + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -492,18 +489,18 @@ impl<'a> From<&'a str> for AuthMode { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct EncOpts<'a> { +pub struct EncOpts { pub mode: Option, - pub key_hashes: Vec>, + pub key_hashes: Vec>, } -impl<'a> MessageRead<'a> for EncOpts<'a> { +impl<'a> MessageRead<'a> for EncOpts { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.mode = Some(r.read_enum(bytes)?), - Ok(18) => msg.key_hashes.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.key_hashes.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -512,7 +509,7 @@ impl<'a> MessageRead<'a> for EncOpts<'a> { } } -impl<'a> MessageWrite for EncOpts<'a> { +impl MessageWrite for EncOpts { fn get_size(&self) -> usize { 0 + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) diff --git a/protocols/gossipsub/src/gossipsub/mod.rs b/protocols/gossipsub/src/gossipsub/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/gossipsub/src/gossipsub/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/gossipsub/src/gossipsub/pb.rs b/protocols/gossipsub/src/gossipsub/pb.rs new file mode 100644 index 00000000000..9a074fd61fc --- /dev/null +++ b/protocols/gossipsub/src/gossipsub/pb.rs @@ -0,0 +1,567 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RPC { + pub subscriptions: Vec, + pub publish: Vec, + pub control: Option, +} + +impl<'a> MessageRead<'a> for RPC { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), + Ok(18) => msg.publish.push(r.read_message::(bytes)?), + Ok(26) => msg.control = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for RPC { + fn get_size(&self) -> usize { + 0 + + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.control.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.control { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_RPC { + +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct SubOpts { + pub subscribe: Option, + pub topic_id: Option, +} + +impl<'a> MessageRead<'a> for SubOpts { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), + Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for SubOpts { + fn get_size(&self) -> usize { + 0 + + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } + if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub from: Option>, + pub data: Option>, + pub seqno: Option>, + pub topic: String, + pub signature: Option>, + pub key: Option>, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.topic = r.read_string(bytes)?.to_owned(), + Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), + Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + 1 + sizeof_len((&self.topic).len()) + + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + w.write_with_tag(34, |w| w.write_string(&**&self.topic))?; + if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlMessage { + pub ihave: Vec, + pub iwant: Vec, + pub graft: Vec, + pub prune: Vec, +} + +impl<'a> MessageRead<'a> for ControlMessage { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ihave.push(r.read_message::(bytes)?), + Ok(18) => msg.iwant.push(r.read_message::(bytes)?), + Ok(26) => msg.graft.push(r.read_message::(bytes)?), + Ok(34) => msg.prune.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for ControlMessage { + fn get_size(&self) -> usize { + 0 + + self.ihave.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.iwant.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.graft.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.prune.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.ihave { w.write_with_tag(10, |w| w.write_message(s))?; } + for s in &self.iwant { w.write_with_tag(18, |w| w.write_message(s))?; } + for s in &self.graft { w.write_with_tag(26, |w| w.write_message(s))?; } + for s in &self.prune { w.write_with_tag(34, |w| w.write_message(s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlIHave { + pub topic_id: Option, + pub message_ids: Vec>, +} + +impl<'a> MessageRead<'a> for ControlIHave { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for ControlIHave { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + for s in &self.message_ids { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlIWant { + pub message_ids: Vec>, +} + +impl<'a> MessageRead<'a> for ControlIWant { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for ControlIWant { + fn get_size(&self) -> usize { + 0 + + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.message_ids { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlGraft { + pub topic_id: Option, +} + +impl<'a> MessageRead<'a> for ControlGraft { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for ControlGraft { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct ControlPrune { + pub topic_id: Option, + pub peers: Vec, + pub backoff: Option, +} + +impl<'a> MessageRead<'a> for ControlPrune { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.peers.push(r.read_message::(bytes)?), + Ok(24) => msg.backoff = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for ControlPrune { + fn get_size(&self) -> usize { + 0 + + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.peers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.backoff.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } + for s in &self.peers { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.backoff { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct PeerInfo { + pub peer_id: Option>, + pub signed_peer_record: Option>, +} + +impl<'a> MessageRead<'a> for PeerInfo { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.peer_id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.signed_peer_record = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for PeerInfo { + fn get_size(&self) -> usize { + 0 + + self.peer_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.signed_peer_record.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.peer_id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.signed_peer_record { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct TopicDescriptor { + pub name: Option, + pub auth: Option, + pub enc: Option, +} + +impl<'a> MessageRead<'a> for TopicDescriptor { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.name = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.auth = Some(r.read_message::(bytes)?), + Ok(26) => msg.enc = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for TopicDescriptor { + fn get_size(&self) -> usize { + 0 + + self.name.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.auth.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.enc.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.name { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.auth { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.enc { w.write_with_tag(26, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_TopicDescriptor { + +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct AuthOpts { + pub mode: Option, + pub keys: Vec>, +} + +impl<'a> MessageRead<'a> for AuthOpts { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.mode = Some(r.read_enum(bytes)?), + Ok(18) => msg.keys.push(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for AuthOpts { + fn get_size(&self) -> usize { + 0 + + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.keys.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + for s in &self.keys { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_AuthOpts { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum AuthMode { + NONE = 0, + KEY = 1, + WOT = 2, +} + +impl Default for AuthMode { + fn default() -> Self { + AuthMode::NONE + } +} + +impl From for AuthMode { + fn from(i: i32) -> Self { + match i { + 0 => AuthMode::NONE, + 1 => AuthMode::KEY, + 2 => AuthMode::WOT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for AuthMode { + fn from(s: &'a str) -> Self { + match s { + "NONE" => AuthMode::NONE, + "KEY" => AuthMode::KEY, + "WOT" => AuthMode::WOT, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct EncOpts { + pub mode: Option, + pub key_hashes: Vec>, +} + +impl<'a> MessageRead<'a> for EncOpts { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.mode = Some(r.read_enum(bytes)?), + Ok(18) => msg.key_hashes.push(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for EncOpts { + fn get_size(&self) -> usize { + 0 + + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.key_hashes.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + for s in &self.key_hashes { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_EncOpts { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum EncMode { + NONE = 0, + SHAREDKEY = 1, + WOT = 2, +} + +impl Default for EncMode { + fn default() -> Self { + EncMode::NONE + } +} + +impl From for EncMode { + fn from(i: i32) -> Self { + match i { + 0 => EncMode::NONE, + 1 => EncMode::SHAREDKEY, + 2 => EncMode::WOT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for EncMode { + fn from(s: &'a str) -> Self { + match s { + "NONE" => EncMode::NONE, + "SHAREDKEY" => EncMode::SHAREDKEY, + "WOT" => EncMode::WOT, + _ => Self::default(), + } + } +} + +} + +} + diff --git a/protocols/gossipsub/src/mod.rs b/protocols/gossipsub/src/mod.rs new file mode 100644 index 00000000000..7ac564f3c36 --- /dev/null +++ b/protocols/gossipsub/src/mod.rs @@ -0,0 +1,3 @@ +// Automatically generated mod.rs +pub mod compat; +pub mod gossipsub; diff --git a/protocols/identify/src/generated/structs.rs b/protocols/identify/src/generated/structs.rs index 99ca25c0dbc..3be9b6f94ad 100644 --- a/protocols/identify/src/generated/structs.rs +++ b/protocols/identify/src/generated/structs.rs @@ -9,33 +9,32 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Identify<'a> { - pub protocolVersion: Option>, - pub agentVersion: Option>, - pub publicKey: Option>, - pub listenAddrs: Vec>, - pub observedAddr: Option>, - pub protocols: Vec>, +pub struct Identify { + pub protocolVersion: Option, + pub agentVersion: Option, + pub publicKey: Option>, + pub listenAddrs: Vec>, + pub observedAddr: Option>, + pub protocols: Vec, } -impl<'a> MessageRead<'a> for Identify<'a> { +impl<'a> MessageRead<'a> for Identify { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(42) => msg.protocolVersion = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(50) => msg.agentVersion = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(10) => msg.publicKey = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.listenAddrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(34) => msg.observedAddr = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.protocols.push(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(42) => msg.protocolVersion = Some(r.read_string(bytes)?.to_owned()), + Ok(50) => msg.agentVersion = Some(r.read_string(bytes)?.to_owned()), + Ok(10) => msg.publicKey = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.listenAddrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.observedAddr = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.protocols.push(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -44,7 +43,7 @@ impl<'a> MessageRead<'a> for Identify<'a> { } } -impl<'a> MessageWrite for Identify<'a> { +impl MessageWrite for Identify { fn get_size(&self) -> usize { 0 + self.protocolVersion.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) diff --git a/protocols/identify/src/mod.rs b/protocols/identify/src/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/protocols/identify/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/protocols/identify/src/structs.rs b/protocols/identify/src/structs.rs new file mode 100644 index 00000000000..3be9b6f94ad --- /dev/null +++ b/protocols/identify/src/structs.rs @@ -0,0 +1,67 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Identify { + pub protocolVersion: Option, + pub agentVersion: Option, + pub publicKey: Option>, + pub listenAddrs: Vec>, + pub observedAddr: Option>, + pub protocols: Vec, +} + +impl<'a> MessageRead<'a> for Identify { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(42) => msg.protocolVersion = Some(r.read_string(bytes)?.to_owned()), + Ok(50) => msg.agentVersion = Some(r.read_string(bytes)?.to_owned()), + Ok(10) => msg.publicKey = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.listenAddrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(34) => msg.observedAddr = Some(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.protocols.push(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Identify { + fn get_size(&self) -> usize { + 0 + + self.protocolVersion.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.agentVersion.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.publicKey.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.listenAddrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.observedAddr.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.protocols.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.protocolVersion { w.write_with_tag(42, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.agentVersion { w.write_with_tag(50, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.publicKey { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + for s in &self.listenAddrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.observedAddr { w.write_with_tag(34, |w| w.write_bytes(&**s))?; } + for s in &self.protocols { w.write_with_tag(26, |w| w.write_string(&**s))?; } + Ok(()) + } +} + diff --git a/protocols/kad/src/dht/mod.rs b/protocols/kad/src/dht/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/kad/src/dht/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/kad/src/dht/pb.rs b/protocols/kad/src/dht/pb.rs new file mode 100644 index 00000000000..56b819b1a9c --- /dev/null +++ b/protocols/kad/src/dht/pb.rs @@ -0,0 +1,243 @@ +// Automatically generated rust module for 'dht.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Record { + pub key: Vec, + pub value: Vec, + pub timeReceived: String, + pub publisher: Vec, + pub ttl: u32, +} + +impl<'a> MessageRead<'a> for Record { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.key = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.value = r.read_bytes(bytes)?.to_owned(), + Ok(42) => msg.timeReceived = r.read_string(bytes)?.to_owned(), + Ok(5330) => msg.publisher = r.read_bytes(bytes)?.to_owned(), + Ok(6216) => msg.ttl = r.read_uint32(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Record { + fn get_size(&self) -> usize { + 0 + + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } + + if self.value.is_empty() { 0 } else { 1 + sizeof_len((&self.value).len()) } + + if self.timeReceived == String::default() { 0 } else { 1 + sizeof_len((&self.timeReceived).len()) } + + if self.publisher.is_empty() { 0 } else { 2 + sizeof_len((&self.publisher).len()) } + + if self.ttl == 0u32 { 0 } else { 2 + sizeof_varint(*(&self.ttl) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if !self.key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.key))?; } + if !self.value.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.value))?; } + if self.timeReceived != String::default() { w.write_with_tag(42, |w| w.write_string(&**&self.timeReceived))?; } + if !self.publisher.is_empty() { w.write_with_tag(5330, |w| w.write_bytes(&**&self.publisher))?; } + if self.ttl != 0u32 { w.write_with_tag(6216, |w| w.write_uint32(*&self.ttl))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub type_pb: dht::pb::mod_Message::MessageType, + pub clusterLevelRaw: i32, + pub key: Vec, + pub record: Option, + pub closerPeers: Vec, + pub providerPeers: Vec, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(80) => msg.clusterLevelRaw = r.read_int32(bytes)?, + Ok(18) => msg.key = r.read_bytes(bytes)?.to_owned(), + Ok(26) => msg.record = Some(r.read_message::(bytes)?), + Ok(66) => msg.closerPeers.push(r.read_message::(bytes)?), + Ok(74) => msg.providerPeers.push(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + if self.type_pb == dht::pb::mod_Message::MessageType::PUT_VALUE { 0 } else { 1 + sizeof_varint(*(&self.type_pb) as u64) } + + if self.clusterLevelRaw == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.clusterLevelRaw) as u64) } + + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } + + self.record.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.closerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.providerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.type_pb != dht::pb::mod_Message::MessageType::PUT_VALUE { w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; } + if self.clusterLevelRaw != 0i32 { w.write_with_tag(80, |w| w.write_int32(*&self.clusterLevelRaw))?; } + if !self.key.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.key))?; } + if let Some(ref s) = self.record { w.write_with_tag(26, |w| w.write_message(s))?; } + for s in &self.closerPeers { w.write_with_tag(66, |w| w.write_message(s))?; } + for s in &self.providerPeers { w.write_with_tag(74, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_Message { + +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Peer { + pub id: Vec, + pub addrs: Vec>, + pub connection: dht::pb::mod_Message::ConnectionType, +} + +impl<'a> MessageRead<'a> for Peer { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(24) => msg.connection = r.read_enum(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Peer { + fn get_size(&self) -> usize { + 0 + + if self.id.is_empty() { 0 } else { 1 + sizeof_len((&self.id).len()) } + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + if self.connection == dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { 0 } else { 1 + sizeof_varint(*(&self.connection) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if !self.id.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; } + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if self.connection != dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { w.write_with_tag(24, |w| w.write_enum(*&self.connection as i32))?; } + Ok(()) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MessageType { + PUT_VALUE = 0, + GET_VALUE = 1, + ADD_PROVIDER = 2, + GET_PROVIDERS = 3, + FIND_NODE = 4, + PING = 5, +} + +impl Default for MessageType { + fn default() -> Self { + MessageType::PUT_VALUE + } +} + +impl From for MessageType { + fn from(i: i32) -> Self { + match i { + 0 => MessageType::PUT_VALUE, + 1 => MessageType::GET_VALUE, + 2 => MessageType::ADD_PROVIDER, + 3 => MessageType::GET_PROVIDERS, + 4 => MessageType::FIND_NODE, + 5 => MessageType::PING, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for MessageType { + fn from(s: &'a str) -> Self { + match s { + "PUT_VALUE" => MessageType::PUT_VALUE, + "GET_VALUE" => MessageType::GET_VALUE, + "ADD_PROVIDER" => MessageType::ADD_PROVIDER, + "GET_PROVIDERS" => MessageType::GET_PROVIDERS, + "FIND_NODE" => MessageType::FIND_NODE, + "PING" => MessageType::PING, + _ => Self::default(), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ConnectionType { + NOT_CONNECTED = 0, + CONNECTED = 1, + CAN_CONNECT = 2, + CANNOT_CONNECT = 3, +} + +impl Default for ConnectionType { + fn default() -> Self { + ConnectionType::NOT_CONNECTED + } +} + +impl From for ConnectionType { + fn from(i: i32) -> Self { + match i { + 0 => ConnectionType::NOT_CONNECTED, + 1 => ConnectionType::CONNECTED, + 2 => ConnectionType::CAN_CONNECT, + 3 => ConnectionType::CANNOT_CONNECT, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ConnectionType { + fn from(s: &'a str) -> Self { + match s { + "NOT_CONNECTED" => ConnectionType::NOT_CONNECTED, + "CONNECTED" => ConnectionType::CONNECTED, + "CAN_CONNECT" => ConnectionType::CAN_CONNECT, + "CANNOT_CONNECT" => ConnectionType::CANNOT_CONNECT, + _ => Self::default(), + } + } +} + +} + diff --git a/protocols/kad/src/generated/dht/pb.rs b/protocols/kad/src/generated/dht/pb.rs index 0ec5993a05b..56b819b1a9c 100644 --- a/protocols/kad/src/generated/dht/pb.rs +++ b/protocols/kad/src/generated/dht/pb.rs @@ -9,30 +9,29 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Record<'a> { - pub key: Cow<'a, [u8]>, - pub value: Cow<'a, [u8]>, - pub timeReceived: Cow<'a, str>, - pub publisher: Cow<'a, [u8]>, +pub struct Record { + pub key: Vec, + pub value: Vec, + pub timeReceived: String, + pub publisher: Vec, pub ttl: u32, } -impl<'a> MessageRead<'a> for Record<'a> { +impl<'a> MessageRead<'a> for Record { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.key = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(18) => msg.value = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(42) => msg.timeReceived = r.read_string(bytes).map(Cow::Borrowed)?, - Ok(5330) => msg.publisher = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(10) => msg.key = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.value = r.read_bytes(bytes)?.to_owned(), + Ok(42) => msg.timeReceived = r.read_string(bytes)?.to_owned(), + Ok(5330) => msg.publisher = r.read_bytes(bytes)?.to_owned(), Ok(6216) => msg.ttl = r.read_uint32(bytes)?, Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), @@ -42,21 +41,21 @@ impl<'a> MessageRead<'a> for Record<'a> { } } -impl<'a> MessageWrite for Record<'a> { +impl MessageWrite for Record { fn get_size(&self) -> usize { 0 - + if self.key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.key).len()) } - + if self.value == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.value).len()) } - + if self.timeReceived == "" { 0 } else { 1 + sizeof_len((&self.timeReceived).len()) } - + if self.publisher == Cow::Borrowed(b"") { 0 } else { 2 + sizeof_len((&self.publisher).len()) } + + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } + + if self.value.is_empty() { 0 } else { 1 + sizeof_len((&self.value).len()) } + + if self.timeReceived == String::default() { 0 } else { 1 + sizeof_len((&self.timeReceived).len()) } + + if self.publisher.is_empty() { 0 } else { 2 + sizeof_len((&self.publisher).len()) } + if self.ttl == 0u32 { 0 } else { 2 + sizeof_varint(*(&self.ttl) as u64) } } fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.key != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.key))?; } - if self.value != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.value))?; } - if self.timeReceived != "" { w.write_with_tag(42, |w| w.write_string(&**&self.timeReceived))?; } - if self.publisher != Cow::Borrowed(b"") { w.write_with_tag(5330, |w| w.write_bytes(&**&self.publisher))?; } + if !self.key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.key))?; } + if !self.value.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.value))?; } + if self.timeReceived != String::default() { w.write_with_tag(42, |w| w.write_string(&**&self.timeReceived))?; } + if !self.publisher.is_empty() { w.write_with_tag(5330, |w| w.write_bytes(&**&self.publisher))?; } if self.ttl != 0u32 { w.write_with_tag(6216, |w| w.write_uint32(*&self.ttl))?; } Ok(()) } @@ -64,23 +63,23 @@ impl<'a> MessageWrite for Record<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { +pub struct Message { pub type_pb: dht::pb::mod_Message::MessageType, pub clusterLevelRaw: i32, - pub key: Cow<'a, [u8]>, - pub record: Option>, - pub closerPeers: Vec>, - pub providerPeers: Vec>, + pub key: Vec, + pub record: Option, + pub closerPeers: Vec, + pub providerPeers: Vec, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.type_pb = r.read_enum(bytes)?, Ok(80) => msg.clusterLevelRaw = r.read_int32(bytes)?, - Ok(18) => msg.key = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(18) => msg.key = r.read_bytes(bytes)?.to_owned(), Ok(26) => msg.record = Some(r.read_message::(bytes)?), Ok(66) => msg.closerPeers.push(r.read_message::(bytes)?), Ok(74) => msg.providerPeers.push(r.read_message::(bytes)?), @@ -92,12 +91,12 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + if self.type_pb == dht::pb::mod_Message::MessageType::PUT_VALUE { 0 } else { 1 + sizeof_varint(*(&self.type_pb) as u64) } + if self.clusterLevelRaw == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.clusterLevelRaw) as u64) } - + if self.key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.key).len()) } + + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } + self.record.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + self.closerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + self.providerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() @@ -106,7 +105,7 @@ impl<'a> MessageWrite for Message<'a> { fn write_message(&self, w: &mut Writer) -> Result<()> { if self.type_pb != dht::pb::mod_Message::MessageType::PUT_VALUE { w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; } if self.clusterLevelRaw != 0i32 { w.write_with_tag(80, |w| w.write_int32(*&self.clusterLevelRaw))?; } - if self.key != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.key))?; } + if !self.key.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.key))?; } if let Some(ref s) = self.record { w.write_with_tag(26, |w| w.write_message(s))?; } for s in &self.closerPeers { w.write_with_tag(66, |w| w.write_message(s))?; } for s in &self.providerPeers { w.write_with_tag(74, |w| w.write_message(s))?; } @@ -116,24 +115,23 @@ impl<'a> MessageWrite for Message<'a> { pub mod mod_Message { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Peer<'a> { - pub id: Cow<'a, [u8]>, - pub addrs: Vec>, +pub struct Peer { + pub id: Vec, + pub addrs: Vec>, pub connection: dht::pb::mod_Message::ConnectionType, } -impl<'a> MessageRead<'a> for Peer<'a> { +impl<'a> MessageRead<'a> for Peer { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.id = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), Ok(24) => msg.connection = r.read_enum(bytes)?, Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), @@ -143,16 +141,16 @@ impl<'a> MessageRead<'a> for Peer<'a> { } } -impl<'a> MessageWrite for Peer<'a> { +impl MessageWrite for Peer { fn get_size(&self) -> usize { 0 - + if self.id == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.id).len()) } + + if self.id.is_empty() { 0 } else { 1 + sizeof_len((&self.id).len()) } + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + if self.connection == dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { 0 } else { 1 + sizeof_varint(*(&self.connection) as u64) } } fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.id != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; } + if !self.id.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; } for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } if self.connection != dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { w.write_with_tag(24, |w| w.write_enum(*&self.connection as i32))?; } Ok(()) diff --git a/protocols/kad/src/mod.rs b/protocols/kad/src/mod.rs new file mode 100644 index 00000000000..6ffc566d1a0 --- /dev/null +++ b/protocols/kad/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod dht; diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 57e98c4e17d..650d57d4556 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -28,6 +28,7 @@ smallvec = "1.6.1" static_assertions = "1" thiserror = "1.0" void = "1" +quick-protobuf = "0.8.1" [build-dependencies] prost-build = "0.11" diff --git a/protocols/relay/build.rs b/protocols/relay/build.rs deleted file mode 100644 index 965fb241712..00000000000 --- a/protocols/relay/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/message.proto"], &["src/"]).unwrap(); -} diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 6919626d978..1e509a85ccc 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -23,7 +23,7 @@ mod handler; pub mod rate_limiter; -use crate::message_proto; +use crate::proto; use crate::protocol::{inbound_hop, outbound_stop}; use either::Either; use instant::Instant; @@ -337,7 +337,7 @@ impl NetworkBehaviour for Behaviour { peer_id: event_source, event: Either::Left(handler::In::DenyReservationReq { inbound_reservation_req, - status: message_proto::Status::ResourceLimitExceeded, + status: proto::Status::RESOURCE_LIMIT_EXCEEDED, }), } .into() @@ -452,7 +452,7 @@ impl NetworkBehaviour for Behaviour { event: Either::Left(handler::In::DenyCircuitReq { circuit_id: None, inbound_circuit_req, - status: message_proto::Status::ResourceLimitExceeded, + status: proto::Status::RESOURCE_LIMIT_EXCEEDED, }), } } else if let Some(dst_conn) = self @@ -488,7 +488,7 @@ impl NetworkBehaviour for Behaviour { event: Either::Left(handler::In::DenyCircuitReq { circuit_id: None, inbound_circuit_req, - status: message_proto::Status::NoReservation, + status: proto::Status::NO_RESERVATION, }), } }; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index a1aaf0fa9d7..b7ffeeed4be 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -20,7 +20,7 @@ use crate::behaviour::CircuitId; use crate::copy_future::CopyFuture; -use crate::message_proto::Status; +use crate::proto; use crate::protocol::{inbound_hop, outbound_stop}; use bytes::Bytes; use either::Either; @@ -58,12 +58,12 @@ pub enum In { }, DenyReservationReq { inbound_reservation_req: inbound_hop::ReservationReq, - status: Status, + status: proto::Status, }, DenyCircuitReq { circuit_id: Option, inbound_circuit_req: inbound_hop::CircuitReq, - status: Status, + status: proto::Status, }, NegotiateOutboundConnect { circuit_id: CircuitId, @@ -208,7 +208,7 @@ pub enum Event { src_peer_id: PeerId, src_connection_id: ConnectionId, inbound_circuit_req: inbound_hop::CircuitReq, - status: Status, + status: proto::Status, error: ConnectionHandlerUpgrErr, }, /// An inbound circuit has closed. @@ -547,12 +547,14 @@ impl Handler { >, ) { let (non_fatal_error, status) = match error { - ConnectionHandlerUpgrErr::Timeout => { - (ConnectionHandlerUpgrErr::Timeout, Status::ConnectionFailed) - } - ConnectionHandlerUpgrErr::Timer => { - (ConnectionHandlerUpgrErr::Timer, Status::ConnectionFailed) - } + ConnectionHandlerUpgrErr::Timeout => ( + ConnectionHandlerUpgrErr::Timeout, + proto::Status::CONNECTION_FAILED, + ), + ConnectionHandlerUpgrErr::Timer => ( + ConnectionHandlerUpgrErr::Timer, + proto::Status::CONNECTION_FAILED, + ), ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select( upgrade::NegotiationError::Failed, )) => { @@ -581,10 +583,10 @@ impl Handler { outbound_stop::UpgradeError::CircuitFailed(error) => { let status = match error { outbound_stop::CircuitFailedReason::ResourceLimitExceeded => { - Status::ResourceLimitExceeded + proto::Status::RESOURCE_LIMIT_EXCEEDED } outbound_stop::CircuitFailedReason::PermissionDenied => { - Status::PermissionDenied + proto::Status::PERMISSION_DENIED } }; ( diff --git a/protocols/relay/src/generated/message_v2/pb.rs b/protocols/relay/src/generated/message_v2/pb.rs index ef10b27329f..2344f26b541 100644 --- a/protocols/relay/src/generated/message_v2/pb.rs +++ b/protocols/relay/src/generated/message_v2/pb.rs @@ -9,7 +9,6 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; @@ -66,15 +65,15 @@ impl<'a> From<&'a str> for Status { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct HopMessage<'a> { +pub struct HopMessage { pub type_pb: message_v2::pb::mod_HopMessage::Type, - pub peer: Option>, - pub reservation: Option>, + pub peer: Option, + pub reservation: Option, pub limit: Option, pub status: Option, } -impl<'a> MessageRead<'a> for HopMessage<'a> { +impl<'a> MessageRead<'a> for HopMessage { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -92,7 +91,7 @@ impl<'a> MessageRead<'a> for HopMessage<'a> { } } -impl<'a> MessageWrite for HopMessage<'a> { +impl MessageWrite for HopMessage { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.type_pb) as u64) @@ -154,14 +153,14 @@ impl<'a> From<&'a str> for Type { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct StopMessage<'a> { +pub struct StopMessage { pub type_pb: message_v2::pb::mod_StopMessage::Type, - pub peer: Option>, + pub peer: Option, pub limit: Option, pub status: Option, } -impl<'a> MessageRead<'a> for StopMessage<'a> { +impl<'a> MessageRead<'a> for StopMessage { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -178,7 +177,7 @@ impl<'a> MessageRead<'a> for StopMessage<'a> { } } -impl<'a> MessageWrite for StopMessage<'a> { +impl MessageWrite for StopMessage { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.type_pb) as u64) @@ -235,18 +234,18 @@ impl<'a> From<&'a str> for Type { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Peer<'a> { - pub id: Cow<'a, [u8]>, - pub addrs: Vec>, +pub struct Peer { + pub id: Vec, + pub addrs: Vec>, } -impl<'a> MessageRead<'a> for Peer<'a> { +impl<'a> MessageRead<'a> for Peer { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.id = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -255,7 +254,7 @@ impl<'a> MessageRead<'a> for Peer<'a> { } } -impl<'a> MessageWrite for Peer<'a> { +impl MessageWrite for Peer { fn get_size(&self) -> usize { 0 + 1 + sizeof_len((&self.id).len()) @@ -271,20 +270,20 @@ impl<'a> MessageWrite for Peer<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Reservation<'a> { +pub struct Reservation { pub expire: u64, - pub addrs: Vec>, - pub voucher: Option>, + pub addrs: Vec>, + pub voucher: Option>, } -impl<'a> MessageRead<'a> for Reservation<'a> { +impl<'a> MessageRead<'a> for Reservation { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.expire = r.read_uint64(bytes)?, - Ok(18) => msg.addrs.push(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(26) => msg.voucher = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.voucher = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -293,7 +292,7 @@ impl<'a> MessageRead<'a> for Reservation<'a> { } } -impl<'a> MessageWrite for Reservation<'a> { +impl MessageWrite for Reservation { fn get_size(&self) -> usize { 0 + 1 + sizeof_varint(*(&self.expire) as u64) diff --git a/protocols/relay/src/lib.rs b/protocols/relay/src/lib.rs index aa5a82043f5..db75454087a 100644 --- a/protocols/relay/src/lib.rs +++ b/protocols/relay/src/lib.rs @@ -29,9 +29,11 @@ mod priv_client; mod protocol; pub mod v2; -#[allow(clippy::derive_partial_eq_without_eq)] -mod message_proto { - include!(concat!(env!("OUT_DIR"), "/message_v2.pb.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::message_v2::pb::mod_HopMessage::Type as HopMessageType; + pub use self::message_v2::pb::mod_StopMessage::Type as StopMessageType; + pub use self::message_v2::pb::{HopMessage, Limit, Peer, Reservation, Status, StopMessage}; } pub use behaviour::{Behaviour, CircuitId, Config, Event}; diff --git a/protocols/relay/src/message_v2/mod.rs b/protocols/relay/src/message_v2/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/relay/src/message_v2/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/relay/src/message_v2/pb.rs b/protocols/relay/src/message_v2/pb.rs new file mode 100644 index 00000000000..2344f26b541 --- /dev/null +++ b/protocols/relay/src/message_v2/pb.rs @@ -0,0 +1,346 @@ +// Automatically generated rust module for 'message.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Status { + OK = 100, + RESERVATION_REFUSED = 200, + RESOURCE_LIMIT_EXCEEDED = 201, + PERMISSION_DENIED = 202, + CONNECTION_FAILED = 203, + NO_RESERVATION = 204, + MALFORMED_MESSAGE = 400, + UNEXPECTED_MESSAGE = 401, +} + +impl Default for Status { + fn default() -> Self { + Status::OK + } +} + +impl From for Status { + fn from(i: i32) -> Self { + match i { + 100 => Status::OK, + 200 => Status::RESERVATION_REFUSED, + 201 => Status::RESOURCE_LIMIT_EXCEEDED, + 202 => Status::PERMISSION_DENIED, + 203 => Status::CONNECTION_FAILED, + 204 => Status::NO_RESERVATION, + 400 => Status::MALFORMED_MESSAGE, + 401 => Status::UNEXPECTED_MESSAGE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Status { + fn from(s: &'a str) -> Self { + match s { + "OK" => Status::OK, + "RESERVATION_REFUSED" => Status::RESERVATION_REFUSED, + "RESOURCE_LIMIT_EXCEEDED" => Status::RESOURCE_LIMIT_EXCEEDED, + "PERMISSION_DENIED" => Status::PERMISSION_DENIED, + "CONNECTION_FAILED" => Status::CONNECTION_FAILED, + "NO_RESERVATION" => Status::NO_RESERVATION, + "MALFORMED_MESSAGE" => Status::MALFORMED_MESSAGE, + "UNEXPECTED_MESSAGE" => Status::UNEXPECTED_MESSAGE, + _ => Self::default(), + } + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct HopMessage { + pub type_pb: message_v2::pb::mod_HopMessage::Type, + pub peer: Option, + pub reservation: Option, + pub limit: Option, + pub status: Option, +} + +impl<'a> MessageRead<'a> for HopMessage { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(18) => msg.peer = Some(r.read_message::(bytes)?), + Ok(26) => msg.reservation = Some(r.read_message::(bytes)?), + Ok(34) => msg.limit = Some(r.read_message::(bytes)?), + Ok(40) => msg.status = Some(r.read_enum(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for HopMessage { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.type_pb) as u64) + + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.reservation.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; + if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.reservation { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(34, |w| w.write_message(s))?; } + if let Some(ref s) = self.status { w.write_with_tag(40, |w| w.write_enum(*s as i32))?; } + Ok(()) + } +} + +pub mod mod_HopMessage { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Type { + RESERVE = 0, + CONNECT = 1, + STATUS = 2, +} + +impl Default for Type { + fn default() -> Self { + Type::RESERVE + } +} + +impl From for Type { + fn from(i: i32) -> Self { + match i { + 0 => Type::RESERVE, + 1 => Type::CONNECT, + 2 => Type::STATUS, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Type { + fn from(s: &'a str) -> Self { + match s { + "RESERVE" => Type::RESERVE, + "CONNECT" => Type::CONNECT, + "STATUS" => Type::STATUS, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct StopMessage { + pub type_pb: message_v2::pb::mod_StopMessage::Type, + pub peer: Option, + pub limit: Option, + pub status: Option, +} + +impl<'a> MessageRead<'a> for StopMessage { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = r.read_enum(bytes)?, + Ok(18) => msg.peer = Some(r.read_message::(bytes)?), + Ok(26) => msg.limit = Some(r.read_message::(bytes)?), + Ok(32) => msg.status = Some(r.read_enum(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for StopMessage { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.type_pb) as u64) + + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; + if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.status { w.write_with_tag(32, |w| w.write_enum(*s as i32))?; } + Ok(()) + } +} + +pub mod mod_StopMessage { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Type { + CONNECT = 0, + STATUS = 1, +} + +impl Default for Type { + fn default() -> Self { + Type::CONNECT + } +} + +impl From for Type { + fn from(i: i32) -> Self { + match i { + 0 => Type::CONNECT, + 1 => Type::STATUS, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Type { + fn from(s: &'a str) -> Self { + match s { + "CONNECT" => Type::CONNECT, + "STATUS" => Type::STATUS, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Peer { + pub id: Vec, + pub addrs: Vec>, +} + +impl<'a> MessageRead<'a> for Peer { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Peer { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_len((&self.id).len()) + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Reservation { + pub expire: u64, + pub addrs: Vec>, + pub voucher: Option>, +} + +impl<'a> MessageRead<'a> for Reservation { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.expire = r.read_uint64(bytes)?, + Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(26) => msg.voucher = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Reservation { + fn get_size(&self) -> usize { + 0 + + 1 + sizeof_varint(*(&self.expire) as u64) + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + self.voucher.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + w.write_with_tag(8, |w| w.write_uint64(*&self.expire))?; + for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.voucher { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Limit { + pub duration: Option, + pub data: Option, +} + +impl<'a> MessageRead<'a> for Limit { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.duration = Some(r.read_uint32(bytes)?), + Ok(16) => msg.data = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Limit { + fn get_size(&self) -> usize { + 0 + + self.duration.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.data.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.duration { w.write_with_tag(8, |w| w.write_uint32(*s))?; } + if let Some(ref s) = self.data { w.write_with_tag(16, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + diff --git a/protocols/relay/src/mod.rs b/protocols/relay/src/mod.rs new file mode 100644 index 00000000000..18bb20c7eb0 --- /dev/null +++ b/protocols/relay/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod message_v2; diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 9bf09e3d178..da5e08ec4ff 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -18,8 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::Status; use crate::priv_client::transport; +use crate::proto; use crate::protocol::{self, inbound_stop, outbound_hop}; use either::Either; use futures::channel::{mpsc, oneshot}; @@ -268,7 +268,7 @@ impl Handler { .circuit_deny_futs .insert( src_peer_id, - inbound_circuit.deny(Status::NoReservation).boxed(), + inbound_circuit.deny(proto::Status::NO_RESERVATION).boxed(), ) .is_some() { diff --git a/protocols/relay/src/protocol.rs b/protocols/relay/src/protocol.rs index f688fe6ab67..4933eb6a523 100644 --- a/protocols/relay/src/protocol.rs +++ b/protocols/relay/src/protocol.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto; +use crate::proto; use std::time::Duration; pub mod inbound_hop; @@ -37,8 +37,8 @@ pub struct Limit { data_in_bytes: Option, } -impl From for Limit { - fn from(limit: message_proto::Limit) -> Self { +impl From for Limit { + fn from(limit: proto::Limit) -> Self { Limit { duration: limit.duration.map(|d| Duration::from_secs(d.into())), data_in_bytes: limit.data, diff --git a/protocols/relay/src/protocol/inbound_hop.rs b/protocols/relay/src/protocol/inbound_hop.rs index 23e376fb11d..90513f05bd4 100644 --- a/protocols/relay/src/protocol/inbound_hop.rs +++ b/protocols/relay/src/protocol/inbound_hop.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{hop_message, HopMessage, Limit, Reservation, Status}; +use crate::proto; use crate::protocol::{HOP_PROTOCOL_NAME, MAX_MESSAGE_SIZE}; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; @@ -51,11 +51,14 @@ impl upgrade::InboundUpgrade for Upgrade { type Future = BoxFuture<'static, Result>; fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { - let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new( + substream, + quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE), + ); async move { - let HopMessage { - r#type, + let proto::HopMessage { + type_pb, peer, reservation: _, limit: _, @@ -65,21 +68,19 @@ impl upgrade::InboundUpgrade for Upgrade { .await .ok_or(FatalUpgradeError::StreamClosed)??; - let r#type = - hop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?; - let req = match r#type { - hop_message::Type::Reserve => Req::Reserve(ReservationReq { + let req = match type_pb { + proto::HopMessageType::RESERVE => Req::Reserve(ReservationReq { substream, reservation_duration: self.reservation_duration, max_circuit_duration: self.max_circuit_duration, max_circuit_bytes: self.max_circuit_bytes, }), - hop_message::Type::Connect => { + proto::HopMessageType::CONNECT => { let dst = PeerId::from_bytes(&peer.ok_or(FatalUpgradeError::MissingPeer)?.id) .map_err(|_| FatalUpgradeError::ParsePeerId)?; Req::Connect(CircuitReq { dst, substream }) } - hop_message::Type::Status => { + proto::HopMessageType::STATUS => { return Err(FatalUpgradeError::UnexpectedTypeStatus.into()) } }; @@ -124,7 +125,7 @@ pub enum Req { } pub struct ReservationReq { - substream: Framed>, + substream: Framed>, reservation_duration: Duration, max_circuit_duration: Duration, max_circuit_bytes: u64, @@ -132,10 +133,10 @@ pub struct ReservationReq { impl ReservationReq { pub async fn accept(self, addrs: Vec) -> Result<(), UpgradeError> { - let msg = HopMessage { - r#type: hop_message::Type::Status.into(), + let msg = proto::HopMessage { + type_pb: proto::HopMessageType::STATUS, peer: None, - reservation: Some(Reservation { + reservation: Some(proto::Reservation { addrs: addrs.into_iter().map(|a| a.to_vec()).collect(), expire: (SystemTime::now() + self.reservation_duration) .duration_since(SystemTime::UNIX_EPOCH) @@ -143,7 +144,7 @@ impl ReservationReq { .as_secs(), voucher: None, }), - limit: Some(Limit { + limit: Some(proto::Limit { duration: Some( self.max_circuit_duration .as_secs() @@ -152,25 +153,25 @@ impl ReservationReq { ), data: Some(self.max_circuit_bytes), }), - status: Some(Status::Ok.into()), + status: Some(proto::Status::OK), }; self.send(msg).await } - pub async fn deny(self, status: Status) -> Result<(), UpgradeError> { - let msg = HopMessage { - r#type: hop_message::Type::Status.into(), + pub async fn deny(self, status: proto::Status) -> Result<(), UpgradeError> { + let msg = proto::HopMessage { + type_pb: proto::HopMessageType::STATUS, peer: None, reservation: None, limit: None, - status: Some(status.into()), + status: Some(status), }; self.send(msg).await } - async fn send(mut self, msg: HopMessage) -> Result<(), UpgradeError> { + async fn send(mut self, msg: proto::HopMessage) -> Result<(), UpgradeError> { self.substream.send(msg).await?; self.substream.flush().await?; self.substream.close().await?; @@ -181,7 +182,7 @@ impl ReservationReq { pub struct CircuitReq { dst: PeerId, - substream: Framed>, + substream: Framed>, } impl CircuitReq { @@ -190,12 +191,12 @@ impl CircuitReq { } pub async fn accept(mut self) -> Result<(NegotiatedSubstream, Bytes), UpgradeError> { - let msg = HopMessage { - r#type: hop_message::Type::Status.into(), + let msg = proto::HopMessage { + type_pb: proto::HopMessageType::STATUS, peer: None, reservation: None, limit: None, - status: Some(Status::Ok.into()), + status: Some(proto::Status::OK), }; self.send(msg).await?; @@ -214,19 +215,22 @@ impl CircuitReq { Ok((io, read_buffer.freeze())) } - pub async fn deny(mut self, status: Status) -> Result<(), UpgradeError> { - let msg = HopMessage { - r#type: hop_message::Type::Status.into(), + pub async fn deny(mut self, status: proto::Status) -> Result<(), UpgradeError> { + let msg = proto::HopMessage { + type_pb: proto::HopMessageType::STATUS, peer: None, reservation: None, limit: None, - status: Some(status.into()), + status: Some(status), }; self.send(msg).await?; self.substream.close().await.map_err(Into::into) } - async fn send(&mut self, msg: HopMessage) -> Result<(), quick_protobuf_codec::Error> { + async fn send( + &mut self, + msg: proto::HopMessage, + ) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; diff --git a/protocols/relay/src/protocol/inbound_stop.rs b/protocols/relay/src/protocol/inbound_stop.rs index 30e3d57de4b..86a549cd882 100644 --- a/protocols/relay/src/protocol/inbound_stop.rs +++ b/protocols/relay/src/protocol/inbound_stop.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{stop_message, Status, StopMessage}; +use crate::proto; use crate::protocol::{self, MAX_MESSAGE_SIZE, STOP_PROTOCOL_NAME}; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; @@ -45,11 +45,14 @@ impl upgrade::InboundUpgrade for Upgrade { type Future = BoxFuture<'static, Result>; fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { - let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new( + substream, + quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE), + ); async move { - let StopMessage { - r#type, + let proto::StopMessage { + type_pb, peer, limit, status: _, @@ -58,10 +61,8 @@ impl upgrade::InboundUpgrade for Upgrade { .await .ok_or(FatalUpgradeError::StreamClosed)??; - let r#type = - stop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?; - match r#type { - stop_message::Type::Connect => { + match type_pb { + proto::StopMessageType::CONNECT => { let src_peer_id = PeerId::from_bytes(&peer.ok_or(FatalUpgradeError::MissingPeer)?.id) .map_err(|_| FatalUpgradeError::ParsePeerId)?; @@ -71,7 +72,9 @@ impl upgrade::InboundUpgrade for Upgrade { limit: limit.map(Into::into), }) } - stop_message::Type::Status => Err(FatalUpgradeError::UnexpectedTypeStatus.into()), + proto::StopMessageType::STATUS => { + Err(FatalUpgradeError::UnexpectedTypeStatus.into()) + } } } .boxed() @@ -107,7 +110,8 @@ pub enum FatalUpgradeError { } pub struct Circuit { - substream: Framed>, + substream: + Framed>, src_peer_id: PeerId, limit: Option, } @@ -122,11 +126,11 @@ impl Circuit { } pub async fn accept(mut self) -> Result<(NegotiatedSubstream, Bytes), UpgradeError> { - let msg = StopMessage { - r#type: stop_message::Type::Status.into(), + let msg = proto::StopMessage { + type_pb: proto::StopMessageType::STATUS, peer: None, limit: None, - status: Some(Status::Ok.into()), + status: Some(proto::Status::OK), }; self.send(msg).await?; @@ -145,18 +149,21 @@ impl Circuit { Ok((io, read_buffer.freeze())) } - pub async fn deny(mut self, status: Status) -> Result<(), UpgradeError> { - let msg = StopMessage { - r#type: stop_message::Type::Status.into(), + pub async fn deny(mut self, status: proto::Status) -> Result<(), UpgradeError> { + let msg = proto::StopMessage { + type_pb: proto::StopMessageType::STATUS, peer: None, limit: None, - status: Some(status.into()), + status: Some(status), }; self.send(msg).await.map_err(Into::into) } - async fn send(&mut self, msg: StopMessage) -> Result<(), quick_protobuf_codec::Error> { + async fn send( + &mut self, + msg: proto::StopMessage, + ) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; diff --git a/protocols/relay/src/protocol/outbound_hop.rs b/protocols/relay/src/protocol/outbound_hop.rs index 229be977a52..e1ddda99738 100644 --- a/protocols/relay/src/protocol/outbound_hop.rs +++ b/protocols/relay/src/protocol/outbound_hop.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{hop_message, HopMessage, Peer, Status}; +use crate::proto; use crate::protocol::{Limit, HOP_PROTOCOL_NAME, MAX_MESSAGE_SIZE}; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; @@ -52,16 +52,16 @@ impl upgrade::OutboundUpgrade for Upgrade { fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { let msg = match self { - Upgrade::Reserve => HopMessage { - r#type: hop_message::Type::Reserve.into(), + Upgrade::Reserve => proto::HopMessage { + type_pb: proto::HopMessageType::RESERVE, peer: None, reservation: None, limit: None, status: None, }, - Upgrade::Connect { dst_peer_id } => HopMessage { - r#type: hop_message::Type::Connect.into(), - peer: Some(Peer { + Upgrade::Connect { dst_peer_id } => proto::HopMessage { + type_pb: proto::HopMessageType::CONNECT, + peer: Some(proto::Peer { id: dst_peer_id.to_bytes(), addrs: vec![], }), @@ -71,12 +71,15 @@ impl upgrade::OutboundUpgrade for Upgrade { }, }; - let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new( + substream, + quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE), + ); async move { substream.send(msg).await?; - let HopMessage { - r#type, + let proto::HopMessage { + type_pb, peer: _, reservation, limit, @@ -86,31 +89,28 @@ impl upgrade::OutboundUpgrade for Upgrade { .await .ok_or(FatalUpgradeError::StreamClosed)??; - let r#type = - hop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?; - match r#type { - hop_message::Type::Connect => { + match type_pb { + proto::HopMessageType::CONNECT => { return Err(FatalUpgradeError::UnexpectedTypeConnect.into()) } - hop_message::Type::Reserve => { + proto::HopMessageType::RESERVE => { return Err(FatalUpgradeError::UnexpectedTypeReserve.into()) } - hop_message::Type::Status => {} + proto::HopMessageType::STATUS => {} } - let status = Status::from_i32(status.ok_or(FatalUpgradeError::MissingStatusField)?) - .ok_or(FatalUpgradeError::ParseStatusField)?; - let limit = limit.map(Into::into); let output = match self { Upgrade::Reserve => { - match status { - Status::Ok => {} - Status::ReservationRefused => { + match status + .ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))? + { + proto::Status::OK => {} + proto::Status::RESERVATION_REFUSED => { return Err(ReservationFailedReason::Refused.into()) } - Status::ResourceLimitExceeded => { + proto::Status::RESOURCE_LIMIT_EXCEEDED => { return Err(ReservationFailedReason::ResourceLimitExceeded.into()) } s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()), @@ -126,7 +126,7 @@ impl upgrade::OutboundUpgrade for Upgrade { let addrs = reservation .addrs .into_iter() - .map(TryFrom::try_from) + .map(|b| Multiaddr::try_from(b.to_vec())) .collect::, _>>() .map_err(|_| FatalUpgradeError::InvalidReservationAddrs)?; @@ -153,18 +153,20 @@ impl upgrade::OutboundUpgrade for Upgrade { } } Upgrade::Connect { .. } => { - match status { - Status::Ok => {} - Status::ResourceLimitExceeded => { + match status + .ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))? + { + proto::Status::OK => {} + proto::Status::RESOURCE_LIMIT_EXCEEDED => { return Err(CircuitFailedReason::ResourceLimitExceeded.into()) } - Status::ConnectionFailed => { + proto::Status::CONNECTION_FAILED => { return Err(CircuitFailedReason::ConnectionFailed.into()) } - Status::NoReservation => { + proto::Status::NO_RESERVATION => { return Err(CircuitFailedReason::NoReservation.into()) } - Status::PermissionDenied => { + proto::Status::PERMISSION_DENIED => { return Err(CircuitFailedReason::PermissionDenied.into()) } s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()), @@ -256,7 +258,7 @@ pub enum FatalUpgradeError { #[error("Failed to parse response type field.")] ParseStatusField, #[error("Unexpected message status '{0:?}'")] - UnexpectedStatus(Status), + UnexpectedStatus(proto::Status), } pub enum Output { diff --git a/protocols/relay/src/protocol/outbound_stop.rs b/protocols/relay/src/protocol/outbound_stop.rs index e42b8eefe74..5302fed65a8 100644 --- a/protocols/relay/src/protocol/outbound_stop.rs +++ b/protocols/relay/src/protocol/outbound_stop.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::message_proto::{stop_message, Limit, Peer, Status, StopMessage}; +use crate::proto; use crate::protocol::{MAX_MESSAGE_SIZE, STOP_PROTOCOL_NAME}; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; @@ -51,13 +51,13 @@ impl upgrade::OutboundUpgrade for Upgrade { type Future = BoxFuture<'static, Result>; fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future { - let msg = StopMessage { - r#type: stop_message::Type::Connect.into(), - peer: Some(Peer { + let msg = proto::StopMessage { + type_pb: proto::StopMessageType::CONNECT, + peer: Some(proto::Peer { id: self.relay_peer_id.to_bytes(), addrs: vec![], }), - limit: Some(Limit { + limit: Some(proto::Limit { duration: Some( self.max_circuit_duration .as_secs() @@ -69,12 +69,15 @@ impl upgrade::OutboundUpgrade for Upgrade { status: None, }; - let mut substream = Framed::new(substream, quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE)); + let mut substream = Framed::new( + substream, + quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE), + ); async move { substream.send(msg).await?; - let StopMessage { - r#type, + let proto::StopMessage { + type_pb, peer: _, limit: _, status, @@ -83,23 +86,19 @@ impl upgrade::OutboundUpgrade for Upgrade { .await .ok_or(FatalUpgradeError::StreamClosed)??; - let r#type = - stop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?; - match r#type { - stop_message::Type::Connect => { + match type_pb { + proto::StopMessageType::CONNECT => { return Err(FatalUpgradeError::UnexpectedTypeConnect.into()) } - stop_message::Type::Status => {} + proto::StopMessageType::STATUS => {} } - let status = Status::from_i32(status.ok_or(FatalUpgradeError::MissingStatusField)?) - .ok_or(FatalUpgradeError::ParseStatusField)?; - match status { - Status::Ok => {} - Status::ResourceLimitExceeded => { + match status.ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))? { + proto::Status::OK => {} + proto::Status::RESOURCE_LIMIT_EXCEEDED => { return Err(CircuitFailedReason::ResourceLimitExceeded.into()) } - Status::PermissionDenied => { + proto::Status::PERMISSION_DENIED => { return Err(CircuitFailedReason::PermissionDenied.into()) } s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()), @@ -159,5 +158,5 @@ pub enum FatalUpgradeError { #[error("Failed to parse response type field.")] ParseStatusField, #[error("Unexpected message status '{0:?}'")] - UnexpectedStatus(Status), + UnexpectedStatus(proto::Status), } diff --git a/protocols/rendezvous/src/generated/rendezvous/pb.rs b/protocols/rendezvous/src/generated/rendezvous/pb.rs index 45a84862e1a..2419ecf6a37 100644 --- a/protocols/rendezvous/src/generated/rendezvous/pb.rs +++ b/protocols/rendezvous/src/generated/rendezvous/pb.rs @@ -15,16 +15,16 @@ use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { +pub struct Message { pub type_pb: Option, - pub register: Option>, - pub registerResponse: Option>, - pub unregister: Option>, - pub discover: Option>, - pub discoverResponse: Option>, + pub register: Option, + pub registerResponse: Option, + pub unregister: Option, + pub discover: Option, + pub discoverResponse: Option, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { @@ -43,7 +43,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -67,24 +67,23 @@ impl<'a> MessageWrite for Message<'a> { pub mod mod_Message { -use std::borrow::Cow; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Register<'a> { - pub ns: Option>, - pub signedPeerRecord: Option>, +pub struct Register { + pub ns: Option, + pub signedPeerRecord: Option>, pub ttl: Option, } -impl<'a> MessageRead<'a> for Register<'a> { +impl<'a> MessageRead<'a> for Register { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.signedPeerRecord = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.signedPeerRecord = Some(r.read_bytes(bytes)?.to_owned()), Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), @@ -94,7 +93,7 @@ impl<'a> MessageRead<'a> for Register<'a> { } } -impl<'a> MessageWrite for Register<'a> { +impl MessageWrite for Register { fn get_size(&self) -> usize { 0 + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -112,19 +111,19 @@ impl<'a> MessageWrite for Register<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct RegisterResponse<'a> { +pub struct RegisterResponse { pub status: Option, - pub statusText: Option>, + pub statusText: Option, pub ttl: Option, } -impl<'a> MessageRead<'a> for RegisterResponse<'a> { +impl<'a> MessageRead<'a> for RegisterResponse { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.status = Some(r.read_enum(bytes)?), - Ok(18) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), @@ -134,7 +133,7 @@ impl<'a> MessageRead<'a> for RegisterResponse<'a> { } } -impl<'a> MessageWrite for RegisterResponse<'a> { +impl MessageWrite for RegisterResponse { fn get_size(&self) -> usize { 0 + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) @@ -152,18 +151,18 @@ impl<'a> MessageWrite for RegisterResponse<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Unregister<'a> { - pub ns: Option>, - pub id: Option>, +pub struct Unregister { + pub ns: Option, + pub id: Option>, } -impl<'a> MessageRead<'a> for Unregister<'a> { +impl<'a> MessageRead<'a> for Unregister { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -172,7 +171,7 @@ impl<'a> MessageRead<'a> for Unregister<'a> { } } -impl<'a> MessageWrite for Unregister<'a> { +impl MessageWrite for Unregister { fn get_size(&self) -> usize { 0 + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -188,20 +187,20 @@ impl<'a> MessageWrite for Unregister<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Discover<'a> { - pub ns: Option>, +pub struct Discover { + pub ns: Option, pub limit: Option, - pub cookie: Option>, + pub cookie: Option>, } -impl<'a> MessageRead<'a> for Discover<'a> { +impl<'a> MessageRead<'a> for Discover { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), Ok(16) => msg.limit = Some(r.read_uint64(bytes)?), - Ok(26) => msg.cookie = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(26) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -210,7 +209,7 @@ impl<'a> MessageRead<'a> for Discover<'a> { } } -impl<'a> MessageWrite for Discover<'a> { +impl MessageWrite for Discover { fn get_size(&self) -> usize { 0 + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) @@ -228,22 +227,22 @@ impl<'a> MessageWrite for Discover<'a> { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct DiscoverResponse<'a> { - pub registrations: Vec>, - pub cookie: Option>, +pub struct DiscoverResponse { + pub registrations: Vec, + pub cookie: Option>, pub status: Option, - pub statusText: Option>, + pub statusText: Option, } -impl<'a> MessageRead<'a> for DiscoverResponse<'a> { +impl<'a> MessageRead<'a> for DiscoverResponse { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(10) => msg.registrations.push(r.read_message::(bytes)?), - Ok(18) => msg.cookie = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), Ok(24) => msg.status = Some(r.read_enum(bytes)?), - Ok(34) => msg.statusText = Some(r.read_string(bytes).map(Cow::Borrowed)?), + Ok(34) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -252,7 +251,7 @@ impl<'a> MessageRead<'a> for DiscoverResponse<'a> { } } -impl<'a> MessageWrite for DiscoverResponse<'a> { +impl MessageWrite for DiscoverResponse { fn get_size(&self) -> usize { 0 + self.registrations.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() diff --git a/protocols/rendezvous/src/mod.rs b/protocols/rendezvous/src/mod.rs new file mode 100644 index 00000000000..448a0207b2d --- /dev/null +++ b/protocols/rendezvous/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod rendezvous; diff --git a/protocols/rendezvous/src/rendezvous/mod.rs b/protocols/rendezvous/src/rendezvous/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/protocols/rendezvous/src/rendezvous/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/protocols/rendezvous/src/rendezvous/pb.rs b/protocols/rendezvous/src/rendezvous/pb.rs new file mode 100644 index 00000000000..2419ecf6a37 --- /dev/null +++ b/protocols/rendezvous/src/rendezvous/pb.rs @@ -0,0 +1,364 @@ +// Automatically generated rust module for 'rpc.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub type_pb: Option, + pub register: Option, + pub registerResponse: Option, + pub unregister: Option, + pub discover: Option, + pub discoverResponse: Option, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.type_pb = Some(r.read_enum(bytes)?), + Ok(18) => msg.register = Some(r.read_message::(bytes)?), + Ok(26) => msg.registerResponse = Some(r.read_message::(bytes)?), + Ok(34) => msg.unregister = Some(r.read_message::(bytes)?), + Ok(42) => msg.discover = Some(r.read_message::(bytes)?), + Ok(50) => msg.discoverResponse = Some(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.register.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.registerResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.unregister.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.discover.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + + self.discoverResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.type_pb { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.register { w.write_with_tag(18, |w| w.write_message(s))?; } + if let Some(ref s) = self.registerResponse { w.write_with_tag(26, |w| w.write_message(s))?; } + if let Some(ref s) = self.unregister { w.write_with_tag(34, |w| w.write_message(s))?; } + if let Some(ref s) = self.discover { w.write_with_tag(42, |w| w.write_message(s))?; } + if let Some(ref s) = self.discoverResponse { w.write_with_tag(50, |w| w.write_message(s))?; } + Ok(()) + } +} + +pub mod mod_Message { + +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Register { + pub ns: Option, + pub signedPeerRecord: Option>, + pub ttl: Option, +} + +impl<'a> MessageRead<'a> for Register { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.signedPeerRecord = Some(r.read_bytes(bytes)?.to_owned()), + Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Register { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.signedPeerRecord.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.signedPeerRecord { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct RegisterResponse { + pub status: Option, + pub statusText: Option, + pub ttl: Option, +} + +impl<'a> MessageRead<'a> for RegisterResponse { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.status = Some(r.read_enum(bytes)?), + Ok(18) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), + Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for RegisterResponse { + fn get_size(&self) -> usize { + 0 + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.status { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.statusText { w.write_with_tag(18, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Unregister { + pub ns: Option, + pub id: Option>, +} + +impl<'a> MessageRead<'a> for Unregister { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), + Ok(18) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Unregister { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.id { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Discover { + pub ns: Option, + pub limit: Option, + pub cookie: Option>, +} + +impl<'a> MessageRead<'a> for Discover { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), + Ok(16) => msg.limit = Some(r.read_uint64(bytes)?), + Ok(26) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Discover { + fn get_size(&self) -> usize { + 0 + + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.limit.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } + if let Some(ref s) = self.limit { w.write_with_tag(16, |w| w.write_uint64(*s))?; } + if let Some(ref s) = self.cookie { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DiscoverResponse { + pub registrations: Vec, + pub cookie: Option>, + pub status: Option, + pub statusText: Option, +} + +impl<'a> MessageRead<'a> for DiscoverResponse { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.registrations.push(r.read_message::(bytes)?), + Ok(18) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), + Ok(24) => msg.status = Some(r.read_enum(bytes)?), + Ok(34) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DiscoverResponse { + fn get_size(&self) -> usize { + 0 + + self.registrations.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() + + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.registrations { w.write_with_tag(10, |w| w.write_message(s))?; } + if let Some(ref s) = self.cookie { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.status { w.write_with_tag(24, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.statusText { w.write_with_tag(34, |w| w.write_string(&**s))?; } + Ok(()) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MessageType { + REGISTER = 0, + REGISTER_RESPONSE = 1, + UNREGISTER = 2, + DISCOVER = 3, + DISCOVER_RESPONSE = 4, +} + +impl Default for MessageType { + fn default() -> Self { + MessageType::REGISTER + } +} + +impl From for MessageType { + fn from(i: i32) -> Self { + match i { + 0 => MessageType::REGISTER, + 1 => MessageType::REGISTER_RESPONSE, + 2 => MessageType::UNREGISTER, + 3 => MessageType::DISCOVER, + 4 => MessageType::DISCOVER_RESPONSE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for MessageType { + fn from(s: &'a str) -> Self { + match s { + "REGISTER" => MessageType::REGISTER, + "REGISTER_RESPONSE" => MessageType::REGISTER_RESPONSE, + "UNREGISTER" => MessageType::UNREGISTER, + "DISCOVER" => MessageType::DISCOVER, + "DISCOVER_RESPONSE" => MessageType::DISCOVER_RESPONSE, + _ => Self::default(), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ResponseStatus { + OK = 0, + E_INVALID_NAMESPACE = 100, + E_INVALID_SIGNED_PEER_RECORD = 101, + E_INVALID_TTL = 102, + E_INVALID_COOKIE = 103, + E_NOT_AUTHORIZED = 200, + E_INTERNAL_ERROR = 300, + E_UNAVAILABLE = 400, +} + +impl Default for ResponseStatus { + fn default() -> Self { + ResponseStatus::OK + } +} + +impl From for ResponseStatus { + fn from(i: i32) -> Self { + match i { + 0 => ResponseStatus::OK, + 100 => ResponseStatus::E_INVALID_NAMESPACE, + 101 => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, + 102 => ResponseStatus::E_INVALID_TTL, + 103 => ResponseStatus::E_INVALID_COOKIE, + 200 => ResponseStatus::E_NOT_AUTHORIZED, + 300 => ResponseStatus::E_INTERNAL_ERROR, + 400 => ResponseStatus::E_UNAVAILABLE, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ResponseStatus { + fn from(s: &'a str) -> Self { + match s { + "OK" => ResponseStatus::OK, + "E_INVALID_NAMESPACE" => ResponseStatus::E_INVALID_NAMESPACE, + "E_INVALID_SIGNED_PEER_RECORD" => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, + "E_INVALID_TTL" => ResponseStatus::E_INVALID_TTL, + "E_INVALID_COOKIE" => ResponseStatus::E_INVALID_COOKIE, + "E_NOT_AUTHORIZED" => ResponseStatus::E_NOT_AUTHORIZED, + "E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR, + "E_UNAVAILABLE" => ResponseStatus::E_UNAVAILABLE, + _ => Self::default(), + } + } +} + +} + diff --git a/transports/noise/build.rs b/transports/noise/build.rs deleted file mode 100644 index c9cf60412cd..00000000000 --- a/transports/noise/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/io/handshake/payload.proto"], &["src"]).unwrap(); -} diff --git a/transports/noise/src/generated/payload/proto.rs b/transports/noise/src/generated/payload/proto.rs index 5481910f796..7b17a58ef37 100644 --- a/transports/noise/src/generated/payload/proto.rs +++ b/transports/noise/src/generated/payload/proto.rs @@ -9,27 +9,26 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct NoiseHandshakePayload<'a> { - pub identity_key: Cow<'a, [u8]>, - pub identity_sig: Cow<'a, [u8]>, - pub data: Cow<'a, [u8]>, +pub struct NoiseHandshakePayload { + pub identity_key: Vec, + pub identity_sig: Vec, + pub data: Vec, } -impl<'a> MessageRead<'a> for NoiseHandshakePayload<'a> { +impl<'a> MessageRead<'a> for NoiseHandshakePayload { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.identity_key = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(18) => msg.identity_sig = r.read_bytes(bytes).map(Cow::Borrowed)?, - Ok(26) => msg.data = r.read_bytes(bytes).map(Cow::Borrowed)?, + Ok(10) => msg.identity_key = r.read_bytes(bytes)?.to_owned(), + Ok(18) => msg.identity_sig = r.read_bytes(bytes)?.to_owned(), + Ok(26) => msg.data = r.read_bytes(bytes)?.to_owned(), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -38,18 +37,18 @@ impl<'a> MessageRead<'a> for NoiseHandshakePayload<'a> { } } -impl<'a> MessageWrite for NoiseHandshakePayload<'a> { +impl MessageWrite for NoiseHandshakePayload { fn get_size(&self) -> usize { 0 - + if self.identity_key == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.identity_key).len()) } - + if self.identity_sig == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.identity_sig).len()) } - + if self.data == Cow::Borrowed(b"") { 0 } else { 1 + sizeof_len((&self.data).len()) } + + if self.identity_key.is_empty() { 0 } else { 1 + sizeof_len((&self.identity_key).len()) } + + if self.identity_sig.is_empty() { 0 } else { 1 + sizeof_len((&self.identity_sig).len()) } + + if self.data.is_empty() { 0 } else { 1 + sizeof_len((&self.data).len()) } } fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.identity_key != Cow::Borrowed(b"") { w.write_with_tag(10, |w| w.write_bytes(&**&self.identity_key))?; } - if self.identity_sig != Cow::Borrowed(b"") { w.write_with_tag(18, |w| w.write_bytes(&**&self.identity_sig))?; } - if self.data != Cow::Borrowed(b"") { w.write_with_tag(26, |w| w.write_bytes(&**&self.data))?; } + if !self.identity_key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.identity_key))?; } + if !self.identity_sig.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.identity_sig))?; } + if !self.data.is_empty() { w.write_with_tag(26, |w| w.write_bytes(&**&self.data))?; } Ok(()) } } diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index cdcea04509a..fd8c00cb74b 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -20,11 +20,6 @@ //! Noise protocol handshake I/O. -#[allow(clippy::derive_partial_eq_without_eq)] -mod payload_proto { - include!(concat!(env!("OUT_DIR"), "/payload.proto.rs")); -} - use crate::io::{framed::NoiseFramed, NoiseOutput}; use crate::protocol::{KeypairIdentity, Protocol, PublicKey}; #[allow(deprecated)] diff --git a/transports/noise/src/io/handshake/payload.proto b/transports/noise/src/io/handshake/payload.proto deleted file mode 100644 index 1893dc55037..00000000000 --- a/transports/noise/src/io/handshake/payload.proto +++ /dev/null @@ -1,11 +0,0 @@ -syntax = "proto3"; - -package payload.proto; - -// Payloads for Noise handshake messages. - -message NoiseHandshakePayload { - bytes identity_key = 1; - bytes identity_sig = 2; - bytes data = 3; -} diff --git a/transports/plaintext/src/generated/structs.rs b/transports/plaintext/src/generated/structs.rs index 306d78db920..aa4d94f35dd 100644 --- a/transports/plaintext/src/generated/structs.rs +++ b/transports/plaintext/src/generated/structs.rs @@ -9,25 +9,24 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Exchange<'a> { - pub id: Option>, - pub pubkey: Option>, +pub struct Exchange { + pub id: Option>, + pub pubkey: Option>, } -impl<'a> MessageRead<'a> for Exchange<'a> { +impl<'a> MessageRead<'a> for Exchange { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { - Ok(10) => msg.id = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), - Ok(18) => msg.pubkey = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(10) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.pubkey = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -36,7 +35,7 @@ impl<'a> MessageRead<'a> for Exchange<'a> { } } -impl<'a> MessageWrite for Exchange<'a> { +impl MessageWrite for Exchange { fn get_size(&self) -> usize { 0 + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) diff --git a/transports/plaintext/src/mod.rs b/transports/plaintext/src/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/transports/plaintext/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/transports/plaintext/src/structs.rs b/transports/plaintext/src/structs.rs new file mode 100644 index 00000000000..aa4d94f35dd --- /dev/null +++ b/transports/plaintext/src/structs.rs @@ -0,0 +1,51 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Exchange { + pub id: Option>, + pub pubkey: Option>, +} + +impl<'a> MessageRead<'a> for Exchange { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), + Ok(18) => msg.pubkey = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Exchange { + fn get_size(&self) -> usize { + 0 + + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + + self.pubkey.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if let Some(ref s) = self.pubkey { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + diff --git a/transports/webrtc/src/generated/webrtc/pb.rs b/transports/webrtc/src/generated/webrtc/pb.rs index ac6943dcf59..9e33e97188c 100644 --- a/transports/webrtc/src/generated/webrtc/pb.rs +++ b/transports/webrtc/src/generated/webrtc/pb.rs @@ -9,25 +9,24 @@ #![cfg_attr(rustfmt, rustfmt_skip)] -use std::borrow::Cow; use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; use quick_protobuf::sizeofs::*; use super::super::*; #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Debug, Default, PartialEq, Clone)] -pub struct Message<'a> { +pub struct Message { pub flag: Option, - pub message: Option>, + pub message: Option>, } -impl<'a> MessageRead<'a> for Message<'a> { +impl<'a> MessageRead<'a> for Message { fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { let mut msg = Self::default(); while !r.is_eof() { match r.next_tag(bytes) { Ok(8) => msg.flag = Some(r.read_enum(bytes)?), - Ok(18) => msg.message = Some(r.read_bytes(bytes).map(Cow::Borrowed)?), + Ok(18) => msg.message = Some(r.read_bytes(bytes)?.to_owned()), Ok(t) => { r.read_unknown(bytes, t)?; } Err(e) => return Err(e), } @@ -36,7 +35,7 @@ impl<'a> MessageRead<'a> for Message<'a> { } } -impl<'a> MessageWrite for Message<'a> { +impl MessageWrite for Message { fn get_size(&self) -> usize { 0 + self.flag.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) diff --git a/transports/webrtc/src/mod.rs b/transports/webrtc/src/mod.rs new file mode 100644 index 00000000000..5e9f6373b12 --- /dev/null +++ b/transports/webrtc/src/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod webrtc; diff --git a/transports/webrtc/src/webrtc/mod.rs b/transports/webrtc/src/webrtc/mod.rs new file mode 100644 index 00000000000..aec6164c7ef --- /dev/null +++ b/transports/webrtc/src/webrtc/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod pb; diff --git a/transports/webrtc/src/webrtc/pb.rs b/transports/webrtc/src/webrtc/pb.rs new file mode 100644 index 00000000000..9e33e97188c --- /dev/null +++ b/transports/webrtc/src/webrtc/pb.rs @@ -0,0 +1,91 @@ +// Automatically generated rust module for 'message.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::super::*; + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub flag: Option, + pub message: Option>, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.flag = Some(r.read_enum(bytes)?), + Ok(18) => msg.message = Some(r.read_bytes(bytes)?.to_owned()), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + self.flag.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) + + self.message.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if let Some(ref s) = self.flag { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } + if let Some(ref s) = self.message { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } + Ok(()) + } +} + +pub mod mod_Message { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Flag { + FIN = 0, + STOP_SENDING = 1, + RESET = 2, +} + +impl Default for Flag { + fn default() -> Self { + Flag::FIN + } +} + +impl From for Flag { + fn from(i: i32) -> Self { + match i { + 0 => Flag::FIN, + 1 => Flag::STOP_SENDING, + 2 => Flag::RESET, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for Flag { + fn from(s: &'a str) -> Self { + match s { + "FIN" => Flag::FIN, + "STOP_SENDING" => Flag::STOP_SENDING, + "RESET" => Flag::RESET, + _ => Self::default(), + } + } +} + +} + From 2e2bbe7b79d5342b9441387ab93b11098127b1f4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Feb 2023 19:15:11 -0500 Subject: [PATCH 31/66] Fix core --- core/src/peer_record.rs | 4 ++-- core/src/signed_envelope.rs | 1 - transports/noise/build.rs | 23 +++++++++++++++++++ transports/noise/src/io/handshake.rs | 5 ++++ .../noise/src/io/handshake/payload.proto | 11 +++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 transports/noise/build.rs create mode 100644 transports/noise/src/io/handshake/payload.proto diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index dc7fe826f42..2b3fd36eb01 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -177,10 +177,10 @@ mod tests { let payload = { let record = proto::PeerRecord { - peer_id: Cow::from(identity_a.public().to_peer_id().to_bytes()), + peer_id: identity_a.public().to_peer_id().to_bytes(), seq: 0, addresses: vec![proto::AddressInfo { - multiaddr: Cow::from(addr.to_vec()), + multiaddr: addr.to_vec(), }], }; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 78e2e18b67e..2725da1502e 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -2,7 +2,6 @@ use crate::identity::error::SigningError; use crate::identity::Keypair; use crate::{identity, proto, DecodeError, PublicKey}; use quick_protobuf::{BytesReader, Writer}; -use std::borrow::Cow; use std::convert::TryInto; use std::fmt; use unsigned_varint::encode::usize_buffer; diff --git a/transports/noise/build.rs b/transports/noise/build.rs new file mode 100644 index 00000000000..c9cf60412cd --- /dev/null +++ b/transports/noise/build.rs @@ -0,0 +1,23 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +fn main() { + prost_build::compile_protos(&["src/io/handshake/payload.proto"], &["src"]).unwrap(); +} diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index fd8c00cb74b..cdcea04509a 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -20,6 +20,11 @@ //! Noise protocol handshake I/O. +#[allow(clippy::derive_partial_eq_without_eq)] +mod payload_proto { + include!(concat!(env!("OUT_DIR"), "/payload.proto.rs")); +} + use crate::io::{framed::NoiseFramed, NoiseOutput}; use crate::protocol::{KeypairIdentity, Protocol, PublicKey}; #[allow(deprecated)] diff --git a/transports/noise/src/io/handshake/payload.proto b/transports/noise/src/io/handshake/payload.proto new file mode 100644 index 00000000000..1893dc55037 --- /dev/null +++ b/transports/noise/src/io/handshake/payload.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package payload.proto; + +// Payloads for Noise handshake messages. + +message NoiseHandshakePayload { + bytes identity_key = 1; + bytes identity_sig = 2; + bytes data = 3; +} From 59ff24de5bed15c54b4daac7b9afe17bfa814653 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Feb 2023 19:15:28 -0500 Subject: [PATCH 32/66] Run fmt --- protocols/dcutr/src/protocol/inbound.rs | 5 +---- protocols/dcutr/src/protocol/outbound.rs | 6 +----- protocols/relay/src/protocol/inbound_hop.rs | 5 +---- protocols/relay/src/protocol/inbound_stop.rs | 8 ++------ 4 files changed, 5 insertions(+), 19 deletions(-) diff --git a/protocols/dcutr/src/protocol/inbound.rs b/protocols/dcutr/src/protocol/inbound.rs index 96bee210e2a..fde42c6c924 100644 --- a/protocols/dcutr/src/protocol/inbound.rs +++ b/protocols/dcutr/src/protocol/inbound.rs @@ -103,10 +103,7 @@ impl PendingConnect { ) -> Result, UpgradeError> { let msg = proto::HolePunch { type_pb: proto::Type::CONNECT, - ObsAddrs: local_obs_addrs - .into_iter() - .map(|a| a.to_vec()) - .collect(), + ObsAddrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).collect(), }; self.substream.send(msg).await?; diff --git a/protocols/dcutr/src/protocol/outbound.rs b/protocols/dcutr/src/protocol/outbound.rs index 7bebcd260a4..9bbb1eb89a8 100644 --- a/protocols/dcutr/src/protocol/outbound.rs +++ b/protocols/dcutr/src/protocol/outbound.rs @@ -61,11 +61,7 @@ impl upgrade::OutboundUpgrade for Upgrade { let msg = proto::HolePunch { type_pb: proto::Type::CONNECT, - ObsAddrs: self - .obs_addrs - .into_iter() - .map(|a| a.to_vec()) - .collect(), + ObsAddrs: self.obs_addrs.into_iter().map(|a| a.to_vec()).collect(), }; async move { diff --git a/protocols/relay/src/protocol/inbound_hop.rs b/protocols/relay/src/protocol/inbound_hop.rs index 90513f05bd4..283ceacc886 100644 --- a/protocols/relay/src/protocol/inbound_hop.rs +++ b/protocols/relay/src/protocol/inbound_hop.rs @@ -227,10 +227,7 @@ impl CircuitReq { self.substream.close().await.map_err(Into::into) } - async fn send( - &mut self, - msg: proto::HopMessage, - ) -> Result<(), quick_protobuf_codec::Error> { + async fn send(&mut self, msg: proto::HopMessage) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; diff --git a/protocols/relay/src/protocol/inbound_stop.rs b/protocols/relay/src/protocol/inbound_stop.rs index 86a549cd882..77152f0b237 100644 --- a/protocols/relay/src/protocol/inbound_stop.rs +++ b/protocols/relay/src/protocol/inbound_stop.rs @@ -110,8 +110,7 @@ pub enum FatalUpgradeError { } pub struct Circuit { - substream: - Framed>, + substream: Framed>, src_peer_id: PeerId, limit: Option, } @@ -160,10 +159,7 @@ impl Circuit { self.send(msg).await.map_err(Into::into) } - async fn send( - &mut self, - msg: proto::StopMessage, - ) -> Result<(), quick_protobuf_codec::Error> { + async fn send(&mut self, msg: proto::StopMessage) -> Result<(), quick_protobuf_codec::Error> { self.substream.send(msg).await?; self.substream.flush().await?; From a8762f8c78738bb8f2067ee6479d77f3a313a575 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Feb 2023 21:59:06 -0500 Subject: [PATCH 33/66] Remove prost from identify --- misc/quick-protobuf-codec/src/lib.rs | 8 ++--- protocols/autonat/src/protocol.rs | 7 ++--- protocols/identify/Cargo.toml | 5 +--- protocols/identify/build.rs | 23 --------------- protocols/identify/src/lib.rs | 6 ++-- protocols/identify/src/protocol.rs | 44 ++++++++++++++-------------- protocols/identify/src/structs.proto | 27 ----------------- 7 files changed, 33 insertions(+), 87 deletions(-) delete mode 100644 protocols/identify/build.rs delete mode 100644 protocols/identify/src/structs.proto diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index 9648ae08d15..51f2d603fdb 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] use asynchronous_codec::{Decoder, Encoder}; -use bytes::BytesMut; +use bytes::{Bytes, BytesMut}; use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use std::marker::PhantomData; use unsigned_varint::codec::UviBytes; @@ -35,11 +35,11 @@ impl Encoder for Codec { type Error = Error; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { - let mut encoded_msg = BytesMut::new(); - let mut writer = Writer::new(encoded_msg.as_mut()); + let mut encoded_msg = Vec::new(); + let mut writer = Writer::new(&mut encoded_msg); item.write_message(&mut writer) .expect("Encoding to succeed"); - self.uvi.encode(encoded_msg.freeze(), dst)?; + self.uvi.encode(Bytes::from(encoded_msg), dst)?; Ok(()) } diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/protocol.rs index 288877e745a..eb4396d7e5e 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/protocol.rs @@ -24,7 +24,6 @@ use futures::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; use libp2p_core::{upgrade, Multiaddr, PeerId}; use libp2p_request_response::{self as request_response, ProtocolName}; use quick_protobuf::{BytesReader, Writer}; -use std::borrow::Cow; use std::{convert::TryFrom, io}; #[derive(Clone, Debug)] @@ -276,12 +275,12 @@ impl DialResponse { let dial_response = match self.result { Ok(addr) => proto::DialResponse { status: Some(proto::ResponseStatus::OK), - statusText: self.status_text.map(Cow::from), - addr: Some(Cow::from(addr.to_vec())), + statusText: self.status_text, + addr: Some(addr.to_vec()), }, Err(error) => proto::DialResponse { status: Some(error.into()), - statusText: self.status_text.map(Cow::from), + statusText: self.status_text, addr: None, }, }; diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 35e6417058a..6066a3b9727 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -19,7 +19,7 @@ libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4.1" lru = "0.9.0" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } -prost = "0.11" +quick-protobuf = "0.8" smallvec = "1.6.1" thiserror = "1.0" void = "1.0" @@ -34,9 +34,6 @@ libp2p-noise = { path = "../../transports/noise" } libp2p-swarm = { path = "../../swarm", features = ["async-std"] } libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] } -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/protocols/identify/build.rs b/protocols/identify/build.rs deleted file mode 100644 index 56c7b20121a..00000000000 --- a/protocols/identify/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/structs.proto"], &["src"]).unwrap(); -} diff --git a/protocols/identify/src/lib.rs b/protocols/identify/src/lib.rs index 0155e1b64fe..7fb8df253ff 100644 --- a/protocols/identify/src/lib.rs +++ b/protocols/identify/src/lib.rs @@ -69,7 +69,7 @@ mod behaviour; mod handler; mod protocol; -#[allow(clippy::derive_partial_eq_without_eq)] -mod structs_proto { - include!(concat!(env!("OUT_DIR"), "/structs.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::structs::Identify; } diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 96365497552..523e199dee6 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::structs_proto; +use crate::proto; use asynchronous_codec::{FramedRead, FramedWrite}; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{ @@ -169,18 +169,18 @@ where let pubkey_bytes = info.public_key.to_protobuf_encoding(); - let message = structs_proto::Identify { - agent_version: Some(info.agent_version), - protocol_version: Some(info.protocol_version), - public_key: Some(pubkey_bytes), - listen_addrs, - observed_addr: Some(info.observed_addr.to_vec()), + let message = proto::Identify { + agentVersion: Some(info.agent_version), + protocolVersion: Some(info.protocol_version), + publicKey: Some(pubkey_bytes), + listenAddrs: listen_addrs, + observedAddr: Some(info.observed_addr.to_vec()), protocols: info.protocols, }; let mut framed_io = FramedWrite::new( io, - quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), ); framed_io.send(message).await?; @@ -197,7 +197,7 @@ where let info = FramedRead::new( socket, - quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_SIZE_BYTES), ) .next() .await @@ -209,17 +209,17 @@ where Ok(info) } -impl TryFrom for Info { +impl TryFrom for Info { type Error = UpgradeError; - fn try_from(msg: structs_proto::Identify) -> Result { + fn try_from(msg: proto::Identify) -> Result { fn parse_multiaddr(bytes: Vec) -> Result { Multiaddr::try_from(bytes) } let listen_addrs = { let mut addrs = Vec::new(); - for addr in msg.listen_addrs.into_iter() { + for addr in msg.listenAddrs.into_iter() { match parse_multiaddr(addr) { Ok(a) => addrs.push(a), Err(e) => { @@ -230,9 +230,9 @@ impl TryFrom for Info { addrs }; - let public_key = PublicKey::from_protobuf_encoding(&msg.public_key.unwrap_or_default())?; + let public_key = PublicKey::from_protobuf_encoding(&msg.publicKey.unwrap_or_default())?; - let observed_addr = match parse_multiaddr(msg.observed_addr.unwrap_or_default()) { + let observed_addr = match parse_multiaddr(msg.observedAddr.unwrap_or_default()) { Ok(a) => a, Err(e) => { debug!("Unable to parse multiaddr: {e:?}"); @@ -241,8 +241,8 @@ impl TryFrom for Info { }; let info = Info { public_key, - protocol_version: msg.protocol_version.unwrap_or_default(), - agent_version: msg.agent_version.unwrap_or_default(), + protocol_version: msg.protocolVersion.unwrap_or_default(), + agent_version: msg.agentVersion.unwrap_or_default(), listen_addrs, protocols: msg.protocols, observed_addr, @@ -372,13 +372,13 @@ mod tests { a }; - let payload = structs_proto::Identify { - agent_version: None, - listen_addrs: vec![valid_multiaddr_bytes, invalid_multiaddr], - observed_addr: None, - protocol_version: None, + let payload = proto::Identify { + agentVersion: None, + listenAddrs: vec![valid_multiaddr_bytes, invalid_multiaddr], + observedAddr: None, + protocolVersion: None, protocols: vec![], - public_key: Some( + publicKey: Some( identity::Keypair::generate_ed25519() .public() .to_protobuf_encoding(), diff --git a/protocols/identify/src/structs.proto b/protocols/identify/src/structs.proto deleted file mode 100644 index fbe6836bfdb..00000000000 --- a/protocols/identify/src/structs.proto +++ /dev/null @@ -1,27 +0,0 @@ -syntax = "proto2"; - -package structs; - -message Identify { - // protocolVersion determines compatibility between peers - optional string protocolVersion = 5; // e.g. ipfs/1.0.0 - - // agentVersion is like a UserAgent string in browsers, or client version in bittorrent - // includes the client name and client. - optional string agentVersion = 6; // e.g. go-ipfs/0.1.0 - - // publicKey is this node's public key (which also gives its node.ID) - // - may not need to be sent, as secure channel implies it has been sent. - // - then again, if we change / disable secure channel, may still want it. - optional bytes publicKey = 1; - - // listenAddrs are the multiaddrs the sender node listens for open connections on - repeated bytes listenAddrs = 2; - - // oservedAddr is the multiaddr of the remote endpoint that the sender node perceives - // this is useful information to convey to the other side, as it helps the remote endpoint - // determine whether its connection to the local peer goes through NAT. - optional bytes observedAddr = 4; - - repeated string protocols = 3; -} From fc13432b6346fb639d8f788170c0519ecf3fe0e4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Feb 2023 22:02:14 -0500 Subject: [PATCH 34/66] Fix autonat --- protocols/autonat/src/protocol.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/protocol.rs index eb4396d7e5e..78753d76e6e 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/protocol.rs @@ -354,11 +354,8 @@ mod tests { type_pb: Some(proto::MessageType::DIAL), dial: Some(proto::Dial { peer: Some(proto::PeerInfo { - id: Some(Cow::from(PeerId::random().to_bytes())), - addrs: vec![ - Cow::from(valid_multiaddr_bytes), - Cow::from(invalid_multiaddr), - ], + id: Some(PeerId::random().to_bytes()), + addrs: vec![valid_multiaddr_bytes, invalid_multiaddr], }), }), dialResponse: None, From 4bd8ab01ad6d14b5b4a48635e4213577131512a8 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Feb 2023 22:05:20 -0500 Subject: [PATCH 35/66] Update changelogs --- misc/quick-protobuf-codec/CHANGELOG.md | 2 +- protocols/dcutr/CHANGELOG.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/misc/quick-protobuf-codec/CHANGELOG.md b/misc/quick-protobuf-codec/CHANGELOG.md index 8e0f4d845db..cff8b9e5512 100644 --- a/misc/quick-protobuf-codec/CHANGELOG.md +++ b/misc/quick-protobuf-codec/CHANGELOG.md @@ -1,6 +1,6 @@ # 0.1.0 [unreleased] -- Update to `quick-protobuf`. See [PR 3312]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. [PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 diff --git a/protocols/dcutr/CHANGELOG.md b/protocols/dcutr/CHANGELOG.md index afcd7e07478..d2db8040bb3 100644 --- a/protocols/dcutr/CHANGELOG.md +++ b/protocols/dcutr/CHANGELOG.md @@ -11,10 +11,14 @@ - Rename types in public API to follow naming conventions defined in [issue 2217]. See [PR 3214]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3213]: https://github.com/libp2p/rust-libp2p/pull/3213 [PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153 [issue 2217]: https://github.com/libp2p/rust-libp2p/issues/2217 [PR 3214]: https://github.com/libp2p/rust-libp2p/pull/3214 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 + # 0.8.1 From 55d6f4d9cb136536551d46a92765df38623df4ed Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 4 Feb 2023 18:35:46 -0500 Subject: [PATCH 36/66] Update docs --- misc/quick-protobuf-codec/Cargo.toml | 2 +- misc/quick-protobuf-codec/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/misc/quick-protobuf-codec/Cargo.toml b/misc/quick-protobuf-codec/Cargo.toml index eab4d073faf..34eb7f43d3b 100644 --- a/misc/quick-protobuf-codec/Cargo.toml +++ b/misc/quick-protobuf-codec/Cargo.toml @@ -2,7 +2,7 @@ name = "quick-protobuf-codec" edition = "2021" rust-version = "1.60.0" -description = "Asynchronous de-/encoding of Protobuf structs using asynchronous-codec, unsigned-varint and prost." +description = "Asynchronous de-/encoding of Protobuf structs using asynchronous-codec, unsigned-varint and quick-protobuf." version = "0.1.0" authors = ["Max Inden "] license = "MIT" diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index 51f2d603fdb..54785fbe209 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -7,8 +7,8 @@ use std::marker::PhantomData; use unsigned_varint::codec::UviBytes; /// [`Codec`] implements [`Encoder`] and [`Decoder`], uses [`unsigned_varint`] -/// to prefix messages with their length and uses [`prost`] and a provided -/// `struct` implementing [`Message`] to do the encoding. +/// to prefix messages with their length and uses [`quick_protobuf`] and a provided +/// `struct` implementing [`MessageRead`] and [`MessageWrite`] to do the encoding. pub struct Codec { uvi: UviBytes, phantom: PhantomData<(In, Out)>, From b5cfe293d1777e6b34c9b2c2e6ab036f47e5fe00 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 4 Feb 2023 18:44:37 -0500 Subject: [PATCH 37/66] Remove prost from floodsub --- protocols/floodsub/CHANGELOG.md | 3 + protocols/floodsub/Cargo.toml | 5 +- protocols/floodsub/build.rs | 23 ----- protocols/floodsub/src/floodsub/mod.rs | 2 - protocols/floodsub/src/floodsub/pb.rs | 137 ------------------------- protocols/floodsub/src/lib.rs | 6 +- protocols/floodsub/src/mod.rs | 2 - protocols/floodsub/src/protocol.rs | 14 +-- protocols/floodsub/src/rpc.proto | 20 ---- 9 files changed, 14 insertions(+), 198 deletions(-) delete mode 100644 protocols/floodsub/build.rs delete mode 100644 protocols/floodsub/src/floodsub/mod.rs delete mode 100644 protocols/floodsub/src/floodsub/pb.rs delete mode 100644 protocols/floodsub/src/mod.rs delete mode 100644 protocols/floodsub/src/rpc.proto diff --git a/protocols/floodsub/CHANGELOG.md b/protocols/floodsub/CHANGELOG.md index a15fc84982f..33d4fd4920d 100644 --- a/protocols/floodsub/CHANGELOG.md +++ b/protocols/floodsub/CHANGELOG.md @@ -6,7 +6,10 @@ - Read and write protocols messages via `prost-codec`. See [PR 3224]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [pr 3224]: https://github.com/libp2p/rust-libp2p/pull/3224 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.41.0 diff --git a/protocols/floodsub/Cargo.toml b/protocols/floodsub/Cargo.toml index 2c09335f688..c76433013e7 100644 --- a/protocols/floodsub/Cargo.toml +++ b/protocols/floodsub/Cargo.toml @@ -18,15 +18,12 @@ futures = "0.3.1" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" -prost = "0.11" +quick-protobuf = "0.8" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" smallvec = "1.6.1" thiserror = "1.0.37" -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/protocols/floodsub/build.rs b/protocols/floodsub/build.rs deleted file mode 100644 index a3de99880dc..00000000000 --- a/protocols/floodsub/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/rpc.proto"], &["src"]).unwrap(); -} diff --git a/protocols/floodsub/src/floodsub/mod.rs b/protocols/floodsub/src/floodsub/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/floodsub/src/floodsub/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/floodsub/src/floodsub/pb.rs b/protocols/floodsub/src/floodsub/pb.rs deleted file mode 100644 index a11135ad053..00000000000 --- a/protocols/floodsub/src/floodsub/pb.rs +++ /dev/null @@ -1,137 +0,0 @@ -// Automatically generated rust module for 'rpc.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct RPC { - pub subscriptions: Vec, - pub publish: Vec, -} - -impl<'a> MessageRead<'a> for RPC { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), - Ok(18) => msg.publish.push(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for RPC { - fn get_size(&self) -> usize { - 0 - + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } - for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } - Ok(()) - } -} - -pub mod mod_RPC { - -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct SubOpts { - pub subscribe: Option, - pub topic_id: Option, -} - -impl<'a> MessageRead<'a> for SubOpts { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), - Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for SubOpts { - fn get_size(&self) -> usize { - 0 - + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } - if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } - Ok(()) - } -} - -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic_ids: Vec, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), - Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), - Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } - for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } - Ok(()) - } -} - diff --git a/protocols/floodsub/src/lib.rs b/protocols/floodsub/src/lib.rs index 7a37bfd9c21..c0d1327f2df 100644 --- a/protocols/floodsub/src/lib.rs +++ b/protocols/floodsub/src/lib.rs @@ -29,9 +29,9 @@ pub mod protocol; mod layer; mod topic; -#[allow(clippy::derive_partial_eq_without_eq)] -mod rpc_proto { - include!(concat!(env!("OUT_DIR"), "/floodsub.pb.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::floodsub::pb::{mod_RPC::SubOpts, Message, RPC}; } pub use self::layer::{Floodsub, FloodsubEvent}; diff --git a/protocols/floodsub/src/mod.rs b/protocols/floodsub/src/mod.rs deleted file mode 100644 index 1cef0041eb6..00000000000 --- a/protocols/floodsub/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod floodsub; diff --git a/protocols/floodsub/src/protocol.rs b/protocols/floodsub/src/protocol.rs index c92b89991b8..7a70806606d 100644 --- a/protocols/floodsub/src/protocol.rs +++ b/protocols/floodsub/src/protocol.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::rpc_proto; +use crate::proto; use crate::topic::Topic; use asynchronous_codec::Framed; use futures::{ @@ -65,7 +65,7 @@ where Box::pin(async move { let mut framed = Framed::new( socket, - quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), ); let rpc = framed @@ -152,7 +152,7 @@ where Box::pin(async move { let mut framed = Framed::new( socket, - quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), + quick_protobuf_codec::Codec::::new(MAX_MESSAGE_LEN_BYTES), ); framed.send(self.into_rpc()).await?; framed.close().await?; @@ -163,12 +163,12 @@ where impl FloodsubRpc { /// Turns this `FloodsubRpc` into a message that can be sent to a substream. - fn into_rpc(self) -> rpc_proto::Rpc { - rpc_proto::Rpc { + fn into_rpc(self) -> proto::RPC { + proto::RPC { publish: self .messages .into_iter() - .map(|msg| rpc_proto::Message { + .map(|msg| proto::Message { from: Some(msg.source.to_bytes()), data: Some(msg.data), seqno: Some(msg.sequence_number), @@ -179,7 +179,7 @@ impl FloodsubRpc { subscriptions: self .subscriptions .into_iter() - .map(|topic| rpc_proto::rpc::SubOpts { + .map(|topic| proto::SubOpts { subscribe: Some(topic.action == FloodsubSubscriptionAction::Subscribe), topic_id: Some(topic.topic.into()), }) diff --git a/protocols/floodsub/src/rpc.proto b/protocols/floodsub/src/rpc.proto deleted file mode 100644 index 84f0ea51795..00000000000 --- a/protocols/floodsub/src/rpc.proto +++ /dev/null @@ -1,20 +0,0 @@ -syntax = "proto2"; - -package floodsub.pb; - -message RPC { - repeated SubOpts subscriptions = 1; - repeated Message publish = 2; - - message SubOpts { - optional bool subscribe = 1; // subscribe or unsubcribe - optional string topic_id = 2; - } -} - -message Message { - optional bytes from = 1; - optional bytes data = 2; - optional bytes seqno = 3; - repeated string topic_ids = 4; -} From c2eaefd5b237bf770a70d4dbb6e9e89278168b3f Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 4 Feb 2023 19:41:56 -0500 Subject: [PATCH 38/66] Remove prost from gossipsub --- protocols/gossipsub/CHANGELOG.md | 3 + protocols/gossipsub/Cargo.toml | 5 +- protocols/gossipsub/build.rs | 23 - protocols/gossipsub/src/behaviour.rs | 68 ++- protocols/gossipsub/src/behaviour/tests.rs | 2 +- protocols/gossipsub/src/compat.proto | 12 - protocols/gossipsub/src/compat/mod.rs | 2 - protocols/gossipsub/src/compat/pb.rs | 67 --- protocols/gossipsub/src/gossipsub/mod.rs | 2 - protocols/gossipsub/src/gossipsub/pb.rs | 567 --------------------- protocols/gossipsub/src/handler.rs | 12 +- protocols/gossipsub/src/mod.rs | 3 - protocols/gossipsub/src/protocol.rs | 19 +- protocols/gossipsub/src/rpc.proto | 84 --- protocols/gossipsub/src/rpc_proto.rs | 46 +- protocols/gossipsub/src/topic.rs | 15 +- protocols/gossipsub/src/types.rs | 30 +- 17 files changed, 105 insertions(+), 855 deletions(-) delete mode 100644 protocols/gossipsub/build.rs delete mode 100644 protocols/gossipsub/src/compat.proto delete mode 100644 protocols/gossipsub/src/compat/mod.rs delete mode 100644 protocols/gossipsub/src/compat/pb.rs delete mode 100644 protocols/gossipsub/src/gossipsub/mod.rs delete mode 100644 protocols/gossipsub/src/gossipsub/pb.rs delete mode 100644 protocols/gossipsub/src/mod.rs delete mode 100644 protocols/gossipsub/src/rpc.proto diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index c24178fcb94..ad65bef3226 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -14,10 +14,13 @@ It is preferred to import the gossipsub protocol as a module (`use libp2p::gossipsub;`), and refer to its types via `gossipsub::`. For example: `gossipsub::Behaviour` or `gossipsub::RawMessage`. See [PR 3303]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3207]: https://github.com/libp2p/rust-libp2p/pull/3207/ [PR 3303]: https://github.com/libp2p/rust-libp2p/pull/3303/ [PR 3381]: https://github.com/libp2p/rust-libp2p/pull/3381/ [discussion 2174]: https://github.com/libp2p/rust-libp2p/discussions/2174 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.43.0 diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 185f4b1ab3a..715b3913b82 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -24,7 +24,7 @@ log = "0.4.11" sha2 = "0.10.0" base64 = "0.20.0" smallvec = "1.6.1" -prost = "0.11" +quick-protobuf = "0.8" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } hex_fmt = "0.3.0" regex = "1.5.5" @@ -46,9 +46,6 @@ libp2p-swarm = { path = "../../swarm" } libp2p-yamux = { path = "../../muxers/yamux" } quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" } -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/protocols/gossipsub/build.rs b/protocols/gossipsub/build.rs deleted file mode 100644 index a0c81052bdc..00000000000 --- a/protocols/gossipsub/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/rpc.proto", "src/compat.proto"], &["src"]).unwrap(); -} diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index eccecd3a498..89fd5477bff 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -32,7 +32,6 @@ use std::{ use futures::StreamExt; use log::{debug, error, trace, warn}; use prometheus_client::registry::Registry; -use prost::Message as _; use rand::{seq::SliceRandom, thread_rng}; use libp2p_core::{ @@ -64,7 +63,8 @@ use crate::types::{ Subscription, SubscriptionAction, }; use crate::types::{PeerConnections, PeerKind, Rpc}; -use crate::{rpc_proto, TopicScoreParams}; +use crate::{rpc_proto::proto, TopicScoreParams}; +use quick_protobuf::{MessageWrite, Writer}; use std::{cmp::Ordering::Equal, fmt::Debug}; use wasm_timer::Interval; @@ -178,8 +178,8 @@ impl From for PublishConfig { let public_key = keypair.public(); let key_enc = public_key.to_protobuf_encoding(); let key = if key_enc.len() <= 42 { - // The public key can be inlined in [`rpc_proto::Message::from`], so we don't include it - // specifically in the [`rpc_proto::Message::key`] field. + // The public key can be inlined in [`rpc_proto::proto::::Message::from`], so we don't include it + // specifically in the [`rpc_proto::proto::Message::key`] field. None } else { // Include the protobuf encoding of the public key in the message. @@ -609,7 +609,7 @@ where .into_protobuf(); // check that the size doesn't exceed the max transmission size - if event.encoded_len() > self.config.max_transmit_size() { + if event.get_size() > self.config.max_transmit_size() { return Err(PublishError::MessageTooLarge); } @@ -720,7 +720,7 @@ where } // Send to peers we know are subscribed to the topic. - let msg_bytes = event.encoded_len(); + let msg_bytes = event.get_size(); for peer_id in recipient_peers.iter() { trace!("Sending message to peer: {:?}", peer_id); self.send_message(*peer_id, event.clone())?; @@ -1339,7 +1339,7 @@ where } .into_protobuf(); - let msg_bytes = message.encoded_len(); + let msg_bytes = message.get_size(); if self.send_message(*peer_id, message).is_err() { error!("Failed to send cached messages. Messages too large"); @@ -2736,7 +2736,7 @@ where } .into_protobuf(); - let msg_bytes = event.encoded_len(); + let msg_bytes = event.get_size(); for peer in recipient_peers.iter() { debug!("Sending message: {:?} to peer {:?}", msg_id, peer); self.send_message(*peer, event.clone())?; @@ -2767,7 +2767,7 @@ where let sequence_number: u64 = rand::random(); let signature = { - let message = rpc_proto::Message { + let message = proto::Message { from: Some(author.clone().to_bytes()), data: Some(data.clone()), seqno: Some(sequence_number.to_be_bytes().to_vec()), @@ -2776,10 +2776,12 @@ where key: None, }; - let mut buf = Vec::with_capacity(message.encoded_len()); + let mut buf = Vec::with_capacity(message.get_size()); + let mut writer = Writer::new(&mut buf); + message - .encode(&mut buf) - .expect("Buffer has sufficient capacity"); + .write_message(&mut writer) + .expect("Encoding to succeed"); // the signature is over the bytes "libp2p-pubsub:" let mut signature_bytes = SIGNING_PREFIX.to_vec(); @@ -2878,11 +2880,7 @@ where /// Send a [`Rpc`] message to a peer. This will wrap the message in an arc if it /// is not already an arc. - fn send_message( - &mut self, - peer_id: PeerId, - message: rpc_proto::Rpc, - ) -> Result<(), PublishError> { + fn send_message(&mut self, peer_id: PeerId, message: proto::RPC) -> Result<(), PublishError> { // If the message is oversized, try and fragment it. If it cannot be fragmented, log an // error and drop the message (all individual messages should be small enough to fit in the // max_transmit_size) @@ -2902,12 +2900,12 @@ where // If a message is too large to be sent as-is, this attempts to fragment it into smaller RPC // messages to be sent. - fn fragment_message(&self, rpc: rpc_proto::Rpc) -> Result, PublishError> { - if rpc.encoded_len() < self.config.max_transmit_size() { + fn fragment_message(&self, rpc: proto::RPC) -> Result, PublishError> { + if rpc.get_size() < self.config.max_transmit_size() { return Ok(vec![rpc]); } - let new_rpc = rpc_proto::Rpc { + let new_rpc = proto::RPC { subscriptions: Vec::new(), publish: Vec::new(), control: None, @@ -2923,7 +2921,7 @@ where // create a new RPC if the new object plus 5% of its size (for length prefix // buffers) exceeds the max transmit size. - if rpc_list[list_index].encoded_len() + (($object_size as f64) * 1.05) as usize + if rpc_list[list_index].get_size() + (($object_size as f64) * 1.05) as usize > self.config.max_transmit_size() && rpc_list[list_index] != new_rpc { @@ -2935,7 +2933,7 @@ where macro_rules! add_item { ($object: ident, $type: ident ) => { - let object_size = $object.encoded_len(); + let object_size = $object.get_size(); if object_size + 2 > self.config.max_transmit_size() { // This should not be possible. All received and published messages have already @@ -2963,12 +2961,12 @@ where // handle the control messages. If all are within the max_transmit_size, send them without // fragmenting, otherwise, fragment the control messages - let empty_control = rpc_proto::ControlMessage::default(); + let empty_control = proto::ControlMessage::default(); if let Some(control) = rpc.control.as_ref() { - if control.encoded_len() + 2 > self.config.max_transmit_size() { + if control.get_size() + 2 > self.config.max_transmit_size() { // fragment the RPC for ihave in &control.ihave { - let len = ihave.encoded_len(); + let len = ihave.get_size(); create_or_add_rpc!(len); rpc_list .last_mut() @@ -2979,7 +2977,7 @@ where .push(ihave.clone()); } for iwant in &control.iwant { - let len = iwant.encoded_len(); + let len = iwant.get_size(); create_or_add_rpc!(len); rpc_list .last_mut() @@ -2990,7 +2988,7 @@ where .push(iwant.clone()); } for graft in &control.graft { - let len = graft.encoded_len(); + let len = graft.get_size(); create_or_add_rpc!(len); rpc_list .last_mut() @@ -3001,7 +2999,7 @@ where .push(graft.clone()); } for prune in &control.prune { - let len = prune.encoded_len(); + let len = prune.get_size(); create_or_add_rpc!(len); rpc_list .last_mut() @@ -3012,7 +3010,7 @@ where .push(prune.clone()); } } else { - let len = control.encoded_len(); + let len = control.get_size(); create_or_add_rpc!(len); rpc_list.last_mut().expect("Always an element").control = Some(control.clone()); } @@ -3753,7 +3751,7 @@ mod local_test { // Messages over the limit should be split - while rpc_proto.encoded_len() < max_transmit_size { + while rpc_proto.get_size() < max_transmit_size { rpc.messages.push(test_message()); rpc_proto = rpc.clone().into_protobuf(); } @@ -3770,7 +3768,7 @@ mod local_test { // all fragmented messages should be under the limit for message in fragmented_messages { assert!( - message.encoded_len() < max_transmit_size, + message.get_size() < max_transmit_size, "all messages should be less than the transmission size" ); } @@ -3797,7 +3795,7 @@ mod local_test { .fragment_message(rpc_proto.clone()) .expect("Messages must be valid"); - if rpc_proto.encoded_len() < max_transmit_size { + if rpc_proto.get_size() < max_transmit_size { assert_eq!( fragmented_messages.len(), 1, @@ -3813,12 +3811,12 @@ mod local_test { // all fragmented messages should be under the limit for message in fragmented_messages { assert!( - message.encoded_len() < max_transmit_size, - "all messages should be less than the transmission size: list size {} max size{}", message.encoded_len(), max_transmit_size + message.get_size() < max_transmit_size, + "all messages should be less than the transmission size: list size {} max size{}", message.get_size(), max_transmit_size ); // ensure they can all be encoded - let mut buf = bytes::BytesMut::with_capacity(message.encoded_len()); + let mut buf = bytes::BytesMut::with_capacity(message.get_size()); codec.encode(message, &mut buf).unwrap() } } diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index 18e08e0ebb4..2ced2f4efcf 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -284,7 +284,7 @@ where } // Converts a protobuf message into a gossipsub message for reading the Gossipsub event queue. -fn proto_to_message(rpc: &crate::rpc_proto::Rpc) -> Rpc { +fn proto_to_message(rpc: &proto::RPC) -> Rpc { // Store valid messages. let mut messages = Vec::with_capacity(rpc.publish.len()); let rpc = rpc.clone(); diff --git a/protocols/gossipsub/src/compat.proto b/protocols/gossipsub/src/compat.proto deleted file mode 100644 index b2753bf7e41..00000000000 --- a/protocols/gossipsub/src/compat.proto +++ /dev/null @@ -1,12 +0,0 @@ -syntax = "proto2"; - -package compat.pb; - -message Message { - optional bytes from = 1; - optional bytes data = 2; - optional bytes seqno = 3; - repeated string topic_ids = 4; - optional bytes signature = 5; - optional bytes key = 6; -} \ No newline at end of file diff --git a/protocols/gossipsub/src/compat/mod.rs b/protocols/gossipsub/src/compat/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/gossipsub/src/compat/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/gossipsub/src/compat/pb.rs b/protocols/gossipsub/src/compat/pb.rs deleted file mode 100644 index fd59c38e2b4..00000000000 --- a/protocols/gossipsub/src/compat/pb.rs +++ /dev/null @@ -1,67 +0,0 @@ -// Automatically generated rust module for 'compat.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic_ids: Vec, - pub signature: Option>, - pub key: Option>, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), - Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), - Ok(34) => msg.topic_ids.push(r.read_string(bytes)?.to_owned()), - Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), - Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.topic_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } - for s in &self.topic_ids { w.write_with_tag(34, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - diff --git a/protocols/gossipsub/src/gossipsub/mod.rs b/protocols/gossipsub/src/gossipsub/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/gossipsub/src/gossipsub/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/gossipsub/src/gossipsub/pb.rs b/protocols/gossipsub/src/gossipsub/pb.rs deleted file mode 100644 index 9a074fd61fc..00000000000 --- a/protocols/gossipsub/src/gossipsub/pb.rs +++ /dev/null @@ -1,567 +0,0 @@ -// Automatically generated rust module for 'rpc.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct RPC { - pub subscriptions: Vec, - pub publish: Vec, - pub control: Option, -} - -impl<'a> MessageRead<'a> for RPC { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.subscriptions.push(r.read_message::(bytes)?), - Ok(18) => msg.publish.push(r.read_message::(bytes)?), - Ok(26) => msg.control = Some(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for RPC { - fn get_size(&self) -> usize { - 0 - + self.subscriptions.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.publish.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.control.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - for s in &self.subscriptions { w.write_with_tag(10, |w| w.write_message(s))?; } - for s in &self.publish { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.control { w.write_with_tag(26, |w| w.write_message(s))?; } - Ok(()) - } -} - -pub mod mod_RPC { - -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct SubOpts { - pub subscribe: Option, - pub topic_id: Option, -} - -impl<'a> MessageRead<'a> for SubOpts { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.subscribe = Some(r.read_bool(bytes)?), - Ok(18) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for SubOpts { - fn get_size(&self) -> usize { - 0 - + self.subscribe.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.subscribe { w.write_with_tag(8, |w| w.write_bool(*s))?; } - if let Some(ref s) = self.topic_id { w.write_with_tag(18, |w| w.write_string(&**s))?; } - Ok(()) - } -} - -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub from: Option>, - pub data: Option>, - pub seqno: Option>, - pub topic: String, - pub signature: Option>, - pub key: Option>, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.from = Some(r.read_bytes(bytes)?.to_owned()), - Ok(18) => msg.data = Some(r.read_bytes(bytes)?.to_owned()), - Ok(26) => msg.seqno = Some(r.read_bytes(bytes)?.to_owned()), - Ok(34) => msg.topic = r.read_string(bytes)?.to_owned(), - Ok(42) => msg.signature = Some(r.read_bytes(bytes)?.to_owned()), - Ok(50) => msg.key = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + self.from.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.data.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.seqno.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + 1 + sizeof_len((&self.topic).len()) - + self.signature.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.key.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.from { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.data { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.seqno { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } - w.write_with_tag(34, |w| w.write_string(&**&self.topic))?; - if let Some(ref s) = self.signature { w.write_with_tag(42, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.key { w.write_with_tag(50, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlMessage { - pub ihave: Vec, - pub iwant: Vec, - pub graft: Vec, - pub prune: Vec, -} - -impl<'a> MessageRead<'a> for ControlMessage { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.ihave.push(r.read_message::(bytes)?), - Ok(18) => msg.iwant.push(r.read_message::(bytes)?), - Ok(26) => msg.graft.push(r.read_message::(bytes)?), - Ok(34) => msg.prune.push(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for ControlMessage { - fn get_size(&self) -> usize { - 0 - + self.ihave.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.iwant.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.graft.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.prune.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - for s in &self.ihave { w.write_with_tag(10, |w| w.write_message(s))?; } - for s in &self.iwant { w.write_with_tag(18, |w| w.write_message(s))?; } - for s in &self.graft { w.write_with_tag(26, |w| w.write_message(s))?; } - for s in &self.prune { w.write_with_tag(34, |w| w.write_message(s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlIHave { - pub topic_id: Option, - pub message_ids: Vec>, -} - -impl<'a> MessageRead<'a> for ControlIHave { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), - Ok(18) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for ControlIHave { - fn get_size(&self) -> usize { - 0 - + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } - for s in &self.message_ids { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlIWant { - pub message_ids: Vec>, -} - -impl<'a> MessageRead<'a> for ControlIWant { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.message_ids.push(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for ControlIWant { - fn get_size(&self) -> usize { - 0 - + self.message_ids.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - for s in &self.message_ids { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlGraft { - pub topic_id: Option, -} - -impl<'a> MessageRead<'a> for ControlGraft { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for ControlGraft { - fn get_size(&self) -> usize { - 0 - + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct ControlPrune { - pub topic_id: Option, - pub peers: Vec, - pub backoff: Option, -} - -impl<'a> MessageRead<'a> for ControlPrune { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.topic_id = Some(r.read_string(bytes)?.to_owned()), - Ok(18) => msg.peers.push(r.read_message::(bytes)?), - Ok(24) => msg.backoff = Some(r.read_uint64(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for ControlPrune { - fn get_size(&self) -> usize { - 0 - + self.topic_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.peers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.backoff.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.topic_id { w.write_with_tag(10, |w| w.write_string(&**s))?; } - for s in &self.peers { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.backoff { w.write_with_tag(24, |w| w.write_uint64(*s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct PeerInfo { - pub peer_id: Option>, - pub signed_peer_record: Option>, -} - -impl<'a> MessageRead<'a> for PeerInfo { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.peer_id = Some(r.read_bytes(bytes)?.to_owned()), - Ok(18) => msg.signed_peer_record = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for PeerInfo { - fn get_size(&self) -> usize { - 0 - + self.peer_id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.signed_peer_record.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.peer_id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.signed_peer_record { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct TopicDescriptor { - pub name: Option, - pub auth: Option, - pub enc: Option, -} - -impl<'a> MessageRead<'a> for TopicDescriptor { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.name = Some(r.read_string(bytes)?.to_owned()), - Ok(18) => msg.auth = Some(r.read_message::(bytes)?), - Ok(26) => msg.enc = Some(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for TopicDescriptor { - fn get_size(&self) -> usize { - 0 - + self.name.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.auth.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.enc.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.name { w.write_with_tag(10, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.auth { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.enc { w.write_with_tag(26, |w| w.write_message(s))?; } - Ok(()) - } -} - -pub mod mod_TopicDescriptor { - -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct AuthOpts { - pub mode: Option, - pub keys: Vec>, -} - -impl<'a> MessageRead<'a> for AuthOpts { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.mode = Some(r.read_enum(bytes)?), - Ok(18) => msg.keys.push(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for AuthOpts { - fn get_size(&self) -> usize { - 0 - + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.keys.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } - for s in &self.keys { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -pub mod mod_AuthOpts { - - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum AuthMode { - NONE = 0, - KEY = 1, - WOT = 2, -} - -impl Default for AuthMode { - fn default() -> Self { - AuthMode::NONE - } -} - -impl From for AuthMode { - fn from(i: i32) -> Self { - match i { - 0 => AuthMode::NONE, - 1 => AuthMode::KEY, - 2 => AuthMode::WOT, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for AuthMode { - fn from(s: &'a str) -> Self { - match s { - "NONE" => AuthMode::NONE, - "KEY" => AuthMode::KEY, - "WOT" => AuthMode::WOT, - _ => Self::default(), - } - } -} - -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct EncOpts { - pub mode: Option, - pub key_hashes: Vec>, -} - -impl<'a> MessageRead<'a> for EncOpts { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.mode = Some(r.read_enum(bytes)?), - Ok(18) => msg.key_hashes.push(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for EncOpts { - fn get_size(&self) -> usize { - 0 - + self.mode.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.key_hashes.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.mode { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } - for s in &self.key_hashes { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -pub mod mod_EncOpts { - - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum EncMode { - NONE = 0, - SHAREDKEY = 1, - WOT = 2, -} - -impl Default for EncMode { - fn default() -> Self { - EncMode::NONE - } -} - -impl From for EncMode { - fn from(i: i32) -> Self { - match i { - 0 => EncMode::NONE, - 1 => EncMode::SHAREDKEY, - 2 => EncMode::WOT, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for EncMode { - fn from(s: &'a str) -> Self { - match s { - "NONE" => EncMode::NONE, - "SHAREDKEY" => EncMode::SHAREDKEY, - "WOT" => EncMode::WOT, - _ => Self::default(), - } - } -} - -} - -} - diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 39a65e7455a..5346ce57c73 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -20,6 +20,7 @@ use crate::error::{HandlerError, ValidationError}; use crate::protocol::{GossipsubCodec, ProtocolConfig}; +use crate::rpc_proto::proto; use crate::types::{PeerKind, RawMessage, Rpc}; use asynchronous_codec::Framed; use futures::prelude::*; @@ -67,7 +68,7 @@ pub enum HandlerEvent { #[derive(Debug)] pub enum HandlerIn { /// A gossipsub message to send. - Message(crate::rpc_proto::Rpc), + Message(proto::RPC), /// The peer has joined the mesh. JoinedMesh, /// The peer has left the mesh. @@ -93,7 +94,7 @@ pub struct Handler { inbound_substream: Option, /// Queue of values that we want to send to the remote. - send_queue: SmallVec<[crate::rpc_proto::Rpc; 16]>, + send_queue: SmallVec<[proto::RPC; 16]>, /// Flag indicating that an outbound substream is being established to prevent duplicate /// requests. @@ -149,10 +150,7 @@ enum OutboundSubstreamState { /// Waiting for the user to send a message. The idle state for an outbound substream. WaitingOutput(Framed), /// Waiting to send a message to the remote. - PendingSend( - Framed, - crate::rpc_proto::Rpc, - ), + PendingSend(Framed, proto::RPC), /// Waiting to flush the substream so that the data arrives to the remote. PendingFlush(Framed), /// The substream is being closed. Used by either substream. @@ -251,7 +249,7 @@ impl ConnectionHandler for Handler { type Error = HandlerError; type InboundOpenInfo = (); type InboundProtocol = ProtocolConfig; - type OutboundOpenInfo = crate::rpc_proto::Rpc; + type OutboundOpenInfo = proto::RPC; type OutboundProtocol = ProtocolConfig; fn listen_protocol(&self) -> SubstreamProtocol { diff --git a/protocols/gossipsub/src/mod.rs b/protocols/gossipsub/src/mod.rs deleted file mode 100644 index 7ac564f3c36..00000000000 --- a/protocols/gossipsub/src/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -// Automatically generated mod.rs -pub mod compat; -pub mod gossipsub; diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index 7926c5ff8d2..9a7b0c2a699 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -25,7 +25,7 @@ use crate::topic::TopicHash; use crate::types::{ ControlAction, MessageId, PeerInfo, PeerKind, RawMessage, Rpc, Subscription, SubscriptionAction, }; -use crate::{rpc_proto, Config}; +use crate::{rpc_proto::proto, Config}; use asynchronous_codec::{Decoder, Encoder, Framed}; use byteorder::{BigEndian, ByteOrder}; use bytes::BytesMut; @@ -35,7 +35,7 @@ use libp2p_core::{ identity::PublicKey, InboundUpgrade, OutboundUpgrade, PeerId, ProtocolName, UpgradeInfo, }; use log::{debug, warn}; -use prost::Message as _; +use quick_protobuf::Writer; use std::pin::Pin; use unsigned_varint::codec; @@ -191,7 +191,7 @@ pub struct GossipsubCodec { /// Determines the level of validation performed on incoming messages. validation_mode: ValidationMode, /// The codec to handle common encoding/decoding of protobuf messages - codec: quick_protobuf_codec::Codec, + codec: quick_protobuf_codec::Codec, } impl GossipsubCodec { @@ -206,7 +206,9 @@ impl GossipsubCodec { /// Verifies a gossipsub message. This returns either a success or failure. All errors /// are logged, which prevents error handling in the codec and handler. We simply drop invalid /// messages and log warnings, rather than propagating errors through the codec. - fn verify_signature(message: &rpc_proto::Message) -> bool { + fn verify_signature(message: &proto::Message) -> bool { + use quick_protobuf::MessageWrite; + let from = match message.from.as_ref() { Some(v) => v, None => { @@ -258,10 +260,11 @@ impl GossipsubCodec { let mut message_sig = message.clone(); message_sig.signature = None; message_sig.key = None; - let mut buf = Vec::with_capacity(message_sig.encoded_len()); + let mut buf = Vec::with_capacity(message_sig.get_size()); + let mut writer = Writer::new(&mut buf); message_sig - .encode(&mut buf) - .expect("Buffer has sufficient capacity"); + .write_message(&mut writer) + .expect("Encoding to succeed"); let mut signature_bytes = SIGNING_PREFIX.to_vec(); signature_bytes.extend_from_slice(&buf); public_key.verify(&signature_bytes, signature) @@ -269,7 +272,7 @@ impl GossipsubCodec { } impl Encoder for GossipsubCodec { - type Item = rpc_proto::Rpc; + type Item = proto::RPC; type Error = HandlerError; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), HandlerError> { diff --git a/protocols/gossipsub/src/rpc.proto b/protocols/gossipsub/src/rpc.proto deleted file mode 100644 index 2ce12f3f37f..00000000000 --- a/protocols/gossipsub/src/rpc.proto +++ /dev/null @@ -1,84 +0,0 @@ -syntax = "proto2"; - -package gossipsub.pb; - -message RPC { - repeated SubOpts subscriptions = 1; - repeated Message publish = 2; - - message SubOpts { - optional bool subscribe = 1; // subscribe or unsubscribe - optional string topic_id = 2; - } - - optional ControlMessage control = 3; -} - -message Message { - optional bytes from = 1; - optional bytes data = 2; - optional bytes seqno = 3; - required string topic = 4; - optional bytes signature = 5; - optional bytes key = 6; -} - -message ControlMessage { - repeated ControlIHave ihave = 1; - repeated ControlIWant iwant = 2; - repeated ControlGraft graft = 3; - repeated ControlPrune prune = 4; -} - -message ControlIHave { - optional string topic_id = 1; - repeated bytes message_ids = 2; -} - -message ControlIWant { - repeated bytes message_ids= 1; -} - -message ControlGraft { - optional string topic_id = 1; -} - -message ControlPrune { - optional string topic_id = 1; - repeated PeerInfo peers = 2; // gossipsub v1.1 PX - optional uint64 backoff = 3; // gossipsub v1.1 backoff time (in seconds) -} - -message PeerInfo { - optional bytes peer_id = 1; - optional bytes signed_peer_record = 2; -} - -// topicID = hash(topicDescriptor); (not the topic.name) -message TopicDescriptor { - optional string name = 1; - optional AuthOpts auth = 2; - optional EncOpts enc = 3; - - message AuthOpts { - optional AuthMode mode = 1; - repeated bytes keys = 2; // root keys to trust - - enum AuthMode { - NONE = 0; // no authentication, anyone can publish - KEY = 1; // only messages signed by keys in the topic descriptor are accepted - WOT = 2; // web of trust, certificates can allow publisher set to grow - } - } - - message EncOpts { - optional EncMode mode = 1; - repeated bytes key_hashes = 2; // the hashes of the shared keys used (salted) - - enum EncMode { - NONE = 0; // no encryption, anyone can read - SHAREDKEY = 1; // messages are encrypted with shared key - WOT = 2; // web of trust, certificates can allow publisher set to grow - } - } -} diff --git a/protocols/gossipsub/src/rpc_proto.rs b/protocols/gossipsub/src/rpc_proto.rs index 7b952ef0926..5d198f2f575 100644 --- a/protocols/gossipsub/src/rpc_proto.rs +++ b/protocols/gossipsub/src/rpc_proto.rs @@ -17,27 +17,26 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -#![allow(clippy::derive_partial_eq_without_eq)] -include!(concat!(env!("OUT_DIR"), "/gossipsub.pb.rs")); +pub mod proto { + include!("generated/mod.rs"); + pub use self::gossipsub::pb::{mod_RPC::SubOpts, *}; +} #[cfg(test)] mod test { + use crate::rpc_proto::proto::compat; use crate::IdentTopic as Topic; use libp2p_core::PeerId; - use prost::Message; + use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use rand::Rng; - mod compat_proto { - include!(concat!(env!("OUT_DIR"), "/compat.pb.rs")); - } - #[test] fn test_multi_topic_message_compatibility() { let topic1 = Topic::new("t1").hash(); let topic2 = Topic::new("t2").hash(); - let new_message1 = super::Message { + let new_message1 = super::proto::Message { from: Some(PeerId::random().to_bytes()), data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()), @@ -45,7 +44,7 @@ mod test { signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), }; - let old_message1 = compat_proto::Message { + let old_message1 = compat::pb::Message { from: Some(PeerId::random().to_bytes()), data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()), @@ -53,7 +52,7 @@ mod test { signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), }; - let old_message2 = compat_proto::Message { + let old_message2 = compat::pb::Message { from: Some(PeerId::random().to_bytes()), data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()), @@ -62,22 +61,31 @@ mod test { key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()), }; - let mut new_message1b = Vec::with_capacity(new_message1.encoded_len()); - new_message1.encode(&mut new_message1b).unwrap(); + let mut new_message1b = Vec::with_capacity(new_message1.get_size()); + let mut writer = Writer::new(&mut new_message1b); + new_message1.write_message(&mut writer).unwrap(); - let mut old_message1b = Vec::with_capacity(old_message1.encoded_len()); - old_message1.encode(&mut old_message1b).unwrap(); + let mut old_message1b = Vec::with_capacity(old_message1.get_size()); + let mut writer = Writer::new(&mut old_message1b); + old_message1.write_message(&mut writer).unwrap(); - let mut old_message2b = Vec::with_capacity(old_message2.encoded_len()); - old_message2.encode(&mut old_message2b).unwrap(); + let mut old_message2b = Vec::with_capacity(old_message2.get_size()); + let mut writer = Writer::new(&mut old_message2b); + old_message2.write_message(&mut writer).unwrap(); - let new_message = super::Message::decode(&old_message1b[..]).unwrap(); + let mut reader = BytesReader::from_bytes(&old_message1b[..]); + let new_message = + super::proto::Message::from_reader(&mut reader, &old_message1b[..]).unwrap(); assert_eq!(new_message.topic, topic1.clone().into_string()); - let new_message = super::Message::decode(&old_message2b[..]).unwrap(); + let mut reader = BytesReader::from_bytes(&old_message2b[..]); + let new_message = + super::proto::Message::from_reader(&mut reader, &old_message2b[..]).unwrap(); assert_eq!(new_message.topic, topic2.into_string()); - let old_message = compat_proto::Message::decode(&new_message1b[..]).unwrap(); + let mut reader = BytesReader::from_bytes(&new_message1b[..]); + let old_message = + compat::pb::Message::from_reader(&mut reader, &new_message1b[..]).unwrap(); assert_eq!(old_message.topic_ids, vec![topic1.into_string()]); } } diff --git a/protocols/gossipsub/src/topic.rs b/protocols/gossipsub/src/topic.rs index 342ee8be7ab..48d19f4e968 100644 --- a/protocols/gossipsub/src/topic.rs +++ b/protocols/gossipsub/src/topic.rs @@ -18,10 +18,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::rpc_proto; +use crate::rpc_proto::proto; use base64::encode; use prometheus_client::encoding::EncodeLabelSet; -use prost::Message; +use quick_protobuf::Writer; use sha2::{Digest, Sha256}; use std::fmt; @@ -47,15 +47,18 @@ impl Hasher for Sha256Hash { /// Creates a [`TopicHash`] by SHA256 hashing the topic then base64 encoding the /// hash. fn hash(topic_string: String) -> TopicHash { - let topic_descripter = rpc_proto::TopicDescriptor { + use quick_protobuf::MessageWrite; + + let topic_descripter = proto::TopicDescriptor { name: Some(topic_string), auth: None, enc: None, }; - let mut bytes = Vec::with_capacity(topic_descripter.encoded_len()); + let mut bytes = Vec::with_capacity(topic_descripter.get_size()); + let mut writer = Writer::new(&mut bytes); topic_descripter - .encode(&mut bytes) - .expect("buffer is large enough"); + .write_message(&mut writer) + .expect("Encoding to succeed"); let hash = encode(Sha256::digest(&bytes).as_slice()); TopicHash { hash } } diff --git a/protocols/gossipsub/src/types.rs b/protocols/gossipsub/src/types.rs index 03e6fc05a1d..5a3f54752da 100644 --- a/protocols/gossipsub/src/types.rs +++ b/protocols/gossipsub/src/types.rs @@ -19,15 +19,15 @@ // DEALINGS IN THE SOFTWARE. //! A collection of types using the Gossipsub system. -use crate::rpc_proto; use crate::TopicHash; use libp2p_core::PeerId; use libp2p_swarm::ConnectionId; use prometheus_client::encoding::EncodeLabelValue; -use prost::Message as _; +use quick_protobuf::MessageWrite; use std::fmt; use std::fmt::Debug; +use crate::rpc_proto::proto; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -136,7 +136,7 @@ pub struct RawMessage { impl RawMessage { /// Calculates the encoded length of this message (used for calculating metrics). pub fn raw_protobuf_len(&self) -> usize { - let message = rpc_proto::Message { + let message = proto::Message { from: self.source.map(|m| m.to_bytes()), data: Some(self.data.clone()), seqno: self.sequence_number.map(|s| s.to_be_bytes().to_vec()), @@ -144,7 +144,7 @@ impl RawMessage { signature: self.signature.clone(), key: self.key.clone(), }; - message.encoded_len() + message.get_size() } } @@ -250,19 +250,19 @@ pub struct Rpc { impl Rpc { /// Converts the GossipsubRPC into its protobuf format. // A convenience function to avoid explicitly specifying types. - pub fn into_protobuf(self) -> rpc_proto::Rpc { + pub fn into_protobuf(self) -> proto::RPC { self.into() } } -impl From for rpc_proto::Rpc { +impl From for proto::RPC { /// Converts the RPC into protobuf format. fn from(rpc: Rpc) -> Self { // Messages let mut publish = Vec::new(); for message in rpc.messages.into_iter() { - let message = rpc_proto::Message { + let message = proto::Message { from: message.source.map(|m| m.to_bytes()), data: Some(message.data), seqno: message.sequence_number.map(|s| s.to_be_bytes().to_vec()), @@ -278,14 +278,14 @@ impl From for rpc_proto::Rpc { let subscriptions = rpc .subscriptions .into_iter() - .map(|sub| rpc_proto::rpc::SubOpts { + .map(|sub| proto::SubOpts { subscribe: Some(sub.action == SubscriptionAction::Subscribe), topic_id: Some(sub.topic_hash.into_string()), }) .collect::>(); // control messages - let mut control = rpc_proto::ControlMessage { + let mut control = proto::ControlMessage { ihave: Vec::new(), iwant: Vec::new(), graft: Vec::new(), @@ -301,20 +301,20 @@ impl From for rpc_proto::Rpc { topic_hash, message_ids, } => { - let rpc_ihave = rpc_proto::ControlIHave { + let rpc_ihave = proto::ControlIHave { topic_id: Some(topic_hash.into_string()), message_ids: message_ids.into_iter().map(|msg_id| msg_id.0).collect(), }; control.ihave.push(rpc_ihave); } ControlAction::IWant { message_ids } => { - let rpc_iwant = rpc_proto::ControlIWant { + let rpc_iwant = proto::ControlIWant { message_ids: message_ids.into_iter().map(|msg_id| msg_id.0).collect(), }; control.iwant.push(rpc_iwant); } ControlAction::Graft { topic_hash } => { - let rpc_graft = rpc_proto::ControlGraft { + let rpc_graft = proto::ControlGraft { topic_id: Some(topic_hash.into_string()), }; control.graft.push(rpc_graft); @@ -324,11 +324,11 @@ impl From for rpc_proto::Rpc { peers, backoff, } => { - let rpc_prune = rpc_proto::ControlPrune { + let rpc_prune = proto::ControlPrune { topic_id: Some(topic_hash.into_string()), peers: peers .into_iter() - .map(|info| rpc_proto::PeerInfo { + .map(|info| proto::PeerInfo { peer_id: info.peer_id.map(|id| id.to_bytes()), /// TODO, see https://github.com/libp2p/specs/pull/217 signed_peer_record: None, @@ -341,7 +341,7 @@ impl From for rpc_proto::Rpc { } } - rpc_proto::Rpc { + proto::RPC { subscriptions, publish, control: if empty_control_msg { From fa5fa4d116ba05b769dab70282fb26b234150720 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 4 Feb 2023 19:43:08 -0500 Subject: [PATCH 39/66] Update changelog in identify --- protocols/identify/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index e4ce7498f32..475903a8a41 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -7,7 +7,10 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3208]: https://github.com/libp2p/rust-libp2p/pull/3208 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.41.1 From 86579c3c424e18e1cb866b5eb99f80de899fb95b Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 6 Feb 2023 22:28:52 -0500 Subject: [PATCH 40/66] Remove prost from kad --- protocols/kad/CHANGELOG.md | 3 + protocols/kad/Cargo.toml | 5 +- protocols/kad/build.rs | 23 ---- protocols/kad/src/dht.proto | 89 ------------- protocols/kad/src/dht/mod.rs | 2 - protocols/kad/src/dht/pb.rs | 243 ---------------------------------- protocols/kad/src/lib.rs | 9 +- protocols/kad/src/mod.rs | 2 - protocols/kad/src/protocol.rs | 169 ++++++++++++----------- 9 files changed, 92 insertions(+), 453 deletions(-) delete mode 100644 protocols/kad/build.rs delete mode 100644 protocols/kad/src/dht.proto delete mode 100644 protocols/kad/src/dht/mod.rs delete mode 100644 protocols/kad/src/dht/pb.rs delete mode 100644 protocols/kad/src/mod.rs diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 224ff059b86..cd253c83df8 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -10,8 +10,11 @@ - Bump MSRV to 1.65.0. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3239]: https://github.com/libp2p/rust-libp2p/pull/3239 [PR 3287]: https://github.com/libp2p/rust-libp2p/pull/3287 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.42.1 diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 127719fa6cd..74fc69e269f 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -20,7 +20,7 @@ futures = "0.3.1" log = "0.4" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } -prost = "0.11" +quick-protobuf = "0.8" rand = "0.8" sha2 = "0.10.0" smallvec = "1.6.1" @@ -39,9 +39,6 @@ libp2p-noise = { path = "../../transports/noise" } libp2p-yamux = { path = "../../muxers/yamux" } quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" } -[build-dependencies] -prost-build = "0.11" - [features] serde = ["dep:serde", "bytes/serde"] diff --git a/protocols/kad/build.rs b/protocols/kad/build.rs deleted file mode 100644 index f05e9e03190..00000000000 --- a/protocols/kad/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/dht.proto"], &["src"]).unwrap(); -} diff --git a/protocols/kad/src/dht.proto b/protocols/kad/src/dht.proto deleted file mode 100644 index 4ff2f6e265b..00000000000 --- a/protocols/kad/src/dht.proto +++ /dev/null @@ -1,89 +0,0 @@ -syntax = "proto3"; -package dht.pb; - -// Record represents a dht record that contains a value -// for a key value pair -message Record { - // The key that references this record - bytes key = 1; - - // The actual value this record is storing - bytes value = 2; - - // Note: These fields were removed from the Record message - // hash of the authors public key - //optional string author = 3; - // A PKI signature for the key+value+author - //optional bytes signature = 4; - - // Time the record was received, set by receiver - string timeReceived = 5; - - // The original publisher of the record. - // Currently specific to rust-libp2p. - bytes publisher = 666; - - // The remaining TTL of the record, in seconds. - // Currently specific to rust-libp2p. - uint32 ttl = 777; -}; - -message Message { - enum MessageType { - PUT_VALUE = 0; - GET_VALUE = 1; - ADD_PROVIDER = 2; - GET_PROVIDERS = 3; - FIND_NODE = 4; - PING = 5; - } - - enum ConnectionType { - // sender does not have a connection to peer, and no extra information (default) - NOT_CONNECTED = 0; - - // sender has a live connection to peer - CONNECTED = 1; - - // sender recently connected to peer - CAN_CONNECT = 2; - - // sender recently tried to connect to peer repeatedly but failed to connect - // ("try" here is loose, but this should signal "made strong effort, failed") - CANNOT_CONNECT = 3; - } - - message Peer { - // ID of a given peer. - bytes id = 1; - - // multiaddrs for a given peer - repeated bytes addrs = 2; - - // used to signal the sender's connection capabilities to the peer - ConnectionType connection = 3; - } - - // defines what type of message it is. - MessageType type = 1; - - // defines what coral cluster level this query/response belongs to. - // in case we want to implement coral's cluster rings in the future. - int32 clusterLevelRaw = 10; // NOT USED - - // Used to specify the key associated with this message. - // PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS - bytes key = 2; - - // Used to return a value - // PUT_VALUE, GET_VALUE - Record record = 3; - - // Used to return peers closer to a key in a query - // GET_VALUE, GET_PROVIDERS, FIND_NODE - repeated Peer closerPeers = 8; - - // Used to return Providers - // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS - repeated Peer providerPeers = 9; -} diff --git a/protocols/kad/src/dht/mod.rs b/protocols/kad/src/dht/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/kad/src/dht/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/kad/src/dht/pb.rs b/protocols/kad/src/dht/pb.rs deleted file mode 100644 index 56b819b1a9c..00000000000 --- a/protocols/kad/src/dht/pb.rs +++ /dev/null @@ -1,243 +0,0 @@ -// Automatically generated rust module for 'dht.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Record { - pub key: Vec, - pub value: Vec, - pub timeReceived: String, - pub publisher: Vec, - pub ttl: u32, -} - -impl<'a> MessageRead<'a> for Record { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.key = r.read_bytes(bytes)?.to_owned(), - Ok(18) => msg.value = r.read_bytes(bytes)?.to_owned(), - Ok(42) => msg.timeReceived = r.read_string(bytes)?.to_owned(), - Ok(5330) => msg.publisher = r.read_bytes(bytes)?.to_owned(), - Ok(6216) => msg.ttl = r.read_uint32(bytes)?, - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Record { - fn get_size(&self) -> usize { - 0 - + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } - + if self.value.is_empty() { 0 } else { 1 + sizeof_len((&self.value).len()) } - + if self.timeReceived == String::default() { 0 } else { 1 + sizeof_len((&self.timeReceived).len()) } - + if self.publisher.is_empty() { 0 } else { 2 + sizeof_len((&self.publisher).len()) } - + if self.ttl == 0u32 { 0 } else { 2 + sizeof_varint(*(&self.ttl) as u64) } - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if !self.key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.key))?; } - if !self.value.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.value))?; } - if self.timeReceived != String::default() { w.write_with_tag(42, |w| w.write_string(&**&self.timeReceived))?; } - if !self.publisher.is_empty() { w.write_with_tag(5330, |w| w.write_bytes(&**&self.publisher))?; } - if self.ttl != 0u32 { w.write_with_tag(6216, |w| w.write_uint32(*&self.ttl))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub type_pb: dht::pb::mod_Message::MessageType, - pub clusterLevelRaw: i32, - pub key: Vec, - pub record: Option, - pub closerPeers: Vec, - pub providerPeers: Vec, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.type_pb = r.read_enum(bytes)?, - Ok(80) => msg.clusterLevelRaw = r.read_int32(bytes)?, - Ok(18) => msg.key = r.read_bytes(bytes)?.to_owned(), - Ok(26) => msg.record = Some(r.read_message::(bytes)?), - Ok(66) => msg.closerPeers.push(r.read_message::(bytes)?), - Ok(74) => msg.providerPeers.push(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + if self.type_pb == dht::pb::mod_Message::MessageType::PUT_VALUE { 0 } else { 1 + sizeof_varint(*(&self.type_pb) as u64) } - + if self.clusterLevelRaw == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.clusterLevelRaw) as u64) } - + if self.key.is_empty() { 0 } else { 1 + sizeof_len((&self.key).len()) } - + self.record.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.closerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.providerPeers.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if self.type_pb != dht::pb::mod_Message::MessageType::PUT_VALUE { w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; } - if self.clusterLevelRaw != 0i32 { w.write_with_tag(80, |w| w.write_int32(*&self.clusterLevelRaw))?; } - if !self.key.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.key))?; } - if let Some(ref s) = self.record { w.write_with_tag(26, |w| w.write_message(s))?; } - for s in &self.closerPeers { w.write_with_tag(66, |w| w.write_message(s))?; } - for s in &self.providerPeers { w.write_with_tag(74, |w| w.write_message(s))?; } - Ok(()) - } -} - -pub mod mod_Message { - -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Peer { - pub id: Vec, - pub addrs: Vec>, - pub connection: dht::pb::mod_Message::ConnectionType, -} - -impl<'a> MessageRead<'a> for Peer { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), - Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), - Ok(24) => msg.connection = r.read_enum(bytes)?, - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Peer { - fn get_size(&self) -> usize { - 0 - + if self.id.is_empty() { 0 } else { 1 + sizeof_len((&self.id).len()) } - + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - + if self.connection == dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { 0 } else { 1 + sizeof_varint(*(&self.connection) as u64) } - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if !self.id.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; } - for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if self.connection != dht::pb::mod_Message::ConnectionType::NOT_CONNECTED { w.write_with_tag(24, |w| w.write_enum(*&self.connection as i32))?; } - Ok(()) - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum MessageType { - PUT_VALUE = 0, - GET_VALUE = 1, - ADD_PROVIDER = 2, - GET_PROVIDERS = 3, - FIND_NODE = 4, - PING = 5, -} - -impl Default for MessageType { - fn default() -> Self { - MessageType::PUT_VALUE - } -} - -impl From for MessageType { - fn from(i: i32) -> Self { - match i { - 0 => MessageType::PUT_VALUE, - 1 => MessageType::GET_VALUE, - 2 => MessageType::ADD_PROVIDER, - 3 => MessageType::GET_PROVIDERS, - 4 => MessageType::FIND_NODE, - 5 => MessageType::PING, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for MessageType { - fn from(s: &'a str) -> Self { - match s { - "PUT_VALUE" => MessageType::PUT_VALUE, - "GET_VALUE" => MessageType::GET_VALUE, - "ADD_PROVIDER" => MessageType::ADD_PROVIDER, - "GET_PROVIDERS" => MessageType::GET_PROVIDERS, - "FIND_NODE" => MessageType::FIND_NODE, - "PING" => MessageType::PING, - _ => Self::default(), - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum ConnectionType { - NOT_CONNECTED = 0, - CONNECTED = 1, - CAN_CONNECT = 2, - CANNOT_CONNECT = 3, -} - -impl Default for ConnectionType { - fn default() -> Self { - ConnectionType::NOT_CONNECTED - } -} - -impl From for ConnectionType { - fn from(i: i32) -> Self { - match i { - 0 => ConnectionType::NOT_CONNECTED, - 1 => ConnectionType::CONNECTED, - 2 => ConnectionType::CAN_CONNECT, - 3 => ConnectionType::CANNOT_CONNECT, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for ConnectionType { - fn from(s: &'a str) -> Self { - match s { - "NOT_CONNECTED" => ConnectionType::NOT_CONNECTED, - "CONNECTED" => ConnectionType::CONNECTED, - "CAN_CONNECT" => ConnectionType::CAN_CONNECT, - "CANNOT_CONNECT" => ConnectionType::CANNOT_CONNECT, - _ => Self::default(), - } - } -} - -} - diff --git a/protocols/kad/src/lib.rs b/protocols/kad/src/lib.rs index e712da0c7d3..3e9dcecc0e4 100644 --- a/protocols/kad/src/lib.rs +++ b/protocols/kad/src/lib.rs @@ -48,9 +48,12 @@ mod behaviour; mod jobs; mod query; -#[allow(clippy::derive_partial_eq_without_eq)] -mod dht_proto { - include!(concat!(env!("OUT_DIR"), "/dht.pb.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::dht::pb::{ + mod_Message::{ConnectionType, MessageType, Peer}, + Message, Record, + }; } pub use addresses::Addresses; diff --git a/protocols/kad/src/mod.rs b/protocols/kad/src/mod.rs deleted file mode 100644 index 6ffc566d1a0..00000000000 --- a/protocols/kad/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod dht; diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 3988d08f24d..47d7c33e496 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -26,7 +26,7 @@ //! to poll the underlying transport for incoming messages, and the `Sink` component //! is used to send messages to remote peers. -use crate::dht_proto as proto; +use crate::proto; use crate::record::{self, Record}; use asynchronous_codec::Framed; use bytes::BytesMut; @@ -35,7 +35,7 @@ use futures::prelude::*; use instant::Instant; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_core::{Multiaddr, PeerId}; -use prost::Message; +use quick_protobuf::{BytesReader, Writer}; use std::{borrow::Cow, convert::TryFrom, time::Duration}; use std::{io, iter}; use unsigned_varint::codec; @@ -59,26 +59,26 @@ pub enum KadConnectionType { CannotConnect = 3, } -impl From for KadConnectionType { - fn from(raw: proto::message::ConnectionType) -> KadConnectionType { - use proto::message::ConnectionType::*; +impl From for KadConnectionType { + fn from(raw: proto::ConnectionType) -> KadConnectionType { + use proto::ConnectionType::*; match raw { - NotConnected => KadConnectionType::NotConnected, - Connected => KadConnectionType::Connected, - CanConnect => KadConnectionType::CanConnect, - CannotConnect => KadConnectionType::CannotConnect, + NOT_CONNECTED => KadConnectionType::NotConnected, + CONNECTED => KadConnectionType::Connected, + CAN_CONNECT => KadConnectionType::CanConnect, + CANNOT_CONNECT => KadConnectionType::CannotConnect, } } } -impl From for proto::message::ConnectionType { +impl From for proto::ConnectionType { fn from(val: KadConnectionType) -> Self { - use proto::message::ConnectionType::*; + use proto::ConnectionType::*; match val { - KadConnectionType::NotConnected => NotConnected, - KadConnectionType::Connected => Connected, - KadConnectionType::CanConnect => CanConnect, - KadConnectionType::CannotConnect => CannotConnect, + KadConnectionType::NotConnected => NOT_CONNECTED, + KadConnectionType::Connected => CONNECTED, + KadConnectionType::CanConnect => CAN_CONNECT, + KadConnectionType::CannotConnect => CANNOT_CONNECT, } } } @@ -95,10 +95,10 @@ pub struct KadPeer { } // Builds a `KadPeer` from a corresponding protobuf message. -impl TryFrom for KadPeer { +impl TryFrom for KadPeer { type Error = io::Error; - fn try_from(peer: proto::message::Peer) -> Result { + fn try_from(peer: proto::Peer) -> Result { // TODO: this is in fact a CID; not sure if this should be handled in `from_bytes` or // as a special case here let node_id = PeerId::from_bytes(&peer.id).map_err(|_| invalid_data("invalid peer id"))?; @@ -113,27 +113,20 @@ impl TryFrom for KadPeer { }; } - let connection_ty = proto::message::ConnectionType::from_i32(peer.connection) - .ok_or_else(|| invalid_data("unknown connection type"))? - .into(); - Ok(KadPeer { node_id, multiaddrs: addrs, - connection_ty, + connection_ty: peer.connection.into(), }) } } -impl From for proto::message::Peer { +impl From for proto::Peer { fn from(peer: KadPeer) -> Self { - proto::message::Peer { + proto::Peer { id: peer.node_id.to_bytes(), addrs: peer.multiaddrs.into_iter().map(|a| a.to_vec()).collect(), - connection: { - let ct: proto::message::ConnectionType = peer.connection_ty.into(); - ct as i32 - }, + connection: peer.connection_ty.into(), } } } @@ -195,6 +188,8 @@ where type Error = io::Error; fn upgrade_inbound(self, incoming: C, _: Self::Info) -> Self::Future { + use quick_protobuf::{MessageRead, MessageWrite}; + let mut codec = UviBytes::default(); codec.set_max_len(self.max_packet_size); @@ -203,14 +198,16 @@ where .err_into() .with::<_, _, fn(_) -> _, _>(|response| { let proto_struct = resp_msg_to_proto(response); - let mut buf = Vec::with_capacity(proto_struct.encoded_len()); + let mut buf = Vec::with_capacity(proto_struct.get_size()); + let mut writer = Writer::new(&mut buf); proto_struct - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + .write_message(&mut writer) + .expect("Encoding to succeed"); future::ready(Ok(io::Cursor::new(buf))) }) .and_then::<_, fn(_) -> _>(|bytes| { - let request = match proto::Message::decode(bytes) { + let mut reader = BytesReader::from_bytes(&bytes); + let request = match proto::Message::from_reader(&mut reader, &bytes) { Ok(r) => r, Err(err) => return future::ready(Err(err.into())), }; @@ -229,6 +226,8 @@ where type Error = io::Error; fn upgrade_outbound(self, incoming: C, _: Self::Info) -> Self::Future { + use quick_protobuf::{MessageRead, MessageWrite}; + let mut codec = UviBytes::default(); codec.set_max_len(self.max_packet_size); @@ -237,14 +236,16 @@ where .err_into() .with::<_, _, fn(_) -> _, _>(|request| { let proto_struct = req_msg_to_proto(request); - let mut buf = Vec::with_capacity(proto_struct.encoded_len()); + let mut buf = Vec::with_capacity(proto_struct.get_size()); + let mut writer = Writer::new(&mut buf); proto_struct - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + .write_message(&mut writer) + .expect("Encoding to succeed"); future::ready(Ok(io::Cursor::new(buf))) }) .and_then::<_, fn(_) -> _>(|bytes| { - let response = match proto::Message::decode(bytes) { + let mut reader = BytesReader::from_bytes(&bytes); + let response = match proto::Message::from_reader(&mut reader, &bytes) { Ok(r) => r, Err(err) => return future::ready(Err(err.into())), }; @@ -351,36 +352,36 @@ pub enum KadResponseMsg { fn req_msg_to_proto(kad_msg: KadRequestMsg) -> proto::Message { match kad_msg { KadRequestMsg::Ping => proto::Message { - r#type: proto::message::MessageType::Ping as i32, + type_pb: proto::MessageType::PING, ..proto::Message::default() }, KadRequestMsg::FindNode { key } => proto::Message { - r#type: proto::message::MessageType::FindNode as i32, + type_pb: proto::MessageType::FIND_NODE, key, - cluster_level_raw: 10, + clusterLevelRaw: 10, ..proto::Message::default() }, KadRequestMsg::GetProviders { key } => proto::Message { - r#type: proto::message::MessageType::GetProviders as i32, + type_pb: proto::MessageType::GET_PROVIDERS, key: key.to_vec(), - cluster_level_raw: 10, + clusterLevelRaw: 10, ..proto::Message::default() }, KadRequestMsg::AddProvider { key, provider } => proto::Message { - r#type: proto::message::MessageType::AddProvider as i32, - cluster_level_raw: 10, + type_pb: proto::MessageType::ADD_PROVIDER, + clusterLevelRaw: 10, key: key.to_vec(), - provider_peers: vec![provider.into()], + providerPeers: vec![provider.into()], ..proto::Message::default() }, KadRequestMsg::GetValue { key } => proto::Message { - r#type: proto::message::MessageType::GetValue as i32, - cluster_level_raw: 10, + type_pb: proto::MessageType::GET_VALUE, + clusterLevelRaw: 10, key: key.to_vec(), ..proto::Message::default() }, KadRequestMsg::PutValue { record } => proto::Message { - r#type: proto::message::MessageType::PutValue as i32, + type_pb: proto::MessageType::PUT_VALUE, key: record.key.to_vec(), record: Some(record_to_proto(record)), ..proto::Message::default() @@ -392,37 +393,37 @@ fn req_msg_to_proto(kad_msg: KadRequestMsg) -> proto::Message { fn resp_msg_to_proto(kad_msg: KadResponseMsg) -> proto::Message { match kad_msg { KadResponseMsg::Pong => proto::Message { - r#type: proto::message::MessageType::Ping as i32, + type_pb: proto::MessageType::PING, ..proto::Message::default() }, KadResponseMsg::FindNode { closer_peers } => proto::Message { - r#type: proto::message::MessageType::FindNode as i32, - cluster_level_raw: 9, - closer_peers: closer_peers.into_iter().map(KadPeer::into).collect(), + type_pb: proto::MessageType::FIND_NODE, + clusterLevelRaw: 9, + closerPeers: closer_peers.into_iter().map(KadPeer::into).collect(), ..proto::Message::default() }, KadResponseMsg::GetProviders { closer_peers, provider_peers, } => proto::Message { - r#type: proto::message::MessageType::GetProviders as i32, - cluster_level_raw: 9, - closer_peers: closer_peers.into_iter().map(KadPeer::into).collect(), - provider_peers: provider_peers.into_iter().map(KadPeer::into).collect(), + type_pb: proto::MessageType::GET_PROVIDERS, + clusterLevelRaw: 9, + closerPeers: closer_peers.into_iter().map(KadPeer::into).collect(), + providerPeers: provider_peers.into_iter().map(KadPeer::into).collect(), ..proto::Message::default() }, KadResponseMsg::GetValue { record, closer_peers, } => proto::Message { - r#type: proto::message::MessageType::GetValue as i32, - cluster_level_raw: 9, - closer_peers: closer_peers.into_iter().map(KadPeer::into).collect(), + type_pb: proto::MessageType::GET_VALUE, + clusterLevelRaw: 9, + closerPeers: closer_peers.into_iter().map(KadPeer::into).collect(), record: record.map(record_to_proto), ..proto::Message::default() }, KadResponseMsg::PutValue { key, value } => proto::Message { - r#type: proto::message::MessageType::PutValue as i32, + type_pb: proto::MessageType::PUT_VALUE, key: key.to_vec(), record: Some(proto::Record { key: key.to_vec(), @@ -438,28 +439,25 @@ fn resp_msg_to_proto(kad_msg: KadResponseMsg) -> proto::Message { /// /// Fails if the protobuf message is not a valid and supported Kademlia request message. fn proto_to_req_msg(message: proto::Message) -> Result { - let msg_type = proto::message::MessageType::from_i32(message.r#type) - .ok_or_else(|| invalid_data(format!("unknown message type: {}", message.r#type)))?; - - match msg_type { - proto::message::MessageType::Ping => Ok(KadRequestMsg::Ping), - proto::message::MessageType::PutValue => { + match message.type_pb { + proto::MessageType::PING => Ok(KadRequestMsg::Ping), + proto::MessageType::PUT_VALUE => { let record = record_from_proto(message.record.unwrap_or_default())?; Ok(KadRequestMsg::PutValue { record }) } - proto::message::MessageType::GetValue => Ok(KadRequestMsg::GetValue { + proto::MessageType::GET_VALUE => Ok(KadRequestMsg::GetValue { key: record::Key::from(message.key), }), - proto::message::MessageType::FindNode => Ok(KadRequestMsg::FindNode { key: message.key }), - proto::message::MessageType::GetProviders => Ok(KadRequestMsg::GetProviders { + proto::MessageType::FIND_NODE => Ok(KadRequestMsg::FindNode { key: message.key }), + proto::MessageType::GET_PROVIDERS => Ok(KadRequestMsg::GetProviders { key: record::Key::from(message.key), }), - proto::message::MessageType::AddProvider => { + proto::MessageType::ADD_PROVIDER => { // TODO: for now we don't parse the peer properly, so it is possible that we get // parsing errors for peers even when they are valid; we ignore these // errors for now, but ultimately we should just error altogether let provider = message - .provider_peers + .providerPeers .into_iter() .find_map(|peer| KadPeer::try_from(peer).ok()); @@ -477,12 +475,9 @@ fn proto_to_req_msg(message: proto::Message) -> Result /// /// Fails if the protobuf message is not a valid and supported Kademlia response message. fn proto_to_resp_msg(message: proto::Message) -> Result { - let msg_type = proto::message::MessageType::from_i32(message.r#type) - .ok_or_else(|| invalid_data(format!("unknown message type: {}", message.r#type)))?; - - match msg_type { - proto::message::MessageType::Ping => Ok(KadResponseMsg::Pong), - proto::message::MessageType::GetValue => { + match message.type_pb { + proto::MessageType::PING => Ok(KadResponseMsg::Pong), + proto::MessageType::GET_VALUE => { let record = if let Some(r) = message.record { Some(record_from_proto(r)?) } else { @@ -490,7 +485,7 @@ fn proto_to_resp_msg(message: proto::Message) -> Result Result { + proto::MessageType::FIND_NODE => { let closer_peers = message - .closer_peers + .closerPeers .into_iter() .filter_map(|peer| KadPeer::try_from(peer).ok()) .collect(); @@ -511,15 +506,15 @@ fn proto_to_resp_msg(message: proto::Message) -> Result { + proto::MessageType::GET_PROVIDERS => { let closer_peers = message - .closer_peers + .closerPeers .into_iter() .filter_map(|peer| KadPeer::try_from(peer).ok()) .collect(); let provider_peers = message - .provider_peers + .providerPeers .into_iter() .filter_map(|peer| KadPeer::try_from(peer).ok()) .collect(); @@ -530,7 +525,7 @@ fn proto_to_resp_msg(message: proto::Message) -> Result { + proto::MessageType::PUT_VALUE => { let key = record::Key::from(message.key); let rec = message .record @@ -542,7 +537,7 @@ fn proto_to_resp_msg(message: proto::Message) -> Result { + proto::MessageType::ADD_PROVIDER => { Err(invalid_data("received an unexpected AddProvider message")) } } @@ -590,7 +585,7 @@ fn record_to_proto(record: Record) -> proto::Record { } }) .unwrap_or(0), - time_received: String::new(), + timeReceived: String::new(), } } @@ -617,10 +612,10 @@ mod tests { a }; - let payload = proto::message::Peer { + let payload = proto::Peer { id: PeerId::random().to_bytes(), addrs: vec![valid_multiaddr_bytes, invalid_multiaddr], - connection: proto::message::ConnectionType::CanConnect.into(), + connection: proto::ConnectionType::CAN_CONNECT, }; let peer = KadPeer::try_from(payload).expect("not to fail"); From f6552dabdc6a9eeccdc08d9cf1e255e5e4594e42 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 8 Feb 2023 20:57:27 -0500 Subject: [PATCH 41/66] Clean up relay --- protocols/relay/CHANGELOG.md | 3 + protocols/relay/Cargo.toml | 6 +- protocols/relay/src/message.proto | 59 ----- protocols/relay/src/message_v2/mod.rs | 2 - protocols/relay/src/message_v2/pb.rs | 346 -------------------------- protocols/relay/src/mod.rs | 2 - 6 files changed, 4 insertions(+), 414 deletions(-) delete mode 100644 protocols/relay/src/message.proto delete mode 100644 protocols/relay/src/message_v2/mod.rs delete mode 100644 protocols/relay/src/message_v2/pb.rs delete mode 100644 protocols/relay/src/mod.rs diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 082f829657e..22f533eb5f3 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -11,8 +11,11 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3238]: https://github.com/libp2p/rust-libp2p/pull/3238 [discussion 2174]: https://github.com/libp2p/rust-libp2p/issues/2174 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.14.0 diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 650d57d4556..cdbbd4816bd 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -21,17 +21,13 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" pin-project = "1" +quick-protobuf = "0.8" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } -prost = "0.11" rand = "0.8.4" smallvec = "1.6.1" static_assertions = "1" thiserror = "1.0" void = "1" -quick-protobuf = "0.8.1" - -[build-dependencies] -prost-build = "0.11" [dev-dependencies] clap = { version = "4.0.13", features = ["derive"] } diff --git a/protocols/relay/src/message.proto b/protocols/relay/src/message.proto deleted file mode 100644 index 0a42ce3d773..00000000000 --- a/protocols/relay/src/message.proto +++ /dev/null @@ -1,59 +0,0 @@ -syntax = "proto2"; - -package message_v2.pb; - -message HopMessage { - enum Type { - RESERVE = 0; - CONNECT = 1; - STATUS = 2; - } - - required Type type = 1; - - optional Peer peer = 2; - optional Reservation reservation = 3; - optional Limit limit = 4; - - optional Status status = 5; -} - -message StopMessage { - enum Type { - CONNECT = 0; - STATUS = 1; - } - - required Type type = 1; - - optional Peer peer = 2; - optional Limit limit = 3; - - optional Status status = 4; -} - -message Peer { - required bytes id = 1; - repeated bytes addrs = 2; -} - -message Reservation { - required uint64 expire = 1; // Unix expiration time (UTC) - repeated bytes addrs = 2; // relay addrs for reserving peer - optional bytes voucher = 3; // reservation voucher -} -message Limit { - optional uint32 duration = 1; // seconds - optional uint64 data = 2; // bytes -} - -enum Status { - OK = 100; - RESERVATION_REFUSED = 200; - RESOURCE_LIMIT_EXCEEDED = 201; - PERMISSION_DENIED = 202; - CONNECTION_FAILED = 203; - NO_RESERVATION = 204; - MALFORMED_MESSAGE = 400; - UNEXPECTED_MESSAGE = 401; -} \ No newline at end of file diff --git a/protocols/relay/src/message_v2/mod.rs b/protocols/relay/src/message_v2/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/relay/src/message_v2/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/relay/src/message_v2/pb.rs b/protocols/relay/src/message_v2/pb.rs deleted file mode 100644 index 2344f26b541..00000000000 --- a/protocols/relay/src/message_v2/pb.rs +++ /dev/null @@ -1,346 +0,0 @@ -// Automatically generated rust module for 'message.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum Status { - OK = 100, - RESERVATION_REFUSED = 200, - RESOURCE_LIMIT_EXCEEDED = 201, - PERMISSION_DENIED = 202, - CONNECTION_FAILED = 203, - NO_RESERVATION = 204, - MALFORMED_MESSAGE = 400, - UNEXPECTED_MESSAGE = 401, -} - -impl Default for Status { - fn default() -> Self { - Status::OK - } -} - -impl From for Status { - fn from(i: i32) -> Self { - match i { - 100 => Status::OK, - 200 => Status::RESERVATION_REFUSED, - 201 => Status::RESOURCE_LIMIT_EXCEEDED, - 202 => Status::PERMISSION_DENIED, - 203 => Status::CONNECTION_FAILED, - 204 => Status::NO_RESERVATION, - 400 => Status::MALFORMED_MESSAGE, - 401 => Status::UNEXPECTED_MESSAGE, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for Status { - fn from(s: &'a str) -> Self { - match s { - "OK" => Status::OK, - "RESERVATION_REFUSED" => Status::RESERVATION_REFUSED, - "RESOURCE_LIMIT_EXCEEDED" => Status::RESOURCE_LIMIT_EXCEEDED, - "PERMISSION_DENIED" => Status::PERMISSION_DENIED, - "CONNECTION_FAILED" => Status::CONNECTION_FAILED, - "NO_RESERVATION" => Status::NO_RESERVATION, - "MALFORMED_MESSAGE" => Status::MALFORMED_MESSAGE, - "UNEXPECTED_MESSAGE" => Status::UNEXPECTED_MESSAGE, - _ => Self::default(), - } - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct HopMessage { - pub type_pb: message_v2::pb::mod_HopMessage::Type, - pub peer: Option, - pub reservation: Option, - pub limit: Option, - pub status: Option, -} - -impl<'a> MessageRead<'a> for HopMessage { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.type_pb = r.read_enum(bytes)?, - Ok(18) => msg.peer = Some(r.read_message::(bytes)?), - Ok(26) => msg.reservation = Some(r.read_message::(bytes)?), - Ok(34) => msg.limit = Some(r.read_message::(bytes)?), - Ok(40) => msg.status = Some(r.read_enum(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for HopMessage { - fn get_size(&self) -> usize { - 0 - + 1 + sizeof_varint(*(&self.type_pb) as u64) - + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.reservation.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; - if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.reservation { w.write_with_tag(26, |w| w.write_message(s))?; } - if let Some(ref s) = self.limit { w.write_with_tag(34, |w| w.write_message(s))?; } - if let Some(ref s) = self.status { w.write_with_tag(40, |w| w.write_enum(*s as i32))?; } - Ok(()) - } -} - -pub mod mod_HopMessage { - - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum Type { - RESERVE = 0, - CONNECT = 1, - STATUS = 2, -} - -impl Default for Type { - fn default() -> Self { - Type::RESERVE - } -} - -impl From for Type { - fn from(i: i32) -> Self { - match i { - 0 => Type::RESERVE, - 1 => Type::CONNECT, - 2 => Type::STATUS, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for Type { - fn from(s: &'a str) -> Self { - match s { - "RESERVE" => Type::RESERVE, - "CONNECT" => Type::CONNECT, - "STATUS" => Type::STATUS, - _ => Self::default(), - } - } -} - -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct StopMessage { - pub type_pb: message_v2::pb::mod_StopMessage::Type, - pub peer: Option, - pub limit: Option, - pub status: Option, -} - -impl<'a> MessageRead<'a> for StopMessage { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.type_pb = r.read_enum(bytes)?, - Ok(18) => msg.peer = Some(r.read_message::(bytes)?), - Ok(26) => msg.limit = Some(r.read_message::(bytes)?), - Ok(32) => msg.status = Some(r.read_enum(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for StopMessage { - fn get_size(&self) -> usize { - 0 - + 1 + sizeof_varint(*(&self.type_pb) as u64) - + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?; - if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.limit { w.write_with_tag(26, |w| w.write_message(s))?; } - if let Some(ref s) = self.status { w.write_with_tag(32, |w| w.write_enum(*s as i32))?; } - Ok(()) - } -} - -pub mod mod_StopMessage { - - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum Type { - CONNECT = 0, - STATUS = 1, -} - -impl Default for Type { - fn default() -> Self { - Type::CONNECT - } -} - -impl From for Type { - fn from(i: i32) -> Self { - match i { - 0 => Type::CONNECT, - 1 => Type::STATUS, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for Type { - fn from(s: &'a str) -> Self { - match s { - "CONNECT" => Type::CONNECT, - "STATUS" => Type::STATUS, - _ => Self::default(), - } - } -} - -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Peer { - pub id: Vec, - pub addrs: Vec>, -} - -impl<'a> MessageRead<'a> for Peer { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(), - Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Peer { - fn get_size(&self) -> usize { - 0 - + 1 + sizeof_len((&self.id).len()) - + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?; - for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Reservation { - pub expire: u64, - pub addrs: Vec>, - pub voucher: Option>, -} - -impl<'a> MessageRead<'a> for Reservation { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.expire = r.read_uint64(bytes)?, - Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), - Ok(26) => msg.voucher = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Reservation { - fn get_size(&self) -> usize { - 0 - + 1 + sizeof_varint(*(&self.expire) as u64) - + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() - + self.voucher.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - w.write_with_tag(8, |w| w.write_uint64(*&self.expire))?; - for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.voucher { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Limit { - pub duration: Option, - pub data: Option, -} - -impl<'a> MessageRead<'a> for Limit { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.duration = Some(r.read_uint32(bytes)?), - Ok(16) => msg.data = Some(r.read_uint64(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Limit { - fn get_size(&self) -> usize { - 0 - + self.duration.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.data.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.duration { w.write_with_tag(8, |w| w.write_uint32(*s))?; } - if let Some(ref s) = self.data { w.write_with_tag(16, |w| w.write_uint64(*s))?; } - Ok(()) - } -} - diff --git a/protocols/relay/src/mod.rs b/protocols/relay/src/mod.rs deleted file mode 100644 index 18bb20c7eb0..00000000000 --- a/protocols/relay/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod message_v2; From 326ddeef4302878c25f94999f9a008e987e1fca0 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 8 Feb 2023 21:27:54 -0500 Subject: [PATCH 42/66] Remove prost from rendezvous --- protocols/rendezvous/CHANGELOG.md | 4 + protocols/rendezvous/Cargo.toml | 5 +- protocols/rendezvous/build.rs | 3 - protocols/rendezvous/src/codec.rs | 224 ++++++------- protocols/rendezvous/src/mod.rs | 2 - protocols/rendezvous/src/rendezvous/mod.rs | 2 - protocols/rendezvous/src/rendezvous/pb.rs | 364 --------------------- protocols/rendezvous/src/rpc.proto | 61 ---- 8 files changed, 109 insertions(+), 556 deletions(-) delete mode 100644 protocols/rendezvous/build.rs delete mode 100644 protocols/rendezvous/src/mod.rs delete mode 100644 protocols/rendezvous/src/rendezvous/mod.rs delete mode 100644 protocols/rendezvous/src/rendezvous/pb.rs delete mode 100644 protocols/rendezvous/src/rpc.proto diff --git a/protocols/rendezvous/CHANGELOG.md b/protocols/rendezvous/CHANGELOG.md index d16191b9b06..db88cb0bcf6 100644 --- a/protocols/rendezvous/CHANGELOG.md +++ b/protocols/rendezvous/CHANGELOG.md @@ -4,6 +4,10 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 + # 0.11.0 - De- and encode protobuf messages using `prost-codec`. See [PR 3058]. diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index 9be32554df1..0955ded41cd 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -19,7 +19,7 @@ instant = "0.1.11" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.0", path = "../../swarm" } log = "0.4" -prost = "0.11" +quick-protobuf = "0.8" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" sha2 = "0.10" @@ -40,9 +40,6 @@ libp2p-tcp = { path = "../../transports/tcp", features = ["tokio"] } rand = "0.8" tokio = { version = "1.15", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] } -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/protocols/rendezvous/build.rs b/protocols/rendezvous/build.rs deleted file mode 100644 index fa982fa3d90..00000000000 --- a/protocols/rendezvous/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - prost_build::compile_protos(&["src/rpc.proto"], &["src"]).unwrap(); -} diff --git a/protocols/rendezvous/src/codec.rs b/protocols/rendezvous/src/codec.rs index d28a01f77b1..6a1e9865f29 100644 --- a/protocols/rendezvous/src/codec.rs +++ b/protocols/rendezvous/src/codec.rs @@ -200,7 +200,7 @@ pub enum ErrorCode { } pub struct RendezvousCodec { - inner: quick_protobuf_codec::Codec, + inner: quick_protobuf_codec::Codec, } impl Default for RendezvousCodec { @@ -216,7 +216,7 @@ impl Encoder for RendezvousCodec { type Error = Error; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { - self.inner.encode(wire::Message::from(item), dst)?; + self.inner.encode(proto::Message::from(item), dst)?; Ok(()) } @@ -246,133 +246,127 @@ pub enum Error { Conversion(#[from] ConversionError), } -impl From for wire::Message { +impl From for proto::Message { fn from(message: Message) -> Self { - use wire::message::*; - match message { Message::Register(NewRegistration { namespace, record, ttl, - }) => wire::Message { - r#type: Some(MessageType::Register.into()), - register: Some(Register { + }) => proto::Message { + type_pb: Some(proto::MessageType::REGISTER), + register: Some(proto::Register { ns: Some(namespace.into()), ttl, - signed_peer_record: Some( - record.into_signed_envelope().into_protobuf_encoding(), - ), + signedPeerRecord: Some(record.into_signed_envelope().into_protobuf_encoding()), }), - register_response: None, + registerResponse: None, unregister: None, discover: None, - discover_response: None, + discoverResponse: None, }, - Message::RegisterResponse(Ok(ttl)) => wire::Message { - r#type: Some(MessageType::RegisterResponse.into()), - register_response: Some(RegisterResponse { - status: Some(ResponseStatus::Ok.into()), - status_text: None, + Message::RegisterResponse(Ok(ttl)) => proto::Message { + type_pb: Some(proto::MessageType::REGISTER_RESPONSE), + registerResponse: Some(proto::RegisterResponse { + status: Some(proto::ResponseStatus::OK), + statusText: None, ttl: Some(ttl), }), register: None, discover: None, unregister: None, - discover_response: None, + discoverResponse: None, }, - Message::RegisterResponse(Err(error)) => wire::Message { - r#type: Some(MessageType::RegisterResponse.into()), - register_response: Some(RegisterResponse { - status: Some(ResponseStatus::from(error).into()), - status_text: None, + Message::RegisterResponse(Err(error)) => proto::Message { + type_pb: Some(proto::MessageType::REGISTER_RESPONSE), + registerResponse: Some(proto::RegisterResponse { + status: Some(proto::ResponseStatus::from(error).into()), + statusText: None, ttl: None, }), register: None, discover: None, unregister: None, - discover_response: None, + discoverResponse: None, }, - Message::Unregister(namespace) => wire::Message { - r#type: Some(MessageType::Unregister.into()), - unregister: Some(Unregister { + Message::Unregister(namespace) => proto::Message { + type_pb: Some(proto::MessageType::UNREGISTER), + unregister: Some(proto::Unregister { ns: Some(namespace.into()), id: None, }), register: None, - register_response: None, + registerResponse: None, discover: None, - discover_response: None, + discoverResponse: None, }, Message::Discover { namespace, cookie, limit, - } => wire::Message { - r#type: Some(MessageType::Discover.into()), - discover: Some(Discover { + } => proto::Message { + type_pb: Some(proto::MessageType::DISCOVER), + discover: Some(proto::Discover { ns: namespace.map(|ns| ns.into()), cookie: cookie.map(|cookie| cookie.into_wire_encoding()), limit, }), register: None, - register_response: None, + registerResponse: None, unregister: None, - discover_response: None, + discoverResponse: None, }, - Message::DiscoverResponse(Ok((registrations, cookie))) => wire::Message { - r#type: Some(MessageType::DiscoverResponse.into()), - discover_response: Some(DiscoverResponse { + Message::DiscoverResponse(Ok((registrations, cookie))) => proto::Message { + type_pb: Some(proto::MessageType::DISCOVER_RESPONSE), + discoverResponse: Some(proto::DiscoverResponse { registrations: registrations .into_iter() - .map(|reggo| Register { + .map(|reggo| proto::Register { ns: Some(reggo.namespace.into()), ttl: Some(reggo.ttl), - signed_peer_record: Some( + signedPeerRecord: Some( reggo.record.into_signed_envelope().into_protobuf_encoding(), ), }) .collect(), - status: Some(ResponseStatus::Ok.into()), - status_text: None, + status: Some(proto::ResponseStatus::OK), + statusText: None, cookie: Some(cookie.into_wire_encoding()), }), register: None, discover: None, unregister: None, - register_response: None, + registerResponse: None, }, - Message::DiscoverResponse(Err(error)) => wire::Message { - r#type: Some(MessageType::DiscoverResponse.into()), - discover_response: Some(DiscoverResponse { + Message::DiscoverResponse(Err(error)) => proto::Message { + type_pb: Some(proto::MessageType::DISCOVER_RESPONSE), + discoverResponse: Some(proto::DiscoverResponse { registrations: Vec::new(), - status: Some(ResponseStatus::from(error).into()), - status_text: None, + status: Some(proto::ResponseStatus::from(error).into()), + statusText: None, cookie: None, }), register: None, discover: None, unregister: None, - register_response: None, + registerResponse: None, }, } } } -impl TryFrom for Message { +impl TryFrom for Message { type Error = ConversionError; - fn try_from(message: wire::Message) -> Result { - use wire::message::*; - + fn try_from(message: proto::Message) -> Result { let message = match message { - wire::Message { - r#type: Some(0), + proto::Message { + type_pb: Some(proto::MessageType::REGISTER), register: - Some(Register { + Some(proto::Register { ns, ttl, - signed_peer_record: Some(signed_peer_record), + signedPeerRecord: Some(signed_peer_record), }), .. } => Message::Register(NewRegistration { @@ -385,31 +379,31 @@ impl TryFrom for Message { &signed_peer_record, )?)?, }), - wire::Message { - r#type: Some(1), - register_response: - Some(RegisterResponse { - status: Some(0), + proto::Message { + type_pb: Some(proto::MessageType::REGISTER_RESPONSE), + registerResponse: + Some(proto::RegisterResponse { + status: Some(proto::ResponseStatus::OK), ttl, .. }), .. } => Message::RegisterResponse(Ok(ttl.ok_or(ConversionError::MissingTtl)?)), - wire::Message { - r#type: Some(3), - discover: Some(Discover { ns, limit, cookie }), + proto::Message { + type_pb: Some(proto::MessageType::DISCOVER), + discover: Some(proto::Discover { ns, limit, cookie }), .. } => Message::Discover { namespace: ns.map(Namespace::new).transpose()?, cookie: cookie.map(Cookie::from_wire_encoding).transpose()?, limit, }, - wire::Message { - r#type: Some(4), - discover_response: - Some(DiscoverResponse { + proto::Message { + type_pb: Some(proto::MessageType::DISCOVER_RESPONSE), + discoverResponse: + Some(proto::DiscoverResponse { registrations, - status: Some(0), + status: Some(proto::ResponseStatus::OK), cookie: Some(cookie), .. }), @@ -427,7 +421,7 @@ impl TryFrom for Message { record: PeerRecord::from_signed_envelope( SignedEnvelope::from_protobuf_encoding( ®go - .signed_peer_record + .signedPeerRecord .ok_or(ConversionError::MissingSignedPeerRecord)?, )?, )?, @@ -439,43 +433,33 @@ impl TryFrom for Message { Message::DiscoverResponse(Ok((registrations, cookie))) } - wire::Message { - r#type: Some(1), - register_response: - Some(RegisterResponse { - status: Some(error_code), + proto::Message { + type_pb: Some(proto::MessageType::REGISTER_RESPONSE), + registerResponse: + Some(proto::RegisterResponse { + status: Some(response_status), .. }), .. - } => { - let error_code = wire::message::ResponseStatus::from_i32(error_code) - .ok_or(ConversionError::BadStatusCode)? - .try_into()?; - Message::RegisterResponse(Err(error_code)) - } - wire::Message { - r#type: Some(2), - unregister: Some(Unregister { ns, .. }), + } => Message::RegisterResponse(Err(response_status.try_into()?)), + proto::Message { + type_pb: Some(proto::MessageType::UNREGISTER), + unregister: Some(proto::Unregister { ns, .. }), .. } => Message::Unregister( ns.map(Namespace::new) .transpose()? .ok_or(ConversionError::MissingNamespace)?, ), - wire::Message { - r#type: Some(4), - discover_response: - Some(DiscoverResponse { - status: Some(error_code), + proto::Message { + type_pb: Some(proto::MessageType::DISCOVER_RESPONSE), + discoverResponse: + Some(proto::DiscoverResponse { + status: Some(response_status), .. }), .. - } => { - let error = wire::message::ResponseStatus::from_i32(error_code) - .ok_or(ConversionError::BadStatusCode)? - .try_into()?; - Message::DiscoverResponse(Err(error)) - } + } => Message::DiscoverResponse(Err(response_status.try_into()?)), _ => return Err(ConversionError::InconsistentWireMessage), }; @@ -527,39 +511,39 @@ impl ConversionError { } } -impl TryFrom for ErrorCode { +impl TryFrom for ErrorCode { type Error = UnmappableStatusCode; - fn try_from(value: wire::message::ResponseStatus) -> Result { - use wire::message::ResponseStatus::*; + fn try_from(value: proto::ResponseStatus) -> Result { + use proto::ResponseStatus::*; let code = match value { - Ok => return Err(UnmappableStatusCode(value)), - EInvalidNamespace => ErrorCode::InvalidNamespace, - EInvalidSignedPeerRecord => ErrorCode::InvalidSignedPeerRecord, - EInvalidTtl => ErrorCode::InvalidTtl, - EInvalidCookie => ErrorCode::InvalidCookie, - ENotAuthorized => ErrorCode::NotAuthorized, - EInternalError => ErrorCode::InternalError, - EUnavailable => ErrorCode::Unavailable, + OK => return Err(UnmappableStatusCode(value)), + E_INVALID_NAMESPACE => ErrorCode::InvalidNamespace, + E_INVALID_SIGNED_PEER_RECORD => ErrorCode::InvalidSignedPeerRecord, + E_INVALID_TTL => ErrorCode::InvalidTtl, + E_INVALID_COOKIE => ErrorCode::InvalidCookie, + E_NOT_AUTHORIZED => ErrorCode::NotAuthorized, + E_INTERNAL_ERROR => ErrorCode::InternalError, + E_UNAVAILABLE => ErrorCode::Unavailable, }; Result::Ok(code) } } -impl From for wire::message::ResponseStatus { +impl From for proto::ResponseStatus { fn from(error_code: ErrorCode) -> Self { - use wire::message::ResponseStatus::*; + use proto::ResponseStatus::*; match error_code { - ErrorCode::InvalidNamespace => EInvalidNamespace, - ErrorCode::InvalidSignedPeerRecord => EInvalidSignedPeerRecord, - ErrorCode::InvalidTtl => EInvalidTtl, - ErrorCode::InvalidCookie => EInvalidCookie, - ErrorCode::NotAuthorized => ENotAuthorized, - ErrorCode::InternalError => EInternalError, - ErrorCode::Unavailable => EUnavailable, + ErrorCode::InvalidNamespace => E_INVALID_NAMESPACE, + ErrorCode::InvalidSignedPeerRecord => E_INVALID_SIGNED_PEER_RECORD, + ErrorCode::InvalidTtl => E_INVALID_TTL, + ErrorCode::InvalidCookie => E_INVALID_COOKIE, + ErrorCode::NotAuthorized => E_NOT_AUTHORIZED, + ErrorCode::InternalError => E_INTERNAL_ERROR, + ErrorCode::Unavailable => E_UNAVAILABLE, } } } @@ -572,11 +556,11 @@ impl From for ConversionError { #[derive(Debug, thiserror::Error)] #[error("The response code ({0:?}) cannot be mapped to our ErrorCode enum")] -pub struct UnmappableStatusCode(wire::message::ResponseStatus); +pub struct UnmappableStatusCode(proto::ResponseStatus); -#[allow(clippy::derive_partial_eq_without_eq)] -mod wire { - include!(concat!(env!("OUT_DIR"), "/rendezvous.pb.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::rendezvous::pb::{mod_Message::*, Message}; } #[cfg(test)] diff --git a/protocols/rendezvous/src/mod.rs b/protocols/rendezvous/src/mod.rs deleted file mode 100644 index 448a0207b2d..00000000000 --- a/protocols/rendezvous/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod rendezvous; diff --git a/protocols/rendezvous/src/rendezvous/mod.rs b/protocols/rendezvous/src/rendezvous/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/protocols/rendezvous/src/rendezvous/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/protocols/rendezvous/src/rendezvous/pb.rs b/protocols/rendezvous/src/rendezvous/pb.rs deleted file mode 100644 index 2419ecf6a37..00000000000 --- a/protocols/rendezvous/src/rendezvous/pb.rs +++ /dev/null @@ -1,364 +0,0 @@ -// Automatically generated rust module for 'rpc.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub type_pb: Option, - pub register: Option, - pub registerResponse: Option, - pub unregister: Option, - pub discover: Option, - pub discoverResponse: Option, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.type_pb = Some(r.read_enum(bytes)?), - Ok(18) => msg.register = Some(r.read_message::(bytes)?), - Ok(26) => msg.registerResponse = Some(r.read_message::(bytes)?), - Ok(34) => msg.unregister = Some(r.read_message::(bytes)?), - Ok(42) => msg.discover = Some(r.read_message::(bytes)?), - Ok(50) => msg.discoverResponse = Some(r.read_message::(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.register.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.registerResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.unregister.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.discover.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - + self.discoverResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.type_pb { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } - if let Some(ref s) = self.register { w.write_with_tag(18, |w| w.write_message(s))?; } - if let Some(ref s) = self.registerResponse { w.write_with_tag(26, |w| w.write_message(s))?; } - if let Some(ref s) = self.unregister { w.write_with_tag(34, |w| w.write_message(s))?; } - if let Some(ref s) = self.discover { w.write_with_tag(42, |w| w.write_message(s))?; } - if let Some(ref s) = self.discoverResponse { w.write_with_tag(50, |w| w.write_message(s))?; } - Ok(()) - } -} - -pub mod mod_Message { - -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Register { - pub ns: Option, - pub signedPeerRecord: Option>, - pub ttl: Option, -} - -impl<'a> MessageRead<'a> for Register { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), - Ok(18) => msg.signedPeerRecord = Some(r.read_bytes(bytes)?.to_owned()), - Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Register { - fn get_size(&self) -> usize { - 0 - + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.signedPeerRecord.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.signedPeerRecord { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct RegisterResponse { - pub status: Option, - pub statusText: Option, - pub ttl: Option, -} - -impl<'a> MessageRead<'a> for RegisterResponse { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.status = Some(r.read_enum(bytes)?), - Ok(18) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), - Ok(24) => msg.ttl = Some(r.read_uint64(bytes)?), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for RegisterResponse { - fn get_size(&self) -> usize { - 0 - + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.ttl.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.status { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } - if let Some(ref s) = self.statusText { w.write_with_tag(18, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.ttl { w.write_with_tag(24, |w| w.write_uint64(*s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Unregister { - pub ns: Option, - pub id: Option>, -} - -impl<'a> MessageRead<'a> for Unregister { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), - Ok(18) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Unregister { - fn get_size(&self) -> usize { - 0 - + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.id { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Discover { - pub ns: Option, - pub limit: Option, - pub cookie: Option>, -} - -impl<'a> MessageRead<'a> for Discover { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.ns = Some(r.read_string(bytes)?.to_owned()), - Ok(16) => msg.limit = Some(r.read_uint64(bytes)?), - Ok(26) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Discover { - fn get_size(&self) -> usize { - 0 - + self.ns.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.limit.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.ns { w.write_with_tag(10, |w| w.write_string(&**s))?; } - if let Some(ref s) = self.limit { w.write_with_tag(16, |w| w.write_uint64(*s))?; } - if let Some(ref s) = self.cookie { w.write_with_tag(26, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct DiscoverResponse { - pub registrations: Vec, - pub cookie: Option>, - pub status: Option, - pub statusText: Option, -} - -impl<'a> MessageRead<'a> for DiscoverResponse { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.registrations.push(r.read_message::(bytes)?), - Ok(18) => msg.cookie = Some(r.read_bytes(bytes)?.to_owned()), - Ok(24) => msg.status = Some(r.read_enum(bytes)?), - Ok(34) => msg.statusText = Some(r.read_string(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for DiscoverResponse { - fn get_size(&self) -> usize { - 0 - + self.registrations.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::() - + self.cookie.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - for s in &self.registrations { w.write_with_tag(10, |w| w.write_message(s))?; } - if let Some(ref s) = self.cookie { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.status { w.write_with_tag(24, |w| w.write_enum(*s as i32))?; } - if let Some(ref s) = self.statusText { w.write_with_tag(34, |w| w.write_string(&**s))?; } - Ok(()) - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum MessageType { - REGISTER = 0, - REGISTER_RESPONSE = 1, - UNREGISTER = 2, - DISCOVER = 3, - DISCOVER_RESPONSE = 4, -} - -impl Default for MessageType { - fn default() -> Self { - MessageType::REGISTER - } -} - -impl From for MessageType { - fn from(i: i32) -> Self { - match i { - 0 => MessageType::REGISTER, - 1 => MessageType::REGISTER_RESPONSE, - 2 => MessageType::UNREGISTER, - 3 => MessageType::DISCOVER, - 4 => MessageType::DISCOVER_RESPONSE, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for MessageType { - fn from(s: &'a str) -> Self { - match s { - "REGISTER" => MessageType::REGISTER, - "REGISTER_RESPONSE" => MessageType::REGISTER_RESPONSE, - "UNREGISTER" => MessageType::UNREGISTER, - "DISCOVER" => MessageType::DISCOVER, - "DISCOVER_RESPONSE" => MessageType::DISCOVER_RESPONSE, - _ => Self::default(), - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum ResponseStatus { - OK = 0, - E_INVALID_NAMESPACE = 100, - E_INVALID_SIGNED_PEER_RECORD = 101, - E_INVALID_TTL = 102, - E_INVALID_COOKIE = 103, - E_NOT_AUTHORIZED = 200, - E_INTERNAL_ERROR = 300, - E_UNAVAILABLE = 400, -} - -impl Default for ResponseStatus { - fn default() -> Self { - ResponseStatus::OK - } -} - -impl From for ResponseStatus { - fn from(i: i32) -> Self { - match i { - 0 => ResponseStatus::OK, - 100 => ResponseStatus::E_INVALID_NAMESPACE, - 101 => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, - 102 => ResponseStatus::E_INVALID_TTL, - 103 => ResponseStatus::E_INVALID_COOKIE, - 200 => ResponseStatus::E_NOT_AUTHORIZED, - 300 => ResponseStatus::E_INTERNAL_ERROR, - 400 => ResponseStatus::E_UNAVAILABLE, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for ResponseStatus { - fn from(s: &'a str) -> Self { - match s { - "OK" => ResponseStatus::OK, - "E_INVALID_NAMESPACE" => ResponseStatus::E_INVALID_NAMESPACE, - "E_INVALID_SIGNED_PEER_RECORD" => ResponseStatus::E_INVALID_SIGNED_PEER_RECORD, - "E_INVALID_TTL" => ResponseStatus::E_INVALID_TTL, - "E_INVALID_COOKIE" => ResponseStatus::E_INVALID_COOKIE, - "E_NOT_AUTHORIZED" => ResponseStatus::E_NOT_AUTHORIZED, - "E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR, - "E_UNAVAILABLE" => ResponseStatus::E_UNAVAILABLE, - _ => Self::default(), - } - } -} - -} - diff --git a/protocols/rendezvous/src/rpc.proto b/protocols/rendezvous/src/rpc.proto deleted file mode 100644 index d4e388ca96e..00000000000 --- a/protocols/rendezvous/src/rpc.proto +++ /dev/null @@ -1,61 +0,0 @@ -syntax = "proto2"; - -package rendezvous.pb; - -message Message { - enum MessageType { - REGISTER = 0; - REGISTER_RESPONSE = 1; - UNREGISTER = 2; - DISCOVER = 3; - DISCOVER_RESPONSE = 4; - } - - enum ResponseStatus { - OK = 0; - E_INVALID_NAMESPACE = 100; - E_INVALID_SIGNED_PEER_RECORD = 101; - E_INVALID_TTL = 102; - E_INVALID_COOKIE = 103; - E_NOT_AUTHORIZED = 200; - E_INTERNAL_ERROR = 300; - E_UNAVAILABLE = 400; - } - - message Register { - optional string ns = 1; - optional bytes signedPeerRecord = 2; - optional uint64 ttl = 3; // in seconds - } - - message RegisterResponse { - optional ResponseStatus status = 1; - optional string statusText = 2; - optional uint64 ttl = 3; // in seconds - } - - message Unregister { - optional string ns = 1; - optional bytes id = 2; - } - - message Discover { - optional string ns = 1; - optional uint64 limit = 2; - optional bytes cookie = 3; - } - - message DiscoverResponse { - repeated Register registrations = 1; - optional bytes cookie = 2; - optional ResponseStatus status = 3; - optional string statusText = 4; - } - - optional MessageType type = 1; - optional Register register = 2; - optional RegisterResponse registerResponse = 3; - optional Unregister unregister = 4; - optional Discover discover = 5; - optional DiscoverResponse discoverResponse = 6; -} From 9ea8ce4469fb2265430566ebda687eb895e8639d Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 18:42:28 -0500 Subject: [PATCH 43/66] Remove prost from noise --- transports/noise/CHANGELOG.md | 3 ++ transports/noise/Cargo.toml | 5 +-- transports/noise/build.rs | 23 ----------- transports/noise/src/io/handshake.rs | 38 ++++++++++--------- .../noise/src/io/handshake/payload.proto | 11 ------ transports/noise/src/lib.rs | 6 +-- 6 files changed, 27 insertions(+), 59 deletions(-) delete mode 100644 transports/noise/build.rs delete mode 100644 transports/noise/src/io/handshake/payload.proto diff --git a/transports/noise/CHANGELOG.md b/transports/noise/CHANGELOG.md index e966d6eee20..66db66c2df5 100644 --- a/transports/noise/CHANGELOG.md +++ b/transports/noise/CHANGELOG.md @@ -6,8 +6,11 @@ - Deprecate `LegacyConfig` without replacement. See [PR 3265]. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + [PR 3227]: https://github.com/libp2p/rust-libp2p/pull/3227 [PR 3265]: https://github.com/libp2p/rust-libp2p/pull/3265 +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.41.0 diff --git a/transports/noise/Cargo.toml b/transports/noise/Cargo.toml index 69ae3c077b3..aaf78e2e660 100644 --- a/transports/noise/Cargo.toml +++ b/transports/noise/Cargo.toml @@ -15,7 +15,7 @@ futures = "0.3.1" libp2p-core = { version = "0.39.0", path = "../../core" } log = "0.4" once_cell = "1.16.0" -prost = "0.11" +quick-protobuf = "0.8" rand = "0.8.3" sha2 = "0.10.0" static_assertions = "1" @@ -37,9 +37,6 @@ libp2p-tcp = { path = "../tcp", features = ["async-io"] } libsodium-sys-stable = { version = "1.19.22", features = ["fetch-latest"] } quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" } -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/transports/noise/build.rs b/transports/noise/build.rs deleted file mode 100644 index c9cf60412cd..00000000000 --- a/transports/noise/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/io/handshake/payload.proto"], &["src"]).unwrap(); -} diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index cdcea04509a..1a6a8694ca2 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -20,9 +20,9 @@ //! Noise protocol handshake I/O. -#[allow(clippy::derive_partial_eq_without_eq)] -mod payload_proto { - include!(concat!(env!("OUT_DIR"), "/payload.proto.rs")); +mod proto { + include!("../generated/mod.rs"); + pub use self::payload::proto::NoiseHandshakePayload; } use crate::io::{framed::NoiseFramed, NoiseOutput}; @@ -33,7 +33,7 @@ use crate::NoiseError; use bytes::Bytes; use futures::prelude::*; use libp2p_core::identity; -use prost::Message; +use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use std::io; /// The identity of the remote established during a handshake. @@ -178,7 +178,8 @@ where { let msg = recv(state).await?; - let mut pb_result = payload_proto::NoiseHandshakePayload::decode(&msg[..]); + let mut reader = BytesReader::from_bytes(&msg[..]); + let mut pb_result = proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..]); #[allow(deprecated)] if pb_result.is_err() && state.legacy.recv_legacy_handshake { @@ -200,7 +201,8 @@ where // frame length, because each length is encoded as a `u16`. if usize::from(u16::from_be_bytes(buf)) + 2 == msg.len() { log::debug!("Attempting fallback legacy protobuf decoding."); - payload_proto::NoiseHandshakePayload::decode(&msg[2..]) + let mut reader = BytesReader::from_bytes(&msg[2..]); + proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[2..]) } else { Err(e) } @@ -233,7 +235,7 @@ pub async fn send_identity(state: &mut State) -> Result<(), NoiseError> where T: AsyncWrite + Unpin, { - let mut pb = payload_proto::NoiseHandshakePayload { + let mut pb = proto::NoiseHandshakePayload { identity_key: state.identity.public.to_protobuf_encoding(), ..Default::default() }; @@ -244,15 +246,15 @@ where #[allow(deprecated)] let mut msg = if state.legacy.send_legacy_handshake { - let mut msg = Vec::with_capacity(2 + pb.encoded_len()); - msg.extend_from_slice(&(pb.encoded_len() as u16).to_be_bytes()); + let mut msg = Vec::with_capacity(2 + pb.get_size()); + msg.extend_from_slice(&(pb.get_size() as u16).to_be_bytes()); msg } else { - Vec::with_capacity(pb.encoded_len()) + Vec::with_capacity(pb.get_size()) }; - pb.encode(&mut msg) - .expect("Vec provides capacity as needed"); + let mut writer = Writer::new(&mut msg); + pb.write_message(&mut writer).expect("Encoding to succeed"); state.io.send(&msg).await?; Ok(()) @@ -263,7 +265,7 @@ pub async fn send_signature_only(state: &mut State) -> Result<(), NoiseErr where T: AsyncWrite + Unpin, { - let mut pb = payload_proto::NoiseHandshakePayload::default(); + let mut pb = proto::NoiseHandshakePayload::default(); if let Some(ref sig) = state.identity.signature { pb.identity_sig = sig.clone() @@ -271,15 +273,15 @@ where #[allow(deprecated)] let mut msg = if state.legacy.send_legacy_handshake { - let mut msg = Vec::with_capacity(2 + pb.encoded_len()); - msg.extend_from_slice(&(pb.encoded_len() as u16).to_be_bytes()); + let mut msg = Vec::with_capacity(2 + pb.get_size()); + msg.extend_from_slice(&(pb.get_size() as u16).to_be_bytes()); msg } else { - Vec::with_capacity(pb.encoded_len()) + Vec::with_capacity(pb.get_size()) }; - pb.encode(&mut msg) - .expect("Vec provides capacity as needed"); + let mut writer = Writer::new(&mut msg); + pb.write_message(&mut writer).expect("Encoding to succeed"); state.io.send(&msg).await?; Ok(()) diff --git a/transports/noise/src/io/handshake/payload.proto b/transports/noise/src/io/handshake/payload.proto deleted file mode 100644 index 1893dc55037..00000000000 --- a/transports/noise/src/io/handshake/payload.proto +++ /dev/null @@ -1,11 +0,0 @@ -syntax = "proto3"; - -package payload.proto; - -// Payloads for Noise handshake messages. - -message NoiseHandshakePayload { - bytes identity_key = 1; - bytes identity_sig = 2; - bytes data = 3; -} diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index 9e8a9ea21de..2c052d190b1 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -285,10 +285,10 @@ pub enum NoiseError { #[derive(Debug, thiserror::Error)] #[error(transparent)] -pub struct DecodeError(prost::DecodeError); +pub struct DecodeError(quick_protobuf::Error); -impl From for NoiseError { - fn from(e: prost::DecodeError) -> Self { +impl From for NoiseError { + fn from(e: quick_protobuf::Error) -> Self { NoiseError::InvalidPayload(DecodeError(e)) } } From a2f3cd4d0f2059b0e3f3ad89ae45e6334f00bb04 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 18:51:31 -0500 Subject: [PATCH 44/66] Remove prost from plaintext --- transports/plaintext/CHANGELOG.md | 4 ++ transports/plaintext/Cargo.toml | 5 +-- transports/plaintext/build.rs | 23 ------------ transports/plaintext/src/error.rs | 6 +-- transports/plaintext/src/handshake.rs | 14 ++++--- transports/plaintext/src/lib.rs | 6 +-- transports/plaintext/src/mod.rs | 2 - transports/plaintext/src/structs.proto | 8 ---- transports/plaintext/src/structs.rs | 51 -------------------------- 9 files changed, 19 insertions(+), 100 deletions(-) delete mode 100644 transports/plaintext/build.rs delete mode 100644 transports/plaintext/src/mod.rs delete mode 100644 transports/plaintext/src/structs.proto delete mode 100644 transports/plaintext/src/structs.rs diff --git a/transports/plaintext/CHANGELOG.md b/transports/plaintext/CHANGELOG.md index 0254272f843..d8fca4ebeae 100644 --- a/transports/plaintext/CHANGELOG.md +++ b/transports/plaintext/CHANGELOG.md @@ -2,6 +2,10 @@ - Update to `libp2p-core` `v0.39.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 + # 0.38.0 - Add more specific error reporting and remove `prost::Error` from public API. See [PR 3058]. diff --git a/transports/plaintext/Cargo.toml b/transports/plaintext/Cargo.toml index 7e1318e0059..0a0dafc3dd8 100644 --- a/transports/plaintext/Cargo.toml +++ b/transports/plaintext/Cargo.toml @@ -16,7 +16,7 @@ bytes = "1" futures = "0.3.1" libp2p-core = { version = "0.39.0", path = "../../core" } log = "0.4.8" -prost = "0.11" +quick-protobuf = "0.8" unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] } void = "1.0.2" @@ -25,9 +25,6 @@ env_logger = "0.10.0" quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" } rand = "0.8" -[build-dependencies] -prost-build = "0.11" - # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] diff --git a/transports/plaintext/build.rs b/transports/plaintext/build.rs deleted file mode 100644 index 56c7b20121a..00000000000 --- a/transports/plaintext/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/structs.proto"], &["src"]).unwrap(); -} diff --git a/transports/plaintext/src/error.rs b/transports/plaintext/src/error.rs index 3943bb068a0..133cca746af 100644 --- a/transports/plaintext/src/error.rs +++ b/transports/plaintext/src/error.rs @@ -41,7 +41,7 @@ pub enum PlainTextError { } #[derive(Debug)] -pub struct DecodeError(prost::DecodeError); +pub struct DecodeError(quick_protobuf::Error); impl fmt::Display for DecodeError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -87,8 +87,8 @@ impl From for PlainTextError { } } -impl From for PlainTextError { - fn from(err: prost::DecodeError) -> PlainTextError { +impl From for PlainTextError { + fn from(err: quick_protobuf::Error) -> PlainTextError { PlainTextError::InvalidPayload(DecodeError(err)) } } diff --git a/transports/plaintext/src/handshake.rs b/transports/plaintext/src/handshake.rs index 0e17ac5bc46..8ce24f9df22 100644 --- a/transports/plaintext/src/handshake.rs +++ b/transports/plaintext/src/handshake.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::error::PlainTextError; -use crate::structs_proto::Exchange; +use crate::proto::Exchange; use crate::PlainText2Config; use asynchronous_codec::{Framed, FramedParts}; @@ -27,7 +27,7 @@ use bytes::{Bytes, BytesMut}; use futures::prelude::*; use libp2p_core::{PeerId, PublicKey}; use log::{debug, trace}; -use prost::Message; +use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use unsigned_varint::codec::UviBytes; @@ -56,10 +56,11 @@ impl HandshakeContext { id: Some(config.local_public_key.to_peer_id().to_bytes()), pubkey: Some(config.local_public_key.to_protobuf_encoding()), }; - let mut buf = Vec::with_capacity(exchange.encoded_len()); + let mut buf = Vec::with_capacity(exchange.get_size()); + let mut writer = Writer::new(&mut buf); exchange - .encode(&mut buf) - .expect("Vec provides capacity as needed"); + .write_message(&mut writer) + .expect("Encoding to succeed"); Self { config, @@ -73,7 +74,8 @@ impl HandshakeContext { self, exchange_bytes: BytesMut, ) -> Result, PlainTextError> { - let prop = Exchange::decode(exchange_bytes)?; + let mut reader = BytesReader::from_bytes(&exchange_bytes); + let prop = Exchange::from_reader(&mut reader, &exchange_bytes)?; let public_key = PublicKey::from_protobuf_encoding(&prop.pubkey.unwrap_or_default())?; let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?; diff --git a/transports/plaintext/src/lib.rs b/transports/plaintext/src/lib.rs index 6a1d66c2af8..c4a6a2ed011 100644 --- a/transports/plaintext/src/lib.rs +++ b/transports/plaintext/src/lib.rs @@ -39,9 +39,9 @@ use void::Void; mod error; mod handshake; -#[allow(clippy::derive_partial_eq_without_eq)] -mod structs_proto { - include!(concat!(env!("OUT_DIR"), "/structs.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::structs::Exchange; } /// `PlainText1Config` is an insecure connection handshake for testing purposes only. diff --git a/transports/plaintext/src/mod.rs b/transports/plaintext/src/mod.rs deleted file mode 100644 index e52c5a80bc0..00000000000 --- a/transports/plaintext/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod structs; diff --git a/transports/plaintext/src/structs.proto b/transports/plaintext/src/structs.proto deleted file mode 100644 index acabedce459..00000000000 --- a/transports/plaintext/src/structs.proto +++ /dev/null @@ -1,8 +0,0 @@ -syntax = "proto2"; - -package structs; - -message Exchange { - optional bytes id = 1; - optional bytes pubkey = 2; -} diff --git a/transports/plaintext/src/structs.rs b/transports/plaintext/src/structs.rs deleted file mode 100644 index aa4d94f35dd..00000000000 --- a/transports/plaintext/src/structs.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Automatically generated rust module for 'structs.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Exchange { - pub id: Option>, - pub pubkey: Option>, -} - -impl<'a> MessageRead<'a> for Exchange { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(10) => msg.id = Some(r.read_bytes(bytes)?.to_owned()), - Ok(18) => msg.pubkey = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Exchange { - fn get_size(&self) -> usize { - 0 - + self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - + self.pubkey.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } - if let Some(ref s) = self.pubkey { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - From dad962add089744a057711f0ddb43c3b9d5dc0d7 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 19:02:57 -0500 Subject: [PATCH 45/66] Remove prost from webrtc --- transports/webrtc/CHANGELOG.md | 4 + transports/webrtc/Cargo.toml | 5 +- transports/webrtc/build.rs | 23 ----- transports/webrtc/src/lib.rs | 7 +- transports/webrtc/src/message.proto | 20 ---- transports/webrtc/src/mod.rs | 2 - transports/webrtc/src/tokio/substream.rs | 29 +++--- .../src/tokio/substream/drop_listener.rs | 2 +- .../webrtc/src/tokio/substream/framed_dc.rs | 2 +- .../webrtc/src/tokio/substream/state.rs | 2 +- transports/webrtc/src/webrtc/mod.rs | 2 - transports/webrtc/src/webrtc/pb.rs | 91 ------------------- 12 files changed, 22 insertions(+), 167 deletions(-) delete mode 100644 transports/webrtc/build.rs delete mode 100644 transports/webrtc/src/message.proto delete mode 100644 transports/webrtc/src/mod.rs delete mode 100644 transports/webrtc/src/webrtc/mod.rs delete mode 100644 transports/webrtc/src/webrtc/pb.rs diff --git a/transports/webrtc/CHANGELOG.md b/transports/webrtc/CHANGELOG.md index b5c4829725a..8e086dc2276 100644 --- a/transports/webrtc/CHANGELOG.md +++ b/transports/webrtc/CHANGELOG.md @@ -4,6 +4,10 @@ - Update to `libp2p-core` `v0.39.0`. +- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. + +[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 + # 0.4.0-alpha - Initial alpha release. \ No newline at end of file diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index b2a132b1d68..762dc8bb359 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -22,7 +22,7 @@ libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-noise = { version = "0.42.0", path = "../../transports/noise" } log = "0.4" multihash = { version = "0.17.0", default-features = false, features = ["sha2"] } -prost = "0.11" +quick-protobuf = "0.8" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } rand = "0.8" rcgen = "0.9.3" @@ -38,9 +38,6 @@ webrtc = { version = "0.6.0", optional = true } tokio = ["dep:tokio", "dep:tokio-util", "dep:webrtc", "if-watch/tokio"] pem = ["webrtc?/pem"] -[build-dependencies] -prost-build = "0.11" - [dev-dependencies] anyhow = "1.0" env_logger = "0.10" diff --git a/transports/webrtc/build.rs b/transports/webrtc/build.rs deleted file mode 100644 index 3f582337a68..00000000000 --- a/transports/webrtc/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2022 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -fn main() { - prost_build::compile_protos(&["src/message.proto"], &["src"]).unwrap(); -} diff --git a/transports/webrtc/src/lib.rs b/transports/webrtc/src/lib.rs index 3210ad38cac..95bd0ac6d7e 100644 --- a/transports/webrtc/src/lib.rs +++ b/transports/webrtc/src/lib.rs @@ -80,10 +80,9 @@ //! is to make the hash a part of the remote's multiaddr. On the server side, we turn //! certificate verification off. -mod message_proto { - #![allow(clippy::derive_partial_eq_without_eq)] - - include!(concat!(env!("OUT_DIR"), "/webrtc.pb.rs")); +mod proto { + include!("generated/mod.rs"); + pub use self::webrtc::pb::{mod_Message::Flag, Message}; } #[cfg(feature = "tokio")] diff --git a/transports/webrtc/src/message.proto b/transports/webrtc/src/message.proto deleted file mode 100644 index eab3ceb720b..00000000000 --- a/transports/webrtc/src/message.proto +++ /dev/null @@ -1,20 +0,0 @@ -syntax = "proto2"; - -package webrtc.pb; - -message Message { - enum Flag { - // The sender will no longer send messages on the stream. - FIN = 0; - // The sender will no longer read messages on the stream. Incoming data is - // being discarded on receipt. - STOP_SENDING = 1; - // The sender abruptly terminates the sending part of the stream. The - // receiver can discard any data that it already received on that stream. - RESET = 2; - } - - optional Flag flag=1; - - optional bytes message = 2; -} diff --git a/transports/webrtc/src/mod.rs b/transports/webrtc/src/mod.rs deleted file mode 100644 index 5e9f6373b12..00000000000 --- a/transports/webrtc/src/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod webrtc; diff --git a/transports/webrtc/src/tokio/substream.rs b/transports/webrtc/src/tokio/substream.rs index eadaee4d989..3acfe65a873 100644 --- a/transports/webrtc/src/tokio/substream.rs +++ b/transports/webrtc/src/tokio/substream.rs @@ -31,7 +31,7 @@ use std::{ task::{Context, Poll}, }; -use crate::message_proto::{message::Flag, Message}; +use crate::proto::{Flag, Message}; use crate::tokio::{ substream::drop_listener::GracefullyClosed, substream::framed_dc::FramedDc, @@ -94,7 +94,7 @@ impl Substream { ready!(self.io.poll_ready_unpin(cx))?; self.io.start_send_unpin(Message { - flag: Some(Flag::StopSending.into()), + flag: Some(Flag::STOP_SENDING), message: None, })?; self.state.close_read_message_sent(); @@ -150,7 +150,7 @@ impl AsyncRead for Substream { } } None => { - state.handle_inbound_flag(Flag::Fin, read_buffer); + state.handle_inbound_flag(Flag::FIN, read_buffer); return Poll::Ready(Ok(0)); } } @@ -212,7 +212,7 @@ impl AsyncWrite for Substream { ready!(self.io.poll_ready_unpin(cx))?; self.io.start_send_unpin(Message { - flag: Some(Flag::Fin.into()), + flag: Some(Flag::FIN), message: None, })?; self.state.close_write_message_sent(); @@ -245,15 +245,7 @@ fn io_poll_next( .transpose() .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))? { - Some(Message { flag, message }) => { - let flag = flag - .map(|f| { - Flag::from_i32(f).ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "")) - }) - .transpose()?; - - Poll::Ready(Ok(Some((flag, message)))) - } + Some(Message { flag, message }) => Poll::Ready(Ok(Some((flag, message)))), None => Poll::Ready(Ok(None)), } } @@ -263,7 +255,7 @@ mod tests { use super::*; use asynchronous_codec::Encoder; use bytes::BytesMut; - use prost::Message; + use quick_protobuf::{MessageWrite, Writer}; use unsigned_varint::codec::UviBytes; #[test] @@ -271,15 +263,16 @@ mod tests { // Largest possible message. let message = [0; MAX_DATA_LEN]; - let protobuf = crate::message_proto::Message { - flag: Some(crate::message_proto::message::Flag::Fin.into()), + let protobuf = crate::proto::Message { + flag: Some(crate::proto::Flag::FIN), message: Some(message.to_vec()), }; let mut encoded_msg = BytesMut::new(); + let mut writer = Writer::new(&mut encoded_msg); protobuf - .encode(&mut encoded_msg) - .expect("BytesMut to have sufficient capacity."); + .write_message(&mut writer) + .expect("Encoding to succeed"); assert_eq!(encoded_msg.len(), message.len() + PROTO_OVERHEAD); let mut uvi = UviBytes::default(); diff --git a/transports/webrtc/src/tokio/substream/drop_listener.rs b/transports/webrtc/src/tokio/substream/drop_listener.rs index 892d9c876a0..3ce695e421d 100644 --- a/transports/webrtc/src/tokio/substream/drop_listener.rs +++ b/transports/webrtc/src/tokio/substream/drop_listener.rs @@ -27,7 +27,7 @@ use std::io; use std::pin::Pin; use std::task::{Context, Poll}; -use crate::message_proto::{message::Flag, Message}; +use crate::proto::{Flag, Message}; use crate::tokio::substream::framed_dc::FramedDc; #[must_use] diff --git a/transports/webrtc/src/tokio/substream/framed_dc.rs b/transports/webrtc/src/tokio/substream/framed_dc.rs index bd9c2eff373..1bf2b3cf769 100644 --- a/transports/webrtc/src/tokio/substream/framed_dc.rs +++ b/transports/webrtc/src/tokio/substream/framed_dc.rs @@ -26,7 +26,7 @@ use webrtc::data::data_channel::{DataChannel, PollDataChannel}; use std::sync::Arc; use super::{MAX_DATA_LEN, MAX_MSG_LEN, VARINT_LEN}; -use crate::message_proto::Message; +use crate::proto::Message; pub type FramedDc = Framed, quick_protobuf_codec::Codec>; diff --git a/transports/webrtc/src/tokio/substream/state.rs b/transports/webrtc/src/tokio/substream/state.rs index 6a5340fb1f1..cdf04d5dffe 100644 --- a/transports/webrtc/src/tokio/substream/state.rs +++ b/transports/webrtc/src/tokio/substream/state.rs @@ -22,7 +22,7 @@ use bytes::Bytes; use std::io; -use crate::message_proto::message::Flag; +use crate::proto::Flag; #[derive(Debug, Copy, Clone)] pub enum State { diff --git a/transports/webrtc/src/webrtc/mod.rs b/transports/webrtc/src/webrtc/mod.rs deleted file mode 100644 index aec6164c7ef..00000000000 --- a/transports/webrtc/src/webrtc/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Automatically generated mod.rs -pub mod pb; diff --git a/transports/webrtc/src/webrtc/pb.rs b/transports/webrtc/src/webrtc/pb.rs deleted file mode 100644 index 9e33e97188c..00000000000 --- a/transports/webrtc/src/webrtc/pb.rs +++ /dev/null @@ -1,91 +0,0 @@ -// Automatically generated rust module for 'message.proto' file - -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(unused_imports)] -#![allow(unknown_lints)] -#![allow(clippy::all)] -#![cfg_attr(rustfmt, rustfmt_skip)] - - -use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; -use quick_protobuf::sizeofs::*; -use super::super::*; - -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Debug, Default, PartialEq, Clone)] -pub struct Message { - pub flag: Option, - pub message: Option>, -} - -impl<'a> MessageRead<'a> for Message { - fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { - let mut msg = Self::default(); - while !r.is_eof() { - match r.next_tag(bytes) { - Ok(8) => msg.flag = Some(r.read_enum(bytes)?), - Ok(18) => msg.message = Some(r.read_bytes(bytes)?.to_owned()), - Ok(t) => { r.read_unknown(bytes, t)?; } - Err(e) => return Err(e), - } - } - Ok(msg) - } -} - -impl MessageWrite for Message { - fn get_size(&self) -> usize { - 0 - + self.flag.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64)) - + self.message.as_ref().map_or(0, |m| 1 + sizeof_len((m).len())) - } - - fn write_message(&self, w: &mut Writer) -> Result<()> { - if let Some(ref s) = self.flag { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; } - if let Some(ref s) = self.message { w.write_with_tag(18, |w| w.write_bytes(&**s))?; } - Ok(()) - } -} - -pub mod mod_Message { - - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum Flag { - FIN = 0, - STOP_SENDING = 1, - RESET = 2, -} - -impl Default for Flag { - fn default() -> Self { - Flag::FIN - } -} - -impl From for Flag { - fn from(i: i32) -> Self { - match i { - 0 => Flag::FIN, - 1 => Flag::STOP_SENDING, - 2 => Flag::RESET, - _ => Self::default(), - } - } -} - -impl<'a> From<&'a str> for Flag { - fn from(s: &'a str) -> Self { - match s { - "FIN" => Flag::FIN, - "STOP_SENDING" => Flag::STOP_SENDING, - "RESET" => Flag::RESET, - _ => Self::default(), - } - } -} - -} - From 469699a50c5429bcccc78bfe07076e28c9b7f5a4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 19:04:06 -0500 Subject: [PATCH 46/66] Clean up --- protocols/autonat/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/protocols/autonat/src/lib.rs b/protocols/autonat/src/lib.rs index 5a32dc50181..e0fc3e9bc81 100644 --- a/protocols/autonat/src/lib.rs +++ b/protocols/autonat/src/lib.rs @@ -34,7 +34,6 @@ pub use self::{ }; pub use libp2p_request_response::{InboundFailure, OutboundFailure}; -#[allow(clippy::derive_partial_eq_without_eq)] mod proto { include!("generated/mod.rs"); pub use self::structs::{mod_Message::*, Message}; From 02d642b89f4712c90efd18d1dbbfc0d316a3927c Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 19:10:10 -0500 Subject: [PATCH 47/66] Fix cargo check with all features --- transports/webrtc/src/tokio/substream/drop_listener.rs | 2 +- transports/webrtc/src/tokio/substream/state.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/transports/webrtc/src/tokio/substream/drop_listener.rs b/transports/webrtc/src/tokio/substream/drop_listener.rs index 3ce695e421d..3e3dc18a1d6 100644 --- a/transports/webrtc/src/tokio/substream/drop_listener.rs +++ b/transports/webrtc/src/tokio/substream/drop_listener.rs @@ -100,7 +100,7 @@ impl Future for DropListener { State::SendingReset { mut stream } => match stream.poll_ready_unpin(cx)? { Poll::Ready(()) => { stream.start_send_unpin(Message { - flag: Some(Flag::Reset.into()), + flag: Some(Flag::RESET), message: None, })?; *state = State::Flushing { stream }; diff --git a/transports/webrtc/src/tokio/substream/state.rs b/transports/webrtc/src/tokio/substream/state.rs index cdf04d5dffe..2140ec8ea9f 100644 --- a/transports/webrtc/src/tokio/substream/state.rs +++ b/transports/webrtc/src/tokio/substream/state.rs @@ -60,19 +60,19 @@ impl State { let current = *self; match (current, flag) { - (Self::Open, Flag::Fin) => { + (Self::Open, Flag::FIN) => { *self = Self::ReadClosed; } - (Self::WriteClosed, Flag::Fin) => { + (Self::WriteClosed, Flag::FIN) => { *self = Self::BothClosed { reset: false }; } - (Self::Open, Flag::StopSending) => { + (Self::Open, Flag::STOP_SENDING) => { *self = Self::WriteClosed; } - (Self::ReadClosed, Flag::StopSending) => { + (Self::ReadClosed, Flag::STOP_SENDING) => { *self = Self::BothClosed { reset: false }; } - (_, Flag::Reset) => { + (_, Flag::RESET) => { buffer.clear(); *self = Self::BothClosed { reset: true }; } From 929d96c212e244dfbc742555a6e7bf02702d35a4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 21:06:32 -0500 Subject: [PATCH 48/66] Fix webrtc tests --- transports/webrtc/src/tokio/substream.rs | 4 ++-- transports/webrtc/src/tokio/substream/state.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/transports/webrtc/src/tokio/substream.rs b/transports/webrtc/src/tokio/substream.rs index 3acfe65a873..e3f9ef9e074 100644 --- a/transports/webrtc/src/tokio/substream.rs +++ b/transports/webrtc/src/tokio/substream.rs @@ -268,7 +268,7 @@ mod tests { message: Some(message.to_vec()), }; - let mut encoded_msg = BytesMut::new(); + let mut encoded_msg = Vec::new(); let mut writer = Writer::new(&mut encoded_msg); protobuf .write_message(&mut writer) @@ -277,7 +277,7 @@ mod tests { let mut uvi = UviBytes::default(); let mut dst = BytesMut::new(); - uvi.encode(encoded_msg.clone().freeze(), &mut dst).unwrap(); + uvi.encode(encoded_msg.as_slice(), &mut dst).unwrap(); // Ensure the varint prefixed and protobuf encoded largest message is no longer than the // maximum limit specified in the libp2p WebRTC specification. diff --git a/transports/webrtc/src/tokio/substream/state.rs b/transports/webrtc/src/tokio/substream/state.rs index 2140ec8ea9f..a064c424ec3 100644 --- a/transports/webrtc/src/tokio/substream/state.rs +++ b/transports/webrtc/src/tokio/substream/state.rs @@ -333,7 +333,7 @@ mod tests { fn cannot_read_after_receiving_fin() { let mut open = State::Open; - open.handle_inbound_flag(Flag::Fin, &mut Bytes::default()); + open.handle_inbound_flag(Flag::FIN, &mut Bytes::default()); let error = open.read_barrier().unwrap_err(); assert_eq!(error.kind(), ErrorKind::BrokenPipe) @@ -355,7 +355,7 @@ mod tests { fn cannot_write_after_receiving_stop_sending() { let mut open = State::Open; - open.handle_inbound_flag(Flag::StopSending, &mut Bytes::default()); + open.handle_inbound_flag(Flag::STOP_SENDING, &mut Bytes::default()); let error = open.write_barrier().unwrap_err(); assert_eq!(error.kind(), ErrorKind::BrokenPipe) @@ -377,7 +377,7 @@ mod tests { fn everything_broken_after_receiving_reset() { let mut open = State::Open; - open.handle_inbound_flag(Flag::Reset, &mut Bytes::default()); + open.handle_inbound_flag(Flag::RESET, &mut Bytes::default()); let error1 = open.read_barrier().unwrap_err(); let error2 = open.write_barrier().unwrap_err(); let error3 = open.close_write_barrier().unwrap_err(); @@ -393,7 +393,7 @@ mod tests { fn should_read_flags_in_async_write_after_read_closed() { let mut open = State::Open; - open.handle_inbound_flag(Flag::Fin, &mut Bytes::default()); + open.handle_inbound_flag(Flag::FIN, &mut Bytes::default()); assert!(open.read_flags_in_async_write()) } @@ -402,8 +402,8 @@ mod tests { fn cannot_read_or_write_after_receiving_fin_and_stop_sending() { let mut open = State::Open; - open.handle_inbound_flag(Flag::Fin, &mut Bytes::default()); - open.handle_inbound_flag(Flag::StopSending, &mut Bytes::default()); + open.handle_inbound_flag(Flag::FIN, &mut Bytes::default()); + open.handle_inbound_flag(Flag::STOP_SENDING, &mut Bytes::default()); let error1 = open.read_barrier().unwrap_err(); let error2 = open.write_barrier().unwrap_err(); @@ -503,7 +503,7 @@ mod tests { let mut open = State::Open; let mut buffer = Bytes::copy_from_slice(b"foobar"); - open.handle_inbound_flag(Flag::Reset, &mut buffer); + open.handle_inbound_flag(Flag::RESET, &mut buffer); assert!(buffer.is_empty()); } From 2a1ff8cc454735c54eac498fcee3d6a76ec2a6ce Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 21:10:36 -0500 Subject: [PATCH 49/66] Fix clippy errors --- protocols/rendezvous/src/codec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/rendezvous/src/codec.rs b/protocols/rendezvous/src/codec.rs index 6a1e9865f29..39447a56bdb 100644 --- a/protocols/rendezvous/src/codec.rs +++ b/protocols/rendezvous/src/codec.rs @@ -280,7 +280,7 @@ impl From for proto::Message { Message::RegisterResponse(Err(error)) => proto::Message { type_pb: Some(proto::MessageType::REGISTER_RESPONSE), registerResponse: Some(proto::RegisterResponse { - status: Some(proto::ResponseStatus::from(error).into()), + status: Some(proto::ResponseStatus::from(error)), statusText: None, ttl: None, }), @@ -342,7 +342,7 @@ impl From for proto::Message { type_pb: Some(proto::MessageType::DISCOVER_RESPONSE), discoverResponse: Some(proto::DiscoverResponse { registrations: Vec::new(), - status: Some(proto::ResponseStatus::from(error).into()), + status: Some(proto::ResponseStatus::from(error)), statusText: None, cookie: None, }), From c38e9e38093621e52fd007953a61bc3a29b6ad76 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 21:14:45 -0500 Subject: [PATCH 50/66] Fix clippy errors take 2 --- protocols/autonat/src/protocol.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/protocol.rs index 78753d76e6e..2bee14200d1 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/protocol.rs @@ -247,7 +247,7 @@ impl DialResponse { let addr = Multiaddr::try_from(addr.to_vec()) .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; Self { - status_text: statusText.map(|status| status.to_string()), + status_text: statusText, result: Ok(addr), } } @@ -256,7 +256,7 @@ impl DialResponse { statusText, addr: None, }) => Self { - status_text: statusText.map(|status| status.to_string()), + status_text: statusText, result: Err(ResponseError::try_from(status)?), }, _ => { From 003de70c26254a02813bc1d4c5ad11c0a34c68dc Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 13 Feb 2023 21:19:45 -0500 Subject: [PATCH 51/66] Fix CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42dcb155690..d8c133a3e97 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -329,7 +329,7 @@ jobs: **/generated/*.proto - name: Generate proto files - run: pb-rs ${{ steps.glob.outputs.paths }} + run: pb-rs -D ${{ steps.glob.outputs.paths }} - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 run: | From 4f7605a314763ab87182f0cb6b9748f7ace444a0 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Feb 2023 16:47:29 -0500 Subject: [PATCH 52/66] Fix pb-rs version --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9b109506369..dc910ea88b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -301,7 +301,7 @@ jobs: - uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0 - - run: cargo install pb-rs --locked + - run: cargo install --version 0.10.0 pb-rs --locked - name: Glob match uses: tj-actions/glob@v16 From 23526e6cc5174586d00035a6f04c5ed15e196c12 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Feb 2023 16:49:04 -0500 Subject: [PATCH 53/66] Use long option name --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc910ea88b4..5a312d10c14 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -311,7 +311,7 @@ jobs: **/generated/*.proto - name: Generate proto files - run: pb-rs -D ${{ steps.glob.outputs.paths }} + run: pb-rs --dont_use_cow ${{ steps.glob.outputs.paths }} - name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794 run: | From c42a5a82de98de52a8c59b472e5633b2a6c778da Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Feb 2023 17:05:22 -0500 Subject: [PATCH 54/66] Address changelog feedback --- misc/quick-protobuf-codec/CHANGELOG.md | 17 ----------------- protocols/floodsub/CHANGELOG.md | 3 --- 2 files changed, 20 deletions(-) diff --git a/misc/quick-protobuf-codec/CHANGELOG.md b/misc/quick-protobuf-codec/CHANGELOG.md index cff8b9e5512..03b075f332f 100644 --- a/misc/quick-protobuf-codec/CHANGELOG.md +++ b/misc/quick-protobuf-codec/CHANGELOG.md @@ -3,20 +3,3 @@ - Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. [PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 - -# 0.3.0 - -- Implement `From` trait for `std::io::Error`. See [PR 2622]. -- Don't leak `prost` dependency in `Error` type. See [PR 3058]. - -- Update `rust-version` to reflect the actual MSRV: 1.60.0. See [PR 3090]. - -[PR 2622]: https://github.com/libp2p/rust-libp2p/pull/2622/ -[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058/ -[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090 - -# 0.2.0 - -- Update to prost(-build) `v0.11`. See [PR 2788]. - -[PR 2788]: https://github.com/libp2p/rust-libp2p/pull/2788/ diff --git a/protocols/floodsub/CHANGELOG.md b/protocols/floodsub/CHANGELOG.md index 33d4fd4920d..3042afd857c 100644 --- a/protocols/floodsub/CHANGELOG.md +++ b/protocols/floodsub/CHANGELOG.md @@ -4,11 +4,8 @@ - Update to `libp2p-swarm` `v0.42.0`. -- Read and write protocols messages via `prost-codec`. See [PR 3224]. - - Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. -[pr 3224]: https://github.com/libp2p/rust-libp2p/pull/3224 [PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 # 0.41.0 From ece12c17e2e184085d0458842318ca09b222ce5c Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Feb 2023 17:08:22 -0500 Subject: [PATCH 55/66] Remove line --- protocols/dcutr/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/protocols/dcutr/CHANGELOG.md b/protocols/dcutr/CHANGELOG.md index d2db8040bb3..24d452c3d18 100644 --- a/protocols/dcutr/CHANGELOG.md +++ b/protocols/dcutr/CHANGELOG.md @@ -19,7 +19,6 @@ [PR 3214]: https://github.com/libp2p/rust-libp2p/pull/3214 [PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312 - # 0.8.1 - Skip unparsable multiaddr in `InboundUpgrade::upgrade_inbound` and From 90ebad6bf8bb1b5391e3feacf2c68900a5de0ab1 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 23 Feb 2023 20:37:15 -0500 Subject: [PATCH 56/66] Zeroize Data --- core/src/identity.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/identity.rs b/core/src/identity.rs index 6e77f662263..5c290c11c5a 100644 --- a/core/src/identity.rs +++ b/core/src/identity.rs @@ -192,8 +192,9 @@ impl Keypair { impl zeroize::Zeroize for proto::PrivateKey { fn zeroize(&mut self) { + // KeyType cannot be zeroized. self.Type = proto::KeyType::default(); - self.Data = Vec::default(); + self.Data.zeroize(); } } From 66a2af59eb2fd2cce70c84c57f09e9a86c5bc13e Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 23 Feb 2023 20:37:43 -0500 Subject: [PATCH 57/66] Remove protoc installation from ci --- .github/workflows/cache-factory.yml | 6 ------ .github/workflows/ci.yml | 25 ------------------------- .github/workflows/docs.yml | 4 ---- 3 files changed, 35 deletions(-) diff --git a/.github/workflows/cache-factory.yml b/.github/workflows/cache-factory.yml index 70cc185f900..7392eca40e8 100644 --- a/.github/workflows/cache-factory.yml +++ b/.github/workflows/cache-factory.yml @@ -33,9 +33,6 @@ jobs: matrix: rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }} steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -55,9 +52,6 @@ jobs: make_stable_rust_cache: runs-on: ubuntu-latest steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a312d10c14..012454727f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,9 +23,6 @@ jobs: matrix: crate: ${{ fromJSON(needs.gather_published_crates.outputs.members) }} steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - name: Get MSRV for ${{ matrix.crate }} @@ -97,16 +94,6 @@ jobs: os: windows-latest runs-on: ${{ matrix.os }} steps: - - name: Install Protoc - if: ${{ matrix.os != 'ubuntu-latest' }} - uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - - - name: Install Protoc - if: ${{ matrix.os == 'ubuntu-latest' }} - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -130,9 +117,6 @@ jobs: - features: "mdns tcp dns tokio" - features: "mdns tcp dns async-std" steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -150,9 +134,6 @@ jobs: name: Check rustdoc intra-doc links runs-on: ubuntu-latest steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -176,9 +157,6 @@ jobs: beta ] steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -199,9 +177,6 @@ jobs: name: IPFS Integration tests runs-on: ubuntu-latest steps: - - name: Install Protoc - run: sudo apt-get install protobuf-compiler - - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 9901be942be..470e2f00f33 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -9,10 +9,6 @@ jobs: name: Build documentation runs-on: ubuntu-latest steps: - - name: Install Protoc - uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - name: Checkout uses: actions/checkout@v3 - name: Install nightly toolchain From c014e1e461e158f6cb3e31a509afca82b534a6b3 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 23 Feb 2023 20:39:02 -0500 Subject: [PATCH 58/66] Remove protoc installation from interop-tests --- interop-tests/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interop-tests/Dockerfile b/interop-tests/Dockerfile index b4bed894884..a9d1428c4b6 100644 --- a/interop-tests/Dockerfile +++ b/interop-tests/Dockerfile @@ -4,7 +4,7 @@ FROM rust:1.67.0 # Setup protoc. TODO this breaks reproducibility and uses an old version of protoc. # In the future protobuf generated files will be checked in, so we can remove this WORKDIR /protoc-setup -RUN apt-get update && apt-get install -y cmake protobuf-compiler +RUN apt-get update && apt-get install -y cmake # Run with access to the target cache to speed up builds WORKDIR /workspace From a4335ced45ce9bdb2554d493624bf75d7e3edca1 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 11:25:35 -0500 Subject: [PATCH 59/66] Revert ci changes --- .github/workflows/cache-factory.yml | 6 ++++++ .github/workflows/ci.yml | 22 ++++++++++++++++++++++ .github/workflows/docs.yml | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/.github/workflows/cache-factory.yml b/.github/workflows/cache-factory.yml index 7392eca40e8..70cc185f900 100644 --- a/.github/workflows/cache-factory.yml +++ b/.github/workflows/cache-factory.yml @@ -33,6 +33,9 @@ jobs: matrix: rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }} steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -52,6 +55,9 @@ jobs: make_stable_rust_cache: runs-on: ubuntu-latest steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 012454727f6..690522ae394 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,9 @@ jobs: matrix: crate: ${{ fromJSON(needs.gather_published_crates.outputs.members) }} steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - name: Get MSRV for ${{ matrix.crate }} @@ -94,6 +97,16 @@ jobs: os: windows-latest runs-on: ${{ matrix.os }} steps: + - name: Install Protoc + if: ${{ matrix.os != 'ubuntu-latest' }} + uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Protoc + if: ${{ matrix.os == 'ubuntu-latest' }} + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -117,6 +130,9 @@ jobs: - features: "mdns tcp dns tokio" - features: "mdns tcp dns async-std" steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -134,6 +150,9 @@ jobs: name: Check rustdoc intra-doc links runs-on: ubuntu-latest steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 @@ -157,6 +176,9 @@ jobs: beta ] steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 470e2f00f33..9901be942be 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -9,6 +9,10 @@ jobs: name: Build documentation runs-on: ubuntu-latest steps: + - name: Install Protoc + uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} - name: Checkout uses: actions/checkout@v3 - name: Install nightly toolchain From 8531ac40873ea141478280d185d94a17e870177c Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 11:26:21 -0500 Subject: [PATCH 60/66] Fmt relay --- protocols/relay/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 00ff07ce043..6a59cf3826c 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -23,9 +23,9 @@ mod handler; pub mod rate_limiter; -use crate::proto; use crate::behaviour::handler::Handler; use crate::multiaddr_ext::MultiaddrExt; +use crate::proto; use crate::protocol::{inbound_hop, outbound_stop}; use either::Either; use instant::Instant; From 5ca874799e211cebfd957c0d54b3b5812d4c8445 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 18:57:21 -0500 Subject: [PATCH 61/66] Remove transparent --- core/src/lib.rs | 8 +++++++- transports/noise/src/lib.rs | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index e911ad52531..3b35f9388b2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,6 +47,7 @@ mod proto { /// Multi-address re-export. pub use multiaddr; +use std::fmt::{self, Formatter}; pub type Negotiated = multistream_select::Negotiated; mod peer_id; @@ -75,5 +76,10 @@ pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo}; #[derive(thiserror::Error, Debug)] -#[error(transparent)] pub struct DecodeError(quick_protobuf::Error); + +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index 2c052d190b1..e6f47f62b1e 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -65,6 +65,8 @@ pub use protocol::x25519::X25519; pub use protocol::x25519_spec::X25519Spec; pub use protocol::{AuthenticKeypair, Keypair, KeypairIdentity, PublicKey, SecretKey}; pub use protocol::{Protocol, ProtocolParams, IK, IX, XX}; +use std::fmt; +use std::fmt::Formatter; use crate::handshake::State; use crate::io::handshake; @@ -284,9 +286,14 @@ pub enum NoiseError { } #[derive(Debug, thiserror::Error)] -#[error(transparent)] pub struct DecodeError(quick_protobuf::Error); +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + impl From for NoiseError { fn from(e: quick_protobuf::Error) -> Self { NoiseError::InvalidPayload(DecodeError(e)) From 98e302b70fe2fb84e70de16ef57fc6d0bfca2520 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 19:09:55 -0500 Subject: [PATCH 62/66] Add protoc installation to ci --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 690522ae394..5a312d10c14 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -199,6 +199,9 @@ jobs: name: IPFS Integration tests runs-on: ubuntu-latest steps: + - name: Install Protoc + run: sudo apt-get install protobuf-compiler + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@9cd00a88a73addc8617065438eff914dd08d0955 # v1 From b0a41ecec020ceeec41ac9c930a9e3e21e9ad1b0 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 19:30:04 -0500 Subject: [PATCH 63/66] Remove display impl --- core/src/lib.rs | 8 +------- transports/noise/src/lib.rs | 9 +-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index 3b35f9388b2..e911ad52531 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,7 +47,6 @@ mod proto { /// Multi-address re-export. pub use multiaddr; -use std::fmt::{self, Formatter}; pub type Negotiated = multistream_select::Negotiated; mod peer_id; @@ -76,10 +75,5 @@ pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo}; #[derive(thiserror::Error, Debug)] +#[error(transparent)] pub struct DecodeError(quick_protobuf::Error); - -impl fmt::Display for DecodeError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index e6f47f62b1e..2c052d190b1 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -65,8 +65,6 @@ pub use protocol::x25519::X25519; pub use protocol::x25519_spec::X25519Spec; pub use protocol::{AuthenticKeypair, Keypair, KeypairIdentity, PublicKey, SecretKey}; pub use protocol::{Protocol, ProtocolParams, IK, IX, XX}; -use std::fmt; -use std::fmt::Formatter; use crate::handshake::State; use crate::io::handshake; @@ -286,14 +284,9 @@ pub enum NoiseError { } #[derive(Debug, thiserror::Error)] +#[error(transparent)] pub struct DecodeError(quick_protobuf::Error); -impl fmt::Display for DecodeError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - impl From for NoiseError { fn from(e: quick_protobuf::Error) -> Self { NoiseError::InvalidPayload(DecodeError(e)) From da3e08876bdff4d4b0ba77b9f0a67dddbfee3394 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 19:54:07 -0500 Subject: [PATCH 64/66] Box error --- core/src/lib.rs | 3 ++- core/src/peer_record.rs | 3 ++- core/src/signed_envelope.rs | 3 ++- transports/noise/src/lib.rs | 5 +++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index e911ad52531..bbbe30b37b5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,6 +47,7 @@ mod proto { /// Multi-address re-export. pub use multiaddr; +use std::error::Error; pub type Negotiated = multistream_select::Negotiated; mod peer_id; @@ -76,4 +77,4 @@ pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, U #[derive(thiserror::Error, Debug)] #[error(transparent)] -pub struct DecodeError(quick_protobuf::Error); +pub struct DecodeError(Box); diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 2b3fd36eb01..c3787a09f1d 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -36,7 +36,8 @@ impl PeerRecord { let (payload, signing_key) = envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?; let mut reader = BytesReader::from_bytes(payload); - let record = proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError)?; + let record = proto::PeerRecord::from_reader(&mut reader, payload) + .map_err(|e| DecodeError(Box::new(e)))?; let peer_id = PeerId::from_bytes(&record.peer_id)?; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 2725da1502e..44b222435cc 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -98,7 +98,8 @@ impl SignedEnvelope { use quick_protobuf::MessageRead; let mut reader = BytesReader::from_bytes(bytes); - let envelope = proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError)?; + let envelope = proto::Envelope::from_reader(&mut reader, bytes) + .map_err(|e| DecodeError(Box::new(e)))?; Ok(Self { key: envelope diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index 2c052d190b1..215d712b089 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -65,6 +65,7 @@ pub use protocol::x25519::X25519; pub use protocol::x25519_spec::X25519Spec; pub use protocol::{AuthenticKeypair, Keypair, KeypairIdentity, PublicKey, SecretKey}; pub use protocol::{Protocol, ProtocolParams, IK, IX, XX}; +use std::error::Error; use crate::handshake::State; use crate::io::handshake; @@ -285,11 +286,11 @@ pub enum NoiseError { #[derive(Debug, thiserror::Error)] #[error(transparent)] -pub struct DecodeError(quick_protobuf::Error); +pub struct DecodeError(Box); impl From for NoiseError { fn from(e: quick_protobuf::Error) -> Self { - NoiseError::InvalidPayload(DecodeError(e)) + NoiseError::InvalidPayload(DecodeError(Box::new(e))) } } From 2b1b2744c3a21df1777467bdc1d2cfb7d37f5def Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 24 Feb 2023 22:41:42 -0500 Subject: [PATCH 65/66] Change DecodeError --- core/src/lib.rs | 45 +++++++++++++++++++++++++++++++++---- core/src/peer_record.rs | 4 ++-- core/src/signed_envelope.rs | 4 ++-- transports/noise/src/lib.rs | 45 +++++++++++++++++++++++++++++++++---- 4 files changed, 86 insertions(+), 12 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index bbbe30b37b5..a37b1246cf5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,7 +47,6 @@ mod proto { /// Multi-address re-export. pub use multiaddr; -use std::error::Error; pub type Negotiated = multistream_select::Negotiated; mod peer_id; @@ -75,6 +74,44 @@ pub use translation::address_translation; pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo}; -#[derive(thiserror::Error, Debug)] -#[error(transparent)] -pub struct DecodeError(Box); +// This type was created to avoid breaking changes +// caused by quick_protobuf::Error related to auto traits. +// See https://github.com/libp2p/rust-libp2p/pull/3312. +#[derive(Debug, thiserror::Error)] +pub enum DecodeError { + #[error("{0}")] + Io(String), + #[error("{0}")] + Utf8(core::str::Utf8Error), + #[error("{0}")] + Deprecated(&'static str), + #[error("{0}")] + UnknownWireType(u8), + #[error("Varint decoding error")] + Varint, + #[error("{0}")] + Message(String), + #[error("{0}")] + Map(u8), + #[error("Out of data when reading from or writing to a byte buffer")] + UnexpectedEndOfBuffer, + #[error("The supplied output buffer is not large enough to serialize the message")] + OutputBufferTooSmall, +} + +impl From for DecodeError { + fn from(e: quick_protobuf::Error) -> Self { + use quick_protobuf::Error; + match e { + Error::Io(e) => Self::Io(e.to_string()), + Error::Utf8(e) => Self::Utf8(e), + Error::Deprecated(e) => Self::Deprecated(e), + Error::UnknownWireType(e) => Self::UnknownWireType(e), + Error::Varint => Self::Varint, + Error::Message(e) => Self::Message(e), + Error::Map(e) => Self::Map(e), + Error::UnexpectedEndOfBuffer => Self::UnexpectedEndOfBuffer, + Error::OutputBufferTooSmall => Self::OutputBufferTooSmall, + } + } +} diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index c3787a09f1d..2a01a18fd42 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -36,8 +36,8 @@ impl PeerRecord { let (payload, signing_key) = envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?; let mut reader = BytesReader::from_bytes(payload); - let record = proto::PeerRecord::from_reader(&mut reader, payload) - .map_err(|e| DecodeError(Box::new(e)))?; + let record = + proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError::from)?; let peer_id = PeerId::from_bytes(&record.peer_id)?; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 44b222435cc..12c1324efa9 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -98,8 +98,8 @@ impl SignedEnvelope { use quick_protobuf::MessageRead; let mut reader = BytesReader::from_bytes(bytes); - let envelope = proto::Envelope::from_reader(&mut reader, bytes) - .map_err(|e| DecodeError(Box::new(e)))?; + let envelope = + proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?; Ok(Self { key: envelope diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index 215d712b089..4ee0c6ccec1 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -65,7 +65,6 @@ pub use protocol::x25519::X25519; pub use protocol::x25519_spec::X25519Spec; pub use protocol::{AuthenticKeypair, Keypair, KeypairIdentity, PublicKey, SecretKey}; pub use protocol::{Protocol, ProtocolParams, IK, IX, XX}; -use std::error::Error; use crate::handshake::State; use crate::io::handshake; @@ -284,13 +283,51 @@ pub enum NoiseError { SigningError(#[from] identity::error::SigningError), } +// This type was created to avoid breaking changes +// caused by quick_protobuf::Error related to auto traits. +// See https://github.com/libp2p/rust-libp2p/pull/3312. #[derive(Debug, thiserror::Error)] -#[error(transparent)] -pub struct DecodeError(Box); +pub enum DecodeError { + #[error("{0}")] + Io(String), + #[error("{0}")] + Utf8(core::str::Utf8Error), + #[error("{0}")] + Deprecated(&'static str), + #[error("{0}")] + UnknownWireType(u8), + #[error("Varint decoding error")] + Varint, + #[error("{0}")] + Message(String), + #[error("{0}")] + Map(u8), + #[error("Out of data when reading from or writing to a byte buffer")] + UnexpectedEndOfBuffer, + #[error("The supplied output buffer is not large enough to serialize the message")] + OutputBufferTooSmall, +} + +impl From for DecodeError { + fn from(e: quick_protobuf::Error) -> Self { + use quick_protobuf::Error; + match e { + Error::Io(e) => Self::Io(e.to_string()), + Error::Utf8(e) => Self::Utf8(e), + Error::Deprecated(e) => Self::Deprecated(e), + Error::UnknownWireType(e) => Self::UnknownWireType(e), + Error::Varint => Self::Varint, + Error::Message(e) => Self::Message(e), + Error::Map(e) => Self::Map(e), + Error::UnexpectedEndOfBuffer => Self::UnexpectedEndOfBuffer, + Error::OutputBufferTooSmall => Self::OutputBufferTooSmall, + } + } +} impl From for NoiseError { fn from(e: quick_protobuf::Error) -> Self { - NoiseError::InvalidPayload(DecodeError(Box::new(e))) + NoiseError::InvalidPayload(e.into()) } } From dfc55b880cf28b8ecbcbf95f81ac908878175b29 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 28 Feb 2023 21:40:33 -0500 Subject: [PATCH 66/66] Make an error type that does not expose anything --- core/src/lib.rs | 45 +++++++++---------------------------- transports/noise/src/lib.rs | 43 ++++++++--------------------------- 2 files changed, 19 insertions(+), 69 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index a37b1246cf5..f3e00b1ff15 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,6 +47,8 @@ mod proto { /// Multi-address re-export. pub use multiaddr; +use std::fmt; +use std::fmt::Formatter; pub type Negotiated = multistream_select::Negotiated; mod peer_id; @@ -74,44 +76,17 @@ pub use translation::address_translation; pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo}; -// This type was created to avoid breaking changes -// caused by quick_protobuf::Error related to auto traits. -// See https://github.com/libp2p/rust-libp2p/pull/3312. #[derive(Debug, thiserror::Error)] -pub enum DecodeError { - #[error("{0}")] - Io(String), - #[error("{0}")] - Utf8(core::str::Utf8Error), - #[error("{0}")] - Deprecated(&'static str), - #[error("{0}")] - UnknownWireType(u8), - #[error("Varint decoding error")] - Varint, - #[error("{0}")] - Message(String), - #[error("{0}")] - Map(u8), - #[error("Out of data when reading from or writing to a byte buffer")] - UnexpectedEndOfBuffer, - #[error("The supplied output buffer is not large enough to serialize the message")] - OutputBufferTooSmall, -} +pub struct DecodeError(String); impl From for DecodeError { fn from(e: quick_protobuf::Error) -> Self { - use quick_protobuf::Error; - match e { - Error::Io(e) => Self::Io(e.to_string()), - Error::Utf8(e) => Self::Utf8(e), - Error::Deprecated(e) => Self::Deprecated(e), - Error::UnknownWireType(e) => Self::UnknownWireType(e), - Error::Varint => Self::Varint, - Error::Message(e) => Self::Message(e), - Error::Map(e) => Self::Map(e), - Error::UnexpectedEndOfBuffer => Self::UnexpectedEndOfBuffer, - Error::OutputBufferTooSmall => Self::OutputBufferTooSmall, - } + Self(e.to_string()) + } +} + +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) } } diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index 4ee0c6ccec1..05a0d4d93a0 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -65,6 +65,8 @@ pub use protocol::x25519::X25519; pub use protocol::x25519_spec::X25519Spec; pub use protocol::{AuthenticKeypair, Keypair, KeypairIdentity, PublicKey, SecretKey}; pub use protocol::{Protocol, ProtocolParams, IK, IX, XX}; +use std::fmt; +use std::fmt::Formatter; use crate::handshake::State; use crate::io::handshake; @@ -283,45 +285,18 @@ pub enum NoiseError { SigningError(#[from] identity::error::SigningError), } -// This type was created to avoid breaking changes -// caused by quick_protobuf::Error related to auto traits. -// See https://github.com/libp2p/rust-libp2p/pull/3312. #[derive(Debug, thiserror::Error)] -pub enum DecodeError { - #[error("{0}")] - Io(String), - #[error("{0}")] - Utf8(core::str::Utf8Error), - #[error("{0}")] - Deprecated(&'static str), - #[error("{0}")] - UnknownWireType(u8), - #[error("Varint decoding error")] - Varint, - #[error("{0}")] - Message(String), - #[error("{0}")] - Map(u8), - #[error("Out of data when reading from or writing to a byte buffer")] - UnexpectedEndOfBuffer, - #[error("The supplied output buffer is not large enough to serialize the message")] - OutputBufferTooSmall, +pub struct DecodeError(String); + +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } } impl From for DecodeError { fn from(e: quick_protobuf::Error) -> Self { - use quick_protobuf::Error; - match e { - Error::Io(e) => Self::Io(e.to_string()), - Error::Utf8(e) => Self::Utf8(e), - Error::Deprecated(e) => Self::Deprecated(e), - Error::UnknownWireType(e) => Self::UnknownWireType(e), - Error::Varint => Self::Varint, - Error::Message(e) => Self::Message(e), - Error::Map(e) => Self::Map(e), - Error::UnexpectedEndOfBuffer => Self::UnexpectedEndOfBuffer, - Error::OutputBufferTooSmall => Self::OutputBufferTooSmall, - } + Self(e.to_string()) } }