mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-08 23:11:19 +00:00
fix: remove ipfs dependency and upgrade multiaddr (#1387)
- Upgrades @multiformats/multiaddr to 11.0.0 - Removes ipfs-http-client and delegate router dependencies - Test delegation using interface stubs instead of implementations
This commit is contained in:
parent
a4566ede92
commit
633d4a9740
@ -1,6 +1,6 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { createLibp2p } from './libp2p.js'
|
import { createLibp2p } from './libp2p.js'
|
||||||
import { stdinToStream, streamToConsole } from './stream.js'
|
import { stdinToStream, streamToConsole } from './stream.js'
|
||||||
import { createFromJSON } from '@libp2p/peer-id-factory'
|
import { createFromJSON } from '@libp2p/peer-id-factory'
|
||||||
@ -31,7 +31,7 @@ async function run () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Dial to the remote peer (the "listener")
|
// Dial to the remote peer (the "listener")
|
||||||
const listenerMa = new Multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toString()}`)
|
const listenerMa = multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toString()}`)
|
||||||
const stream = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
|
const stream = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
|
||||||
|
|
||||||
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
|
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
|
||||||
|
@ -3,15 +3,15 @@
|
|||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@chainsafe/libp2p-noise": "^8.0.0",
|
"@chainsafe/libp2p-noise": "^8.0.1",
|
||||||
"ipfs-core": "^0.15.4",
|
"ipfs-core": "^0.15.4",
|
||||||
"libp2p": "../../",
|
"libp2p": "../../",
|
||||||
"@libp2p/delegated-content-routing": "^2.0.1",
|
"@libp2p/delegated-content-routing": "^2.0.1",
|
||||||
"@libp2p/delegated-peer-routing": "^2.0.1",
|
"@libp2p/delegated-peer-routing": "^2.0.1",
|
||||||
"@libp2p/kad-dht": "^3.0.0",
|
"@libp2p/kad-dht": "^3.0.0",
|
||||||
"@libp2p/mplex": "^5.2.1",
|
"@libp2p/mplex": "^5.2.3",
|
||||||
"@libp2p/webrtc-star": "^3.0.0",
|
"@libp2p/webrtc-star": "^3.0.3",
|
||||||
"@libp2p/websockets": "^3.0.0",
|
"@libp2p/websockets": "^3.0.4",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"react-scripts": "5.0.0"
|
"react-scripts": "5.0.0"
|
||||||
|
@ -9,11 +9,11 @@
|
|||||||
},
|
},
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@chainsafe/libp2p-noise": "^8.0.0",
|
"@chainsafe/libp2p-noise": "^8.0.1",
|
||||||
"@libp2p/bootstrap": "^2.0.0",
|
"@libp2p/bootstrap": "^2.0.1",
|
||||||
"@libp2p/mplex": "^5.2.1",
|
"@libp2p/mplex": "^5.2.3",
|
||||||
"@libp2p/webrtc-star": "^3.0.0",
|
"@libp2p/webrtc-star": "^3.0.3",
|
||||||
"@libp2p/websockets": "^3.0.0",
|
"@libp2p/websockets": "^3.0.4",
|
||||||
"libp2p": "../../"
|
"libp2p": "../../"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -10,9 +10,9 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@libp2p/webrtc-direct": "^2.0.0",
|
"@libp2p/webrtc-direct": "^2.0.0",
|
||||||
"@chainsafe/libp2p-noise": "^8.0.0",
|
"@chainsafe/libp2p-noise": "^8.0.1",
|
||||||
"@libp2p/bootstrap": "^2.0.0",
|
"@libp2p/bootstrap": "^2.0.1",
|
||||||
"@libp2p/mplex": "^5.2.1",
|
"@libp2p/mplex": "^5.2.3",
|
||||||
"libp2p": "../../",
|
"libp2p": "../../",
|
||||||
"wrtc": "^0.4.7"
|
"wrtc": "^0.4.7"
|
||||||
},
|
},
|
||||||
|
63
package.json
63
package.json
@ -99,36 +99,36 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@achingbrain/nat-port-mapper": "^1.0.3",
|
"@achingbrain/nat-port-mapper": "^1.0.3",
|
||||||
"@libp2p/components": "^2.1.0",
|
"@libp2p/components": "^2.1.0",
|
||||||
"@libp2p/connection": "^4.0.1",
|
"@libp2p/connection": "^4.0.2",
|
||||||
"@libp2p/crypto": "^1.0.3",
|
"@libp2p/crypto": "^1.0.4",
|
||||||
"@libp2p/interface-address-manager": "^1.0.2",
|
"@libp2p/interface-address-manager": "^1.0.3",
|
||||||
"@libp2p/interface-connection": "^3.0.1",
|
"@libp2p/interface-connection": "^3.0.2",
|
||||||
"@libp2p/interface-connection-encrypter": "^2.0.1",
|
"@libp2p/interface-connection-encrypter": "^2.0.1",
|
||||||
"@libp2p/interface-connection-manager": "^1.1.0",
|
"@libp2p/interface-connection-manager": "^1.1.1",
|
||||||
"@libp2p/interface-content-routing": "^1.0.2",
|
"@libp2p/interface-content-routing": "^1.0.2",
|
||||||
"@libp2p/interface-dht": "^1.0.1",
|
"@libp2p/interface-dht": "^1.0.1",
|
||||||
"@libp2p/interface-metrics": "^3.0.0",
|
"@libp2p/interface-metrics": "^3.0.0",
|
||||||
"@libp2p/interface-peer-discovery": "^1.0.1",
|
"@libp2p/interface-peer-discovery": "^1.0.1",
|
||||||
"@libp2p/interface-peer-id": "^1.0.4",
|
"@libp2p/interface-peer-id": "^1.0.4",
|
||||||
"@libp2p/interface-peer-info": "^1.0.2",
|
"@libp2p/interface-peer-info": "^1.0.3",
|
||||||
"@libp2p/interface-peer-routing": "^1.0.1",
|
"@libp2p/interface-peer-routing": "^1.0.1",
|
||||||
"@libp2p/interface-peer-store": "^1.2.1",
|
"@libp2p/interface-peer-store": "^1.2.2",
|
||||||
"@libp2p/interface-pubsub": "^2.0.1",
|
"@libp2p/interface-pubsub": "^2.1.0",
|
||||||
"@libp2p/interface-registrar": "^2.0.3",
|
"@libp2p/interface-registrar": "^2.0.3",
|
||||||
"@libp2p/interface-stream-muxer": "^2.0.2",
|
"@libp2p/interface-stream-muxer": "^2.0.2",
|
||||||
"@libp2p/interface-transport": "^1.0.3",
|
"@libp2p/interface-transport": "^1.0.4",
|
||||||
"@libp2p/interfaces": "^3.0.3",
|
"@libp2p/interfaces": "^3.0.3",
|
||||||
"@libp2p/logger": "^2.0.1",
|
"@libp2p/logger": "^2.0.1",
|
||||||
"@libp2p/multistream-select": "^3.0.0",
|
"@libp2p/multistream-select": "^3.0.0",
|
||||||
"@libp2p/peer-collections": "^2.0.0",
|
"@libp2p/peer-collections": "^2.0.0",
|
||||||
"@libp2p/peer-id": "^1.1.15",
|
"@libp2p/peer-id": "^1.1.15",
|
||||||
"@libp2p/peer-id-factory": "^1.0.18",
|
"@libp2p/peer-id-factory": "^1.0.18",
|
||||||
"@libp2p/peer-record": "^4.0.2",
|
"@libp2p/peer-record": "^4.0.3",
|
||||||
"@libp2p/peer-store": "^3.1.3",
|
"@libp2p/peer-store": "^3.1.5",
|
||||||
"@libp2p/tracked-map": "^2.0.1",
|
"@libp2p/tracked-map": "^2.0.1",
|
||||||
"@libp2p/utils": "^3.0.1",
|
"@libp2p/utils": "^3.0.2",
|
||||||
"@multiformats/mafmt": "^11.0.2",
|
"@multiformats/mafmt": "^11.0.2",
|
||||||
"@multiformats/multiaddr": "^10.3.3",
|
"@multiformats/multiaddr": "^11.0.0",
|
||||||
"abortable-iterator": "^4.0.2",
|
"abortable-iterator": "^4.0.2",
|
||||||
"any-signal": "^3.0.0",
|
"any-signal": "^3.0.0",
|
||||||
"datastore-core": "^8.0.1",
|
"datastore-core": "^8.0.1",
|
||||||
@ -168,26 +168,24 @@
|
|||||||
"xsalsa20": "^1.1.0"
|
"xsalsa20": "^1.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@chainsafe/libp2p-noise": "^8.0.0",
|
"@chainsafe/libp2p-noise": "^8.0.1",
|
||||||
"@chainsafe/libp2p-yamux": "^1.0.0",
|
"@chainsafe/libp2p-yamux": "^1.0.0",
|
||||||
"@libp2p/bootstrap": "^2.0.0",
|
"@libp2p/bootstrap": "^2.0.1",
|
||||||
"@libp2p/daemon-client": "^3.0.0",
|
"@libp2p/daemon-client": "^3.0.1",
|
||||||
"@libp2p/daemon-server": "^3.0.0",
|
"@libp2p/daemon-server": "^3.0.1",
|
||||||
"@libp2p/delegated-content-routing": "^2.0.1",
|
|
||||||
"@libp2p/delegated-peer-routing": "^2.0.1",
|
|
||||||
"@libp2p/floodsub": "^3.0.0",
|
"@libp2p/floodsub": "^3.0.0",
|
||||||
"@libp2p/interface-compliance-tests": "^3.0.1",
|
"@libp2p/interface-compliance-tests": "^3.0.2",
|
||||||
"@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.1",
|
"@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.2",
|
||||||
"@libp2p/interface-mocks": "^4.0.1",
|
"@libp2p/interface-mocks": "^4.0.3",
|
||||||
"@libp2p/interop": "^3.0.0",
|
"@libp2p/interop": "^3.0.1",
|
||||||
"@libp2p/kad-dht": "^3.0.4",
|
"@libp2p/kad-dht": "^3.0.5",
|
||||||
"@libp2p/mdns": "^3.0.0",
|
"@libp2p/mdns": "^3.0.1",
|
||||||
"@libp2p/mplex": "^5.2.1",
|
"@libp2p/mplex": "^5.2.3",
|
||||||
"@libp2p/pubsub": "^3.0.1",
|
"@libp2p/pubsub": "^3.1.3",
|
||||||
"@libp2p/tcp": "^3.0.5",
|
"@libp2p/tcp": "^3.1.1",
|
||||||
"@libp2p/topology": "^3.0.0",
|
"@libp2p/topology": "^3.0.1",
|
||||||
"@libp2p/webrtc-star": "^3.0.0",
|
"@libp2p/webrtc-star": "^3.0.3",
|
||||||
"@libp2p/websockets": "^3.0.0",
|
"@libp2p/websockets": "^3.0.4",
|
||||||
"@types/node-forge": "^1.0.0",
|
"@types/node-forge": "^1.0.0",
|
||||||
"@types/p-fifo": "^1.0.0",
|
"@types/p-fifo": "^1.0.0",
|
||||||
"@types/varint": "^6.0.0",
|
"@types/varint": "^6.0.0",
|
||||||
@ -197,11 +195,8 @@
|
|||||||
"delay": "^5.0.0",
|
"delay": "^5.0.0",
|
||||||
"execa": "^6.1.0",
|
"execa": "^6.1.0",
|
||||||
"go-libp2p": "^0.0.6",
|
"go-libp2p": "^0.0.6",
|
||||||
"into-stream": "^7.0.0",
|
|
||||||
"ipfs-http-client": "^58.0.0",
|
|
||||||
"it-pushable": "^3.0.0",
|
"it-pushable": "^3.0.0",
|
||||||
"it-to-buffer": "^2.0.2",
|
"it-to-buffer": "^2.0.2",
|
||||||
"nock": "^13.0.3",
|
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"p-defer": "^4.0.0",
|
"p-defer": "^4.0.0",
|
||||||
"p-event": "^5.0.1",
|
"p-event": "^5.0.1",
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import type { AddressManagerEvents } from '@libp2p/interface-address-manager'
|
import type { AddressManagerEvents } from '@libp2p/interface-address-manager'
|
||||||
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { peerIdFromString } from '@libp2p/peer-id'
|
import { peerIdFromString } from '@libp2p/peer-id'
|
||||||
import type { Components } from '@libp2p/components'
|
import type { Components } from '@libp2p/components'
|
||||||
|
|
||||||
@ -58,28 +59,28 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|||||||
* Get peer listen multiaddrs
|
* Get peer listen multiaddrs
|
||||||
*/
|
*/
|
||||||
getListenAddrs (): Multiaddr[] {
|
getListenAddrs (): Multiaddr[] {
|
||||||
return Array.from(this.listen).map((a) => new Multiaddr(a))
|
return Array.from(this.listen).map((a) => multiaddr(a))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get peer announcing multiaddrs
|
* Get peer announcing multiaddrs
|
||||||
*/
|
*/
|
||||||
getAnnounceAddrs (): Multiaddr[] {
|
getAnnounceAddrs (): Multiaddr[] {
|
||||||
return Array.from(this.announce).map((a) => new Multiaddr(a))
|
return Array.from(this.announce).map((a) => multiaddr(a))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get observed multiaddrs
|
* Get observed multiaddrs
|
||||||
*/
|
*/
|
||||||
getObservedAddrs (): Multiaddr[] {
|
getObservedAddrs (): Multiaddr[] {
|
||||||
return Array.from(this.observed).map((a) => new Multiaddr(a))
|
return Array.from(this.observed).map((a) => multiaddr(a))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add peer observed addresses
|
* Add peer observed addresses
|
||||||
*/
|
*/
|
||||||
addObservedAddr (addr: string | Multiaddr): void {
|
addObservedAddr (addr: string | Multiaddr): void {
|
||||||
let ma = new Multiaddr(addr)
|
let ma = multiaddr(addr)
|
||||||
const remotePeer = ma.getPeerId()
|
const remotePeer = ma.getPeerId()
|
||||||
|
|
||||||
// strip our peer id if it has been passed
|
// strip our peer id if it has been passed
|
||||||
@ -88,7 +89,7 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|||||||
|
|
||||||
// use same encoding for comparison
|
// use same encoding for comparison
|
||||||
if (remotePeerId.equals(this.components.getPeerId())) {
|
if (remotePeerId.equals(this.components.getPeerId())) {
|
||||||
ma = ma.decapsulate(new Multiaddr(`/p2p/${this.components.getPeerId().toString()}`))
|
ma = ma.decapsulate(multiaddr(`/p2p/${this.components.getPeerId().toString()}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +119,7 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|||||||
|
|
||||||
// Create advertising list
|
// Create advertising list
|
||||||
return this.announceFilter(Array.from(addrSet)
|
return this.announceFilter(Array.from(addrSet)
|
||||||
.map(str => new Multiaddr(str)))
|
.map(str => multiaddr(str)))
|
||||||
.map(ma => {
|
.map(ma => {
|
||||||
if (ma.getPeerId() === this.components.getPeerId().toString()) {
|
if (ma.getPeerId() === this.components.getPeerId().toString()) {
|
||||||
return ma
|
return ma
|
||||||
|
@ -37,7 +37,7 @@ Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGU
|
|||||||
Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:
|
Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import Libp2p from 'libp2p'
|
import Libp2p from 'libp2p'
|
||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { Mplex } from '@libp2p/mplex'
|
import { Mplex } from '@libp2p/mplex'
|
||||||
@ -47,7 +47,7 @@ const relayAddr = ...
|
|||||||
|
|
||||||
const node = await createLibp2p({
|
const node = await createLibp2p({
|
||||||
addresses: {
|
addresses: {
|
||||||
listen: [new Multiaddr(`${relayAddr}/p2p-circuit`)]
|
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
|
||||||
},
|
},
|
||||||
transports: [
|
transports: [
|
||||||
new TCP()
|
new TCP()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { CircuitRelay } from '../pb/index.js'
|
import { CircuitRelay } from '../pb/index.js'
|
||||||
import type { StreamHandler } from './stream-handler.js'
|
import type { StreamHandler } from './stream-handler.js'
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler)
|
|||||||
try {
|
try {
|
||||||
if (msg.dstPeer?.addrs != null) {
|
if (msg.dstPeer?.addrs != null) {
|
||||||
msg.dstPeer.addrs.forEach((addr) => {
|
msg.dstPeer.addrs.forEach((addr) => {
|
||||||
return new Multiaddr(addr)
|
return multiaddr(addr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@ -32,7 +32,7 @@ export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler)
|
|||||||
try {
|
try {
|
||||||
if (msg.srcPeer?.addrs != null) {
|
if (msg.srcPeer?.addrs != null) {
|
||||||
msg.srcPeer.addrs.forEach((addr) => {
|
msg.srcPeer.addrs.forEach((addr) => {
|
||||||
return new Multiaddr(addr)
|
return multiaddr(addr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
@ -3,7 +3,8 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|||||||
import type { PeerStore } from '@libp2p/interface-peer-store'
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
||||||
import type { Listener } from '@libp2p/interface-transport'
|
import type { Listener } from '@libp2p/interface-transport'
|
||||||
import { peerIdFromString } from '@libp2p/peer-id'
|
import { peerIdFromString } from '@libp2p/peer-id'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
|
|
||||||
export interface ListenerOptions {
|
export interface ListenerOptions {
|
||||||
peerStore: PeerStore
|
peerStore: PeerStore
|
||||||
@ -18,7 +19,7 @@ export function createListener (options: ListenerOptions): Listener {
|
|||||||
*/
|
*/
|
||||||
async function listen (addr: Multiaddr): Promise<void> {
|
async function listen (addr: Multiaddr): Promise<void> {
|
||||||
const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
|
const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
|
||||||
const ma = new Multiaddr(addrString)
|
const ma = multiaddr(addrString)
|
||||||
|
|
||||||
const relayPeerStr = ma.getPeerId()
|
const relayPeerStr = ma.getPeerId()
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { logger } from '@libp2p/logger'
|
import { logger } from '@libp2p/logger'
|
||||||
import errCode from 'err-code'
|
import errCode from 'err-code'
|
||||||
import * as mafmt from '@multiformats/mafmt'
|
import * as mafmt from '@multiformats/mafmt'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { CircuitRelay as CircuitPB } from './pb/index.js'
|
import { CircuitRelay as CircuitPB } from './pb/index.js'
|
||||||
import { codes } from '../errors.js'
|
import { codes } from '../errors.js'
|
||||||
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
|
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
|
||||||
@ -134,8 +135,8 @@ export class Circuit implements Transport, Initializable {
|
|||||||
if (virtualConnection != null) {
|
if (virtualConnection != null) {
|
||||||
const remoteAddr = connection.remoteAddr
|
const remoteAddr = connection.remoteAddr
|
||||||
.encapsulate('/p2p-circuit')
|
.encapsulate('/p2p-circuit')
|
||||||
.encapsulate(new Multiaddr(request.dstPeer?.addrs[0]))
|
.encapsulate(multiaddr(request.dstPeer?.addrs[0]))
|
||||||
const localAddr = new Multiaddr(request.srcPeer?.addrs[0])
|
const localAddr = multiaddr(request.srcPeer?.addrs[0])
|
||||||
const maConn = streamToMaConnection({
|
const maConn = streamToMaConnection({
|
||||||
stream: virtualConnection,
|
stream: virtualConnection,
|
||||||
remoteAddr,
|
remoteAddr,
|
||||||
@ -162,8 +163,8 @@ export class Circuit implements Transport, Initializable {
|
|||||||
async dial (ma: Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
async dial (ma: Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
||||||
// Check the multiaddr to see if it contains a relay and a destination peer
|
// Check the multiaddr to see if it contains a relay and a destination peer
|
||||||
const addrs = ma.toString().split('/p2p-circuit')
|
const addrs = ma.toString().split('/p2p-circuit')
|
||||||
const relayAddr = new Multiaddr(addrs[0])
|
const relayAddr = multiaddr(addrs[0])
|
||||||
const destinationAddr = new Multiaddr(addrs[addrs.length - 1])
|
const destinationAddr = multiaddr(addrs[addrs.length - 1])
|
||||||
const relayId = relayAddr.getPeerId()
|
const relayId = relayAddr.getPeerId()
|
||||||
const destinationId = destinationAddr.getPeerId()
|
const destinationId = destinationAddr.getPeerId()
|
||||||
|
|
||||||
@ -198,7 +199,7 @@ export class Circuit implements Transport, Initializable {
|
|||||||
},
|
},
|
||||||
dstPeer: {
|
dstPeer: {
|
||||||
id: destinationPeer.toBytes(),
|
id: destinationPeer.toBytes(),
|
||||||
addrs: [new Multiaddr(destinationAddr).bytes]
|
addrs: [multiaddr(destinationAddr).bytes]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -3,7 +3,8 @@ import all from 'it-all'
|
|||||||
import filter from 'it-filter'
|
import filter from 'it-filter'
|
||||||
import { pipe } from 'it-pipe'
|
import { pipe } from 'it-pipe'
|
||||||
import errCode from 'err-code'
|
import errCode from 'err-code'
|
||||||
import { Multiaddr, Resolver } from '@multiformats/multiaddr'
|
import type { Multiaddr, Resolver } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr, resolvers } from '@multiformats/multiaddr'
|
||||||
import { TimeoutController } from 'timeout-abort-controller'
|
import { TimeoutController } from 'timeout-abort-controller'
|
||||||
import { AbortError } from '@libp2p/interfaces/errors'
|
import { AbortError } from '@libp2p/interfaces/errors'
|
||||||
import { anySignal } from 'any-signal'
|
import { anySignal } from 'any-signal'
|
||||||
@ -117,7 +118,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(init.resolvers ?? {})) {
|
for (const [key, value] of Object.entries(init.resolvers ?? {})) {
|
||||||
Multiaddr.resolvers.set(key, value)
|
resolvers.set(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,7 +372,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|||||||
*/
|
*/
|
||||||
async _resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
async _resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
||||||
try {
|
try {
|
||||||
ma = new Multiaddr(ma.toString()) // Use current multiaddr module
|
ma = multiaddr(ma.toString()) // Use current multiaddr module
|
||||||
const multiaddrs = await ma.resolve(options)
|
const multiaddrs = await ma.resolve(options)
|
||||||
return multiaddrs
|
return multiaddrs
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { peerIdFromString } from '@libp2p/peer-id'
|
import { peerIdFromString } from '@libp2p/peer-id'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr, isMultiaddr } from '@multiformats/multiaddr'
|
||||||
import errCode from 'err-code'
|
import errCode from 'err-code'
|
||||||
import { codes } from './errors.js'
|
import { codes } from './errors.js'
|
||||||
import { isPeerId } from '@libp2p/interface-peer-id'
|
import { isPeerId } from '@libp2p/interface-peer-id'
|
||||||
@ -39,12 +40,12 @@ export function getPeer (peer: PeerId | Multiaddr | string): PeerInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof peer === 'string') {
|
if (typeof peer === 'string') {
|
||||||
peer = new Multiaddr(peer)
|
peer = multiaddr(peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr
|
let addr
|
||||||
|
|
||||||
if (Multiaddr.isMultiaddr(peer)) {
|
if (isMultiaddr(peer)) {
|
||||||
addr = peer
|
addr = peer
|
||||||
peer = peerIdFromMultiaddr(peer)
|
peer = peerIdFromMultiaddr(peer)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import { pipe } from 'it-pipe'
|
|||||||
import drain from 'it-drain'
|
import drain from 'it-drain'
|
||||||
import first from 'it-first'
|
import first from 'it-first'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { Multiaddr, protocols } from '@multiformats/multiaddr'
|
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
||||||
import { Identify } from './pb/message.js'
|
import { Identify } from './pb/message.js'
|
||||||
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
||||||
import {
|
import {
|
||||||
@ -354,7 +354,7 @@ export class IdentifyService implements Startable {
|
|||||||
|
|
||||||
// LEGACY: Update peers data in PeerStore
|
// LEGACY: Update peers data in PeerStore
|
||||||
try {
|
try {
|
||||||
await this.components.getPeerStore().addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
|
await this.components.getPeerStore().addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
log.error('received invalid addrs', err)
|
log.error('received invalid addrs', err)
|
||||||
}
|
}
|
||||||
@ -506,7 +506,7 @@ export class IdentifyService implements Startable {
|
|||||||
// LEGACY: Update peers data in PeerStore
|
// LEGACY: Update peers data in PeerStore
|
||||||
try {
|
try {
|
||||||
await this.components.getPeerStore().addressBook.set(id,
|
await this.components.getPeerStore().addressBook.set(id,
|
||||||
message.listenAddrs.map((addr) => new Multiaddr(addr)))
|
message.listenAddrs.map((addr) => multiaddr(addr)))
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
log.error('received invalid addrs', err)
|
log.error('received invalid addrs', err)
|
||||||
}
|
}
|
||||||
@ -527,7 +527,7 @@ export class IdentifyService implements Startable {
|
|||||||
static getCleanMultiaddr (addr: Uint8Array | string | null | undefined) {
|
static getCleanMultiaddr (addr: Uint8Array | string | null | undefined) {
|
||||||
if (addr != null && addr.length > 0) {
|
if (addr != null && addr.length > 0) {
|
||||||
try {
|
try {
|
||||||
return new Multiaddr(addr)
|
return multiaddr(addr)
|
||||||
} catch {
|
} catch {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { upnpNat, NatAPI } from '@achingbrain/nat-port-mapper'
|
import { upnpNat, NatAPI } from '@achingbrain/nat-port-mapper'
|
||||||
import { logger } from '@libp2p/logger'
|
import { logger } from '@libp2p/logger'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { fromNodeAddress } from '@multiformats/multiaddr'
|
||||||
import { isBrowser } from 'wherearewe'
|
import { isBrowser } from 'wherearewe'
|
||||||
import isPrivateIp from 'private-ip'
|
import isPrivateIp from 'private-ip'
|
||||||
import * as pkg from './version.js'
|
import * as pkg from './version.js'
|
||||||
@ -157,7 +157,7 @@ export class NatManager implements Startable {
|
|||||||
protocol: transport.toUpperCase() === 'TCP' ? 'TCP' : 'UDP'
|
protocol: transport.toUpperCase() === 'TCP' ? 'TCP' : 'UDP'
|
||||||
})
|
})
|
||||||
|
|
||||||
this.components.getAddressManager().addObservedAddr(Multiaddr.fromNodeAddress({
|
this.components.getAddressManager().addObservedAddr(fromNodeAddress({
|
||||||
family: 4,
|
family: 4,
|
||||||
address: publicIp,
|
address: publicIp,
|
||||||
port: publicPort
|
port: publicPort
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import { Multiaddr, protocols } from '@multiformats/multiaddr'
|
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
||||||
import { AddressFilter, DefaultAddressManager } from '../../src/address-manager/index.js'
|
import { AddressFilter, DefaultAddressManager } from '../../src/address-manager/index.js'
|
||||||
import { createNode } from '../utils/creators/peer.js'
|
import { createNode } from '../utils/creators/peer.js'
|
||||||
import { createFromJSON } from '@libp2p/peer-id-factory'
|
import { createFromJSON } from '@libp2p/peer-id-factory'
|
||||||
@ -48,8 +48,8 @@ describe('Address Manager', () => {
|
|||||||
|
|
||||||
const listenMultiaddrs = am.getListenAddrs()
|
const listenMultiaddrs = am.getListenAddrs()
|
||||||
expect(listenMultiaddrs.length).to.equal(2)
|
expect(listenMultiaddrs.length).to.equal(2)
|
||||||
expect(listenMultiaddrs[0].equals(new Multiaddr(listenAddresses[0]))).to.equal(true)
|
expect(listenMultiaddrs[0].equals(multiaddr(listenAddresses[0]))).to.equal(true)
|
||||||
expect(listenMultiaddrs[1].equals(new Multiaddr(listenAddresses[1]))).to.equal(true)
|
expect(listenMultiaddrs[1].equals(multiaddr(listenAddresses[1]))).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return announce multiaddrs on get', () => {
|
it('should return announce multiaddrs on get', () => {
|
||||||
@ -67,7 +67,7 @@ describe('Address Manager', () => {
|
|||||||
|
|
||||||
const announceMultiaddrs = am.getAnnounceAddrs()
|
const announceMultiaddrs = am.getAnnounceAddrs()
|
||||||
expect(announceMultiaddrs.length).to.equal(1)
|
expect(announceMultiaddrs.length).to.equal(1)
|
||||||
expect(announceMultiaddrs[0].equals(new Multiaddr(announceAddreses[0]))).to.equal(true)
|
expect(announceMultiaddrs[0].equals(multiaddr(announceAddreses[0]))).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should add observed addresses', () => {
|
it('should add observed addresses', () => {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr, protocols } from '@multiformats/multiaddr'
|
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
||||||
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
||||||
import { AddressesOptions } from './utils.js'
|
import { AddressesOptions } from './utils.js'
|
||||||
import { createNode } from '../utils/creators/peer.js'
|
import { createNode } from '../utils/creators/peer.js'
|
||||||
@ -114,9 +114,9 @@ describe('libp2p.multiaddrs', () => {
|
|||||||
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(0)
|
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(0)
|
||||||
|
|
||||||
// Stub transportManager addresses to add a public address
|
// Stub transportManager addresses to add a public address
|
||||||
const stubMa = new Multiaddr('/ip4/120.220.10.1/tcp/1000')
|
const stubMa = multiaddr('/ip4/120.220.10.1/tcp/1000')
|
||||||
sinon.stub(libp2p.components.getTransportManager(), 'getAddrs').returns([
|
sinon.stub(libp2p.components.getTransportManager(), 'getAddrs').returns([
|
||||||
...listenAddresses.map((a) => new Multiaddr(a)),
|
...listenAddresses.map((a) => multiaddr(a)),
|
||||||
stubMa
|
stubMa
|
||||||
])
|
])
|
||||||
|
|
||||||
@ -165,7 +165,7 @@ describe('libp2p.multiaddrs', () => {
|
|||||||
|
|
||||||
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(listenAddresses.length)
|
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(listenAddresses.length)
|
||||||
|
|
||||||
libp2p.components.getAddressManager().addObservedAddr(new Multiaddr(ma))
|
libp2p.components.getAddressManager().addObservedAddr(multiaddr(ma))
|
||||||
|
|
||||||
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(listenAddresses.length + 1)
|
expect(libp2p.components.getAddressManager().getAddresses()).to.have.lengthOf(listenAddresses.length + 1)
|
||||||
expect(libp2p.components.getAddressManager().getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code).toString())).to.include(ma)
|
expect(libp2p.components.getAddressManager().getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code).toString())).to.include(ma)
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import nock from 'nock'
|
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import pDefer from 'p-defer'
|
import pDefer from 'p-defer'
|
||||||
import { CID } from 'multiformats/cid'
|
import { CID } from 'multiformats/cid'
|
||||||
import { create as createIpfsHttpClient } from 'ipfs-http-client'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
|
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
|
||||||
import drain from 'it-drain'
|
import drain from 'it-drain'
|
||||||
import all from 'it-all'
|
import all from 'it-all'
|
||||||
import { createNode, createPeerId, populateAddressBooks } from '../utils/creators/peer.js'
|
import { createNode, createPeerId, populateAddressBooks } from '../utils/creators/peer.js'
|
||||||
@ -16,6 +13,9 @@ import { createRoutingOptions } from './utils.js'
|
|||||||
import type { Libp2p } from '../../src/index.js'
|
import type { Libp2p } from '../../src/index.js'
|
||||||
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
||||||
import type { Libp2pNode } from '../../src/libp2p.js'
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
||||||
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
||||||
|
import { StubbedInstance, stubInterface } from 'ts-sinon'
|
||||||
|
import { peerIdFromString } from '@libp2p/peer-id'
|
||||||
|
|
||||||
describe('content-routing', () => {
|
describe('content-routing', () => {
|
||||||
describe('no routers', () => {
|
describe('no routers', () => {
|
||||||
@ -119,14 +119,12 @@ describe('content-routing', () => {
|
|||||||
|
|
||||||
describe('via delegate router', () => {
|
describe('via delegate router', () => {
|
||||||
let node: Libp2pNode
|
let node: Libp2pNode
|
||||||
let delegate: DelegatedContentRouting
|
let delegate: StubbedInstance<ContentRouting>
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
delegate = new DelegatedContentRouting(createIpfsHttpClient({
|
delegate = stubInterface<ContentRouting>()
|
||||||
host: '0.0.0.0',
|
delegate.provide.returns(Promise.resolve())
|
||||||
protocol: 'http',
|
delegate.findProviders.returns(async function * () {}())
|
||||||
port: 60197
|
|
||||||
}))
|
|
||||||
|
|
||||||
node = await createNode({
|
node = await createNode({
|
||||||
config: createBaseOptions({
|
config: createBaseOptions({
|
||||||
@ -149,7 +147,7 @@ describe('content-routing', () => {
|
|||||||
it('should use the delegate router to provide', async () => {
|
it('should use the delegate router to provide', async () => {
|
||||||
const deferred = pDefer()
|
const deferred = pDefer()
|
||||||
|
|
||||||
sinon.stub(delegate, 'provide').callsFake(async () => {
|
delegate.provide.callsFake(async () => {
|
||||||
deferred.resolve()
|
deferred.resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -161,14 +159,14 @@ describe('content-routing', () => {
|
|||||||
it('should use the delegate router to find providers', async () => {
|
it('should use the delegate router to find providers', async () => {
|
||||||
const deferred = pDefer()
|
const deferred = pDefer()
|
||||||
|
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.returns(async function * () {
|
||||||
yield {
|
yield {
|
||||||
id: node.peerId,
|
id: node.peerId,
|
||||||
multiaddrs: [],
|
multiaddrs: [],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
deferred.resolve()
|
deferred.resolve()
|
||||||
})
|
}())
|
||||||
|
|
||||||
await drain(node.contentRouting.findProviders(CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')))
|
await drain(node.contentRouting.findProviders(CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')))
|
||||||
|
|
||||||
@ -177,94 +175,61 @@ describe('content-routing', () => {
|
|||||||
|
|
||||||
it('should be able to register as a provider', async () => {
|
it('should be able to register as a provider', async () => {
|
||||||
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||||
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
|
|
||||||
|
|
||||||
const mockBlockApi = nock('http://0.0.0.0:60197')
|
|
||||||
// mock the block/stat call
|
|
||||||
.post('/api/v0/block/stat')
|
|
||||||
.query(true)
|
|
||||||
.reply(200, '{"Key":"QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB","Size":"2169"}', [
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
const mockDhtApi = nock('http://0.0.0.0:60197')
|
|
||||||
// mock the dht/provide call
|
|
||||||
.post('/api/v0/dht/provide')
|
|
||||||
.query(true)
|
|
||||||
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":4}\n`, [
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
await node.contentRouting.provide(cid)
|
await node.contentRouting.provide(cid)
|
||||||
|
|
||||||
expect(mockBlockApi.isDone()).to.equal(true)
|
expect(delegate.provide.calledWith(cid)).to.equal(true)
|
||||||
expect(mockDhtApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle errors when registering as a provider', async () => {
|
it('should handle errors when registering as a provider', async () => {
|
||||||
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
// mock the block/stat call
|
delegate.provide.withArgs(cid).throws(new Error('Could not provide'))
|
||||||
.post('/api/v0/block/stat')
|
|
||||||
.query(true)
|
|
||||||
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
|
|
||||||
|
|
||||||
await expect(node.contentRouting.provide(cid))
|
await expect(node.contentRouting.provide(cid))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
|
.with.property('message', 'Could not provide')
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should be able to find providers', async () => {
|
it('should be able to find providers', async () => {
|
||||||
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||||
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
|
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
|
||||||
|
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
delegate.findProviders.withArgs(cid).returns(async function * () {
|
||||||
.post('/api/v0/dht/findprovs')
|
yield {
|
||||||
.query(true)
|
id: peerIdFromString(provider),
|
||||||
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":4}\n`, [
|
multiaddrs: [
|
||||||
'Content-Type', 'application/json',
|
multiaddr('/ip4/0.0.0.0/tcp/0')
|
||||||
'X-Chunked-Output', '1'
|
],
|
||||||
])
|
protocols: []
|
||||||
|
}
|
||||||
|
}())
|
||||||
|
|
||||||
const providers = await all(node.contentRouting.findProviders(cid))
|
const providers = await all(node.contentRouting.findProviders(cid))
|
||||||
|
|
||||||
expect(providers).to.have.length(1)
|
expect(providers).to.have.length(1)
|
||||||
expect(providers[0].id.toString()).to.equal(provider)
|
expect(providers[0].id.toString()).to.equal(provider)
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle errors when finding providers', async () => {
|
it('should handle errors when finding providers', async () => {
|
||||||
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
.post('/api/v0/dht/findprovs')
|
|
||||||
.query(true)
|
|
||||||
.reply(502, 'Bad Gateway', [
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
try {
|
delegate.findProviders.withArgs(cid).throws(new Error('Could not find providers'))
|
||||||
for await (const _ of node.contentRouting.findProviders(cid)) { } // eslint-disable-line
|
|
||||||
throw new Error('should handle errors when finding providers')
|
|
||||||
} catch (err: any) {
|
|
||||||
expect(err).to.exist()
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
await expect(drain(node.contentRouting.findProviders(cid)))
|
||||||
|
.to.eventually.be.rejected()
|
||||||
|
.with.property('message', 'Could not find providers')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('via dht and delegate routers', () => {
|
describe('via dht and delegate routers', () => {
|
||||||
let node: Libp2pNode
|
let node: Libp2pNode
|
||||||
let delegate: DelegatedContentRouting
|
let delegate: StubbedInstance<ContentRouting>
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
delegate = new DelegatedContentRouting(createIpfsHttpClient({
|
delegate = stubInterface<ContentRouting>()
|
||||||
host: '0.0.0.0',
|
delegate.provide.returns(Promise.resolve())
|
||||||
protocol: 'http',
|
delegate.findProviders.returns(async function * () {}())
|
||||||
port: 60197
|
|
||||||
}))
|
|
||||||
|
|
||||||
node = await createNode({
|
node = await createNode({
|
||||||
config: createRoutingOptions({
|
config: createRoutingOptions({
|
||||||
@ -284,7 +249,7 @@ describe('content-routing', () => {
|
|||||||
const result: PeerInfo = {
|
const result: PeerInfo = {
|
||||||
id: providerPeerId,
|
id: providerPeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/49320')
|
multiaddr('/ip4/123.123.123.123/tcp/49320')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -294,7 +259,7 @@ describe('content-routing', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sinon.stub(node.dht, 'findProviders').callsFake(async function * () {})
|
sinon.stub(node.dht, 'findProviders').callsFake(async function * () {})
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.callsFake(async function * () {
|
||||||
yield result
|
yield result
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -313,7 +278,7 @@ describe('content-routing', () => {
|
|||||||
const result = {
|
const result = {
|
||||||
id: providerPeerId,
|
id: providerPeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/49320')
|
multiaddr('/ip4/123.123.123.123/tcp/49320')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -327,7 +292,7 @@ describe('content-routing', () => {
|
|||||||
sinon.stub(node.dht, 'findProviders').callsFake(async function * () { // eslint-disable-line require-yield
|
sinon.stub(node.dht, 'findProviders').callsFake(async function * () { // eslint-disable-line require-yield
|
||||||
await defer.promise
|
await defer.promise
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.callsFake(async function * () {
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
await defer.promise
|
await defer.promise
|
||||||
@ -344,7 +309,7 @@ describe('content-routing', () => {
|
|||||||
const result = {
|
const result = {
|
||||||
id: providerPeerId,
|
id: providerPeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/49320')
|
multiaddr('/ip4/123.123.123.123/tcp/49320')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -363,7 +328,7 @@ describe('content-routing', () => {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.callsFake(async function * () {
|
||||||
yield result
|
yield result
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -377,14 +342,14 @@ describe('content-routing', () => {
|
|||||||
const result1 = {
|
const result1 = {
|
||||||
id: providerPeerId,
|
id: providerPeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/49320')
|
multiaddr('/ip4/123.123.123.123/tcp/49320')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
const result2 = {
|
const result2 = {
|
||||||
id: providerPeerId,
|
id: providerPeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/213.213.213.213/tcp/2344')
|
multiaddr('/ip4/213.213.213.213/tcp/2344')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -403,7 +368,7 @@ describe('content-routing', () => {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.callsFake(async function * () {
|
||||||
yield result2
|
yield result2
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -430,7 +395,7 @@ describe('content-routing', () => {
|
|||||||
dhtDeferred.resolve()
|
dhtDeferred.resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
sinon.stub(delegate, 'provide').callsFake(async function () {
|
delegate.provide.callsFake(async function () {
|
||||||
delegatedDeferred.resolve()
|
delegatedDeferred.resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -465,7 +430,7 @@ describe('content-routing', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () { // eslint-disable-line require-yield
|
delegate.findProviders.callsFake(async function * () { // eslint-disable-line require-yield
|
||||||
})
|
})
|
||||||
|
|
||||||
const providers = []
|
const providers = []
|
||||||
@ -491,7 +456,7 @@ describe('content-routing', () => {
|
|||||||
|
|
||||||
sinon.stub(node.dht, 'findProviders').callsFake(async function * () {})
|
sinon.stub(node.dht, 'findProviders').callsFake(async function * () {})
|
||||||
|
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
delegate.findProviders.callsFake(async function * () {
|
||||||
yield results[0]
|
yield results[0]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import pWaitFor from 'p-wait-for'
|
import pWaitFor from 'p-wait-for'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { subsystemMulticodecs, createSubsystemOptions } from './utils.js'
|
import { subsystemMulticodecs, createSubsystemOptions } from './utils.js'
|
||||||
@ -10,8 +11,8 @@ import type { PeerId } from '@libp2p/interface-peer-id'
|
|||||||
import { createLibp2pNode, Libp2pNode } from '../../../src/libp2p.js'
|
import { createLibp2pNode, Libp2pNode } from '../../../src/libp2p.js'
|
||||||
import { start } from '@libp2p/interfaces/startable'
|
import { start } from '@libp2p/interfaces/startable'
|
||||||
|
|
||||||
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/8000')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/8000')
|
||||||
const remoteListenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/8001')
|
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/8001')
|
||||||
|
|
||||||
async function getRemoteAddr (remotePeerId: PeerId, libp2p: Libp2pNode) {
|
async function getRemoteAddr (remotePeerId: PeerId, libp2p: Libp2pNode) {
|
||||||
const addrs = await libp2p.components.getPeerStore().addressBook.get(remotePeerId)
|
const addrs = await libp2p.components.getPeerStore().addressBook.get(remotePeerId)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import { WebSockets } from '@libp2p/websockets'
|
import { WebSockets } from '@libp2p/websockets'
|
||||||
import { Plaintext } from '../../src/insecure/index.js'
|
import { Plaintext } from '../../src/insecure/index.js'
|
||||||
import { createPeerId } from '../utils/creators/peer.js'
|
import { createPeerId } from '../utils/creators/peer.js'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
||||||
import type { Libp2pOptions } from '../../src/index.js'
|
import type { Libp2pOptions } from '../../src/index.js'
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ describe('Consume peer record', () => {
|
|||||||
|
|
||||||
await libp2p.start()
|
await libp2p.start()
|
||||||
|
|
||||||
libp2p.components.getAddressManager().addObservedAddr(new Multiaddr('/ip4/123.123.123.123/tcp/3983'))
|
libp2p.components.getAddressManager().addObservedAddr(multiaddr('/ip4/123.123.123.123/tcp/3983'))
|
||||||
|
|
||||||
await p
|
await p
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import delay from 'delay'
|
|||||||
import { DialAction, DialRequest } from '../../src/connection-manager/dialer/dial-request.js'
|
import { DialAction, DialRequest } from '../../src/connection-manager/dialer/dial-request.js'
|
||||||
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks'
|
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks'
|
||||||
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'
|
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'
|
||||||
import { Components } from '@libp2p/components'
|
import { Components } from '@libp2p/components'
|
||||||
const error = new Error('dial failure')
|
const error = new Error('dial failure')
|
||||||
@ -29,7 +29,7 @@ describe('Dial Request', () => {
|
|||||||
})
|
})
|
||||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer,
|
dialer,
|
||||||
dialAction
|
dialAction
|
||||||
})
|
})
|
||||||
@ -59,7 +59,7 @@ describe('Dial Request', () => {
|
|||||||
})
|
})
|
||||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer,
|
dialer,
|
||||||
dialAction
|
dialAction
|
||||||
})
|
})
|
||||||
@ -105,7 +105,7 @@ describe('Dial Request', () => {
|
|||||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||||
const dialerGetTokensSpy = sinon.spy(dialer, 'getTokens')
|
const dialerGetTokensSpy = sinon.spy(dialer, 'getTokens')
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer,
|
dialer,
|
||||||
dialAction
|
dialAction
|
||||||
})
|
})
|
||||||
@ -144,7 +144,7 @@ describe('Dial Request', () => {
|
|||||||
})
|
})
|
||||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer,
|
dialer,
|
||||||
dialAction
|
dialAction
|
||||||
})
|
})
|
||||||
@ -191,7 +191,7 @@ describe('Dial Request', () => {
|
|||||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||||
const dialerGetTokensSpy = sinon.spy(dialer, 'getTokens')
|
const dialerGetTokensSpy = sinon.spy(dialer, 'getTokens')
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer,
|
dialer,
|
||||||
dialAction
|
dialAction
|
||||||
})
|
})
|
||||||
@ -237,7 +237,7 @@ describe('Dial Request', () => {
|
|||||||
const signals: Record<string, AbortSignal | undefined> = {}
|
const signals: Record<string, AbortSignal | undefined> = {}
|
||||||
|
|
||||||
const dialRequest = new DialRequest({
|
const dialRequest = new DialRequest({
|
||||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
addrs: Object.keys(actions).map(str => multiaddr(str)),
|
||||||
dialer: new DefaultDialer(new Components(), {
|
dialer: new DefaultDialer(new Components(), {
|
||||||
maxParallelDials: 3
|
maxParallelDials: 3
|
||||||
}),
|
}),
|
||||||
|
@ -5,7 +5,8 @@ import sinon from 'sinon'
|
|||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { Mplex } from '@libp2p/mplex'
|
import { Mplex } from '@libp2p/mplex'
|
||||||
import { Plaintext } from '../../src/insecure/index.js'
|
import { Plaintext } from '../../src/insecure/index.js'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
|
|
||||||
import delay from 'delay'
|
import delay from 'delay'
|
||||||
import pDefer from 'p-defer'
|
import pDefer from 'p-defer'
|
||||||
@ -33,8 +34,8 @@ import swarmKey from '../fixtures/swarm.key.js'
|
|||||||
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
|
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
|
||||||
|
|
||||||
const swarmKeyBuffer = uint8ArrayFromString(swarmKey)
|
const swarmKeyBuffer = uint8ArrayFromString(swarmKey)
|
||||||
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
|
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
|
||||||
|
|
||||||
describe('Dialing (direct, TCP)', () => {
|
describe('Dialing (direct, TCP)', () => {
|
||||||
let remoteTM: DefaultTransportManager
|
let remoteTM: DefaultTransportManager
|
||||||
@ -176,9 +177,9 @@ describe('Dialing (direct, TCP)', () => {
|
|||||||
|
|
||||||
it('should dial to the max concurrency', async () => {
|
it('should dial to the max concurrency', async () => {
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8000'),
|
multiaddr('/ip4/0.0.0.0/tcp/8000'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8001'),
|
multiaddr('/ip4/0.0.0.0/tcp/8001'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8002')
|
multiaddr('/ip4/0.0.0.0/tcp/8002')
|
||||||
]
|
]
|
||||||
const peerId = await createFromJSON(Peers[1])
|
const peerId = await createFromJSON(Peers[1])
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@ import { WebSockets } from '@libp2p/websockets'
|
|||||||
import * as filters from '@libp2p/websockets/filters'
|
import * as filters from '@libp2p/websockets/filters'
|
||||||
import { Mplex } from '@libp2p/mplex'
|
import { Mplex } from '@libp2p/mplex'
|
||||||
import { Plaintext } from '../../src/insecure/index.js'
|
import { Plaintext } from '../../src/insecure/index.js'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { AbortError } from '@libp2p/interfaces/errors'
|
import { AbortError } from '@libp2p/interfaces/errors'
|
||||||
import { MemoryDatastore } from 'datastore-core/memory'
|
import { MemoryDatastore } from 'datastore-core/memory'
|
||||||
import { codes as ErrorCodes } from '../../src/errors.js'
|
import { codes as ErrorCodes } from '../../src/errors.js'
|
||||||
@ -31,7 +32,7 @@ import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
|
|||||||
import type { PeerId } from '@libp2p/interface-peer-id'
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
||||||
import { pEvent } from 'p-event'
|
import { pEvent } from 'p-event'
|
||||||
|
|
||||||
const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999')
|
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999')
|
||||||
|
|
||||||
describe('Dialing (direct, WebSockets)', () => {
|
describe('Dialing (direct, WebSockets)', () => {
|
||||||
let localTM: TransportManager
|
let localTM: TransportManager
|
||||||
@ -174,7 +175,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, Array.from({ length: 11 }, (_, i) => new Multiaddr(`/ip4/127.0.0.1/tcp/1500${i}/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5`)))
|
await localComponents.getPeerStore().addressBook.set(remotePeerId, Array.from({ length: 11 }, (_, i) => multiaddr(`/ip4/127.0.0.1/tcp/1500${i}/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5`)))
|
||||||
|
|
||||||
await expect(dialer.dial(remoteAddr))
|
await expect(dialer.dial(remoteAddr))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
@ -183,9 +184,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
|||||||
|
|
||||||
it('should sort addresses on dial', async () => {
|
it('should sort addresses on dial', async () => {
|
||||||
const peerMultiaddrs = [
|
const peerMultiaddrs = [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/15001/ws'),
|
multiaddr('/ip4/127.0.0.1/tcp/15001/ws'),
|
||||||
new Multiaddr('/ip4/20.0.0.1/tcp/15001/ws'),
|
multiaddr('/ip4/20.0.0.1/tcp/15001/ws'),
|
||||||
new Multiaddr('/ip4/30.0.0.1/tcp/15001/ws')
|
multiaddr('/ip4/30.0.0.1/tcp/15001/ws')
|
||||||
]
|
]
|
||||||
|
|
||||||
const publicAddressesFirstSpy = sinon.spy(publicAddressesFirst)
|
const publicAddressesFirstSpy = sinon.spy(publicAddressesFirst)
|
||||||
@ -213,9 +214,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
|||||||
|
|
||||||
it('should dial to the max concurrency', async () => {
|
it('should dial to the max concurrency', async () => {
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8000/ws'),
|
multiaddr('/ip4/0.0.0.0/tcp/8000/ws'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
|
multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8002/ws')
|
multiaddr('/ip4/0.0.0.0/tcp/8002/ws')
|
||||||
]
|
]
|
||||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||||
|
|
||||||
@ -253,9 +254,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
|||||||
|
|
||||||
it('.destroy should abort pending dials', async () => {
|
it('.destroy should abort pending dials', async () => {
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8000/ws'),
|
multiaddr('/ip4/0.0.0.0/tcp/8000/ws'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
|
multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
|
||||||
new Multiaddr('/ip4/0.0.0.0/tcp/8002/ws')
|
multiaddr('/ip4/0.0.0.0/tcp/8002/ws')
|
||||||
]
|
]
|
||||||
const dialer = new DefaultDialer(localComponents, {
|
const dialer = new DefaultDialer(localComponents, {
|
||||||
maxParallelDials: 2
|
maxParallelDials: 2
|
||||||
@ -592,7 +593,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
|||||||
|
|
||||||
await libp2p.start()
|
await libp2p.start()
|
||||||
|
|
||||||
await expect(libp2p.dial(new Multiaddr(`/ip4/127.0.0.1/tcp/1234/ws/p2p/${peerId.toString()}`)))
|
await expect(libp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/1234/ws/p2p/${peerId.toString()}`)))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
.and.to.have.property('code', ErrorCodes.ERR_DIALED_SELF)
|
.and.to.have.property('code', ErrorCodes.ERR_DIALED_SELF)
|
||||||
})
|
})
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { codes as ErrorCodes } from '../../src/errors.js'
|
import { codes as ErrorCodes } from '../../src/errors.js'
|
||||||
import { createNode } from '../utils/creators/peer.js'
|
import { createNode } from '../utils/creators/peer.js'
|
||||||
import { createBaseOptions } from '../utils/base-options.browser.js'
|
import { createBaseOptions } from '../utils/base-options.browser.js'
|
||||||
@ -90,8 +91,8 @@ describe('Dialing (resolvable addresses)', () => {
|
|||||||
|
|
||||||
it('resolves dnsaddr to ws local address', async () => {
|
it('resolves dnsaddr to ws local address', async () => {
|
||||||
const remoteId = remoteLibp2p.peerId
|
const remoteId = remoteLibp2p.peerId
|
||||||
const dialAddr = new Multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
||||||
const relayedAddrFetched = new Multiaddr(relayedAddr(remoteId))
|
const relayedAddrFetched = multiaddr(relayedAddr(remoteId))
|
||||||
|
|
||||||
// Transport spy
|
// Transport spy
|
||||||
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
||||||
@ -111,8 +112,8 @@ describe('Dialing (resolvable addresses)', () => {
|
|||||||
|
|
||||||
it('resolves a dnsaddr recursively', async () => {
|
it('resolves a dnsaddr recursively', async () => {
|
||||||
const remoteId = remoteLibp2p.peerId
|
const remoteId = remoteLibp2p.peerId
|
||||||
const dialAddr = new Multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
||||||
const relayedAddrFetched = new Multiaddr(relayedAddr(remoteId))
|
const relayedAddrFetched = multiaddr(relayedAddr(remoteId))
|
||||||
|
|
||||||
// Transport spy
|
// Transport spy
|
||||||
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
||||||
@ -142,10 +143,10 @@ describe('Dialing (resolvable addresses)', () => {
|
|||||||
// Resolver just returns the received multiaddrs
|
// Resolver just returns the received multiaddrs
|
||||||
it('stops recursive resolve if finds dns4/dns6 and dials it', async () => {
|
it('stops recursive resolve if finds dns4/dns6 and dials it', async () => {
|
||||||
const remoteId = remoteLibp2p.peerId
|
const remoteId = remoteLibp2p.peerId
|
||||||
const dialAddr = new Multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
||||||
|
|
||||||
// Stub resolver
|
// Stub resolver
|
||||||
const dnsMa = new Multiaddr(`/dns4/ams-1.remote.libp2p.io/tcp/443/wss/p2p/${remoteId.toString()}`)
|
const dnsMa = multiaddr(`/dns4/ams-1.remote.libp2p.io/tcp/443/wss/p2p/${remoteId.toString()}`)
|
||||||
resolver.returns(Promise.resolve([
|
resolver.returns(Promise.resolve([
|
||||||
`${dnsMa.toString()}`
|
`${dnsMa.toString()}`
|
||||||
]))
|
]))
|
||||||
@ -170,8 +171,8 @@ describe('Dialing (resolvable addresses)', () => {
|
|||||||
|
|
||||||
it('resolves a dnsaddr recursively not failing if one address fails to resolve', async () => {
|
it('resolves a dnsaddr recursively not failing if one address fails to resolve', async () => {
|
||||||
const remoteId = remoteLibp2p.peerId
|
const remoteId = remoteLibp2p.peerId
|
||||||
const dialAddr = new Multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
||||||
const relayedAddrFetched = new Multiaddr(relayedAddr(remoteId))
|
const relayedAddrFetched = multiaddr(relayedAddr(remoteId))
|
||||||
|
|
||||||
// Transport spy
|
// Transport spy
|
||||||
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
const transport = getTransport(libp2p, Circuit.prototype[Symbol.toStringTag])
|
||||||
@ -193,7 +194,7 @@ describe('Dialing (resolvable addresses)', () => {
|
|||||||
|
|
||||||
it('fails to dial if resolve fails and there are no addresses to dial', async () => {
|
it('fails to dial if resolve fails and there are no addresses to dial', async () => {
|
||||||
const remoteId = remoteLibp2p.peerId
|
const remoteId = remoteLibp2p.peerId
|
||||||
const dialAddr = new Multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
const dialAddr = multiaddr(`/dnsaddr/remote.libp2p.io/p2p/${remoteId.toString()}`)
|
||||||
|
|
||||||
// Stub resolver
|
// Stub resolver
|
||||||
resolver.returns(Promise.reject(new Error()))
|
resolver.returns(Promise.reject(new Error()))
|
||||||
|
4
test/fixtures/browser.ts
vendored
4
test/fixtures/browser.ts
vendored
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
|
|
||||||
export const MULTIADDRS_WEBSOCKETS = [
|
export const MULTIADDRS_WEBSOCKETS = [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/15001/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5')
|
multiaddr('/ip4/127.0.0.1/tcp/15001/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5')
|
||||||
]
|
]
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { codes } from '../../src/errors.js'
|
import { codes } from '../../src/errors.js'
|
||||||
import { IdentifyService, IdentifyServiceInit, Message } from '../../src/identify/index.js'
|
import { IdentifyService, IdentifyServiceInit, Message } from '../../src/identify/index.js'
|
||||||
@ -30,7 +30,7 @@ import { TimeoutController } from 'timeout-abort-controller'
|
|||||||
import { CustomEvent } from '@libp2p/interfaces/events'
|
import { CustomEvent } from '@libp2p/interfaces/events'
|
||||||
import pDefer from 'p-defer'
|
import pDefer from 'p-defer'
|
||||||
|
|
||||||
const listenMaddrs = [new Multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
||||||
|
|
||||||
const defaultInit: IdentifyServiceInit = {
|
const defaultInit: IdentifyServiceInit = {
|
||||||
protocolPrefix: 'ipfs',
|
protocolPrefix: 'ipfs',
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { IdentifyService, IdentifyServiceInit } from '../../src/identify/index.js'
|
import { IdentifyService, IdentifyServiceInit } from '../../src/identify/index.js'
|
||||||
import Peers from '../fixtures/peers.js'
|
import Peers from '../fixtures/peers.js'
|
||||||
import { PersistentPeerStore } from '@libp2p/peer-store'
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
||||||
@ -25,7 +25,7 @@ import delay from 'delay'
|
|||||||
import { pEvent } from 'p-event'
|
import { pEvent } from 'p-event'
|
||||||
import { start, stop } from '@libp2p/interfaces/startable'
|
import { start, stop } from '@libp2p/interfaces/startable'
|
||||||
|
|
||||||
const listenMaddrs = [new Multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
||||||
|
|
||||||
const defaultInit: IdentifyServiceInit = {
|
const defaultInit: IdentifyServiceInit = {
|
||||||
protocolPrefix: 'ipfs',
|
protocolPrefix: 'ipfs',
|
||||||
@ -119,7 +119,7 @@ describe('identify (push)', () => {
|
|||||||
await remoteIdentify.identify(remoteToLocal)
|
await remoteIdentify.identify(remoteToLocal)
|
||||||
|
|
||||||
const updatedProtocol = '/special-new-protocol/1.0.0'
|
const updatedProtocol = '/special-new-protocol/1.0.0'
|
||||||
const updatedAddress = new Multiaddr('/ip4/127.0.0.1/tcp/48322')
|
const updatedAddress = multiaddr('/ip4/127.0.0.1/tcp/48322')
|
||||||
|
|
||||||
// should have protocols but not our new one
|
// should have protocols but not our new one
|
||||||
const identifiedProtocols = await remoteComponents.getPeerStore().protoBook.get(localComponents.getPeerId())
|
const identifiedProtocols = await remoteComponents.getPeerStore().protoBook.get(localComponents.getPeerId())
|
||||||
@ -250,7 +250,7 @@ describe('identify (push)', () => {
|
|||||||
await remoteIdentify.identify(remoteToLocal)
|
await remoteIdentify.identify(remoteToLocal)
|
||||||
|
|
||||||
const updatedProtocol = '/special-new-protocol/1.0.0'
|
const updatedProtocol = '/special-new-protocol/1.0.0'
|
||||||
const updatedAddress = new Multiaddr('/ip4/127.0.0.1/tcp/48322')
|
const updatedAddress = multiaddr('/ip4/127.0.0.1/tcp/48322')
|
||||||
|
|
||||||
// should have protocols but not our new one
|
// should have protocols but not our new one
|
||||||
const identifiedProtocols = await remoteComponents.getPeerStore().protoBook.get(localComponents.getPeerId())
|
const identifiedProtocols = await remoteComponents.getPeerStore().protoBook.get(localComponents.getPeerId())
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||||
import Peers from '../fixtures/peers.js'
|
import Peers from '../fixtures/peers.js'
|
||||||
import { createLibp2pNode } from '../../src/libp2p.js'
|
import { createLibp2pNode } from '../../src/libp2p.js'
|
||||||
@ -228,7 +228,7 @@ describe('libp2p.dialer.identifyService', () => {
|
|||||||
await identityServiceIdentifySpy.firstCall.returnValue
|
await identityServiceIdentifySpy.firstCall.returnValue
|
||||||
sinon.stub(libp2p, 'isStarted').returns(true)
|
sinon.stub(libp2p, 'isStarted').returns(true)
|
||||||
|
|
||||||
await libp2p.peerStore.addressBook.add(libp2p.peerId, [new Multiaddr('/ip4/180.0.0.1/tcp/15001/ws')])
|
await libp2p.peerStore.addressBook.add(libp2p.peerId, [multiaddr('/ip4/180.0.0.1/tcp/15001/ws')])
|
||||||
|
|
||||||
// the protocol change event listener in the identity service is async
|
// the protocol change event listener in the identity service is async
|
||||||
await pWaitFor(() => identityServicePushSpy.callCount === 1)
|
await pWaitFor(() => identityServicePushSpy.callCount === 1)
|
||||||
|
@ -12,7 +12,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
|
|||||||
import { createFromJSON, createRSAPeerId } from '@libp2p/peer-id-factory'
|
import { createFromJSON, createRSAPeerId } from '@libp2p/peer-id-factory'
|
||||||
import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
|
import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
|
||||||
import { mockMultiaddrConnPair } from '@libp2p/interface-mocks'
|
import { mockMultiaddrConnPair } from '@libp2p/interface-mocks'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { peerIdFromBytes } from '@libp2p/peer-id'
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
||||||
|
|
||||||
describe('plaintext', () => {
|
describe('plaintext', () => {
|
||||||
@ -39,8 +39,8 @@ describe('plaintext', () => {
|
|||||||
const { inbound, outbound } = mockMultiaddrConnPair({
|
const { inbound, outbound } = mockMultiaddrConnPair({
|
||||||
remotePeer,
|
remotePeer,
|
||||||
addrs: [
|
addrs: [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1235')
|
multiaddr('/ip4/127.0.0.1/tcp/1235')
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -60,8 +60,8 @@ describe('plaintext', () => {
|
|||||||
const { inbound, outbound } = mockMultiaddrConnPair({
|
const { inbound, outbound } = mockMultiaddrConnPair({
|
||||||
remotePeer,
|
remotePeer,
|
||||||
addrs: [
|
addrs: [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1235')
|
multiaddr('/ip4/127.0.0.1/tcp/1235')
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import { createClient } from '@libp2p/daemon-client'
|
|||||||
import { createLibp2p, Libp2pOptions } from '../src/index.js'
|
import { createLibp2p, Libp2pOptions } from '../src/index.js'
|
||||||
import { Noise } from '@chainsafe/libp2p-noise'
|
import { Noise } from '@chainsafe/libp2p-noise'
|
||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { KadDHT } from '@libp2p/kad-dht'
|
import { KadDHT } from '@libp2p/kad-dht'
|
||||||
import { path as p2pd } from 'go-libp2p'
|
import { path as p2pd } from 'go-libp2p'
|
||||||
import { execa } from 'execa'
|
import { execa } from 'execa'
|
||||||
@ -23,7 +23,7 @@ import { FloodSub } from '@libp2p/floodsub'
|
|||||||
|
|
||||||
async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
|
async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
|
||||||
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000
|
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000
|
||||||
const apiAddr = new Multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`)
|
const apiAddr = multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`)
|
||||||
|
|
||||||
const log = logger(`go-libp2p:${controlPort}`)
|
const log = logger(`go-libp2p:${controlPort}`)
|
||||||
|
|
||||||
@ -133,7 +133,7 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const node = await createLibp2p(opts)
|
const node = await createLibp2p(opts)
|
||||||
const server = await createServer(new Multiaddr('/ip4/0.0.0.0/tcp/0'), node)
|
const server = await createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node)
|
||||||
await server.start()
|
await server.start()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -7,7 +7,7 @@ import { Bootstrap } from '@libp2p/bootstrap'
|
|||||||
import { randomBytes } from '@libp2p/crypto'
|
import { randomBytes } from '@libp2p/crypto'
|
||||||
import { KadDHT } from '@libp2p/kad-dht'
|
import { KadDHT } from '@libp2p/kad-dht'
|
||||||
import { MulticastDNS } from '@libp2p/mdns'
|
import { MulticastDNS } from '@libp2p/mdns'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||||
import { createBaseOptions } from '../utils/base-options.js'
|
import { createBaseOptions } from '../utils/base-options.js'
|
||||||
import { createPeerId } from '../utils/creators/peer.js'
|
import { createPeerId } from '../utils/creators/peer.js'
|
||||||
@ -16,7 +16,7 @@ import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
|||||||
import { CustomEvent } from '@libp2p/interfaces/events'
|
import { CustomEvent } from '@libp2p/interfaces/events'
|
||||||
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
||||||
|
|
||||||
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
|
||||||
describe('peer discovery scenarios', () => {
|
describe('peer discovery scenarios', () => {
|
||||||
let peerId: PeerId, remotePeerId1: PeerId, remotePeerId2: PeerId
|
let peerId: PeerId, remotePeerId1: PeerId, remotePeerId2: PeerId
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import nock from 'nock'
|
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import intoStream from 'into-stream'
|
|
||||||
import delay from 'delay'
|
import delay from 'delay'
|
||||||
import pDefer from 'p-defer'
|
import pDefer from 'p-defer'
|
||||||
import pWaitFor from 'p-wait-for'
|
import pWaitFor from 'p-wait-for'
|
||||||
import drain from 'it-drain'
|
import drain from 'it-drain'
|
||||||
import all from 'it-all'
|
import all from 'it-all'
|
||||||
import { create as createIpfsHttpClient } from 'ipfs-http-client'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
|
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
|
||||||
import { createNode, createPeerId, populateAddressBooks } from '../utils/creators/peer.js'
|
import { createNode, createPeerId, populateAddressBooks } from '../utils/creators/peer.js'
|
||||||
import type { Libp2pNode } from '../../src/libp2p.js'
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
||||||
import { createBaseOptions } from '../utils/base-options.js'
|
import { createBaseOptions } from '../utils/base-options.js'
|
||||||
@ -19,9 +15,10 @@ import { createRoutingOptions } from './utils.js'
|
|||||||
import type { PeerId } from '@libp2p/interface-peer-id'
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
||||||
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
||||||
import { EventTypes, MessageType } from '@libp2p/interface-dht'
|
import { EventTypes, MessageType } from '@libp2p/interface-dht'
|
||||||
import { peerIdFromString } from '@libp2p/peer-id'
|
|
||||||
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
||||||
import { KadDHT } from '@libp2p/kad-dht'
|
import { KadDHT } from '@libp2p/kad-dht'
|
||||||
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
||||||
|
import { StubbedInstance, stubInterface } from 'ts-sinon'
|
||||||
|
|
||||||
describe('peer-routing', () => {
|
describe('peer-routing', () => {
|
||||||
let peerId: PeerId
|
let peerId: PeerId
|
||||||
@ -237,14 +234,12 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
describe('via delegate router', () => {
|
describe('via delegate router', () => {
|
||||||
let node: Libp2pNode
|
let node: Libp2pNode
|
||||||
let delegate: DelegatedPeerRouting
|
let delegate: StubbedInstance<PeerRouting>
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
delegate = new DelegatedPeerRouting(createIpfsHttpClient({
|
delegate = stubInterface<PeerRouting>()
|
||||||
host: '0.0.0.0',
|
delegate.findPeer.returns(Promise.reject(new Error('Could not find peer')))
|
||||||
protocol: 'http',
|
delegate.getClosestPeers.returns(async function * () {}())
|
||||||
port: 60197
|
|
||||||
}))
|
|
||||||
|
|
||||||
node = await createNode({
|
node = await createNode({
|
||||||
config: createBaseOptions({
|
config: createBaseOptions({
|
||||||
@ -254,7 +249,6 @@ describe('peer-routing', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
nock.cleanAll()
|
|
||||||
sinon.restore()
|
sinon.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -268,7 +262,7 @@ describe('peer-routing', () => {
|
|||||||
it('should use the delegate router to find peers', async () => {
|
it('should use the delegate router to find peers', async () => {
|
||||||
const remotePeerId = await createPeerId()
|
const remotePeerId = await createPeerId()
|
||||||
|
|
||||||
const delegateFindPeerStub = sinon.stub(delegate, 'findPeer').callsFake(async function () {
|
delegate.findPeer.callsFake(async function () {
|
||||||
return {
|
return {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [],
|
multiaddrs: [],
|
||||||
@ -276,16 +270,15 @@ describe('peer-routing', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(delegateFindPeerStub.called).to.be.false()
|
expect(delegate.findPeer.called).to.be.false()
|
||||||
await node.peerRouting.findPeer(remotePeerId)
|
await node.peerRouting.findPeer(remotePeerId)
|
||||||
expect(delegateFindPeerStub.called).to.be.true()
|
expect(delegate.findPeer.called).to.be.true()
|
||||||
delegateFindPeerStub.restore()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should use the delegate router to get the closest peers', async () => {
|
it('should use the delegate router to get the closest peers', async () => {
|
||||||
const remotePeerId = await createPeerId()
|
const remotePeerId = await createPeerId()
|
||||||
|
|
||||||
const delegateGetClosestPeersStub = sinon.stub(delegate, 'getClosestPeers').callsFake(async function * () {
|
delegate.getClosestPeers.callsFake(async function * () {
|
||||||
yield {
|
yield {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [],
|
multiaddrs: [],
|
||||||
@ -293,26 +286,9 @@ describe('peer-routing', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(delegateGetClosestPeersStub.called).to.be.false()
|
expect(delegate.getClosestPeers.called).to.be.false()
|
||||||
await drain(node.peerRouting.getClosestPeers(remotePeerId.toBytes()))
|
await drain(node.peerRouting.getClosestPeers(remotePeerId.toBytes()))
|
||||||
expect(delegateGetClosestPeersStub.called).to.be.true()
|
expect(delegate.getClosestPeers.called).to.be.true()
|
||||||
delegateGetClosestPeersStub.restore()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be able to find a peer', async () => {
|
|
||||||
const peerKey = peerIdFromString('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
.post('/api/v0/dht/findpeer')
|
|
||||||
.query(true)
|
|
||||||
.reply(200, `{"Extra":"","ID":"some other id","Responses":null,"Type":0}\n{"Extra":"","ID":"","Responses":[{"Addrs":["/ip4/127.0.0.1/tcp/4001"],"ID":"${peerKey.toString()}"}],"Type":2}\n`, [
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
const peer = await node.peerRouting.findPeer(peerKey)
|
|
||||||
|
|
||||||
expect(peer.id.toString()).to.equal(peerKey.toString())
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should error when peer tries to find itself', async () => {
|
it('should error when peer tries to find itself', async () => {
|
||||||
@ -321,91 +297,27 @@ describe('peer-routing', () => {
|
|||||||
.and.to.have.property('code', 'ERR_FIND_SELF')
|
.and.to.have.property('code', 'ERR_FIND_SELF')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should error when a peer cannot be found', async () => {
|
it('should handle errors from the delegate when finding closest peers', async () => {
|
||||||
const peerId = await createEd25519PeerId()
|
const remotePeerId = await createPeerId()
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
.post('/api/v0/dht/findpeer')
|
|
||||||
.query(true)
|
|
||||||
.reply(200, '{"Extra":"","ID":"some other id","Responses":null,"Type":6}\n{"Extra":"","ID":"yet another id","Responses":null,"Type":0}\n{"Extra":"routing:not found","ID":"","Responses":null,"Type":3}\n', [
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
await expect(node.peerRouting.findPeer(peerId))
|
delegate.getClosestPeers.callsFake(async function * () { // eslint-disable-line require-yield
|
||||||
.to.eventually.be.rejected()
|
throw new Error('Could not find closer peers')
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle errors from the api', async () => {
|
expect(delegate.getClosestPeers.called).to.be.false()
|
||||||
const peerId = await createEd25519PeerId()
|
await expect(drain(node.peerRouting.getClosestPeers(remotePeerId.toBytes())))
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
.to.eventually.be.rejectedWith('Could not find closer peers')
|
||||||
.post('/api/v0/dht/findpeer')
|
|
||||||
.query(true)
|
|
||||||
.reply(502)
|
|
||||||
|
|
||||||
await expect(node.peerRouting.findPeer(peerId))
|
|
||||||
.to.eventually.be.rejected()
|
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be able to get the closest peers', async () => {
|
|
||||||
const peerId = await createEd25519PeerId()
|
|
||||||
const closest1 = '12D3KooWLewYMMdGWAtuX852n4rgCWkK7EBn4CWbwwBzhsVoKxk3'
|
|
||||||
const closest2 = '12D3KooWDtoQbpKhtnWddfj72QmpFvvLDTsBLTFkjvgQm6cde2AK'
|
|
||||||
|
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
.post('/api/v0/dht/query')
|
|
||||||
.query(true)
|
|
||||||
.reply(200,
|
|
||||||
() => intoStream([
|
|
||||||
`{"Extra":"","ID":"${closest1}","Responses":[{"ID":"${closest1}","Addrs":["/ip4/127.0.0.1/tcp/63930","/ip4/127.0.0.1/tcp/63930"]}],"Type":1}\n`,
|
|
||||||
`{"Extra":"","ID":"${closest2}","Responses":[{"ID":"${closest2}","Addrs":["/ip4/127.0.0.1/tcp/63506","/ip4/127.0.0.1/tcp/63506"]}],"Type":1}\n`,
|
|
||||||
`{"Extra":"","ID":"${closest2}","Responses":[],"Type":2}\n`,
|
|
||||||
`{"Extra":"","ID":"${closest1}","Responses":[],"Type":2}\n`
|
|
||||||
]),
|
|
||||||
[
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
const closestPeers = await all(node.peerRouting.getClosestPeers(peerId.toBytes()))
|
|
||||||
|
|
||||||
expect(closestPeers).to.have.length(2)
|
|
||||||
expect(closestPeers[0].id.toString()).to.equal(closest1)
|
|
||||||
expect(closestPeers[0].multiaddrs).to.have.lengthOf(2)
|
|
||||||
expect(closestPeers[1].id.toString()).to.equal(closest2)
|
|
||||||
expect(closestPeers[1].multiaddrs).to.have.lengthOf(2)
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle errors when getting the closest peers', async () => {
|
|
||||||
const peerId = await createEd25519PeerId()
|
|
||||||
|
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
|
||||||
.post('/api/v0/dht/query')
|
|
||||||
.query(true)
|
|
||||||
.reply(502, 'Bad Gateway', [
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
await expect(drain(node.peerRouting.getClosestPeers(peerId.toBytes()))).to.eventually.be.rejected()
|
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('via dht and delegate routers', () => {
|
describe('via dht and delegate routers', () => {
|
||||||
let node: Libp2pNode
|
let node: Libp2pNode
|
||||||
let delegate: DelegatedPeerRouting
|
let delegate: StubbedInstance<PeerRouting>
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
delegate = new DelegatedPeerRouting(createIpfsHttpClient({
|
delegate = stubInterface<PeerRouting>()
|
||||||
host: '0.0.0.0',
|
delegate.findPeer.throws(new Error('Could not find peer'))
|
||||||
protocol: 'http',
|
delegate.getClosestPeers.returns(async function * () {}())
|
||||||
port: 60197
|
|
||||||
}))
|
|
||||||
|
|
||||||
node = await createNode({
|
node = await createNode({
|
||||||
config: createRoutingOptions({
|
config: createRoutingOptions({
|
||||||
@ -434,7 +346,8 @@ describe('peer-routing', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sinon.stub(node.dht, 'findPeer').callsFake(async function * () {})
|
sinon.stub(node.dht, 'findPeer').callsFake(async function * () {})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
delegate.findPeer.reset()
|
||||||
|
delegate.findPeer.callsFake(async () => {
|
||||||
return results
|
return results
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -466,7 +379,8 @@ describe('peer-routing', () => {
|
|||||||
}
|
}
|
||||||
await defer.promise
|
await defer.promise
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
delegate.findPeer.reset()
|
||||||
|
delegate.findPeer.callsFake(async () => {
|
||||||
return results
|
return results
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -498,7 +412,8 @@ describe('peer-routing', () => {
|
|||||||
peer: result
|
peer: result
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
delegate.findPeer.reset()
|
||||||
|
delegate.findPeer.callsFake(async () => {
|
||||||
return await defer.promise
|
return await defer.promise
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -513,7 +428,7 @@ describe('peer-routing', () => {
|
|||||||
const result = {
|
const result = {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/38982')
|
multiaddr('/ip4/123.123.123.123/tcp/38982')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -532,7 +447,8 @@ describe('peer-routing', () => {
|
|||||||
peer: result
|
peer: result
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
delegate.findPeer.reset()
|
||||||
|
delegate.findPeer.callsFake(async () => {
|
||||||
const deferred = pDefer<PeerInfo>()
|
const deferred = pDefer<PeerInfo>()
|
||||||
|
|
||||||
return await deferred.promise
|
return await deferred.promise
|
||||||
@ -557,7 +473,7 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
sinon.stub(node.dht, 'getClosestPeers').callsFake(async function * () { })
|
sinon.stub(node.dht, 'getClosestPeers').callsFake(async function * () { })
|
||||||
|
|
||||||
sinon.stub(delegate, 'getClosestPeers').callsFake(async function * () {
|
delegate.getClosestPeers.callsFake(async function * () {
|
||||||
yield results[0]
|
yield results[0]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -572,7 +488,7 @@ describe('peer-routing', () => {
|
|||||||
const result = {
|
const result = {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/38982')
|
multiaddr('/ip4/123.123.123.123/tcp/38982')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}
|
}
|
||||||
@ -585,7 +501,7 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
sinon.stub(node.dht, 'getClosestPeers').callsFake(async function * () { })
|
sinon.stub(node.dht, 'getClosestPeers').callsFake(async function * () { })
|
||||||
|
|
||||||
sinon.stub(delegate, 'getClosestPeers').callsFake(async function * () {
|
delegate.getClosestPeers.callsFake(async function * () {
|
||||||
yield result
|
yield result
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -599,7 +515,7 @@ describe('peer-routing', () => {
|
|||||||
const results = [{
|
const results = [{
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/38982')
|
multiaddr('/ip4/123.123.123.123/tcp/38982')
|
||||||
],
|
],
|
||||||
protocols: []
|
protocols: []
|
||||||
}]
|
}]
|
||||||
@ -619,7 +535,7 @@ describe('peer-routing', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
sinon.stub(delegate, 'getClosestPeers').callsFake(async function * () {
|
delegate.getClosestPeers.callsFake(async function * () {
|
||||||
yield * results
|
yield * results
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -650,8 +566,8 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
it('should be enabled and start by default', async () => {
|
it('should be enabled and start by default', async () => {
|
||||||
const results: PeerInfo[] = [
|
const results: PeerInfo[] = [
|
||||||
{ id: peerIds[0], multiaddrs: [new Multiaddr('/ip4/30.0.0.1/tcp/2000')], protocols: [] },
|
{ id: peerIds[0], multiaddrs: [multiaddr('/ip4/30.0.0.1/tcp/2000')], protocols: [] },
|
||||||
{ id: peerIds[1], multiaddrs: [new Multiaddr('/ip4/32.0.0.1/tcp/2000')], protocols: [] }
|
{ id: peerIds[1], multiaddrs: [multiaddr('/ip4/32.0.0.1/tcp/2000')], protocols: [] }
|
||||||
]
|
]
|
||||||
|
|
||||||
node = await createNode({
|
node = await createNode({
|
||||||
@ -773,7 +689,7 @@ describe('peer-routing', () => {
|
|||||||
messageType: MessageType.FIND_NODE,
|
messageType: MessageType.FIND_NODE,
|
||||||
from: peerIds[0],
|
from: peerIds[0],
|
||||||
closer: [
|
closer: [
|
||||||
{ id: peerIds[0], multiaddrs: [new Multiaddr('/ip4/30.0.0.1/tcp/2000')], protocols: [] }
|
{ id: peerIds[0], multiaddrs: [multiaddr('/ip4/30.0.0.1/tcp/2000')], protocols: [] }
|
||||||
],
|
],
|
||||||
providers: []
|
providers: []
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import { pipe } from 'it-pipe'
|
|||||||
import { createNode, populateAddressBooks } from '../utils/creators/peer.js'
|
import { createNode, populateAddressBooks } from '../utils/creators/peer.js'
|
||||||
import { createBaseOptions } from '../utils/base-options.js'
|
import { createBaseOptions } from '../utils/base-options.js'
|
||||||
import { PROTOCOL } from '../../src/ping/constants.js'
|
import { PROTOCOL } from '../../src/ping/constants.js'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import pDefer from 'p-defer'
|
import pDefer from 'p-defer'
|
||||||
import type { Libp2pNode } from '../../src/libp2p.js'
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ describe('ping', () => {
|
|||||||
afterEach(async () => await Promise.all(nodes.map(async n => await n.stop())))
|
afterEach(async () => await Promise.all(nodes.map(async n => await n.stop())))
|
||||||
|
|
||||||
it('ping once from peer0 to peer1 using a multiaddr', async () => {
|
it('ping once from peer0 to peer1 using a multiaddr', async () => {
|
||||||
const ma = new Multiaddr(`${nodes[2].getMultiaddrs()[0].toString()}/p2p/${nodes[2].peerId.toString()}`)
|
const ma = multiaddr(`${nodes[2].getMultiaddrs()[0].toString()}/p2p/${nodes[2].peerId.toString()}`)
|
||||||
const latency = await nodes[0].ping(ma)
|
const latency = await nodes[0].ping(ma)
|
||||||
|
|
||||||
expect(latency).to.be.a('Number')
|
expect(latency).to.be.a('Number')
|
||||||
|
@ -6,7 +6,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|||||||
import { PreSharedKeyConnectionProtector, generateKey } from '../../src/pnet/index.js'
|
import { PreSharedKeyConnectionProtector, generateKey } from '../../src/pnet/index.js'
|
||||||
import { INVALID_PSK } from '../../src/pnet/errors.js'
|
import { INVALID_PSK } from '../../src/pnet/errors.js'
|
||||||
import { mockMultiaddrConnPair } from '@libp2p/interface-mocks'
|
import { mockMultiaddrConnPair } from '@libp2p/interface-mocks'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
||||||
|
|
||||||
const swarmKeyBuffer = new Uint8Array(95)
|
const swarmKeyBuffer = new Uint8Array(95)
|
||||||
@ -28,8 +28,8 @@ describe('private network', () => {
|
|||||||
it('should protect a simple connection', async () => {
|
it('should protect a simple connection', async () => {
|
||||||
const { inbound, outbound } = mockMultiaddrConnPair({
|
const { inbound, outbound } = mockMultiaddrConnPair({
|
||||||
addrs: [
|
addrs: [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1235')
|
multiaddr('/ip4/127.0.0.1/tcp/1235')
|
||||||
],
|
],
|
||||||
remotePeer: await createEd25519PeerId()
|
remotePeer: await createEd25519PeerId()
|
||||||
})
|
})
|
||||||
@ -63,8 +63,8 @@ describe('private network', () => {
|
|||||||
it('should not be able to share correct data with different keys', async () => {
|
it('should not be able to share correct data with different keys', async () => {
|
||||||
const { inbound, outbound } = mockMultiaddrConnPair({
|
const { inbound, outbound } = mockMultiaddrConnPair({
|
||||||
addrs: [
|
addrs: [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
multiaddr('/ip4/127.0.0.1/tcp/1234'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/1235')
|
multiaddr('/ip4/127.0.0.1/tcp/1235')
|
||||||
],
|
],
|
||||||
remotePeer: await createEd25519PeerId()
|
remotePeer: await createEd25519PeerId()
|
||||||
})
|
})
|
||||||
|
@ -5,16 +5,14 @@ import { pEvent } from 'p-event'
|
|||||||
import defer from 'p-defer'
|
import defer from 'p-defer'
|
||||||
import pWaitFor from 'p-wait-for'
|
import pWaitFor from 'p-wait-for'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import nock from 'nock'
|
|
||||||
import { create as createIpfsHttpClient } from 'ipfs-http-client'
|
|
||||||
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
|
|
||||||
import { RELAY_CODEC } from '../../src/circuit/multicodec.js'
|
import { RELAY_CODEC } from '../../src/circuit/multicodec.js'
|
||||||
import { createNode } from '../utils/creators/peer.js'
|
import { createNode } from '../utils/creators/peer.js'
|
||||||
import type { Libp2pNode } from '../../src/libp2p.js'
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
||||||
import type { Options as PWaitForOptions } from 'p-wait-for'
|
import type { Options as PWaitForOptions } from 'p-wait-for'
|
||||||
import type Sinon from 'sinon'
|
|
||||||
import { createRelayOptions, createNodeOptions } from './utils.js'
|
import { createRelayOptions, createNodeOptions } from './utils.js'
|
||||||
import { protocols } from '@multiformats/multiaddr'
|
import { protocols } from '@multiformats/multiaddr'
|
||||||
|
import { StubbedInstance, stubInterface } from 'ts-sinon'
|
||||||
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
||||||
|
|
||||||
async function usingAsRelay (node: Libp2pNode, relay: Libp2pNode, opts?: PWaitForOptions<boolean>) {
|
async function usingAsRelay (node: Libp2pNode, relay: Libp2pNode, opts?: PWaitForOptions<boolean>) {
|
||||||
// Wait for peer to be used as a relay
|
// Wait for peer to be used as a relay
|
||||||
@ -331,27 +329,33 @@ describe('auto-relay', () => {
|
|||||||
let local: Libp2pNode
|
let local: Libp2pNode
|
||||||
let remote: Libp2pNode
|
let remote: Libp2pNode
|
||||||
let relayLibp2p: Libp2pNode
|
let relayLibp2p: Libp2pNode
|
||||||
let contentRoutingProvideSpy: Sinon.SinonSpy
|
let localDelegate: StubbedInstance<ContentRouting>
|
||||||
|
let remoteDelegate: StubbedInstance<ContentRouting>
|
||||||
|
let relayDelegate: StubbedInstance<ContentRouting>
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const delegate = new DelegatedContentRouting(createIpfsHttpClient({
|
localDelegate = stubInterface<ContentRouting>()
|
||||||
host: '0.0.0.0',
|
localDelegate.findProviders.returns(async function * () {}())
|
||||||
protocol: 'http',
|
|
||||||
port: 60197
|
remoteDelegate = stubInterface<ContentRouting>()
|
||||||
}))
|
remoteDelegate.findProviders.returns(async function * () {}())
|
||||||
|
|
||||||
|
relayDelegate = stubInterface<ContentRouting>()
|
||||||
|
relayDelegate.provide.returns(Promise.resolve())
|
||||||
|
relayDelegate.findProviders.returns(async function * () {}())
|
||||||
|
|
||||||
;[local, remote, relayLibp2p] = await Promise.all([
|
;[local, remote, relayLibp2p] = await Promise.all([
|
||||||
createNode({
|
createNode({
|
||||||
config: createNodeOptions({
|
config: createNodeOptions({
|
||||||
contentRouters: [
|
contentRouters: [
|
||||||
delegate
|
localDelegate
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
createNode({
|
createNode({
|
||||||
config: createNodeOptions({
|
config: createNodeOptions({
|
||||||
contentRouters: [
|
contentRouters: [
|
||||||
delegate
|
remoteDelegate
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
@ -369,43 +373,30 @@ describe('auto-relay', () => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
contentRouters: [
|
contentRouters: [
|
||||||
delegate
|
relayDelegate
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
|
|
||||||
contentRoutingProvideSpy = sinon.spy(relayLibp2p.contentRouting, 'provide')
|
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
nock('http://0.0.0.0:60197')
|
|
||||||
// mock the refs call
|
|
||||||
.post('/api/v0/refs')
|
|
||||||
.query(true)
|
|
||||||
.reply(200, undefined, [
|
|
||||||
'Content-Type', 'application/json',
|
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
|
|
||||||
// Start each node
|
// Start each node
|
||||||
await Promise.all([local, remote, relayLibp2p].map(async libp2p => await libp2p.start()))
|
await Promise.all([local, remote, relayLibp2p].map(async libp2p => await libp2p.start()))
|
||||||
|
|
||||||
// Should provide on start
|
// Should provide on start
|
||||||
await pWaitFor(() => contentRoutingProvideSpy.callCount === 1)
|
await pWaitFor(() => relayDelegate.provide.callCount === 1)
|
||||||
|
|
||||||
const provider = relayLibp2p.peerId.toString()
|
const provider = relayLibp2p.peerId
|
||||||
const multiaddrs = relayLibp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
const multiaddrs = relayLibp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
||||||
|
|
||||||
// Mock findProviders
|
localDelegate.findProviders.returns(async function * () {
|
||||||
nock('http://0.0.0.0:60197')
|
yield {
|
||||||
.post('/api/v0/dht/findprovs')
|
id: provider,
|
||||||
.query(true)
|
multiaddrs,
|
||||||
.twice()
|
protocols: []
|
||||||
.reply(200, `{"Extra":"","ID":"${provider}","Responses":[{"Addrs":${JSON.stringify(multiaddrs)},"ID":"${provider}"}],"Type":4}\n`, [
|
}
|
||||||
'Content-Type', 'application/json',
|
}())
|
||||||
'X-Chunked-Output', '1'
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@ -417,8 +408,6 @@ describe('auto-relay', () => {
|
|||||||
const originalMultiaddrsLength = local.getMultiaddrs().length
|
const originalMultiaddrsLength = local.getMultiaddrs().length
|
||||||
|
|
||||||
// Spy Find Providers
|
// Spy Find Providers
|
||||||
const contentRoutingFindProvidersSpy = sinon.spy(local.contentRouting, 'findProviders')
|
|
||||||
|
|
||||||
const relayAddr = relayLibp2p.getMultiaddrs().pop()
|
const relayAddr = relayLibp2p.getMultiaddrs().pop()
|
||||||
|
|
||||||
if (relayAddr == null) {
|
if (relayAddr == null) {
|
||||||
@ -435,7 +424,7 @@ describe('auto-relay', () => {
|
|||||||
await local.hangUp(relayAddr)
|
await local.hangUp(relayAddr)
|
||||||
|
|
||||||
// Should try to find relay service providers
|
// Should try to find relay service providers
|
||||||
await pWaitFor(() => contentRoutingFindProvidersSpy.callCount === 1, {
|
await pWaitFor(() => localDelegate.findProviders.callCount === 1, {
|
||||||
timeout: 1000
|
timeout: 1000
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { pipe } from 'it-pipe'
|
import { pipe } from 'it-pipe'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { createNode } from '../utils/creators/peer.js'
|
import { createNode } from '../utils/creators/peer.js'
|
||||||
@ -150,7 +150,7 @@ describe('Dialing (via relay, TCP)', () => {
|
|||||||
|
|
||||||
// Connect the destination peer and the relay
|
// Connect the destination peer and the relay
|
||||||
const tcpAddrs = dstLibp2p.components.getTransportManager().getAddrs()
|
const tcpAddrs = dstLibp2p.components.getTransportManager().getAddrs()
|
||||||
sinon.stub(dstLibp2p.components.getAddressManager(), 'getListenAddrs').returns([new Multiaddr(`${relayAddr.toString()}/p2p-circuit`)])
|
sinon.stub(dstLibp2p.components.getAddressManager(), 'getListenAddrs').returns([multiaddr(`${relayAddr.toString()}/p2p-circuit`)])
|
||||||
|
|
||||||
await dstLibp2p.components.getTransportManager().listen(dstLibp2p.components.getAddressManager().getListenAddrs())
|
await dstLibp2p.components.getTransportManager().listen(dstLibp2p.components.getAddressManager().getListenAddrs())
|
||||||
expect(dstLibp2p.components.getTransportManager().getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')])
|
expect(dstLibp2p.components.getTransportManager().getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')])
|
||||||
|
@ -7,7 +7,7 @@ import { DefaultTransportManager } from '../../src/transport-manager.js'
|
|||||||
import { PersistentPeerStore } from '@libp2p/peer-store'
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
||||||
import { PeerRecord } from '@libp2p/peer-record'
|
import { PeerRecord } from '@libp2p/peer-record'
|
||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { mockUpgrader } from '@libp2p/interface-mocks'
|
import { mockUpgrader } from '@libp2p/interface-mocks'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import Peers from '../fixtures/peers.js'
|
import Peers from '../fixtures/peers.js'
|
||||||
@ -18,8 +18,8 @@ import { Components } from '@libp2p/components'
|
|||||||
import { PeerRecordUpdater } from '../../src/peer-record-updater.js'
|
import { PeerRecordUpdater } from '../../src/peer-record-updater.js'
|
||||||
|
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
|
multiaddr('/ip4/127.0.0.1/tcp/0'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
]
|
]
|
||||||
|
|
||||||
describe('Transport Manager (TCP)', () => {
|
describe('Transport Manager (TCP)', () => {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { WebSockets } from '@libp2p/websockets'
|
import { WebSockets } from '@libp2p/websockets'
|
||||||
import * as filters from '@libp2p/websockets/filters'
|
import * as filters from '@libp2p/websockets/filters'
|
||||||
import { Plaintext } from '../../src/insecure/index.js'
|
import { Plaintext } from '../../src/insecure/index.js'
|
||||||
@ -17,7 +17,7 @@ import { createEd25519PeerId, createFromJSON } from '@libp2p/peer-id-factory'
|
|||||||
import type { PeerId } from '@libp2p/interface-peer-id'
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
||||||
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
||||||
|
|
||||||
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
|
||||||
describe('Transport Manager (WebSockets)', () => {
|
describe('Transport Manager (WebSockets)', () => {
|
||||||
let tm: DefaultTransportManager
|
let tm: DefaultTransportManager
|
||||||
@ -71,7 +71,7 @@ describe('Transport Manager (WebSockets)', () => {
|
|||||||
|
|
||||||
it('should fail to dial an unsupported address', async () => {
|
it('should fail to dial an unsupported address', async () => {
|
||||||
tm.add(new WebSockets({ filter: filters.all }))
|
tm.add(new WebSockets({ filter: filters.all }))
|
||||||
const addr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
const addr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
await expect(tm.dial(addr))
|
await expect(tm.dial(addr))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
|
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import { expect } from 'aegir/chai'
|
import { expect } from 'aegir/chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import { Mplex } from '@libp2p/mplex'
|
import { Mplex } from '@libp2p/mplex'
|
||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import { pipe } from 'it-pipe'
|
import { pipe } from 'it-pipe'
|
||||||
import all from 'it-all'
|
import all from 'it-all'
|
||||||
import pSettle from 'p-settle'
|
import pSettle from 'p-settle'
|
||||||
@ -34,8 +34,8 @@ import { PersistentPeerStore } from '@libp2p/peer-store'
|
|||||||
import { MemoryDatastore } from 'datastore-core'
|
import { MemoryDatastore } from 'datastore-core'
|
||||||
|
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
|
multiaddr('/ip4/127.0.0.1/tcp/0'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
]
|
]
|
||||||
|
|
||||||
describe('Upgrader', () => {
|
describe('Upgrader', () => {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Multiaddr } from '@multiformats/multiaddr'
|
import { multiaddr } from '@multiformats/multiaddr'
|
||||||
import Peers from '../../fixtures/peers.js'
|
import Peers from '../../fixtures/peers.js'
|
||||||
import { createBaseOptions } from '../base-options.browser.js'
|
import { createBaseOptions } from '../base-options.browser.js'
|
||||||
import { createEd25519PeerId, createFromJSON, createRSAPeerId } from '@libp2p/peer-id-factory'
|
import { createEd25519PeerId, createFromJSON, createRSAPeerId } from '@libp2p/peer-id-factory'
|
||||||
@ -6,7 +6,7 @@ import { createLibp2pNode, Libp2pNode } from '../../../src/libp2p.js'
|
|||||||
import type { AddressesConfig, Libp2pOptions } from '../../../src/index.js'
|
import type { AddressesConfig, Libp2pOptions } from '../../../src/index.js'
|
||||||
import type { PeerId } from '@libp2p/interface-peer-id'
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
||||||
|
|
||||||
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
|
||||||
export interface CreatePeerOptions {
|
export interface CreatePeerOptions {
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user