diff --git a/.gitignore b/.gitignore index 1531bdf9de..c31542c23e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist .coverage package-lock.json yarn.lock +.vscode \ No newline at end of file diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 78c09f84a7..9d34d6244e 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -19,7 +19,7 @@ - [Customizing DHT](#customizing-dht) - [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing) - [Setup with Relay](#setup-with-relay) - - [Setup with Auto Relay](#setup-with-auto-relay) + - [Setup with Automatic Reservations](#setup-with-automatic-reservations) - [Setup with Keychain](#setup-with-keychain) - [Configuring Dialing](#configuring-dialing) - [Configuring Connection Manager](#configuring-connection-manager) @@ -433,22 +433,31 @@ const node = await createLibp2p({ transports: [tcp()], streamMuxers: [mplex()], connectionEncryption: [noise()], - relay: { // Circuit Relay options (this config is part of libp2p core configurations) + relay: { // Circuit Relay options enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay. hop: { - enabled: true, // Allows you to be a relay for other peers - active: true // You will attempt to dial destination peers if you are not connected to them + enabled: true, // Allows you to be a relay for other peers. + timeout: 30 * 1000, // Incoming hop requests must complete within this timeout + applyConnectionLimits: true // Apply data/duration limits to relayed connections (default: true) + limit: { + duration: 120 * 1000 // the maximum amount of ms a relayed connection can be open for + data: BigInt(1 << 17) // the maximum amount of data that can be transferred over a relayed connection + } }, advertise: { + enabled: true, // Allows you to disable advertising the Hop service bootDelay: 15 * 60 * 1000, // Delay before HOP relay service is advertised on the network - enabled: true, // Allows you to disable the advertise of the Hop service - ttl: 30 * 60 * 1000 // Delay Between HOP relay service advertisements on the network + ttl: 30 * 60 * 1000 // Delay Between HOP relay service advertisements on the network + }, + reservationManager: { // the reservation manager creates reservations on discovered relays + enabled: true, // enable the reservation manager, default: false + maxReservations: 1 // the maximum number of relays to create reservations on } } }) ``` -#### Setup with Auto Relay +#### Setup with Automatic Reservations ```js import { createLibp2p } from 'libp2p' @@ -462,9 +471,9 @@ const node = await createLibp2p({ connectionEncryption: [noise()] relay: { // Circuit Relay options (this config is part of libp2p core configurations) enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay. - autoRelay: { + reservationManager: { enabled: true, // Allows you to bind to relays with HOP enabled for improving node dialability - maxListeners: 2 // Configure maximum number of HOP relays to use + maxListeners: 2 // Configure maximum number of HOP relays to use } } }) diff --git a/package.json b/package.json index a13f1f06bf..304988af69 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "@libp2p/interface-libp2p": "^1.0.0", "@libp2p/interface-metrics": "^4.0.0", "@libp2p/interface-peer-discovery": "^1.0.1", - "@libp2p/interface-peer-id": "^2.0.0", + "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-info": "^1.0.3", "@libp2p/interface-peer-routing": "^1.0.1", "@libp2p/interface-peer-store": "^1.2.2", @@ -122,7 +122,7 @@ "@libp2p/peer-id": "^2.0.0", "@libp2p/peer-id-factory": "^2.0.0", "@libp2p/peer-record": "^5.0.0", - "@libp2p/peer-store": "^6.0.0", + "@libp2p/peer-store": "^6.0.4", "@libp2p/tracked-map": "^3.0.0", "@libp2p/utils": "^3.0.2", "@multiformats/mafmt": "^11.0.2", @@ -143,11 +143,13 @@ "it-map": "^2.0.0", "it-merge": "^2.0.0", "it-pair": "^2.0.2", + "it-pb-stream": "^3.0.0", "it-pipe": "^2.0.3", "it-sort": "^2.0.0", "it-stream-types": "^1.0.4", "merge-options": "^3.0.4", "multiformats": "^11.0.0", + "p-defer": "^4.0.0", "p-fifo": "^1.0.0", "p-retry": "^5.0.0", "private-ip": "^3.0.0", @@ -162,17 +164,18 @@ "xsalsa20": "^1.1.0" }, "devDependencies": { + "@chainsafe/libp2p-gossipsub": "^6.2.0", "@chainsafe/libp2p-noise": "^11.0.0", "@chainsafe/libp2p-yamux": "^3.0.3", "@libp2p/bootstrap": "^6.0.0", - "@libp2p/daemon-client": "^4.0.1", - "@libp2p/daemon-server": "^4.0.1", + "@libp2p/daemon-client": "^5.0.0", + "@libp2p/daemon-server": "^4.1.0", "@libp2p/floodsub": "^6.0.0", "@libp2p/interface-compliance-tests": "^3.0.2", "@libp2p/interface-connection-compliance-tests": "^2.0.3", "@libp2p/interface-connection-encrypter-compliance-tests": "^4.0.0", "@libp2p/interface-mocks": "^9.0.0", - "@libp2p/interop": "^4.0.0", + "@libp2p/interop": "^7.0.0", "@libp2p/kad-dht": "^7.0.0", "@libp2p/mdns": "^6.0.0", "@libp2p/mplex": "^7.0.0", @@ -188,11 +191,10 @@ "cborg": "^1.8.1", "delay": "^5.0.0", "execa": "^7.0.0", - "go-libp2p": "^0.0.6", + "go-libp2p": "^1.0.1", "it-pushable": "^3.0.0", "it-to-buffer": "^3.0.0", "npm-run-all": "^4.1.5", - "p-defer": "^4.0.0", "p-event": "^5.0.1", "p-times": "^4.0.0", "p-wait-for": "^5.0.0", diff --git a/src/circuit/IMPLEMENTATION_NOTES.md b/src/circuit/IMPLEMENTATION_NOTES.md deleted file mode 100644 index abd4d61d17..0000000000 --- a/src/circuit/IMPLEMENTATION_NOTES.md +++ /dev/null @@ -1,128 +0,0 @@ -EDIT: This document is outdated and here only for historical purposes - -NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation - -Example: - -- if there are apples - - eat them -- if not, check for pears - - then eat them -- if not, check for cherries - - then eat them - -Or, - -- if there are apples - - eat them -- if not - - check for pears - - then eat them -- if not - - check for cherries - - then eat them - -In order to minimize nesting, the first example is preferred - -# Relay flow - -## Relay transport (dialer/listener) - -- ### Dial over a relay - - See if there is a relay that's already connected to the destination peer, if not - - Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful. - - If successful - - Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address - - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`. - - If no relays could connect, fail the same way a regular transport would - - Once the connection has been established, the swarm should treat it as a regular connection, - - i.e. muxing, encrypt, etc should all be performed on the relayed connection - -- ### Listen for relayed connections - - Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections - - A connection arrives - - read the address of the source peer from the incoming connection stream - - if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list - - pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow - -- ### Relay discovery and static relay addresses in swarm config - - - #### Relay address in swarm config - - A peer has relay addresses in its swarm config section - - On node startup, connect to the relays in swarm config - - if successful add address to swarms PeerInfo's multiaddresses - - `identify` should take care of announcing that the peer is reachable over the listed relays - - - #### Passive relay discovery - - A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.) - - *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible* - - *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list* - - *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.* - - *Should it be possible to disable passive relay discovery?* - - This could be useful when the peer wants to be reachable **only** over the listed relays - - If the incoming peer is a relay, send an `ls` and record its peers - -## Relay Nodes - -- ### Passive relay node - - *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.* - - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id - - check its swarm's peerbook(?) see if its a known peer, if it is - - use the swarms existing connection and - - send the multistream header and the source peer address to the dest peer - - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource` - - circuit the source and dest connections - - if couldn't dial, or the connection/stream to the dest peer closed prematurelly - - close the src stream - - -- ### Active relay node - - *An active relay node can dial other peers even if its swarm doesnt know about those peers* - - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id - - use the swarm to dial to the dest node - - send the multistream header and the source peer address to the dest peer - - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource` - - circuit the source and dest connections - - if couldn't dial, or the connection/stream to the dest peer closed prematurely - - close the src stream - -- ### `ls` command - - *A relay node can allow the peers known to it's swarm to be listed* - - *this should be possible to enable/disable from the config* - - when a relay gets the `ls` request - - if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs - - e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]` - - if disabled, respond with `na` - - -## Relay Implementation notes - -- ### Relay transport - - Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution. - -- ### `circuit-relay` - - This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay. - -## Relay swarm integration - -- The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p - - Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods - - - ### Listener - - the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto - - if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate - - when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed - - - ### Dialer - - When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on - - *The relay will be used in two cases* - - If the peer has an explicit relay address that it can be reached on - - no other transport is available - - The relay will attempt to dial the peer over that relay - - If no explicit relay address is provided - - no other transport is available - - A generic circuit address will be added to the peers multiaddr list - - i.e. `/p2p-circuit/ipfs/QmDest` - - If another transport is available, then use that instead of the relay - - diff --git a/src/circuit/README.md b/src/circuit/README.md deleted file mode 100644 index 712640e3c3..0000000000 --- a/src/circuit/README.md +++ /dev/null @@ -1,111 +0,0 @@ -# js-libp2p-circuit - -> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection) interface for dial/listen. - -**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit. - -`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node. You can read more about this in its [spec](https://github.com/libp2p/specs/tree/master/relay). - -## Table of Contents - -- [js-libp2p-circuit](#js-libp2p-circuit) - - [Table of Contents](#table-of-contents) - - [Why?](#why) - - [libp2p-circuit and IPFS](#libp2p-circuit-and-ipfs) - - [Usage](#usage) - - [API](#api) - - [Implementation rational](#implementation-rational) - -### Why? - -`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed. - -The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include: - - routing traffic between private nets and circumventing NAT layers - - route mangling for better privacy (matreshka/shallot dialing). - -It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes. - -### libp2p-circuit and IPFS - -Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network. - -## Usage - -Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGURATION.md#setup-with-relay). - -Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows: - -```js -import { multiaddr } from '@multiformats/multiaddr' -import Libp2p from 'libp2p' -import { tcp } from '@libp2p/tcp' -import { mplex } from '@libp2p/mplex' -import { noise } from '@chainsafe/libp2p-noise' - -const relayAddr = ... - -const node = await createLibp2p({ - addresses: { - listen: [multiaddr(`${relayAddr}/p2p-circuit`)] - }, - transports: [ - tcp() - ], - streamMuxers: [ - mplex() - ], - connectionEncryption: [ - noise() - ] - }, - config: { - relay: { // Circuit Relay options (this config is part of libp2p core configurations) - enabled: true // Allows you to dial and accept relayed connections. Does not make you a relay. - } - } -}) -``` - -## API - -[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport) - -`libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e: - -`/p2p-circuit/ip4/127.0.0.1/tcp/4001/p2p/QmHash` - -Both for dialing and listening. - -### Implementation rational - -This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening. - -``` -libp2p libp2p-circuit (transport) -+-------------------------------------------------+ +--------------------------+ -| +---------------------------------+ | | | -| | | | | +------------------+ | -| | | | circuit-relay listens for the HOP | | | | -| | libp2p <------------------------------------------------| circuit-relay | | -| | | | message to handle incomming relay | | | | -| | | | requests from other nodes | +------------------+ | -| +---------------------------------+ | | | -| ^ ^ ^ ^ ^ ^ | | +------------------+ | -| | | | | | | | | | +-------------+ | | -| | | | | | | | dialer uses libp2p to dial | | | | | | -| | | | +----------------------------------------------------------------------> dialer | | | -| | | transports | | to a circuit-relay node using the | | | | | | -| | | | | | | HOP message | | +-------------+ | | -| | | | | | | | | | | -| v v | v v | | | | | -|+------------------|----------------------------+| | | +-------------+ | | -|| | | | | || | | | | | | -||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | | -|| | +--------------------------------------------------------------------------> | | | -|| | | |plugs in just || circuit-relay nodes | | +-------------+ | | -|| | | |as any other || | | | | -|| | | |transport || | +------------------+ | -|+-----------------------------------------------+| | | -+-------------------------------------------------+ +--------------------------+ -``` diff --git a/src/circuit/auto-relay.ts b/src/circuit/auto-relay.ts deleted file mode 100644 index c6b2722cca..0000000000 --- a/src/circuit/auto-relay.ts +++ /dev/null @@ -1,292 +0,0 @@ -import { logger } from '@libp2p/logger' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import { RELAY_CODEC } from './multicodec.js' -import { canHop } from './circuit/hop.js' -import { namespaceToCid } from './utils.js' -import { - CIRCUIT_PROTO_CODE, - HOP_METADATA_KEY, - HOP_METADATA_VALUE, - RELAY_RENDEZVOUS_NS -} from './constants.js' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interface-peer-store' -import type { Connection } from '@libp2p/interface-connection' -import sort from 'it-sort' -import all from 'it-all' -import { pipe } from 'it-pipe' -import { publicAddressesFirst } from '@libp2p/utils/address-sort' -import type { RelayComponents } from './index.js' - -const log = logger('libp2p:auto-relay') - -const noop = () => {} - -export interface AutoRelayInit { - addressSorter?: AddressSorter - maxListeners?: number - onError?: (error: Error, msg?: string) => void -} - -export class AutoRelay { - private readonly components: RelayComponents - private readonly addressSorter: AddressSorter - private readonly maxListeners: number - private readonly listenRelays: Set - private readonly onError: (error: Error, msg?: string) => void - - constructor (components: RelayComponents, init: AutoRelayInit) { - this.components = components - this.addressSorter = init.addressSorter ?? publicAddressesFirst - this.maxListeners = init.maxListeners ?? 1 - this.listenRelays = new Set() - this.onError = init.onError ?? noop - - this._onProtocolChange = this._onProtocolChange.bind(this) - this._onPeerDisconnected = this._onPeerDisconnected.bind(this) - - this.components.peerStore.addEventListener('change:protocols', (evt) => { - void this._onProtocolChange(evt).catch(err => { - log.error(err) - }) - }) - this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected) - } - - /** - * Check if a peer supports the relay protocol. - * If the protocol is not supported, check if it was supported before and remove it as a listen relay. - * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if - * inside the threshold. - */ - async _onProtocolChange (evt: CustomEvent) { - const { - peerId, - protocols - } = evt.detail - const id = peerId.toString() - - // Check if it has the protocol - const hasProtocol = protocols.find(protocol => protocol === RELAY_CODEC) - - // If no protocol, check if we were keeping the peer before as a listenRelay - if (hasProtocol == null) { - if (this.listenRelays.has(id)) { - await this._removeListenRelay(id) - } - - return - } - - if (this.listenRelays.has(id)) { - return - } - - // If protocol, check if can hop, store info in the metadataBook and listen on it - try { - const connections = this.components.connectionManager.getConnections(peerId) - - if (connections.length === 0) { - return - } - - const connection = connections[0] - - // Do not hop on a relayed connection - if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) { - log(`relayed connection to ${id} will not be used to hop on`) - return - } - - const supportsHop = await canHop({ connection }) - - if (supportsHop) { - await this.components.peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE)) - await this._addListenRelay(connection, id) - } - } catch (err: any) { - this.onError(err) - } - } - - /** - * Peer disconnects - */ - _onPeerDisconnected (evt: CustomEvent) { - const connection = evt.detail - const peerId = connection.remotePeer - const id = peerId.toString() - - // Not listening on this relay - if (!this.listenRelays.has(id)) { - return - } - - this._removeListenRelay(id).catch(err => { - log.error(err) - }) - } - - /** - * Attempt to listen on the given relay connection - */ - async _addListenRelay (connection: Connection, id: string): Promise { - try { - // Check if already listening on enough relays - if (this.listenRelays.size >= this.maxListeners) { - return - } - - // Get peer known addresses and sort them with public addresses first - const remoteAddrs = await pipe( - await this.components.peerStore.addressBook.get(connection.remotePeer), - (source) => sort(source, this.addressSorter), - async (source) => await all(source) - ) - - // Attempt to listen on relay - const result = await Promise.all( - remoteAddrs.map(async addr => { - try { - let multiaddr = addr.multiaddr - - if (multiaddr.getPeerId() == null) { - multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`) - } - - multiaddr = multiaddr.encapsulate('/p2p-circuit') - - // Announce multiaddrs will update on listen success by TransportManager event being triggered - await this.components.transportManager.listen([multiaddr]) - return true - } catch (err: any) { - log.error('error listening on circuit address', err) - this.onError(err) - } - - return false - }) - ) - - if (result.includes(true)) { - this.listenRelays.add(id) - } - } catch (err: any) { - this.onError(err) - this.listenRelays.delete(id) - } - } - - /** - * Remove listen relay - */ - async _removeListenRelay (id: string) { - if (this.listenRelays.delete(id)) { - // TODO: this should be responsibility of the connMgr - await this._listenOnAvailableHopRelays([id]) - } - } - - /** - * Try to listen on available hop relay connections. - * The following order will happen while we do not have enough relays. - * 1. Check the metadata store for known relays, try to listen on the ones we are already connected. - * 2. Dial and try to listen on the peers we know that support hop but are not connected. - * 3. Search the network. - */ - async _listenOnAvailableHopRelays (peersToIgnore: string[] = []) { - // TODO: The peer redial issue on disconnect should be handled by connection gating - // Check if already listening on enough relays - if (this.listenRelays.size >= this.maxListeners) { - return - } - - const knownHopsToDial = [] - const peers = await this.components.peerStore.all() - - // Check if we have known hop peers to use and attempt to listen on the already connected - for (const { id, metadata } of peers) { - const idStr = id.toString() - - // Continue to next if listening on this or peer to ignore - if (this.listenRelays.has(idStr)) { - continue - } - - if (peersToIgnore.includes(idStr)) { - continue - } - - const supportsHop = metadata.get(HOP_METADATA_KEY) - - // Continue to next if it does not support Hop - if ((supportsHop == null) || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) { - continue - } - - const connections = this.components.connectionManager.getConnections(id) - - // If not connected, store for possible later use. - if (connections.length === 0) { - knownHopsToDial.push(id) - continue - } - - await this._addListenRelay(connections[0], idStr) - - // Check if already listening on enough relays - if (this.listenRelays.size >= this.maxListeners) { - return - } - } - - // Try to listen on known peers that are not connected - for (const peerId of knownHopsToDial) { - await this._tryToListenOnRelay(peerId) - - // Check if already listening on enough relays - if (this.listenRelays.size >= this.maxListeners) { - return - } - } - - // Try to find relays to hop on the network - try { - const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) - for await (const provider of this.components.contentRouting.findProviders(cid)) { - if (provider.multiaddrs.length === 0) { - continue - } - - const peerId = provider.id - - if (peerId.equals(this.components.peerId)) { - // Skip the provider if it's us as dialing will fail - continue - } - - await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs) - - await this._tryToListenOnRelay(peerId) - - // Check if already listening on enough relays - if (this.listenRelays.size >= this.maxListeners) { - return - } - } - } catch (err: any) { - this.onError(err) - } - } - - async _tryToListenOnRelay (peerId: PeerId) { - try { - const connection = await this.components.connectionManager.openConnection(peerId) - await this._addListenRelay(connection, peerId.toString()) - } catch (err: any) { - log.error('Could not use %p as relay', peerId, err) - this.onError(err, `could not connect and listen on known hop relay ${peerId.toString()}`) - } - } -} diff --git a/src/circuit/circuit/hop.ts b/src/circuit/circuit/hop.ts deleted file mode 100644 index 1ec63e0caf..0000000000 --- a/src/circuit/circuit/hop.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { logger } from '@libp2p/logger' -import errCode from 'err-code' -import { validateAddrs } from './utils.js' -import { StreamHandler } from './stream-handler.js' -import { CircuitRelay as CircuitPB } from '../pb/index.js' -import { pipe } from 'it-pipe' -import { codes as Errors } from '../../errors.js' -import { stop } from './stop.js' -import { RELAY_CODEC } from '../multicodec.js' -import type { Connection } from '@libp2p/interface-connection' -import { peerIdFromBytes } from '@libp2p/peer-id' -import type { Duplex } from 'it-stream-types' -import type { Circuit } from '../transport.js' -import type { ConnectionManager } from '@libp2p/interface-connection-manager' -import type { AbortOptions } from '@libp2p/interfaces' -import type { Uint8ArrayList } from 'uint8arraylist' - -const log = logger('libp2p:circuit:hop') - -export interface HopRequest { - connection: Connection - request: CircuitPB - streamHandler: StreamHandler - circuit: Circuit - connectionManager: ConnectionManager -} - -export async function handleHop (hopRequest: HopRequest): Promise { - const { - connection, - request, - streamHandler, - circuit, - connectionManager - } = hopRequest - - // Ensure hop is enabled - if (!circuit.hopEnabled()) { - log('HOP request received but we are not acting as a relay') - return streamHandler.end({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY - }) - } - - // Validate the HOP request has the required input - try { - validateAddrs(request, streamHandler) - } catch (err: any) { - log.error('invalid hop request via peer %p %o', connection.remotePeer, err) - - return - } - - if (request.dstPeer == null) { - log('HOP request received but we do not receive a dstPeer') - return - } - - // Get the connection to the destination (stop) peer - const destinationPeer = peerIdFromBytes(request.dstPeer.id) - - const destinationConnections = connectionManager.getConnections(destinationPeer) - if (destinationConnections.length === 0 && !circuit.hopActive()) { - log('HOP request received but we are not connected to the destination peer') - return streamHandler.end({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.HOP_NO_CONN_TO_DST - }) - } - - // TODO: Handle being an active relay - if (destinationConnections.length === 0) { - log('did not have connection to remote peer') - return streamHandler.end({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.HOP_NO_CONN_TO_DST - }) - } - - // Handle the incoming HOP request by performing a STOP request - const stopRequest = { - type: CircuitPB.Type.STOP, - dstPeer: request.dstPeer, - srcPeer: request.srcPeer - } - - let destinationStream: Duplex - try { - log('performing STOP request') - const result = await stop({ - connection: destinationConnections[0], - request: stopRequest - }) - - if (result == null) { - throw new Error('Could not stop') - } - - destinationStream = result - } catch (err: any) { - log.error(err) - - return - } - - log('hop request from %p is valid', connection.remotePeer) - streamHandler.write({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.SUCCESS - }) - const sourceStream = streamHandler.rest() - - log('creating related connections') - // Short circuit the two streams to create the relayed connection - return await pipe( - sourceStream, - destinationStream, - sourceStream - ) -} - -export interface HopConfig extends AbortOptions { - connection: Connection - request: CircuitPB -} - -/** - * Performs a HOP request to a relay peer, to request a connection to another - * peer. A new, virtual, connection will be created between the two via the relay. - */ -export async function hop (options: HopConfig): Promise> { - const { - connection, - request, - signal - } = options - - // Create a new stream to the relay - const stream = await connection.newStream(RELAY_CODEC, { - signal - }) - // Send the HOP request - const streamHandler = new StreamHandler({ stream }) - streamHandler.write(request) - - const response = await streamHandler.read() - - if (response == null) { - throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED) - } - - if (response.code === CircuitPB.Status.SUCCESS) { - log('hop request was successful') - - return streamHandler.rest() - } - - log('hop request failed with code %d, closing stream', response.code) - streamHandler.close() - - throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED) -} - -export interface CanHopOptions extends AbortOptions { - connection: Connection -} - -/** - * Performs a CAN_HOP request to a relay peer, in order to understand its capabilities - */ -export async function canHop (options: CanHopOptions) { - const { - connection, - signal - } = options - - // Create a new stream to the relay - const stream = await connection.newStream(RELAY_CODEC, { - signal - }) - - // Send the HOP request - const streamHandler = new StreamHandler({ stream }) - streamHandler.write({ - type: CircuitPB.Type.CAN_HOP - }) - - const response = await streamHandler.read() - await streamHandler.close() - - if (response == null || response.code !== CircuitPB.Status.SUCCESS) { - return false - } - - return true -} - -export interface HandleCanHopOptions { - connection: Connection - streamHandler: StreamHandler - circuit: Circuit -} - -/** - * Creates an unencoded CAN_HOP response based on the Circuits configuration - */ -export function handleCanHop (options: HandleCanHopOptions) { - const { - connection, - streamHandler, - circuit - } = options - const canHop = circuit.hopEnabled() - log('can hop (%s) request from %p', canHop, connection.remotePeer) - streamHandler.end({ - type: CircuitPB.Type.STATUS, - code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY - }) -} diff --git a/src/circuit/circuit/stop.ts b/src/circuit/circuit/stop.ts deleted file mode 100644 index 2e27d010fe..0000000000 --- a/src/circuit/circuit/stop.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { logger } from '@libp2p/logger' -import { CircuitRelay as CircuitPB } from '../pb/index.js' -import { RELAY_CODEC } from '../multicodec.js' -import { StreamHandler } from './stream-handler.js' -import { validateAddrs } from './utils.js' -import type { Connection } from '@libp2p/interface-connection' -import type { Duplex } from 'it-stream-types' -import type { AbortOptions } from '@libp2p/interfaces' -import type { Uint8ArrayList } from 'uint8arraylist' - -const log = logger('libp2p:circuit:stop') - -export interface HandleStopOptions { - connection: Connection - request: CircuitPB - streamHandler: StreamHandler -} - -/** - * Handles incoming STOP requests - */ -export function handleStop (options: HandleStopOptions): Duplex | undefined { - const { - connection, - request, - streamHandler - } = options - - // Validate the STOP request has the required input - try { - validateAddrs(request, streamHandler) - } catch (err: any) { - log.error('invalid stop request via peer %p %o', connection.remotePeer, err) - return - } - - // The request is valid - log('stop request is valid') - streamHandler.write({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.SUCCESS - }) - - return streamHandler.rest() -} - -export interface StopOptions extends AbortOptions { - connection: Connection - request: CircuitPB -} - -/** - * Creates a STOP request - */ -export async function stop (options: StopOptions) { - const { - connection, - request, - signal - } = options - - const stream = await connection.newStream(RELAY_CODEC, { - signal - }) - log('starting stop request to %p', connection.remotePeer) - const streamHandler = new StreamHandler({ stream }) - - streamHandler.write(request) - const response = await streamHandler.read() - - if (response == null) { - streamHandler.close() - return - } - - if (response.code === CircuitPB.Status.SUCCESS) { - log('stop request to %p was successful', connection.remotePeer) - return streamHandler.rest() - } - - log('stop request failed with code %d', response.code) - streamHandler.close() -} diff --git a/src/circuit/circuit/stream-handler.ts b/src/circuit/circuit/stream-handler.ts deleted file mode 100644 index 0638733f4e..0000000000 --- a/src/circuit/circuit/stream-handler.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { logger } from '@libp2p/logger' -import * as lp from 'it-length-prefixed' -import { Handshake, handshake } from 'it-handshake' -import { CircuitRelay } from '../pb/index.js' -import type { Stream } from '@libp2p/interface-connection' -import type { Source } from 'it-stream-types' -import type { Uint8ArrayList } from 'uint8arraylist' - -const log = logger('libp2p:circuit:stream-handler') - -export interface StreamHandlerOptions { - /** - * A duplex iterable - */ - stream: Stream - - /** - * max bytes length of message - */ - maxLength?: number -} - -export class StreamHandler { - private readonly stream: Stream - private readonly shake: Handshake - private readonly decoder: Source - - constructor (options: StreamHandlerOptions) { - const { stream, maxLength = 4096 } = options - - this.stream = stream - this.shake = handshake(this.stream) - this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength }) - } - - /** - * Read and decode message - */ - async read () { - // @ts-expect-error FIXME is a source, needs to be a generator - const msg = await this.decoder.next() - - if (msg.value != null) { - const value = CircuitRelay.decode(msg.value) - log('read message type', value.type) - return value - } - - log('read received no value, closing stream') - // End the stream, we didn't get data - this.close() - } - - /** - * Encode and write array of buffers - */ - write (msg: CircuitRelay) { - log('write message type %s', msg.type) - this.shake.write(lp.encode.single(CircuitRelay.encode(msg))) - } - - /** - * Return the handshake rest stream and invalidate handler - */ - rest () { - this.shake.rest() - return this.shake.stream - } - - /** - * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message - */ - end (msg: CircuitRelay) { - this.write(msg) - this.close() - } - - /** - * Close the stream - */ - close () { - log('closing the stream') - void this.rest().sink([]).catch(err => { - log.error(err) - }) - } -} diff --git a/src/circuit/circuit/utils.ts b/src/circuit/circuit/utils.ts deleted file mode 100644 index b1c6d78e96..0000000000 --- a/src/circuit/circuit/utils.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { multiaddr } from '@multiformats/multiaddr' -import { CircuitRelay } from '../pb/index.js' -import type { StreamHandler } from './stream-handler.js' - -/** - * Write a response - */ -function writeResponse (streamHandler: StreamHandler, status: CircuitRelay.Status) { - streamHandler.write({ - type: CircuitRelay.Type.STATUS, - code: status - }) -} - -/** - * Validate incomming HOP/STOP message - */ -export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler) { - try { - if (msg.dstPeer?.addrs != null) { - msg.dstPeer.addrs.forEach((addr) => { - return multiaddr(addr) - }) - } - } catch (err: any) { - writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP - ? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID - : CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID) - throw err - } - - try { - if (msg.srcPeer?.addrs != null) { - msg.srcPeer.addrs.forEach((addr) => { - return multiaddr(addr) - }) - } - } catch (err: any) { - writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP - ? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID - : CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID) - throw err - } -} diff --git a/src/circuit/client.ts b/src/circuit/client.ts new file mode 100644 index 0000000000..9ffaa0028e --- /dev/null +++ b/src/circuit/client.ts @@ -0,0 +1,381 @@ +import { logger } from '@libp2p/logger' +import { RELAY_V2_HOP_CODEC } from './multicodec.js' +import { getExpiration, namespaceToCid } from './utils.js' +import { + CIRCUIT_PROTO_CODE, + DEFAULT_MAX_RESERVATIONS, + RELAY_RENDEZVOUS_NS +} from './constants.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { AddressSorter } from '@libp2p/interface-peer-store' +import type { Connection } from '@libp2p/interface-connection' +import sort from 'it-sort' +import all from 'it-all' +import { pipe } from 'it-pipe' +import { publicAddressesFirst } from '@libp2p/utils/address-sort' +import { reserve } from './hop.js' +import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' +import type { Startable } from '@libp2p/interfaces/startable' +import type { Components } from '../components.js' +import type { RelayReservationManagerConfig } from './index.js' +import { PeerSet, PeerMap, PeerList } from '@libp2p/peer-collections' + +const log = logger('libp2p:circuit:client') + +const noop = () => {} + +/** + * CircuitServiceInit initializes the circuit service using values + * from the provided config and an @type{AddressSorter}. + */ +export interface CircuitServiceInit extends RelayReservationManagerConfig { + /** + * Allows prioritizing addresses from the peerstore for dialing. The + * default behavior is to prioritise public addresses. + */ + addressSorter?: AddressSorter + /** + * A callback to invoke when an error occurs in the circuit service. + */ + onError?: (error: Error) => void +} + +export interface RelayReservationManagerEvents { + 'relay:reservation': CustomEvent +} + +/** + * ReservationManager automatically makes a circuit v2 reservation on any connected + * peers that support the circuit v2 HOP protocol. + */ +export class RelayReservationManager extends EventEmitter implements Startable { + private readonly components: Components + private readonly addressSorter: AddressSorter + private readonly maxReservations: number + private readonly relays: PeerSet + private readonly reservationMap: PeerMap> + private readonly onError: (error: Error) => void + private started: boolean + + constructor (components: Components, init: CircuitServiceInit) { + super() + this.started = false + this.components = components + this.addressSorter = init.addressSorter ?? publicAddressesFirst + this.maxReservations = init.maxReservations ?? DEFAULT_MAX_RESERVATIONS + this.relays = new PeerSet() + this.reservationMap = new PeerMap() + this.onError = init.onError ?? noop + + this._onProtocolChange = this._onProtocolChange.bind(this) + this._onPeerDisconnected = this._onPeerDisconnected.bind(this) + this._onPeerConnect = this._onPeerConnect.bind(this) + + this.components.peerStore.addEventListener('change:protocols', (evt) => { + void this._onProtocolChange(evt.detail).catch(err => { + log.error('handling protocol change failed', err) + }) + }) + + this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected) + this.components.connectionManager.addEventListener('peer:connect', this._onPeerConnect) + } + + isStarted () { + return this.started + } + + start () { + void this._listenOnAvailableHopRelays().catch(err => { log.error('error listening on relays', err) }) + this.started = true + } + + async stop () { + this.reservationMap.forEach((timer) => clearTimeout(timer)) + this.reservationMap.clear() + this.relays.clear() + } + + /** + * Check if a peer supports the relay protocol. + * If the protocol is not supported, check if it was supported before and remove it as a listen relay. + * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if + * inside the threshold. + */ + async _onProtocolChange ({ peerId, protocols }: {peerId: PeerId, protocols: string[]}) { + if (peerId.equals(this.components.peerId)) { + return + } + + // Check if it has the protocol + const hasProtocol = protocols.includes(RELAY_V2_HOP_CODEC) + log.trace('Peer %p protocol change %p', peerId, this.components.peerId) + + // If no protocol, check if we were keeping the peer before as a listenRelay + if (!hasProtocol) { + if (this.relays.has(peerId)) { + await this._removeListenRelay(peerId) + } + return + } + + if (this.relays.has(peerId)) { + return + } + + // If protocol, check if can hop, store info in the metadataBook and listen on it + try { + const connections = this.components.connectionManager.getConnections(peerId) + + // if no connections, try to listen on relay + if (connections.length === 0) { + void this._tryToListenOnRelay(peerId) + return + } + const connection = connections[0] + + // Do not hop on a relayed connection + if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) { + log('relayed connection to %p will not be used to hop on', peerId) + return + } + + await this._addListenRelay(connection, peerId) + } catch (err: any) { + log.error('could not add %p as relay', peerId) + this.onError(err) + } + } + + /** + * Handle case when peer connects. If we already have the peer in the protobook, + * we treat this event as an `onProtocolChange`. + */ + _onPeerConnect ({ detail: connection }: CustomEvent) { + void this.components.peerStore.protoBook.get(connection.remotePeer) + .then((protocols) => { + void this._onProtocolChange({ peerId: connection.remotePeer, protocols }) + .catch((err) => log.error('handling reconnect failed', err)) + }, + (err) => { + // this is not necessarily an error as we will only have the protocols stored + // in case of a reconnect + log.trace('could not fetch protocols for peer: %p', connection.remotePeer, err) + }) + } + + /** + * Peer disconnects + */ + _onPeerDisconnected (evt: CustomEvent) { + const connection = evt.detail + const peerId = connection.remotePeer + clearTimeout(this.reservationMap.get(peerId)) + this.reservationMap.delete(peerId) + + // Not listening on this relay + if (!this.relays.has(peerId)) { + return + } + + this._removeListenRelay(peerId).catch(err => { + log.error(err) + }) + } + + /** + * Attempt to listen on the given relay connection + */ + async _addListenRelay (connection: Connection, peerId: PeerId): Promise { + log.trace('peerId %p is being added as relay', peerId) + try { + // Check if already enough relay reservations + if (this.relays.size >= this.maxReservations) { + return + } + + await this.createOrRefreshReservation(peerId) + + // Get peer known addresses and sort them with public addresses first + const remoteAddrs = await pipe( + await this.components.peerStore.addressBook.get(connection.remotePeer), + (source) => sort(source, this.addressSorter), + async (source) => await all(source) + ) + + // Attempt to listen on relay + const result = await Promise.all( + remoteAddrs.map(async addr => { + let multiaddr = addr.multiaddr + + if (multiaddr.getPeerId() == null) { + multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`) + } + multiaddr = multiaddr.encapsulate('/p2p-circuit') + try { + // Announce multiaddrs will update on listen success by TransportManager event being triggered + await this.components.transportManager.listen([multiaddr]) + return true + } catch (err: any) { + log.error('error listening on circuit address', multiaddr, err) + this.onError(err) + } + + return false + }) + ) + + if (result.includes(true)) { + this.relays.add(peerId) + } + } catch (err: any) { + this.relays.delete(peerId) + log.error('error adding relay for %p %s', peerId, err) + this.onError(err) + } + } + + /** + * Remove listen relay + */ + async _removeListenRelay (PeerId: PeerId) { + const recheck = this.relays.has(PeerId) + this.relays.delete(PeerId) + if (recheck) { + // TODO: this should be responsibility of the connMgr + await this._listenOnAvailableHopRelays(new PeerList([PeerId])) + } + } + + /** + * Try to listen on available hop relay connections. + * The following order will happen while we do not have enough relays. + * 1. Check the metadata store for known relays, try to listen on the ones we are already connected. + * 2. Dial and try to listen on the peers we know that support hop but are not connected. + * 3. Search the network. + */ + async _listenOnAvailableHopRelays (peersToIgnore: PeerList = new PeerList([])) { + // Check if already listening on enough relays + if (this.relays.size >= this.maxReservations) { + return + } + + const knownHopsToDial: PeerId[] = [] + const peers = (await this.components.peerStore.all()) + // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on + .filter(({ id, protocols }) => + protocols.includes(RELAY_V2_HOP_CODEC) && !this.relays.has(id) && !peersToIgnore.includes(id) + ) + .map(({ id }) => { + const connections = this.components.connectionManager.getConnections(id) + if (connections.length === 0) { + knownHopsToDial.push(id) + return [id, null] + } + return [id, connections[0]] + }) + .sort(() => Math.random() - 0.5) + + // Check if we have known hop peers to use and attempt to listen on the already connected + for (const [id, conn] of peers) { + await this._addListenRelay(conn as Connection, id as PeerId) + + // Check if already listening on enough relays + if (this.relays.size >= this.maxReservations) { + return + } + } + + // Try to listen on known peers that are not connected + for (const peerId of knownHopsToDial) { + // Check if already listening on enough relays + if (this.relays.size >= this.maxReservations) { + return + } + + await this._tryToListenOnRelay(peerId) + } + + // Try to find relays to hop on the network + try { + const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) + for await (const provider of this.components.contentRouting.findProviders(cid)) { + if ( + provider.multiaddrs.length > 0 && + !provider.id.equals(this.components.peerId) + ) { + const peerId = provider.id + + await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs) + await this._tryToListenOnRelay(peerId) + + // Check if already listening on enough relays + if (this.relays.size >= this.maxReservations) { + return + } + } + } + } catch (err: any) { + log.error('failed when finding relays on the network', err) + this.onError(err) + } + } + + async _tryToListenOnRelay (peerId: PeerId) { + try { + if (peerId.equals(this.components.peerId)) { + log.trace('Skipping dialling self %p', peerId.toString()) + return + } + const connection = await this.components.connectionManager.openConnection(peerId) + await this._addListenRelay(connection, peerId) + } catch (err: any) { + log.error('Could not connect and listen on relay %p', peerId, err) + this.onError(err) + } + } + + private readonly createOrRefreshReservation = async (peerId: PeerId) => { + try { + const connections = this.components.connectionManager.getConnections(peerId) + + if (connections.length === 0) { + throw new Error('No connections to peer') + } + + const connection = connections[0] + + const reservation = await reserve(connection) + + const refreshReservation = this.createOrRefreshReservation + + if (reservation != null) { + log('new reservation on %p', peerId) + + // clear any previous timeouts + const previous = this.reservationMap.get(peerId) + if (previous != null) { + clearTimeout(previous) + } + + const timeout = setTimeout( + (peerId: PeerId) => { + void refreshReservation(peerId).catch(err => { + log.error('error refreshing reservation for %p', peerId, err) + }) + }, + Math.max(getExpiration(reservation.expire) - 100, 0), + peerId + ) + this.reservationMap.set( + peerId, + timeout + ) + this.dispatchEvent(new CustomEvent('relay:reservation')) + } + } catch (err: any) { + log.error(err) + await this._removeListenRelay(peerId) + } + } +} diff --git a/src/circuit/constants.ts b/src/circuit/constants.ts index 8405a0fa74..49a927be6c 100644 --- a/src/circuit/constants.ts +++ b/src/circuit/constants.ts @@ -16,16 +16,22 @@ export const ADVERTISE_TTL = 30 * minute export const CIRCUIT_PROTO_CODE = 290 /** - * PeerStore metadaBook key for HOP relay service + * Relay HOP relay service namespace for discovery */ -export const HOP_METADATA_KEY = 'hop_relay' +export const RELAY_RENDEZVOUS_NS = '/libp2p/relay' /** - * PeerStore metadaBook value for HOP relay service + * Maximum reservations for auto relay */ -export const HOP_METADATA_VALUE = 'true' +export const DEFAULT_MAX_RESERVATIONS = 1 -/** - * Relay HOP relay service namespace for discovery - */ -export const RELAY_RENDEZVOUS_NS = '/libp2p/relay' +export const RELAY_DESTINATION_TAG = 'relay-destination' + +// circuit v2 connection limits +// https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66 + +// 2 min is the default connection duration +export const DEFAULT_DURATION_LIMIT = 2 * 60 * 1000 + +// 128k is the default data limit +export const DEFAULT_DATA_LIMIT = BigInt(1 << 17) diff --git a/src/circuit/hop.ts b/src/circuit/hop.ts new file mode 100644 index 0000000000..a93c3128ba --- /dev/null +++ b/src/circuit/hop.ts @@ -0,0 +1,210 @@ +import type { PeerId } from '@libp2p/interface-peer-id' +import { RecordEnvelope } from '@libp2p/peer-record' +import { logger } from '@libp2p/logger' +import type { Connection, Stream } from '@libp2p/interface-connection' +import { HopMessage, Limit, Reservation, Status, StopMessage } from './pb/index.js' +import type { Multiaddr } from '@multiformats/multiaddr' +import { multiaddr } from '@multiformats/multiaddr' +import type { Acl, ReservationStore } from './interfaces.js' +import { RELAY_V2_HOP_CODEC } from './multicodec.js' +import { stop } from './stop.js' +import { ReservationVoucherRecord } from './reservation-voucher.js' +import { peerIdFromBytes } from '@libp2p/peer-id' +import type { ConnectionManager } from '@libp2p/interface-connection-manager' +import type { ProtobufStream } from 'it-pb-stream' +import { pbStream } from 'it-pb-stream' +import { CIRCUIT_PROTO_CODE, RELAY_DESTINATION_TAG } from './constants.js' +import type { PeerStore } from '@libp2p/interface-peer-store' +import { createLimitedRelay } from './utils.js' + +const log = logger('libp2p:circuit:v2:hop') + +export interface HopProtocolOptions { + connection: Connection + request: HopMessage + stream: ProtobufStream + relayPeer: PeerId + relayAddrs: Multiaddr[] + limit?: Limit + acl?: Acl + reservationStore: ReservationStore + connectionManager: ConnectionManager + peerStore: PeerStore +} + +export async function handleHopProtocol (options: HopProtocolOptions): Promise { + const { stream, request } = options + log('received hop message') + switch (request.type) { + case HopMessage.Type.RESERVE: await handleReserve(options); break + case HopMessage.Type.CONNECT: await handleConnect(options); break + default: { + log.error('invalid hop request type %s via peer %s', options.request.type, options.connection.remotePeer) + stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }) + } + } +} + +export async function reserve (connection: Connection): Promise { + log('requesting reservation from %s', connection.remotePeer) + const stream = await connection.newStream([RELAY_V2_HOP_CODEC]) + const pbstr = pbStream(stream) + const hopstr = pbstr.pb(HopMessage) + hopstr.write({ type: HopMessage.Type.RESERVE }) + + let response: HopMessage + try { + response = await hopstr.read() + } catch (err: any) { + log.error('error passing reserve message response from %s because', connection.remotePeer, err.message) + stream.close() + throw err + } + + if (response.status === Status.OK && (response.reservation != null)) { + return response.reservation + } + const errMsg = `reservation failed with status ${response.status ?? 'undefined'}` + log.error(errMsg) + throw new Error(errMsg) +} + +const isRelayAddr = (ma: Multiaddr): boolean => ma.protoCodes().includes(CIRCUIT_PROTO_CODE) + +async function handleReserve ({ connection, stream: pbstr, relayPeer, relayAddrs, acl, reservationStore, peerStore }: HopProtocolOptions): Promise { + const hopstr = pbstr.pb(HopMessage) + log('hop reserve request from %s', connection.remotePeer) + + if (isRelayAddr(connection.remoteAddr)) { + log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }) + return + } + + if ((await acl?.allowReserve?.(connection.remotePeer, connection.remoteAddr)) === false) { + log.error('acl denied reservation to %s', connection.remotePeer) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }) + return + } + + const result = await reservationStore.reserve(connection.remotePeer, connection.remoteAddr) + + if (result.status !== Status.OK) { + hopstr.write({ type: HopMessage.Type.STATUS, status: result.status }) + return + } + + try { + // tag relayed peer + // result.expire is non-null if `ReservationStore.reserve` returns with status == OK + if (result.expire != null) { + const ttl = new Date().getTime() - result.expire + await peerStore.tagPeer(relayPeer, RELAY_DESTINATION_TAG, { value: 1, ttl }) + } + hopstr.write({ + type: HopMessage.Type.STATUS, + status: Status.OK, + reservation: await makeReservation(relayAddrs, relayPeer, connection.remotePeer, BigInt(result.expire ?? 0)), + limit: (await reservationStore.get(relayPeer))?.limit + }) + log('sent confirmation response to %s', connection.remotePeer) + } catch (err) { + log.error('failed to send confirmation response to %s', connection.remotePeer) + await reservationStore.removeReservation(connection.remotePeer) + } +} + +async function handleConnect (options: HopProtocolOptions): Promise { + const { connection, stream, request, reservationStore, connectionManager, acl } = options + const hopstr = stream.pb(HopMessage) + + log('hop connect request from %s', connection.remotePeer) + + let dstPeer: PeerId + try { + if (request.peer == null) { + log.error('no peer info in hop connect request') + throw new Error('no peer info in request') + } + request.peer.addrs.forEach(multiaddr) + dstPeer = peerIdFromBytes(request.peer.id) + } catch (err) { + log.error('invalid hop connect request via peer %p %s', connection.remotePeer, err) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }) + return + } + + if (acl?.allowConnect !== undefined) { + const status = await acl.allowConnect(connection.remotePeer, connection.remoteAddr, dstPeer) + if (status !== Status.OK) { + log.error('hop connect denied for %s with status %s', connection.remotePeer, status) + hopstr.write({ type: HopMessage.Type.STATUS, status: status }) + return + } + } + + if (!await reservationStore.hasReservation(dstPeer)) { + log.error('hop connect denied for %s with status %s', connection.remotePeer, Status.NO_RESERVATION) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }) + return + } + + const connections = connectionManager.getConnections(dstPeer) + if (connections.length === 0) { + log('hop connect denied for %s as there is no destination connection', connection.remotePeer) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }) + return + } + const destinationConnection = connections[0] + log('hop connect request from %s to %s is valid', connection.remotePeer, dstPeer) + + const destinationStream = await stop({ + connection: destinationConnection, + request: { + type: StopMessage.Type.CONNECT, + peer: { + id: connection.remotePeer.toBytes(), + addrs: [multiaddr('/p2p/' + connection.remotePeer.toString()).bytes] + } + } + }) + + if (destinationStream == null) { + log.error('failed to open stream to destination peer %s', destinationConnection?.remotePeer) + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED }) + return + } + + hopstr.write({ type: HopMessage.Type.STATUS, status: Status.OK }) + const sourceStream = stream.unwrap() + + log('connection to destination established, short circuiting streams...') + const limit = (await reservationStore.get(dstPeer))?.limit + // Short circuit the two streams to create the relayed connection + return createLimitedRelay(sourceStream, destinationStream, limit) +} + +async function makeReservation ( + relayAddrs: Multiaddr[], + relayPeerId: PeerId, + remotePeer: PeerId, + expire: bigint +): Promise { + const addrs = [] + + for (const relayAddr of relayAddrs) { + addrs.push(relayAddr.bytes) + } + + const voucher = await RecordEnvelope.seal(new ReservationVoucherRecord({ + peer: remotePeer, + relay: relayPeerId, + expiration: Number(expire) + }), relayPeerId) + + return { + addrs, + expire, + voucher: voucher.marshal() + } +} diff --git a/src/circuit/index.ts b/src/circuit/index.ts index a5870fbc71..e09bdee41a 100644 --- a/src/circuit/index.ts +++ b/src/circuit/index.ts @@ -1,135 +1,73 @@ -import { logger } from '@libp2p/logger' -import { codes } from '../errors.js' -import { - setDelayedInterval, - clearDelayedInterval -// @ts-expect-error set-delayed-interval does not export types -} from 'set-delayed-interval' -import { AutoRelay } from './auto-relay.js' -import { namespaceToCid } from './utils.js' -import { - RELAY_RENDEZVOUS_NS -} from './constants.js' -import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store' -import type { Startable } from '@libp2p/interfaces/startable' -import type { ContentRouting } from '@libp2p/interface-content-routing' -import type { ConnectionManager } from '@libp2p/interface-connection-manager' -import type { TransportManager } from '@libp2p/interface-transport' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { StreamHandlerOptions } from '@libp2p/interface-registrar' - -export interface RelayConfig extends StreamHandlerOptions { +/** + * RelayConfig configures the circuit v2 relay transport. + */ +export interface RelayConfig { + /** + * Enable dialing a client over a relay and receiving relayed connections. + * This in itself does not enable the node to act as a relay. + */ enabled: boolean advertise: RelayAdvertiseConfig hop: HopConfig - autoRelay: AutoRelayConfig -} - -export interface HopConfig { - enabled?: boolean - active?: boolean - timeout: number -} - -const log = logger('libp2p:relay') - -export interface RelayAdvertiseConfig { - bootDelay?: number - enabled?: boolean - ttl?: number + reservationManager: RelayReservationManagerConfig } -export interface AutoRelayConfig { +/** + * RelayReservationManagerConfig allows the node to automatically listen + * on any discovered relays upto a specified maximum. + */ +export interface RelayReservationManagerConfig { + /** + * enable or disable autorelay (default: false) + */ enabled?: boolean /** - * maximum number of relays to listen + * maximum number of relays to listen (default: 1) */ - maxListeners: number -} - -export interface RelayInit extends RelayConfig { - addressSorter?: AddressSorter -} - -export interface RelayComponents { - peerId: PeerId - contentRouting: ContentRouting - peerStore: PeerStore - connectionManager: ConnectionManager - transportManager: TransportManager + maxReservations?: number } -export class Relay implements Startable { - private readonly components: RelayComponents - private readonly init: RelayInit - // @ts-expect-error this field isn't used anywhere? - private readonly autoRelay?: AutoRelay - private timeout?: any - private started: boolean - +/** + * Configures using the node as a HOP relay + */ +export interface HopConfig { /** - * Creates an instance of Relay + * If true this node will function as a limited relay (default: false) */ - constructor (components: RelayComponents, init: RelayInit) { - this.components = components - // Create autoRelay if enabled - this.autoRelay = init.autoRelay?.enabled !== false - ? new AutoRelay(components, { - addressSorter: init.addressSorter, - ...init.autoRelay - }) - : undefined - - this.started = false - this.init = init - this._advertiseService = this._advertiseService.bind(this) - } - - isStarted () { - return this.started - } + enabled?: boolean /** - * Start Relay service + * timeout for hop requests to complete */ - async start () { - // Advertise service if HOP enabled - if (this.init.hop.enabled !== false && this.init.advertise.enabled !== false) { - this.timeout = setDelayedInterval( - this._advertiseService, this.init.advertise.ttl, this.init.advertise.bootDelay - ) - } - - this.started = true - } + timeout: number /** - * Stop Relay service + * If false, no connection limits will be applied to relayed connections (default: true) */ - async stop () { - if (this.timeout != null) { - clearDelayedInterval(this.timeout) - } - - this.started = false - } + applyConnectionLimits?: boolean /** - * Advertise hop relay service in the network. + * Limits to apply to incoming relay connections - relayed connections will be closed if + * these limits are exceeded. */ - async _advertiseService () { - try { - const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) - await this.components.contentRouting.provide(cid) - } catch (err: any) { - if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) { - log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) - // Stop the advertise - await this.stop() - } else { - log.error(err) - } - } + limit?: { + /** + * How long to relay a connection for in milliseconds (default: 2m) + */ + duration?: number + + /** + * How many bytes to allow to be transferred over a relayed connection (default: 128k) + */ + data?: bigint } } + +export interface RelayAdvertiseConfig { + bootDelay?: number + enabled?: boolean + ttl?: number +} + +export { Relay } from './relay.js' diff --git a/src/circuit/interfaces.ts b/src/circuit/interfaces.ts new file mode 100644 index 0000000000..3c0d428c68 --- /dev/null +++ b/src/circuit/interfaces.ts @@ -0,0 +1,28 @@ +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Multiaddr } from '@multiformats/multiaddr' +import type { Limit, Status } from './pb/index.js' + +export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED + +export interface Reservation { + addr: Multiaddr + expire: Date + limit?: Limit +} + +export interface ReservationStore { + reserve: (peer: PeerId, addr: Multiaddr, limit?: Limit) => {status: ReservationStatus, expire?: number} + removeReservation: (peer: PeerId) => void + hasReservation: (dst: PeerId) => boolean + get: (peer: PeerId) => Reservation | undefined +} + +export type AclStatus = Status.OK | Status.RESOURCE_LIMIT_EXCEEDED | Status.PERMISSION_DENIED + +export interface Acl { + allowReserve: (peer: PeerId, addr: Multiaddr) => Promise + /** + * Checks if connection should be allowed + */ + allowConnect: (src: PeerId, addr: Multiaddr, dst: PeerId) => Promise +} diff --git a/src/circuit/multicodec.ts b/src/circuit/multicodec.ts index fcd2822165..f1c290732c 100644 --- a/src/circuit/multicodec.ts +++ b/src/circuit/multicodec.ts @@ -1,2 +1,3 @@ -export const RELAY_CODEC = '/libp2p/circuit/relay/0.1.0' +export const RELAY_V2_HOP_CODEC = '/libp2p/circuit/relay/0.2.0/hop' +export const RELAY_V2_STOP_CODEC = '/libp2p/circuit/relay/0.2.0/stop' diff --git a/src/circuit/pb/index.proto b/src/circuit/pb/index.proto index 1eaec2e29b..c0ecb1d796 100644 --- a/src/circuit/pb/index.proto +++ b/src/circuit/pb/index.proto @@ -1,42 +1,67 @@ syntax = "proto3"; -message CircuitRelay { - - enum Status { - SUCCESS = 100; - HOP_SRC_ADDR_TOO_LONG = 220; - HOP_DST_ADDR_TOO_LONG = 221; - HOP_SRC_MULTIADDR_INVALID = 250; - HOP_DST_MULTIADDR_INVALID = 251; - HOP_NO_CONN_TO_DST = 260; - HOP_CANT_DIAL_DST = 261; - HOP_CANT_OPEN_DST_STREAM = 262; - HOP_CANT_SPEAK_RELAY = 270; - HOP_CANT_RELAY_TO_SELF = 280; - STOP_SRC_ADDR_TOO_LONG = 320; - STOP_DST_ADDR_TOO_LONG = 321; - STOP_SRC_MULTIADDR_INVALID = 350; - STOP_DST_MULTIADDR_INVALID = 351; - STOP_RELAY_REFUSED = 390; - MALFORMED_MESSAGE = 400; +message HopMessage { + enum Type { + RESERVE = 0; + CONNECT = 1; + STATUS = 2; } - enum Type { // RPC identifier, either HOP, STOP or STATUS - HOP = 1; - STOP = 2; - STATUS = 3; - CAN_HOP = 4; - } + // the presence of this field is enforced at application level + optional Type type = 1; + + optional Peer peer = 2; + optional Reservation reservation = 3; + optional Limit limit = 4; + + optional Status status = 5; +} - message Peer { - required bytes id = 1; // peer id - repeated bytes addrs = 2; // peer's known addresses +message StopMessage { + enum Type { + CONNECT = 0; + STATUS = 1; } - optional Type type = 1; // Type of the message + // the presence of this field is enforced at application level + optional Type type = 1; + + optional Peer peer = 2; + optional Limit limit = 3; + + optional Status status = 4; +} + +message Peer { + bytes id = 1; + repeated bytes addrs = 2; +} + +message Reservation { + uint64 expire = 1; // Unix expiration time (UTC) + repeated bytes addrs = 2; // relay addrs for reserving peer + optional bytes voucher = 3; // reservation voucher +} - optional Peer srcPeer = 2; // srcPeer and dstPeer are used when Type is HOP or STATUS - optional Peer dstPeer = 3; +message Limit { + optional uint32 duration = 1; // seconds + optional uint64 data = 2; // bytes +} + +enum Status { + UNUSED = 0; + OK = 100; + RESERVATION_REFUSED = 200; + RESOURCE_LIMIT_EXCEEDED = 201; + PERMISSION_DENIED = 202; + CONNECTION_FAILED = 203; + NO_RESERVATION = 204; + MALFORMED_MESSAGE = 400; + UNEXPECTED_MESSAGE = 401; +} - optional Status code = 4; // Status code, used when Type is STATUS +message ReservationVoucher { + bytes relay = 1; + bytes peer = 2; + uint64 expiration = 3; } diff --git a/src/circuit/pb/index.ts b/src/circuit/pb/index.ts index 533fd39669..e14b653842 100644 --- a/src/circuit/pb/index.ts +++ b/src/circuit/pb/index.ts @@ -2,184 +2,183 @@ /* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ +/* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' -export interface CircuitRelay { - type?: CircuitRelay.Type - srcPeer?: CircuitRelay.Peer - dstPeer?: CircuitRelay.Peer - code?: CircuitRelay.Status +export interface HopMessage { + type?: HopMessage.Type + peer?: Peer + reservation?: Reservation + limit?: Limit + status?: Status } -export namespace CircuitRelay { - export enum Status { - SUCCESS = 'SUCCESS', - HOP_SRC_ADDR_TOO_LONG = 'HOP_SRC_ADDR_TOO_LONG', - HOP_DST_ADDR_TOO_LONG = 'HOP_DST_ADDR_TOO_LONG', - HOP_SRC_MULTIADDR_INVALID = 'HOP_SRC_MULTIADDR_INVALID', - HOP_DST_MULTIADDR_INVALID = 'HOP_DST_MULTIADDR_INVALID', - HOP_NO_CONN_TO_DST = 'HOP_NO_CONN_TO_DST', - HOP_CANT_DIAL_DST = 'HOP_CANT_DIAL_DST', - HOP_CANT_OPEN_DST_STREAM = 'HOP_CANT_OPEN_DST_STREAM', - HOP_CANT_SPEAK_RELAY = 'HOP_CANT_SPEAK_RELAY', - HOP_CANT_RELAY_TO_SELF = 'HOP_CANT_RELAY_TO_SELF', - STOP_SRC_ADDR_TOO_LONG = 'STOP_SRC_ADDR_TOO_LONG', - STOP_DST_ADDR_TOO_LONG = 'STOP_DST_ADDR_TOO_LONG', - STOP_SRC_MULTIADDR_INVALID = 'STOP_SRC_MULTIADDR_INVALID', - STOP_DST_MULTIADDR_INVALID = 'STOP_DST_MULTIADDR_INVALID', - STOP_RELAY_REFUSED = 'STOP_RELAY_REFUSED', - MALFORMED_MESSAGE = 'MALFORMED_MESSAGE' - } - - enum __StatusValues { - SUCCESS = 100, - HOP_SRC_ADDR_TOO_LONG = 220, - HOP_DST_ADDR_TOO_LONG = 221, - HOP_SRC_MULTIADDR_INVALID = 250, - HOP_DST_MULTIADDR_INVALID = 251, - HOP_NO_CONN_TO_DST = 260, - HOP_CANT_DIAL_DST = 261, - HOP_CANT_OPEN_DST_STREAM = 262, - HOP_CANT_SPEAK_RELAY = 270, - HOP_CANT_RELAY_TO_SELF = 280, - STOP_SRC_ADDR_TOO_LONG = 320, - STOP_DST_ADDR_TOO_LONG = 321, - STOP_SRC_MULTIADDR_INVALID = 350, - STOP_DST_MULTIADDR_INVALID = 351, - STOP_RELAY_REFUSED = 390, - MALFORMED_MESSAGE = 400 - } - - export namespace Status { - export const codec = () => { - return enumeration(__StatusValues) - } - } - +export namespace HopMessage { export enum Type { - HOP = 'HOP', - STOP = 'STOP', - STATUS = 'STATUS', - CAN_HOP = 'CAN_HOP' + RESERVE = 'RESERVE', + CONNECT = 'CONNECT', + STATUS = 'STATUS' } enum __TypeValues { - HOP = 1, - STOP = 2, - STATUS = 3, - CAN_HOP = 4 + RESERVE = 0, + CONNECT = 1, + STATUS = 2 } export namespace Type { - export const codec = () => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - export interface Peer { - id: Uint8Array - addrs: Uint8Array[] - } + let _codec: Codec - export namespace Peer { - let _codec: Codec + export const codec = (): Codec => { + if (_codec == null) { + _codec = message((obj, w, opts = {}) => { + if (opts.lengthDelimited !== false) { + w.fork() + } - export const codec = (): Codec => { - if (_codec == null) { - _codec = message((obj, w, opts = {}) => { - if (opts.lengthDelimited !== false) { - w.fork() - } + if (obj.type != null) { + w.uint32(8) + HopMessage.Type.codec().encode(obj.type, w) + } - if (opts.writeDefaults === true || (obj.id != null && obj.id.byteLength > 0)) { - w.uint32(10) - w.bytes(obj.id) - } + if (obj.peer != null) { + w.uint32(18) + Peer.codec().encode(obj.peer, w, { + writeDefaults: false + }) + } - if (obj.addrs != null) { - for (const value of obj.addrs) { - w.uint32(18) - w.bytes(value) - } - } + if (obj.reservation != null) { + w.uint32(26) + Reservation.codec().encode(obj.reservation, w, { + writeDefaults: false + }) + } - if (opts.lengthDelimited !== false) { - w.ldelim() - } - }, (reader, length) => { - const obj: any = { - id: new Uint8Array(0), - addrs: [] - } + if (obj.limit != null) { + w.uint32(34) + Limit.codec().encode(obj.limit, w, { + writeDefaults: false + }) + } - const end = length == null ? reader.len : reader.pos + length - - while (reader.pos < end) { - const tag = reader.uint32() - - switch (tag >>> 3) { - case 1: - obj.id = reader.bytes() - break - case 2: - obj.addrs.push(reader.bytes()) - break - default: - reader.skipType(tag & 7) - break - } - } + if (obj.status != null) { + w.uint32(40) + Status.codec().encode(obj.status, w) + } - return obj - }) - } + if (opts.lengthDelimited !== false) { + w.ldelim() + } + }, (reader, length) => { + const obj: any = {} - return _codec - } + const end = length == null ? reader.len : reader.pos + length - export const encode = (obj: Peer): Uint8Array => { - return encodeMessage(obj, Peer.codec()) + while (reader.pos < end) { + const tag = reader.uint32() + + switch (tag >>> 3) { + case 1: + obj.type = HopMessage.Type.codec().decode(reader) + break + case 2: + obj.peer = Peer.codec().decode(reader, reader.uint32()) + break + case 3: + obj.reservation = Reservation.codec().decode(reader, reader.uint32()) + break + case 4: + obj.limit = Limit.codec().decode(reader, reader.uint32()) + break + case 5: + obj.status = Status.codec().decode(reader) + break + default: + reader.skipType(tag & 7) + break + } + } + + return obj + }) } - export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => { - return decodeMessage(buf, Peer.codec()) + return _codec + } + + export const encode = (obj: HopMessage): Uint8Array => { + return encodeMessage(obj, HopMessage.codec()) + } + + export const decode = (buf: Uint8Array | Uint8ArrayList): HopMessage => { + return decodeMessage(buf, HopMessage.codec()) + } +} + +export interface StopMessage { + type?: StopMessage.Type + peer?: Peer + limit?: Limit + status?: Status +} + +export namespace StopMessage { + export enum Type { + CONNECT = 'CONNECT', + STATUS = 'STATUS' + } + + enum __TypeValues { + CONNECT = 0, + STATUS = 1 + } + + export namespace Type { + export const codec = (): Codec => { + return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } if (obj.type != null) { w.uint32(8) - CircuitRelay.Type.codec().encode(obj.type, w) + StopMessage.Type.codec().encode(obj.type, w) } - if (obj.srcPeer != null) { + if (obj.peer != null) { w.uint32(18) - CircuitRelay.Peer.codec().encode(obj.srcPeer, w, { + Peer.codec().encode(obj.peer, w, { writeDefaults: false }) } - if (obj.dstPeer != null) { + if (obj.limit != null) { w.uint32(26) - CircuitRelay.Peer.codec().encode(obj.dstPeer, w, { + Limit.codec().encode(obj.limit, w, { writeDefaults: false }) } - if (obj.code != null) { + if (obj.status != null) { w.uint32(32) - CircuitRelay.Status.codec().encode(obj.code, w) + Status.codec().encode(obj.status, w) } if (opts.lengthDelimited !== false) { @@ -195,16 +194,337 @@ export namespace CircuitRelay { switch (tag >>> 3) { case 1: - obj.type = CircuitRelay.Type.codec().decode(reader) + obj.type = StopMessage.Type.codec().decode(reader) break case 2: - obj.srcPeer = CircuitRelay.Peer.codec().decode(reader, reader.uint32()) + obj.peer = Peer.codec().decode(reader, reader.uint32()) break case 3: - obj.dstPeer = CircuitRelay.Peer.codec().decode(reader, reader.uint32()) + obj.limit = Limit.codec().decode(reader, reader.uint32()) break case 4: - obj.code = CircuitRelay.Status.codec().decode(reader) + obj.status = Status.codec().decode(reader) + break + default: + reader.skipType(tag & 7) + break + } + } + + return obj + }) + } + + return _codec + } + + export const encode = (obj: StopMessage): Uint8Array => { + return encodeMessage(obj, StopMessage.codec()) + } + + export const decode = (buf: Uint8Array | Uint8ArrayList): StopMessage => { + return decodeMessage(buf, StopMessage.codec()) + } +} + +export interface Peer { + id: Uint8Array + addrs: Uint8Array[] +} + +export namespace Peer { + let _codec: Codec + + export const codec = (): Codec => { + if (_codec == null) { + _codec = message((obj, w, opts = {}) => { + if (opts.lengthDelimited !== false) { + w.fork() + } + + if (opts.writeDefaults === true || (obj.id != null && obj.id.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.id) + } + + if (obj.addrs != null) { + for (const value of obj.addrs) { + w.uint32(18) + w.bytes(value) + } + } + + if (opts.lengthDelimited !== false) { + w.ldelim() + } + }, (reader, length) => { + const obj: any = { + id: new Uint8Array(0), + addrs: [] + } + + const end = length == null ? reader.len : reader.pos + length + + while (reader.pos < end) { + const tag = reader.uint32() + + switch (tag >>> 3) { + case 1: + obj.id = reader.bytes() + break + case 2: + obj.addrs.push(reader.bytes()) + break + default: + reader.skipType(tag & 7) + break + } + } + + return obj + }) + } + + return _codec + } + + export const encode = (obj: Peer): Uint8Array => { + return encodeMessage(obj, Peer.codec()) + } + + export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => { + return decodeMessage(buf, Peer.codec()) + } +} + +export interface Reservation { + expire: bigint + addrs: Uint8Array[] + voucher?: Uint8Array +} + +export namespace Reservation { + let _codec: Codec + + export const codec = (): Codec => { + if (_codec == null) { + _codec = message((obj, w, opts = {}) => { + if (opts.lengthDelimited !== false) { + w.fork() + } + + if (opts.writeDefaults === true || obj.expire !== 0n) { + w.uint32(8) + w.uint64(obj.expire) + } + + if (obj.addrs != null) { + for (const value of obj.addrs) { + w.uint32(18) + w.bytes(value) + } + } + + if (obj.voucher != null) { + w.uint32(26) + w.bytes(obj.voucher) + } + + if (opts.lengthDelimited !== false) { + w.ldelim() + } + }, (reader, length) => { + const obj: any = { + expire: 0n, + addrs: [] + } + + const end = length == null ? reader.len : reader.pos + length + + while (reader.pos < end) { + const tag = reader.uint32() + + switch (tag >>> 3) { + case 1: + obj.expire = reader.uint64() + break + case 2: + obj.addrs.push(reader.bytes()) + break + case 3: + obj.voucher = reader.bytes() + break + default: + reader.skipType(tag & 7) + break + } + } + + return obj + }) + } + + return _codec + } + + export const encode = (obj: Reservation): Uint8Array => { + return encodeMessage(obj, Reservation.codec()) + } + + export const decode = (buf: Uint8Array | Uint8ArrayList): Reservation => { + return decodeMessage(buf, Reservation.codec()) + } +} + +export interface Limit { + duration?: number + data?: bigint +} + +export namespace Limit { + let _codec: Codec + + export const codec = (): Codec => { + if (_codec == null) { + _codec = message((obj, w, opts = {}) => { + if (opts.lengthDelimited !== false) { + w.fork() + } + + if (obj.duration != null) { + w.uint32(8) + w.uint32(obj.duration) + } + + if (obj.data != null) { + w.uint32(16) + w.uint64(obj.data) + } + + if (opts.lengthDelimited !== false) { + w.ldelim() + } + }, (reader, length) => { + const obj: any = {} + + const end = length == null ? reader.len : reader.pos + length + + while (reader.pos < end) { + const tag = reader.uint32() + + switch (tag >>> 3) { + case 1: + obj.duration = reader.uint32() + break + case 2: + obj.data = reader.uint64() + break + default: + reader.skipType(tag & 7) + break + } + } + + return obj + }) + } + + return _codec + } + + export const encode = (obj: Limit): Uint8Array => { + return encodeMessage(obj, Limit.codec()) + } + + export const decode = (buf: Uint8Array | Uint8ArrayList): Limit => { + return decodeMessage(buf, Limit.codec()) + } +} + +export enum Status { + UNUSED = 'UNUSED', + OK = 'OK', + RESERVATION_REFUSED = 'RESERVATION_REFUSED', + RESOURCE_LIMIT_EXCEEDED = 'RESOURCE_LIMIT_EXCEEDED', + PERMISSION_DENIED = 'PERMISSION_DENIED', + CONNECTION_FAILED = 'CONNECTION_FAILED', + NO_RESERVATION = 'NO_RESERVATION', + MALFORMED_MESSAGE = 'MALFORMED_MESSAGE', + UNEXPECTED_MESSAGE = 'UNEXPECTED_MESSAGE' +} + +enum __StatusValues { + UNUSED = 0, + OK = 100, + RESERVATION_REFUSED = 200, + RESOURCE_LIMIT_EXCEEDED = 201, + PERMISSION_DENIED = 202, + CONNECTION_FAILED = 203, + NO_RESERVATION = 204, + MALFORMED_MESSAGE = 400, + UNEXPECTED_MESSAGE = 401 +} + +export namespace Status { + export const codec = (): Codec => { + return enumeration(__StatusValues) + } +} +export interface ReservationVoucher { + relay: Uint8Array + peer: Uint8Array + expiration: bigint +} + +export namespace ReservationVoucher { + let _codec: Codec + + export const codec = (): Codec => { + if (_codec == null) { + _codec = message((obj, w, opts = {}) => { + if (opts.lengthDelimited !== false) { + w.fork() + } + + if (opts.writeDefaults === true || (obj.relay != null && obj.relay.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.relay) + } + + if (opts.writeDefaults === true || (obj.peer != null && obj.peer.byteLength > 0)) { + w.uint32(18) + w.bytes(obj.peer) + } + + if (opts.writeDefaults === true || obj.expiration !== 0n) { + w.uint32(24) + w.uint64(obj.expiration) + } + + if (opts.lengthDelimited !== false) { + w.ldelim() + } + }, (reader, length) => { + const obj: any = { + relay: new Uint8Array(0), + peer: new Uint8Array(0), + expiration: 0n + } + + const end = length == null ? reader.len : reader.pos + length + + while (reader.pos < end) { + const tag = reader.uint32() + + switch (tag >>> 3) { + case 1: + obj.relay = reader.bytes() + break + case 2: + obj.peer = reader.bytes() + break + case 3: + obj.expiration = reader.uint64() break default: reader.skipType(tag & 7) @@ -219,11 +539,11 @@ export namespace CircuitRelay { return _codec } - export const encode = (obj: CircuitRelay): Uint8Array => { - return encodeMessage(obj, CircuitRelay.codec()) + export const encode = (obj: ReservationVoucher): Uint8Array => { + return encodeMessage(obj, ReservationVoucher.codec()) } - export const decode = (buf: Uint8Array | Uint8ArrayList): CircuitRelay => { - return decodeMessage(buf, CircuitRelay.codec()) + export const decode = (buf: Uint8Array | Uint8ArrayList): ReservationVoucher => { + return decodeMessage(buf, ReservationVoucher.codec()) } } diff --git a/src/circuit/relay.ts b/src/circuit/relay.ts new file mode 100644 index 0000000000..6ec8e3e015 --- /dev/null +++ b/src/circuit/relay.ts @@ -0,0 +1,87 @@ +import { logger } from '@libp2p/logger' +import { codes } from '../errors.js' +import { + setDelayedInterval, + clearDelayedInterval + // @ts-expect-error set-delayed-interval does not export types +} from 'set-delayed-interval' +import { namespaceToCid } from './utils.js' +import { + RELAY_RENDEZVOUS_NS +} from './constants.js' +import type { AddressSorter } from '@libp2p/interface-peer-store' +import type { Startable } from '@libp2p/interfaces/startable' +import type { Components } from '../components.js' +import type { HopConfig, RelayAdvertiseConfig } from './index.js' + +const log = logger('libp2p:circuit:relay') + +export interface RelayInit { + addressSorter?: AddressSorter + maxListeners?: number + hop: HopConfig + advertise: RelayAdvertiseConfig +} + +export class Relay implements Startable { + private readonly components: Components + private readonly init: RelayInit + private timeout?: any + private started: boolean + + /** + * Creates an instance of Relay + */ + constructor (components: Components, init: RelayInit) { + this.components = components + this.started = false + this.init = init + this._advertiseService = this._advertiseService.bind(this) + } + + isStarted () { + return this.started + } + + /** + * Start Relay service + */ + async start () { + // Advertise service if HOP enabled and advertising enabled + if (this.init.hop.enabled === true && this.init.advertise.enabled === true) { + this.timeout = setDelayedInterval( + this._advertiseService, this.init.advertise.ttl, this.init.advertise.bootDelay + ) + } + + this.started = true + } + + /** + * Stop Relay service + */ + async stop () { + try { + clearDelayedInterval(this.timeout) + } catch (err) { } + + this.started = false + } + + /** + * Advertise hop relay service in the network. + */ + async _advertiseService () { + try { + const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) + await this.components.contentRouting.provide(cid) + } catch (err: any) { + if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) { + log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) + await this.stop() + } else { + log.error('could not advertise service: ', err) + } + } + } +} diff --git a/src/circuit/reservation-store.ts b/src/circuit/reservation-store.ts new file mode 100644 index 0000000000..b48cbff8b9 --- /dev/null +++ b/src/circuit/reservation-store.ts @@ -0,0 +1,106 @@ +import { Limit, Status } from './pb/index.js' +import type { ReservationStore as IReservationStore, ReservationStatus, Reservation } from './interfaces.js' +import type { Multiaddr } from '@multiformats/multiaddr' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Startable } from '@libp2p/interfaces/startable' +import { PeerMap } from '@libp2p/peer-collections' +import type { RecursivePartial } from '@libp2p/interfaces' +import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from './constants.js' + +export interface ReservationStoreInit { + /* + * maximum number of reservations allowed, default: 15 + */ + maxReservations: number + /* + * interval after which stale reservations are cleared, default: 300s + */ + reservationClearInterval: number + /* + * apply default relay limits to a new reservation, default: true + */ + applyDefaultLimit: boolean + /** + * reservation ttl, default: 2 hours + */ + reservationTtl: number + /** + * The maximum time a relayed connection can be open for + */ + defaultDurationLimit?: number + /** + * The maximum amount of data allowed to be transferred over a relayed connection + */ + defaultDataLimit?: bigint +} + +export type ReservationStoreOptions = RecursivePartial + +export class ReservationStore implements IReservationStore, Startable { + private readonly reservations = new PeerMap() + private _started = false; + private interval: any + private readonly init: ReservationStoreInit + + constructor (options?: ReservationStoreOptions) { + this.init = { + maxReservations: options?.maxReservations ?? 15, + reservationClearInterval: options?.reservationClearInterval ?? 300 * 1000, + applyDefaultLimit: options?.applyDefaultLimit !== false, + reservationTtl: options?.reservationTtl ?? 2 * 60 * 60 * 1000, + defaultDurationLimit: options?.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT, + defaultDataLimit: options?.defaultDataLimit ?? DEFAULT_DATA_LIMIT + } + } + + isStarted () { + return this._started + } + + start () { + if (this._started) { + return + } + this._started = true + this.interval = setInterval( + () => { + const now = (new Date()).getTime() + this.reservations.forEach((r, k) => { + if (r.expire.getTime() < now) { + this.reservations.delete(k) + } + }) + }, + this.init.reservationClearInterval + ) + } + + stop () { + clearInterval(this.interval) + } + + reserve (peer: PeerId, addr: Multiaddr, limit?: Limit): { status: ReservationStatus, expire?: number } { + if (this.reservations.size >= this.init.maxReservations && !this.reservations.has(peer)) { + return { status: Status.RESERVATION_REFUSED } + } + const expire = new Date(Date.now() + this.init.reservationTtl) + let checkedLimit: Limit | undefined + if (this.init.applyDefaultLimit) { + checkedLimit = limit ?? { data: this.init.defaultDataLimit, duration: this.init.defaultDurationLimit } + } + this.reservations.set(peer, { addr, expire, limit: checkedLimit }) + return { status: Status.OK, expire: expire.getTime() } + } + + removeReservation (peer: PeerId): void { + this.reservations.delete(peer) + } + + hasReservation (dst: PeerId): boolean { + return this.reservations.has(dst) + } + + get (peer: PeerId): Reservation | undefined { + return this.reservations.get(peer) + } +} diff --git a/src/circuit/reservation-voucher.ts b/src/circuit/reservation-voucher.ts new file mode 100644 index 0000000000..9979ae0692 --- /dev/null +++ b/src/circuit/reservation-voucher.ts @@ -0,0 +1,51 @@ +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Record } from '@libp2p/interface-record' +import { ReservationVoucher } from './pb/index.js' + +export interface ReservationVoucherOptions { + relay: PeerId + peer: PeerId + expiration: number +} + +export class ReservationVoucherRecord implements Record { + public readonly domain = 'libp2p-relay-rsvp' + public readonly codec = new Uint8Array([0x03, 0x02]) + + private readonly relay: PeerId + private readonly peer: PeerId + private readonly expiration: number + + constructor ({ relay, peer, expiration }: ReservationVoucherOptions) { + this.relay = relay + this.peer = peer + this.expiration = expiration + } + + marshal () { + return ReservationVoucher.encode({ + relay: this.relay.toBytes(), + peer: this.peer.toBytes(), + expiration: BigInt(this.expiration) + }) + } + + equals (other: Record) { + if (!(other instanceof ReservationVoucherRecord)) { + return false + } + if (!this.peer.equals(other.peer)) { + return false + } + + if (!this.relay.equals(other.relay)) { + return false + } + + if (this.expiration !== other.expiration) { + return false + } + + return true + } +} diff --git a/src/circuit/stop.ts b/src/circuit/stop.ts new file mode 100644 index 0000000000..e650fe6683 --- /dev/null +++ b/src/circuit/stop.ts @@ -0,0 +1,92 @@ + +import { Status, StopMessage } from './pb/index.js' +import type { Connection, Stream } from '@libp2p/interface-connection' + +import { logger } from '@libp2p/logger' +import { RELAY_V2_STOP_CODEC } from './multicodec.js' +import { multiaddr } from '@multiformats/multiaddr' +import { pbStream, ProtobufStream } from 'it-pb-stream' + +const log = logger('libp2p:circuit:v2:stop') + +export interface HandleStopOptions { + connection: Connection + request: StopMessage + pbstr: ProtobufStream +} + +const isValidStop = (request: StopMessage): boolean => { + if (request.peer == null) { + return false + } + try { + request.peer.addrs.forEach(multiaddr) + } catch (_err) { + return false + } + return true +} +export async function handleStop ({ + connection, + request, + pbstr +}: HandleStopOptions) { + const stopstr = pbstr.pb(StopMessage) + log('new circuit relay v2 stop stream from %s', connection.remotePeer) + // Validate the STOP request has the required input + if (request.type !== StopMessage.Type.CONNECT) { + log.error('invalid stop connect request via peer %s', connection.remotePeer) + stopstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }) + return + } + if (!isValidStop(request)) { + log.error('invalid stop connect request via peer %s', connection.remotePeer) + stopstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }) + return + } + + // TODO: go-libp2p marks connection transient if there is limit field present in request. + // Cannot find any reference to transient connections in js-libp2p + + stopstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }) + return pbstr.unwrap() +} + +export interface StopOptions { + connection: Connection + request: StopMessage +} + +/** + * Creates a STOP request + * + */ +export async function stop ({ + connection, + request +}: StopOptions): Promise { + const stream = await connection.newStream([RELAY_V2_STOP_CODEC]) + log('starting circuit relay v2 stop request to %s', connection.remotePeer) + const pbstr = pbStream(stream) + const stopstr = pbstr.pb(StopMessage) + stopstr.write(request) + let response + try { + response = await stopstr.read() + } catch (err) { + log.error('error parsing stop message response from %s', connection.remotePeer) + } + + if (response == null) { + log.error('could not read response from %s', connection.remotePeer) + stream.close() + return + } + if (response.status === Status.OK) { + log('stop request to %s was successful', connection.remotePeer) + return pbstr.unwrap() + } + + log('stop request failed with code %d', response.status) + stream.close() +} diff --git a/src/circuit/transport.ts b/src/circuit/transport.ts index ebb793ca18..f1957dd855 100644 --- a/src/circuit/transport.ts +++ b/src/circuit/transport.ts @@ -1,36 +1,37 @@ +import * as CircuitV2 from './pb/index.js' +import { ReservationStore } from './reservation-store.js' import { logger } from '@libp2p/logger' -import errCode from 'err-code' +import createError from 'err-code' import * as mafmt from '@multiformats/mafmt' -import type { Multiaddr } from '@multiformats/multiaddr' import { multiaddr } from '@multiformats/multiaddr' -import { CircuitRelay as CircuitPB } from './pb/index.js' import { codes } from '../errors.js' import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn' -import { RELAY_CODEC } from './multicodec.js' +import { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './multicodec.js' import { createListener } from './listener.js' -import { handleCanHop, handleHop, hop } from './circuit/hop.js' -import { handleStop } from './circuit/stop.js' -import { StreamHandler } from './circuit/stream-handler.js' -import { symbol, Upgrader } from '@libp2p/interface-transport' +import { symbol, TransportManager, Upgrader } from '@libp2p/interface-transport' import { peerIdFromString } from '@libp2p/peer-id' import type { AbortOptions } from '@libp2p/interfaces' import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar' import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interface-transport' -import type { Connection } from '@libp2p/interface-connection' +import type { Connection, Stream } from '@libp2p/interface-connection' import type { RelayConfig } from './index.js' -import { abortableDuplex } from 'abortable-iterator' -import { TimeoutController } from 'timeout-abort-controller' -import { setMaxListeners } from 'events' -import type { Uint8ArrayList } from 'uint8arraylist' -import type { Duplex } from 'it-stream-types' -import type { Startable } from '@libp2p/interfaces/startable' -import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { PeerId } from '@libp2p/interface-peer-id' +import { handleHopProtocol } from './hop.js' +import { handleStop } from './stop.js' +import type { Multiaddr } from '@multiformats/multiaddr' import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Startable } from '@libp2p/interfaces/dist/src/startable' +import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { AddressManager } from '@libp2p/interface-address-manager' +import { pbStream } from 'it-pb-stream' +import pDefer from 'p-defer' const log = logger('libp2p:circuit') +export interface CircuitOptions { + limit?: number +} + export interface CircuitComponents { peerId: PeerId peerStore: PeerStore @@ -38,17 +39,33 @@ export interface CircuitComponents { connectionManager: ConnectionManager upgrader: Upgrader addressManager: AddressManager + transportManager: TransportManager +} + +interface ConnectOptions { + stream: Stream + connection: Connection + destinationPeer: PeerId + destinationAddr: Multiaddr + relayAddr: Multiaddr + ma: Multiaddr + disconnectOnFailure: boolean } export class Circuit implements Transport, Startable { private handler?: ConnectionHandler private readonly components: CircuitComponents + private readonly reservationStore: ReservationStore private readonly _init: RelayConfig private _started: boolean - constructor (components: CircuitComponents, init: RelayConfig) { - this._init = init + constructor (components: CircuitComponents, options: RelayConfig) { this.components = components + this._init = options + this.reservationStore = new ReservationStore({ + defaultDataLimit: options.hop?.limit?.data, + defaultDurationLimit: options.hop?.limit?.duration + }) this._started = false } @@ -63,26 +80,38 @@ export class Circuit implements Transport, Startable { this._started = true - await this.components.registrar.handle(RELAY_CODEC, (data) => { - void this._onProtocol(data).catch(err => { + // only handle hop if enabled + if (this._init.hop.enabled === true) { + void this.components.registrar.handle(RELAY_V2_HOP_CODEC, (data) => { + void this.onHop(data).catch(err => { + log.error(err) + }) + }) + .catch(err => { + log.error(err) + }) + } + + void this.components.registrar.handle(RELAY_V2_STOP_CODEC, (data) => { + void this.onStop(data).catch(err => { log.error(err) }) - }, { ...this._init }) + }) .catch(err => { log.error(err) }) - } - async stop () { - await this.components.registrar.unhandle(RELAY_CODEC) - } - - hopEnabled () { - return true + if (this._init.hop.enabled === true) { + void this.reservationStore.start() + } } - hopActive () { - return true + async stop () { + if (this._init.hop.enabled === true) { + this.reservationStore.stop() + await this.components.registrar.unhandle(RELAY_V2_HOP_CODEC) + } + await this.components.registrar.unhandle(RELAY_V2_STOP_CODEC) } get [symbol] (): true { @@ -90,99 +119,78 @@ export class Circuit implements Transport, Startable { } get [Symbol.toStringTag] () { - return 'libp2p/circuit-relay-v1' + return 'libp2p/circuit-relay-v2' } - async _onProtocol (data: IncomingStreamData) { - const { connection, stream } = data - const controller = new TimeoutController(this._init.hop.timeout) + async onHop ({ connection, stream }: IncomingStreamData) { + log('received circuit v2 hop protocol stream from %s', connection.remotePeer) - try { - // fails on node < 15.4 - setMaxListeners?.(Infinity, controller.signal) - } catch {} + const hopTimeoutPromise = pDefer() + const timeout = setTimeout(() => { + hopTimeoutPromise.reject('timed out') + }, this._init.hop.timeout) + const pbstr = pbStream(stream) try { - const source = abortableDuplex(stream, controller.signal) - const streamHandler = new StreamHandler({ - stream: { - ...stream, - ...source - } - }) - const request = await streamHandler.read() + const request: CircuitV2.HopMessage = await Promise.race([ + pbstr.pb(CircuitV2.HopMessage).read(), + hopTimeoutPromise.promise as any + ]) - if (request == null) { - log('request was invalid, could not read from stream') - streamHandler.write({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.MALFORMED_MESSAGE - }) - streamHandler.close() - return + if (request?.type == null) { + throw new Error('request was invalid, could not read from stream') } - let virtualConnection: Duplex | undefined - - switch (request.type) { - case CircuitPB.Type.CAN_HOP: { - log('received CAN_HOP request from %p', connection.remotePeer) - await handleCanHop({ circuit: this, connection, streamHandler }) - break - } - case CircuitPB.Type.HOP: { - log('received HOP request from %p', connection.remotePeer) - await handleHop({ - connection, - request, - streamHandler, - circuit: this, - connectionManager: this.components.connectionManager - }) - break - } - case CircuitPB.Type.STOP: { - log('received STOP request from %p', connection.remotePeer) - virtualConnection = await handleStop({ - connection, - request, - streamHandler - }) - break - } - default: { - log('Request of type %s not supported', request.type) - streamHandler.write({ - type: CircuitPB.Type.STATUS, - code: CircuitPB.Status.MALFORMED_MESSAGE - }) - streamHandler.close() - return - } - } + await Promise.race([ + handleHopProtocol({ + connection, + stream: pbstr, + connectionManager: this.components.connectionManager, + relayPeer: this.components.peerId, + relayAddrs: this.components.addressManager.getListenAddrs(), + reservationStore: this.reservationStore, + peerStore: this.components.peerStore, + request + }), + hopTimeoutPromise.promise + ]) + } catch (_err) { + pbstr.pb(CircuitV2.HopMessage).write({ + type: CircuitV2.HopMessage.Type.STATUS, + status: CircuitV2.Status.MALFORMED_MESSAGE + }) + stream.abort(_err as Error) + } finally { + clearTimeout(timeout) + } + } - if (virtualConnection != null) { - const remoteAddr = connection.remoteAddr - .encapsulate('/p2p-circuit') - .encapsulate(multiaddr(request.dstPeer?.addrs[0])) - const localAddr = multiaddr(request.srcPeer?.addrs[0]) - const maConn = streamToMaConnection({ - stream: virtualConnection, - remoteAddr, - localAddr - }) - const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound' - log('new %s connection %s', type, maConn.remoteAddr) + async onStop ({ connection, stream }: IncomingStreamData) { + const pbstr = pbStream(stream) + const request = await pbstr.readPB(CircuitV2.StopMessage) + log('received circuit v2 stop protocol request from %s', connection.remotePeer) + if (request?.type === undefined) { + return + } - const conn = await this.components.upgrader.upgradeInbound(maConn) - log('%s connection %s upgraded', type, maConn.remoteAddr) + const mStream = await handleStop({ + connection, + pbstr, + request + }) - if (this.handler != null) { - this.handler(conn) - } - } - } finally { - controller.clear() + if (mStream != null) { + const remoteAddr = multiaddr(request.peer?.addrs?.[0]) + const localAddr = this.components.transportManager.getAddrs()[0] + const maConn = streamToMaConnection({ + stream: mStream as any, + remoteAddr, + localAddr + }) + log('new inbound connection %s', maConn.remoteAddr) + const conn = await this.components.upgrader.upgradeInbound(maConn) + log('%s connection %s upgraded', 'inbound', maConn.remoteAddr) + this.handler?.(conn) } } @@ -200,7 +208,7 @@ export class Circuit implements Transport, Startable { if (relayId == null || destinationId == null) { const errMsg = 'Circuit relay dial failed as addresses did not have peer id' log.error(errMsg) - throw errCode(new Error(errMsg), codes.ERR_RELAYED_DIAL) + throw createError(new Error(errMsg), codes.ERR_RELAYED_DIAL) } const relayPeer = peerIdFromString(relayId) @@ -217,34 +225,61 @@ export class Circuit implements Transport, Startable { } try { - const virtualConnection = await hop({ - ...options, + const stream = await relayConnection.newStream([RELAY_V2_HOP_CODEC]) + return await this.connectV2({ + stream, connection: relayConnection, - request: { - type: CircuitPB.Type.HOP, - srcPeer: { - id: this.components.peerId.toBytes(), - addrs: this.components.addressManager.getAddresses().map(addr => addr.bytes) - }, - dstPeer: { - id: destinationPeer.toBytes(), - addrs: [multiaddr(destinationAddr).bytes] - } + destinationPeer, + destinationAddr, + relayAddr, + ma, + disconnectOnFailure + }) + } catch (err: any) { + log.error('Circuit relay dial failed', err) + disconnectOnFailure && await relayConnection.close() + throw err + } + } + + async connectV2 ( + { + stream, connection, destinationPeer, + destinationAddr, relayAddr, ma, + disconnectOnFailure + }: ConnectOptions + ) { + try { + const pbstr = pbStream(stream) + const hopstr = pbstr.pb(CircuitV2.HopMessage) + hopstr.write({ + type: CircuitV2.HopMessage.Type.CONNECT, + peer: { + id: destinationPeer.toBytes(), + addrs: [multiaddr(destinationAddr).bytes] } }) - const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.components.peerId.toString()}`) + const status = await hopstr.read() + if (status.status !== CircuitV2.Status.OK) { + throw createError(new Error(`failed to connect via relay with status ${status?.status?.toString() ?? 'undefined'}`), codes.ERR_HOP_REQUEST_FAILED) + } + + // TODO: do something with limit and transient connection + + let localAddr = relayAddr + localAddr = localAddr.encapsulate(`/p2p-circuit/p2p/${this.components.peerId.toString()}`) const maConn = streamToMaConnection({ - stream: virtualConnection, + stream: pbstr.unwrap(), remoteAddr: ma, localAddr }) log('new outbound connection %s', maConn.remoteAddr) - - return await this.components.upgrader.upgradeOutbound(maConn) - } catch (err: any) { + const conn = await this.components.upgrader.upgradeOutbound(maConn) + return conn + } catch (err) { log.error('Circuit relay dial failed', err) - disconnectOnFailure && await relayConnection.close() + disconnectOnFailure && await connection.close() throw err } } diff --git a/src/circuit/utils.ts b/src/circuit/utils.ts index eb3bcd6fa8..bad7597ba4 100644 --- a/src/circuit/utils.ts +++ b/src/circuit/utils.ts @@ -1,5 +1,145 @@ import { CID } from 'multiformats/cid' import { sha256 } from 'multiformats/hashes/sha2' +import type { Source } from 'it-stream-types' +import type { Uint8ArrayList } from 'uint8arraylist' +import type { Limit } from './pb/index.js' +import { logger } from '@libp2p/logger' +import type { Stream } from '@libp2p/interface-connection' + +const log = logger('libp2p:circuit:v2:util') + +const doRelay = (src: Stream, dst: Stream) => { + queueMicrotask(() => { + void dst.sink(src.source).catch(err => log.error('error while relating streams:', err)) + }) + + queueMicrotask(() => { + void src.sink(dst.source).catch(err => log.error('error while relaying streams:', err)) + }) +} + +export function createLimitedRelay (source: Stream, destination: Stream, limit?: Limit) { + // trivial case + if (limit == null) { + doRelay(source, destination) + return + } + + const dataLimit = limit.data ?? 0n + const durationLimit = limit.duration ?? 0 + const src = durationLimitDuplex(dataLimitDuplex(source, dataLimit), durationLimit) + const dst = durationLimitDuplex(dataLimitDuplex(destination, dataLimit), durationLimit) + + doRelay(src, dst) +} + +const dataLimitSource = (stream: Stream, limit: bigint): Stream => { + if (limit === 0n) { + return stream + } + + const source = stream.source + + stream.source = (async function * (): Source { + let total = 0n + + for await (const buf of source) { + const len = BigInt(buf.byteLength) + if (total + len > limit) { + // this is a safe downcast since len is guarantee to be in the range for a number + const remaining = Number(limit - total) + try { + if (remaining !== 0) { + yield buf + } + } finally { + stream.abort(new Error('data limit exceeded')) + } + return + } + + yield buf + + total += len + } + })() + + return stream +} + +const dataLimitSink = (stream: Stream, limit: bigint): Stream => { + if (limit === 0n) { + return stream + } + + const sink = stream.sink + + stream.sink = async (source: Source) => { + await sink((async function * (): Source { + let total = 0n + + for await (const buf of source) { + const len = BigInt(buf.byteLength) + if (total + len > limit) { + // this is a safe downcast since len is guarantee to be in the range for a number + const remaining = Number(limit - total) + try { + if (remaining !== 0) { + yield buf.subarray(0, remaining) + } + } finally { + stream.abort(new Error('data limit exceeded')) + } + return + } + + total += len + yield buf + } + })()) + } + + return stream +} + +const dataLimitDuplex = (stream: Stream, limit: bigint): Stream => { + dataLimitSource(stream, limit) + dataLimitSink(stream, limit) + + return stream +} + +const durationLimitDuplex = (stream: Stream, limit: number): Stream => { + if (limit === 0) { + return stream + } + + let timedOut = false + const timeout = setTimeout( + () => { + timedOut = true + stream.abort(new Error('exceeded connection duration limit')) + }, + limit + ) + + const source = stream.source + + stream.source = (async function * (): Source { + try { + for await (const buf of source) { + if (timedOut) { + return + } + yield buf + } + } finally { + clearTimeout(timeout) + } + })() + + return stream +} /** * Convert a namespace string into a cid @@ -10,3 +150,8 @@ export async function namespaceToCid (namespace: string): Promise { return CID.createV0(hash) } + +/** returns number of ms beween now and expiration time */ +export function getExpiration (expireTime: bigint): number { + return Number(expireTime) - new Date().getTime() +} diff --git a/src/config.ts b/src/config.ts index e856061c99..925b2f105f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -58,12 +58,11 @@ const DefaultConfig: Partial = { }, hop: { enabled: false, - active: false, timeout: 30000 }, - autoRelay: { + reservationManager: { enabled: false, - maxListeners: 2 + maxReservations: 2 } }, identify: { diff --git a/src/connection-manager/dialer/dial-request.ts b/src/connection-manager/dialer/dial-request.ts index 18e90de34f..77d1a306a9 100644 --- a/src/connection-manager/dialer/dial-request.ts +++ b/src/connection-manager/dialer/dial-request.ts @@ -124,6 +124,13 @@ export class DialRequest { return conn })) + } catch (err: any) { + // if we only dialed one address, unwrap the AggregateError + if (this.addrs.length === 1 && err.name === 'AggregateError') { + throw err.errors[0] + } + + throw err } finally { // success/failure happened, abort everything else dialAbortControllers.forEach(c => { diff --git a/src/errors.ts b/src/errors.ts index 99ecc45600..7fc428b6d4 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -74,5 +74,6 @@ export enum codes { ERR_NO_HANDLER_FOR_PROTOCOL = 'ERR_NO_HANDLER_FOR_PROTOCOL', ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS', ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS', - ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED' + ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED', + ERR_TRANSFER_LIMIT_EXCEEDED = 'ERR_TRANSFER_LIMIT_EXCEEDED', } diff --git a/src/fetch/pb/proto.ts b/src/fetch/pb/proto.ts index 1f41f6e506..c607724c95 100644 --- a/src/fetch/pb/proto.ts +++ b/src/fetch/pb/proto.ts @@ -2,6 +2,7 @@ /* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ +/* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -84,7 +85,7 @@ export namespace FetchResponse { } export namespace StatusCode { - export const codec = () => { + export const codec = (): Codec => { return enumeration(__StatusCodeValues) } } diff --git a/src/identify/pb/message.ts b/src/identify/pb/message.ts index 2498ac37bf..a78e23255d 100644 --- a/src/identify/pb/message.ts +++ b/src/identify/pb/message.ts @@ -2,6 +2,7 @@ /* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ +/* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' diff --git a/src/insecure/pb/proto.ts b/src/insecure/pb/proto.ts index e800904eb4..6098e23eed 100644 --- a/src/insecure/pb/proto.ts +++ b/src/insecure/pb/proto.ts @@ -2,6 +2,7 @@ /* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ +/* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -89,7 +90,7 @@ enum __KeyTypeValues { } export namespace KeyType { - export const codec = () => { + export const codec = (): Codec => { return enumeration(__KeyTypeValues) } } diff --git a/src/libp2p.ts b/src/libp2p.ts index 96543ca534..2f15c2538e 100644 --- a/src/libp2p.ts +++ b/src/libp2p.ts @@ -1,3 +1,4 @@ +import { RelayReservationManager } from './circuit/client.js' import { logger } from '@libp2p/logger' import type { AbortOptions } from '@libp2p/interfaces' import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' @@ -60,6 +61,7 @@ export class Libp2pNode extends EventEmitter implements Libp2p { public dht: DualDHT public pubsub: PubSub public identifyService: IdentifyService + public circuitService?: RelayReservationManager public fetchService: FetchService public pingService: PingService public components: Components @@ -181,6 +183,14 @@ export class Libp2pNode extends EventEmitter implements Libp2p { }) this.configureComponent(this.identifyService) + if (init.relay.reservationManager.enabled === true) { + this.circuitService = new RelayReservationManager(this.components, { + addressSorter: init.connectionManager.addressSorter, + ...init.relay.reservationManager + }) + this.services.push(this.circuitService) + } + // dht provided components (peerRouting, contentRouting, dht) if (init.dht != null) { this.dht = this.components.dht = init.dht(this.components) @@ -230,7 +240,6 @@ export class Libp2pNode extends EventEmitter implements Libp2p { this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay))) this.configureComponent(new Relay(this.components, { - addressSorter: init.connectionManager.addressSorter, ...init.relay })) } diff --git a/src/upgrader.ts b/src/upgrader.ts index 8589a4e11f..436e1f2d03 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -149,7 +149,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg try { // fails on node < 15.4 setMaxListeners?.(Infinity, timeoutController.signal) - } catch {} + } catch { } try { const abortableStream = abortableDuplex(maConn, timeoutController.signal) @@ -439,7 +439,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg try { // fails on node < 15.4 setMaxListeners?.(Infinity, controller.signal) - } catch {} + } catch { } } const { stream, protocol } = await mss.select(muxedStream, protocols, options) @@ -626,7 +626,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg * Selects one of the given muxers via multistream-select. That * muxer will be used for all future streams on the connection. */ - async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map): Promise<{stream: Duplex, muxerFactory?: StreamMuxerFactory}> { + async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map): Promise<{ stream: Duplex, muxerFactory?: StreamMuxerFactory }> { const protocols = Array.from(muxers.keys()) log('outbound selecting muxer %s', protocols) try { @@ -646,7 +646,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg * Registers support for one of the given muxers via multistream-select. The * selected muxer will be used for all future streams on the connection. */ - async _multiplexInbound (connection: MultiaddrConnection, muxers: Map): Promise<{stream: Duplex, muxerFactory?: StreamMuxerFactory}> { + async _multiplexInbound (connection: MultiaddrConnection, muxers: Map): Promise<{ stream: Duplex, muxerFactory?: StreamMuxerFactory }> { const protocols = Array.from(muxers.keys()) log('inbound handling muxers %s', protocols) try { diff --git a/test/circuit/hop.spec.ts b/test/circuit/hop.spec.ts new file mode 100644 index 0000000000..fd757b948e --- /dev/null +++ b/test/circuit/hop.spec.ts @@ -0,0 +1,788 @@ +import type { Connection, Stream } from '@libp2p/interface-connection' +import { mockConnection, mockDuplex, mockMultiaddrConnection, mockStream } from '@libp2p/interface-mocks' +import type { PeerId } from '@libp2p/interface-peer-id' +import { expect } from 'aegir/chai' +import { pair } from 'it-pair' +import * as sinon from 'sinon' +import { Circuit } from '../../src/circuit/transport.js' +import { handleHopProtocol } from '../../src/circuit/hop.js' +import { HopMessage, Status, StopMessage } from '../../src/circuit/pb/index.js' +import { ReservationStore } from '../../src/circuit/reservation-store.js' +import { Components, DefaultComponents } from '../../src/components.js' +import { DefaultConnectionManager } from '../../src/connection-manager/index.js' +import { DefaultRegistrar } from '../../src/registrar.js' +import { DefaultUpgrader } from '../../src/upgrader.js' +import * as peerUtils from '../utils/creators/peer.js' +import * as Constants from '../../src/constants.js' +import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' +import { publicAddressesFirst } from '@libp2p/utils/address-sort' +import { PersistentPeerStore } from '@libp2p/peer-store' +import { multiaddr } from '@multiformats/multiaddr' +import type { AclStatus } from '../../src/circuit/interfaces.js' +import { pbStream } from 'it-pb-stream' +import { pipe } from 'it-pipe' +import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' +import { duplexPair } from 'it-pair/duplex' +import all from 'it-all' +import type { PeerStore } from '@libp2p/interface-peer-store' +import { MemoryDatastore } from 'datastore-core' +import { Uint8ArrayList } from 'uint8arraylist' +import type { Duplex } from 'it-stream-types' +import { pushable } from 'it-pushable' + +/* eslint-env mocha */ + +describe('Circuit v2 - hop protocol', function () { + describe('reserve', function () { + let relayPeer: PeerId, + conn: Connection, + stream: Stream, + reservationStore: ReservationStore, + peerStore: PeerStore + + beforeEach(async () => { + [, relayPeer] = await peerUtils.createPeerIds(2) + conn = mockConnection(mockMultiaddrConnection(mockDuplex(), relayPeer)) + stream = mockStream(pair()) + reservationStore = new ReservationStore() + peerStore = new PersistentPeerStore(new DefaultComponents({ datastore: new MemoryDatastore() })) + }) + + this.afterEach(async function () { + sinon.restore() + await conn.close() + }) + + it('error on unknown message type', async function () { + const stream = mockStream(pair()) + const pbstr = pbStream(stream) + await handleHopProtocol({ + connection: mockConnection(mockMultiaddrConnection(mockDuplex(), await peerUtils.createPeerId())), + stream: pbstr, + request: {}, + relayPeer, + relayAddrs: [], + reservationStore, + connectionManager: sinon.stub() as any, + peerStore + }) + const msg = await pbstr.pb(HopMessage).read() + expect(msg.type).to.be.equal(HopMessage.Type.STATUS) + expect(msg.status).to.be.equal(Status.UNEXPECTED_MESSAGE) + }) + + it('should reserve slot', async function () { + const expire: number = 123 + const reserveStub = sinon.stub(reservationStore, 'reserve') + reserveStub.resolves({ status: Status.OK, expire }) + const pbstr = pbStream(stream) + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + peerStore, + reservationStore + }) + expect(reserveStub.calledOnceWith(conn.remotePeer, conn.remoteAddr)).to.be.true() + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.limit).to.be.undefined() + expect(response.status).to.be.equal(Status.OK) + expect(response.reservation?.expire).to.be.equal(BigInt(expire)) + expect(response.reservation?.voucher).to.not.be.undefined() + expect(response.reservation?.addrs?.length).to.be.greaterThan(0) + }) + + it('should fail to reserve slot - relayed connection', async function () { + const reserveStub = sinon.stub(reservationStore, 'reserve') + const connStub = sinon.stub(conn, 'remoteAddr') + connStub.value(multiaddr('/ip4/127.0.0.1/tcp/1234/p2p-circuit')) + const pbstr = pbStream(stream) + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + peerStore, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + reservationStore + }) + expect(reserveStub.notCalled).to.be.true() + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.limit).to.be.undefined() + expect(response.status).to.be.equal(Status.PERMISSION_DENIED) + }) + + it('should fail to reserve slot - acl denied', async function () { + const reserveStub = sinon.stub(reservationStore, 'reserve') + const pbstr = pbStream(stream) + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + peerStore, + reservationStore, + acl: { allowReserve: async function () { return false }, allowConnect: sinon.stub() as any } + }) + expect(reserveStub.notCalled).to.be.true() + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.limit).to.be.undefined() + expect(response.status).to.be.equal(Status.PERMISSION_DENIED) + }) + + it('should fail to reserve slot - resource exceeded', async function () { + const reserveStub = sinon.stub(reservationStore, 'reserve') + reserveStub.resolves({ status: Status.RESERVATION_REFUSED }) + const pbstr = pbStream(stream) + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + peerStore, + reservationStore + }) + expect(reserveStub.calledOnce).to.be.true() + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.limit).to.be.undefined() + expect(response.status).to.be.equal(Status.RESERVATION_REFUSED) + }) + + it('should fail to reserve slot - failed to write response', async function () { + const reserveStub = sinon.stub(reservationStore, 'reserve') + const removeReservationStub = sinon.stub(reservationStore, 'removeReservation') + reserveStub.resolves({ status: Status.OK, expire: 123 }) + removeReservationStub.resolves() + const pbstr = pbStream(stream) + const backup = pbstr.write + pbstr.write = function () { throw new Error('connection reset') } + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + peerStore, + reservationStore + }) + expect(reserveStub.calledOnce).to.be.true() + expect(removeReservationStub.calledOnce).to.be.true() + pbstr.write = backup + }) + + it('should tag peer', async () => { + const expire: number = 123 + const reserveStub = sinon.stub(reservationStore, 'reserve') + reserveStub.resolves({ status: Status.OK, expire }) + const pbstr = pbStream(stream) + await handleHopProtocol({ + request: { + type: HopMessage.Type.RESERVE + }, + connection: conn, + stream: pbstr, + relayPeer, + connectionManager: sinon.stub() as any, + relayAddrs: [multiaddr('/ip4/127.0.0.1/udp/1234')], + peerStore, + reservationStore + }) + expect(reserveStub.calledOnceWith(conn.remotePeer, conn.remoteAddr)).to.be.true() + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.limit).to.be.undefined() + expect(response.status).to.be.equal(Status.OK) + expect(response.reservation?.expire).to.be.equal(BigInt(expire)) + expect(response.reservation?.voucher).to.not.be.undefined() + expect(response.reservation?.addrs?.length).to.be.greaterThan(0) + + const tags = await peerStore.getTags(relayPeer) + expect(tags).length(1) + expect(tags[0].value).equal(1) + }) + }) + + describe('connect', function () { + let relayPeer: PeerId, + dstPeer: PeerId, + conn: Connection, + stream: Stream, + reservationStore: ReservationStore, + circuit: Circuit, + components: Components + + beforeEach(async () => { + [, relayPeer, dstPeer] = await peerUtils.createPeerIds(3) + conn = mockConnection(mockMultiaddrConnection(mockDuplex(), relayPeer)) + stream = mockStream(pair()) + reservationStore = new ReservationStore() + // components + components = new DefaultComponents({ datastore: new MemoryDatastore() }) + components.connectionManager = new DefaultConnectionManager(components, + { + maxConnections: 300, + minConnections: 50, + autoDial: true, + autoDialInterval: 10000, + maxParallelDials: Constants.MAX_PARALLEL_DIALS, + maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS, + dialTimeout: Constants.DIAL_TIMEOUT, + inboundUpgradeTimeout: Constants.INBOUND_UPGRADE_TIMEOUT, + resolvers: { + dnsaddr: dnsaddrResolver + }, + addressSorter: publicAddressesFirst + } + ) + components.peerStore = new PersistentPeerStore(components) + components.registrar = new DefaultRegistrar(components) + components.upgrader = new DefaultUpgrader(components, { + connectionEncryption: [], + muxers: [], + inboundUpgradeTimeout: 10000 + }) + + circuit = new Circuit(components, { + enabled: true, + advertise: { + enabled: false + }, + hop: { + enabled: true, + timeout: 30000 + }, + reservationManager: { + enabled: false, + maxReservations: 2 + } + }) + }) + + this.afterEach(async function () { + await conn.close() + }) + + it('should succeed to connect', async function () { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + const getReservationStub = sinon.stub(reservationStore, 'get') + hasReservationStub.resolves(true) + getReservationStub.resolves({ expire: new Date(Date.now() + 2 * 60 * 1000), addr: multiaddr('/ip4/0.0.0.0') }) + const dstConn = mockConnection( + mockMultiaddrConnection(pair(), dstPeer) + ) + const streamStub = sinon.stub(dstConn, 'newStream') + const dstStream = mockStream(pair()) + streamStub.resolves(dstStream) + const dstStreamHandler = pbStream(dstStream) + dstStreamHandler.pb(StopMessage).write({ + type: StopMessage.Type.STATUS, + status: Status.OK + }) + const pbstr = pbStream(stream) + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([dstConn]) + await handleHopProtocol({ + connection: conn, + stream: pbstr, + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager + }) + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.OK) + }) + + it('should fail to connect - invalid request', async function () { + const pbstr = pbStream(stream) + await handleHopProtocol({ + connection: conn, + stream: pbstr, + request: { + type: HopMessage.Type.CONNECT, + // @ts-expect-error {} is missing the following properties from peer: id, addrs + peer: {} + }, + reservationStore, + circuit + }) + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.MALFORMED_MESSAGE) + }) + + it('should failed to connect - acl denied', async function () { + const pbstr = pbStream(stream) + const acl = { + allowConnect: async () => await Promise.resolve(Status.PERMISSION_DENIED as AclStatus), + allowReserve: async () => await Promise.resolve(false) + } + await handleHopProtocol({ + connection: conn, + stream: pbstr, + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager, + acl + }) + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.PERMISSION_DENIED) + }) + + it('should fail to connect - no reservation', async function () { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + hasReservationStub.resolves(false) + const pbstr = pbStream(stream) + await handleHopProtocol({ + connection: conn, + stream: pbstr, + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: sinon.stub() as any, + connectionManager: components.connectionManager + }) + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.NO_RESERVATION) + }) + + it('should fail to connect - no connection', async function () { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + hasReservationStub.resolves(true) + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([]) + const pbstr = pbStream(stream) + await handleHopProtocol({ + connection: conn, + stream: pbstr, + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: sinon.stub() as any, + connectionManager: components.connectionManager + }) + const response = await pbstr.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.NO_RESERVATION) + expect(stub.calledOnce).to.be.true() + }) + }) + + describe('connection limits', () => { + let relayPeer: PeerId, + dstPeer: PeerId, + conn: Connection, + reservationStore: ReservationStore, + components: Components + + beforeEach(async () => { + [, relayPeer, dstPeer] = await peerUtils.createPeerIds(3) + conn = mockConnection(mockMultiaddrConnection(mockDuplex(), relayPeer)) + reservationStore = new ReservationStore() + // components + components = new DefaultComponents({ datastore: new MemoryDatastore() }) + components.connectionManager = new DefaultConnectionManager(components, + + { + maxConnections: 300, + minConnections: 50, + autoDial: true, + autoDialInterval: 10000, + maxParallelDials: Constants.MAX_PARALLEL_DIALS, + maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS, + dialTimeout: Constants.DIAL_TIMEOUT, + inboundUpgradeTimeout: Constants.INBOUND_UPGRADE_TIMEOUT, + resolvers: { + dnsaddr: dnsaddrResolver + }, + addressSorter: publicAddressesFirst + } + ) + components.peerStore = new PersistentPeerStore(components) + components.registrar = new DefaultRegistrar(components) + components.upgrader = new DefaultUpgrader(components, { + connectionEncryption: [], + muxers: [], + inboundUpgradeTimeout: 10000 + }) + }) + + it('should connect - data limit - src to dest', async () => { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + const getReservationStub = sinon.stub(reservationStore, 'get') + hasReservationStub.resolves(true) + getReservationStub.resolves({ + expire: new Date(Date.now() + 2 * 60 * 1000), + addr: multiaddr('/ip4/0.0.0.0'), + // set limit + limit: { + data: BigInt(5), + duration: 0 + } + }) + const dstConn = mockConnection( + mockMultiaddrConnection(pair(), dstPeer) + ) + const [dstServer, dstClient] = duplexPair() + const [srcServer, srcClient] = duplexPair() + + // resolve the destination stream for the server + const dstStream = mockStream(dstServer) + const dstStreamAbortStub = sinon.stub(dstStream, 'abort') + const streamStub = sinon.stub(dstConn, 'newStream') + streamStub.resolves(dstStream) + + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([dstConn]) + const srcServerStream = mockStream(srcServer) + const srcServerAbort = sinon.spy(srcServerStream, 'abort') + const handleHop = expect(handleHopProtocol({ + connection: conn, + stream: pbStream(srcServerStream), + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager + })).to.eventually.fulfilled() + + const dstClientPbStream = pbStream(dstClient) + const stopConnectRequest = await dstClientPbStream.pb(StopMessage).read() + expect(stopConnectRequest.type).to.eq(StopMessage.Type.CONNECT) + // write response + dstClientPbStream.pb(StopMessage).write({ + type: StopMessage.Type.STATUS, + status: Status.OK + }) + + await handleHop + const srcClientPbStream = pbStream(srcClient) + const response = await srcClientPbStream.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.OK) + + const sourceStream = srcClientPbStream.unwrap() + const destStream = dstClientPbStream.unwrap() + + const sender = pushable() + void pipe(sender, sourceStream) + // source to dest, write 4 bytes + sender.push(uint8arrayFromString('0123')) + // source to dest, exceed stream limit + sender.push(uint8arrayFromString('extra')) + const data = await all(destStream.source) + const sum = data.reduce((prev: number, cur: Uint8ArrayList) => prev + cur.length, 0) + expect(sum).eql(5) + expect(dstStreamAbortStub.callCount).to.equal(1) + expect(srcServerAbort.callCount).to.equal(1) + }) + + it('should connect - data limit - dest to src', async () => { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + const getReservationStub = sinon.stub(reservationStore, 'get') + hasReservationStub.resolves(true) + getReservationStub.resolves({ + expire: new Date(Date.now() + 2 * 60 * 1000), + addr: multiaddr('/ip4/0.0.0.0'), + // set limit + limit: { + data: BigInt(5), + duration: 0 + } + }) + const dstConn = mockConnection( + mockMultiaddrConnection(pair(), dstPeer) + ) + const [dstServer, dstClient] = duplexPair() + const [srcServer, srcClient] = duplexPair() + + // resolve the destination stream for the server + const streamStub = sinon.stub(dstConn, 'newStream') + const dstServerStream = mockStream(dstServer) + const dstServerStreamAbortStub = sinon.spy(dstServerStream, 'abort') + streamStub.resolves(dstServerStream) + + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([dstConn]) + + // source stream on the server + const srcServerStream = mockStream(srcServer) + const srcServerStreamAbortStub = sinon.stub(srcServerStream, 'abort') + const handleHop = expect(handleHopProtocol({ + connection: conn, + stream: pbStream(srcServerStream), + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager + })).to.eventually.fulfilled() + + const dstClientPbStream = pbStream(dstClient) + const stopConnectRequest = await dstClientPbStream.pb(StopMessage).read() + expect(stopConnectRequest.type).to.eq(StopMessage.Type.CONNECT) + // write response + dstClientPbStream.pb(StopMessage).write({ + type: StopMessage.Type.STATUS, + status: Status.OK + }) + + await handleHop + const srcClientPbStream = pbStream(srcClient) + const response = await srcClientPbStream.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.OK) + + const sourceStream = srcClientPbStream.unwrap() + const destStream = dstClientPbStream.unwrap() + + const sender = pushable() + void pipe(sender, destStream) + // dest to source, write 4 bytes + sender.push(uint8arrayFromString('0123')) + // dest to source, exceed stream limit + sender.push(uint8arrayFromString('extra')) + const data = await all(sourceStream.source) + const sum = data.reduce((prev: number, cur: Uint8ArrayList) => prev + cur.length, 0) + expect(sum).equal(5) + expect(dstServerStreamAbortStub.callCount).to.equal(1) + expect(srcServerStreamAbortStub.callCount).to.equal(1) + }) + + it('should connect - duration limit - dest to src', async () => { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + const getReservationStub = sinon.stub(reservationStore, 'get') + hasReservationStub.resolves(true) + getReservationStub.resolves({ + expire: new Date(Date.now() + 2 * 60 * 1000), + addr: multiaddr('/ip4/0.0.0.0'), + // set limit + limit: { + // 500 ms duration limit + duration: 500 + } + }) + const dstConn = mockConnection( + mockMultiaddrConnection(pair(), dstPeer) + ) + const [dstServer, dstClient] = duplexPair() + const [srcServer, srcClient] = duplexPair() + + // resolve the destination stream for the server + const streamStub = sinon.stub(dstConn, 'newStream') + const dstServerStream = mockStream(dstServer) + streamStub.resolves(dstServerStream) + + const dstAbortStub = sinon.stub(dstServerStream, 'abort') + + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([dstConn]) + + const srcServerStream = mockStream(srcServer) + const srcServerAbortStub = sinon.stub(srcServerStream, 'abort') + const handleHop = expect(handleHopProtocol({ + connection: conn, + stream: pbStream(srcServerStream), + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager + })).to.eventually.fulfilled() + + const dstClientPbStream = pbStream(dstClient) + const stopConnectRequest = await dstClientPbStream.pb(StopMessage).read() + expect(stopConnectRequest.type).to.eq(StopMessage.Type.CONNECT) + // write response + dstClientPbStream.pb(StopMessage).write({ + type: StopMessage.Type.STATUS, + status: Status.OK + }) + + await handleHop + const srcClientPbStream = pbStream(srcClient) + const response = await srcClientPbStream.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.OK) + + const sourceStream = srcClientPbStream.unwrap() as Duplex + const destStream = dstClientPbStream.unwrap() + + const periodicSender = (period: number, count: number) => async function * () { + const data = new Uint8ArrayList(new Uint8Array([0, 0, 0, 0])) + while (count > 0) { + await new Promise((resolve) => setTimeout(resolve, period)) + yield data + count-- + } + } + // dest to source, write 4 messages + void pipe(periodicSender(200, 4), destStream) + + const received = await all(sourceStream.source) + expect(received.reduce((p, c) => p + c.length, 0)).to.equal(8) + expect(dstAbortStub.callCount).to.equal(1) + expect(srcServerAbortStub.callCount).to.equal(1) + }) + + it('should connect - duration limit - src to dest', async () => { + const hasReservationStub = sinon.stub(reservationStore, 'hasReservation') + const getReservationStub = sinon.stub(reservationStore, 'get') + hasReservationStub.resolves(true) + getReservationStub.resolves({ + expire: new Date(Date.now() + 2 * 60 * 1000), + addr: multiaddr('/ip4/0.0.0.0'), + // set limit + limit: { + // 500 ms duration limit + duration: 500 + } + }) + const dstConn = mockConnection( + mockMultiaddrConnection(pair(), dstPeer) + ) + const [dstServer, dstClient] = duplexPair() + const [srcServer, srcClient] = duplexPair() + + // resolve the destination stream for the server + const streamStub = sinon.stub(dstConn, 'newStream') + const dstServerStream = mockStream(dstServer) + const dstServerAbortStub = sinon.stub(dstServerStream, 'abort') + streamStub.resolves(dstServerStream) + + const stub = sinon.stub(components.connectionManager, 'getConnections') + stub.returns([dstConn]) + const srcServerStream = mockStream(srcServer) + const srcAbortStub = sinon.stub(srcServerStream, 'abort') + const handleHop = expect(handleHopProtocol({ + connection: conn, + stream: pbStream(srcServerStream), + request: { + type: HopMessage.Type.CONNECT, + peer: { + id: dstPeer.toBytes(), + addrs: [] + } + }, + relayPeer: relayPeer, + relayAddrs: [], + reservationStore, + peerStore: components.peerStore, + connectionManager: components.connectionManager + })).to.eventually.fulfilled() + + const dstClientPbStream = pbStream(dstClient) + const stopConnectRequest = await dstClientPbStream.pb(StopMessage).read() + expect(stopConnectRequest.type).to.eq(StopMessage.Type.CONNECT) + // write response + dstClientPbStream.pb(StopMessage).write({ + type: StopMessage.Type.STATUS, + status: Status.OK + }) + + await handleHop + const srcClientPbStream = pbStream(srcClient) + const response = await srcClientPbStream.pb(HopMessage).read() + expect(response.type).to.be.equal(HopMessage.Type.STATUS) + expect(response.status).to.be.equal(Status.OK) + + const sourceStream = srcClientPbStream.unwrap() as Duplex + const destStream = dstClientPbStream.unwrap() + + const periodicSender = (period: number, count: number) => async function * () { + const data = new Uint8ArrayList(new Uint8Array([0, 0, 0, 0])) + while (count > 0) { + await new Promise((resolve) => setTimeout(resolve, period)) + yield data + count-- + } + } + // dest to source, write 4 messages + void pipe(periodicSender(200, 4), sourceStream) + + const received = await all(destStream.source) + const sum = received.reduce((prev: number, cur: Uint8ArrayList) => prev + cur.length, 0) + expect(sum).equals(8) + expect(srcAbortStub.callCount).to.equal(1) + expect(dstServerAbortStub.callCount).to.equal(1) + }) + }) +}) diff --git a/test/circuit/reservation-store.spec.ts b/test/circuit/reservation-store.spec.ts new file mode 100644 index 0000000000..9682f65c14 --- /dev/null +++ b/test/circuit/reservation-store.spec.ts @@ -0,0 +1,87 @@ +import { multiaddr } from '@multiformats/multiaddr' +import { expect } from 'aegir/chai' +import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from '../../src/circuit/constants.js' +import { Status } from '../../src/circuit/pb/index.js' +import { ReservationStore } from '../../src/circuit/reservation-store.js' +import { createPeerId } from '../utils/creators/peer.js' + +/* eslint-env mocha */ + +describe('Circuit v2 - reservation store', function () { + it('should add reservation', async function () { + const store = new ReservationStore({ maxReservations: 2 }) + const peer = await createPeerId() + const result = await store.reserve(peer, multiaddr()) + expect(result.status).to.equal(Status.OK) + expect(result.expire).to.not.be.undefined() + expect(await store.hasReservation(peer)).to.be.true() + }) + it('should add reservation if peer already has reservation', async function () { + const store = new ReservationStore({ maxReservations: 1 }) + const peer = await createPeerId() + await store.reserve(peer, multiaddr()) + const result = await store.reserve(peer, multiaddr()) + expect(result.status).to.equal(Status.OK) + expect(result.expire).to.not.be.undefined() + expect(await store.hasReservation(peer)).to.be.true() + }) + + it('should fail to add reservation on exceeding limit', async function () { + const store = new ReservationStore({ maxReservations: 0 }) + const peer = await createPeerId() + const result = await store.reserve(peer, multiaddr()) + expect(result.status).to.equal(Status.RESERVATION_REFUSED) + }) + + it('should remove reservation', async function () { + const store = new ReservationStore({ maxReservations: 10 }) + const peer = await createPeerId() + const result = await store.reserve(peer, multiaddr()) + expect(result.status).to.equal(Status.OK) + expect(await store.hasReservation(peer)).to.be.true() + await store.removeReservation(peer) + expect(await store.hasReservation(peer)).to.be.false() + await store.removeReservation(peer) + }) + + it('should apply configured default connection limits', async function () { + const defaultDataLimit = 10n + const defaultDurationLimit = 10 + + const store = new ReservationStore({ + defaultDataLimit, + defaultDurationLimit + }) + const peer = await createPeerId() + await store.reserve(peer, multiaddr()) + + const reservation = store.get(peer) + + expect(reservation).to.have.nested.property('limit.data', defaultDataLimit) + expect(reservation).to.have.nested.property('limit.duration', defaultDurationLimit) + }) + + it('should apply default connection limits', async function () { + const store = new ReservationStore() + const peer = await createPeerId() + await store.reserve(peer, multiaddr()) + + const reservation = store.get(peer) + + expect(reservation).to.have.nested.property('limit.data', DEFAULT_DATA_LIMIT) + expect(reservation).to.have.nested.property('limit.duration', DEFAULT_DURATION_LIMIT) + }) + + it('should not apply default connection limits when they have been disabled', async function () { + const store = new ReservationStore({ + applyDefaultLimit: false + }) + const peer = await createPeerId() + await store.reserve(peer, multiaddr()) + + const reservation = store.get(peer) + + expect(reservation).to.not.have.nested.property('limit.data') + expect(reservation).to.not.have.nested.property('limit.duration') + }) +}) diff --git a/test/circuit/stop.spec.ts b/test/circuit/stop.spec.ts new file mode 100644 index 0000000000..2c67eb1e89 --- /dev/null +++ b/test/circuit/stop.spec.ts @@ -0,0 +1,64 @@ +import { pair } from 'it-pair' +import type { Connection, Stream } from '@libp2p/interface-connection' +import type { PeerId } from '@libp2p/interface-peer-id' +import { createPeerIds } from '../utils/creators/peer.js' +import { handleStop, stop } from '../../src/circuit/stop.js' +import { Status, StopMessage } from '../../src/circuit/pb/index.js' +import { expect } from 'aegir/chai' +import sinon from 'sinon' +import { mockConnection, mockMultiaddrConnection, mockStream } from '@libp2p/interface-mocks' +import { pbStream, ProtobufStream } from 'it-pb-stream' + +/* eslint-env mocha */ + +describe('Circuit v2 - stop protocol', function () { + let srcPeer: PeerId, relayPeer: PeerId, conn: Connection, pbstr: ProtobufStream + + beforeEach(async () => { + [srcPeer, relayPeer] = await createPeerIds(2) + conn = mockConnection(mockMultiaddrConnection(pair(), relayPeer)) + pbstr = pbStream(mockStream(pair())) + }) + + this.afterEach(async function () { + await conn.close() + }) + + it('handle stop - success', async function () { + await handleStop({ connection: conn, request: { type: StopMessage.Type.CONNECT, peer: { id: srcPeer.toBytes(), addrs: [] } }, pbstr }) + const response = await pbstr.pb(StopMessage).read() + expect(response.status).to.be.equal(Status.OK) + }) + + it('handle stop error - invalid request - wrong type', async function () { + await handleStop({ connection: conn, request: { type: StopMessage.Type.STATUS, peer: { id: srcPeer.toBytes(), addrs: [] } }, pbstr }) + const response = await pbstr.pb(StopMessage).read() + expect(response.status).to.be.equal(Status.UNEXPECTED_MESSAGE) + }) + + it('handle stop error - invalid request - missing peer', async function () { + await handleStop({ connection: conn, request: { type: StopMessage.Type.CONNECT }, pbstr }) + const response = await pbstr.pb(StopMessage).read() + expect(response.status).to.be.equal(Status.MALFORMED_MESSAGE) + }) + + it('handle stop error - invalid request - invalid peer addr', async function () { + await handleStop({ connection: conn, request: { type: StopMessage.Type.CONNECT, peer: { id: srcPeer.toBytes(), addrs: [new Uint8Array(32)] } }, pbstr }) + const response = await pbstr.pb(StopMessage).read() + expect(response.status).to.be.equal(Status.MALFORMED_MESSAGE) + }) + + it('send stop - success', async function () { + const streamStub = sinon.stub(conn, 'newStream') + streamStub.resolves(mockStream(pair())) + await stop({ connection: conn, request: { type: StopMessage.Type.CONNECT, peer: { id: srcPeer.toBytes(), addrs: [] } } }) + pbstr.pb(StopMessage).write({ type: StopMessage.Type.STATUS, status: Status.OK }) + }) + + it('send stop - should not fall apart with invalid status response', async function () { + const streamStub = sinon.stub(conn, 'newStream') + streamStub.resolves(mockStream(pair())) + await stop({ connection: conn, request: { type: StopMessage.Type.CONNECT, peer: { id: srcPeer.toBytes(), addrs: [] } } }) + pbstr.write(new Uint8Array(10)) + }) +}) diff --git a/test/configuration/protocol-prefix.node.ts b/test/configuration/protocol-prefix.node.ts index c21cdd5658..6418dd093f 100644 --- a/test/configuration/protocol-prefix.node.ts +++ b/test/configuration/protocol-prefix.node.ts @@ -33,7 +33,7 @@ describe('Protocol prefix is configurable', () => { const protocols = await libp2p.peerStore.protoBook.get(libp2p.peerId) expect(protocols).to.include.members([ `/${testProtocol}/fetch/0.0.1`, - '/libp2p/circuit/relay/0.1.0', + '/libp2p/circuit/relay/0.2.0/stop', `/${testProtocol}/id/1.0.0`, `/${testProtocol}/id/push/1.0.0`, `/${testProtocol}/ping/1.0.0` @@ -46,7 +46,7 @@ describe('Protocol prefix is configurable', () => { const protocols = await libp2p.peerStore.protoBook.get(libp2p.peerId) expect(protocols).to.include.members([ - '/libp2p/circuit/relay/0.1.0', + '/libp2p/circuit/relay/0.2.0/stop', '/ipfs/id/1.0.0', '/ipfs/id/push/1.0.0', '/ipfs/ping/1.0.0', diff --git a/test/dialing/direct.node.ts b/test/dialing/direct.node.ts index a2162a7eaa..956d75bb7d 100644 --- a/test/dialing/direct.node.ts +++ b/test/dialing/direct.node.ts @@ -553,14 +553,9 @@ describe('libp2p.dialer (direct, TCP)', () => { expect(dialResults).to.have.length(10) for (const result of dialResults) { - expect(result).to.have.property('status', 'rejected') - expect(result).to.have.property('reason').that.has.property('name', 'AggregateError') - // All errors should be the exact same as `error` - // @ts-expect-error reason is any - for (const err of result.reason.errors) { - expect(err).to.equal(error) - } + expect(result).to.have.property('status', 'rejected') + expect(result).to.have.property('reason', error) } // 1 connection, because we know the peer in the multiaddr diff --git a/test/dialing/resolver.spec.ts b/test/dialing/resolver.spec.ts index 4c2f7dca77..0334a607bf 100644 --- a/test/dialing/resolver.spec.ts +++ b/test/dialing/resolver.spec.ts @@ -14,6 +14,9 @@ import { Circuit } from '../../src/circuit/transport.js' import pDefer from 'p-defer' import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks' import { peerIdFromString } from '@libp2p/peer-id' +import { pEvent } from 'p-event' +import { createFromJSON } from '@libp2p/peer-id-factory' +import { RELAY_V2_HOP_CODEC } from '../../src/circuit/multicodec.js' const relayAddr = MULTIADDRS_WEBSOCKETS[0] @@ -53,6 +56,9 @@ describe('Dialing (resolvable addresses)', () => { }, relay: { enabled: true, + reservationManager: { + enabled: true + }, hop: { enabled: false } @@ -73,6 +79,9 @@ describe('Dialing (resolvable addresses)', () => { }, relay: { enabled: true, + reservationManager: { + enabled: true + }, hop: { enabled: false } @@ -81,6 +90,8 @@ describe('Dialing (resolvable addresses)', () => { started: true }) ]) + + await Promise.all([libp2p, remoteLibp2p].map(async n => await n.start())) }) afterEach(async () => { @@ -89,6 +100,12 @@ describe('Dialing (resolvable addresses)', () => { }) it('resolves dnsaddr to ws local address', async () => { + const { default: Peers } = await import('../fixtures/peers.js') + + // Use the last peer + const peerId = await createFromJSON(Peers[Peers.length - 1]) + // ensure remote libp2p creates reservation on relay + await remoteLibp2p.components.peerStore.protoBook.add(peerId, [RELAY_V2_HOP_CODEC]) const remoteId = remoteLibp2p.peerId const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`) const relayedAddrFetched = multiaddr(relayedAddr(remoteId)) @@ -100,6 +117,12 @@ describe('Dialing (resolvable addresses)', () => { // Resolver stub resolver.onCall(0).returns(Promise.resolve(getDnsRelayedAddrStub(remoteId))) + // create reservation on relay + if (remoteLibp2p.circuitService == null) { + throw new Error('remote libp2p has no circuit service') + } + await pEvent(remoteLibp2p.circuitService, 'relay:reservation') + // Dial with address resolve const connection = await libp2p.dial(dialAddr) expect(connection).to.exist() @@ -114,6 +137,19 @@ describe('Dialing (resolvable addresses)', () => { const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`) const relayedAddrFetched = multiaddr(relayedAddr(remoteId)) + const { default: Peers } = await import('../fixtures/peers.js') + + // Use the last peer + const relayId = await createFromJSON(Peers[Peers.length - 1]) + // ensure remote libp2p creates reservation on relay + await remoteLibp2p.components.peerStore.protoBook.add(relayId, [RELAY_V2_HOP_CODEC]) + + // create reservation on relay + if (remoteLibp2p.circuitService == null) { + throw new Error('remote libp2p has no circuit service') + } + await pEvent(remoteLibp2p.circuitService, 'relay:reservation') + // Transport spy const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag]) const transportDialSpy = sinon.spy(transport, 'dial') @@ -173,6 +209,19 @@ describe('Dialing (resolvable addresses)', () => { const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`) const relayedAddrFetched = multiaddr(relayedAddr(remoteId)) + const { default: Peers } = await import('../fixtures/peers.js') + + // Use the last peer + const relayId = await createFromJSON(Peers[Peers.length - 1]) + // ensure remote libp2p creates reservation on relay + await remoteLibp2p.components.peerStore.protoBook.add(relayId, [RELAY_V2_HOP_CODEC]) + + // create reservation on relay + if (remoteLibp2p.circuitService == null) { + throw new Error('remote libp2p has no circuit service') + } + await pEvent(remoteLibp2p.circuitService, 'relay:reservation') + // Transport spy const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag]) const transportDialSpy = sinon.spy(transport, 'dial') diff --git a/test/interop.ts b/test/interop.ts index 10fc85822f..f326bbef0e 100644 --- a/test/interop.ts +++ b/test/interop.ts @@ -18,20 +18,34 @@ import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import type { PeerId } from '@libp2p/interface-peer-id' import { peerIdFromKeys } from '@libp2p/peer-id' import { floodsub } from '@libp2p/floodsub' - -// IPFS_LOGGING=debug DEBUG=libp2p*,go-libp2p:* npm run test:interop +import { gossipsub } from '@chainsafe/libp2p-gossipsub' + +/** + * @packageDocumentation + * + * To enable debug logging, run the tests with the following env vars: + * + * ```console + * DEBUG=libp2p*,go-libp2p:* npm run test:interop + * ``` + */ async function createGoPeer (options: SpawnOptions): Promise { const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000 - const apiAddr = multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`) + const apiAddr = multiaddr(`/ip4/127.0.0.1/tcp/${controlPort}`) const log = logger(`go-libp2p:${controlPort}`) const opts = [ - `-listen=${apiAddr.toString()}`, - '-hostAddrs=/ip4/0.0.0.0/tcp/0' + `-listen=${apiAddr.toString()}` ] + if (options.noListen === true) { + opts.push('-noListenAddrs') + } else { + opts.push('-hostAddrs=/ip4/127.0.0.1/tcp/0') + } + if (options.noise === true) { opts.push('-noise=true') } @@ -40,6 +54,10 @@ async function createGoPeer (options: SpawnOptions): Promise { opts.push('-dhtServer') } + if (options.relay === true) { + opts.push('-relay') + } + if (options.pubsub === true) { opts.push('-pubsub') } @@ -52,8 +70,18 @@ async function createGoPeer (options: SpawnOptions): Promise { opts.push(`-id=${options.key}`) } + if (options.muxer === 'mplex') { + opts.push('-muxer=mplex') + } else { + opts.push('-muxer=yamux') + } + const deferred = pDefer() - const proc = execa(p2pd(), opts) + const proc = execa(p2pd(), opts, { + env: { + GOLOG_LOG_LEVEL: 'debug' + } + }) proc.stdout?.on('data', (buf: Buffer) => { const str = buf.toString() @@ -91,11 +119,14 @@ async function createJsPeer (options: SpawnOptions): Promise { const opts: Libp2pOptions = { peerId, addresses: { - listen: ['/ip4/0.0.0.0/tcp/0'] + listen: options.noListen === true ? [] : ['/ip4/127.0.0.1/tcp/0'] }, transports: [tcp()], streamMuxers: [], - connectionEncryption: [noise()] + connectionEncryption: [noise()], + nat: { + enabled: false + } } if (options.muxer === 'mplex') { @@ -108,7 +139,17 @@ async function createJsPeer (options: SpawnOptions): Promise { if (options.pubsubRouter === 'floodsub') { opts.pubsub = floodsub() } else { - opts.pubsub = floodsub() + opts.pubsub = gossipsub() + } + } + + opts.relay = { + enabled: true, + hop: { + enabled: options.relay === true + }, + reservationManager: { + enabled: false } } @@ -136,7 +177,7 @@ async function createJsPeer (options: SpawnOptions): Promise { const node = await createLibp2p(opts) - const server = await createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node) + const server = createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node) await server.start() return { diff --git a/test/relay/auto-relay.node.ts b/test/relay/auto-relay.node.ts index 806e70d20f..e623db28cf 100644 --- a/test/relay/auto-relay.node.ts +++ b/test/relay/auto-relay.node.ts @@ -5,7 +5,7 @@ import { pEvent } from 'p-event' import defer from 'p-defer' import pWaitFor from 'p-wait-for' import sinon from 'sinon' -import { RELAY_CODEC } from '../../src/circuit/multicodec.js' +import { RELAY_V2_HOP_CODEC } from '../../src/circuit/multicodec.js' import { createNode } from '../utils/creators/peer.js' import type { Libp2pNode } from '../../src/libp2p.js' import type { Options as PWaitForOptions } from 'p-wait-for' @@ -17,23 +17,15 @@ import type { ContentRouting } from '@libp2p/interface-content-routing' async function usingAsRelay (node: Libp2pNode, relay: Libp2pNode, opts?: PWaitForOptions) { // Wait for peer to be used as a relay await pWaitFor(() => { - for (const addr of node.getMultiaddrs()) { - if (addr.toString().includes(`${relay.peerId.toString()}/p2p-circuit`)) { - return true - } - } - - return false + const search = `${relay.peerId.toString()}/p2p-circuit` + return node.getMultiaddrs().find(addr => addr.toString().includes(search)) !== undefined }, opts) } async function discoveredRelayConfig (node: Libp2pNode, relay: Libp2pNode) { await pWaitFor(async () => { const peerData = await node.peerStore.get(relay.peerId) - const supportsRelay = peerData.protocols.includes(RELAY_CODEC) - const supportsHop = peerData.metadata.has('hop_relay') - - return supportsRelay && supportsHop + return peerData.protocols.includes(RELAY_V2_HOP_CODEC) }) } @@ -75,7 +67,7 @@ describe('auto-relay', () => { // Peer has relay multicodec const knownProtocols = await libp2p.peerStore.protoBook.get(relayLibp2p.peerId) - expect(knownProtocols).to.include(RELAY_CODEC) + expect(knownProtocols).to.include(RELAY_V2_HOP_CODEC) }) }) @@ -100,7 +92,7 @@ describe('auto-relay', () => { afterEach(async () => { // Stop each node - return await Promise.all([libp2p, relayLibp2p1, relayLibp2p2, relayLibp2p3].map(async libp2p => await libp2p.stop())) + await Promise.all([libp2p, relayLibp2p1, relayLibp2p2, relayLibp2p3].map(async libp2p => await libp2p.stop())) }) it('should ask if node supports hop on protocol change (relay protocol) and add to listen multiaddrs', async () => { @@ -114,7 +106,7 @@ describe('auto-relay', () => { // Peer has relay multicodec const knownProtocols = await relayLibp2p1.peerStore.protoBook.get(relayLibp2p2.peerId) - expect(knownProtocols).to.include(RELAY_CODEC) + expect(knownProtocols).to.include(RELAY_V2_HOP_CODEC) }) it('should be able to dial a peer from its relayed address previously added', async () => { @@ -143,7 +135,7 @@ describe('auto-relay', () => { // Relay2 has relay multicodec const knownProtocols2 = await relayLibp2p1.peerStore.protoBook.get(relayLibp2p2.peerId) - expect(knownProtocols2).to.include(RELAY_CODEC) + expect(knownProtocols2).to.include(RELAY_V2_HOP_CODEC) // Discover an extra relay and connect await relayLibp2p1.peerStore.addressBook.add(relayLibp2p3.peerId, relayLibp2p3.getMultiaddrs()) @@ -157,7 +149,7 @@ describe('auto-relay', () => { // Relay2 has relay multicodec const knownProtocols3 = await relayLibp2p1.peerStore.protoBook.get(relayLibp2p3.peerId) - expect(knownProtocols3).to.include(RELAY_CODEC) + expect(knownProtocols3).to.include(RELAY_V2_HOP_CODEC) }) it('should not listen on a relayed address we disconnect from peer', async () => { @@ -367,9 +359,9 @@ describe('auto-relay', () => { ttl: 1000, enabled: true }, - autoRelay: { + reservationManager: { enabled: true, - maxListeners: 1 + maxReservations: 1 } }, contentRouters: [ diff --git a/test/relay/relay.node.ts b/test/relay/relay.node.ts index 823050e157..ba16029d4c 100644 --- a/test/relay/relay.node.ts +++ b/test/relay/relay.node.ts @@ -1,20 +1,19 @@ -/* eslint-env mocha */ - import { expect } from 'aegir/chai' -import sinon from 'sinon' import { multiaddr } from '@multiformats/multiaddr' import { pipe } from 'it-pipe' +import { pEvent } from 'p-event' +import * as sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createNode } from '../utils/creators/peer.js' +import { RELAY_V2_HOP_CODEC } from '../../src/circuit/multicodec.js' import { codes as Errors } from '../../src/errors.js' import type { Libp2pNode } from '../../src/libp2p.js' -import all from 'it-all' -import { RELAY_CODEC } from '../../src/circuit/multicodec.js' -import { StreamHandler } from '../../src/circuit/circuit/stream-handler.js' -import { CircuitRelay } from '../../src/circuit/pb/index.js' +import { createNode } from '../utils/creators/peer.js' import { createNodeOptions, createRelayOptions } from './utils.js' +import all from 'it-all' import delay from 'delay' +/* eslint-env mocha */ + describe('Dialing (via relay, TCP)', () => { let srcLibp2p: Libp2pNode let relayLibp2p: Libp2pNode @@ -26,7 +25,7 @@ describe('Dialing (via relay, TCP)', () => { createNode({ config: createNodeOptions({ relay: { - autoRelay: { + reservationManager: { enabled: false } } @@ -35,7 +34,7 @@ describe('Dialing (via relay, TCP)', () => { createNode({ config: createRelayOptions({ relay: { - autoRelay: { + reservationManager: { enabled: false } } @@ -44,8 +43,8 @@ describe('Dialing (via relay, TCP)', () => { createNode({ config: createNodeOptions({ relay: { - autoRelay: { - enabled: false + reservationManager: { + enabled: true } } }) @@ -69,12 +68,16 @@ describe('Dialing (via relay, TCP)', () => { const relayAddr = relayLibp2p.components.transportManager.getAddrs()[0] const relayIdString = relayLibp2p.peerId.toString() + await dstLibp2p.dial(relayAddr.encapsulate(`/p2p/${relayIdString}`)) + // make sure we have reservation before trying to dial. Previously relay initiated connection. + if (dstLibp2p.circuitService == null) { + throw new Error('remote libp2p has no circuit service') + } + await pEvent(dstLibp2p.circuitService, 'relay:reservation') const dialAddr = relayAddr .encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toString()}`) - await relayLibp2p.dial(dstLibp2p.getMultiaddrs()[0]) - const connection = await srcLibp2p.dial(dialAddr) expect(connection).to.exist() @@ -91,6 +94,7 @@ describe('Dialing (via relay, TCP)', () => { ) expect(output.slice()).to.eql(input) + echoStream.close() }) it('should fail to connect to a peer over a relay with inactive connections', async () => { @@ -103,7 +107,7 @@ describe('Dialing (via relay, TCP)', () => { await expect(srcLibp2p.dial(dialAddr)) .to.eventually.be.rejected() - .and.to.have.nested.property('.errors[0].code', Errors.ERR_HOP_REQUEST_FAILED) + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) }) it('should not stay connected to a relay when not already connected and HOP fails', async () => { @@ -116,7 +120,7 @@ describe('Dialing (via relay, TCP)', () => { await expect(srcLibp2p.dial(dialAddr)) .to.eventually.be.rejected() - .and.to.have.nested.property('.errors[0].code', Errors.ERR_HOP_REQUEST_FAILED) + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) // We should not be connected to the relay, because we weren't before the dial const srcToRelayConns = srcLibp2p.components.connectionManager.getConnections(relayLibp2p.peerId) @@ -134,7 +138,7 @@ describe('Dialing (via relay, TCP)', () => { await expect(srcLibp2p.dial(dialAddr)) .to.eventually.be.rejected() - .and.to.have.nested.property('.errors[0].code', Errors.ERR_HOP_REQUEST_FAILED) + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) const srcToRelayConn = srcLibp2p.components.connectionManager.getConnections(relayLibp2p.peerId) expect(srcToRelayConn).to.have.lengthOf(1) @@ -157,14 +161,11 @@ describe('Dialing (via relay, TCP)', () => { // send an invalid relay message from the relay to the destination peer const connections = relayLibp2p.getConnections(dstLibp2p.peerId) - const stream = await connections[0].newStream(RELAY_CODEC) - const streamHandler = new StreamHandler({ stream }) - streamHandler.write({ - type: CircuitRelay.Type.STATUS - }) - const res = await streamHandler.read() - expect(res?.code).to.equal(CircuitRelay.Status.MALFORMED_MESSAGE) - streamHandler.close() + // this should fail as the destination peer has HOP disabled + await expect(connections[0].newStream(RELAY_V2_HOP_CODEC)) + .to.be.rejectedWith(/protocol selection failed/) + // empty messages are encoded as { type: RESERVE } for the hop codec, + // so we make the message invalid by adding a zeroed byte // should still be connected const dstToRelayConn = dstLibp2p.components.connectionManager.getConnections(relayLibp2p.peerId) @@ -174,15 +175,14 @@ describe('Dialing (via relay, TCP)', () => { it('should time out when establishing a relay connection', async () => { await relayLibp2p.stop() + relayLibp2p = await createNode({ config: createRelayOptions({ relay: { - autoRelay: { - enabled: false - }, + enabled: true, hop: { // very short timeout - timeout: 10 + timeout: 500 } } }) @@ -192,9 +192,11 @@ describe('Dialing (via relay, TCP)', () => { const dialAddr = relayAddr.encapsulate(`/p2p/${relayLibp2p.peerId.toString()}`) const connection = await srcLibp2p.dial(dialAddr) - const stream = await connection.newStream('/libp2p/circuit/relay/0.1.0') + // this should succeed as the timeout is only effective after + // multistream select negotiates the protocol + const stream = await connection.newStream([RELAY_V2_HOP_CODEC]) - await stream.sink(async function * () { + void stream.sink(async function * () { // delay for longer than the timeout await delay(1000) yield Uint8Array.from([0]) diff --git a/test/relay/utils.ts b/test/relay/utils.ts index 3e78f26c06..aa3739d2cf 100644 --- a/test/relay/utils.ts +++ b/test/relay/utils.ts @@ -15,9 +15,9 @@ export function createNodeOptions (...overrides: Libp2pOptions[]): Libp2pOptions hop: { enabled: false }, - autoRelay: { + reservationManager: { enabled: true, - maxListeners: 1 + maxReservations: 1 } } }, ...overrides) diff --git a/test/utils/creators/peer.ts b/test/utils/creators/peer.ts index 4c0c387902..d80eb153fc 100644 --- a/test/utils/creators/peer.ts +++ b/test/utils/creators/peer.ts @@ -3,6 +3,7 @@ import Peers from '../../fixtures/peers.js' import { createBaseOptions } from '../base-options.browser.js' import { createEd25519PeerId, createFromJSON, createRSAPeerId } from '@libp2p/peer-id-factory' import { createLibp2pNode, Libp2pNode } from '../../../src/libp2p.js' +import pTimes from 'p-times' import type { Libp2pOptions } from '../../../src/index.js' import type { PeerId } from '@libp2p/interface-peer-id' import type { AddressManagerInit } from '../../../src/address-manager/index.js' @@ -72,10 +73,6 @@ export async function populateAddressBooks (peers: Libp2pNode[]) { } export interface CreatePeerIdOptions { - /** - * number of peers (default: 1) - */ - number?: number /** * fixture index for peer-id generation (default: 0) @@ -92,7 +89,7 @@ export interface CreatePeerIdOptions { } /** - * Create Peer-ids + * Create Peer-id */ export async function createPeerId (options: CreatePeerIdOptions = {}): Promise { const opts = options.opts ?? {} @@ -103,3 +100,15 @@ export async function createPeerId (options: CreatePeerIdOptions = {}): Promise< return await createFromJSON(Peers[options.fixture]) } + +/** + * Create Peer-ids + */ +export async function createPeerIds (count: number, options: Omit = {}): Promise { + const opts = options.opts ?? {} + + return await pTimes(count, async (i) => await createPeerId({ + ...opts, + fixture: i + })) +} diff --git a/tsconfig.json b/tsconfig.json index fe4fd056b1..21473ad0ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,10 +7,4 @@ "src", "test" ], - "exclude": [ - "src/circuit/pb/index.js", - "src/fetch/pb/proto.js", - "src/identify/pb/message.js", - "src/insecure/pb/proto.js" - ] }