926 Commits

Author SHA1 Message Date
dependabot[bot]
d263ee67a9
deps: bump log from 0.4.18 to 0.4.19
Pull-Request: #4058.
2023-06-13 10:29:36 +00:00
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
Max Inden
e0f9de3683
chore(kad): extend changelog entry on client/server mode
Pull-Request: #4050.
2023-06-08 05:51:12 +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 Coratger
d2dab94b76
feat(request-response): use cbor codec in ping example
Remove the use of the core `upgrade::transfer` module in `ping` example (`request-response` protocol) in favor of `cbor` codec.

Related #4011.

Pull-Request: #4046.
2023-06-07 13:01:40 +00:00
Thomas Eizinger
61128713eb
feat(core): remove deprecated identity module
Related: #3647.

Pull-Request: #4040.
2023-06-06 19:22:50 +00:00
Max Inden
86e8018016
chore(rendezvous): remove unused dependency mplex
Pull-Request: #4034.
2023-06-06 05:25:40 +00:00
dependabot[bot]
c723df461f
deps: bump clap from 4.3.0 to 4.3.1
Pull-Request: #4027.
2023-06-05 10:59:28 +00:00
dependabot[bot]
1dbdb6a008
deps: bump futures_ringbuf from 0.3.1 to 0.4.0
Pull-Request: #4023.
2023-06-05 09:53:02 +00:00
Max Inden
c9b386b2e3
fix(relay): log when accepting reservation without external addresses
Pull-Request: #4022.
2023-06-05 03:36:09 +00:00
Thomas Coratger
a4450d41c0
feat(dcutr): keep connection alive while we are using it
Similar to #3876, we now compute `connection_keep_alive` based on whether we are still using the connection, applied to the `dcutr` protocol.

Related: https://github.com/libp2p/rust-libp2p/issues/3844.

Pull-Request: #3960.
2023-06-04 07:43:01 +00:00
Thomas Coratger
87e863e8c9
refactor(dcutr): remove direct::Handler
With recent refactorings in `libp2p-swarm`, there is no more need for `handler::direct` at all.

Resolves #4013.

Pull-Request: #4016.
2023-06-01 04:34:28 +00:00
Thomas Eizinger
92c8cc451f
feat(kad): implement automatic client mode
Currently, the kademlia behaviour can only learn that the remote node supports kademlia on a particular connection if we successfully negotiate a stream to them.

Using the newly introduced abstractions from #3651, we don't have to attempt to establish a stream to the remote to learn whether they support kademlia on a connection but we can directly learn it from the `ConnectionEvent::RemoteProtocolsChange` event. This happens directly once a connection is established which should overall benefit the DHT.

Clients do not advertise the kademlia protocol and thus we will immediately learn that a given connection is not suitable for kadmelia requests. We may receive inbound messages from it but this does not affect the routing table.

Resolves: #2032.

Pull-Request: #3877.
2023-05-31 09:02:19 +00:00
Arpan Kapoor
5321a4479b
fix(dcutr): always check for relayed connection first
For a non-relay connection, check if it corresponds to a direct connection upgrade.

As discussed in #3964.

Pull-Request: #3982.
2023-05-31 01:14:10 +00:00
Thomas Eizinger
ddc2b879e2
fix: apply suggestions from 1.71 clippy beta
Pull-Request: #4009.
2023-05-30 13:30:04 +00:00
Max Inden
efb117e236
fix(ping): remove debug_assert on protocol negotiation timeout
The negotiation of the ping protocol can timeout, thus a debug_assert is not correct. Return an `std::io::Error` instead.

Pull-Request: #3995.
2023-05-30 03:21:52 +00:00
dependabot[bot]
616abb6c3b
deps: bump regex from 1.8.1 to 1.8.3
Pull-Request: #4003.
2023-05-30 02:25:45 +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
dependabot[bot]
95eca886f2
deps: bump tokio from 1.28.1 to 1.28.2
Pull-Request: #4001.
2023-05-29 09:59:17 +00:00
Max Inden
9b765fa202
fix(perf): remove --secret-key-seed flag
There is no need for the flag, given that rust-libp2p allows dialing multiaddr without `/p2p`.

Pull-Request: #3994.
2023-05-29 08:04:52 +00:00
dependabot[bot]
c5dd0a2e14
deps: bump base64 from 0.21.0 to 0.21.2
Pull-Request: #3993.
2023-05-28 06:36:18 +00:00
Max Inden
92af0d1281
refactor(perf): use libp2p-request-response
Rewrite `libp2p-perf` protocol using `libp2p-request-response`. Additionally adjust to latest conventions, e.g. the final JSON output.

Pull-Request: #3646.
2023-05-28 05:51:53 +00:00
Onur
801023f468
feat(gossipsub): allow compiling for WASM
This modification removes deprecated dependency `wasm_timer` and enables wasm compatibility on the gossibsup protocol by simply substituting the `wasm_timer::Instant` with `instant::Instant`(which supports `fn checked_add`) and `wasm_timer::Interval` with `futures_ticker::Ticker`.

Pull-Request: #3973.
2023-05-25 06:19:47 +00:00
Max Inden
fc386a51a7
fix: remove obsolete event_process = false
The `libp2p-swarm-derive` `event_process` option was removed in https://github.com/libp2p/rust-libp2p/pull/2840.

Pull-Request: #3984.
2023-05-25 05:44:54 +00:00
Philippe Jalaber
67b26cce4f
feat(mdns): adaptive initial interval peer discovery
Peer discovery with mDNS can be very slow, particularly if the first mDNS query is lost. This patch resolves it by adjusting the timer with an adaptive initial interval. We start with a very short timer (500 ms). If a peer is discovered before the end of the timer, then the timer is reset to the normal query interval value (300s), otherwise the timer's value is multiplied by 2 until it reaches the normal query interval value.

Related: https://github.com/libp2p/rust-libp2p/pull/3323.
Resolves: https://github.com/libp2p/rust-libp2p/issues/3319.

Pull-Request: #3975.
2023-05-25 04:09:24 +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
25bc30f07e
feat(ping): don't close connections upon failures
Previously, the `libp2p-ping` module came with a policy to close a connection after X failed pings. This is only one of many possible policies on how users would want to do connection management.

We remove this policy without a replacement. If users wish to restore this functionality, they can easily implement such policy themselves: The default value of `max_failures` was 1. To restore the previous functionality users can simply close the connection upon the first received ping error.

In this same patch, we also simplify the API of `ping::Event` by removing the layer of `ping::Success` and instead reporting the RTT to the peer directly.

Related: #3591.

Pull-Request: #3947.
2023-05-24 12:33:18 +00:00
Denis Garus
a5cd0d0e03
feat(request-response): add modules for json and cbor messages
This patch adds two modules to `libp2p::request_response`:

- `cbor`
- `json`

Both define a `Behaviour` type-alias that comes with a `Codec` implementation which uses the respective `serde` crate to serialize and deserialize the messages.

Fixes #3905.

Pull-Request: #3952.
2023-05-24 11:13:12 +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
dependabot[bot]
d090b58881
deps: bump clap from 4.2.7 to 4.3.0
Pull-Request: #3970.
2023-05-22 18:48:38 +00:00
dependabot[bot]
382b358c65
deps: bump data-encoding from 2.3.3 to 2.4.0
Pull-Request: #3972.
2023-05-22 18:10:10 +00:00
Thomas Coratger
c224ffe012
feat(kad): Remove TUserData and replace with QueryId
Kademlia `ConnectionHandler` used a generic `TUserData` that is replaced by `QueryId` always used in practice.

Resolves #3607.

Pull-Request: #3959.
2023-05-18 23:13:16 +00:00
Thomas Coratger
b655e84d06
refactor(dcutr): fix typo in observed_addreses
Pull-Request: #3962.
2023-05-18 18:44:25 +00:00
Max Inden
cc5b346a7c
feat(swarm): expose ConnectionId and add conn duration metric
- Exposes the `ConnectionId` in the various `SwarmEvent` variants.
- Tracks connection duration in `libp2p-metrics::swarm`.

Pull-Request: #3927.
2023-05-17 05:19:53 +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 Coratger
7c64c422ad
chore(relay): remove deprecated items
Related: https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3948.
2023-05-15 20:31:30 +00:00
dependabot[bot]
3021814e6b
deps: bump socket2 from 0.5.2 to 0.5.3
Pull-Request: #3934.
2023-05-15 17:30:19 +00:00
Thomas Eizinger
0b209f71cc
feat(libp2p): remove deprecated mplex module
Pull-Request: #3920.
2023-05-15 14:11:13 +00:00
Max Inden
2d83725c09
feat(metrics)!: expose identify metrics for connected peers only
Previously we would increase a counter / gauge / histogram on each received identify information. These metrics are missleading, as e.g. they depend on the identify interval and don't represent the set of currently connected peers.

With this commit, identify information is tracked for the currently connected peers only. Instead of an increase on each received identify information, metrics represent the status quo (Gauge).

Example:

```
\# HELP libp2p_libp2p_identify_remote_protocols Number of connected nodes supporting a specific protocol, with "unrecognized" for each peer supporting one or more unrecognized protocols...
\# TYPE libp2p_libp2p_identify_remote_protocols gauge
libp2p_libp2p_identify_remote_protocols_total{protocol="/ipfs/id/push/1.0.0"} 1
libp2p_libp2p_identify_remote_protocols_total{protocol="/ipfs/id/1.0.0"} 1
libp2p_libp2p_identify_remote_protocols_total{protocol="/ipfs/ping/1.0.0"} 1
libp2p_libp2p_identify_remote_protocols_total{protocol="unrecognized"} 1
\# HELP libp2p_libp2p_identify_remote_listen_addresses Number of connected nodes advertising a specific listen address...
\# TYPE libp2p_libp2p_identify_remote_listen_addresses gauge
libp2p_libp2p_identify_remote_listen_addresses_total{listen_address="/ip4/tcp"} 1
libp2p_libp2p_identify_remote_listen_addresses_total{listen_address="/ip4/udp/quic"} 1
\# HELP libp2p_libp2p_identify_local_observed_addresses Number of connected nodes observing the local node at a specific address...
\# TYPE libp2p_libp2p_identify_local_observed_addresses gauge
libp2p_libp2p_identify_local_observed_addresses_total{observed_address="/ip4/tcp"} 1
```

Pull-Request: #3325.
2023-05-15 02:58:06 +00:00
Thomas Eizinger
1742bffcea
feat(request-response): don't close connection on stream errors
Related: #3591.

Pull-Request: #3913.
2023-05-15 02:40:55 +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
Darius Clark
5b32c8a0d2
feat(transport): allow ListenerId to be user-controlled
`Transport::listen_on` is an asynchronous operation. It returns immediately but the actual process of establishing a listening socket happens as part of `Transport::poll` which will return one or more `TransportEvent`s related to a particular `listen_on` call.

Currently, `listen_on` returns a `ListenerId` which allows the user of the `Transport` interface to correlate the events with a particular `listen_on` call. This "user" is the `Swarm` runtime. Currently, a user of libp2p establishes a new listening socket by talking to the `Swarm::listen_on` interface and it is not possible to do the same thing via the `NetworkBehaviour` trait.

Within the `NetworkBehaviour` trait, we emit _commands_ to the `Swarm` like `ToSwarm::Dial`. These commands don't have a "return value" like a synchronous function does and thus, if we were to add a `ToSwarm::ListenOn` command, it could not receive the `ListenerId` from the `Transport`.

To fix this and to be consistent with our [coding guidelines](https://github.com/libp2p/rust-libp2p/blob/master/docs/coding-guidelines.md#allow-correlating-asynchronous-responses-to-their-requests) we change the interface of `Transport::listen_on` to require the user to pass in a `ListenerId`. This will allow us to construct a command in a `NetworkBehaviour` that remembers this ID which enables precise tracking of which events containing a `ListenerId` correlate which a particular `listen_on` command.

This is especially important in the context of listening on wildcard addresses like `0.0.0.0` because we end up binding to multiple network interfaces and thus emit multiple events for a single `listen_on` call.

Pull-Request: #3567.
2023-05-14 09:42:51 +00:00
Thomas Eizinger
9e625881d5
feat(swarm): deprecate NegotiatedSubstream in favor of Stream
This patch tackles two things at once that are fairly intertwined:

1. There is no such thing as a "substream" in libp2p, the spec and other implementations only talk about "streams". We fix this by deprecating `NegotiatedSubstream`.
2. Previously, `NegotiatedSubstream` was a type alias that pointed to a type from `multistream-select`, effectively leaking the version of `multistream-select` to all dependencies of `libp2p-swarm`. We fix this by introducing a `Stream` newtype.

Resolves: #3759.
Related: #3748.

Pull-Request: #3912.
2023-05-12 06:19:23 +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
Hannes
5a867c86b4
chore(ping): remove deprecated items
Related https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3702.
2023-05-09 09:23:27 +00:00
Thomas Eizinger
3165b12c75
feat(kad): remove deprecated public modules
This patch removes the 3 out of 4 deprecated public modules. I've left `store` for now because we made some mistakes in declaring that. The items within `store` still need to be publicly visible but I haven't yet figured out a good way of exporting / naming them. Thus, I've left that to a follow-up PR.

Related: #3647.

Pull-Request: #3896.
2023-05-09 02:50:00 +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
b8a7684153
feat(swarm): remove deprecated connection limits
Users are encouraged to use `libp2p::connection_limit::Behaviour` which is a one-to-one replacement for this functionality but built as a `NetworkBehaviour`.

Related: #3647.

Pull-Request: #3885.
2023-05-08 10:54:53 +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