Skip to content

Commit 24a16df

Browse files
committed
chore: store self protocols in protobook
1 parent 1228d1e commit 24a16df

File tree

6 files changed

+279
-96
lines changed

6 files changed

+279
-96
lines changed

doc/API.md

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* [`peerStore.protoBook.add`](#peerstoreprotobookadd)
3838
* [`peerStore.protoBook.delete`](#peerstoreprotobookdelete)
3939
* [`peerStore.protoBook.get`](#peerstoreprotobookget)
40+
* [`peerStore.protoBook.remove`](#peerstoreprotobookremove)
4041
* [`peerStore.protoBook.set`](#peerstoreprotobookset)
4142
* [`peerStore.delete`](#peerstoredelete)
4243
* [`peerStore.get`](#peerstoreget)
@@ -841,32 +842,6 @@ Consider using `addressBook.add()` if you're not sure this is what you want to d
841842
peerStore.addressBook.add(peerId, multiaddr)
842843
```
843844

844-
### peerStore.protoBook.add
845-
846-
Add known `protocols` of a given peer.
847-
848-
`peerStore.protoBook.add(peerId, protocols)`
849-
850-
#### Parameters
851-
852-
| Name | Type | Description |
853-
|------|------|-------------|
854-
| peerId | [`PeerId`][peer-id] | peerId to set |
855-
| protocols | `Array<string>` | protocols to add |
856-
857-
#### Returns
858-
859-
| Type | Description |
860-
|------|-------------|
861-
| `ProtoBook` | Returns the Proto Book component |
862-
863-
#### Example
864-
865-
```js
866-
peerStore.protoBook.add(peerId, protocols)
867-
```
868-
869-
870845
### peerStore.keyBook.delete
871846

872847
Delete the provided peer from the book.
@@ -1089,6 +1064,31 @@ Set known metadata of a given `peerId`.
10891064
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
10901065
```
10911066

1067+
### peerStore.protoBook.add
1068+
1069+
Add known `protocols` of a given peer.
1070+
1071+
`peerStore.protoBook.add(peerId, protocols)`
1072+
1073+
#### Parameters
1074+
1075+
| Name | Type | Description |
1076+
|------|------|-------------|
1077+
| peerId | [`PeerId`][peer-id] | peerId to set |
1078+
| protocols | `Array<string>` | protocols to add |
1079+
1080+
#### Returns
1081+
1082+
| Type | Description |
1083+
|------|-------------|
1084+
| `ProtoBook` | Returns the Proto Book component |
1085+
1086+
#### Example
1087+
1088+
```js
1089+
peerStore.protoBook.add(peerId, protocols)
1090+
```
1091+
10921092
### peerStore.protoBook.delete
10931093

10941094
Delete the provided peer from the book.
@@ -1145,6 +1145,31 @@ peerStore.protoBook.get(peerId)
11451145
// [ '/proto/1.0.0', '/proto/1.1.0' ]
11461146
```
11471147

1148+
### peerStore.protoBook.remove
1149+
1150+
Remove given `protocols` of a given peer.
1151+
1152+
`peerStore.protoBook.remove(peerId, protocols)`
1153+
1154+
#### Parameters
1155+
1156+
| Name | Type | Description |
1157+
|------|------|-------------|
1158+
| peerId | [`PeerId`][peer-id] | peerId to set |
1159+
| protocols | `Array<string>` | protocols to remove |
1160+
1161+
#### Returns
1162+
1163+
| Type | Description |
1164+
|------|-------------|
1165+
| `ProtoBook` | Returns the Proto Book component |
1166+
1167+
#### Example
1168+
1169+
```js
1170+
peerStore.protoBook.remove(peerId, protocols)
1171+
```
1172+
11481173
### peerStore.protoBook.set
11491174

11501175
Set known `protocols` of a given peer.

src/identify/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ class IdentifyService {
5151
* @class
5252
* @param {object} options
5353
* @param {Libp2p} options.libp2p
54-
* @param {Map<string, handler>} options.protocols - A reference to the protocols we support
5554
*/
56-
constructor ({ libp2p, protocols }) {
55+
constructor ({ libp2p }) {
5756
/**
5857
* @property {PeerStore}
5958
*/
@@ -74,10 +73,9 @@ class IdentifyService {
7473
*/
7574
this._libp2p = libp2p
7675

77-
this._protocols = protocols
78-
7976
this.handleMessage = this.handleMessage.bind(this)
8077

78+
// When a new connection happens, trigger identify
8179
this.connectionManager.on('peer:connect', (connection) => {
8280
const peerId = connection.remotePeer
8381

@@ -90,6 +88,13 @@ class IdentifyService {
9088
this.pushToPeerStore()
9189
}
9290
})
91+
92+
// When self protocols change, trigger identify-push
93+
this.peerStore.on('change:protocols', ({ peerId }) => {
94+
if (peerId.toString() === this.peerId.toString()) {
95+
this.pushToPeerStore()
96+
}
97+
})
9398
}
9499

95100
/**
@@ -101,7 +106,7 @@ class IdentifyService {
101106
async push (connections) {
102107
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
103108
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
104-
const protocols = Array.from(this._protocols.keys())
109+
const protocols = this.peerStore.protoBook.get(this.peerId) || []
105110

106111
const pushes = connections.map(async connection => {
107112
try {
@@ -132,6 +137,11 @@ class IdentifyService {
132137
* @returns {void}
133138
*/
134139
pushToPeerStore () {
140+
// Do not try to push if libp2p node is not running
141+
if (!this._libp2p.isStarted()) {
142+
return
143+
}
144+
135145
const connections = []
136146
let connection
137147
for (const peer of this.peerStore.peers.values()) {
@@ -251,6 +261,7 @@ class IdentifyService {
251261
}
252262

253263
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
264+
const protocols = this.peerStore.protoBook.get(this.peerId) || []
254265

255266
const message = Message.encode({
256267
protocolVersion: PROTOCOL_VERSION,
@@ -259,7 +270,7 @@ class IdentifyService {
259270
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
260271
signedPeerRecord,
261272
observedAddr: connection.remoteAddr.bytes,
262-
protocols: Array.from(this._protocols.keys())
273+
protocols
263274
})
264275

265276
try {

src/index.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ class Libp2p extends EventEmitter {
155155
})
156156

157157
// Add the identify service since we can multiplex
158-
this.identifyService = new IdentifyService({
159-
libp2p: this,
160-
protocols: this.upgrader.protocols
161-
})
158+
this.identifyService = new IdentifyService({ libp2p: this })
162159
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
163160
}
164161

@@ -430,10 +427,8 @@ class Libp2p extends EventEmitter {
430427
this.upgrader.protocols.set(protocol, handler)
431428
})
432429

433-
// Only push if libp2p is running
434-
if (this.isStarted() && this.identifyService) {
435-
this.identifyService.pushToPeerStore()
436-
}
430+
// Add new protocols to self protocols in the Protobook
431+
this.peerStore.protoBook.add(this.peerId, protocols)
437432
}
438433

439434
/**
@@ -448,10 +443,8 @@ class Libp2p extends EventEmitter {
448443
this.upgrader.protocols.delete(protocol)
449444
})
450445

451-
// Only push if libp2p is running
452-
if (this.isStarted() && this.identifyService) {
453-
this.identifyService.pushToPeerStore()
454-
}
446+
// Remove protocols from self protocols in the Protobook
447+
this.peerStore.protoBook.remove(this.peerId, protocols)
455448
}
456449

457450
async _onStarting () {

src/peer-store/proto-book.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,50 @@ class ProtoBook extends Book {
112112
return this
113113
}
114114

115-
protocols = [...newSet]
116-
117115
this._setData(peerId, newSet)
118116
log(`added provided protocols for ${id}`)
119117

120118
return this
121119
}
120+
121+
/**
122+
* Removes known protocols of a provided peer.
123+
* If the protocols did not exist before, nothing will be done.
124+
*
125+
* @param {PeerId} peerId
126+
* @param {Array<string>} protocols
127+
* @returns {ProtoBook}
128+
*/
129+
remove (peerId, protocols) {
130+
if (!PeerId.isPeerId(peerId)) {
131+
log.error('peerId must be an instance of peer-id to store data')
132+
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
133+
}
134+
135+
if (!protocols) {
136+
log.error('protocols must be provided to store data')
137+
throw errcode(new Error('protocols must be provided'), ERR_INVALID_PARAMETERS)
138+
}
139+
140+
const id = peerId.toB58String()
141+
const recSet = this.data.get(id)
142+
143+
if (recSet) {
144+
const newSet = new Set([
145+
...recSet
146+
].filter((p) => !protocols.includes(p)))
147+
148+
// Any protocol removed?
149+
if (recSet.size === newSet.size) {
150+
return this
151+
}
152+
153+
this._setData(peerId, newSet)
154+
log(`removed provided protocols for ${id}`)
155+
}
156+
157+
return this
158+
}
122159
}
123160

124161
module.exports = ProtoBook

0 commit comments

Comments
 (0)