From 73f9fc7877aafbdd7061e77514aef1e2f8753328 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 3 Jul 2023 16:57:15 -0500 Subject: [PATCH] feat: added default announce filter (#1573) --- packages/libp2p/src/address-manager/utils.ts | 10 ++++++---- packages/libp2p/src/config/config.ts | 9 +-------- packages/libp2p/src/connection-manager/utils.ts | 2 +- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/libp2p/src/address-manager/utils.ts b/packages/libp2p/src/address-manager/utils.ts index bfcdb6211b..8790e8f1bf 100644 --- a/packages/libp2p/src/address-manager/utils.ts +++ b/packages/libp2p/src/address-manager/utils.ts @@ -1,6 +1,7 @@ -import { type ObjectSchema, object, array, string } from 'yup' +import { type ObjectSchema, object, array, string, mixed } from 'yup' import { validateMultiaddr } from '../config/helpers.js' import type { AddressManagerInit } from '.' +import type { Multiaddr } from '@multiformats/multiaddr' export function debounce (func: () => void, wait: number): () => void { let timeout: ReturnType | undefined @@ -18,8 +19,9 @@ export function debounce (func: () => void, wait: number): () => void { export function validateAddressManagerConfig (opts: AddressManagerInit): ObjectSchema> { return object({ - listen: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), - announce: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), - noAnnounce: array().of(string()).test('is multiaddr', validateMultiaddr).optional() + listen: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), + announce: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), + noAnnounce: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), + announceFilter: mixed().default(() => (addrs: Multiaddr[]): Multiaddr[] => addrs), }) } diff --git a/packages/libp2p/src/config/config.ts b/packages/libp2p/src/config/config.ts index 9b73606680..c570bb83ed 100644 --- a/packages/libp2p/src/config/config.ts +++ b/packages/libp2p/src/config/config.ts @@ -3,7 +3,6 @@ import { publicAddressesFirst } from '@libp2p/utils/address-sort' import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' import mergeOptions from 'merge-options' import type { ServiceMap, RecursivePartial } from '@libp2p/interface' -import type { Multiaddr } from '@multiformats/multiaddr' import type { Libp2pInit, ServiceFactoryMap } from '../index.js' import type { AddressManagerInit } from '../address-manager' import { validateAddressManagerConfig } from '../address-manager/utils.js' @@ -13,12 +12,6 @@ import type { ConnectionManagerInit } from '../connection-manager/index.js' import { validateServicesConfig } from './helpers.js' const DefaultConfig: Partial = { - addresses: { - listen: [], - announce: [], - noAnnounce: [], - announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs - }, connectionManager: { resolvers: { dnsaddr: dnsaddrResolver @@ -33,7 +26,7 @@ const DefaultConfig: Partial = { export function validateConfig > (opts: RecursivePartial>): Libp2pInit { const libp2pConfig = object({ addresses: validateAddressManagerConfig(opts?.addresses as AddressManagerInit), - connectionManager: validateConnectionManagerConfig(opts?.connectionManager as ConnectionManagerInit) + connectionManager: validateConnectionManagerConfig(opts?.connectionManager as ConnectionManagerInit), }) //@ts-expect-error diff --git a/packages/libp2p/src/connection-manager/utils.ts b/packages/libp2p/src/connection-manager/utils.ts index d6604ad897..d63ca22c99 100644 --- a/packages/libp2p/src/connection-manager/utils.ts +++ b/packages/libp2p/src/connection-manager/utils.ts @@ -93,6 +93,6 @@ export const validateConnectionManagerConfig = (opts: ConnectionManagerInit): Ob allow: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), deny: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), inboundConnectionThreshold: number().max(opts?.maxConnections ?? MAX_CONNECTIONS, `inboundConnectionThreshold must be less than the max connections limit: ${opts?.maxConnections}`).integer().default(INBOUND_CONNECTION_THRESHOLD), - maxIncomingPendingConnections: number().integer().max(opts?.maxConnections ?? MAX_CONNECTIONS, `maxIncomingPendingConnections must be less than the max connections limit: ${opts?.maxConnections}`).default(MAX_INCOMING_PENDING_CONNECTIONS) + maxIncomingPendingConnections: number().integer().max(opts?.maxConnections ?? MAX_CONNECTIONS, `maxIncomingPendingConnections must be less than the max connections limit: ${opts?.maxConnections}`).default(MAX_INCOMING_PENDING_CONNECTIONS), }) }