2583 Commits

Author SHA1 Message Date
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 Coratger
6329d74334
feat(swarm): remove deprecated items
Related: https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3956.
2023-05-24 14:32:59 +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
Thomas Eizinger
a424310d60
fix(swarm): poll handler after injecting LocalProtocolsChange event
Previously, we would not call the handler upon injecting `ConnectionEvent::LocalProtocolsChange`. This would prevent protocols from being able to react to this change and e.g. issue events or open streams.

Pull-Request: #3979.
2023-05-24 06:25:05 +00:00
Thomas Coratger
ccb6e9586d
feat(transports): remove deprecated items in tcp
Related: https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3978.
2023-05-23 16:50:24 +00:00
Thomas Eizinger
fd53245889
feat(mergify): only queue PRs if they target master
This is useful for stacked PRs. One can apply the `send-it` label and wait for GitHub to retarget the PR to the actual base branch once the dependent PR merges.

Pull-request: #3941.
2023-05-23 09:12:12 +10:00
dependabot[bot]
a7be3c0e6e
deps: bump Swatinem/rust-cache from 2.3.0 to 2.4.0
Pull-Request: #3965.
2023-05-22 19:47:57 +00:00
dependabot[bot]
61725ca772
deps: bump wasm-bindgen-futures from 0.4.35 to 0.4.36
Pull-Request: #3968.
2023-05-22 19:28:36 +00:00
dependabot[bot]
ba5ca84292
deps: bump js-sys from 0.3.62 to 0.3.63
Pull-Request: #3969.
2023-05-22 19:08:57 +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]
ee38a0051f
deps: bump proc-macro2 from 1.0.57 to 1.0.58
Pull-Request: #3971.
2023-05-22 18:30:09 +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
dependabot[bot]
9867fcebb6
deps: bump quinn-proto from 0.9.3 to 0.10.1
Pull-Request: #3938.
2023-05-22 08:47:51 +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
dependabot[bot]
a2ad916bc4
deps: bump wasm-bindgen from 0.2.85 to 0.2.86
Pull-Request: #3961.
2023-05-18 16:05:23 +00:00
dependabot[bot]
74fa48b43f
deps: bump tokio from 1.28.0 to 1.28.1
Pull-Request: #3931.
2023-05-18 15:47:10 +00:00
dependabot[bot]
2651e197e6
deps: bump proc-macro2 from 1.0.56 to 1.0.57
Pull-Request: #3946.
2023-05-18 15:28:46 +00:00
dependabot[bot]
b3fa954d4b
deps: bump libc from 0.2.143 to 0.2.144
Pull-Request: #3932.
2023-05-18 15:08:56 +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
dependabot[bot]
a580906a88
deps: bump Swatinem/rust-cache from 2.2.1 to 2.3.0
Pull-Request: #3929.
2023-05-16 19:41:17 +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
fbd471cd79
feat(websocket): remove deflate compression option
This option is the only reason we pull in a dependency on the shared `zlib` library. Unfortunately, we cannot use a pure Rust backend here because that one does not support configuring the window bits of the deflate algorithm which is used in the deflate websocket extension.

Using websockets in libp2p is already crazy inefficient because you end up with double encryption when using `/wss` (which is enforced by browsers if your website is served via https). A good encryption algorithm like noise or TLS produces an output that looks completely random. Any attempt in compressing this is pretty much wasted CPU power.

Thus, I think removing this configuration option does not really hurt and allows us to remove the dependency on the `zlib` shared library.

Pull-Request: #3949.
2023-05-16 09:40:00 +00:00
Max Inden
4fdd83e42f
fix(identity): remove unused file secp256r1.pk8
Introduced in https://github.com/libp2p/rust-libp2p/pull/3863/.

Pull-Request: #3950.
2023-05-16 05:31:30 +00:00
dependabot[bot]
3f88fb6059
deps: bump js-sys from 0.3.61 to 0.3.62
Pull-Request: #3930.
2023-05-15 21:00:03 +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]
2512b6cd18
deps: bump pin-project from 1.0.12 to 1.1.0
Pull-Request: #3937.
2023-05-15 17:49:21 +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
dependabot[bot]
86ea8fe4aa
deps: bump wasm-bindgen-futures from 0.4.34 to 0.4.35
Pull-Request: #3935.
2023-05-15 17:08:16 +00:00
dependabot[bot]
21c003701d
deps: bump serde from 1.0.162 to 1.0.163
Pull-Request: #3933.
2023-05-15 16:49:23 +00:00
Thomas Eizinger
0b209f71cc
feat(libp2p): remove deprecated mplex module
Pull-Request: #3920.
2023-05-15 14:11:13 +00:00
Thomas Eizinger
08f0b5e9c9
feat(core): make the upgrade module an implementation detail
Resolves: #3748.

Pull-Request: #3915.
2023-05-15 13:24:18 +00:00
Thomas Eizinger
5c785b92e7
feat(plaintext): remove Plaintext1Config
`Plaintext2Config` works with the upgrade infrastructure and is thus preferable.

Related: #3915.

Pull-Request: #3940.
2023-05-15 12:38:44 +00:00
Thomas Eizinger
adcc10b841
refactor(swarm-derive): better error reporting for invalid attributes
This PR refactors the error reporting away from panicking to returning `syn::Result` and adds two unit tests for the parsing of attributes that users interact with.

Pull-Request: #3922.
2023-05-15 12:15:14 +00:00
Thomas Eizinger
0bc724ab00
ci: reduce size of Rust interop-test image
By using a multi-stage docker build, a distroless base image and a release build, we can get the size of the Rust interop test down to 50MB. Previously, the image would be around 500MB. A debug build image would still have ~400MB. The release build slows down our interop build step by about 1min 20s. That however is only because we don't currently seem to utilize the caches that from what I understand should work on self-hosted runners. I opted https://github.com/libp2p/rust-libp2p/issues/3925 for that.

Resolves: #3881.

Pull-Request: #3926.
2023-05-15 11:58:28 +00:00
Thomas Coratger
3cc6c7e080
feat(identity): remove deprecated items
Related: https://github.com/libp2p/rust-libp2p/issues/3647.

Pull-Request: #3928.
2023-05-15 11:39:54 +00:00
Thomas Eizinger
856af16512
fix(metrics): don't grab unnecessary reference
This clippy warning started to pop up on my machine. Not sure how it slipped through CI!

Pull-Request: #3939.
2023-05-15 11:20:52 +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
8c00dc9680
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 02:05:42 +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 Eizinger
a00daca5fa
refactor(swarm-derive): improve warning message
Pull-Request: #3921.
2023-05-14 13:12:06 +00:00
Thomas Eizinger
38c08049e2
feat(swarm): remove deprecated NetworkBehaviourAction type
Pull-Request: #3919.
2023-05-14 11:23:49 +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
234a0d24db
feat(yamux): future-proof public API
With this set of changes, we prepare the public API of `libp2p-yamux` to be as minimal as possible and allow for upgrades of the underlying `yamux` library in patch releases.

Related: #3013.

Pull-Request: #3908.
2023-05-12 03:01:06 +00:00
Thomas Eizinger
25958a2f8c
feat(identity): make secp256k1::SecretKey::sign infallible
Previously, this function would return an error in case the provided digest is not of 32 bytes long. As per our [spec](https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#secp256k1), we hash _all_ messages with SHA256 before signing, thus this error can never happen in practice.

This brings us one step closer to an infallible `Keypair::sign` which is now only fallible due to RSA signing. If we manage to fix that as well, constructors like `noise::Config::new` will become infallible.

Pull-Request: #3850.
2023-05-12 02:43:14 +00:00