@libp2p/peer-store 11.2.2 → 11.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +36 -68
- package/dist/src/index.js.map +1 -1
- package/dist/src/store.d.ts +18 -9
- package/dist/src/store.d.ts.map +1 -1
- package/dist/src/store.js +80 -27
- package/dist/src/store.js.map +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts.map +1 -1
- package/dist/src/utils/bytes-to-peer.js +1 -4
- package/dist/src/utils/bytes-to-peer.js.map +1 -1
- package/dist/src/utils/dedupe-addresses.d.ts +2 -1
- package/dist/src/utils/dedupe-addresses.d.ts.map +1 -1
- package/dist/src/utils/dedupe-addresses.js +2 -2
- package/dist/src/utils/dedupe-addresses.js.map +1 -1
- package/dist/src/utils/to-peer-pb.d.ts +2 -1
- package/dist/src/utils/to-peer-pb.d.ts.map +1 -1
- package/dist/src/utils/to-peer-pb.js +7 -4
- package/dist/src/utils/to-peer-pb.js.map +1 -1
- package/package.json +12 -10
- package/src/index.ts +43 -70
- package/src/store.ts +100 -28
- package/src/utils/bytes-to-peer.ts +3 -5
- package/src/utils/dedupe-addresses.ts +3 -2
- package/src/utils/to-peer-pb.ts +12 -5
|
@@ -2,7 +2,8 @@ import type { AddressFilter } from '../index.js';
|
|
|
2
2
|
import type { Peer as PeerPB } from '../pb/peer.js';
|
|
3
3
|
import type { ExistingPeer } from '../store.js';
|
|
4
4
|
import type { PeerId, PeerData } from '@libp2p/interface';
|
|
5
|
-
|
|
5
|
+
import type { AbortOptions } from '@multiformats/multiaddr';
|
|
6
|
+
export interface ToPBPeerOptions extends AbortOptions {
|
|
6
7
|
addressFilter?: AddressFilter;
|
|
7
8
|
existingPeer?: ExistingPeer;
|
|
8
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-peer-pb.d.ts","sourceRoot":"","sources":["../../../src/utils/to-peer-pb.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EAAO,IAAI,IAAI,MAAM,EAAE,MAAM,eAAe,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAW,QAAQ,EAAc,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"to-peer-pb.d.ts","sourceRoot":"","sources":["../../../src/utils/to-peer-pb.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EAAO,IAAI,IAAI,MAAM,EAAE,MAAM,eAAe,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAW,QAAQ,EAAc,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAE3D,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACnD,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAA;CAC5B;AAED,wBAAsB,QAAQ,CAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,OAAO,GAAG,OAAO,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CA8J/I"}
|
|
@@ -112,7 +112,7 @@ export async function toPeerPB(peerId, data, strategy, options) {
|
|
|
112
112
|
publicKey = publicKeyToProtobuf(peerId.publicKey);
|
|
113
113
|
}
|
|
114
114
|
const output = {
|
|
115
|
-
addresses: await dedupeFilterAndSortAddresses(peerId, options.addressFilter ?? (async () => true), addresses, options.existingPeer?.peerPB.addresses),
|
|
115
|
+
addresses: await dedupeFilterAndSortAddresses(peerId, options.addressFilter ?? (async () => true), addresses, options.existingPeer?.peerPB.addresses, options),
|
|
116
116
|
protocols: [...protocols.values()].sort((a, b) => {
|
|
117
117
|
return a.localeCompare(b);
|
|
118
118
|
}),
|
|
@@ -189,9 +189,12 @@ function mapTag(key, tag) {
|
|
|
189
189
|
if (tag.ttl != null) {
|
|
190
190
|
expiry = BigInt(Date.now() + Number(tag.ttl));
|
|
191
191
|
}
|
|
192
|
-
|
|
193
|
-
value: tag.value ?? 0
|
|
194
|
-
expiry
|
|
192
|
+
const output = {
|
|
193
|
+
value: tag.value ?? 0
|
|
195
194
|
};
|
|
195
|
+
if (expiry != null) {
|
|
196
|
+
output.expiry = expiry;
|
|
197
|
+
}
|
|
198
|
+
return output;
|
|
196
199
|
}
|
|
197
200
|
//# sourceMappingURL=to-peer-pb.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-peer-pb.js","sourceRoot":"","sources":["../../../src/utils/to-peer-pb.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,4BAA4B,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"to-peer-pb.js","sourceRoot":"","sources":["../../../src/utils/to-peer-pb.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,4BAA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ACxB,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/peer-store",
|
|
3
|
-
"version": "11.2.
|
|
3
|
+
"version": "11.2.3",
|
|
4
4
|
"description": "Stores information about peers libp2p knows on the network",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/peer-store#readme",
|
|
@@ -48,29 +48,31 @@
|
|
|
48
48
|
"test:electron-main": "aegir test -t electron-main"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@libp2p/crypto": "^5.1.
|
|
52
|
-
"@libp2p/interface": "^2.10.
|
|
53
|
-
"@libp2p/peer-
|
|
54
|
-
"@libp2p/peer-
|
|
51
|
+
"@libp2p/crypto": "^5.1.5",
|
|
52
|
+
"@libp2p/interface": "^2.10.3",
|
|
53
|
+
"@libp2p/peer-collections": "^6.0.31",
|
|
54
|
+
"@libp2p/peer-id": "^5.1.6",
|
|
55
|
+
"@libp2p/peer-record": "^8.0.31",
|
|
55
56
|
"@multiformats/multiaddr": "^12.4.0",
|
|
56
57
|
"interface-datastore": "^8.3.1",
|
|
57
58
|
"it-all": "^3.0.8",
|
|
58
|
-
"
|
|
59
|
-
"
|
|
59
|
+
"main-event": "^1.0.1",
|
|
60
|
+
"mortice": "^3.2.1",
|
|
61
|
+
"multiformats": "^13.3.6",
|
|
60
62
|
"protons-runtime": "^5.5.0",
|
|
61
63
|
"uint8arraylist": "^2.4.8",
|
|
62
64
|
"uint8arrays": "^5.1.0"
|
|
63
65
|
},
|
|
64
66
|
"devDependencies": {
|
|
65
|
-
"@libp2p/logger": "^5.1.
|
|
67
|
+
"@libp2p/logger": "^5.1.19",
|
|
66
68
|
"@types/sinon": "^17.0.4",
|
|
67
|
-
"aegir": "^47.0.
|
|
69
|
+
"aegir": "^47.0.14",
|
|
68
70
|
"benchmark": "^2.1.4",
|
|
69
71
|
"datastore-core": "^10.0.2",
|
|
70
72
|
"delay": "^6.0.0",
|
|
71
73
|
"p-defer": "^4.0.1",
|
|
72
74
|
"p-event": "^6.0.1",
|
|
73
|
-
"protons": "^7.6.
|
|
75
|
+
"protons": "^7.6.1",
|
|
74
76
|
"sinon": "^20.0.0"
|
|
75
77
|
},
|
|
76
78
|
"sideEffects": false
|
package/src/index.ts
CHANGED
|
@@ -4,27 +4,30 @@
|
|
|
4
4
|
* The peer store is where libp2p stores data about the peers it has encountered on the network.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { isPeerId } from '@libp2p/interface'
|
|
7
8
|
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
8
9
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
9
10
|
import all from 'it-all'
|
|
10
11
|
import { PersistentStore } from './store.js'
|
|
11
12
|
import type { PeerUpdate } from './store.js'
|
|
12
|
-
import type { ComponentLogger, Libp2pEvents, Logger,
|
|
13
|
+
import type { ComponentLogger, Libp2pEvents, Logger, PeerId, PeerStore, Peer, PeerData, PeerQuery, PeerInfo, AbortOptions, ConsumePeerRecordOptions, Metrics } from '@libp2p/interface'
|
|
13
14
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
14
15
|
import type { Datastore } from 'interface-datastore'
|
|
16
|
+
import type { TypedEventTarget } from 'main-event'
|
|
15
17
|
|
|
16
18
|
export interface PersistentPeerStoreComponents {
|
|
17
19
|
peerId: PeerId
|
|
18
20
|
datastore: Datastore
|
|
19
21
|
events: TypedEventTarget<Libp2pEvents>
|
|
20
22
|
logger: ComponentLogger
|
|
23
|
+
metrics?: Metrics
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* Return true to allow storing the passed multiaddr for the passed peer
|
|
25
28
|
*/
|
|
26
29
|
export interface AddressFilter {
|
|
27
|
-
(peerId: PeerId, multiaddr: Multiaddr): Promise<boolean> | boolean
|
|
30
|
+
(peerId: PeerId, multiaddr: Multiaddr, options?: AbortOptions): Promise<boolean> | boolean
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
export interface PersistentPeerStoreInit {
|
|
@@ -74,74 +77,48 @@ class PersistentPeerStore implements PeerStore {
|
|
|
74
77
|
readonly [Symbol.toStringTag] = '@libp2p/peer-store'
|
|
75
78
|
|
|
76
79
|
async forEach (fn: (peer: Peer,) => void, query?: PeerQuery): Promise<void> {
|
|
77
|
-
this.
|
|
78
|
-
|
|
79
|
-
this.log.trace('forEach got read lock')
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
for await (const peer of this.store.all(query)) {
|
|
83
|
-
fn(peer)
|
|
84
|
-
}
|
|
85
|
-
} finally {
|
|
86
|
-
this.log.trace('forEach release read lock')
|
|
87
|
-
release()
|
|
80
|
+
for await (const peer of this.store.all(query)) {
|
|
81
|
+
fn(peer)
|
|
88
82
|
}
|
|
89
83
|
}
|
|
90
84
|
|
|
91
85
|
async all (query?: PeerQuery): Promise<Peer[]> {
|
|
92
|
-
this.
|
|
93
|
-
const release = await this.store.lock.readLock()
|
|
94
|
-
this.log.trace('all got read lock')
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
return await all(this.store.all(query))
|
|
98
|
-
} finally {
|
|
99
|
-
this.log.trace('all release read lock')
|
|
100
|
-
release()
|
|
101
|
-
}
|
|
86
|
+
return all(this.store.all(query))
|
|
102
87
|
}
|
|
103
88
|
|
|
104
|
-
async delete (peerId: PeerId): Promise<void> {
|
|
105
|
-
this.
|
|
106
|
-
const release = await this.store.lock.writeLock()
|
|
107
|
-
this.log.trace('delete got write lock')
|
|
89
|
+
async delete (peerId: PeerId, options?: AbortOptions): Promise<void> {
|
|
90
|
+
const release = await this.store.getReadLock(peerId, options)
|
|
108
91
|
|
|
109
92
|
try {
|
|
110
|
-
await this.store.delete(peerId)
|
|
93
|
+
await this.store.delete(peerId, options)
|
|
111
94
|
} finally {
|
|
112
|
-
this.log.trace('delete release write lock')
|
|
113
95
|
release()
|
|
114
96
|
}
|
|
115
97
|
}
|
|
116
98
|
|
|
117
|
-
async has (peerId: PeerId): Promise<boolean> {
|
|
118
|
-
this.
|
|
119
|
-
const release = await this.store.lock.readLock()
|
|
120
|
-
this.log.trace('has got read lock')
|
|
99
|
+
async has (peerId: PeerId, options?: AbortOptions): Promise<boolean> {
|
|
100
|
+
const release = await this.store.getReadLock(peerId, options)
|
|
121
101
|
|
|
122
102
|
try {
|
|
123
|
-
return await this.store.has(peerId)
|
|
103
|
+
return await this.store.has(peerId, options)
|
|
124
104
|
} finally {
|
|
125
105
|
this.log.trace('has release read lock')
|
|
126
|
-
release()
|
|
106
|
+
release?.()
|
|
127
107
|
}
|
|
128
108
|
}
|
|
129
109
|
|
|
130
|
-
async get (peerId: PeerId): Promise<Peer> {
|
|
131
|
-
this.
|
|
132
|
-
const release = await this.store.lock.readLock()
|
|
133
|
-
this.log.trace('get got read lock')
|
|
110
|
+
async get (peerId: PeerId, options?: AbortOptions): Promise<Peer> {
|
|
111
|
+
const release = await this.store.getReadLock(peerId, options)
|
|
134
112
|
|
|
135
113
|
try {
|
|
136
|
-
return await this.store.load(peerId)
|
|
114
|
+
return await this.store.load(peerId, options)
|
|
137
115
|
} finally {
|
|
138
|
-
|
|
139
|
-
release()
|
|
116
|
+
release?.()
|
|
140
117
|
}
|
|
141
118
|
}
|
|
142
119
|
|
|
143
|
-
async getInfo (peerId: PeerId): Promise<PeerInfo> {
|
|
144
|
-
const peer = await this.get(peerId)
|
|
120
|
+
async getInfo (peerId: PeerId, options?: AbortOptions): Promise<PeerInfo> {
|
|
121
|
+
const peer = await this.get(peerId, options)
|
|
145
122
|
|
|
146
123
|
return {
|
|
147
124
|
id: peer.id,
|
|
@@ -149,59 +126,55 @@ class PersistentPeerStore implements PeerStore {
|
|
|
149
126
|
}
|
|
150
127
|
}
|
|
151
128
|
|
|
152
|
-
async save (id: PeerId, data: PeerData): Promise<Peer> {
|
|
153
|
-
this.
|
|
154
|
-
const release = await this.store.lock.writeLock()
|
|
155
|
-
this.log.trace('save got write lock')
|
|
129
|
+
async save (id: PeerId, data: PeerData, options?: AbortOptions): Promise<Peer> {
|
|
130
|
+
const release = await this.store.getWriteLock(id, options)
|
|
156
131
|
|
|
157
132
|
try {
|
|
158
|
-
const result = await this.store.save(id, data)
|
|
133
|
+
const result = await this.store.save(id, data, options)
|
|
159
134
|
|
|
160
135
|
this.#emitIfUpdated(id, result)
|
|
161
136
|
|
|
162
137
|
return result.peer
|
|
163
138
|
} finally {
|
|
164
|
-
|
|
165
|
-
release()
|
|
139
|
+
release?.()
|
|
166
140
|
}
|
|
167
141
|
}
|
|
168
142
|
|
|
169
|
-
async patch (id: PeerId, data: PeerData): Promise<Peer> {
|
|
170
|
-
this.
|
|
171
|
-
const release = await this.store.lock.writeLock()
|
|
172
|
-
this.log.trace('patch got write lock')
|
|
143
|
+
async patch (id: PeerId, data: PeerData, options?: AbortOptions): Promise<Peer> {
|
|
144
|
+
const release = await this.store.getWriteLock(id, options)
|
|
173
145
|
|
|
174
146
|
try {
|
|
175
|
-
const result = await this.store.patch(id, data)
|
|
147
|
+
const result = await this.store.patch(id, data, options)
|
|
176
148
|
|
|
177
149
|
this.#emitIfUpdated(id, result)
|
|
178
150
|
|
|
179
151
|
return result.peer
|
|
180
152
|
} finally {
|
|
181
|
-
|
|
182
|
-
release()
|
|
153
|
+
release?.()
|
|
183
154
|
}
|
|
184
155
|
}
|
|
185
156
|
|
|
186
|
-
async merge (id: PeerId, data: PeerData): Promise<Peer> {
|
|
187
|
-
this.
|
|
188
|
-
const release = await this.store.lock.writeLock()
|
|
189
|
-
this.log.trace('merge got write lock')
|
|
157
|
+
async merge (id: PeerId, data: PeerData, options?: AbortOptions): Promise<Peer> {
|
|
158
|
+
const release = await this.store.getWriteLock(id, options)
|
|
190
159
|
|
|
191
160
|
try {
|
|
192
|
-
const result = await this.store.merge(id, data)
|
|
161
|
+
const result = await this.store.merge(id, data, options)
|
|
193
162
|
|
|
194
163
|
this.#emitIfUpdated(id, result)
|
|
195
164
|
|
|
196
165
|
return result.peer
|
|
197
166
|
} finally {
|
|
198
|
-
|
|
199
|
-
release()
|
|
167
|
+
release?.()
|
|
200
168
|
}
|
|
201
169
|
}
|
|
202
170
|
|
|
203
|
-
async consumePeerRecord (buf: Uint8Array,
|
|
204
|
-
|
|
171
|
+
async consumePeerRecord (buf: Uint8Array, options?: ConsumePeerRecordOptions): Promise<boolean>
|
|
172
|
+
async consumePeerRecord (buf: Uint8Array, expectedPeer?: PeerId, options?: AbortOptions): Promise<boolean>
|
|
173
|
+
async consumePeerRecord (buf: Uint8Array, arg1?: any, arg2?: any): Promise<boolean> {
|
|
174
|
+
const expectedPeer: PeerId | undefined = isPeerId(arg1) ? arg1 : isPeerId(arg1?.expectedPeer) ? arg1.expectedPeer : undefined
|
|
175
|
+
const options: AbortOptions | undefined = isPeerId(arg1) ? arg2 : arg1 === undefined ? arg2 : arg1
|
|
176
|
+
|
|
177
|
+
const envelope = await RecordEnvelope.openAndCertify(buf, PeerRecord.DOMAIN, options)
|
|
205
178
|
const peerId = peerIdFromCID(envelope.publicKey.toCID())
|
|
206
179
|
|
|
207
180
|
if (expectedPeer?.equals(peerId) === false) {
|
|
@@ -213,7 +186,7 @@ class PersistentPeerStore implements PeerStore {
|
|
|
213
186
|
let peer: Peer | undefined
|
|
214
187
|
|
|
215
188
|
try {
|
|
216
|
-
peer = await this.get(peerId)
|
|
189
|
+
peer = await this.get(peerId, options)
|
|
217
190
|
} catch (err: any) {
|
|
218
191
|
if (err.name !== 'NotFoundError') {
|
|
219
192
|
throw err
|
|
@@ -222,7 +195,7 @@ class PersistentPeerStore implements PeerStore {
|
|
|
222
195
|
|
|
223
196
|
// ensure seq is greater than, or equal to, the last received
|
|
224
197
|
if (peer?.peerRecordEnvelope != null) {
|
|
225
|
-
const storedEnvelope =
|
|
198
|
+
const storedEnvelope = RecordEnvelope.createFromProtobuf(peer.peerRecordEnvelope)
|
|
226
199
|
const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
|
|
227
200
|
|
|
228
201
|
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
@@ -237,7 +210,7 @@ class PersistentPeerStore implements PeerStore {
|
|
|
237
210
|
isCertified: true,
|
|
238
211
|
multiaddr
|
|
239
212
|
}))
|
|
240
|
-
})
|
|
213
|
+
}, options)
|
|
241
214
|
|
|
242
215
|
return true
|
|
243
216
|
}
|
package/src/store.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NotFoundError } from '@libp2p/interface'
|
|
2
|
+
import { PeerMap, trackedPeerMap } from '@libp2p/peer-collections'
|
|
2
3
|
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
3
4
|
import mortice from 'mortice'
|
|
4
5
|
import { base32 } from 'multiformats/bases/base32'
|
|
@@ -11,8 +12,9 @@ import { NAMESPACE_COMMON, peerIdToDatastoreKey } from './utils/peer-id-to-datas
|
|
|
11
12
|
import { toPeerPB } from './utils/to-peer-pb.js'
|
|
12
13
|
import type { AddressFilter, PersistentPeerStoreComponents, PersistentPeerStoreInit } from './index.js'
|
|
13
14
|
import type { PeerUpdate as PeerUpdateExternal, PeerId, Peer, PeerData, PeerQuery, Logger } from '@libp2p/interface'
|
|
15
|
+
import type { AbortOptions } from '@multiformats/multiaddr'
|
|
14
16
|
import type { Datastore, Key, Query } from 'interface-datastore'
|
|
15
|
-
import type { Mortice } from 'mortice'
|
|
17
|
+
import type { Mortice, Release } from 'mortice'
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* Event detail emitted when peer data changes
|
|
@@ -52,10 +54,15 @@ function mapQuery (query: PeerQuery, maxAddressAge: number): Query {
|
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
export interface Lock {
|
|
58
|
+
refs: number
|
|
59
|
+
lock: Mortice
|
|
60
|
+
}
|
|
61
|
+
|
|
55
62
|
export class PersistentStore {
|
|
56
63
|
private readonly peerId: PeerId
|
|
57
64
|
private readonly datastore: Datastore
|
|
58
|
-
|
|
65
|
+
private locks: PeerMap<Lock>
|
|
59
66
|
private readonly addressFilter?: AddressFilter
|
|
60
67
|
private readonly log: Logger
|
|
61
68
|
private readonly maxAddressAge: number
|
|
@@ -66,17 +73,80 @@ export class PersistentStore {
|
|
|
66
73
|
this.peerId = components.peerId
|
|
67
74
|
this.datastore = components.datastore
|
|
68
75
|
this.addressFilter = init.addressFilter
|
|
69
|
-
this.
|
|
70
|
-
name: '
|
|
71
|
-
|
|
76
|
+
this.locks = trackedPeerMap({
|
|
77
|
+
name: 'libp2p_peer_store_locks',
|
|
78
|
+
metrics: components.metrics
|
|
72
79
|
})
|
|
73
80
|
this.maxAddressAge = init.maxAddressAge ?? MAX_ADDRESS_AGE
|
|
74
81
|
this.maxPeerAge = init.maxPeerAge ?? MAX_PEER_AGE
|
|
75
82
|
}
|
|
76
83
|
|
|
77
|
-
|
|
84
|
+
getLock (peerId: PeerId): Lock {
|
|
85
|
+
let lock = this.locks.get(peerId)
|
|
86
|
+
|
|
87
|
+
if (lock == null) {
|
|
88
|
+
lock = {
|
|
89
|
+
refs: 0,
|
|
90
|
+
lock: mortice({
|
|
91
|
+
name: peerId.toString(),
|
|
92
|
+
singleProcess: true
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this.locks.set(peerId, lock)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
lock.refs++
|
|
100
|
+
|
|
101
|
+
return lock
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private maybeRemoveLock (peerId: PeerId, lock: Lock): void {
|
|
105
|
+
lock.refs--
|
|
106
|
+
|
|
107
|
+
if (lock.refs === 0) {
|
|
108
|
+
lock.lock.finalize()
|
|
109
|
+
this.locks.delete(peerId)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async getReadLock (peerId: PeerId, options?: AbortOptions): Promise<Release> {
|
|
114
|
+
const lock = this.getLock(peerId)
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const release = await lock.lock.readLock(options)
|
|
118
|
+
|
|
119
|
+
return () => {
|
|
120
|
+
release()
|
|
121
|
+
this.maybeRemoveLock(peerId, lock)
|
|
122
|
+
}
|
|
123
|
+
} catch (err) {
|
|
124
|
+
this.maybeRemoveLock(peerId, lock)
|
|
125
|
+
|
|
126
|
+
throw err
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async getWriteLock (peerId: PeerId, options?: AbortOptions): Promise<Release> {
|
|
131
|
+
const lock = this.getLock(peerId)
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const release = await lock.lock.writeLock(options)
|
|
135
|
+
|
|
136
|
+
return () => {
|
|
137
|
+
release()
|
|
138
|
+
this.maybeRemoveLock(peerId, lock)
|
|
139
|
+
}
|
|
140
|
+
} catch (err) {
|
|
141
|
+
this.maybeRemoveLock(peerId, lock)
|
|
142
|
+
|
|
143
|
+
throw err
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async has (peerId: PeerId, options?: AbortOptions): Promise<boolean> {
|
|
78
148
|
try {
|
|
79
|
-
await this.load(peerId)
|
|
149
|
+
await this.load(peerId, options)
|
|
80
150
|
|
|
81
151
|
return true
|
|
82
152
|
} catch (err: any) {
|
|
@@ -88,41 +158,43 @@ export class PersistentStore {
|
|
|
88
158
|
return false
|
|
89
159
|
}
|
|
90
160
|
|
|
91
|
-
async delete (peerId: PeerId): Promise<void> {
|
|
161
|
+
async delete (peerId: PeerId, options?: AbortOptions): Promise<void> {
|
|
92
162
|
if (this.peerId.equals(peerId)) {
|
|
93
163
|
return
|
|
94
164
|
}
|
|
95
165
|
|
|
96
|
-
await this.datastore.delete(peerIdToDatastoreKey(peerId))
|
|
166
|
+
await this.datastore.delete(peerIdToDatastoreKey(peerId), options)
|
|
97
167
|
}
|
|
98
168
|
|
|
99
|
-
async load (peerId: PeerId): Promise<Peer> {
|
|
169
|
+
async load (peerId: PeerId, options?: AbortOptions): Promise<Peer> {
|
|
100
170
|
const key = peerIdToDatastoreKey(peerId)
|
|
101
|
-
const buf = await this.datastore.get(key)
|
|
171
|
+
const buf = await this.datastore.get(key, options)
|
|
102
172
|
const peer = PeerPB.decode(buf)
|
|
103
173
|
|
|
104
174
|
if (this.#peerIsExpired(peerId, peer)) {
|
|
105
|
-
await this.datastore.delete(key)
|
|
175
|
+
await this.datastore.delete(key, options)
|
|
106
176
|
throw new NotFoundError()
|
|
107
177
|
}
|
|
108
178
|
|
|
109
179
|
return pbToPeer(peerId, peer, this.peerId.equals(peerId) ? Infinity : this.maxAddressAge)
|
|
110
180
|
}
|
|
111
181
|
|
|
112
|
-
async save (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
|
|
113
|
-
const existingPeer = await this.#findExistingPeer(peerId)
|
|
182
|
+
async save (peerId: PeerId, data: PeerData, options?: AbortOptions): Promise<PeerUpdate> {
|
|
183
|
+
const existingPeer = await this.#findExistingPeer(peerId, options)
|
|
114
184
|
|
|
115
185
|
const peerPb: PeerPB = await toPeerPB(peerId, data, 'patch', {
|
|
186
|
+
...options,
|
|
116
187
|
addressFilter: this.addressFilter
|
|
117
188
|
})
|
|
118
189
|
|
|
119
190
|
return this.#saveIfDifferent(peerId, peerPb, existingPeer)
|
|
120
191
|
}
|
|
121
192
|
|
|
122
|
-
async patch (peerId: PeerId, data: Partial<PeerData
|
|
123
|
-
const existingPeer = await this.#findExistingPeer(peerId)
|
|
193
|
+
async patch (peerId: PeerId, data: Partial<PeerData>, options?: AbortOptions): Promise<PeerUpdate> {
|
|
194
|
+
const existingPeer = await this.#findExistingPeer(peerId, options)
|
|
124
195
|
|
|
125
196
|
const peerPb: PeerPB = await toPeerPB(peerId, data, 'patch', {
|
|
197
|
+
...options,
|
|
126
198
|
addressFilter: this.addressFilter,
|
|
127
199
|
existingPeer
|
|
128
200
|
})
|
|
@@ -130,8 +202,8 @@ export class PersistentStore {
|
|
|
130
202
|
return this.#saveIfDifferent(peerId, peerPb, existingPeer)
|
|
131
203
|
}
|
|
132
204
|
|
|
133
|
-
async merge (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
|
|
134
|
-
const existingPeer = await this.#findExistingPeer(peerId)
|
|
205
|
+
async merge (peerId: PeerId, data: PeerData, options?: AbortOptions): Promise<PeerUpdate> {
|
|
206
|
+
const existingPeer = await this.#findExistingPeer(peerId, options)
|
|
135
207
|
|
|
136
208
|
const peerPb: PeerPB = await toPeerPB(peerId, data, 'merge', {
|
|
137
209
|
addressFilter: this.addressFilter,
|
|
@@ -141,8 +213,8 @@ export class PersistentStore {
|
|
|
141
213
|
return this.#saveIfDifferent(peerId, peerPb, existingPeer)
|
|
142
214
|
}
|
|
143
215
|
|
|
144
|
-
async * all (
|
|
145
|
-
for await (const { key, value } of this.datastore.query(mapQuery(
|
|
216
|
+
async * all (options?: PeerQuery): AsyncGenerator<Peer, void, unknown> {
|
|
217
|
+
for await (const { key, value } of this.datastore.query(mapQuery(options ?? {}, this.maxAddressAge), options)) {
|
|
146
218
|
const peerId = keyToPeerId(key)
|
|
147
219
|
|
|
148
220
|
// skip self peer if present
|
|
@@ -154,7 +226,7 @@ export class PersistentStore {
|
|
|
154
226
|
|
|
155
227
|
// remove expired peer
|
|
156
228
|
if (this.#peerIsExpired(peerId, peer)) {
|
|
157
|
-
await this.datastore.delete(key)
|
|
229
|
+
await this.datastore.delete(key, options)
|
|
158
230
|
continue
|
|
159
231
|
}
|
|
160
232
|
|
|
@@ -162,21 +234,21 @@ export class PersistentStore {
|
|
|
162
234
|
}
|
|
163
235
|
}
|
|
164
236
|
|
|
165
|
-
async #findExistingPeer (peerId: PeerId): Promise<ExistingPeer | undefined> {
|
|
237
|
+
async #findExistingPeer (peerId: PeerId, options?: AbortOptions): Promise<ExistingPeer | undefined> {
|
|
166
238
|
try {
|
|
167
239
|
const key = peerIdToDatastoreKey(peerId)
|
|
168
|
-
const buf = await this.datastore.get(key)
|
|
240
|
+
const buf = await this.datastore.get(key, options)
|
|
169
241
|
const peerPB = PeerPB.decode(buf)
|
|
170
242
|
|
|
171
243
|
// remove expired peer
|
|
172
244
|
if (this.#peerIsExpired(peerId, peerPB)) {
|
|
173
|
-
await this.datastore.delete(key)
|
|
245
|
+
await this.datastore.delete(key, options)
|
|
174
246
|
throw new NotFoundError()
|
|
175
247
|
}
|
|
176
248
|
|
|
177
249
|
return {
|
|
178
250
|
peerPB,
|
|
179
|
-
peer:
|
|
251
|
+
peer: pbToPeer(peerId, peerPB, this.maxAddressAge)
|
|
180
252
|
}
|
|
181
253
|
} catch (err: any) {
|
|
182
254
|
if (err.name !== 'NotFoundError') {
|
|
@@ -185,15 +257,15 @@ export class PersistentStore {
|
|
|
185
257
|
}
|
|
186
258
|
}
|
|
187
259
|
|
|
188
|
-
async #saveIfDifferent (peerId: PeerId, peer: PeerPB, existingPeer?: ExistingPeer): Promise<PeerUpdate> {
|
|
260
|
+
async #saveIfDifferent (peerId: PeerId, peer: PeerPB, existingPeer?: ExistingPeer, options?: AbortOptions): Promise<PeerUpdate> {
|
|
189
261
|
// record last update
|
|
190
262
|
peer.updated = Date.now()
|
|
191
263
|
const buf = PeerPB.encode(peer)
|
|
192
264
|
|
|
193
|
-
await this.datastore.put(peerIdToDatastoreKey(peerId), buf)
|
|
265
|
+
await this.datastore.put(peerIdToDatastoreKey(peerId), buf, options)
|
|
194
266
|
|
|
195
267
|
return {
|
|
196
|
-
peer:
|
|
268
|
+
peer: pbToPeer(peerId, peer, this.maxAddressAge),
|
|
197
269
|
previous: existingPeer?.peer,
|
|
198
270
|
updated: existingPeer == null || !peerEquals(peer, existingPeer.peerPB)
|
|
199
271
|
}
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
|
|
2
2
|
import { peerIdFromPublicKey } from '@libp2p/peer-id'
|
|
3
3
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
4
|
-
import { base58btc } from 'multiformats/bases/base58'
|
|
5
|
-
import * as Digest from 'multiformats/hashes/digest'
|
|
6
4
|
import { Peer as PeerPB } from '../pb/peer.js'
|
|
7
5
|
import type { PeerId, Peer, Tag } from '@libp2p/interface'
|
|
6
|
+
import type { Digest } from 'multiformats/hashes/digest'
|
|
8
7
|
|
|
9
8
|
function populatePublicKey (peerId: PeerId, protobuf: PeerPB): PeerId {
|
|
10
9
|
if (peerId.publicKey != null || protobuf.publicKey == null) {
|
|
11
10
|
return peerId
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
let digest:
|
|
13
|
+
let digest: Digest<18, number> | undefined
|
|
15
14
|
|
|
16
15
|
if (peerId.type === 'RSA') {
|
|
17
16
|
// avoid hashing public key
|
|
18
|
-
|
|
19
|
-
digest = Digest.decode(multihash)
|
|
17
|
+
digest = peerId.toMultihash()
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
const publicKey = publicKeyFromProtobuf(protobuf.publicKey, digest)
|
|
@@ -3,8 +3,9 @@ import { isMultiaddr, multiaddr } from '@multiformats/multiaddr'
|
|
|
3
3
|
import type { AddressFilter } from '../index.js'
|
|
4
4
|
import type { Address as AddressPB } from '../pb/peer.js'
|
|
5
5
|
import type { PeerId, Address } from '@libp2p/interface'
|
|
6
|
+
import type { AbortOptions } from '@multiformats/multiaddr'
|
|
6
7
|
|
|
7
|
-
export async function dedupeFilterAndSortAddresses (peerId: PeerId, filter: AddressFilter, addresses: Array<Address | AddressPB | undefined>, existingAddresses?: AddressPB[]): Promise<AddressPB[]> {
|
|
8
|
+
export async function dedupeFilterAndSortAddresses (peerId: PeerId, filter: AddressFilter, addresses: Array<Address | AddressPB | undefined>, existingAddresses?: AddressPB[], options?: AbortOptions): Promise<AddressPB[]> {
|
|
8
9
|
const addressMap = new Map<string, Address>()
|
|
9
10
|
|
|
10
11
|
for (const addr of addresses) {
|
|
@@ -20,7 +21,7 @@ export async function dedupeFilterAndSortAddresses (peerId: PeerId, filter: Addr
|
|
|
20
21
|
throw new InvalidParametersError('Multiaddr was invalid')
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
if (!(await filter(peerId, addr.multiaddr))) {
|
|
24
|
+
if (!(await filter(peerId, addr.multiaddr, options))) {
|
|
24
25
|
continue
|
|
25
26
|
}
|
|
26
27
|
|