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 5019a60
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 68 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
7 changes: 5 additions & 2 deletions src/peer-store/persistent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ class PersistentPeerStore extends PeerStore {

const encodedData = Addresses.encode({
addrs: entry.addresses.map((address) => ({
multiaddr: address.multiaddr.buffer
multiaddr: address.multiaddr.buffer,
isCertified: address.isCertified
})),
certified_record: entry.record ? {
seq: entry.record.seqNumber,
raw: entry.record.raw
} : undefined
})

batch.put(key, encodedData)
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -302,7 +304,8 @@ class PersistentPeerStore extends PeerStore {
peerId,
{
addresses: decoded.addrs.map((address) => ({
multiaddr: multiaddr(address.multiaddr)
multiaddr: multiaddr(address.multiaddr),
isCertified: Boolean(address.isCertified)
})),
record: decoded.certified_record ? {
raw: decoded.certified_record.raw,
Expand Down
3 changes: 3 additions & 0 deletions src/peer-store/persistent/pb/address-book.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ message Addresses {
// Address represents a single multiaddr.
message Address {
required bytes multiaddr = 1;
// Flag to indicate if the address comes from a certified source.
optional bool isCertified = 2;
}
// CertifiedRecord contains a serialized signed PeerRecord used to
Expand Down
16 changes: 7 additions & 9 deletions src/record/envelope/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ const formatSignaturePayload = (domain, payloadType, payload) => {
])
}

const unmarshalEnvelope = async (data) => {
/**
* Unmarshal a serialized Envelope protobuf message.
* @param {Buffer} data
* @return {Promise<Envelope>}
*/
Envelope.createFromProtobuf = async (data) => {
const envelopeData = Protobuf.decode(data)
const peerId = await PeerId.createFromPubKey(envelopeData.public_key)

Expand All @@ -124,13 +129,6 @@ const unmarshalEnvelope = async (data) => {
})
}

/**
* Unmarshal a serialized Envelope protobuf message.
* @param {Buffer} data
* @return {Promise<Envelope>}
*/
Envelope.createFromProtobuf = unmarshalEnvelope

/**
* Seal marshals the given Record, places the marshaled bytes inside an Envelope
* and signs it with the given peerId's private key.
Expand Down Expand Up @@ -163,7 +161,7 @@ Envelope.seal = async (record, peerId) => {
* @return {Envelope}
*/
Envelope.openAndCertify = async (data, domain) => {
const envelope = await unmarshalEnvelope(data)
const envelope = await Envelope.createFromProtobuf(data)
const valid = await envelope.validate(domain)

if (!valid) {
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
Loading

0 comments on commit 5019a60

Please sign in to comment.