38 Commits

Author SHA1 Message Date
Roman Borschel
ec0f8a3150
[tcp] Port-reuse, async-io, if-watch (#1887)
* Update tomls.

* Let transports decide when to translate.

* Improve tcp transport.

* Update stuff.

* Remove background task. Enhance documentation.

To avoid spawning a background task and thread within
`TcpConfig::new()`, with communication via unbounded channels,
a `TcpConfig` now keeps track of the listening addresses
for port reuse in an `Arc<RwLock>`. Furthermore, an `IfWatcher`
is only used by a `TcpListenStream` if it listens on any interface
and directly polls the `IfWatcher` both for initialisation and
new events.

Includes some documentation and test enhancements.

* Reintroduce feature flags for tokio vs async-io.

To avoid having an extra reactor thread running for tokio
users and to make sure all TCP I/O uses the mio-based
tokio reactor.

Thereby run tests with both backends.

* Add missing files.

* Fix docsrs attributes.

* Update transports/tcp/src/lib.rs

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

* Restore chat-tokio example.

* Forward poll_write_vectored for tokio's AsyncWrite.

* Update changelogs.

Co-authored-by: David Craven <david@craven.ch>
Co-authored-by: Max Inden <mail@max-inden.de>
2021-01-12 13:35:11 +01:00
Max Inden
c98b9ef407
*: Switch futures_codec to asynchronous-codec (#1908)
`futures-codec` has not been updated in the recent months. It still
depends on `bytes` `v0.5` preventing all downstream dependencies to
upgrade to `bytes` `v1.0`.

This commit replaces `futures_codec` in favor of `asynchronous-codec`
The latter is a fully upgraded fork of the former.

In addition this commit upgrades:

- bytes to v1
- unsigned-varint to v0.6.0
- prost to v0.7
2021-01-12 12:48:37 +01:00
Max Inden
d918e9a79d
*: Update changelogs and cargo tomls for new reqresp release 2021-01-04 18:51:05 +01:00
David Craven
6a85eb46fc
Re-export throttled-specific response channel (#1902) 2021-01-04 18:45:01 +01:00
Max Inden
dd9f56e7f9
*CHANGELOG.md: Prepare v0.33.0 release (#1895) 2020-12-18 10:03:20 +01:00
Max Inden
22817b5d2b
swarm: Remove NotifyHandler::All (#1880)
Remove `NotifyHandler::All` thus removing the requirement for events
send from a `NetworkBehaviour` to a `ProtocolsHandler` to be `Clone`. An
implementor of `NetworkBehaviour` can still notify all
`ProtocolHandler`s for a given peer by emitting one `NotifyHandler`
event per connection to that peer.
2020-12-17 11:01:45 +01:00
Max Inden
0968b049fe
protocols/request-response: Derive Clone for {Inbound,Outbound}Failure (#1891) 2020-12-16 16:38:45 +01:00
Max Inden
3af5ba45f3
protocols/request-response: Emit InboundFailure::ConnectionClosed (#1886)
A user of libp2p-request-response is guaranteed to receive an additional event
after receiving a request via `RequestResponseEvent::Message`.

After receiving the request:

- If the user responds in time and the connection is still alive, the
user can expect a `ResponseSent`.

- If the user drops the response channel, the user can expect an
`InboundFailure::ResponseOmission`.

- If the user does not respond in time, the user can expect an
`InboundFailure::Timeout`.

Thus far the user did not receive an event when the connection to the
peer closes. With this commit:

- If the connection to the peer closes before the users calls
`send_response` or after the user calls `send_response` but before the
response can be send on the network, the user can expect an
`InboundFailure::ConnectionClosed`.
2020-12-16 16:03:35 +01:00
David Craven
23b0aa016f
Stack allocated PeerId (#1874)
* Stack allocate PeerId.

* Update stuff.

* Upgrade rusttls to fix build.

* Remove unnecessary manual implementations.

* Remove PeerId::into_bytes.

* Remove bytes dependency.

* Perform some cleanup.

* Use Into<kbucket::Key<K>>.

* Update versions and changelogs.

* Fix PR link.

* Fix benchmarks.

Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-12-15 14:40:39 +01:00
Roman Borschel
9b5d5dbc61
Prepare v0.32 (#1879) 2020-12-08 12:07:20 +01:00
Roman Borschel
3edc467d75
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.

At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.

Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.

As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.

* Update protocols/request-response/src/lib.rs

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

* Address some minor clippy warnings. (#1868)

* Track pending credit request IDs.

In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.

* Small cleanup

* Cleanup

* Update versions and changelogs.

* Unreleased

Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
Roman Borschel
bc102bc923
Prepare release of libp2p-v0.31 and all unreleased sub-crates. (#1857)
* Prepare v0.31

* Update core-derive

* Update minimum multiaddr patch version in libp2p crate.
2020-11-25 15:30:13 +01:00
Roman Borschel
1bd013c843
[swarm] Configurable and "infinite" scores for external addresses. (#1842)
* Add "infinite" scores for external addresses.

Extend address scores with an infinite cardinal, permitting
addresses to be retained "forever" or until explicitly removed.

Expose (external) address scores on the API.

* Update swarm/src/registry.rs

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

* Fix compilation.

* Update CHANGELOG

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-11-18 15:52:33 +01:00
Volker Mische
3f38c1c8bb
feat: upgrade multihash (#1792)
* feat: upgrade to multihash 0.13

`multihash` changes a lot internally, it is using stack allocation instead
of heap allocation. This leads to a few limitations in regards on how
`Multihash` can be used.

Therefore `PeerId` is now using a `Bytes` internally so that only minimal
changes are needed.

* Update versions and changelogs.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: Roman S. Borschel <roman@parity.io>
2020-11-17 11:15:20 +01:00
Max Inden
5f8a003bcd
*: Update changelogs to release v0.30.0 (#1829)
* *: Update changelogs to release v0.30.0

* muxers/mplex/CHANGELOG: Reference pull request

* misc/multistream-select/CHANGELOG: Update date
2020-11-09 17:46:07 +01:00
Roman Borschel
2ba78b4ce7
[mplex, yamux] Streamline configuration API. (#1822)
* Streamline mplex and yamux configurations.

  * For all configuration options that exist for both multiplexers
    and have the same semantics, use the same names for the
    configuration.
  * Rename `Config` to `YamuxConfig` for consistentcy with
    the majority of other protocols, e.g. `MplexConfig`, `PingConfig`,
    `KademliaConfig`, etc.
  * Completely hide `yamux` APIs within `libp2p-yamux`. This allows
    to fully control the libp2p API and streamline it with other
    muxer APIs, consciously choosing e.g. which configuration options
    to make configurable in libp2p and which to fix to certain values.
    It does also not necessarily prescribe new incompatible version bumps of
    yamux for `libp2p-yamux`, as no `yamux` types are exposed. The cost
    is some more duplication of configuration options in the API, as well
    as the need to update `libp2p-yamux` if `yamux` introduces new
    configuration options that `libp2p-yamux` wants to expose as well.

* Update CHANGELOGs.
2020-11-06 09:46:22 +01:00
dependabot[bot]
8f53ec61fd
Update minicbor requirement from 0.6 to 0.7 (#1819)
Updates the requirements on [minicbor](https://gitlab.com/twittner/minicbor) to permit the latest version.
- [Release notes](https://gitlab.com/twittner/minicbor/tags)
- [Commits](https://gitlab.com/twittner/minicbor/compare/minicbor-v0.6.0...minicbor-v0.7.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-11-02 09:27:11 +01:00
Thomas Eizinger
335e55e60d
Remove ConnectionInfo trait (#1813)
In all cases, we pass the PeerId directly as the connection info.
The flexbility of doing something different here was originally
envisioned but turned out to be never needed.

For reference see: https://github.com/libp2p/rust-libp2p/issues/1798#issuecomment-714526056

Co-authored-by: Max Inden <mail@max-inden.de>
2020-10-30 15:51:27 +01:00
Roman Borschel
a5f210adf3
Prepare release. (#1797) 2020-10-16 20:36:47 +02:00
Roman Borschel
dc56d44edb
Refine boxing during transport construction. (#1794)
* Rework boxing during transport construction.

* Cleanup

* Fix chat-tokio example.

* Update changelogs and versions.
2020-10-16 16:53:02 +02:00
Roman Borschel
3e31ea9337
[multistream-select] Fix panic with V1Lazy (regression) and more convenient transport boxing. (#1783)
* [multistream-select] Fix panic with V1Lazy and add integration tests.

Fixes a panic when using the `V1Lazy` negotiation protocol,
a regression introduced in https://github.com/libp2p/rust-libp2p/pull/1484.

Thereby adds integration tests for a transport upgrade with both
`V1` and `V1Lazy` to the `multistream-select` crate to prevent
future regressions.

* Cleanup.

* Update changelog.
2020-10-07 11:10:54 +02:00
dependabot[bot]
c14f0c5b18
Update minicbor requirement from 0.5 to 0.6 (#1772)
* Update minicbor requirement from 0.5 to 0.6

Updates the requirements on [minicbor](https://gitlab.com/twittner/minicbor) to permit the latest version.
- [Release notes](https://gitlab.com/twittner/minicbor/tags)
- [Commits](https://gitlab.com/twittner/minicbor/compare/minicbor-v0.5.0...minicbor-v0.6.0)

Signed-off-by: dependabot[bot] <support@github.com>

* *: Update changelogs and cargo tomls

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Max Inden <mail@max-inden.de>
2020-09-23 21:28:15 +02:00
Roman Borschel
a262609db4
Prepare release(s). (#1738) 2020-09-09 12:20:25 +02:00
Toralf Wittner
f86bc01e20
Add back the LRU cache to Throttled. (#1731)
* Restore `RequestResponse::throttled`.

In contrast to the existing "throttled" approach this PR adds back-
pressure to the protocol without requiring pre-existing knowledge
of all nodes about their limits. It adds small, CBOR-encoded headers
to the actual payload data. Extra credit messages communicate back
to the sender how many more requests it is allowed to send.

* Remove some noise.

* Resend credit grant after connection closed.

Should an error in some lower layer cause a connection to be closed,
our previously sent credit grant may not have reached the remote peer.
Therefore, pessimistically, a credit grant is resent whenever a
connection is closed. The remote ignores duplicate grants.

* Remove inbound/outbound tracking per peer.

* Send ACK as response to duplicate credit grants.

* Simplify.

* Fix grammar.

* Incorporate review feedback.

- Remove `ResponseSent` which was a leftover from previous attemps
  and issue a credit grant immediately in `send_response`.
- Only resend credit grants after a connection is closed if we are
  still connected to this peer.

* Move codec/header.rs to throttled/codec.rs.

* More review suggestions.

* Generalise `ProtocolWrapper` and use shorter prefix.

* Update protocols/request-response/src/lib.rs

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

* Update protocols/request-response/src/throttled.rs

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

* Update protocols/request-response/src/throttled.rs

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

* Minor comment changes.

* Limit max. header size to 8KiB

* Always construct initial limit with 1.

Since honest senders always assume a send budget of 1 and wait for
credit afterwards, setting the default limit to a higher value
can only become effective after informing the peer about it which
means leaving `max_recv` at 1 and setting `next_max` to the desired
value.

* Use LRU cache to keep previous peer infos.

Peers may send all their requests, reconnect and send again all their
requests, starting from a fresh budget. The LRU cache keeps the peer
information around and reuses it when the peer reconnects, continuing
with the previous remaining limit.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-09-09 11:04:20 +02:00
Roman S. Borschel
08a7b1dcd2 Update request-response changelog. 2020-09-07 17:25:15 +02:00
Toralf Wittner
d988b05f85
Restore RequestResponse::throttled. (#1726)
* Restore `RequestResponse::throttled`.

In contrast to the existing "throttled" approach this PR adds back-
pressure to the protocol without requiring pre-existing knowledge
of all nodes about their limits. It adds small, CBOR-encoded headers
to the actual payload data. Extra credit messages communicate back
to the sender how many more requests it is allowed to send.

* Remove some noise.

* Resend credit grant after connection closed.

Should an error in some lower layer cause a connection to be closed,
our previously sent credit grant may not have reached the remote peer.
Therefore, pessimistically, a credit grant is resent whenever a
connection is closed. The remote ignores duplicate grants.

* Remove inbound/outbound tracking per peer.

* Send ACK as response to duplicate credit grants.

* Simplify.

* Fix grammar.

* Incorporate review feedback.

- Remove `ResponseSent` which was a leftover from previous attemps
  and issue a credit grant immediately in `send_response`.
- Only resend credit grants after a connection is closed if we are
  still connected to this peer.

* Move codec/header.rs to throttled/codec.rs.

* More review suggestions.

* Generalise `ProtocolWrapper` and use shorter prefix.

* Update protocols/request-response/src/lib.rs

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

* Update protocols/request-response/src/throttled.rs

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

* Update protocols/request-response/src/throttled.rs

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

* Minor comment changes.

* Limit max. header size to 8KiB

* Always construct initial limit with 1.

Since honest senders always assume a send budget of 1 and wait for
credit afterwards, setting the default limit to a higher value
can only become effective after informing the peer about it which
means leaving `max_recv` at 1 and setting `next_max` to the desired
value.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-09-07 17:22:40 +02:00
Roman S. Borschel
c94262d03a Set development versions appropriately and update changelogs. 2020-09-03 12:31:45 +02:00
Toralf Wittner
0b05b21093
Add ProtocolsHandler::InboundOpenInfo. (#1714)
* Add `ProtocolsHandler::InboundOpenInfo`.

* Update swarm/src/protocols_handler/multi.rs

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

* Update CHANGELOGs and versions.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-08-23 16:57:20 +02:00
Max Inden
3d07a05f8b
*: Prepare v0.24.0 release (#1713)
* */CHANGELOG: Update dates

* Cargo.toml: Update to 0.24.0
2020-08-18 17:04:34 +02:00
Toralf Wittner
b4ad2d6297
Disable RequestResponse::throttled. (#1711)
Can be enabled again after #1706 is resolved.

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-08-18 14:29:38 +02:00
Roman Borschel
96484fcd5e
Fix connection keep-alive for request-response. (#1700) 2020-08-13 13:13:23 +02:00
Toralf Wittner
7d47ada077
Fix ping test in request-response. (#1702)
The codec impl did not check that it actually read any bytes in
`read_request` and `read_response`. The used `read_one` function
does not error on EOF either, so instead of signalling connection
loss the codec could produce empty `Ping` or `Pong` messages.
2020-08-13 12:49: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
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
Demi Obenour
9178459cc8
Automatic fixes by cargo-fix (#1662) 2020-07-27 22:27:33 +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
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
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