Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 17, 2020
1 parent 864d153 commit 90b8761
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AddressBook extends Book {
}

// Verify peerId
if (peerRecord.peerId.toB58String() !== envelope.peerId.toB58String()) {
if (!peerRecord.peerId.equals(envelope.peerId)) {
log('signing key does not match PeerId in the PeerRecord')
return false
}
Expand Down Expand Up @@ -220,10 +220,10 @@ class AddressBook extends Book {
const id = peerId.toB58String()

const entry = this.data.get(id) || {}
const rec = entry.addresses
const rec = entry.addresses || []

// Add recorded uniquely to the new array (Union)
rec && rec.forEach((addr) => {
rec.forEach((addr) => {
if (!addresses.find(r => r.multiaddr.equals(addr.multiaddr))) {
addresses.push(addr)
}
Expand All @@ -244,7 +244,7 @@ class AddressBook extends Book {
log(`added provided multiaddrs for ${id}`)

// Notify the existance of a new peer
if (!rec) {
if (!entry.addresses) {
this._ps.emit('peer', peerId)
}

Expand Down
2 changes: 1 addition & 1 deletion src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PeerStore extends EventEmitter {
/**
* @constructor
*/
constructor ({ peerId } = {}) {
constructor ({ peerId }) {
super()

this._peerId = peerId
Expand Down
6 changes: 3 additions & 3 deletions test/dialing/direct.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Dialing (direct, TCP)', () => {
})
remoteTM.add(Transport.prototype[Symbol.toStringTag], Transport)

peerStore = new PeerStore()
peerStore = new PeerStore({ peerId: remotePeerId })
localTM = new TransportManager({
libp2p: {},
upgrader: mockUpgrader
Expand Down Expand Up @@ -97,13 +97,13 @@ describe('Dialing (direct, TCP)', () => {
})

it('should be able to connect to a given peer id', async () => {
const peerStore = new PeerStore()
const peerId = await PeerId.createFromJSON(Peers[0])
const peerStore = new PeerStore({ peerId })
const dialer = new Dialer({
transportManager: localTM,
peerStore
})

const peerId = await PeerId.createFromJSON(Peers[0])
peerStore.addressBook.set(peerId, [remoteAddr])

const connection = await dialer.connectToPeer(peerId)
Expand Down
17 changes: 4 additions & 13 deletions test/dialing/direct.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const Transport = require('libp2p-websockets')
const Muxer = require('libp2p-mplex')
const { NOISE: Crypto } = require('libp2p-noise')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const AggregateError = require('aggregate-error')
const { AbortError } = require('libp2p-interfaces/src/transport/errors')

Expand All @@ -24,7 +23,6 @@ const PeerStore = require('../../src/peer-store')
const TransportManager = require('../../src/transport-manager')
const Libp2p = require('../../src')

const Peers = require('../fixtures/peers')
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
const mockUpgrader = require('../utils/mockUpgrader')
const createMockConnection = require('../utils/mockConnection')
Expand All @@ -35,9 +33,11 @@ const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
describe('Dialing (direct, WebSockets)', () => {
let localTM
let peerStore
let peerId

before(() => {
peerStore = new PeerStore()
before(async () => {
[peerId] = await createPeerId()
peerStore = new PeerStore({ peerId })
localTM = new TransportManager({
libp2p: {},
upgrader: mockUpgrader,
Expand Down Expand Up @@ -132,7 +132,6 @@ describe('Dialing (direct, WebSockets)', () => {
}
}
})
const peerId = await PeerId.createFromJSON(Peers[0])

const connection = await dialer.connectToPeer(peerId)
expect(connection).to.exist()
Expand All @@ -149,7 +148,6 @@ describe('Dialing (direct, WebSockets)', () => {
}
}
})
const peerId = await PeerId.createFromJSON(Peers[0])

await expect(dialer.connectToPeer(peerId))
.to.eventually.be.rejectedWith(AggregateError)
Expand Down Expand Up @@ -198,7 +196,6 @@ describe('Dialing (direct, WebSockets)', () => {
const deferredDial = pDefer()
sinon.stub(localTM, 'dial').callsFake(() => deferredDial.promise)

const [peerId] = await createPeerId()
// Perform 3 multiaddr dials
dialer.connectToPeer(peerId)

Expand Down Expand Up @@ -245,7 +242,6 @@ describe('Dialing (direct, WebSockets)', () => {
})

// Perform 3 multiaddr dials
const [peerId] = await createPeerId()
const dialPromise = dialer.connectToPeer(peerId)

// Let the call stack run
Expand All @@ -266,14 +262,9 @@ describe('Dialing (direct, WebSockets)', () => {
})

describe('libp2p.dialer', () => {
let peerId
let libp2p
let remoteLibp2p

before(async () => {
peerId = await PeerId.createFromJSON(Peers[0])
})

afterEach(async () => {
sinon.restore()
libp2p && await libp2p.stop()
Expand Down
20 changes: 10 additions & 10 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs
},
protocols
Expand All @@ -60,7 +60,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: listenMaddrs
},
protocols
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs
},
protocols
Expand All @@ -113,7 +113,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: listenMaddrs
},
protocols
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: []
},
protocols
Expand All @@ -165,7 +165,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: []
},
protocols
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs
},
protocols: new Map([
Expand All @@ -215,7 +215,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager,
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: []
}
})
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs
},
protocols: new Map([
Expand All @@ -276,7 +276,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager,
peerStore: new PeerStore(),
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: []
}
})
Expand Down
16 changes: 8 additions & 8 deletions test/peer-store/address-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('addressBook', () => {
let peerStore, ab

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -150,7 +150,7 @@ describe('addressBook', () => {
let peerStore, ab

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -278,7 +278,7 @@ describe('addressBook', () => {
let peerStore, ab

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -313,7 +313,7 @@ describe('addressBook', () => {
let peerStore, ab

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -349,7 +349,7 @@ describe('addressBook', () => {
let peerStore, ab

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -405,9 +405,9 @@ describe('addressBook', () => {
describe('certified records', () => {
let peerStore, ab

describe('consumes successfully a valid peer record and stores its data', () => {
describe('consumes a valid peer record and stores its data', () => {
beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down Expand Up @@ -600,7 +600,7 @@ describe('addressBook', () => {

describe('fails to consume invalid peer records', () => {
beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
ab = peerStore.addressBook
})

Expand Down
2 changes: 1 addition & 1 deletion test/peer-store/key-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('keyBook', () => {

beforeEach(async () => {
[peerId] = await peerUtils.createPeerId()
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
kb = peerStore.keyBook
})

Expand Down
10 changes: 5 additions & 5 deletions test/peer-store/metadata-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('metadataBook', () => {
let peerStore, mb

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
mb = peerStore.metadataBook
})

Expand Down Expand Up @@ -158,7 +158,7 @@ describe('metadataBook', () => {
let peerStore, mb

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
mb = peerStore.metadataBook
})

Expand Down Expand Up @@ -194,7 +194,7 @@ describe('metadataBook', () => {
let peerStore, mb

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
mb = peerStore.metadataBook
})

Expand Down Expand Up @@ -243,7 +243,7 @@ describe('metadataBook', () => {
let peerStore, mb

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
mb = peerStore.metadataBook
})

Expand Down Expand Up @@ -300,7 +300,7 @@ describe('metadataBook', () => {
let peerStore, mb

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId })
mb = peerStore.metadataBook
})

Expand Down
8 changes: 4 additions & 4 deletions test/peer-store/peer-store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe('peer-store', () => {
let peerIds
before(async () => {
peerIds = await peerUtils.createPeerId({
number: 4
number: 5
})
})

describe('empty books', () => {
let peerStore

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId: peerIds[4] })
})

it('has an empty map of peers', () => {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('peer-store', () => {
let peerStore

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId: peerIds[4] })

// Add peer0 with { addr1, addr2 } and { proto1 }
peerStore.addressBook.set(peerIds[0], [addr1, addr2])
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('peer-store', () => {
let peerStore

beforeEach(() => {
peerStore = new PeerStore()
peerStore = new PeerStore({ peerId: peerIds[4] })
})

it('returns peers if only addresses are known', () => {
Expand Down
Loading

0 comments on commit 90b8761

Please sign in to comment.