From a0a850093ff0422530773dc83b6d5e9a0518ff11 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 22 Nov 2019 16:26:41 -0600 Subject: [PATCH] fix: use libp2p dialer --- src/network.js | 13 +++++++++---- test/network.spec.js | 23 ++++++++++++----------- test/utils/test-dht.js | 14 ++++++++------ 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/network.js b/src/network.js index a76b7965..b23efb3e 100644 --- a/src/network.js +++ b/src/network.js @@ -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()) } @@ -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()) } diff --git a/test/network.spec.js b/test/network.spec.js index 39cafc17..8c0a9fb5 100644 --- a/test/network.spec.js +++ b/test/network.spec.js @@ -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 + } } } @@ -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 = [] @@ -95,7 +94,9 @@ describe('Network', () => { } return { - stream: { source, sink } + newStream: () => { + return { stream: { source, sink } } + } } } @@ -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) })() @@ -142,7 +141,9 @@ describe('Network', () => { } return { - stream: { source, sink } + newStream: () => { + return { stream: { source, sink } } + } } } diff --git a/test/utils/test-dht.js b/test/utils/test-dht.js index ad9a2b86..0acf3ec6 100644 --- a/test/utils/test-dht.js +++ b/test/utils/test-dht.js @@ -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 = { @@ -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 @@ -58,7 +59,7 @@ class TestDHT { await remoteOnConnect(p, c0) await remoteHandler({ - protocol: protocol, + protocol: PROTOCOL_DHT, stream: c0.stream, connection: { remotePeer: p.id @@ -66,17 +67,18 @@ class TestDHT { }) 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: {