37 Commits

Author SHA1 Message Date
Thomas Eizinger
803eb10dcd
Disambiguate calls to NetworkBehaviour::inject_event (#1543)
* Disambiguate calls to NetworkBehaviour::inject_event

There is a gnarly edge-case with the custom-derive where rustc
cannot disambiguate the call if:

- The NetworkBehaviourEventProcess trait is imported
- We nest NetworkBehaviours that use the custom-derive

* Update misc/core-derive/src/lib.rs

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

* Fix build and add CHANGELOG

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-04-09 16:01:26 +02:00
Tobin Harding
b1059cd801
Pass the error to inject_listener_closed method (#1517)
* Pass the error to inject_listener_closed method

If there is an error when the listener closes, found in the
`NetworkEvent::ListenerClosed` `reason` field, we would like to pass it
on to the `inject_listener_closed()` method so that implementors of this
method have access to it.

Add an error parameter to `inject_listener_closed`.  Convert the
`reason` field from a `Result` to an `Option` and if there is an error
pass `Some(error)` at the method call site.

* Pass 'reason' as a Result

* Finish change

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-04-01 14:49:42 +02:00
Roman Borschel
be970466b3
Full support for multiple connections per peer in libp2p-swarm. (#1519)
* [libp2p-swarm] Make the multiple connections per peer first-class.

This commit makes the notion of multiple connections per peer
first-class in the API of libp2p-swarm, introducing the new
callbacks `inject_connection_established` and
`inject_connection_closed`. The `endpoint` parameter from
`inject_connected` and `inject_disconnected` is removed,
since the first connection to open may not be the last
connection to close, i.e. it cannot be guaranteed,
as was previously the case, that the endpoints passed
to these callbacks match up.

* Have identify track all addresses.

So that identify requests can be answered with the correct
observed address of the connection on which the request
arrives.

* Cleanup

* Cleanup

* Improve the `Peer` state API.

* Remove connection ID from `SwarmEvent::Dialing`.

* Mark `DialPeerCondition` non-exhaustive.

* Re-encapsulate `NetworkConfig`.

To retain the possibility of not re-exposing all
network configuration choices, thereby providing
a more convenient API on the \`SwarmBuilder\`.

* Rework Swarm::dial API.

* Update CHANGELOG.

* Doc formatting tweaks.
2020-03-31 15:41:13 +02:00
Roman Borschel
8337687b3a
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.

Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.

The gist of these changes is as follows:

The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.

Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.

The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.

[1]: https://github.com/paritytech/substrate/issues/4272

* Fix intra-rustdoc links.

* Update core/src/connection/pool.rs

Co-Authored-By: Max Inden <mail@max-inden.de>

* Address some review feedback and fix doc links.

* Allow responses to be sent on the same connection.

* Remove unnecessary remainders of inject_replaced.

* Update swarm/src/behaviour.rs

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

* Update swarm/src/lib.rs

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

* Update core/src/connection/manager.rs

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

* Update core/src/connection/manager.rs

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

* Update core/src/connection/pool.rs

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

* Incorporate more review feedback.

* Move module declaration below imports.

* Update core/src/connection/manager.rs

Co-Authored-By: Toralf Wittner <tw@dtex.org>

* Update core/src/connection/manager.rs

Co-Authored-By: Toralf Wittner <tw@dtex.org>

* Simplify as per review.

* Fix rustoc link.

* Add try_notify_handler and simplify.

* Relocate DialingConnection and DialingAttempt.

For better visibility constraints.

* Small cleanup.

* Small cleanup. More robust EstablishedConnectionIter.

* Clarify semantics of `DialingPeer::connect`.

* Don't call inject_disconnected on InvalidPeerId.

To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.

* Provide public ConnectionId constructor.

Mainly needed for testing purposes, e.g. in substrate.

* Move the established connection limit check to the right place.

* Clean up connection error handling.

Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.

* Revert change in log level and clarify an invariant.

* Remove inject_replaced entirely.

* Allow notifying all connection handlers.

Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.

* Finishing touches.

Small API simplifications and code deduplication.
Some more useful debug logging.

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
Guillaume GONI
4472840acf
NetworkBehaviour derive fix IntoProtocolsHandler/ProtocolsHandler::select ambiguity (#1466)
* NetworkBehaviour derive fix IntoProtocolsHandler/ProtocolsHandler::select ambiguity

* Missing space misc/core-derive/src/lib.rs

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

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-02-24 16:56:36 +01:00
vms
e2ddab223d
add poll parameters to custom poll function (#1442)
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
2020-02-13 11:17:55 +01:00
Pierre Krieger
1eff4b9823
Simplify trait bounds on NetworkBehaviour (#1405)
* Simplify trait bounds requirements

* More work

* Moar

* Finish

* Fix final tests

* More simplification

* Use separate traits for Inbound/Outbound

* Update gossipsub and remove warnings

* Add documentation to swarm

* Remove BoxSubstream

* Fix tests not compiling

* Fix stack overflow

* Address concerns

* For some reason my IDE ignored libp2p-kad
2020-02-07 16:29:30 +01:00
Joonas Koivunen
7df3e74d79 Enhance NetworkBehaviour docs (#1362)
* add docs about deriving NetworkBehaviour

* add commentary to the chat example

also minor stuff like reordering to match the struct elements, note why the listening address is
reported in poll.

* fix remove the warning on the example ignored field

* Apply suggestions from code review

Thanks for suggestions!

Co-Authored-By: Max Inden <mail@max-inden.de>

* chore cleanup added docs after suggestions

* fix print all listening addresses in examples/chat

not that there should be more than one but it makes sense to repeat the full example here.

* chore remove confusing writing on printing the listening addrs

* fix use intra-docs, spelling

Co-Authored-By: Roman S. Borschel <roman@parity.io>

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
2020-01-14 12:48:16 +01:00
Pierre Krieger
42a45e2630
Remove Negotiated from upgrade traits (#1388)
* Remove Negotiated from upgrade traits

* Remove import
2020-01-13 14:34:43 +01:00
Pierre Krieger
213c633b92
Pass &mut std::task::Context to poll 2019-12-10 16:54:38 +01:00
Pierre Krieger
98dac8d509
Merge master into stable-futures (#1325)
* Update parking_lot to v0.9 (#1300)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Publish 0.13.1 (#1304)

* Publish 0.13.1

* Update CHANGELOG.md

Co-Authored-By: Toralf Wittner <tw@dtex.org>

* Update some deps of core-derive (#1299)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
2019-11-28 18:03:59 +01:00
Toralf Wittner
b7644722ee
Fix examples and update core-derive. (#1317) 2019-11-25 10:45:04 +01:00
Pierre Krieger
170d2d268f
Switch to stable futures (#1196)
* Switch to stable futures

* Remove from_fn

* Fix secio

* Fix core --lib tests
2019-09-16 11:08:44 +02:00
Toralf Wittner
2c4b52a545 Return TCP listener errors. (#1218)
* Add listener ID and error event.

Report listener errors to client code so they are aware that an error
occurred within a listener. By default we continue to poll listeners
which produced an error, but clients can remove listeners by ID.

* tcp: Report errors.

Instead of silently waiting after errors we return all errors, but pause
after each error, before continuing.

* Add a test.

To ease testing, `Listener` is made generic and we test that no values
and errors are lost. Elapsed time between item generation is not
measured.

* Support the new methods in core-derive.

* Address review concerns.

* Remove `Display` impl of `ListenerId`.

* Add 'static bound to `on_listener_error` error.
2019-08-13 15:41:12 +02:00
Toralf Wittner
68c36d87d3
Move swarm and protocols handler into swarm crate. (#1188)
Move swarm and protocols handler into swarm crate.
2019-07-04 14:47:59 +02:00
Pierre Krieger
b6378ac526
PollParameters is now a trait (#1177)
* PollParameters is now a trait

* Fix unused variable
2019-06-18 10:23:26 +02:00
Pierre Krieger
a953b613cf
Add NetworkBehaviour::inject_new_external_addr (#1063) 2019-04-16 17:00:20 +02:00
Pierre Krieger
9b6336672b
Add NetworkBehaviour methods for listened addresses (#1061) 2019-04-16 15:36:08 +02:00
Pierre Krieger
34db72a080
Split address reach error and node reach error (#1013)
* Split address reach error and node reach error

* Small comments about order of operatoins

* Minor doc change
2019-03-20 20:28:55 +01:00
Roman Borschel
eeed66707b Address edition-2018 idioms. (#929) 2019-02-11 14:58:15 +01:00
Pierre Krieger
6b3b3ed2b5
Fix generics in out_event type in derive (#922) 2019-02-06 15:45:19 +01:00
Pierre Krieger
c9b7e237b6
Add NetworkBehaviour::inject_replaced (#914)
* Add NetworkBehaviour::inject_replaced

* Address style

* Forgot to call set_disconnected

* Also add incoming addresses to kbuckets
2019-02-04 15:21:50 +01:00
Pierre Krieger
a77da73010
Add inject_dial_failure and make addresses_of_peer mut (#901)
* Add inject_dial_failure and make addresses_of_peer mut

* Fix tests
2019-01-30 14:55:39 +01:00
Pierre Krieger
df923526ca
Embed the topology in the NetworkBehaviour (#889)
* Embed the topology in the NetworkBehaviour

* Put topologies inside of Floodsub and Kad

* Fix core tests

* Fix chat example

* More work

* Some cleanup

* Restore external addresses system
2019-01-26 23:57:53 +01:00
Dan Robertson
615dd3332f derive(NetworkBehaviour) handles trailing commas (#874)
Properly handle trailing commas in derive(NetworkBehaviour)
2019-01-21 11:56:01 +01:00
Dan Robertson
69abfbb18e Fix custom derive when using a where clause (#853)
When injecting the users where clause we need to ensure a comma is
inserted between the users where clause and ours.
2019-01-15 17:00:56 +01:00
Pierre Krieger
bf52e9bd19
Add IntoNodeHandler and IntoProtocolsHandler traits (#848)
* Add IntoNodeHandler

* Add IntoProtocolsHandler
2019-01-14 14:22:25 +01:00
Pierre Krieger
d10cafa804
Make deriving the NetworkBehaviour more ergonomic (#782) 2018-12-20 15:21:13 +01:00
Pierre Krieger
c5d08ab48c
Enhance the swarm a bit (#711)
* Replace the &mut TTopology with a &mut PollParameters

* Add supported_protocols

* Add external_addresses

* Report out addresses in Kademlia

* Fix the custom derive

* Some comments

* Fix compilation on stable
2018-12-01 13:34:57 +01:00
Pierre Krieger
179e65ff5c
Fix core-derive (#707) 2018-11-29 18:01:16 +01:00
Pierre Krieger
3aa1fcbdc6
Add a KademliaHandler (#580)
* Rework Kademlia for the new design

* Minor work on protocol.rs

* More work

* Remove QueryTarget::FindValue

* Finish work on query

* Query timeout test

* Work on topology

* More work

* Update protocols/kad/src/topology.rs

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

* Fix trailing whitespaces

* Use if let
2018-11-29 12:11:35 +01:00
Pierre Krieger
e12795d1e6
Fix the custom derive (#689)
* Fix the custom derive

* Concern
2018-11-27 16:10:34 +01:00
Pierre Krieger
45114aef46
Move the Swarm in its own module (#681) 2018-11-27 11:22:55 +01:00
Pierre Krieger
37994b34e7
Behavior -> Behaviour (#650) 2018-11-16 12:59:57 +01:00
Pierre Krieger
1c9091655b
Move protocols_handler to the root (#643) 2018-11-15 18:39:20 +01:00
Toralf Wittner
2e549884ef Split ConnectionUpgrade. (#642)
Introduce `InboundUpgrade` and `OutboundUpgrade`.
2018-11-15 17:41:11 +01:00
Pierre Krieger
623728b35d
Add a custom derive for NetworkBehaviour (#619)
* Add ProtocolsHandlerSelect

* Add a custom derive for NetworkBehaviour

* Remove 2018 edition

* More work

* Update the tests and work

* Allow ignored fields

* More fixes

* Give access to everything in the poll method
2018-11-12 17:12:47 +01:00