Skip to content

Commit ce38033

Browse files
vasco-santosjacobheun
authored andcommittedMay 28, 2020
feat: keybook
1 parent 3f2b06d commit ce38033

12 files changed

+363
-72
lines changed
 

‎doc/API.md

+86
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* [`peerStore.addressBook.get`](#peerstoreaddressbookget)
2727
* [`peerStore.addressBook.getMultiaddrsForPeer`](#peerstoreaddressbookgetmultiaddrsforpeer)
2828
* [`peerStore.addressBook.set`](#peerstoreaddressbookset)
29+
* [`peerStore.keyBook.delete`](#peerstorekeybookdelete)
30+
* [`peerStore.keyBook.get`](#peerstorekeybookget)
31+
* [`peerStore.keyBook.set`](#peerstorekeybookset)
2932
* [`peerStore.protoBook.add`](#peerstoreprotobookadd)
3033
* [`peerStore.protoBook.delete`](#peerstoreprotobookdelete)
3134
* [`peerStore.protoBook.get`](#peerstoreprotobookget)
@@ -811,6 +814,89 @@ Add known `protocols` of a given peer.
811814
peerStore.protoBook.add(peerId, protocols)
812815
```
813816

817+
* [`peerStore.keyBook.get`](#peerstorekeybookget)
818+
* [`peerStore.keyBook.set`](#peerstorekeybookset)
819+
820+
### peerStore.keyBook.delete
821+
822+
Delete the provided peer from the book.
823+
824+
`peerStore.keyBook.delete(peerId)`
825+
826+
#### Parameters
827+
828+
| Name | Type | Description |
829+
|------|------|-------------|
830+
| peerId | [`PeerId`][peer-id] | peerId to remove |
831+
832+
#### Returns
833+
834+
| Type | Description |
835+
|------|-------------|
836+
| `boolean` | true if found and removed |
837+
838+
#### Example
839+
840+
```js
841+
peerStore.keyBook.delete(peerId)
842+
// false
843+
peerStore.keyBook.set(peerId)
844+
peerStore.keyBook.delete(peerId)
845+
// true
846+
```
847+
848+
### peerStore.keyBook.get
849+
850+
Get the known `PublicKey` of a provided peer.
851+
852+
`peerStore.keyBook.get(peerId)`
853+
854+
#### Parameters
855+
856+
| Name | Type | Description |
857+
|------|------|-------------|
858+
| peerId | [`PeerId`][peer-id] | peerId to get |
859+
860+
#### Returns
861+
862+
| Type | Description |
863+
|------|-------------|
864+
| `RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey` | Peer PublicKey |
865+
866+
#### Example
867+
868+
```js
869+
peerStore.keyBook.get(peerId)
870+
// undefined
871+
peerStore.keyBook.set(peerId) // with inline public key
872+
peerStore.keyBook.get(peerId)
873+
// PublicKey
874+
```
875+
876+
### peerStore.keyBook.set
877+
878+
Set known `peerId`. This can include its Public Key.
879+
880+
`peerStore.keyBook.set(peerId)`
881+
882+
#### Parameters
883+
884+
| Name | Type | Description |
885+
|------|------|-------------|
886+
| peerId | [`PeerId`][peer-id] | peerId to set |
887+
888+
#### Returns
889+
890+
| Type | Description |
891+
|------|-------------|
892+
| `KeyBook` | Returns the Key Book component |
893+
894+
#### Example
895+
896+
```js
897+
peerStore.keyBook.set(peerId)
898+
```
899+
814900
### peerStore.protoBook.delete
815901

816902
Delete the provided peer from the book.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"aegir": "^22.0.0",
8484
"chai": "^4.2.0",
8585
"chai-as-promised": "^7.1.1",
86+
"chai-bytes": "^0.1.2",
8687
"cids": "^0.8.0",
8788
"delay": "^4.3.0",
8889
"dirty-chai": "^2.0.1",

‎src/peer-store/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ A `peerId.toString()` identifier mapping to a `Address` object, which should hav
5252

5353
#### Key Book
5454

55-
The `keyBook` tracks the keys of the peers.
55+
The `keyBook` tracks the publick keys of the peers by keeping their [`PeerId`][peer-id].
5656

57-
**Not Yet Implemented**
57+
`Map<string, PeerId`
58+
59+
A `peerId.toString()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
5860

5961
#### Protocol Book
6062

@@ -74,8 +76,9 @@ For the complete API documentation, you should check the [API.md](../../doc/API.
7476

7577
Access to its underlying books:
7678

77-
- `peerStore.protoBook.*`
7879
- `peerStore.addressBook.*`
80+
- `peerStore.keyBook.*`
81+
- `peerStore.protoBook.*`
7982

8083
### Events
8184

@@ -107,8 +110,6 @@ All the known peer protocols are stored with a key pattern as follows:
107110

108111
**KeyBook**
109112

110-
_NOT_YET_IMPLEMENTED_
111-
112113
All public keys are stored under the following pattern:
113114

114115
` /peers/keys/<b32 peer id no padding>`
@@ -127,3 +128,5 @@ Metadata is stored under the following key pattern:
127128
- Further API methods will probably need to be added in the context of multiaddr validity and confidence.
128129
- When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
129130
- When improving libp2p configuration, we should think about a possible way of allowing the configuration of Bootstrap to be influenced by the persisted peers, as a way to decrease the load on Bootstrap nodes.
131+
132+
[peer-id]: https://github.com/libp2p/js-peer-id

‎src/peer-store/address-book.js

-10
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ class AddressBook extends Book {
8686
this._setData(peerId, addresses)
8787
log(`stored provided multiaddrs for ${id}`)
8888

89-
// Notify the existance of a new peer
90-
if (!rec) {
91-
this._ps.emit('peer', peerId)
92-
}
93-
9489
return this
9590
}
9691

@@ -130,11 +125,6 @@ class AddressBook extends Book {
130125

131126
log(`added provided multiaddrs for ${id}`)
132127

133-
// Notify the existance of a new peer
134-
if (!rec) {
135-
this._ps.emit('peer', peerId)
136-
}
137-
138128
return this
139129
}
140130

‎src/peer-store/book.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Book {
4747
* Set data into the datastructure, persistence and emit it using the provided transformers.
4848
* @private
4949
* @param {PeerId} peerId peerId of the data to store
50-
* @param {Array<*>} data data to store.
50+
* @param {*} data data to store.
5151
* @param {Object} [options] storing options.
5252
* @param {boolean} [options.emit = true] emit the provided data.
5353
* @return {void}
@@ -57,22 +57,27 @@ class Book {
5757

5858
// Store data in memory
5959
this.data.set(b58key, data)
60-
this._setPeerId(peerId)
60+
61+
// Store PeerId
62+
if (!PeerId.isPeerId(data)) {
63+
this._ps.keyBook.set(peerId)
64+
}
6165

6266
// Emit event
63-
emit && this._ps.emit(this.eventName, {
64-
peerId,
65-
[this.eventProperty]: this.eventTransformer(data)
66-
})
67+
emit && this._emit(peerId, data)
6768
}
6869

6970
/**
70-
* Add known data of a provided peer.
71+
* Emit data.
72+
* @private
7173
* @param {PeerId} peerId
72-
* @param {Array<Data>|Data} data
74+
* @param {*} data
7375
*/
74-
add (peerId, data) {
75-
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
76+
_emit (peerId, data) {
77+
this._ps.emit(this.eventName, {
78+
peerId,
79+
[this.eventProperty]: this.eventTransformer(data)
80+
})
7681
}
7782

7883
/**
@@ -104,24 +109,10 @@ class Book {
104109
return false
105110
}
106111

107-
this._ps.emit(this.eventName, {
108-
peerId,
109-
[this.eventProperty]: []
110-
})
112+
this._emit(peerId, [])
111113

112114
return true
113115
}
114-
115-
/**
116-
* Set PeerId into peerStore datastructure.
117-
* @private
118-
* @param {PeerId} peerId
119-
*/
120-
_setPeerId (peerId) {
121-
if (!this._ps.peerIds.get(peerId)) {
122-
this._ps.peerIds.set(peerId.toB58String(), peerId)
123-
}
124-
}
125116
}
126117

127118
module.exports = Book

‎src/peer-store/index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { EventEmitter } = require('events')
99
const PeerId = require('peer-id')
1010

1111
const AddressBook = require('./address-book')
12+
const KeyBook = require('./key-book')
1213
const ProtoBook = require('./proto-book')
1314

1415
const {
@@ -42,16 +43,14 @@ class PeerStore extends EventEmitter {
4243
this.addressBook = new AddressBook(this)
4344

4445
/**
45-
* ProtoBook containing a map of peerIdStr to supported protocols.
46+
* KeyBook containing a map of peerIdStr to their PeerId with public keys.
4647
*/
47-
this.protoBook = new ProtoBook(this)
48+
this.keyBook = new KeyBook(this)
4849

4950
/**
50-
* TODO: this should only exist until we have the key-book
51-
* Map known peers to their peer-id.
52-
* @type {Map<string, Array<PeerId>}
51+
* ProtoBook containing a map of peerIdStr to supported protocols.
5352
*/
54-
this.peerIds = new Map()
53+
this.protoBook = new ProtoBook(this)
5554
}
5655

5756
/**
@@ -73,7 +72,7 @@ class PeerStore extends EventEmitter {
7372

7473
// AddressBook
7574
for (const [idStr, addresses] of this.addressBook.data.entries()) {
76-
const id = PeerId.createFromCID(idStr)
75+
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)
7776
peersData.set(idStr, {
7877
id,
7978
addresses,
@@ -84,10 +83,11 @@ class PeerStore extends EventEmitter {
8483
// ProtoBook
8584
for (const [idStr, protocols] of this.protoBook.data.entries()) {
8685
const pData = peersData.get(idStr)
86+
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)
8787

8888
if (!pData) {
8989
peersData.set(idStr, {
90-
id: PeerId.createFromCID(idStr),
90+
id,
9191
addresses: [],
9292
protocols: Array.from(protocols)
9393
})
@@ -104,8 +104,10 @@ class PeerStore extends EventEmitter {
104104
*/
105105
delete (peerId) {
106106
const addressesDeleted = this.addressBook.delete(peerId)
107+
const keyDeleted = this.keyBook.delete(peerId)
107108
const protocolsDeleted = this.protoBook.delete(peerId)
108-
return addressesDeleted || protocolsDeleted
109+
110+
return addressesDeleted || keyDeleted || protocolsDeleted
109111
}
110112

111113
/**
@@ -118,7 +120,7 @@ class PeerStore extends EventEmitter {
118120
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
119121
}
120122

121-
const id = this.peerIds.get(peerId.toB58String())
123+
const id = this.keyBook.data.get(peerId.toB58String())
122124
const addresses = this.addressBook.get(peerId)
123125
const protocols = this.protoBook.get(peerId)
124126

‎src/peer-store/key-book.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict'
2+
3+
const errcode = require('err-code')
4+
const debug = require('debug')
5+
const log = debug('libp2p:peer-store:key-book')
6+
log.error = debug('libp2p:peer-store:key-book:error')
7+
8+
const PeerId = require('peer-id')
9+
10+
const Book = require('./book')
11+
12+
const {
13+
codes: { ERR_INVALID_PARAMETERS }
14+
} = require('../errors')
15+
16+
/**
17+
* The KeyBook is responsible for keeping the known public keys of a peer.
18+
*/
19+
class KeyBook extends Book {
20+
/**
21+
* @constructor
22+
* @param {PeerStore} peerStore
23+
*/
24+
constructor (peerStore) {
25+
super({
26+
peerStore,
27+
eventName: 'change:pubkey', // TODO: the name is not probably the best!?
28+
eventProperty: 'pubkey',
29+
eventTransformer: (data) => data.pubKey
30+
})
31+
32+
/**
33+
* Map known peers to their known Public Key.
34+
* @type {Map<string, PeerId>}
35+
*/
36+
this.data = new Map()
37+
}
38+
39+
/**
40+
* Set PeerId. If the peer was not known before, it will be added.
41+
* @override
42+
* @param {PeerId} peerId
43+
* @return {KeyBook}
44+
*/
45+
set (peerId) {
46+
if (!PeerId.isPeerId(peerId)) {
47+
log.error('peerId must be an instance of peer-id to store data')
48+
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
49+
}
50+
51+
const id = peerId.toB58String()
52+
const recPeerId = this.data.get(id)
53+
54+
!recPeerId && this._ps.emit('peer', peerId)
55+
// If no record available, or it is incomplete
56+
if (!recPeerId || (peerId.pubKey && !recPeerId.pubKey)) {
57+
this._setData(peerId, peerId, {
58+
emit: Boolean(peerId.pubKey) // No persistence if no public key
59+
})
60+
log(`stored provided public key for ${id}`)
61+
}
62+
63+
return this
64+
}
65+
66+
/**
67+
* Get Public key of the given PeerId, if stored.
68+
* @override
69+
* @param {PeerId} peerId
70+
* @return {PublicKey}
71+
*/
72+
get (peerId) {
73+
if (!PeerId.isPeerId(peerId)) {
74+
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
75+
}
76+
77+
const rec = this.data.get(peerId.toB58String())
78+
79+
return rec ? rec.pubKey : undefined
80+
}
81+
}
82+
83+
module.exports = KeyBook

‎src/peer-store/persistent/consts.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ module.exports.NAMESPACE_COMMON = '/peers/'
55
// /peers/protos/<b32 peer id no padding>
66
module.exports.NAMESPACE_ADDRESS = '/peers/addrs/'
77

8+
// /peers/keys/<b32 peer id no padding>
9+
module.exports.NAMESPACE_KEYS = '/peers/keys/'
10+
811
// /peers/addrs/<b32 peer id no padding>
912
module.exports.NAMESPACE_PROTOCOL = '/peers/protos/'

‎src/peer-store/persistent/index.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const PeerStore = require('..')
1313
const {
1414
NAMESPACE_ADDRESS,
1515
NAMESPACE_COMMON,
16+
NAMESPACE_KEYS,
1617
NAMESPACE_PROTOCOL
1718
} = require('./consts')
1819

@@ -56,10 +57,11 @@ class PersistentPeerStore extends PeerStore {
5657
// Handlers for dirty peers
5758
this.on('change:protocols', this._addDirtyPeer)
5859
this.on('change:multiaddrs', this._addDirtyPeer)
60+
this.on('change:pubkey', this._addDirtyPeer)
5961

6062
// Load data
6163
for await (const entry of this._datastore.query({ prefix: NAMESPACE_COMMON })) {
62-
this._processDatastoreEntry(entry)
64+
await this._processDatastoreEntry(entry)
6365
}
6466

6567
log('PeerStore started')
@@ -110,11 +112,14 @@ class PersistentPeerStore extends PeerStore {
110112
const batch = this._datastore.batch()
111113
for (const peerIdStr of commitPeers) {
112114
// PeerId (replace by keyBook)
113-
const peerId = this.peerIds.get(peerIdStr)
115+
const peerId = this.keyBook.data.get(peerIdStr) || PeerId.createFromB58String(peerIdStr)
114116

115117
// Address Book
116118
this._batchAddressBook(peerId, batch)
117119

120+
// Key Book
121+
this._batchKeyBook(peerId, batch)
122+
118123
// Proto Book
119124
this._batchProtoBook(peerId, batch)
120125
}
@@ -154,6 +159,31 @@ class PersistentPeerStore extends PeerStore {
154159
}
155160
}
156161

162+
/**
163+
* Add Key book data of the peer to the batch.
164+
* @private
165+
* @param {PeerId} peerId
166+
* @param {Object} batch
167+
*/
168+
_batchKeyBook (peerId, batch) {
169+
const b32key = peerId.toString()
170+
const key = new Key(`${NAMESPACE_KEYS}${b32key}`)
171+
172+
try {
173+
// Deleted from the book
174+
if (!peerId.pubKey) {
175+
batch.delete(key)
176+
return
177+
}
178+
179+
const encodedData = peerId.marshalPubKey()
180+
181+
batch.put(key, encodedData)
182+
} catch (err) {
183+
log.error(err)
184+
}
185+
}
186+
157187
/**
158188
* Add proto book data of the peer to the batch.
159189
* @private
@@ -187,8 +217,9 @@ class PersistentPeerStore extends PeerStore {
187217
* @param {Object} params
188218
* @param {Key} params.key datastore key
189219
* @param {Buffer} params.value datastore value stored
220+
* @return {Promise<void>}
190221
*/
191-
_processDatastoreEntry ({ key, value }) {
222+
async _processDatastoreEntry ({ key, value }) {
192223
try {
193224
const keyParts = key.toString().split('/')
194225
const peerId = PeerId.createFromCID(keyParts[3])
@@ -205,6 +236,14 @@ class PersistentPeerStore extends PeerStore {
205236
})),
206237
{ emit: false })
207238
break
239+
case 'keys':
240+
decoded = await PeerId.createFromPubKey(value)
241+
242+
this.keyBook._setData(
243+
decoded,
244+
decoded,
245+
{ emit: false })
246+
break
208247
case 'protos':
209248
decoded = Protocols.decode(value)
210249

‎test/peer-store/key-book.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-bytes'))
7+
const { expect } = chai
8+
const sinon = require('sinon')
9+
10+
const PeerId = require('peer-id')
11+
const PeerStore = require('../../src/peer-store')
12+
13+
const peerUtils = require('../utils/creators/peer')
14+
const {
15+
codes: { ERR_INVALID_PARAMETERS }
16+
} = require('../../src/errors')
17+
18+
describe('keyBook', () => {
19+
let peerId, peerStore, kb
20+
21+
beforeEach(async () => {
22+
[peerId] = await peerUtils.createPeerId()
23+
peerStore = new PeerStore()
24+
kb = peerStore.keyBook
25+
})
26+
27+
it('throwns invalid parameters error if invalid PeerId is provided', () => {
28+
try {
29+
kb.set('invalid peerId')
30+
} catch (err) {
31+
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
32+
return
33+
}
34+
throw new Error('invalid peerId should throw error')
35+
})
36+
37+
it('stores the peerId in the book and returns the public key', () => {
38+
// Set PeerId
39+
kb.set(peerId)
40+
41+
// Get public key
42+
const pubKey = kb.get(peerId)
43+
expect(peerId.pubKey.bytes).to.equalBytes(pubKey.bytes)
44+
})
45+
46+
it('should not store if already stored', () => {
47+
const spy = sinon.spy(kb, '_setData')
48+
49+
// Set PeerId
50+
kb.set(peerId)
51+
kb.set(peerId)
52+
53+
expect(spy).to.have.property('callCount', 1)
54+
})
55+
56+
it('stores if already stored but there was no public key stored', () => {
57+
const spy = sinon.spy(kb, '_setData')
58+
59+
// Set PeerId without public key
60+
const p = PeerId.createFromB58String(peerId.toB58String())
61+
kb.set(p)
62+
63+
// Set complete peerId
64+
kb.set(peerId)
65+
66+
expect(spy).to.have.property('callCount', 2)
67+
})
68+
})

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

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
66
const { expect } = chai
7+
const sinon = require('sinon')
78

89
const PeerStore = require('../../src/peer-store')
910
const multiaddr = require('multiaddr')
@@ -48,6 +49,27 @@ describe('peer-store', () => {
4849
const peer = peerStore.get(peerIds[0])
4950
expect(peer).to.not.exist()
5051
})
52+
53+
it('sets the peer to the KeyBook when added to the AddressBook', () => {
54+
const spyPeerStore = sinon.spy(peerStore.keyBook, 'set')
55+
56+
peerStore.addressBook.set(peerIds[0], [addr1, addr2])
57+
expect(spyPeerStore).to.have.property('callCount', 1)
58+
})
59+
60+
it('sets the peer to the KeyBook when added to the ProtoBook', () => {
61+
const spyPeerStore = sinon.spy(peerStore.keyBook, 'set')
62+
63+
peerStore.protoBook.set(peerIds[0], [proto1])
64+
expect(spyPeerStore).to.have.property('callCount', 1)
65+
})
66+
67+
it('does not re-set the to the KeyBook when directly added to it', () => {
68+
const spyPeerStore = sinon.spy(peerStore.keyBook, 'set')
69+
70+
peerStore.keyBook.set(peerIds[0])
71+
expect(spyPeerStore).to.have.property('callCount', 1)
72+
})
5173
})
5274

5375
describe('previously populated books', () => {
@@ -108,6 +130,8 @@ describe('peer-store', () => {
108130

109131
const peerMultiaddrs = peer.addresses.map((mi) => mi.multiaddr)
110132
expect(peerMultiaddrs).to.have.members([addr1, addr2])
133+
134+
expect(peer.id).to.exist()
111135
})
112136

113137
it('gets the stored information of a peer that is not present in all its books', () => {

‎test/peer-store/persisted-peer-store.spec.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ describe('Persisted PeerStore', () => {
6868
// AddressBook
6969
peerStore.addressBook.set(peer, multiaddrs)
7070

71-
expect(spyDirty).to.have.property('callCount', 1)
72-
expect(spyDs).to.have.property('callCount', 1)
71+
expect(spyDirty).to.have.property('callCount', 2) // Address + PeerId
72+
expect(spyDs).to.have.property('callCount', 2)
7373

7474
// ProtoBook
7575
peerStore.protoBook.set(peer, protocols)
7676

77-
expect(spyDirty).to.have.property('callCount', 2)
78-
expect(spyDs).to.have.property('callCount', 2)
77+
expect(spyDirty).to.have.property('callCount', 3) // Protocol
78+
expect(spyDs).to.have.property('callCount', 3)
7979

80-
// Should have two peer records stored in the datastore
80+
// Should have three peer records stored in the datastore
8181
const queryParams = {
8282
prefix: '/peers/'
8383
}
@@ -86,7 +86,7 @@ describe('Persisted PeerStore', () => {
8686
for await (const _ of datastore.query(queryParams)) { // eslint-disable-line
8787
count++
8888
}
89-
expect(count).to.equal(2)
89+
expect(count).to.equal(3)
9090

9191
// Validate data
9292
const storedPeer = peerStore.get(peer)
@@ -114,11 +114,11 @@ describe('Persisted PeerStore', () => {
114114
peerStore.protoBook.set(peers[0], protocols)
115115
peerStore.protoBook.set(peers[1], protocols)
116116

117-
expect(spyDs).to.have.property('callCount', 4)
117+
expect(spyDs).to.have.property('callCount', 6) // 2 AddressBook + 2 ProtoBook + 2 KeyBook
118118
expect(peerStore.peers.size).to.equal(2)
119119

120120
await peerStore.stop()
121-
peerStore.peerIds.clear()
121+
peerStore.keyBook.data.clear()
122122
peerStore.addressBook.data.clear()
123123
peerStore.protoBook.data.clear()
124124

@@ -127,8 +127,8 @@ describe('Persisted PeerStore', () => {
127127

128128
await peerStore.start()
129129

130-
expect(spy).to.have.property('callCount', 4) // 4 datastore entries
131-
expect(spyDs).to.have.property('callCount', 4) // 4 previous operations
130+
expect(spy).to.have.property('callCount', 6) // 6 datastore entries
131+
expect(spyDs).to.have.property('callCount', 6) // 6 previous operations
132132

133133
expect(peerStore.peers.size).to.equal(2)
134134
expect(peerStore.addressBook.data.size).to.equal(2)
@@ -149,15 +149,17 @@ describe('Persisted PeerStore', () => {
149149

150150
const spyDs = sinon.spy(datastore, 'batch')
151151
const spyAddressBook = sinon.spy(peerStore.addressBook, 'delete')
152+
const spyKeyBook = sinon.spy(peerStore.keyBook, 'delete')
152153
const spyProtoBook = sinon.spy(peerStore.protoBook, 'delete')
153154

154155
// Delete from PeerStore
155156
peerStore.delete(peer)
156157
await peerStore.stop()
157158

158159
expect(spyAddressBook).to.have.property('callCount', 1)
160+
expect(spyKeyBook).to.have.property('callCount', 1)
159161
expect(spyProtoBook).to.have.property('callCount', 1)
160-
expect(spyDs).to.have.property('callCount', 2)
162+
expect(spyDs).to.have.property('callCount', 3)
161163

162164
// Should have zero peer records stored in the datastore
163165
const queryParams = {
@@ -199,7 +201,7 @@ describe('Persisted PeerStore', () => {
199201
// Remove data from the same Peer
200202
peerStore.addressBook.delete(peers[0])
201203

202-
expect(spyDirty).to.have.property('callCount', 3)
204+
expect(spyDirty).to.have.property('callCount', 4) // 2 AddrBook ops, 1 ProtoBook op, 1 KeyBook op
203205
expect(peerStore._dirtyPeers.size).to.equal(1)
204206
expect(spyDs).to.have.property('callCount', 0)
205207

@@ -213,16 +215,15 @@ describe('Persisted PeerStore', () => {
213215
// Add data for second book
214216
peerStore.addressBook.set(peers[1], multiaddrs)
215217

216-
expect(spyDirty).to.have.property('callCount', 4)
218+
expect(spyDirty).to.have.property('callCount', 6)
217219
expect(spyDs).to.have.property('callCount', 1)
218-
expect(peerStore._dirtyPeers.size).to.equal(0) // Reset
219220

220221
// Should have two peer records stored in the datastore
221222
let count = 0
222223
for await (const _ of datastore.query(queryParams)) { // eslint-disable-line
223224
count++
224225
}
225-
expect(count).to.equal(2)
226+
expect(count).to.equal(4)
226227
expect(peerStore.peers.size).to.equal(2)
227228
})
228229

@@ -239,7 +240,7 @@ describe('Persisted PeerStore', () => {
239240
peerStore.protoBook.set(peer, protocols)
240241

241242
expect(spyDs).to.have.property('callCount', 0)
242-
expect(spyDirty).to.have.property('callCount', 1)
243+
expect(spyDirty).to.have.property('callCount', 2) // ProtoBook + KeyBook
243244
expect(peerStore._dirtyPeers.size).to.equal(1)
244245

245246
const queryParams = {
@@ -251,7 +252,7 @@ describe('Persisted PeerStore', () => {
251252

252253
await peerStore.stop()
253254

254-
expect(spyDirty).to.have.property('callCount', 1)
255+
expect(spyDirty).to.have.property('callCount', 2)
255256
expect(spyDs).to.have.property('callCount', 1)
256257
expect(peerStore._dirtyPeers.size).to.equal(0) // Reset
257258

@@ -260,7 +261,7 @@ describe('Persisted PeerStore', () => {
260261
for await (const _ of datastore.query(queryParams)) { // eslint-disable-line
261262
count++
262263
}
263-
expect(count).to.equal(1)
264+
expect(count).to.equal(2)
264265
expect(peerStore.peers.size).to.equal(1)
265266
})
266267
})
@@ -353,7 +354,7 @@ describe('libp2p.peerStore (Persisted)', () => {
353354

354355
await newNode.start()
355356

356-
expect(spy).to.have.property('callCount', 4) // 4 datastore entries
357+
expect(spy).to.have.property('callCount', 6) // 6 datastore entries
357358

358359
expect(newNode.peerStore.peers.size).to.equal(2)
359360

0 commit comments

Comments
 (0)
Please sign in to comment.