Skip to content

Commit 54212cb

Browse files
chore: deprecate old peer store api (#598)
* chore: deprecate old peer-store api BREAKING CHANGE: the peer-store api changed. Check the API docs for the new specification. * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com>
1 parent be45fc4 commit 54212cb

File tree

10 files changed

+106
-154
lines changed

10 files changed

+106
-154
lines changed

doc/API.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
* [`metrics.forPeer`](#metricsforpeer)
4242
* [`metrics.forProtocol`](#metricsforprotocol)
4343
* [Events](#events)
44+
* [`libp2p`](#libp2p)
45+
* [`libp2p.peerStore`](#libp2ppeerStore)
4446
* [Types](#types)
4547
* [`Stats`](#stats)
4648

@@ -1099,7 +1101,9 @@ console.log(peerStats.toJSON())
10991101

11001102
## Events
11011103

1102-
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
1104+
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
1105+
1106+
### libp2p
11031107

11041108
#### An error has occurred
11051109

@@ -1132,6 +1136,28 @@ This event will be triggered anytime we are disconnected from another peer, rega
11321136

11331137
- `peer`: instance of [`PeerInfo`][peer-info]
11341138

1139+
### libp2p.peerStore
1140+
1141+
#### A new peer is added to the peerStore
1142+
1143+
`libp2p.peerStore.on('peer', (peerId) => {})`
1144+
1145+
- `peerId`: instance of [`PeerId`][peer-id]
1146+
1147+
#### Known multiaddrs for a peer change
1148+
1149+
`libp2p.peerStore.on('change:multiaddrs', ({ peerId, multiaddrs}) => {})`
1150+
1151+
- `peerId`: instance of [`PeerId`][peer-id]
1152+
- `multiaddrs`: array of known [`multiaddr`][multiaddr] for the peer
1153+
1154+
#### Known protocols for a peer change
1155+
1156+
`libp2p.peerStore.on('change:protocols', ({ peerId, protocols}) => {})`
1157+
1158+
- `peerId`: instance of [`PeerId`][peer-id]
1159+
- `protocols`: array of known, supported protocols for the peer (string identifiers)
1160+
11351161
## Types
11361162

11371163
### Stats

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"cids": "^0.8.0",
8585
"delay": "^4.3.0",
8686
"dirty-chai": "^2.0.1",
87-
"interop-libp2p": "~0.0.1",
87+
"interop-libp2p": "libp2p/interop#chore/update-libp2p-daemon-with-peerstore",
8888
"it-concat": "^1.0.0",
8989
"it-pair": "^1.0.0",
9090
"it-pushable": "^1.4.0",
@@ -93,7 +93,7 @@
9393
"libp2p-delegated-peer-routing": "^0.4.0",
9494
"libp2p-floodsub": "^0.20.0",
9595
"libp2p-gossipsub": "^0.2.0",
96-
"libp2p-kad-dht": "^0.18.2",
96+
"libp2p-kad-dht": "^0.19.0-pre.0",
9797
"libp2p-mdns": "^0.13.0",
9898
"libp2p-mplex": "^0.9.1",
9999
"libp2p-secio": "^0.12.1",

src/peer-store/address-book.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AddressBook extends Book {
2828

2929
/**
3030
* @constructor
31-
* @param {EventEmitter} peerStore
31+
* @param {PeerStore} peerStore
3232
*/
3333
constructor (peerStore) {
3434
/**
@@ -80,6 +80,7 @@ class AddressBook extends Book {
8080
}
8181

8282
this.data.set(id, multiaddrInfos)
83+
this._setPeerId(peerId)
8384
log(`stored provided multiaddrs for ${id}`)
8485

8586
// TODO: Remove peerInfo and its usage on peer-info deprecate
@@ -133,6 +134,7 @@ class AddressBook extends Book {
133134
return this
134135
}
135136

137+
this._setPeerId(peerId)
136138
this.data.set(id, multiaddrInfos)
137139

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

src/peer-store/book.js

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class Book {
8282

8383
return true
8484
}
85+
86+
_setPeerId (peerId) {
87+
if (!this._ps.peerIds.get(peerId)) {
88+
this._ps.peerIds.set(peerId.toB58String(), peerId)
89+
}
90+
}
8591
}
8692

8793
module.exports = Book

src/peer-store/index.js

+10-92
Original file line numberDiff line numberDiff line change
@@ -43,97 +43,13 @@ class PeerStore extends EventEmitter {
4343
* ProtoBook containing a map of peerIdStr to supported protocols.
4444
*/
4545
this.protoBook = new ProtoBook(this)
46-
}
47-
48-
// TODO: Temporary adapter for modules using PeerStore
49-
// This should be removed under a breaking change
50-
/**
51-
* Stores the peerInfo of a new peer on each book.
52-
* @param {PeerInfo} peerInfo
53-
* @param {object} [options]
54-
* @param {boolean} [options.replace = true]
55-
* @return {PeerInfo}
56-
*/
57-
put (peerInfo, options) {
58-
const multiaddrs = peerInfo.multiaddrs.toArray()
59-
const protocols = Array.from(peerInfo.protocols || new Set())
60-
61-
this.addressBook.set(peerInfo.id, multiaddrs, options)
62-
this.protoBook.set(peerInfo.id, protocols, options)
63-
64-
const peer = this.find(peerInfo.id)
65-
const pInfo = new PeerInfo(peerInfo.id)
66-
67-
if (!peer) {
68-
return pInfo
69-
}
70-
71-
peer.protocols.forEach((p) => pInfo.protocols.add(p))
72-
peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr))
73-
74-
return pInfo
75-
}
76-
77-
// TODO: Temporary adapter for modules using PeerStore
78-
// This should be removed under a breaking change
79-
/**
80-
* Get the info of the given id.
81-
* @param {peerId} peerId
82-
* @returns {PeerInfo}
83-
*/
84-
get (peerId) {
85-
const peer = this.find(peerId)
86-
87-
const pInfo = new PeerInfo(peerId)
88-
peer.protocols.forEach((p) => pInfo.protocols.add(p))
89-
peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr))
90-
91-
return pInfo
92-
}
93-
94-
// TODO: Temporary adapter for modules using PeerStore
95-
// This should be removed under a breaking change
96-
/**
97-
* Has the info to the given id.
98-
* @param {PeerId} peerId
99-
* @returns {boolean}
100-
*/
101-
has (peerId) {
102-
return Boolean(this.find(peerId))
103-
}
104-
105-
// TODO: Temporary adapter for modules using PeerStore
106-
// This should be removed under a breaking change
107-
/**
108-
* Removes the peer provided.
109-
* @param {PeerId} peerId
110-
* @returns {boolean} true if found and removed
111-
*/
112-
remove (peerId) {
113-
return this.delete(peerId)
114-
}
11546

116-
// TODO: Temporary adapter for modules using PeerStore
117-
// This should be removed under a breaking change
118-
/**
119-
* Completely replaces the existing peers metadata with the given `peerInfo`
120-
* @param {PeerInfo} peerInfo
121-
* @returns {void}
122-
*/
123-
replace (peerInfo) {
124-
this.put(peerInfo)
125-
}
126-
127-
// TODO: Temporary adapter for modules using PeerStore
128-
// This should be removed under a breaking change
129-
/**
130-
* Returns the known multiaddrs for a given `PeerInfo`. All returned multiaddrs
131-
* will include the encapsulated `PeerId` of the peer.
132-
* @param {PeerInfo} peerInfo
133-
* @returns {Array<Multiaddr>}
134-
*/
135-
multiaddrsForPeer (peerInfo) {
136-
return this.addressBook.getMultiaddrsForPeer(peerInfo.id)
47+
/**
48+
* TODO: this should only exist until we have the key-book
49+
* Map known peers to their peer-id.
50+
* @type {Map<string, Array<PeerId>}
51+
*/
52+
this.peerIds = new Map()
13753
}
13854

13955
/**
@@ -195,15 +111,16 @@ class PeerStore extends EventEmitter {
195111
}
196112

197113
/**
198-
* Find the stored information of a given peer.
114+
* Get the stored information of a given peer.
199115
* @param {PeerId} peerId
200116
* @returns {peerInfo}
201117
*/
202-
find (peerId) {
118+
get (peerId) {
203119
if (!PeerId.isPeerId(peerId)) {
204120
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
205121
}
206122

123+
const id = this.peerIds.get(peerId.toB58String())
207124
const multiaddrInfos = this.addressBook.get(peerId)
208125
const protocols = this.protoBook.get(peerId)
209126

@@ -212,6 +129,7 @@ class PeerStore extends EventEmitter {
212129
}
213130

214131
return {
132+
id: id || peerId,
215133
multiaddrInfos: multiaddrInfos || [],
216134
protocols: protocols || []
217135
}

src/peer-store/proto-book.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const {
2222
class ProtoBook extends Book {
2323
/**
2424
* @constructor
25-
* @param {EventEmitter} peerStore
25+
* @param {PeerStore} peerStore
2626
*/
2727
constructor (peerStore) {
2828
/**
@@ -71,6 +71,7 @@ class ProtoBook extends Book {
7171
}
7272

7373
this.data.set(id, newSet)
74+
this._setPeerId(peerId)
7475
log(`stored provided protocols for ${id}`)
7576

7677
// TODO: Remove peerInfo and its usage on peer-info deprecate
@@ -118,6 +119,7 @@ class ProtoBook extends Book {
118119
protocols = [...newSet]
119120

120121
this.data.set(id, newSet)
122+
this._setPeerId(peerId)
121123
log(`added provided protocols for ${id}`)
122124

123125
// TODO: Remove peerInfo and its usage on peer-info deprecate

test/content-routing/dht/operation.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('DHT subsystem operates correctly', () => {
7474
])
7575

7676
await libp2p.contentRouting.put(key, value)
77-
7877
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
78+
7979
expect(fetchedValue).to.eql(value)
8080
})
8181
})

0 commit comments

Comments
 (0)