Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
fix: disable js dht during circuit tests (#430)
Browse files Browse the repository at this point in the history
There aren't enough peers to use the DHT during circuit tests, so disable it for JS the same way it's disabled for Go.
  • Loading branch information
achingbrain committed Jan 26, 2022
1 parent dd43a59 commit 8a1bbd5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 50 deletions.
24 changes: 12 additions & 12 deletions test/circuit/v1/browser.js
Expand Up @@ -102,11 +102,11 @@ export default {
createProc(['/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star'], factory)
]),
connect: async (nodeA, nodeB, relay) => {
await relay.api.swarm.connect(getWsAddr(nodeA.api.peerId.addresses))
await relay.api.swarm.connect(getWrtcStarAddr(nodeB.api.peerId.addresses))
await relay.api.swarm.connect(await getWsAddr(nodeA.api))
await relay.api.swarm.connect(await getWrtcStarAddr(nodeB.api))
// TODO: needed until https://github.com/ipfs/interop/issues/17 is resolved
await delay(5000)
const nodeBCircuitAddr = `${getWrtcStarAddr(relay.api.peerId.addresses)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
const nodeBCircuitAddr = `${await getWrtcStarAddr(relay.api)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
await nodeA.api.swarm.connect(nodeBCircuitAddr)
},
skip: () => true // go-ipfs does not know what p2p-webrtc-star is
Expand All @@ -118,11 +118,11 @@ export default {
createProc(['/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star'], factory)
]),
connect: async (nodeA, nodeB, relay) => {
await relay.api.swarm.connect(getWsAddr(nodeA.api.peerId.addresses))
await relay.api.swarm.connect(getWrtcStarAddr(nodeB.api.peerId.addresses))
await relay.api.swarm.connect(await getWsAddr(nodeA.api))
await relay.api.swarm.connect(await getWrtcStarAddr(nodeB.api))
// TODO: needed until https://github.com/ipfs/interop/issues/17 is resolved
await delay(3000)
const nodeBCircuitAddr = `${getWrtcStarAddr(relay.api.peerId.addresses)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
const nodeBCircuitAddr = `${await getWrtcStarAddr(relay.api)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
await nodeA.api.swarm.connect(nodeBCircuitAddr)
},
skip: () => isWebWorker // no webrtc support in webworkers
Expand All @@ -134,11 +134,11 @@ export default {
createGo([randomWsAddr], factory)
]),
connect: async (nodeA, nodeB, relay) => {
await relay.api.swarm.connect(getWrtcStarAddr(nodeA.api.peerId.addresses))
await relay.api.swarm.connect(getWsAddr(nodeB.api.peerId.addresses))
await relay.api.swarm.connect(await getWrtcStarAddr(nodeA.api))
await relay.api.swarm.connect(await getWsAddr(nodeB.api))
// TODO: needed until https://github.com/ipfs/interop/issues/17 is resolved
await delay(5000)
const nodeBCircuitAddr = `${getWrtcStarAddr(relay.api.peerId.addresses)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
const nodeBCircuitAddr = `${await getWrtcStarAddr(relay.api)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
await nodeA.api.swarm.connect(nodeBCircuitAddr)
},
skip: () => isWebWorker // no webrtc support in webworkers
Expand All @@ -150,11 +150,11 @@ export default {
createJs([randomWsAddr], factory)
]),
connect: async (nodeA, nodeB, relay) => {
await relay.api.swarm.connect(getWrtcStarAddr(nodeA.api.peerId.addresses))
await relay.api.swarm.connect(getWsAddr(nodeB.api.peerId.addresses))
await relay.api.swarm.connect(await getWrtcStarAddr(nodeA.api))
await relay.api.swarm.connect(await getWsAddr(nodeB.api))
// TODO: needed until https://github.com/ipfs/interop/issues/17 is resolved
await delay(3000)
const nodeBCircuitAddr = `${getWrtcStarAddr(relay.api.peerId.addresses)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
const nodeBCircuitAddr = `${await getWrtcStarAddr(relay.api)}/p2p-circuit/p2p/${nodeB.api.peerId.id}`
await nodeA.api.swarm.connect(nodeBCircuitAddr)
},
skip: () => isWebWorker // no webrtc support in webworkers
Expand Down
107 changes: 70 additions & 37 deletions test/utils/circuit.js
Expand Up @@ -3,6 +3,7 @@ import delay from 'delay'
import randomBytes from 'iso-random-stream/src/random.js'
import concat from 'it-concat'
import WS from 'libp2p-websockets'
import pRetry from 'p-retry'
import filters from 'libp2p-websockets/src/filters.js'
import { expect } from 'aegir/utils/chai.js'

Expand All @@ -21,6 +22,9 @@ export function createProc (addrs, factory, relay) {
Addresses: {
Swarm: addrs
},
Routing: {
Type: 'none'
},
relay: { // FIXME: this is circuit v1, needs support of v2
enabled: true,
hop: {
Expand All @@ -34,6 +38,9 @@ export function createProc (addrs, factory, relay) {
[transportKey]: {
filter: filters.all
}
},
dht: {
enabled: false
}
}
}
Expand All @@ -52,22 +59,32 @@ export function createJs (addrs, factory, relay) {
Addresses: {
Swarm: addrs
},
Routing: {
Type: 'none'
},
relay: { // FIXME: this is circuit v1, needs support of v2
enabled: true,
hop: {
enabled: true
}
},
libp2p: {
config: {
dht: {
enabled: false
}
}
}
}
}
})
}

// creates "private" go-ipfs node which is uses static relay if specified
export function createGo (addrs, factory, relay) {
export async function createGo (addrs, factory, relay) {
let StaticRelays
if (relay) {
StaticRelays = [getWsAddr(relay.api.peerId.addresses)]
StaticRelays = [await getWsAddr(relay.api)]
}
return factory.spawn({
type: 'go',
Expand Down Expand Up @@ -151,58 +168,74 @@ export async function send (nodeA, nodeB) {
expect(buffer.slice()).to.deep.equal(data)
}

export function getWsAddr (addrs) {
addrs = addrs.map((a) => a.toString())
const result = addrs
.find((a) => {
return a.includes('/ws') && !a.includes('/p2p-websocket-star')
})
export async function getWsAddr (api) {
return await pRetry(async () => {
const id = await api.id()

if (!result) {
throw new Error('No ws address found in ' + addrs)
}
const result = id.addresses
.map((a) => a.toString())
.find((a) => {
return a.includes('/ws') && !a.includes('/p2p-websocket-star')
})

if (!result) {
throw new Error(`No ws address found in ${id.addresses}`)
}

return result
return result
})
}

export function getWsStarAddr (addrs) {
addrs = addrs.map((a) => a.toString())
const result = addrs
.find((a) => a.includes('/p2p-websocket-star'))
export async function getWsStarAddr (api) {
return await pRetry(async () => {
const id = await api.id()

if (!result) {
throw new Error('No wsstar address found in ' + addrs)
}
const result = id.addresses
.map((a) => a.toString())
.find((a) => a.includes('/p2p-websocket-star'))

if (!result) {
throw new Error(`No wsstar address found in ${id.addresses}`)
}

return result
return result
})
}

export function getWrtcStarAddr (addrs) {
addrs = addrs.map((a) => a.toString())
const result = addrs
.find((a) => a.includes('/p2p-webrtc-star'))
export async function getWrtcStarAddr (api) {
return await pRetry(async () => {
const id = await api.id()

if (!result) {
throw new Error('No webrtcstar address found in ' + addrs)
}
const result = id.addresses
.map((a) => a.toString())
.find((a) => a.includes('/p2p-webrtc-star'))

return result
if (!result) {
throw new Error(`No webrtcstar address found in ${id.addresses}`)
}

return result
})
}

export function getTcpAddr (addrs) {
addrs = addrs.map((a) => a.toString())
const result = addrs
.find((a) => !a.includes('/ws') && !a.includes('/p2p-websocket-star'))
export async function getTcpAddr (api) {
return await pRetry(async () => {
const id = await api.id()

if (!result) {
throw new Error('No TCP address found in ' + addrs)
}
const result = id.addresses
.map((a) => a.toString())
.find((a) => !a.includes('/ws') && !a.includes('/p2p-websocket-star'))

return result
if (!result) {
throw new Error(`No TCP address found in ${id.addresses}`)
}

return result
})
}

export async function connect (nodeA, nodeB, relay, timeout = 1000) {
const relayWsAddr = getWsAddr(relay.api.peerId.addresses)
const relayWsAddr = await getWsAddr(relay.api)
const nodeAId = nodeA.api.peerId.id
const nodeBId = nodeB.api.peerId.id

Expand Down
8 changes: 7 additions & 1 deletion test/utils/relayd.js
Expand Up @@ -38,7 +38,13 @@ export async function getRelayV (version, factory) {
addresses: [
`${config.Network.ListenAddrs[0]}/p2p/${id}`
]
}
},
id: () => Promise.resolve({
id,
addresses: [
`${config.Network.ListenAddrs[0]}/p2p/${id}`
]
})
}
}
relays.set(version, result)
Expand Down

0 comments on commit 8a1bbd5

Please sign in to comment.