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

fix: disable js dht during circuit tests #430

Merged
merged 10 commits into from
Jan 26, 2022
24 changes: 12 additions & 12 deletions test/circuit/v1/browser.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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