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

Commit

Permalink
fix: use libp2p dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 22, 2019
1 parent 44054b2 commit a0a8500
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ class Network {
throw errcode(new Error('Network is offline'), 'ERR_NETWORK_OFFLINE')
}

this._log('sending to: %s', to.toB58String())
const id = to.toB58String()
this._log('sending to: %s', id)

const { stream } = await this.dht.dialer.dialProtocol(to, c.PROTOCOL_DHT)
const conn = await this.dht.dialer.connectToPeer(to)
const { stream } = await conn.newStream(c.PROTOCOL_DHT)

return this._writeReadMessage(stream, msg.serialize())
}
Expand All @@ -136,9 +138,12 @@ class Network {
throw errcode(new Error('Network is offline'), 'ERR_NETWORK_OFFLINE')
}

this._log('sending to: %s', to.toB58String())
const id = to.toB58String()
this._log('sending to: %s', id)

const conn = await this.dht.dialer.connectToPeer(to)
const { stream } = await conn.newStream(c.PROTOCOL_DHT)

const { stream } = await this.dht.dialer.dialProtocol(to, c.PROTOCOL_DHT)
return this._writeMessage(stream, msg.serialize())
}

Expand Down
23 changes: 12 additions & 11 deletions test/network.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ describe('Network', () => {
const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0)

// mock dial
dht.dialer.dialProtocol = (peer, protocol) => {
expect(protocol).to.eql('/ipfs/kad/1.0.0')

dht.dialer.connectToPeer = () => {
return {
stream: pair() // {source, sink} streams that are internally connected
newStream: () => {
return { stream: pair() } // {source, sink} streams that are internally connected
}
}
}

Expand All @@ -56,8 +56,7 @@ describe('Network', () => {
const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0)

// mock it
dht.dialer.dialProtocol = async (peer, protocol) => {
expect(protocol).to.eql('/ipfs/kad/1.0.0')
dht.dialer.connectToPeer = async () => {
const msg = new Message(Message.TYPES.FIND_NODE, Buffer.from('world'), 0)

const data = []
Expand Down Expand Up @@ -95,7 +94,9 @@ describe('Network', () => {
}

return {
stream: { source, sink }
newStream: () => {
return { stream: { source, sink } }
}
}
}

Expand All @@ -119,9 +120,7 @@ describe('Network', () => {
const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0)

// mock it
dht.dialer.dialProtocol = (peer, protocol) => {
expect(protocol).to.eql('/ipfs/kad/1.0.0')

dht.dialer.connectToPeer = () => {
const source = (async function * () { // eslint-disable-line require-yield
await delay(1000)
})()
Expand All @@ -142,7 +141,9 @@ describe('Network', () => {
}

return {
stream: { source, sink }
newStream: () => {
return { stream: { source, sink } }
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions test/utils/test-dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestDHT {

async _spawnOne (index, options = {}) {
const regRecord = {}
const peerStore = new PeerBook()

// Disable random walk by default for more controlled testing
options = {
Expand All @@ -41,7 +42,7 @@ class TestDHT {

p.multiaddrs.add(`/ip4/127.0.0.1/tcp/${port}/p2p/${p.id.toB58String()}`)

const dial = async (peer, protocol) => {
const connectToPeer = async (peer) => {
const remotePeerB58 = peer.toB58String()
const remoteDht = this.nodes.find(
(node) => node.peerInfo.id.toB58String() === remotePeerB58
Expand All @@ -58,25 +59,26 @@ class TestDHT {
await remoteOnConnect(p, c0)

await remoteHandler({
protocol: protocol,
protocol: PROTOCOL_DHT,
stream: c0.stream,
connection: {
remotePeer: p.id
}
})

return {
stream: c1.stream
newStream: () => {
return { stream: c1.stream }
}
}
}

const dht = new KadDHT({
dialer: {
dial,
dialProtocol: dial
connectToPeer
},
registrar: createMockRegistrar(regRecord),
peerStore: new PeerBook(),
peerStore,
peerInfo: p,
validators: {
v: {
Expand Down

0 comments on commit a0a8500

Please sign in to comment.