Skip to content

Commit d42da6c

Browse files
committed
chore: store self protocols in protobook
1 parent 152a048 commit d42da6c

File tree

6 files changed

+278
-96
lines changed

6 files changed

+278
-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
@@ -50,9 +50,8 @@ class IdentifyService {
5050
* @constructor
5151
* @param {object} options
5252
* @param {Libp2p} options.libp2p
53-
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
5453
*/
55-
constructor ({ libp2p, protocols }) {
54+
constructor ({ libp2p }) {
5655
/**
5756
* @property {PeerStore}
5857
*/
@@ -73,10 +72,9 @@ class IdentifyService {
7372
*/
7473
this._libp2p = libp2p
7574

76-
this._protocols = protocols
77-
7875
this.handleMessage = this.handleMessage.bind(this)
7976

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

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

9499
/**
@@ -99,7 +104,7 @@ class IdentifyService {
99104
async push (connections) {
100105
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
101106
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
102-
const protocols = Array.from(this._protocols.keys())
107+
const protocols = this.peerStore.protoBook.get(this.peerId) || []
103108

104109
const pushes = connections.map(async connection => {
105110
try {
@@ -129,6 +134,11 @@ class IdentifyService {
129134
* @returns {void}
130135
*/
131136
pushToPeerStore () {
137+
// Do not try to push if libp2p node is not running
138+
if (!this._libp2p.isStarted()) {
139+
return
140+
}
141+
132142
const connections = []
133143
let connection
134144
for (const peer of this.peerStore.peers.values()) {
@@ -247,6 +257,7 @@ class IdentifyService {
247257
}
248258

249259
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
260+
const protocols = this.peerStore.protoBook.get(this.peerId) || []
250261

251262
const message = Message.encode({
252263
protocolVersion: PROTOCOL_VERSION,
@@ -255,7 +266,7 @@ class IdentifyService {
255266
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
256267
signedPeerRecord,
257268
observedAddr: connection.remoteAddr.bytes,
258-
protocols: Array.from(this._protocols.keys())
269+
protocols
259270
})
260271

261272
try {

src/index.js

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

159159
// Add the identify service since we can multiplex
160-
this.identifyService = new IdentifyService({
161-
libp2p: this,
162-
protocols: this.upgrader.protocols
163-
})
160+
this.identifyService = new IdentifyService({ libp2p: this })
164161
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
165162
}
166163

@@ -422,10 +419,8 @@ class Libp2p extends EventEmitter {
422419
this.upgrader.protocols.set(protocol, handler)
423420
})
424421

425-
// Only push if libp2p is running
426-
if (this.isStarted() && this.identifyService) {
427-
this.identifyService.pushToPeerStore()
428-
}
422+
// Add new protocols to self protocols in the Protobook
423+
this.peerStore.protoBook.add(this.peerId, protocols)
429424
}
430425

431426
/**
@@ -439,10 +434,8 @@ class Libp2p extends EventEmitter {
439434
this.upgrader.protocols.delete(protocol)
440435
})
441436

442-
// Only push if libp2p is running
443-
if (this.isStarted() && this.identifyService) {
444-
this.identifyService.pushToPeerStore()
445-
}
437+
// Remove protocols from self protocols in the Protobook
438+
this.peerStore.protoBook.remove(this.peerId, protocols)
446439
}
447440

448441
async _onStarting () {

src/peer-store/proto-book.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,49 @@ class ProtoBook extends Book {
108108
return this
109109
}
110110

111-
protocols = [...newSet]
112-
113111
this._setData(peerId, newSet)
114112
log(`added provided protocols for ${id}`)
115113

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

120156
module.exports = ProtoBook

0 commit comments

Comments
 (0)