1261 Commits

Author SHA1 Message Date
Pierre Krieger
fbc6ea5c5e
Allow injecting an existing connection (#1157) 2019-06-03 17:27:43 +02:00
Roman Borschel
710ce90c6e
Kademlia: Fix premature query termination. (#1155)
* Fix premature query termination.

* Don't skip over 'NotContacted' closest peers.

* Code style tweak.
2019-06-03 12:47:01 +02:00
Roman Borschel
3440d1896e
Send on pending RPCs on established connection. (#1156) 2019-06-03 12:08:01 +02:00
Arve Knudsen
6a2a401458 Add deflate protocol (#1146) 2019-05-29 11:03:50 +02:00
Pierre Krieger
e7ab8eb1c3
Allow a path for WS multiaddresses (#1093)
* Allow a path for WS multiaddresses

* Fix tests

* Finish

* Tests

* Don't accept any path other than /
2019-05-24 14:12:44 +02:00
Pierre Krieger
39c476edeb
Bump secio and wasm-ext crates (#1145) 2019-05-24 11:37:04 +02:00
Thomas Eizinger
5866e6aa93 Add comit-rs to README (#1141) 2019-05-23 20:29:34 +02:00
Pierre Krieger
7c108b66f2
Fix small todo in handled tasks (#1114)
* Fix small todo in handled tasks

* Address review
2019-05-23 15:16:46 +02:00
Pierre Krieger
18bb40bbb9
Switch to parity-send-wrapper (#1143) 2019-05-23 14:04:09 +02:00
Roman Borschel
0bdd637b01
Kademlia: Fix status updates in KBuckets. (#1140)
* Fix status updates in KBuckets.

`KBucket::update` does currently not correctly update the `first_connected_pos`
before reinserting the node. This may result in connected nodes being considered
disconnected and thus eligible for replacement by a pending node if the bucket is
full and a new connected node is added.

Tests have been added for checking that `KBucket::update` preserves the status
and ordering of all other nodes in the bucket.

* Small test improvement.

Set an expectation for the new position, instead of taking the assigned position.
2019-05-23 12:18:13 +02:00
Pierre Krieger
6b78fa0cf7
Update dependencies (#1142)
* parking-lot to 0.8

* zeroize to 0.8
2019-05-23 11:14:34 +02:00
Roman Borschel
b6ab8e64d4
Update Kademlia protobuf. (#1138) 2019-05-22 15:47:46 +02:00
Roman Borschel
09f54df44d
Kademlia: Optimise iteration over closest keys / entries. (#1117)
* Kademlia: Optimise iteration over closest entries.

The current implementation for finding the entries whose keys are closest
to some target key in the Kademlia routing table involves copying the
keys of all buckets into a new `Vec` which is then sorted based on the
distances to the target and turned into an iterator from which only a
small number of elements (by default 20) are drawn.

This commit introduces an iterator over buckets for finding the closest
keys to a target that visits the buckets in the optimal order, based on
the information contained in the distance bit-string representing the
distance between the local key and the target.

Correctness is tested against full-table scans.

Also included:

  * Updated documentation.
  * The `Entry` API was moved to the `kbucket::entry` sub-module for
    ease of maintenance.
  * The pending node handling has been slightly refactored in order to
    bring code and documentation in agreement and clarify the semantics
    a little.

* Rewrite pending node handling and add tests.
2019-05-22 14:49:38 +02:00
Pierre Krieger
8adc5fa069
Add golem to the list of users (#1137) 2019-05-22 11:04:49 +02:00
TriplEight
5b97babe1c new dockerfile (#1122)
* new dockerfile

* caching
2019-05-17 19:55:50 +02:00
Roman Borschel
c80205454a
Improve XOR metric. (#1108)
There are two issues with the current definition and use of Kademlia's
XOR metric:

  1. The distance is currently equated with the bucket index, i.e.
     `distance(a,b) - 1` is the index of the bucket into which either
     peer is put by the other. The result is a metric that is not
     unidirectional, as defined in the Kademlia paper and as implemented
     in e.g. libp2p-go and libp2p-js, which is to interpret the result
     of the XOR as an integer in its entirety.

  2. The current `KBucketsPeerId` trait and its instances allow computing
     distances between types with differing bit lengths as well as between
     types that hash all inputs again (i.e. `KadHash`) and "plain" `PeerId`s
     or `Multihash`es. This can result in computed distances that are either
     incorrect as per the requirement of the libp2p specs that all distances
     are to be computed from the XOR of the SHA256 of the input keys, or
     even fall outside of the image of the metric used for the `KBucketsTable`.
     In the latter case, such distances are not currently used as a bucket index
     - they can only occur in the context of comparing distances for the purpose
     of sorting peers - but that still seems undesirable.

These issues are addressed here as follows:

  * Unidirectionality of the XOR metric is restored by keeping the "full"
    integer representation of the bitwise XOR. The result is an XOR metric
    as defined in the paper. This also opens the door to avoiding the
    "full table scan" when searching for the keys closest to a given key -
    the ideal order in which to visit the buckets can be computed with the
    help of the distance bit string.

  * As a simplification and to make it easy to "do the right thing", the
    XOR metric is only defined on an opaque `kbucket::Key` type, partially
    derived from the current `KadHash`. `KadHash` and `KBucketsPeerId`
    are removed.
2019-05-17 17:27:57 +02:00
Pierre Krieger
93d89964e1
Add OptionalUpgrade (#1131) 2019-05-17 15:29:59 +02:00
Pierre Krieger
1d7df0c9ca
Bump versions (#1129) 2019-05-15 19:34:41 +02:00
Pierre Krieger
1be1eaf137
Add secp256k1::SecretKey::to_bytes (#1126) 2019-05-15 19:26:07 +02:00
Andre-Philippe Paquet
4c20d3134c Fix ED25519 signature validation (#1127) 2019-05-15 19:06:29 +02:00
Pierre Krieger
3e4715d829
rw-sink-stream to v0.1.2 (#1125) 2019-05-15 18:49:52 +02:00
Pierre Krieger
bd96b66fb5
Publish version 0.8.0 (#1123) v0.8.0 2019-05-15 16:50:43 +02:00
Pierre Krieger
e9448ec8ca
Allow changing the Kademlia protocol name (#1118)
* Allow changing the Kademlia protocol name

* Expose the method to the behaviour

* Address review
2019-05-15 15:44:51 +02:00
Pierre Krieger
87a352c84f
Add some diagnostics for the same address being reported despite not in list (#1124)
* Add more diagnostics for TCP

* Address review

* Publish libp2p-tcp 0.7.2

* Add another diagnostic
2019-05-15 14:48:26 +02:00
Pierre Krieger
3d4d8df713
Add an OptionalTransport type (#1116)
* Add OptionalTransport

* Fix copyright

* Some documentation

* Apply suggestions from code review

Co-Authored-By: Toralf Wittner <tw@dtex.org>
2019-05-15 11:26:43 +02:00
Pierre Krieger
53e22281cf
Reexport ConnectedPoint at the root (#1119) 2019-05-14 20:26:29 +02:00
Toralf Wittner
057b379541 Replace secp256k1 crate with libsecp256k1. (#1029)
* Replace `secp256k1` crate with `libsecp256k1`.

Unfortunately we could not implement `AsRef<[u8]>` for `SecretKey`
as the crate does not provide a means to do so.

* Fix `DecodingError` invocation.

* Remove the cc for wasm

* Revert "Remove the cc for wasm"

This reverts commit 3a19db35e62931c6e9ffbff6c21f9b0d7ae5403a.

* Fix CircleCI build
2019-05-14 19:33:30 +02:00
Pierre Krieger
43d9084735
Add parity_multiaddr::from_websockets_url (#1113)
* Add parity_multiaddr::from_websockets_url

* Make from_websockets_url more genreic

* Oops, forgot the file

* Use the URL crate

* Add dns_and_port test

* Address review

* Add support for unix
2019-05-14 14:19:07 +02:00
Pierre Krieger
c2398adf67
Add implementations of prepare_uninitialized_buffer and read_buf where relevant (#1107)
* Fix #1080

* Fix browser WebSockets
2019-05-10 11:26:18 +02:00
Pierre Krieger
089e349671
Pass the ConnectedPoint to into_handler() (#1085) 2019-05-10 11:05:22 +02:00
Toralf Wittner
fd0e48bf37 Add IntoProtocolsHandler::inbound_protocol. (#1099) 2019-05-08 20:23:28 +02:00
Roman Borschel
61b236172b
Some Kademlia code cleanup. (#1101) 2019-05-08 10:10:58 +02:00
Roman Borschel
8537eb38b9
Integrate identity keys with libp2p-noise for authentication. (#1027)
* Integrate use of identity keys into libp2p-noise.

In order to make libp2p-noise usable with a `Swarm`, which requires a
`Transport::Output` that is a pair of a peer ID and an implementation
of `StreamMuxer`, it is necessary to bridge the gap between static
DH public keys and public identity keys from which peer IDs are derived.

Because the DH static keys and the identity keys need not be
related, it is thus generally necessary that the public identity keys are
exchanged as part of the Noise handshake, which the Noise protocol
accomodates for through the use of handshake message payloads.

The implementation of the existing (IK, IX, XX) handshake patterns is thus
changed to send the public identity keys in the handshake payloads.
Additionally, to facilitate the use of any identity keypair with Noise
handshakes, the static DH public keys are signed using the identity
keypairs and the signatures sent alongside the public identity key
in handshake payloads, unless the static DH public key is "linked"
to the public identity key by other means, e.g. when an Ed25519 identity
keypair is (re)used as an X25519 keypair.

* libp2p-noise doesn't build for wasm.

Thus the development transport needs to be still constructed with secio
for transport security when building for wasm.

* Documentation tweaks.

* For consistency, avoid wildcard enum imports.

* For consistency, avoid wildcard enum imports.

* Slightly simplify io:🤝:State::finish.

* Simplify creation of 2-byte arrays.

* Remove unnecessary cast and obey 100 char line limit.

* Update protocols/noise/src/protocol.rs

Co-Authored-By: romanb <romanb@users.noreply.github.com>

* Address more review comments.

* Cosmetics

* Cosmetics

* Give authentic DH keypairs a distinct type.

This has a couple of advantages:

  * Signing the DH public key only needs to happen once, before
    creating a `NoiseConfig` for an authenticated handshake.

  * The identity keypair only needs to be borrowed and can be
    dropped if it is not used further outside of the Noise
    protocol, since it is no longer needed during Noise handshakes.

  * It is explicit in the construction of a `NoiseConfig` for
    a handshake pattern, whether it operates with a plain `Keypair`
    or a keypair that is authentic w.r.t. a public identity key
    and future handshake patterns may be built with either.

  * The function signatures for constructing `NoiseConfig`s for
    handshake patterns are simplified and a few unnecessary trait
    bounds removed.

* Post-merge corrections.

* Add note on experimental status of libp2p-noise.
2019-05-07 10:22:42 +02:00
Roman Borschel
e44b443b91
Filter requesting peer from results. (#1102)
Although not explicitly mentioned in the paper, it seems clear that
including an entry for the requesting peer in a FIND_NODE response
never gives useful information and just occupies a result slot that may
have been better filled with another peer that the requestor may not
know about.

There is one explicit mention that this is the desired behavior
in a somewhat dated design document of another p2p framework [1]:

"The recipient of a FIND_NODE should never return a triple containing
the nodeID of the requestor."

The same reasoning supposedly applies to the libp2p-specific `GET_PROVIDERS`
request.

[1] http://xlattice.sourceforge.net/components/protocol/kademlia/specs.html#FIND_NODE
2019-05-06 11:40:13 +02:00
Pierre Krieger
77cd2453f7
Reexport libp2p-wasm-ext (#1100) 2019-05-06 11:17:35 +02:00
Roman Borschel
808a7a5ef6
Fix self-dialing in Kademlia. (#1097)
* Fix self-dialing in Kademlia.

Addresses https://github.com/libp2p/rust-libp2p/issues/341 which is the cause
for one of the observations made in https://github.com/libp2p/rust-libp2p/issues/1053.
However, the latter is not assumed to be fully addressed by these changes and
needs further investigation.

Currently, whenever a search for a key yields a response containing the initiating
peer as one of the closest peers known to the remote, the local node
would attempt to dial itself. That attempt is ignored by the Swarm, but
the Kademlia behaviour now believes it still has a query ongoing which is
always doomed to time out. That timeout delays successful completion of the query.
Hence, any query where a remote responds with the ID of the local node takes at
least as long as the `rpc_timeout` to complete, which possibly affects almost
all queries in smaller clusters where every node knows about every other.

This problem is fixed here by ensuring that Kademlia never tries to dial the local node.
Furthermore, `Discovered` events are no longer emitted for the local node
and it is not inserted into the `untrusted_addresses` from discovery, as described
in #341.

This commit also includes a change to the condition for freezing / terminating
a Kademlia query upon receiving a response. Specifically, the condition is
tightened such that it only applies if in addition to `parallelism`
consecutive responses that failed to yield a peer closer to the target, the
last response must also either not have reported any new peer or the
number of collected peers has already reached the number of desired results.
In effect, a Kademlia query now tries harder to actually return `k`
closest peers.

Tests have been refactored and expanded.

* Add another comment.
2019-05-02 21:43:29 +02:00
Toralf Wittner
77ce5a52dd
Add ranked address collection. (#1096)
Keep external addresses in a ranked collection and ensure iteration is
performed in order from highest to lowest rank.
2019-05-02 19:46:27 +02:00
Fedor Sakharov
431f6e0641 remove direct dependencies on multiaddr (#1092) 2019-04-30 20:14:57 +02:00
elferdo
585f84c88a Replace PeerId with Multihash for interface consistency (#1095)
* Change a PeerId for a Multihash

* Update protocols/kad/src/behaviour.rs

Co-Authored-By: elferdo <elferdo@gmail.com>

* Update protocols/kad/src/behaviour.rs

Co-Authored-By: elferdo <elferdo@gmail.com>
2019-04-30 19:39:26 +02:00
Fedor Sakharov
68df8c07cf muxing: adds an error type to streammuxer (#1083)
* muxing: adds an error type to streammuxer

* Update examples/chat.rs

Co-Authored-By: montekki <fedor.sakharov@gmail.com>

* make the trait error type bound to io error
2019-04-28 13:42:18 +02:00
Pierre Krieger
47a775dbce
Add wasm-ext-transport (#1070)
* Add wasm-ext-transport

* Fix paths

* Adjust listen_on to return an Iterator

* Adjust read() to produce Iterator

* Remove map_err when possible

* Cargo fmt

* Adjust errors as well

* Small comment fix

* Revert "Adjust errors as well"

This reverts commit 97eb5149dafeaca9910a2809ee47e6d332ce1cb1.

* More dev on Debug

* Differentiate based on error
2019-04-25 15:44:40 +02:00
Pierre Krieger
ce4ca3cc75
Switch to wasm-timer (#1071) 2019-04-25 15:08:06 +02:00
Roman Borschel
9a525d5dea
Add back simple, optional keep-alive to libp2p-ping. (#1088)
This is now a very simple option serving multiple purposes:

  * It allows for stable (integration) tests involving a Swarm, which
    are otherwise subject to race conditions due to the connection being
    allowed to terminate at any time with `KeepAlive::No`
    (which remains the default).

  * It makes for a more entertaining ping example which continuously
    sends pings.

  * Maybe someone wants to use the ping protocol for application-layer
    connection keep-alive after all.
2019-04-25 10:33:57 +02:00
Pierre Krieger
a375d558b1
Add a OneSubstreamMuxer (#1079)
* Add a OneSubstreamMuxer

* Renames and tweaks

* Add the file back
2019-04-23 15:08:59 +02:00
Pierre Krieger
b4345ee8ba
Bump to 0.7.0 (#1081)
* Bump to 0.7.0

* Update CHANGELOG.md

Co-Authored-By: tomaka <pierre.krieger1708@gmail.com>

* Update for #1078

* New version of multihash and multiaddr as well
v0.7.0
2019-04-23 13:03:29 +02:00
Roman Borschel
8cde987e6d Rename KeepAlive constructors. (#1078)
* KeepAlive::Now => KeepAlive::No
  * KeepAlive::Forever => KeepAlive::Yes

As suggested in #1072.
2019-04-23 11:58:49 +02:00
Pierre Krieger
5c34f8a0ed
&mut self -> &mut Self (#1073) 2019-04-23 10:54:25 +02:00
Roman Borschel
45f308c815
Small addendum to #1072. (#1077)
* Small addendum to #1072.

  * Missed two review comments related to documentation.
  * Avoid creating new `Delay`s when possible, i.e. when the deadline
    did not change, since they're not exactly cheap and returning
    `KeepAlive::Until(t)` with the same instant `t` over a prolonged
    period of time is common.

* Even better.
2019-04-21 15:48:50 +02:00
Roman Borschel
d5c6370b15
Remove libp2p-ping keep-alive functionality. (#1067)
* Fix connection & handler shutdown when using `KeepAlive::Now`.

Delay::new(Instant::now()) is never immediately ready, resulting in
`KeepAlive::Now` to have no effect, since the delay is re-created on
every execution of `poll()` in the `NodeHandlerWrapper`. It can also
send the node handler into a busy-loop, since every newly
created Delay will trigger a task wakeup, which creates a new Delay
with Instant::now(), and so forth.

The use of `Delay::new(Instant::now())` for "immediate" connection shutdown
is therefore removed here entirely. An important assumption is thereby
that as long as the node handler non-empty `negotiating_in` and `negotiating_out`,
the handler is not dependent on such a Delay for task wakeup.

* Correction to the libp2p-ping connection timeout.

The current connection timeout is always short of one `interval`,
because the "countdown" begins with the last received or sent pong
(depending on the policy). In effect, the current default config has
a connection timeout of 5 seconds (20 - 15) from the point when a ping is sent.

Instead, the "countdown" of the connection timeout should always begin
with the next scheduled ping. That also makes all configurations valid,
avoiding pitfalls.

The important properties of the ping handler are now checked to hold for all
configurations, in particular:

  * The next ping must be scheduled no earlier than the ping interval
    and no later than the connection timeout.

  * The "countdown" for the connection timeout starts on the next ping,
    i.e. the full connection timeout remains at the instant when the
    next ping is sent.

* Do not keep connections alive.

The ping protocol is not supposed to keep otherwise idle connections
alive, only to add an additional condition for terminating them in
the form of a configurable number of consecutive failed ping requests.

In this context, the `PingPolicy` does not seem useful any longer.
2019-04-20 16:16:31 +02:00
Roman Borschel
8d388d25d4
Fix connection & handler shutdown when using KeepAlive::Now. (#1072)
* Fix connection & handler shutdown when using `KeepAlive::Now`.

Delay::new(Instant::now()) is never immediately ready, resulting in
`KeepAlive::Now` to have no effect, since the delay is re-created on
every execution of `poll()` in the `NodeHandlerWrapper`. It can also
send the node handler into a busy-loop, since every newly
created Delay will trigger a task wakeup, which creates a new Delay
with Instant::now(), and so forth.

The use of `Delay::new(Instant::now())` for "immediate" connection shutdown
is therefore removed here entirely. An important assumption is thereby
that as long as the node handler non-empty `negotiating_in` and `negotiating_out`,
the handler is not dependent on such a Delay for task wakeup.

* Trigger CI.
2019-04-20 16:00:21 +02:00