1261 Commits

Author SHA1 Message Date
Max Inden
e32ff74809
swarm/one_shot: Initialize handler with KeepAlive::Until (#1698)
* swarm/one_shot: Add test for not keeping alive idle connection

A `OneShotHandler` without any ongoing requests should not keep the
underlying connection alive indefinitely.

* swarm/one_shot: Initialize handler with KeepAlive::Until

The `OneShotHandler` `keep_alive` property is altered on incoming and
outgoing reqeusts. By default it is initialized in `KeepAlive::Yes`. In
case there are no incoming or outgoing requests happening, this state is
never changed and thus the handler keeps the underlying connection alive
indefinitely.

With this commit the handler is initialized with `KeepAlive::Until`. As
before the `keep_alive` timer is updated on incoming requests and set to
`KeepAlive::Yes` on outgoing requests.

* swarm/one_shot: Move KeepAlive logic to poll

A `ProtocolsHandler` can be created before the underlying connection is
established. Thus setting a keep alive timeout might be problematic.
Instead set `keep_alive` to `Yes` at construction and alter it within
`ProtocolsHandler::poll`.

* swarm/CHANGELOG: Add entry for OneShotHandler keep-alive
2020-08-13 11:18:20 +02:00
Age Manning
9eca739331
protocols/gossipsub/CHANGELOG: Add entry for signing config
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-08-12 17:59:16 +02:00
Toralf Wittner
30de0b4d64
Add Throttled to libp2p-request-response. (#1696)
* Use a single exchange instead of two one_shots.

* Add `Throttled` to libp2p-request-response.

Wraps the existing `RequestResponse` behaviour and applies strict limits
to the number of inbound and outbound requests per peer.

The wrapper is opt-in and if not used, the protocol behaviour of
`RequestResponse` does not change. This PR also does not introduce
an extra protocol, hence the limits applied need to be known a priori
for all nodes which is not always possible or desirable. As mentioned
in #1687 I think that we should eventually augment the protocol with
metadata which allows a more dynamic exchange of requests and responses.

This PR also replaces the two oneshot channels with a single one from the
scambio crate which saves one allocation per request/response. If not
desirable because the crate has seen less testing the first commit could
be reverted.

* Fix rustdoc error.

* Remove some leftovers from development.

* Add docs to `NetworBehaviourAction::{map_in,map_out}`.

* Apply suggestions from code review

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>

* Add `ping_protocol_throttled` test.

* Add another test.

* Revert "Use a single exchange instead of two one_shots."

This reverts commit e34e1297d411298f6c69e238aa6c96e0b795d989.

# Conflicts:
#	protocols/request-response/Cargo.toml
#	protocols/request-response/src/handler/protocol.rs

* Update CHANGELOG.

* Update CHANGELOG.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-08-12 16:04:54 +02:00
Roman Borschel
b595972961
[ping] Refactor the ping protocol for conformity. (#1692)
* Refactor the ping protocol.

Such that pings are sent over a single substream, as it is
done in other libp2p implementations. Note that, since each
peer sends its pings over a single, dedicated substream,
every peer that participates in the protocol has effectively
two open substreams.

* Cleanup

* Update ping changelog.

* Update protocols/ping/src/protocol.rs

Co-authored-by: Max Inden <mail@max-inden.de>

Co-authored-by: Max Inden <mail@max-inden.de>
2020-08-10 12:54:55 +02:00
Roman S. Borschel
5cd981b875 Update swarm changelog. 2020-08-10 10:13:50 +02:00
Ashley
b6ec95eb77
Add as_ref and as_mut functions to Toggle (#1684) 2020-08-10 10:11:58 +02:00
Guillaume GONI
32df8ea192
add missing feature flag for "noise" to build_tcp_ws_noise_mplex_yamux and build_development_transport (#1693)
* add missing feature flag for noise to build_tcp_ws_noise_mplex_yamux and build_development_transport

* removing useless secio feature flags
2020-08-10 08:29:07 +02:00
Max Inden
5139ec3ace
protocols/kad: Implement NetworkBehaviour::inject_address_change (#1649)
With 826f513 a `StreamMuxer` can notify that the address of a remote
peer changed. This is needed to support the transport protocol QUIC as
remotes can change their IP addresses within the lifetime of a single
connection.

This commit implements the `NetworkBehaviour::inject_address_change`
handler to update the Kademlia routing table accordingly.
2020-08-06 15:35:24 +02:00
Max Inden
7601514eb8
protocols/kad: Expose kbucket distance range (#1680)
Add `KBucketRef::range` exposing the minimum inclusive and maximum
inclusive `Distance` for the bucket.
2020-08-06 10:20:32 +02:00
Niklas Adolfsson
18390fa38e
[core]: fix typo in CHANGELOG.md (#1688) 2020-08-04 23:13:38 +02:00
Roman Borschel
8e1d4edb8b
[core/swarm] Emit events for active connection close and fix disconnect(). (#1619)
* Emit events for active connection close and fix `disconnect()`.

The `Network` does currently not emit events for actively
closed connections, e.g. via `EstablishedConnection::close`
or `ConnectedPeer::disconnect()`. As a result, when actively
closing connections, there will be `ConnectionEstablished`
events emitted without eventually a matching `ConnectionClosed`
event. This seems undesirable and has the consequence that
the `Swarm::ban_peer_id` feature in `libp2p-swarm` does not
result in appropriate calls to `NetworkBehaviour::inject_connection_closed`
and `NetworkBehaviour::inject_disconnected`. Furthermore,
the `disconnect()` functionality in `libp2p-core` is currently
broken as it leaves the `Pool` in an inconsistent state.

This commit does the following:

  1. When connection background tasks are dropped
     (i.e. removed from the `Manager`), they
     always terminate immediately, without attempting
     an orderly close of the connection.
  2. An orderly close is sent to the background task
     of a connection as a regular command. The
     background task emits a `Closed` event
     before terminating.
  3. `Pool::disconnect()` removes all connection
     tasks for the affected peer from the `Manager`,
     i.e. without an orderly close, thereby also
     fixing the discovered state inconsistency
     due to not removing the corresponding entries
     in the `Pool` itself after removing them from
     the `Manager`.
  4. A new test is added to `libp2p-swarm` that
     exercises the ban/unban functionality and
     places assertions on the number and order
     of calls to the `NetworkBehaviour`. In that
     context some new testing utilities have
     been added to `libp2p-swarm`.

This addresses https://github.com/libp2p/rust-libp2p/issues/1584.

* Update swarm/src/lib.rs

Co-authored-by: Toralf Wittner <tw@dtex.org>

* Incorporate some review feedback.

* Adapt to changes in master.

* More verbose panic messages.

* Simplify

There is no need for a `StartClose` future.

* Fix doc links.

* Further small cleanup.

* Update CHANGELOGs and versions.

Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-08-04 11:30:09 +02:00
Roman S. Borschel
67f1b94907 Bump version. 2020-08-03 14:56:55 +02:00
Roman S. Borschel
48efd4a2dd Set release date. 2020-08-03 14:53:20 +02:00
Roman Borschel
e0e5dfbe50
[libp2p-noise] Disable sending of legacy handshake payloads (by default). (#1669)
* [libp2p-noise] Disable sending of legacy handshake payloads.

* Update build_development_transport() to use libp2p-noise.

* Update feature flags.

* Replace feature flag by config options.

* Cleanup

* Cleanup

* Cleanup
2020-08-03 14:52:34 +02:00
Age Manning
d8ad7bddf5
protocols/gossipsub: Add message signing and verification configuration (#1583)
This adds optional message signing and verification to the gossipsub protocol as
per the libp2p specifications.

In addition this commit:

- Removes the LruCache received cache and simply uses the memcache in it's
  place.

- Send subscriptions to all peers

- Prevent invalid messages from being gossiped

- Send grafts when subscriptions are added to the mesh

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Rüdiger Klaehn <rklaehn@protonmail.com>
Co-authored-by: Rüdiger Klaehn <rklaehn@gmail.com>
2020-08-03 10:13:43 +02:00
Toralf Wittner
d0ad105433
memory: Do not return broken pipe error on EOF. (#1683)
Signal the end of stream as `None` instead of producing a broken pipe error.
This restores the behaviour prior to PR #1196. I could not find a motivation
for this particular change in the PR and the previous implementation looks
correct to me.
2020-07-31 12:20:21 +02:00
Roman S. Borschel
3ebe916ef3 Update gossipsub changelog. 2020-07-29 09:28:04 +02:00
Rüdiger Klaehn
9662929f38
Debug instance for the gossipsub behaviour (#1673)
* Add debug instances for MessageCache and GossipSub behaviour

* Manual impl of Debug for GossipsubMessage

* Add pretty printing of protocol_id in debug

* Use hex_fmt instead of hex

* Inline StringOrBytes helper struct

Since it is used only once here

* Limit data of gossipsub msg to 20 bytes

Otherwise they might become very large and useless for debugging
2020-07-29 09:25:53 +02:00
Cheng XU
967f39656a
core-derive: use full qualified name when polling NetworkBehaviour (#1681)
* core-derive: use full qualified name when polling NetworkBehaviour

If the users define a custom poll method also named in `poll` like
`#[behaviour(poll_method = "poll")`, it will cause issues as the wrong
`poll` method being called.

So use full qualified name to avoid ambiguity.

Fixes #1679.

* Update changelog for patch release.

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-07-28 13:26:27 +02:00
Demi Obenour
9178459cc8
Automatic fixes by cargo-fix (#1662) 2020-07-27 22:27:33 +02:00
Pierre Krieger
8a08f72e84
Refactor the bandwidth logging to be less magic (#1670)
* Refactor the bandwidth logging to be less magic

* Apply suggestions from code review

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>

* Complete renaming.

* Update changelog.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-07-27 11:05:59 +02:00
Roman Borschel
5291175f38
Spec-compliant noise handshake payloads (Step 1). (#1658)
* Support spec-compliant reading of noise handshake payloads.

See https://github.com/libp2p/rust-libp2p/issues/1631.

This the first of a three-step process to addressing the issue.
In this step, support for reading noise handshake payloads
without an additional length prefix is added, falling back
to attempting a decoding with length prefix on failure.
Length prefixes are still sent in this step. Hence
interoperability with other libp2p implementations is not
yet achieved after with step.

To achieve a better separation of handshake and transport
I/O, the `NoiseFramed` type has been extracted from
`NoiseOutput`. `NoiseFramed` is a `Sink` and `Stream`
of length-delimited Noise protocol messages. This type
is used in the handshake phase. Once a handshake
completes the underlying Noise session transitions to
transport mode and the `NoiseFramed` is wrapped in
the `NoiseOutput` which provides a regular `AsyncRead`
/ `AsyncWrite` I/O resource on top of the framed
encoding. No new buffers are introduced, they are
just split between `NoiseFramed` and `NoiseOutput`.

The second step involves removing the sending of the
length prefix in a subsequent release.

The third step involves removing the support for reading
length-prefixed protobuf payloads.

* Small cleanup.

* Reuse frame decryption buffer.

Since frames are consumed one-by-one, `NoiseFramed` can have
a `BytesMut` decryption buffer, handing out immutable `Bytes`
views for each decrypted message. Since each view gets fully
consumed and dropped before the next frame is read, the
`BytesMut` decryption buffer in `NoiseFramed` can always
reuse the same buffer, only growing it as necessary.

* Simplify.

* Add missing inner poll_flush().

* Improve nested length detection.

* Avoid unnecessary clearing of send buffers.

Thus reducing the necessary zeroing of send buffers on resize,
as per the previous behaviour.

* Prepare release.
2020-07-17 12:27:30 +02:00
Roman Borschel
7c7d6a9d2e
Upgrade ed25519-dalek to pre.4 (#1665)
* Upgrade ed25519-dalek to pre.4

* Update core/src/identity/ed25519.rs

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-07-17 12:01:49 +02:00
tmakarios
cc3438ec37
Align explanation with previously altered example code (#1663)
Co-authored-by: Tim Makarios <git@freespoken.nz>
2020-07-15 11:14:29 +02:00
Demi Obenour
8e7f71b068
Remove libp2p-secio benchmarks (#1661)
They don’t compile anymore.
2020-07-13 12:39:01 +02:00
Roman Borschel
c8b426005f
[request-response] Close substream after writing request/response. (#1660)
* Close substream after writing request/response.

* Update protocols/request-response/src/handler/protocol.rs

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2020-07-13 12:35:32 +02:00
Rüdiger Klaehn
e61ccd22df
Add ord for multiaddr (#1653)
So you can use it in ordering based containers
2020-07-13 12:20:10 +02:00
Roman Borschel
68587ee180
Update rustls dependency. (#1655)
* Update rustls dependency.

* Bump async-tls lower bound.

* Bump async-tls to 0.8.

0.7.1 has been yanked. Since `libp2p-websockets` exposes
neither `async-tls` nor `rustls` on its API, this can
be a patch release.
2020-07-09 11:50:49 +02:00
Roman Borschel
6ad05b0ff1
[libp2p-swarm] Ignore a node's own addresses on dialing. (#1646)
* Ignore a node's own addresses on dialing.

Dialing attempts of a local node to one of its own
addresses for what appears to be a different peer
ID are futile and bound to fail with an `InvalidPeerId`
error. To avoid such futile dialing attempts, filter
out the node's own addresses from the addresses
reported by the `NetworkBehaviour` for any peer.

There can be a few reasons why a `NetworkBehaviour` may
think an address belongs to a different peer, e.g.:

  1. In the context of e.g. `libp2p-kad`, the local node
     may have changed its network identity (e.g. key rotation)
     and "discovers" its former identity in the DHT, with the same
     address(es).
  2. Another peer may erroneously or intentionally, possibly even maliciously,
    report one of the local node's addresses as its own, making the node
    try to connect to itself.

Relates to https://github.com/paritytech/stakingops-issues/issues/18.

* Remove filtering of external addresses.

Since these are obtained from other peers, it would constitute
an attack vector. It is furthermore usually not possible for
a peer behind a NAT to dial its own external address so these
are unlikely to cause `InvalidPeerId` errors as a result of a
peer dialing itself under the expectation of one of its
former peer IDs.
2020-07-08 11:51:49 +02:00
Thomas Eizinger
c4a5497d2d
Allow users to opt out of the NetworkBehaviourEventProcess mechanism (#1630)
* Allow users to opt-out of the NetworkBehaviourEventProcess mechanism

* Add CHANGELOG entry

* Prepare release.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-07-08 11:32:47 +02:00
Pierre Krieger
d645ccb0df
Improve the code of the WebSocket browser binding (#1644)
* Improve the code of the WebSocket browser binding

* Bump version

* Set release date.

Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-07-06 10:21:03 +02:00
Roman Borschel
568a018090
Re-release websockets 0.20.1 as 0.21. (#1642)
* Re-release websockets 0.20.1 as 0.21.

* CI
2020-07-02 10:38:00 +02:00
Roman Borschel
e9952ea9e3
Prepare releases. (#1640)
* Prepare releases.

* Re-export libp2p-request-response from libp2p crate.

* Clean up changelogs.

* Restore upper bounds.

* Version 0.21 for the root crate.

* Stricter lower bound in libp2p-websocket version in root crate.

* Update misc/peer-id-generator/Cargo.toml

Co-authored-by: Toralf Wittner <tw@dtex.org>

* Update release date.

* Update misc/peer-id-generator/Cargo.toml

Co-authored-by: Toralf Wittner <tw@dtex.org>

Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-07-01 15:36:20 +02:00
Mikhail Zabaluev
5e0bb10395
Document all features on docs.rs (#1637)
* Enable tcp-tokio on docs.rs

To have the documentation built for the optional feature,
it has to be enabled in package metadata for docs.rs.

* Document all features on docs.rs

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-06-30 18:01:44 +02:00
Pierre Krieger
79ff1e4d31
Some fixes to #[cfg] regarding Wasi (#1633)
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-06-30 17:31:02 +02:00
Pierre Krieger
826f5130cd
Allow StreamMuxer to notify changes in the address (#1621)
* Allow StreamMuxer to notify changes in the address

* Fix doc link

* Revert accidental rename

* Other accidental rename

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-06-30 17:10:53 +02:00
Roman Borschel
d5b26359bb
Remove the Discovered event. (#1632)
See the discussion in https://github.com/libp2p/rust-libp2p/pull/1628.
2020-06-30 16:54:56 +02:00
Ashley
92b3ce37d5
Support /dns/ as well as /dns4/ and /dns6/ in the websocket wasm ext impl (#1626)
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-06-30 16:25:17 +02:00
Max Inden
5afc27869d
protocols/kad/src/lib: Expose KademliaBucketInserts (#1638) 2020-06-30 13:09:02 +02:00
Pierre Krieger
9049591d46
Use self.config.idle_timeout in Kademlia handler (#1635) 2020-06-30 07:50:45 +02:00
Friedel Ziegelmayer
823c747904
feat: update async-std to 1.6.2 (#1618)
Ref #1612

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-06-29 17:24:38 +02:00
Roman Borschel
eb8cb43508
Add libp2p-request-response protocol. (#1596)
* Add the libp2p-request-response protocol.

This crate provides a generic implementation for request/response
protocols, whereby each request is sent on a new substream.

* Fix OneShotHandler usage in floodsub.

* Custom ProtocolsHandler and multiple protocols.

  1. Implement a custom ProtocolsHandler instead of using
     the OneShotHandler for better control and error handling.
     In particular, all request/response sending/receiving is
     kept in the substreams upgrades and thus the background
     task of a connection.
  2. Support multiple protocols (usually protocol versions)
     with a single `RequestResponse` instance, with
     configurable inbound/outbound support.

* Small doc clarification.

* Remove unnecessary Sync bounds.

* Remove redundant Clone constraint.

* Update protocols/request-response/Cargo.toml

Co-authored-by: Toralf Wittner <tw@dtex.org>

* Update dev-dependencies.

* Update Cargo.tomls.

* Add changelog.

* Remove Sync bound from RequestResponseCodec::Protocol.

Apparently the compiler just needs some help with the scope
of borrows, which is unfortunate.

* Try async-trait.

* Allow checking whether a ResponseChannel is still open.

Also expand the commentary on `send_response` to indicate that
responses may be discard if they come in too late.

* Add `RequestResponse::is_pending`.

As an analogue of `ResponseChannel::is_open` for outbound requests.

* Revert now unnecessary changes to the OneShotHandler.

Since `libp2p-request-response` is no longer using it.

* Update CHANGELOG for libp2p-swarm.

Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-06-29 17:08:40 +02:00
Roman Borschel
7270ed8721
[libp2p-kad] More control & insight for k-buckets. (#1628)
* More control & insight for k-buckets.

1) More control: It is now possible to disable automatic
insertions of peers into the routing table via a new
`KademliaBucketInserts` configuration option. The
default is `OnConnected`, but it can be set to `Manual`,
in which case `add_address` must be called explicitly.
In order to communicate all situations in which a user
of `Kademlia` may want to manually update the routing
table, two new events are introduced:

  * `KademliaEvent::RoutablePeer`: When a connection to
    a peer with a known listen address is established
    which may be added to the routing table. This is
    also emitted when automatic inserts are allowed but
    the corresponding k-bucket is full.
  * `KademliaEvent::PendingRoutablePeer`: When a connection
    to a peer with a known listen address is established
    which is pending insertion into the routing table
    (but may not make it). This is only emitted when
    `OnConnected` (i.e. automatic inserts) are used.

These complement the existing `UnroutablePeer` and
`RoutingUpdated` events. It is now also possible to
explicitly remove peers and addresses from the
routing table.

2) More insight: `Kademlia::kbuckets` now gives an
iterator over `KBucketRef`s and `Kademlia::bucket`
a particular `KBucketRef`. A `KBucketRef` in turn
allows iteration over its entries. In this way,
the full contents of the routing table can be
inspected, e.g. in order to decide which peer(s)
to remove.

* Update protocols/kad/src/behaviour.rs

* Update protocols/kad/src/behaviour.rs

Co-authored-by: Max Inden <mail@max-inden.de>

* Update CHANGELOG.

Co-authored-by: Max Inden <mail@max-inden.de>
2020-06-29 16:44:44 +02:00
Mikhail Zabaluev
b61c3f9d04
Update libp2p-dns doc to include /dns/ support (#1634)
Now that /dns/ is supported, it should be mentioned in
the documentation as well.
2020-06-29 15:05:11 +02:00
Toralf Wittner
7fd69ecf3f
Fix spelling mistake. (#1627) 2020-06-24 15:19:01 +02:00
Toralf Wittner
c9f121201c
Release v0.20.1 (#1624)
- Update `libp2p-kad` CHANGELOG and increment version to 0.20.1.
- Update `libp2p` version to 0.20.1.
- Fix some linter warning in `libp2p-gossipsub` and increment
  version to 0.19.3 and update CHANGELOG.
2020-06-23 11:51:49 +02:00
Toralf Wittner
32cb0a56bd
kad: Avoid from/into IteratorIndex. (#1623)
This caused problems with type inference.
2020-06-23 11:28:43 +02:00
Toralf Wittner
0eef487ef8
Prepare release. (#1620) 2020-06-22 11:41:28 +02:00
Max Inden
e8e41f4efb
CHANGELOG.md: Add entry for disjoint-paths feature in libp2p-kad (#1617) 2020-06-19 13:57:10 +02:00
Max Inden
9dd2d662e9
protocols/kad: Implement S-Kademlia's lookup over disjoint paths v2 (#1473)
The extension paper S-Kademlia includes a proposal for lookups over
disjoint paths. Within vanilla Kademlia, queries keep track of the
closest nodes in a single bucket. Any adversary along the path can thus
influence all future paths, in case they can come up with the
next-closest (not overall closest) hops. S-Kademlia tries to solve the
attack above by querying over disjoint paths using multiple buckets.

To adjust the libp2p Kademlia implementation accordingly this change-set
introduces an additional peers iterator: `ClosestDisjointPeersIter`.
This new iterator wraps around a set of `ClosestPeersIter`
`ClosestDisjointPeersIter` enforces that each of the `ClosestPeersIter`
explore disjoint paths by having each peer instantly return that was
queried by a different iterator before.
2020-06-19 12:22:26 +02:00