Skip to content

Commit a278d85

Browse files
committed
Merge branch 'master' into fix/add-max-listeners-dialler-relay
2 parents eea07c6 + e9099d4 commit a278d85

File tree

29 files changed

+410
-235
lines changed

29 files changed

+410
-235
lines changed

README.md

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,3 @@
1-
# js-libp2p-monorepo <!-- omit in toc -->
2-
3-
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4-
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5-
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
6-
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)
7-
8-
> JavaScript implementation of libp2p, a modular peer to peer network stack
9-
10-
## Table of contents <!-- omit in toc -->
11-
12-
- [Structure](#structure)
13-
- [Project status](#project-status)
14-
- [Background](#background)
15-
- [Roadmap](#roadmap)
16-
- [Install](#install)
17-
- [Usage](#usage)
18-
- [Configuration](#configuration)
19-
- [Limits](#limits)
20-
- [Getting started](#getting-started)
21-
- [Tutorials and Examples](#tutorials-and-examples)
22-
- [Development](#development)
23-
- [Tests](#tests)
24-
- [Run unit tests](#run-unit-tests)
25-
- [Packages](#packages)
26-
- [Used by](#used-by)
27-
- [Contribute](#contribute)
28-
- [API Docs](#api-docs)
29-
- [License](#license)
30-
- [Contribution](#contribution)
31-
32-
## Structure
33-
34-
- [`/doc`](./doc) Docs for libp2p
35-
- [`/examples/auto-relay`](./examples/auto-relay) Shows how to configure relayed connections
36-
- [`/examples/chat`](./examples/chat) An example chat app using libp2p
37-
- [`/examples/connection-encryption`](./examples/connection-encryption) An example of how to configure connection encrypters
38-
- [`/examples/delegated-routing`](./examples/delegated-routing) How to configure libp2p delegated routers
39-
- [`/examples/discovery-mechanisms`](./examples/discovery-mechanisms) How to configure peer discovery mechanisms
40-
- [`/examples/echo`](./examples/echo) An example echo app
41-
- [`/examples/peer-and-content-routing`](./examples/peer-and-content-routing) How to use peer and content routing
42-
- [`/examples/pnet`](./examples/pnet) How to configure a libp2p private network
43-
- [`/examples/protocol-and-stream-muxing`](./examples/protocol-and-stream-muxing) How to use multiplex protocols streams
44-
- [`/examples/pubsub`](./examples/pubsub) An example using libp2p pubsub
45-
- [`/examples/transports`](./examples/transports) An example using different types of libp2p transport
46-
- [`/interop`](./interop) Multidimension Interop Test
47-
- [`/packages/crypto`](./packages/crypto) Crypto primitives for libp2p
48-
- [`/packages/interface`](./packages/interface) The interface implemented by a libp2p node
49-
- [`/packages/interface-compliance-tests`](./packages/interface-compliance-tests) Compliance tests for JS libp2p interfaces
50-
- [`/packages/interface-internal`](./packages/interface-internal) Interfaces implemented by internal libp2p components
51-
- [`/packages/kad-dht`](./packages/kad-dht) JavaScript implementation of the Kad-DHT for libp2p
52-
- [`/packages/keychain`](./packages/keychain) Key management and cryptographically protected messages
53-
- [`/packages/libp2p`](./packages/libp2p) JavaScript implementation of libp2p, a modular peer to peer network stack
54-
- [`/packages/logger`](./packages/logger) A logging component for use in js-libp2p modules
55-
- [`/packages/metrics-prometheus`](./packages/metrics-prometheus) Collect libp2p metrics for scraping by Prometheus or Graphana
56-
- [`/packages/multistream-select`](./packages/multistream-select) JavaScript implementation of multistream-select
57-
- [`/packages/peer-collections`](./packages/peer-collections) Stores values against a peer id
58-
- [`/packages/peer-discovery-bootstrap`](./packages/peer-discovery-bootstrap) Peer discovery via a list of bootstrap peers
59-
- [`/packages/peer-discovery-mdns`](./packages/peer-discovery-mdns) Node.js libp2p mDNS discovery implementation for peer discovery
60-
- [`/packages/peer-id`](./packages/peer-id) Implementation of @libp2p/interface-peer-id
61-
- [`/packages/peer-id-factory`](./packages/peer-id-factory) Create PeerId instances
62-
- [`/packages/peer-record`](./packages/peer-record) Used to transfer signed peer data across the network
63-
- [`/packages/peer-store`](./packages/peer-store) Stores information about peers libp2p knows on the network
64-
- [`/packages/protocol-perf`](./packages/protocol-perf) Implementation of Perf Protocol
65-
- [`/packages/pubsub`](./packages/pubsub) libp2p pubsub base class
66-
- [`/packages/pubsub-floodsub`](./packages/pubsub-floodsub) libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
67-
- [`/packages/stream-multiplexer-mplex`](./packages/stream-multiplexer-mplex) JavaScript implementation of <https://github.com/libp2p/mplex>
68-
- [`/packages/transport-tcp`](./packages/transport-tcp) A TCP transport for libp2p
69-
- [`/packages/transport-webrtc`](./packages/transport-webrtc) A libp2p transport using WebRTC connections
70-
- [`/packages/transport-websockets`](./packages/transport-websockets) JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec
71-
- [`/packages/transport-webtransport`](./packages/transport-webtransport) JavaScript implementation of the WebTransport module that libp2p uses and that implements the interface-transport spec
72-
- [`/packages/utils`](./packages/utils) Package to aggregate shared logic and dependencies for the libp2p ecosystem
73-
741
<h1 align="center">
752
<a href="https://libp2p.io"><img width="250" src="https://github.com/libp2p/js-libp2p/blob/master/img/libp2p.png?raw=true" alt="libp2p hex logo" /></a>
763
</h1>
@@ -99,6 +26,16 @@
9926
<br>
10027
</p>
10128

29+
# js-libp2p-monorepo <!-- omit in toc -->
30+
31+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
32+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
33+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
34+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)
35+
36+
> JavaScript implementation of libp2p, a modular peer to peer network stack
37+
38+
10239
### Project status
10340

10441
This project has been used in production for years in Ethereum, IPFS, and more. It is actively maintained by multiple organizations and continues to be improved! The API might change, but we strictly follow semver.
@@ -110,6 +47,26 @@ If you are looking for the documentation of the latest release, you can view the
11047

11148
**Want to update libp2p in your project?** Check our [migrations folder](./doc/migrations).
11249

50+
## Table of contents <!-- omit in toc -->
51+
52+
- [Background](#background)
53+
- [Roadmap](#roadmap)
54+
- [Install](#install)
55+
- [Usage](#usage)
56+
- [Configuration](#configuration)
57+
- [Limits](#limits)
58+
- [Getting started](#getting-started)
59+
- [Tutorials and Examples](#tutorials-and-examples)
60+
- [Structure](#structure)
61+
- [Development](#development)
62+
- [Tests](#tests)
63+
- [Run unit tests](#run-unit-tests)
64+
- [Packages](#packages)
65+
- [Used by](#used-by)
66+
- [Contribute](#contribute)
67+
- [API Docs](#api-docs)
68+
- [License](#license)
69+
11370
## Background
11471

11572
libp2p is the product of a long and arduous quest to understand the evolution of the Internet networking stack. In order to build P2P applications, devs have long had to make custom ad-hoc solutions to fit their needs, sometimes making some hard assumptions about their runtimes and the state of the network at the time of their development. Today, looking back more than 20 years, we see a clear pattern in the types of mechanisms built around the Internet Protocol, IP, which can be found throughout many layers of the OSI layer system, libp2p distils these mechanisms into flat categories and defines clear interfaces that once exposed, enable other protocols and applications to use and swap them, enabling upgradability and adaptability for the runtime, without breaking the API.
@@ -159,6 +116,48 @@ If you are starting your journey with `js-libp2p`, read the [GETTING\_STARTED.md
159116

160117
You can find multiple examples on the [examples folder](./examples) that will guide you through using libp2p for several scenarios.
161118

119+
## Structure
120+
121+
- [`/doc`](./doc) Docs for libp2p
122+
- [`/examples/auto-relay`](./examples/auto-relay) Shows how to configure relayed connections
123+
- [`/examples/chat`](./examples/chat) An example chat app using libp2p
124+
- [`/examples/connection-encryption`](./examples/connection-encryption) An example of how to configure connection encrypters
125+
- [`/examples/delegated-routing`](./examples/delegated-routing) How to configure libp2p delegated routers
126+
- [`/examples/discovery-mechanisms`](./examples/discovery-mechanisms) How to configure peer discovery mechanisms
127+
- [`/examples/echo`](./examples/echo) An example echo app
128+
- [`/examples/peer-and-content-routing`](./examples/peer-and-content-routing) How to use peer and content routing
129+
- [`/examples/pnet`](./examples/pnet) How to configure a libp2p private network
130+
- [`/examples/protocol-and-stream-muxing`](./examples/protocol-and-stream-muxing) How to use multiplex protocols streams
131+
- [`/examples/pubsub`](./examples/pubsub) An example using libp2p pubsub
132+
- [`/examples/transports`](./examples/transports) An example using different types of libp2p transport
133+
- [`/interop`](./interop) Multidimension Interop Test
134+
- [`/packages/crypto`](./packages/crypto) Crypto primitives for libp2p
135+
- [`/packages/interface`](./packages/interface) The interface implemented by a libp2p node
136+
- [`/packages/interface-compliance-tests`](./packages/interface-compliance-tests) Compliance tests for JS libp2p interfaces
137+
- [`/packages/interface-internal`](./packages/interface-internal) Interfaces implemented by internal libp2p components
138+
- [`/packages/kad-dht`](./packages/kad-dht) JavaScript implementation of the Kad-DHT for libp2p
139+
- [`/packages/keychain`](./packages/keychain) Key management and cryptographically protected messages
140+
- [`/packages/libp2p`](./packages/libp2p) JavaScript implementation of libp2p, a modular peer to peer network stack
141+
- [`/packages/logger`](./packages/logger) A logging component for use in js-libp2p modules
142+
- [`/packages/metrics-prometheus`](./packages/metrics-prometheus) Collect libp2p metrics for scraping by Prometheus or Graphana
143+
- [`/packages/multistream-select`](./packages/multistream-select) JavaScript implementation of multistream-select
144+
- [`/packages/peer-collections`](./packages/peer-collections) Stores values against a peer id
145+
- [`/packages/peer-discovery-bootstrap`](./packages/peer-discovery-bootstrap) Peer discovery via a list of bootstrap peers
146+
- [`/packages/peer-discovery-mdns`](./packages/peer-discovery-mdns) Node.js libp2p mDNS discovery implementation for peer discovery
147+
- [`/packages/peer-id`](./packages/peer-id) Implementation of @libp2p/interface-peer-id
148+
- [`/packages/peer-id-factory`](./packages/peer-id-factory) Create PeerId instances
149+
- [`/packages/peer-record`](./packages/peer-record) Used to transfer signed peer data across the network
150+
- [`/packages/peer-store`](./packages/peer-store) Stores information about peers libp2p knows on the network
151+
- [`/packages/protocol-perf`](./packages/protocol-perf) Implementation of Perf Protocol
152+
- [`/packages/pubsub`](./packages/pubsub) libp2p pubsub base class
153+
- [`/packages/pubsub-floodsub`](./packages/pubsub-floodsub) libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
154+
- [`/packages/stream-multiplexer-mplex`](./packages/stream-multiplexer-mplex) JavaScript implementation of <https://github.com/libp2p/mplex>
155+
- [`/packages/transport-tcp`](./packages/transport-tcp) A TCP transport for libp2p
156+
- [`/packages/transport-webrtc`](./packages/transport-webrtc) A libp2p transport using WebRTC connections
157+
- [`/packages/transport-websockets`](./packages/transport-websockets) JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec
158+
- [`/packages/transport-webtransport`](./packages/transport-webtransport) JavaScript implementation of the WebTransport module that libp2p uses and that implements the interface-transport spec
159+
- [`/packages/utils`](./packages/utils) Package to aggregate shared logic and dependencies for the libp2p ecosystem
160+
162161
## Development
163162

164163
**Clone and install dependencies:**

packages/libp2p/.aegir.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export default {
2424
const peerId = await createEd25519PeerId()
2525
const libp2p = await createLibp2p({
2626
connectionManager: {
27-
inboundConnectionThreshold: Infinity,
27+
inboundConnectionThreshold: 1000,
28+
maxIncomingPendingConnections: 1000,
29+
maxConnections: 1000,
2830
minConnections: 0
2931
},
3032
addresses: {
@@ -51,7 +53,7 @@ export default {
5153
fetch: fetchService(),
5254
relay: circuitRelayServer({
5355
reservations: {
54-
maxReservations: Infinity
56+
maxReservations: 100000
5557
}
5658
})
5759
}

packages/libp2p/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@
164164
"uint8arraylist": "^2.4.3",
165165
"uint8arrays": "^4.0.6",
166166
"wherearewe": "^2.0.1",
167-
"xsalsa20": "^1.1.0"
167+
"xsalsa20": "^1.1.0",
168+
"yup": "^1.2.0"
168169
},
169170
"devDependencies": {
170171
"@chainsafe/libp2p-gossipsub": "^10.0.0",

packages/libp2p/src/address-manager/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { type ObjectSchema, object, array, string, mixed } from 'yup'
2+
import { validateMultiaddr } from '../config/helpers.js'
3+
import type { AddressManagerInit } from '.'
4+
import type { Multiaddr } from '@multiformats/multiaddr'
5+
16
export function debounce (func: () => void, wait: number): () => void {
27
let timeout: ReturnType<typeof setTimeout> | undefined
38

@@ -11,3 +16,12 @@ export function debounce (func: () => void, wait: number): () => void {
1116
timeout = setTimeout(later, wait)
1217
}
1318
}
19+
20+
export function validateAddressManagerConfig (opts: AddressManagerInit): ObjectSchema<Record<string, unknown>> {
21+
return object({
22+
listen: array().of(string()).test('is multiaddr', validateMultiaddr).default([]),
23+
announce: array().of(string()).test('is multiaddr', validateMultiaddr).default([]),
24+
noAnnounce: array().of(string()).test('is multiaddr', validateMultiaddr).default([]),
25+
announceFilter: mixed().default(() => (addrs: Multiaddr[]): Multiaddr[] => addrs)
26+
})
27+
}

packages/libp2p/src/autonat/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import map from 'it-map'
3131
import parallel from 'it-parallel'
3232
import { pipe } from 'it-pipe'
3333
import isPrivateIp from 'private-ip'
34+
import { number, object, string } from 'yup'
3435
import { codes } from '../errors.js'
3536
import {
3637
MAX_INBOUND_STREAMS,
@@ -108,14 +109,23 @@ class DefaultAutoNATService implements Startable {
108109
private started: boolean
109110

110111
constructor (components: AutoNATComponents, init: AutoNATServiceInit) {
112+
const validatedConfig = object({
113+
protocolPrefix: string().default(PROTOCOL_PREFIX),
114+
timeout: number().integer().default(TIMEOUT),
115+
startupDelay: number().integer().default(STARTUP_DELAY),
116+
refreshInterval: number().integer().default(REFRESH_INTERVAL),
117+
maxInboundStreams: number().integer().default(MAX_INBOUND_STREAMS),
118+
maxOutboundStreams: number().integer().default(MAX_OUTBOUND_STREAMS)
119+
}).validateSync(init)
120+
111121
this.components = components
112122
this.started = false
113-
this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
114-
this.timeout = init.timeout ?? TIMEOUT
115-
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
116-
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
117-
this.startupDelay = init.startupDelay ?? STARTUP_DELAY
118-
this.refreshInterval = init.refreshInterval ?? REFRESH_INTERVAL
123+
this.protocol = `/${validatedConfig.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
124+
this.timeout = validatedConfig.timeout
125+
this.maxInboundStreams = validatedConfig.maxInboundStreams
126+
this.maxOutboundStreams = validatedConfig.maxOutboundStreams
127+
this.startupDelay = validatedConfig.startupDelay
128+
this.refreshInterval = validatedConfig.refreshInterval
119129
this._verifyExternalAddresses = this._verifyExternalAddresses.bind(this)
120130
}
121131

packages/libp2p/src/circuit-relay/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ export const DEFAULT_HOP_TIMEOUT = 30 * second
7070
* How long to wait before starting to advertise the relay service
7171
*/
7272
export const DEFAULT_ADVERT_BOOT_DELAY = 30 * second
73+
74+
/**
75+
* The default timeout for Incoming STOP requests from the relay
76+
*/
77+
export const DEFAULT_STOP_TIMEOUT = 30 * second

packages/libp2p/src/circuit-relay/server/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import { RecordEnvelope } from '@libp2p/peer-record'
66
import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
77
import { pbStream, type ProtobufStream } from 'it-protobuf-stream'
88
import pDefer from 'p-defer'
9+
import { object, number, boolean } from 'yup'
910
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
11+
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from '../../registrar.js'
1012
import {
1113
CIRCUIT_PROTO_CODE,
14+
DEFAULT_DURATION_LIMIT,
1215
DEFAULT_HOP_TIMEOUT,
16+
DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL,
17+
DEFAULT_MAX_RESERVATION_STORE_SIZE,
18+
DEFAULT_MAX_RESERVATION_TTL,
1319
RELAY_SOURCE_TAG,
1420
RELAY_V2_HOP_CODEC,
1521
RELAY_V2_STOP_CODEC
@@ -95,10 +101,6 @@ export interface RelayServerEvents {
95101
'relay:advert:error': CustomEvent<Error>
96102
}
97103

98-
const defaults = {
99-
maxOutboundStopStreams: MAX_CONNECTIONS
100-
}
101-
102104
class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Startable, CircuitRelayService {
103105
private readonly registrar: Registrar
104106
private readonly peerStore: PeerStore
@@ -121,18 +123,32 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
121123
constructor (components: CircuitRelayServerComponents, init: CircuitRelayServerInit = {}) {
122124
super()
123125

126+
const validatedConfig = object({
127+
hopTimeout: number().min(0).integer().default(DEFAULT_HOP_TIMEOUT),
128+
reservations: object({
129+
maxReservations: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_STORE_SIZE),
130+
reservationClearInterval: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL),
131+
applyDefaultLimit: boolean().default(true),
132+
reservationTtl: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_TTL),
133+
defaultDurationLimit: number().integer().min(0).default(DEFAULT_DURATION_LIMIT).max(init?.reservations?.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL, `default duration limit must be less than reservation TTL: ${init?.reservations?.reservationTtl}`)
134+
}),
135+
maxInboundHopStreams: number().integer().min(0).default(DEFAULT_MAX_INBOUND_STREAMS),
136+
maxOutboundHopStreams: number().integer().min(0).default(DEFAULT_MAX_OUTBOUND_STREAMS),
137+
maxOutboundStopStreams: number().integer().min(0).default(MAX_CONNECTIONS)
138+
}).validateSync(init)
139+
124140
this.registrar = components.registrar
125141
this.peerStore = components.peerStore
126142
this.addressManager = components.addressManager
127143
this.peerId = components.peerId
128144
this.connectionManager = components.connectionManager
129145
this.connectionGater = components.connectionGater
130146
this.started = false
131-
this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT
147+
this.hopTimeout = validatedConfig.hopTimeout
132148
this.shutdownController = new AbortController()
133-
this.maxInboundHopStreams = init.maxInboundHopStreams
134-
this.maxOutboundHopStreams = init.maxOutboundHopStreams
135-
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
149+
this.maxInboundHopStreams = validatedConfig.maxInboundHopStreams
150+
this.maxOutboundHopStreams = validatedConfig.maxOutboundHopStreams
151+
this.maxOutboundStopStreams = validatedConfig.maxOutboundStopStreams
136152

137153
try {
138154
// fails on node < 15.4

0 commit comments

Comments
 (0)