Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the rendezvous protocol #2107

Merged
merged 250 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 246 commits
Commits
Show all changes
250 commits
Select commit Hold shift + click to select a range
047840d
Define protobuf messages and add build script to generate rust bindings
rishflab May 24, 2021
76f57bd
WIP:
rishflab May 25, 2021
aed2f95
Add crate to workspace members
thomaseizinger May 25, 2021
61b7343
WIP
thomaseizinger May 25, 2021
88d6fca
Add mapping between protobuf and application types
thomaseizinger May 25, 2021
e191a3d
Return Framed stream from protocol
thomaseizinger May 25, 2021
f728821
Define behaviour API
rishflab May 26, 2021
87df759
Initial PoC for signed peer records
thomaseizinger May 27, 2021
5c61db0
Initial PoC for signed peer records
thomaseizinger May 27, 2021
5428d7f
Add {into,from}_protobuf_encoding for SignedEnvelope
thomaseizinger May 27, 2021
7a12627
Do proper error handling for decoding peer records
thomaseizinger May 27, 2021
c94e215
Integrate signed peer records into rendezvous
thomaseizinger May 27, 2021
193c016
Add {into,from}_protobuf_encoding for SignedEnvelope
thomaseizinger May 27, 2021
5a9aae3
Do proper error handling for decoding peer records
thomaseizinger May 27, 2021
a15296e
Add feature to fix broken unsigned-varint dependency import
rishflab May 27, 2021
4208a02
WIP: Add behaviour and handler
rishflab May 28, 2021
d5ea66b
Remove usage of unsafe code
thomaseizinger May 28, 2021
2166e7e
Re-order contents of codec module by importance
thomaseizinger May 28, 2021
4bbf56d
Introduce `NewRegistration` type so we have a place for the default TTL
thomaseizinger May 28, 2021
c36ac8b
Make `ttl` public so we can actually construct the type
thomaseizinger May 28, 2021
b45688d
Actually a constructor is better
thomaseizinger May 28, 2021
1dc2580
Use convenience _unpin functions instead of manual pinning
thomaseizinger May 28, 2021
e079510
Add intermediate states to handler
rishflab May 28, 2021
a03868a
Rename substream states
rishflab May 28, 2021
436682e
Fix inbound/outbound injection
rishflab May 28, 2021
32a504b
Go to closing after reading from outbound
rishflab May 28, 2021
e377d5d
Prepare everything for writing tests
da-kami May 28, 2021
f925f26
Retreive discovered from hashmap
rishflab May 31, 2021
a61742a
Remove signed peer record todo from handler
rishflab May 31, 2021
b39449d
Handle unregister request in behaviour
rishflab May 31, 2021
d6fc13f
WIP - Happy path registration and discovery test
da-kami May 31, 2021
2edc167
"Handle" connect/disconnect events
rishflab May 31, 2021
d590741
Fix todos/unimplemented except for signed peer record related
rishflab May 31, 2021
81388db
Bubble up signed record todo to behaviour
rishflab Jun 1, 2021
404b484
WIP: signed peer record
rishflab Jun 1, 2021
a89cffc
WIP - Aligen Behaviour, Handler and Codec
da-kami Jun 1, 2021
4ffaa5f
WIP - Test running, but behaviour does not emit events as expected
da-kami Jun 1, 2021
585e4a7
Fix unused imports
rishflab Jun 1, 2021
20ce11f
Reorg behaviour
rishflab Jun 1, 2021
b484718
Added debug prints, Handler not being polled?
rishflab Jun 1, 2021
6f923c2
Fix bug where inbound state never went to closing
rishflab Jun 2, 2021
6a4bbec
Remove duplicated code
rishflab Jun 3, 2021
e1c2d17
Delete unused file
rishflab Jun 3, 2021
7796a69
Use tcp transport in tests
rishflab Jun 3, 2021
f2f89cf
Fix bug where inbound substream was being poisoned
rishflab Jun 3, 2021
dbd6745
Hack: Return poll ready to force swarm to be polled
rishflab Jun 4, 2021
7b22d16
Delete unused code
rishflab Jun 4, 2021
a00b3f8
Replace emission of dummy event with ResponseSent
rishflab Jun 4, 2021
69a00cf
Reverted back to daniel/thomas test architecture
rishflab Jun 7, 2021
c0599c8
Make initial test pass
thomaseizinger Jun 7, 2021
57d471d
Add TODO
thomaseizinger Jun 7, 2021
624a72b
Remove hack fix to trigger Swarm::poll()
rishflab Jun 7, 2021
1f5251a
Reduce code duplication by generating identity inside `new_swarm`
thomaseizinger Jun 8, 2021
94b5dbe
Format with rustfmt
thomaseizinger Jun 8, 2021
10ee7e5
Infer addresses to register from external addresses
thomaseizinger Jun 8, 2021
26d1ac6
Grab PeerId from Swarm when necessary instead of passing it around
thomaseizinger Jun 8, 2021
32b05a1
Remove dead code
thomaseizinger Jun 8, 2021
5fc8583
Replace buggy `connect` functionality with composable extension trait
thomaseizinger Jun 8, 2021
24a0c0d
Use unwrap_or_default to avoid branching
thomaseizinger Jun 8, 2021
dec5a43
Log events as we get them
thomaseizinger Jun 8, 2021
0c3bbaa
Simplify assertion of successful registration
thomaseizinger Jun 8, 2021
5c9c83e
Deal with warnings
thomaseizinger Jun 8, 2021
b766951
Avoid emitting events twice / erroneously
thomaseizinger Jun 8, 2021
a89b28d
Make discover test working
thomaseizinger Jun 8, 2021
acc6583
Remove role name that was only there for debugging
thomaseizinger Jun 8, 2021
1ef3539
Add empty line for better formatting
thomaseizinger Jun 8, 2021
f42ac18
Rename `handler::Input` to `handler::InEvent`
thomaseizinger Jun 8, 2021
8bb25b9
Don't wrap Message on the way up from the handler
thomaseizinger Jun 8, 2021
e337024
Introduce `Next` enum for making `State::poll` fns declarative
thomaseizinger Jun 8, 2021
6128354
Fix documentation of {Inbound,Outbound}State
thomaseizinger Jun 8, 2021
4806c96
Align order of members as per trait definition
thomaseizinger Jun 8, 2021
7e0d9da
Remove noisy logs
thomaseizinger Jun 8, 2021
939faa9
Simplify handling of `InEvent`
thomaseizinger Jun 8, 2021
e970758
Import std::mem
thomaseizinger Jun 8, 2021
35c91ab
Shorten variable and fieldnames
thomaseizinger Jun 8, 2021
7610d3d
Have some fun with Rust
thomaseizinger Jun 8, 2021
5af486e
Use module-level public constructor for making new protocol
thomaseizinger Jun 8, 2021
581cee0
Implement rendezvous protocol using `upgrade::from_fn`
thomaseizinger Jun 8, 2021
43c8753
Return a hashmap of registrations instead of a vector
rishflab Jun 9, 2021
344123f
Allow registration swarm to specify TTL in registration request
rishflab Jun 9, 2021
faa066c
Set unix timestamp in peer record
rishflab Jun 9, 2021
6908659
Attach timestamp to the registration the record
rishflab Jun 9, 2021
5ebb734
Test refreshing of the TTL by sending a new registration request
rishflab Jun 9, 2021
b9dd7d3
Reject register request if specified TTL is too high
rishflab Jun 9, 2021
0d4bd9f
Add failing test for cookie behaviour
thomaseizinger Jun 10, 2021
e1fcdd1
Add basic tests for Registrations behaviour
thomaseizinger Jun 10, 2021
a92be31
Refactor `Registrations::get` to return Iterator
thomaseizinger Jun 10, 2021
89348dc
Add test for overwriting existing registration
thomaseizinger Jun 10, 2021
edeb5b2
Store registrations by ID
thomaseizinger Jun 10, 2021
3a62bee
Don't return namespace from `Registrations::add`
thomaseizinger Jun 10, 2021
5349d93
Create newtype for registration id
thomaseizinger Jun 10, 2021
4c28f11
Don't clone registrations unless necessary
thomaseizinger Jun 10, 2021
4f03f90
Add initial cookie functionality
thomaseizinger Jun 10, 2021
fbea1ef
Make sure new cookie implies old cookie
thomaseizinger Jun 10, 2021
7801601
Add cookies to wire messages
thomaseizinger Jun 10, 2021
e233850
Allow ttl to match exactly upper bound
thomaseizinger Jun 10, 2021
242d444
Use defensive programming to avoid additional levels of indentation
thomaseizinger Jun 10, 2021
cc9a02e
Use `match` with specific error variant instead of if let
thomaseizinger Jun 10, 2021
5277f6a
Remove TODO
thomaseizinger Jun 10, 2021
3240bc5
Rename function to clarify that we are not using protobuf for cookies
thomaseizinger Jun 10, 2021
2d96943
Allow tests to run in parallel
thomaseizinger Jun 10, 2021
58ce18e
Make cookies specific to a namespace if given during DISCOVER
thomaseizinger Jun 10, 2021
29b9fb1
Merge branch 'master' into signed-peer-records
thomaseizinger Jun 11, 2021
36d7039
Fix typo in function name
thomaseizinger Jun 11, 2021
1241d77
Remove `PeerRecord::authenticate`
thomaseizinger Jun 11, 2021
fb39c2c
Re-order module
thomaseizinger Jun 11, 2021
0251bd4
Make `PeerRecord` private
thomaseizinger Jun 11, 2021
799103e
Merge `PeerRecord` into `AuthenticatedPeerRecord` for simplicity
thomaseizinger Jun 11, 2021
dde489d
Use stdlib rather than bytes::BufMut
thomaseizinger Jun 11, 2021
a59a60d
Reduce duplication between sign and verify
thomaseizinger Jun 11, 2021
a9ab281
Extend docs of `SignedEnvelope`
thomaseizinger Jun 11, 2021
de19fda
Slightly re-organize PeerRecord implementations
thomaseizinger Jun 11, 2021
be4bea0
Fix link in docs
thomaseizinger Jun 11, 2021
b33d7d5
Improve docs wording
thomaseizinger Jun 11, 2021
65d0ba6
Make `PeerRecord`s clonable
thomaseizinger Jun 11, 2021
000360c
Merge branch 'signed-peer-records' into rendezvous
thomaseizinger Jun 11, 2021
2f0e4c4
Remodel `RegistrationError` as a struct error
thomaseizinger Jun 11, 2021
8a4d747
Fail if we don't have any external addresses
thomaseizinger Jun 11, 2021
b9ca692
Remove noisy logs
thomaseizinger Jun 11, 2021
444e7a1
Unify naming of substream states
thomaseizinger Jun 11, 2021
09ad250
Close the substream on invalid messages
thomaseizinger Jun 11, 2021
ee74706
Handle error upon reading from outbound gracefully
thomaseizinger Jun 11, 2021
15e0c67
Handle EOF gracefully
thomaseizinger Jun 11, 2021
b5a5364
Handle errors during sending gracefully
thomaseizinger Jun 11, 2021
c4c8795
Allow converting a `ConversionError` to an error code
thomaseizinger Jun 11, 2021
cf8135f
DRY
thomaseizinger Jun 11, 2021
7a2bc6d
Close connection on errors
thomaseizinger Jun 11, 2021
eca21a5
Add TODO for simplification once or-patterns hit stable
thomaseizinger Jun 11, 2021
42ead09
Shorten type and field names to fit things onto one line
thomaseizinger Jun 11, 2021
ebc6f03
Move `SubstreamState` to dedicated module
thomaseizinger Jun 11, 2021
1f3156f
Allow passing cookies to a discover request
thomaseizinger Jun 11, 2021
79a49dd
Exhaustively match result on polling close and explain handling
thomaseizinger Jun 11, 2021
f59cdcf
Group events into client and server side
thomaseizinger Jun 11, 2021
3ae1cd9
Rename event variants for consistency and add documentation
thomaseizinger Jun 11, 2021
a461c58
Rename `DeclinedRegisterRequest` to `PeerNotRegistered` for consistency
thomaseizinger Jun 11, 2021
55be51f
Rename event variants to be more event-y
thomaseizinger Jun 11, 2021
4da0f53
Return namespace in result events of behaviour
thomaseizinger Jun 11, 2021
43f14f9
Remove TODOs that were erroneously copied over
thomaseizinger Jun 11, 2021
82b5d78
Model success and error message variants using `Result`
thomaseizinger Jun 11, 2021
4ccfbad
Add TODOs
thomaseizinger Jun 11, 2021
56c92c2
Update protobuf files to latest version
thomaseizinger Jun 15, 2021
5484c21
Merge branch 'signed-peer-records' into rendezvous
thomaseizinger Jun 15, 2021
3161547
Fix compile errors from new protobuf description
thomaseizinger Jun 15, 2021
13df0d7
Merge branch 'signed-peer-records' into rendezvous
thomaseizinger Jun 15, 2021
912fcec
Add failing test for expiring registration
thomaseizinger Jun 15, 2021
11647a0
Extend `Advance` with arbitrary params
thomaseizinger Jun 16, 2021
756fdb5
Refactor `Advance` to make use of Result
thomaseizinger Jun 17, 2021
28c0cfb
Make sure we always talk to the same handler
thomaseizinger Jun 17, 2021
a23829c
Add implementation of draft PoW proposal
thomaseizinger Jun 16, 2021
a582ea9
Remove expired registrations
rishflab Jun 16, 2021
9923698
Clear registrations_for_peer upon expiry
rishflab Jun 17, 2021
1b36d74
Extend expiry test
rishflab Jun 17, 2021
6f0cc7f
Trigger cleanup of expired registrations using futures
rishflab Jun 18, 2021
e2f779d
Test expiry event is not emitted if unregistered
rishflab Jun 18, 2021
7312592
Ensure next_expiry.poll_next() cannot return None
rishflab Jun 18, 2021
e560e24
Use tokio::test to fix broken tests
rishflab Jun 18, 2021
69dfbee
Fix bug where new registration was not being added
rishflab Jun 18, 2021
bdc102e
Make tests prettier by adding functions on `Registrations`
thomaseizinger Jun 18, 2021
b8a93c3
Switch registrations in cookies to a HashSet
thomaseizinger Jun 18, 2021
c4f802f
Remove TODOs in favor of tasks on draft PR
thomaseizinger Jun 20, 2021
d31ebff
Include Registration in PeerRegistered OutEvent
rishflab Jun 20, 2021
1fe1e3d
Remove redundant field from PeerRegistered
rishflab Jun 21, 2021
476ddc7
Remove unused dependency
rishflab Jun 21, 2021
93846e1
Apply clippy suggestions
thomaseizinger Jun 21, 2021
61f4dbb
Only depend on tokio features for prod where needed
thomaseizinger Jun 21, 2021
e39c304
Make tests actually compile
thomaseizinger Jun 21, 2021
85a0751
Apply clippy suggestions to tests
thomaseizinger Jun 21, 2021
2bf0689
Don't panic on new inbound/outbound substream
thomaseizinger Jun 21, 2021
b6a02b5
Align naming of substream states
thomaseizinger Jun 21, 2021
bbeb6f3
Don't panic on unexpected event from behaviour
thomaseizinger Jun 21, 2021
4a67051
Merge branch 'master' into rendezvous
thomaseizinger Jun 21, 2021
17924e7
Adapt to latest master
thomaseizinger Jun 21, 2021
46c058c
Make use of new `Stream` interface of `Swarm`
thomaseizinger Jun 21, 2021
6d4a915
Add examples for rendezvous and discovery
rishflab Jun 22, 2021
e87c5b8
Add rendezous swarm
rishflab Jun 22, 2021
bdd2cc4
Registration is working
rishflab Jun 22, 2021
26b2ac1
Remove unnecessary naming of field in single field struct variant
thomaseizinger Jun 23, 2021
c3b1de5
RIP PoW
thomaseizinger Jun 23, 2021
eae577b
Free handler from abstractions that were only needed with PoW
thomaseizinger Jun 23, 2021
24c0322
Respect `limit` parameter of `Discover` requests
thomaseizinger Jun 23, 2021
ee53ba1
Make all modules private and only expose particular structs
thomaseizinger Jun 23, 2021
cd68a75
Actually use `Registrations::poll` to check for expired registrations
thomaseizinger Jun 23, 2021
0dfba09
Re-export rendezvous from libp2p meta crate
thomaseizinger Jun 23, 2021
f6cd3b9
Streamline public interface of libp2p_rendezvous crate
thomaseizinger Jun 23, 2021
c25f554
Inline some variables to make examples easier to read
thomaseizinger Jun 23, 2021
5d29f74
Prefer async fn main over task::block_on
thomaseizinger Jun 23, 2021
77ee0ad
Polish register example a bit
thomaseizinger Jun 23, 2021
189dfde
Rename examples to what they are showcasing
thomaseizinger Jun 23, 2021
b873f23
Discover example dials discovered nodes
thomaseizinger Jun 23, 2021
7a452aa
Make discover and register share a common protocol
thomaseizinger Jun 23, 2021
5d8f42f
Make register example listen on a random port
thomaseizinger Jun 23, 2021
ec79ba6
Rename server to rendezvous point
thomaseizinger Jun 23, 2021
a3270ca
Polish rendezvous point example
thomaseizinger Jun 23, 2021
0fdb340
Use libp2p::development_transport for examples
thomaseizinger Jun 23, 2021
dae0f6e
Add Ping behaviour to rendezvous point
thomaseizinger Jun 23, 2021
7776b37
Make logging prettier
thomaseizinger Jun 23, 2021
f1b763a
Have pings be every second for example's sake
thomaseizinger Jun 23, 2021
6d14c5b
Remove timestamp that is not used for anything
thomaseizinger Jun 23, 2021
b2a0c6f
Return full registration from `Registrations`
thomaseizinger Jun 23, 2021
b34b099
Be quiet by default
thomaseizinger Jun 23, 2021
798d5ab
Rename swarms in test-setup to Alice, Bob and Robert
thomaseizinger Jun 23, 2021
c5a968c
Verify peers can only submit registrations for themselves
thomaseizinger Jun 23, 2021
7bdc277
Don't panic on invalid cookie
thomaseizinger Jun 23, 2021
ea20273
Model `inject_event` implementation of mapping to new events
thomaseizinger Jun 23, 2021
531570e
Return a simple list in `Discovered` event
thomaseizinger Jun 23, 2021
fad3941
Honor min and max TTL from spec
thomaseizinger Jun 23, 2021
9600210
Format exports + Cleanup + Re-naming
rishflab Jun 23, 2021
6aa4c36
Introduce `Namespace` newtype to capture max-length invariant
thomaseizinger Jun 23, 2021
e0eaa41
Don't use latest stable features yet
thomaseizinger Jun 23, 2021
4705877
Update to latest revision of spec
thomaseizinger Jun 24, 2021
c3cd411
Remove snake case to be consistent with type
rishflab Jun 25, 2021
2e143a0
Expose Config params
da-kami Jul 1, 2021
8c3b010
Fix typo in test name
thomaseizinger Jul 1, 2021
622484d
Use `wasm-timer` instead of `tokio`
thomaseizinger Jul 1, 2021
b4a66bd
Add copyright header to all files
thomaseizinger Jul 1, 2021
0a16136
Replace log with debug_assert
thomaseizinger Jul 1, 2021
4cacaf2
Remove redundant result return type from register
rishflab Jul 1, 2021
952fde2
Remove unwrap from example to fix build
da-kami Jul 1, 2021
da0e761
Redefine rendezvous behaviour using generic substream handler
thomaseizinger Jul 1, 2021
082dfd0
Simplify Outbound stream by implementing it as a future
thomaseizinger Jul 1, 2021
d3628cf
Define helper struct for substreams that can be implemented as futures
thomaseizinger Jul 1, 2021
8f46361
Add some more docs and split `inject_event` of `Rendezvous` behaviour
thomaseizinger Jul 2, 2021
326c9b2
Fix clippy errors and warnings
thomaseizinger Jul 2, 2021
3df6e52
Fix cargo doc errors and warnings
thomaseizinger Jul 2, 2021
802b152
Make it compile on wasm
thomaseizinger Jul 2, 2021
74a39f4
Store discovered addresses inside behaviour
thomaseizinger Jul 2, 2021
4af8af7
Add register example that doesnt use the Identify protocol
rishflab Jul 8, 2021
e646e47
Merge branch 'master' into rendezvous
thomaseizinger Aug 25, 2021
5f10abf
Fix test compile errors
thomaseizinger Aug 26, 2021
7ec294f
Fix warnings in tests
thomaseizinger Aug 26, 2021
c56ab55
Remove GlobalSpawnTokioExecutor
thomaseizinger Aug 26, 2021
88fa94b
Document design of `poll_substreams`
thomaseizinger Aug 26, 2021
f1b61a6
Minor formatting fixes
thomaseizinger Aug 26, 2021
c3fdd0b
Use a u64 to represent `RegistrationId`
thomaseizinger Aug 26, 2021
9b1dda8
Add docs to public functions of rendezvous
thomaseizinger Aug 26, 2021
433b3b5
Use naming as per https://github.com/libp2p/rust-libp2p/discussions/2174
thomaseizinger Aug 26, 2021
d85e38b
Expose `wasm-bindgen` feature instead of automatically activating it
thomaseizinger Sep 1, 2021
d5cb328
Split behaviour into client and server
thomaseizinger Aug 27, 2021
8115ec3
Allow registrations on client to expire
thomaseizinger Sep 1, 2021
dccdebc
Merge branch 'master' into rendezvous
thomaseizinger Sep 1, 2021
7d6bf59
Migrate rendezvous protocol to new NetworkBehaviourAction
thomaseizinger Sep 1, 2021
e3ff6c2
Make it work without uuid
thomaseizinger Sep 1, 2021
8f46f84
Log errors and close connection as a result
thomaseizinger Sep 1, 2021
5e3d4a1
Remove unnecessary implementation of default fn
thomaseizinger Sep 1, 2021
5696a7e
Reduce duplication in test setup
thomaseizinger Sep 1, 2021
2637e45
Make tests more readable
thomaseizinger Sep 1, 2021
f8edf2f
Final polish
thomaseizinger Sep 7, 2021
700bcb2
Merge branch 'master' into rendezvous
thomaseizinger Sep 7, 2021
2215f02
Adhere to formatting convention of changelog
thomaseizinger Sep 7, 2021
53ce603
Fix bad link
thomaseizinger Sep 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default = [
"pnet",
"relay",
"request-response",
"rendezvous",
"secp256k1",
"tcp-async-io",
"uds",
Expand All @@ -48,6 +49,7 @@ plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
relay = ["libp2p-relay"]
request-response = ["libp2p-request-response"]
rendezvous = ["libp2p-rendezvous"]
tcp-async-io = ["libp2p-tcp", "libp2p-tcp/async-io"]
tcp-tokio = ["libp2p-tcp", "libp2p-tcp/tokio"]
uds = ["libp2p-uds"]
Expand Down Expand Up @@ -78,6 +80,7 @@ libp2p-ping = { version = "0.31.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true }
libp2p-relay = { version = "0.4.0", path = "protocols/relay", optional = true }
libp2p-rendezvous = { version = "0.1.0", path = "protocols/rendezvous", optional = true }
libp2p-request-response = { version = "0.13.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.31.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.25.0", path = "swarm-derive" }
Expand Down Expand Up @@ -115,6 +118,7 @@ members = [
"muxers/yamux",
"protocols/floodsub",
"protocols/gossipsub",
"protocols/rendezvous",
"protocols/identify",
"protocols/kad",
"protocols/mdns",
Expand Down
10 changes: 9 additions & 1 deletion core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@
// DEALINGS IN THE SOFTWARE.

fn main() {
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
prost_build::compile_protos(
&[
"src/keys.proto",
"src/envelope.proto",
"src/peer_record.proto",
],
&["src"],
)
.unwrap();
}
30 changes: 30 additions & 0 deletions core/src/envelope.proto
Original file line number Diff line number Diff line change
@@ -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;
}
57 changes: 36 additions & 21 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub mod error;

use self::error::*;
use crate::{keys_proto, PeerId};
use std::convert::{TryFrom, TryInto};

/// Identity keypair of a node.
///
Expand Down Expand Up @@ -205,6 +206,7 @@ impl PublicKey {
/// that the signature has been produced by the corresponding
/// private key (authenticity), and that the message has not been
/// tampered with (integrity).
#[must_use]
pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
use PublicKey::*;
match self {
Expand All @@ -221,7 +223,35 @@ impl PublicKey {
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
use prost::Message;

let public_key = match self {
let public_key = keys_proto::PublicKey::from(self);

let mut buf = Vec::with_capacity(public_key.encoded_len());
public_key
.encode(&mut buf)
.expect("Vec<u8> 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<PublicKey, DecodingError> {
use prost::Message;

let pubkey = keys_proto::PublicKey::decode(bytes)
.map_err(|e| DecodingError::new("Protobuf").source(e))?;

pubkey.try_into()
}

/// Convert the `PublicKey` into the corresponding `PeerId`.
pub fn to_peer_id(&self) -> PeerId {
self.into()
}
}

impl From<&PublicKey> for keys_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(),
Expand All @@ -236,24 +266,14 @@ impl PublicKey {
r#type: keys_proto::KeyType::Secp256k1 as i32,
data: key.encode().to_vec(),
},
};

let mut buf = Vec::with_capacity(public_key.encoded_len());
public_key
.encode(&mut buf)
.expect("Vec<u8> 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<PublicKey, DecodingError> {
use prost::Message;

#[allow(unused_mut)] // Due to conditional compilation.
let mut pubkey = keys_proto::PublicKey::decode(bytes)
.map_err(|e| DecodingError::new("Protobuf").source(e))?;
impl TryFrom<keys_proto::PublicKey> for PublicKey {
type Error = DecodingError;

fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
let key_type = keys_proto::KeyType::from_i32(pubkey.r#type)
.ok_or_else(|| DecodingError::new(format!("unknown key type: {}", pubkey.r#type)))?;

Expand Down Expand Up @@ -281,11 +301,6 @@ impl PublicKey {
}
}
}

/// Convert the `PublicKey` into the corresponding `PeerId`.
pub fn to_peer_id(&self) -> PeerId {
self.into()
}
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ mod keys_proto {
include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
}

mod envelope_proto {
include!(concat!(env!("OUT_DIR"), "/envelope_proto.rs"));
}

mod peer_record_proto {
include!(concat!(env!("OUT_DIR"), "/peer_record_proto.rs"));
}

/// Multi-address re-export.
pub use multiaddr;
pub type Negotiated<T> = multistream_select::Negotiated<T>;
Expand All @@ -51,6 +59,8 @@ pub mod either;
pub mod identity;
pub mod muxing;
pub mod network;
pub mod peer_record;
pub mod signed_envelope;
pub mod transport;
pub mod upgrade;

Expand All @@ -61,6 +71,8 @@ pub use multihash;
pub use muxing::StreamMuxer;
pub use network::Network;
pub use peer_id::PeerId;
pub use peer_record::PeerRecord;
pub use signed_envelope::SignedEnvelope;
pub use translation::address_translation;
pub use transport::Transport;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo};
Expand Down
2 changes: 1 addition & 1 deletion core/src/network/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
/// A connection may close if
///
/// * it encounters an error, which includes the connection being
/// closed by the remote. In this case `error` is `Some`.
/// closed by the remote. In this case `error` is `ome`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// closed by the remote. In this case `error` is `ome`.
/// closed by the remote. In this case `error` is `Some`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #2218. Unfortunately I can not apply suggestions on a branch on the comit-network repository.

/// * it was actively closed by [`EstablishedConnection::start_close`],
/// i.e. a successful, orderly close. In this case `error` is `None`.
/// * it was actively closed by [`super::peer::ConnectedPeer::disconnect`] or
Expand Down
27 changes: 27 additions & 0 deletions core/src/peer_record.proto
Original file line number Diff line number Diff line change
@@ -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;
}
Loading