315 Commits

Author SHA1 Message Date
Max Inden
2910c985e8
fix(examples): call Swarm::add_external_address in dcutr and relay
> Observed addresses (aka. external address candidates) of the local node, reported by a remote node
> via `libp2p-identify`, are no longer automatically considered confirmed external addresses, in
> other words they are no longer trusted by default. Instead users need to confirm the reported
> observed address either manually, or by using `libp2p-autonat`. In trusted environments users can
> simply extract observed addresses from a `libp2p-identify::Event::Received { info:
> libp2p_identify::Info { observed_addr }}` and confirm them via `Swarm::add_external_address`.

Follow-up to https://github.com/libp2p/rust-libp2p/pull/3954.

Pull-Request: #4052.
2023-06-09 03:26:24 +00:00
Thomas Eizinger
ed14630672
feat: leverage type-safe /p2p in multiaddr
This updates `multiaddr` to version `0.19`.

Depends-On: #3656.
Depends-On: https://github.com/multiformats/rust-multiaddr/pull/83.
Resolves: #4039.

Pull-Request: #4037.
2023-06-08 01:38:18 +00:00
Thomas Eizinger
ddc2b879e2
fix: apply suggestions from 1.71 clippy beta
Pull-Request: #4009.
2023-05-30 13:30:04 +00:00
dependabot[bot]
34d87a8208
deps: bump log from 0.4.17 to 0.4.18
Pull-Request: #4000.
2023-05-29 10:17:23 +00:00
Thomas Eizinger
137443b9df
feat(identify): push changed listen protocols to remote
Now that we have the infrastructure for notifying protocols about changes to our listen protocols, we can use this to actively push those changes to our remotes. This allows other peers to re-configure themselves with very low-latency instead of waiting for the periodic identify event.

Resolves: #3613.

Pull-Request: #3980.
2023-05-25 01:09:35 +00:00
Thomas Eizinger
5e8f2e82e4
feat(swarm): replace address scoring with explicit candidates
Previously, a `NetworkBehaviour` could report an `AddressScore` for an external address. This score was a `u32` and addresses would be ranked amongst those.

In reality, an address is either confirmed to be publicly reachable (via a protocol such as AutoNAT) or merely represents a candidate that might be an external address. In a way, addresses are guilty (private) until proven innocent (publicly reachable).

When a `NetworkBehaviour` reports an address candidate, we perform address translation on it to potentially correct for ephemeral ports of TCP. These candidates are then injected back into the `NetworkBehaviour`. Protocols such as AutoNAT can use these addresses as a source for probing their NAT status. Once confirmed, they can emit a `ToSwarm::ExternalAddrConfirmed` event which again will be passed to all `NetworkBehaviour`s.

This simplified approach will allow us implement Kademlia's client-mode (https://github.com/libp2p/rust-libp2p/issues/2032) without additional configuration options: As soon as an address is reported as publicly reachable, we can activate server-mode for that connection.

Related: https://github.com/libp2p/rust-libp2p/pull/3877.
Related: https://github.com/libp2p/rust-libp2p/issues/3953.
Related: https://github.com/libp2p/rust-libp2p/issues/2032.
Related: https://github.com/libp2p/go-libp2p/issues/2229.

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

Pull-Request: #3954.
2023-05-24 07:52:16 +00:00
Thomas Coratger
9f3c85164c
feat(swarm): rename Custom variant to NotifyBehaviour
Rename `ConnectionHandlerEvent::Custom` to `ConnectionHandlerEvent::NotifyBehaviour`.

Related #3848.

Pull-Request: #3955.
2023-05-16 19:20:00 +00:00
Thomas Eizinger
fd829efd0e
fix(identify): always assert both identify responses
Previously, we used to race the two identify responses and assert the one that finished earlier. In practice, this didn't work but sometimes caused a timeout. See https://github.com/libp2p/rust-libp2p/actions/runs/4973490081/jobs/8899378998#step:7:98 for an example.

Interestingly enough, refactoring this test to always assert both responses reveals a bug! The memory transport by default behaves like TCP and allocates a new ephemeral port for an outgoing connection. I believe this was never hit because the first swarm would always receive its response first and win the race.

To assert this properly, we would have to implement port reuse for the memory transport which I think is unnecessary, hence I just commented out the assertion.

Pull-Request: #3924.
2023-05-15 01:47:21 +00:00
Thomas Coratger
6e36e8aa35
feat(swarm): rename associated types for message passing
Previously, the associated types on `NetworkBehaviour` and `ConnectionHandler` carried generic names like `InEvent` and `OutEvent`. These names are _correct_ in that `OutEvent`s are passed out and `InEvent`s are passed in but they don't help users understand how these types are used.

In theory, a `ConnectionHandler` could be used separately from `NetworkBehaviour`s but that is highly unlikely. Thus, we rename these associated types to indicate, where the message is going to be sent to:

- `NetworkBehaviour::OutEvent` is renamed to `ToSwarm`: It describes the message(s) a `NetworkBehaviour` can emit to the `Swarm`. The user is going to receive those in `SwarmEvent::Behaviour`.
- `ConnectionHandler::InEvent` is renamed to `FromBehaviour`: It describes the message(s) a `ConnectionHandler` can receive from its behaviour via `ConnectionHandler::on_swarm_event`. The `NetworkBehaviour` can send it via the `ToSwarm::NotifyHandler` command.
- `ConnectionHandler::OutEvent` is renamed to `ToBehaviour`: It describes the message(s) a `ConnectionHandler` can send back to the behaviour via the now also renamed `ConnectionHandlerEvent::NotifyBehaviour` (previously `ConnectionHandlerEvent::Custom`)

Resolves: #2854.

Pull-Request: #3848.
2023-05-14 10:58:08 +00:00
Thomas Eizinger
541eba306b
refactor(identify): simplify reply handling
A `ConnectionHandler` is bound to a single peer. There is no need to embed the `PeerId` in the event that we report to the behaviour, it already knows which peer the event relates to.

Pull-Request: #3895.
2023-05-10 01:51:47 +00:00
Thomas Eizinger
b035fc80a0
feat: report changes in supported protocols to ConnectionHandler
With this patch, implementations of `ConnectionHandler` (which are typically composed in a tree) can exchange information about the supported protocols of a remote with each other via `ConnectionHandlerEvent::ReportRemoteProtocols`. The provided `ProtocolSupport` enum can describe either additions or removals of the remote peer's protocols.

This information is aggregated in the connection and passed down to the `ConnectionHandler` via `ConnectionEvent::RemoteProtocolsChange`.

Similarly, if the listen protocols of a connection change, all `ConnectionHandler`s on the connection will be notified via `ConnectionEvent::LocalProtocolsChange`. This will allow us to eventually remove `PollParameters` from `NetworkBehaviour`.

This pattern allows protocols on a connection to communicate with each other. For example, protocols like identify can share the list of (supposedly) supported protocols by the remote with all other handlers. A protocol like kademlia can accurately add and remove a remote from its routing table as a result.

Resolves: #2680.
Related: #3124.

Pull-Request: #3651.
2023-05-08 14:36:30 +00:00
Thomas Eizinger
8224f1a05d
feat(identify): keep connection alive while we are using it
Currently, the `connection_keep_alive` function of identify does not compute anything but its return value is set through the handler state machine. This is hard to understand and causes surprising behaviour because at the moment, we set `KeepAlive::No` as soon as the remote has answered our identify request. Depending on what else is happening on the connection, this might close the connection before we have successfully answered the remote's identify request.

To fix this, we now compute `connection_keep_alive` based on whether we are still using the connection.

Related: #3844.

Pull-Request: #3876.
2023-05-08 09:31:25 +00:00
Thomas Eizinger
81c424ea9e
feat(swarm): make stream uprade errors more ergonomic
The currently provided `ConnectionHandlerUpgrErr` is very hard to use. Not only does it have a long name, it also features 3 levels of nesting which results in a lot of boilerplate. Last but not least, it exposes `multistream-select` as a dependency to all protocols.

We fix all of the above by renaming the type to `StreamUpgradeError` and flattening out its interface. Unrecoverable errors during protocol selection are hidden within the `Io` variant.

Related: #3759.

Pull-Request: #3882.
2023-05-08 08:55:17 +00:00
Thomas Eizinger
0e36c7c072
feat(swarm): remove deprecated IntoConnectionHandler
This removes the deprecated `IntoConnectionHandler` trait and all its implementations. Consequently, `NetworkBehaviour::new_handler` and `NetworkBehaviour::addresses_of_peer` are now gone and the two `handle_` functions are now required to implement.

Related: #3647.

Pull-Request: #3884.
2023-05-08 08:30:29 +00:00
Thomas Eizinger
fa4adc8c9d
refactor(identify): bring tests up to workspace standard
This patch refactors the identify tests to use `libp2p-swarm-test`. This allows us to delete quite a bit of code and makes several dev-dependencies obsolete.

The `correct_transfer` test is made obsolete by more precise assertions in the `periodic_identify` test. This allows us to remove the dependency on the `upgrade::{apply_inbound,apply_outbound}` functions.

Finally, we also fix a bug where the reported listen addresses to the other node could contain duplicates.

Related: #3748.

Pull-Request: #3851.
2023-05-08 03:39:34 +00:00
Thomas Eizinger
c93f753018
feat: replace ProtocolName with AsRef<str>
Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8.

To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`.

We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades.

`multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`.

Resolves: #2831.

Pull-Request: #3746.
2023-05-04 04:47:11 +00:00
Bogdan
f858ec6237
feat(identify): immediately run identify protocol on new connections
Previously, and for unknown legacy reasons, we waited for a configurable delay (default 500ms) upon new connections before we ran the identify protocol. This unnecessarily slows down applications that wait for the identify handshake to complete before performing further actions.

Resolves #3485.

Pull-Request: #3545.
2023-05-02 14:24:56 +00:00
Hannes
3e5f64345b
chore(identify): remove deprecated items
Related: https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3698.
2023-05-02 13:15:34 +00:00
Thomas Eizinger
996b5c8bd0
chore: leverage cargo's workspace inheritance
Previously, we would specify the version and path of our workspace dependencies in each of our crates. This is error prone as https://github.com/libp2p/rust-libp2p/pull/3658#discussion_r1153278072 for example shows. Problems like these happened in the past too.

There is no need for us to ever depend on a earlier version than the most current one in our crates. It thus makes sense that we manage this version in a single place.

Cargo supports a feature called "workspace inheritance" which allows us to share a dependency declaration across a workspace and inherit it with `{ workspace = true }`.

We do this for all our workspace dependencies and for the MSRV.

Resolves #3787.

Pull-Request: #3715.
2023-05-02 09:14:14 +00:00
Max Inden
2d9ae3800f
chore: prepare patch releases on top of v0.51.3
Note that this does not release v0.51.4, i.e. there is no patch release of the meta crate `libp2p`.

Pull-Request: #3854.
2023-05-01 02:56:32 +00:00
Thomas Eizinger
e3a1650933
feat(identify): do not implicitly dial on push
Previously, we would implicitly establish a connection when the user wanted to push identify information to a peer. I believe that this is the wrong behaviour. Instead, I am suggesting to log a message that we are skipping the push to this peer.

Additionally, the way this is currently implemented does not make much sense. Dialing a peer takes time. In case we don't have a connection at all, it could be that we drop the push requests because there isn't an active handler and thus we would have unnecessarily established the connection.

Instead of fixing this - which would require buffering the push messages - I think we should just remove the implicit dial.

Pull-Request: #3843.
2023-04-28 14:06:33 +00:00
Thomas Eizinger
2f5270ba76
feat(noise): deprecate all handshake patterns apart from XX
In the libp2p specs, the only handshake pattern that is specified is the XX handshake. Support for other handshake patterns can be added through external modules. While we are at it, we rename the remaining types to following the laid out naming convention.

The tests for handshakes other than XX are removed. The handshakes still work as we don't touch them in this patch.

Related #2217.

Pull-Request: #3768.
2023-04-28 13:50:31 +00:00
Thomas Eizinger
135942d319
chore: enforce unreachable_pub lint
The `unreachable_pub` lint makes us aware of uses of `pub` that are not actually reachable from the crate root. This is considered good because it means reading a `pub` somewhere means it is actually public API. Some of our crates are quite large and keeping their entire API surface in your head is difficult.

We should strive for most items being `pub(crate)`. This lint helps us enforce that.

Pull-Request: #3735.
2023-04-26 07:31:56 +00:00
DrHuangMHT
058c2d85ec
refactor(identity): follow naming conventions for conversion methods
This PR renames some method names that don't follow Rust naming conventions or behave differently from what the name suggests:
- Enforce "try" prefix on all methods that return `Result`.
- Enforce "encode" method name for methods that return encoded bytes.
- Enforce "to_bytes" method name for methods that return raw bytes.
- Enforce "decode" method name for methods that convert encoded key.
- Enforce "from_bytes" method name for methods that convert raw bytes.

Pull-Request: #3775.
2023-04-14 08:55:13 +00:00
dependabot[bot]
849554abb0
deps: bump lru from 0.9.0 to 0.10.0
Pull-Request: #3558.
2023-04-11 17:54:56 +00:00
Thomas Eizinger
451c64a8b8
fix: specify correct libp2p-swarm dependency
With https://github.com/libp2p/rust-libp2p/pull/3658, these crates depend on the `0.42.1` release to access the new `ToSwarm` type. With the currently specified version, a user could theoretically run into a compile error if they pin `libp2p-swarm` to `0.42.0` in their lockfile but update to the latest patch release of one of these crates.

Pull-Request: #3711.
2023-04-05 19:44:32 +00:00
dependabot[bot]
0ff0ef7a35
deps: bump futures from 0.3.27 to 0.3.28
Pull-Request: #3725.
2023-04-03 14:43:42 +00:00
Max Inden
d7396706d0
fix(changelog): Make release heading levels consistent
See `##` for release headings everywhere. This is consistent with markdown conventions of one `#` per document and in line with https://keepachangelog.com/.

See report in https://github.com/libp2p/rust-libp2p/issues/3531.

Pull-Request: #3561.
2023-03-30 21:04:01 +00:00
Thomas Eizinger
dcbc04e89e
feat(swarm): rename NetworkBehaviourAction to ToSwarm
Resolves #3123.

Pull-Request: #3658.
2023-03-24 13:43:49 +00:00
Victor Ermolaev
2ec5402474
feat(swarm): enforce creation of Swarm via SwarmBuilder
Mark constructors `Swarm::with_X_executor` as deprecated.
Move the deprecated functionality to `SwarmBuilder::with_X_executor`
Use `SwarmBuilder` throughout.

Resolves #3186.
Resolves #3107.

Pull-Request: #3588.
2023-03-13 19:53:14 +00:00
dependabot[bot]
9d0ec7e074
deps: bump futures from 0.3.26 to 0.3.27
Pull-Request: #3597.
2023-03-13 11:35:04 +00:00
Max Inden
3959b2ccef
docs: Prepare v0.51.1 (#3594) 2023-03-12 16:42:57 +01:00
Thomas Eizinger
2a14df25eb
feat: introduce libp2p-identity crate
This patch combines the `libp2p_core::identity` and `libp2p_core::peer_id` modules into a new crate: `libp2p-identity`.

Resolves https://github.com/libp2p/rust-libp2p/issues/3349.

Pull-Request: #3350.
2023-03-12 15:46:58 +01:00
Thomas Eizinger
0cad636eba
fix: move changelog entries to correct version
Whilst https://github.com/libp2p/rust-libp2p/pull/3312 was in development, we pushed a new release out and forgot to move the changelog entries to the new version. Unfortunately, this is all still very manual until we have a solution for https://github.com/libp2p/rust-libp2p/issues/2902 so this stuff keeps happening.

Pull-Request: #3541.
2023-03-10 10:59:49 +00:00
Oleg Kubrakov
b63e05dad6
refactor: move examples to common location
Refactor examples into separate binary crates.

Fixes https://github.com/libp2p/rust-libp2p/issues/3111.

Pull-Request: #3509.
2023-03-08 08:17:33 +00:00
Miguel Guarniz
db82e0210e
feat: migrate to quick-protobuf
Instead of relying on `protoc` and buildscripts, we generate the bindings using `pb-rs` and version them within our codebase. This makes for a better IDE integration, a faster build and an easier use of `rust-libp2p` because we don't force the `protoc` dependency onto them.

Resolves #3024.

Pull-Request: #3312.
2023-03-02 10:45:07 +00:00
Max Inden
71015ee16f
docs: prepare v0.51.0
A large release with lots of changes I am looking forward to. Sorry for the long release cadence.

Anything folks would like to see included that is not yet in `master`? As usual I would like to only block on bug fixes.

Pull-Request: #3491.
2023-02-24 10:42:29 +00:00
Thomas Eizinger
19a554965f
feat(swarm)!: allow NetworkBehaviours to manage connections
Previously, a `ConnectionHandler` was immediately requested from the `NetworkBehaviour` as soon as a new dial was initiated or a new incoming connection accepted.

With this patch, we delay the creation of the handler until the connection is actually established and fully upgraded, i.e authenticated and multiplexed.

As a consequence, `NetworkBehaviour::new_handler` is now deprecated in favor of a new set of callbacks:

- `NetworkBehaviour::handle_pending_inbound_connection`
- `NetworkBehaviour::handle_pending_outbound_connection`
- `NetworkBehaviour::handle_established_inbound_connection`
- `NetworkBehaviour::handle_established_outbound_connection`

All callbacks are fallible, allowing the `NetworkBehaviour` to abort the connection either immediately or after it is fully established. All callbacks also receive a `ConnectionId` parameter which uniquely identifies the connection. For example, in case a `NetworkBehaviour` issues a dial via `NetworkBehaviourAction::Dial`, it can unambiguously detect this dial in these lifecycle callbacks via the `ConnectionId`.

Finally, `NetworkBehaviour::handle_pending_outbound_connection` also replaces `NetworkBehaviour::addresses_of_peer` by allowing the behaviour to return more addresses to be used for the dial.

Resolves #2824.

Pull-Request: #3254.
2023-02-23 23:43:33 +00:00
Elena Frank
79b7cef070
fix(identify): don't close stream in protocol::recv
Don't close the stream `protocol::recv`.

This is a short-term fix for #3298.

The issue behind this is a general one on the QUIC transport when closing streams, as described in #3343. This PR only circumvents the issue for identify. A proper solution for our QUIC transport still needs more thought.

Pull-Request: #3344.
2023-02-19 21:18:54 +00:00
Thomas Eizinger
caed1fe2c7
refactor(swarm)!: remove handler from NetworkBehaviourAction::Dial (#3328)
We create the `ConnectionId` for the new connection as part of `DialOpts`. This allows `NetworkBehaviour`s to accurately track state regarding their own dial attempts.

This patch is the main enabler of https://github.com/libp2p/rust-libp2p/pull/3254. Removing the `handler` field will allow us to deprecate the `NetworkBehaviour::new_handler` function in favor of four new ones that give more control over the connection lifecycle.
2023-02-14 01:09:29 +00:00
dependabot[bot]
0c94237e16
deps: bump futures from 0.3.25 to 0.3.26 (#3405) 2023-01-31 23:34:17 +00:00
Thomas Eizinger
4de54f00f9
refactor: expose and use THandlerOutEvent type alias (#3368)
Previously, we used the full reference to the `OutEvent` of the `ConnectionHandler` in all implementations of `NetworkBehaviour`. Not only is this very verbose, it is also more brittle to changes. With the current implementation plan for #2824, we will be removing the `IntoConnectionHandler` abstraction. Using a type-alias to refer to the `OutEvent` makes the migration much easier.
2023-01-26 11:55:02 +00:00
Thomas Eizinger
4b41f5a994
refactor(core)!: remove EitherOutput (#3341)
The trick with this one is to use `futures::Either` everywhere where we may wrap something that implements any of the `futures` traits. This includes the output of `EitherFuture` itself. We also need to implement `StreamMuxer` on `future::Either` because `StreamMuxer`s may be the the `Output` of `InboundUpgrade`.
2023-01-23 12:31:30 +00:00
Thomas Eizinger
475dc80a07
refactor!: Move ConnectionId and PendingPoint to libp2p-swarm (#3346)
Both of these are only needed as part of `libp2p-swarm`. Them residing in `libp2p-core` is a left-over from when `libp2p-core` still contained `Pool`.
2023-01-18 08:56:32 +00:00
Thomas Eizinger
db2cd43826
refactor(core)!: remove EitherUpgrade (#3339)
We don't need to define our own type here, we can simply implement `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`.
2023-01-18 02:35:07 +00:00
Thomas Eizinger
f4fed3880b
refactor(core)!: remove EitherError in favor of either::Either (#3337)
Defining our own `EitherError` type has no value now that `Either` provides the same implementation.

Related: https://github.com/libp2p/rust-libp2p/issues/3271
2023-01-17 23:05:59 +00:00
dependabot[bot]
a124258851
deps: update lru requirement from 0.8.0 to 0.9.0 (#3294) 2023-01-04 19:09:43 +00:00
Elena Frank
30640968ac
fix: typo in ExternalAddresses::on_swarm_event (#3297)
Fix typo `swarn` -> `swarm`.
2023-01-04 16:41:34 +00:00
Max Inden
e4d67c504d
fix(identify): Don't fail on unknown multiaddr protocol (#3279)
With this commit `libp2p-identify` no longer discards the whole identify payload in case a listen addr of the remote node is invalid, but instead logs the failure, skips the invalid multiaddr and parses the remaining identify payload.

This is especially relevant when rolling out a new protocol to a live network. Say that most nodes of a network run on an implementation version v1. Say that the `multiaddr` implementation is not aware of the `webrtc/` protocol. Say that a new version (v2) is rolled out to the network with support for the `webrtc/` protocol, listening via `webrtc/` by default. In such case all v1 nodes would discard all identify payloads of v2 nodes, given that the v2 identify payloads would contain the `webrtc/` protocol in their `listen_addr` addresses.

See https://github.com/libp2p/rust-libp2p/issues/3244 for details.
2022-12-24 13:20:55 +00:00
Pasha Podolsky
929cbb4670
deps!: Update multiaddr & multihash to 0.17.0 (#3196) 2022-12-20 08:52:08 +00:00