Skip to content

Commit

Permalink
fix: use randomwalk when performing autonat
Browse files Browse the repository at this point in the history
Refactor to reuse the randomwalk component rather than rolling our
own during autonat address verification.
  • Loading branch information
achingbrain committed Jun 6, 2024
1 parent 4bd8e4f commit 3fa33fe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/protocol-autonat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"@libp2p/interface": "^1.4.0",
"@libp2p/interface-internal": "^1.2.2",
"@libp2p/peer-id": "^4.1.2",
"@libp2p/peer-id-factory": "^4.1.2",
"@libp2p/utils": "^5.4.2",
"@multiformats/multiaddr": "^12.2.3",
"it-first": "^3.0.6",
Expand All @@ -68,6 +67,7 @@
},
"devDependencies": {
"@libp2p/logger": "^4.0.13",
"@libp2p/peer-id-factory": "^4.1.2",
"aegir": "^43.0.1",
"it-all": "^3.0.6",
"it-pushable": "^3.2.3",
Expand Down
7 changes: 2 additions & 5 deletions packages/protocol-autonat/src/autonat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { multiaddr, protocols } from '@multiformats/multiaddr'
import first from 'it-first'
Expand Down Expand Up @@ -374,9 +373,6 @@ export class AutoNATService implements Startable {
}
}
})
// find some random peers
const randomPeer = await createEd25519PeerId()
const randomCid = randomPeer.toBytes()

const results: Record<string, { success: number, failure: number }> = {}
const networkSegments: string[] = []
Expand Down Expand Up @@ -449,7 +445,8 @@ export class AutoNATService implements Startable {
}
}

for await (const dialResponse of parallel(map(this.components.peerRouting.getClosestPeers(randomCid, {
// find some random peers
for await (const dialResponse of parallel(map(this.components.randomWalk.walk({
signal
}), (peer) => async () => verifyAddress(peer)), {
concurrency: REQUIRED_SUCCESSFUL_DIALS
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol-autonat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
*/

import { AutoNATService } from './autonat.js'
import type { ComponentLogger, PeerId, PeerRouting } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
import type { ComponentLogger, PeerId } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal'

export interface AutoNATServiceInit {
/**
Expand Down Expand Up @@ -72,8 +72,8 @@ export interface AutoNATComponents {
transportManager: TransportManager
peerId: PeerId
connectionManager: ConnectionManager
peerRouting: PeerRouting
logger: ComponentLogger
randomWalk: RandomWalk
}

export function autoNAT (init: AutoNATServiceInit = {}): (components: AutoNATComponents) => unknown {
Expand Down
24 changes: 12 additions & 12 deletions packages/protocol-autonat/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { AutoNATService } from '../src/autonat.js'
import { PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION } from '../src/constants.js'
import { Message } from '../src/pb/index.js'
import type { AutoNATComponents, AutoNATServiceInit } from '../src/index.js'
import type { Connection, Stream, PeerId, PeerInfo, PeerRouting, Transport } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
import type { Connection, Stream, PeerId, PeerInfo, Transport } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { StubbedInstance } from 'sinon-ts'

Expand All @@ -34,14 +34,14 @@ const defaultInit: AutoNATServiceInit = {
describe('autonat', () => {
let service: any
let components: AutoNATComponents
let peerRouting: StubbedInstance<PeerRouting>
let randomWalk: StubbedInstance<RandomWalk>
let registrar: StubbedInstance<Registrar>
let addressManager: StubbedInstance<AddressManager>
let connectionManager: StubbedInstance<ConnectionManager>
let transportManager: StubbedInstance<TransportManager>

beforeEach(async () => {
peerRouting = stubInterface<PeerRouting>()
randomWalk = stubInterface<RandomWalk>()
registrar = stubInterface<Registrar>()
addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([])
Expand All @@ -52,7 +52,7 @@ describe('autonat', () => {
components = {
peerId: await createEd25519PeerId(),
logger: defaultLogger(),
peerRouting,
randomWalk,
registrar,
addressManager,
connectionManager,
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -156,7 +156,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -195,7 +195,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -240,7 +240,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -287,7 +287,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -339,7 +339,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down Expand Up @@ -372,7 +372,7 @@ describe('autonat', () => {
})
]

peerRouting.getClosestPeers.returns(async function * () {
randomWalk.walk.returns(async function * () {
yield * peers
}())

Expand Down

0 comments on commit 3fa33fe

Please sign in to comment.