Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ckousik committed Jan 24, 2023
1 parent 4a740d3 commit 99cd723
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"test:firefox": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
"test:examples": "cd examples && npm run test:all",
"test:interop": "aegir test -t node -f dist/test/interop.js"
"test:interop": "aegir test -t node -f dist/test/interop.js",
"test:relay": "aegir test -t node -f \"./dist/test/**/auto-relay.{node,spec}.js\" --cov"
},
"dependencies": {
"@achingbrain/nat-port-mapper": "^1.0.3",
Expand Down
7 changes: 4 additions & 3 deletions src/circuit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ export class CircuitService extends EventEmitter<CircuitServiceEvents> implement
const knownHopsToDial: PeerId[] = []
const peers = (await this.components.peerStore.all())
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
.filter(({ id, protocols }) =>
protocols.includes(RELAY_V2_HOP_CODEC) && !this.relays.has(id.toString()) && peersToIgnore.includes(id.toString()))
.filter(({ id, protocols }) => {
const idString = id.toString()
return protocols.includes(RELAY_V2_HOP_CODEC) && !this.relays.has(idString) && !peersToIgnore.includes(idString)
})
.map(({ id }) => {
const connections = this.components.connectionManager.getConnections(id)
if (connections.length === 0) {
Expand All @@ -246,7 +248,6 @@ export class CircuitService extends EventEmitter<CircuitServiceEvents> implement
}
return [id, connections[0]]
})
.filter(([id, conn]) => { conn })
.sort(() => Math.random() - 0.5)

// Check if we have known hop peers to use and attempt to listen on the already connected
Expand Down
12 changes: 7 additions & 5 deletions src/circuit/v2/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { logger } from '@libp2p/logger'
import { StreamHandlerV2 } from './stream-handler.js'
import { RELAY_V2_STOP_CODEC } from '../multicodec.js'
import { validateStopConnectRequest } from './validation.js'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Duplex } from 'it-stream-types'

const log = logger('libp2p:circuit:v2:stop')

Expand All @@ -24,7 +26,7 @@ export async function handleStop ({
// Validate the STOP request has the required input
try {
validateStopConnectRequest(request, streamHandler)
} catch (/** @type {any} */ err) {
} catch (err) {
return log.error('invalid stop connect request via peer %s', connection.remotePeer, err)
}
log('stop request is valid')
Expand Down Expand Up @@ -54,21 +56,21 @@ export interface StopOptions {
export async function stop ({
connection,
request
}: StopOptions) {
}: StopOptions): Promise<Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> | undefined> {
const stream = await connection.newStream([RELAY_V2_STOP_CODEC])
log('starting circuit relay v2 stop request to %s', connection.remotePeer)
const streamHandler = new StreamHandlerV2({ stream })
streamHandler.write(StopMessage.encode(request))
let response
try {
response = StopMessage.decode(await streamHandler.read())
} catch (/** @type {any} */ err) {
} catch (err) {
log.error('error parsing stop message response from %s', connection.remotePeer)
}

if (response == null) {
streamHandler.close()
return undefined
return
}
if (response.status === Status.OK) {
log('stop request to %s was successful', connection.remotePeer)
Expand All @@ -77,5 +79,5 @@ export async function stop ({

log('stop request failed with code %d', response.status)
streamHandler.close()
return undefined
return
}
2 changes: 2 additions & 0 deletions test/relay/auto-relay.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ describe('auto-relay', () => {
// Disconnect from peer used for relay
const disconnectPromise = pEvent(relayLibp2p1.connectionManager, 'peer:disconnect', { timeout: 500 })
await relayLibp2p2.stop()
console.log('disconnected')
const event = await disconnectPromise
expect(event.detail.remotePeer.toString()).to.equal(relayLibp2p2.peerId.toString())

Expand All @@ -210,6 +211,7 @@ describe('auto-relay', () => {
timeout: 1000
})).to.eventually.be.rejected()

console.log('abc')
// Wait for other peer connected to be added as listen addr
await usingAsRelay(relayLibp2p1, relayLibp2p3)
})
Expand Down
7 changes: 0 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,4 @@
"src",
"test"
],
"exclude": [
"src/circuit/v1/pb/index.js",
"src/circuit/v2/pb/index.js",
"src/fetch/pb/proto.js",
"src/identify/pb/message.js",
"src/insecure/pb/proto.js"
]
}

0 comments on commit 99cd723

Please sign in to comment.