Skip to content

Commit 65e8746

Browse files
vasco-santosjacobheun
authored andcommittedMay 28, 2020
chore: add keys to keybook on connection upgraded
1 parent 7b8d016 commit 65e8746

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed
 

‎src/connection-manager/index.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,22 @@ class ConnectionManager extends EventEmitter {
171171
* @param {Connection} connection
172172
*/
173173
onConnect (connection) {
174-
const peerId = connection.remotePeer.toB58String()
175-
const storedConn = this.connections.get(peerId)
174+
const peerId = connection.remotePeer
175+
const peerIdStr = peerId.toB58String()
176+
const storedConn = this.connections.get(peerIdStr)
176177

177178
if (storedConn) {
178179
storedConn.push(connection)
179180
} else {
180-
this.connections.set(peerId, [connection])
181+
this.connections.set(peerIdStr, [connection])
181182
this.emit('peer:connect', connection)
182183
}
183184

184-
if (!this._peerValues.has(peerId)) {
185-
this._peerValues.set(peerId, this._options.defaultPeerValue)
185+
this._libp2p.peerStore.addressBook.add(peerId, [connection.remoteAddr])
186+
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
187+
188+
if (!this._peerValues.has(peerIdStr)) {
189+
this._peerValues.set(peerIdStr, this._options.defaultPeerValue)
186190
}
187191

188192
this._checkLimit('maxConnections', this.size)

‎test/identify/index.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ describe('Identify', () => {
242242
expect(connection).to.exist()
243243

244244
// Wait for peer store to be updated
245-
// Dialer._createDialTarget (add), Identify (replace)
246-
await pWaitFor(() => peerStoreSpySet.callCount === 1 && peerStoreSpyAdd.callCount === 1)
245+
// Dialer._createDialTarget (add), Connected (add), Identify (replace)
246+
await pWaitFor(() => peerStoreSpySet.callCount === 1 && peerStoreSpyAdd.callCount === 2)
247247
expect(libp2p.identifyService.identify.callCount).to.equal(1)
248248

249249
// The connection should have no open streams

‎test/peer-store/peer-store.node.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6-
chai.use(require('chai-as-promised'))
6+
chai.use(require('chai-bytes'))
77
const { expect } = chai
88
const sinon = require('sinon')
99

@@ -23,12 +23,29 @@ describe('libp2p.peerStore', () => {
2323
})
2424
})
2525

26-
it('adds peer address to AddressBook when establishing connection', async () => {
26+
it('adds peer address to AddressBook and keys to the keybook when establishing connection', async () => {
27+
const idStr = libp2p.peerId.toB58String()
28+
const remoteIdStr = remoteLibp2p.peerId.toB58String()
29+
2730
const spyAddressBook = sinon.spy(libp2p.peerStore.addressBook, 'add')
28-
const remoteMultiaddr = `${remoteLibp2p.multiaddrs[0]}/p2p/${remoteLibp2p.peerId.toB58String()}`
31+
const spyKeyBook = sinon.spy(libp2p.peerStore.keyBook, 'set')
32+
33+
const remoteMultiaddr = `${remoteLibp2p.multiaddrs[0]}/p2p/${remoteIdStr}`
2934
const conn = await libp2p.dial(remoteMultiaddr)
3035

3136
expect(conn).to.exist()
32-
expect(spyAddressBook).to.have.property('callCount', 1)
37+
expect(spyAddressBook).to.have.property('called', true)
38+
expect(spyKeyBook).to.have.property('called', true)
39+
40+
const localPeers = libp2p.peerStore.peers
41+
expect(localPeers.size).to.equal(1)
42+
// const publicKeyInLocalPeer = localPeers.get(remoteIdStr).id.pubKey
43+
// expect(publicKeyInLocalPeer.bytes).to.equalBytes(remoteLibp2p.peerId.pubKey.bytes)
44+
45+
const remotePeers = remoteLibp2p.peerStore.peers
46+
expect(remotePeers.size).to.equal(1)
47+
const publicKeyInRemotePeer = remotePeers.get(idStr).id.pubKey
48+
expect(publicKeyInRemotePeer).to.exist()
49+
expect(publicKeyInRemotePeer.bytes).to.equalBytes(libp2p.peerId.pubKey.bytes)
3350
})
3451
})

0 commit comments

Comments
 (0)
Please sign in to comment.