Skip to content

Commit

Permalink
remove unused fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ckousik committed Jan 25, 2023
1 parent 54b8650 commit 97248f7
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 56 deletions.
22 changes: 19 additions & 3 deletions src/circuit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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'
Expand All @@ -16,21 +17,36 @@ import { reserve } from './v2/index.js'
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
import type { Startable } from '@libp2p/interfaces/startable'
import type { Components } from '../components.js'
import type { CircuitServiceConfig } from './index.js'

const log = logger('libp2p:circuit:client')

const noop = () => { }

export interface CircuitServiceInit {
/**
* CircuitServiceInit initializes the circuit service using values
* from the provided config and an @type{AddressSorter}.
*/
export interface CircuitServiceInit extends CircuitServiceConfig {
/**
* Allows prioritizing addresses from the peerstore for dialing. The
* default behavior is to prioritise public addresses.
*/
addressSorter?: AddressSorter
maxReservations?: number
/**
* A callback to invoke when an error occurs in the circuit service.
*/
onError?: (error: Error, msg?: string) => void
}

export interface CircuitServiceEvents {
'relay:reservation': CustomEvent
}

/**
* CircuitService automatically makes a circuit v2 reservation on any connected
* peers that support the circuit v2 HOP protocol.
*/
export class CircuitService extends EventEmitter<CircuitServiceEvents> implements Startable {
private readonly components: Components
private readonly addressSorter: AddressSorter
Expand All @@ -45,7 +61,7 @@ export class CircuitService extends EventEmitter<CircuitServiceEvents> implement
this.started = false
this.components = components
this.addressSorter = init.addressSorter ?? publicAddressesFirst
this.maxReservations = init.maxReservations ?? 1
this.maxReservations = init.maxReservations ?? DEFAULT_MAX_RESERVATIONS
this.relays = new Set()
this.reservationMap = new Map()
this.onError = init.onError ?? noop
Expand Down
5 changes: 5 additions & 0 deletions src/circuit/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const CIRCUIT_PROTO_CODE = 290
* Relay HOP relay service namespace for discovery
*/
export const RELAY_RENDEZVOUS_NS = '/libp2p/relay'

/**
* Maximum reservations for auto relay
*/
export const DEFAULT_MAX_RESERVATIONS = 1
25 changes: 20 additions & 5 deletions src/circuit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ export interface RelayConfig {
enabled: boolean
advertise: RelayAdvertiseConfig
hop: HopConfig
autoRelay: AutoRelayConfig
service: CircuitServiceConfig
}

export interface AutoRelayConfig {
/**
* CircuitServiceConfig allows the node to automatically listen
* on any discovered relays upto a specified maximum.
*/
export interface CircuitServiceConfig {
/**
* enable or disable autorelay (default: false)
*/
enabled?: boolean

/**
* maximum number of relays to listen
* maximum number of relays to listen (default: 1)
*/
maxListeners: number
maxReservations?: number
}

/**
* Configures using the node as a HOP relay
*/
export interface HopConfig {
/**
*
*/
enabled?: boolean
active?: boolean
/**
* timeout for hop requests to complete
*/
timeout: number
}

Expand Down
16 changes: 3 additions & 13 deletions src/circuit/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ import {
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 RelayAdvertiseConfig {
bootDelay?: number
enabled?: boolean
ttl?: number
}

export interface HopConfig {
enabled?: boolean
active?: boolean
}

export interface RelayInit {
addressSorter?: AddressSorter
maxListeners?: number
Expand Down Expand Up @@ -58,8 +48,8 @@ export class Relay implements Startable {
* Start Relay service
*/
async start () {
// Advertise service if HOP enabled
if (this.init.hop.enabled !== false && this.init.advertise.enabled !== false) {
// 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
)
Expand Down
22 changes: 13 additions & 9 deletions src/circuit/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ export class Circuit implements Transport, Startable {
log.error(err)
})

await this.components.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
void this._onV2ProtocolHop(data).catch(err => {
log.error(err)
})
})
.catch(err => {
log.error(err)
if (this._init.hop.enabled === true) {
await this.components.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
void this._onV2ProtocolHop(data).catch(err => {
log.error(err)
})
})
.catch(err => {
log.error(err)
})
}

await this.components.registrar.handle(RELAY_V2_STOP_CODEC, (data) => {
void this._onV2ProtocolStop(data).catch(err => {
log.error(err)
Expand All @@ -105,6 +108,7 @@ export class Circuit implements Transport, Startable {
.catch(err => {
log.error(err)
})

this.reservationStore.start()
}

Expand All @@ -116,11 +120,11 @@ export class Circuit implements Transport, Startable {
}

hopEnabled () {
return true
return this._init.hop.enabled ?? false
}

hopActive () {
return true
return this._init.hop.enabled ?? false
}

get [symbol] (): true {
Expand Down
5 changes: 2 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ const DefaultConfig: Partial<Libp2pInit> = {
},
hop: {
enabled: false,
active: false,
timeout: 30000
},
autoRelay: {
service: {
enabled: false,
maxListeners: 2
maxReservations: 2
}
},
identify: {
Expand Down
4 changes: 2 additions & 2 deletions src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
})
this.configureComponent(this.identifyService)

if (init.relay.autoRelay.enabled === true) {
if (init.relay.service.enabled === true) {
this.circuitService = new CircuitService(this.components, {
addressSorter: init.connectionManager.addressSorter,
...init.relay.autoRelay
...init.relay.service
})
this.services.push(this.circuitService)
}
Expand Down
5 changes: 2 additions & 3 deletions test/circuit/v2/hop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,11 @@ describe('Circuit v2 - hop protocol', function () {
},
hop: {
enabled: true,
active: false,
timeout: 30000
},
autoRelay: {
service: {
enabled: false,
maxListeners: 2
maxReservations: 2
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/dialing/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Dialing (resolvable addresses)', () => {
},
relay: {
enabled: true,
autoRelay: {
service: {
enabled: true
},
hop: {
Expand All @@ -79,7 +79,7 @@ describe('Dialing (resolvable addresses)', () => {
},
relay: {
enabled: true,
autoRelay: {
service: {
enabled: true
},
hop: {
Expand Down
4 changes: 2 additions & 2 deletions test/relay/auto-relay.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ describe('auto-relay', () => {
ttl: 1000,
enabled: true
},
autoRelay: {
service: {
enabled: true,
maxListeners: 1
maxReservations: 1
}
},
contentRouters: [
Expand Down
18 changes: 6 additions & 12 deletions test/relay/relay.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { pEvent } from 'p-event'
import * as sinon from 'sinon'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { RELAY_V2_HOP_CODEC } from '../../src/circuit/multicodec.js'
import { CircuitRelay } from '../../src/circuit/v1/pb/index.js'
import { HopMessage } from '../../src/circuit/v2/pb/index.js'
import { StreamHandlerV2 } from '../../src/circuit/v2/stream-handler.js'
import { codes as Errors } from '../../src/errors.js'
import type { Libp2pNode } from '../../src/libp2p.js'
import { createNode } from '../utils/creators/peer.js'
Expand All @@ -28,7 +25,7 @@ describe('Dialing (via relay, TCP)', () => {
createNode({
config: createNodeOptions({
relay: {
autoRelay: {
service: {
enabled: false
}
}
Expand All @@ -37,7 +34,7 @@ describe('Dialing (via relay, TCP)', () => {
createNode({
config: createRelayOptions({
relay: {
autoRelay: {
service: {
enabled: false
}
}
Expand All @@ -46,7 +43,7 @@ describe('Dialing (via relay, TCP)', () => {
createNode({
config: createNodeOptions({
relay: {
autoRelay: {
service: {
enabled: true
}
}
Expand Down Expand Up @@ -162,14 +159,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_V2_HOP_CODEC)
const streamHandler = new StreamHandlerV2({ stream })
// this should fail as the destination peer has HOP disabled
await expect(connections[0].newStream(RELAY_V2_HOP_CODEC))
.to.be.rejected()
// empty messages are encoded as { type: RESERVE } for the hop codec,
// so we make the message invalid by adding a zeroed byte
streamHandler.write(new Uint8Array([0]))
const res = HopMessage.decode(await streamHandler.read())
expect(res?.status).to.equal(CircuitRelay.Status.MALFORMED_MESSAGE)
streamHandler.close()

// should still be connected
const dstToRelayConn = dstLibp2p.components.connectionManager.getConnections(relayLibp2p.peerId)
Expand Down
4 changes: 2 additions & 2 deletions test/relay/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export function createNodeOptions (...overrides: Libp2pOptions[]): Libp2pOptions
hop: {
enabled: false
},
autoRelay: {
service: {
enabled: true,
maxListeners: 1
maxReservations: 1
}
}
}, ...overrides)
Expand Down

0 comments on commit 97248f7

Please sign in to comment.