Skip to content

Commit 62b4d65

Browse files
committed
Revert "feat: configuration validation (libp2p#1778)"
This reverts commit e9099d4.
1 parent 8482206 commit 62b4d65

File tree

28 files changed

+162
-338
lines changed

28 files changed

+162
-338
lines changed

packages/libp2p/.aegir.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export default {
2424
const peerId = await createEd25519PeerId()
2525
const libp2p = await createLibp2p({
2626
connectionManager: {
27-
inboundConnectionThreshold: 1000,
28-
maxIncomingPendingConnections: 1000,
29-
maxConnections: 1000,
27+
inboundConnectionThreshold: Infinity,
3028
minConnections: 0
3129
},
3230
addresses: {
@@ -53,7 +51,7 @@ export default {
5351
fetch: fetchService(),
5452
relay: circuitRelayServer({
5553
reservations: {
56-
maxReservations: 100000
54+
maxReservations: Infinity
5755
}
5856
})
5957
}

packages/libp2p/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@
165165
"uint8arraylist": "^2.4.3",
166166
"uint8arrays": "^4.0.6",
167167
"wherearewe": "^2.0.1",
168-
"xsalsa20": "^1.1.0",
169-
"yup": "^1.2.0"
168+
"xsalsa20": "^1.1.0"
170169
},
171170
"devDependencies": {
172171
"@chainsafe/libp2p-gossipsub": "^10.0.0",
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
61
export function debounce (func: () => void, wait: number): () => void {
72
let timeout: ReturnType<typeof setTimeout> | undefined
83

@@ -16,12 +11,3 @@ export function debounce (func: () => void, wait: number): () => void {
1611
timeout = setTimeout(later, wait)
1712
}
1813
}
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: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ 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'
3534
import { codes } from '../errors.js'
3635
import {
3736
MAX_INBOUND_STREAMS,
@@ -109,23 +108,14 @@ class DefaultAutoNATService implements Startable {
109108
private started: boolean
110109

111110
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-
121111
this.components = components
122112
this.started = false
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
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
129119
this._verifyExternalAddresses = this._verifyExternalAddresses.bind(this)
130120
}
131121

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,3 @@ 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: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ 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'
109
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
11-
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from '../../registrar.js'
1210
import {
1311
CIRCUIT_PROTO_CODE,
14-
DEFAULT_DURATION_LIMIT,
1512
DEFAULT_HOP_TIMEOUT,
16-
DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL,
17-
DEFAULT_MAX_RESERVATION_STORE_SIZE,
18-
DEFAULT_MAX_RESERVATION_TTL,
1913
RELAY_SOURCE_TAG,
2014
RELAY_V2_HOP_CODEC,
2115
RELAY_V2_STOP_CODEC
@@ -101,6 +95,10 @@ export interface RelayServerEvents {
10195
'relay:advert:error': CustomEvent<Error>
10296
}
10397

98+
const defaults = {
99+
maxOutboundStopStreams: MAX_CONNECTIONS
100+
}
101+
104102
class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Startable, CircuitRelayService {
105103
private readonly registrar: Registrar
106104
private readonly peerStore: PeerStore
@@ -123,32 +121,18 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
123121
constructor (components: CircuitRelayServerComponents, init: CircuitRelayServerInit = {}) {
124122
super()
125123

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-
140124
this.registrar = components.registrar
141125
this.peerStore = components.peerStore
142126
this.addressManager = components.addressManager
143127
this.peerId = components.peerId
144128
this.connectionManager = components.connectionManager
145129
this.connectionGater = components.connectionGater
146130
this.started = false
147-
this.hopTimeout = validatedConfig.hopTimeout
131+
this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT
148132
this.shutdownController = new AbortController()
149-
this.maxInboundHopStreams = validatedConfig.maxInboundHopStreams
150-
this.maxOutboundHopStreams = validatedConfig.maxOutboundHopStreams
151-
this.maxOutboundStopStreams = validatedConfig.maxOutboundStopStreams
133+
this.maxInboundHopStreams = init.maxInboundHopStreams
134+
this.maxOutboundHopStreams = init.maxOutboundHopStreams
135+
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
152136

153137
try {
154138
// fails on node < 15.4

packages/libp2p/src/circuit-relay/server/reservation-store.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PeerMap } from '@libp2p/peer-collections'
2-
import { object, mixed, number, boolean } from 'yup'
32
import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js'
43
import { type Limit, Status } from '../pb/index.js'
54
import type { RelayReservation } from '../index.js'
@@ -51,21 +50,12 @@ export class ReservationStore implements Startable {
5150
private readonly defaultDataLimit: bigint
5251

5352
constructor (options: ReservationStoreOptions = {}) {
54-
const validatedConfig = object({
55-
maxReservations: number().min(0).integer().default(DEFAULT_MAX_RESERVATION_STORE_SIZE),
56-
reservationClearInterval: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL),
57-
applyDefaultLimit: boolean().default(true),
58-
reservationTtl: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_TTL),
59-
defaultDurationLimit: number().integer().min(0).default(DEFAULT_DURATION_LIMIT),
60-
defaultDataLimit: mixed().test('is-bigint', 'Invalid bigint', value => typeof value === 'bigint').default(DEFAULT_DATA_LIMIT)
61-
}).validateSync(options)
62-
63-
this.maxReservations = validatedConfig.maxReservations
64-
this.reservationClearInterval = validatedConfig.reservationClearInterval
65-
this.applyDefaultLimit = validatedConfig.applyDefaultLimit
66-
this.reservationTtl = validatedConfig.reservationTtl
67-
this.defaultDurationLimit = validatedConfig.defaultDurationLimit
68-
this.defaultDataLimit = validatedConfig.defaultDataLimit as bigint
53+
this.maxReservations = options.maxReservations ?? DEFAULT_MAX_RESERVATION_STORE_SIZE
54+
this.reservationClearInterval = options.reservationClearInterval ?? DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL
55+
this.applyDefaultLimit = options.applyDefaultLimit !== false
56+
this.reservationTtl = options.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL
57+
this.defaultDurationLimit = options.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT
58+
this.defaultDataLimit = options.defaultDataLimit ?? DEFAULT_DATA_LIMIT
6959
}
7060

7161
isStarted (): boolean {

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
66
import * as mafmt from '@multiformats/mafmt'
77
import { multiaddr } from '@multiformats/multiaddr'
88
import { pbStream } from 'it-protobuf-stream'
9-
import { number, object } from 'yup'
109
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
1110
import { codes } from '../../errors.js'
12-
import { CIRCUIT_PROTO_CODE, DEFAULT_STOP_TIMEOUT, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
11+
import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
1312
import { StopMessage, HopMessage, Status } from '../pb/index.js'
1413
import { RelayDiscovery, type RelayDiscoveryComponents } from './discovery.js'
1514
import { createListener } from './listener.js'
@@ -101,6 +100,12 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
101100
reservationCompletionTimeout?: number
102101
}
103102

103+
const defaults = {
104+
maxInboundStopStreams: MAX_CONNECTIONS,
105+
maxOutboundStopStreams: MAX_CONNECTIONS,
106+
stopTimeout: 30000
107+
}
108+
104109
class CircuitRelayTransport implements Transport {
105110
private readonly discovery?: RelayDiscovery
106111
private readonly registrar: Registrar
@@ -111,31 +116,24 @@ class CircuitRelayTransport implements Transport {
111116
private readonly addressManager: AddressManager
112117
private readonly connectionGater: ConnectionGater
113118
private readonly reservationStore: ReservationStore
114-
private readonly maxInboundStopStreams?: number
119+
private readonly maxInboundStopStreams: number
115120
private readonly maxOutboundStopStreams?: number
116-
private readonly stopTimeout?: number
121+
private readonly stopTimeout: number
117122
private started: boolean
118123

119124
constructor (components: CircuitRelayTransportComponents, init: CircuitRelayTransportInit) {
120-
const validatedConfig = object({
121-
discoverRelays: number().min(0).integer().default(0),
122-
maxInboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS),
123-
maxOutboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS),
124-
stopTimeout: number().min(0).integer().default(DEFAULT_STOP_TIMEOUT)
125-
}).validateSync(init)
126-
127125
this.registrar = components.registrar
128126
this.peerStore = components.peerStore
129127
this.connectionManager = components.connectionManager
130128
this.peerId = components.peerId
131129
this.upgrader = components.upgrader
132130
this.addressManager = components.addressManager
133131
this.connectionGater = components.connectionGater
134-
this.maxInboundStopStreams = validatedConfig.maxInboundStopStreams
135-
this.maxOutboundStopStreams = validatedConfig.maxOutboundStopStreams
136-
this.stopTimeout = validatedConfig.stopTimeout
132+
this.maxInboundStopStreams = init.maxInboundStopStreams ?? defaults.maxInboundStopStreams
133+
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
134+
this.stopTimeout = init.stopTimeout ?? defaults.stopTimeout
137135

138-
if (validatedConfig.discoverRelays > 0) {
136+
if (init.discoverRelays != null && init.discoverRelays > 0) {
139137
this.discovery = new RelayDiscovery(components)
140138
this.discovery.addEventListener('relay:discover', (evt) => {
141139
this.reservationStore.addRelay(evt.detail, 'discovered')
@@ -323,7 +321,7 @@ class CircuitRelayTransport implements Transport {
323321
* An incoming STOP request means a remote peer wants to dial us via a relay
324322
*/
325323
async onStop ({ connection, stream }: IncomingStreamData): Promise<void> {
326-
const signal = AbortSignal.timeout(this.stopTimeout ?? DEFAULT_STOP_TIMEOUT)
324+
const signal = AbortSignal.timeout(this.stopTimeout)
327325
const pbstr = pbStream(stream).pb(StopMessage)
328326
const request = await pbstr.read({
329327
signal

packages/libp2p/src/config.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { CodeError } from '@libp2p/interface/errors'
2+
import { FaultTolerance } from '@libp2p/interface/transport'
3+
import { defaultAddressSort } from '@libp2p/utils/address-sort'
4+
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
5+
import mergeOptions from 'merge-options'
6+
import { codes, messages } from './errors.js'
7+
import type { Libp2pInit } from './index.js'
8+
import type { ServiceMap, RecursivePartial } from '@libp2p/interface'
9+
import type { Multiaddr } from '@multiformats/multiaddr'
10+
11+
const DefaultConfig: Partial<Libp2pInit> = {
12+
addresses: {
13+
listen: [],
14+
announce: [],
15+
noAnnounce: [],
16+
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
17+
},
18+
connectionManager: {
19+
resolvers: {
20+
dnsaddr: dnsaddrResolver
21+
},
22+
addressSorter: defaultAddressSort
23+
},
24+
transportManager: {
25+
faultTolerance: FaultTolerance.FATAL_ALL
26+
}
27+
}
28+
29+
export function validateConfig <T extends ServiceMap = Record<string, unknown>> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
30+
const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
31+
32+
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
33+
throw new CodeError(messages.ERR_TRANSPORTS_REQUIRED, codes.ERR_TRANSPORTS_REQUIRED)
34+
}
35+
36+
if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef
37+
throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED)
38+
}
39+
40+
return resultingOptions
41+
}

packages/libp2p/src/config/config.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)