@libp2p/peer-store 0.0.0 → 1.0.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/src/address-book.d.ts +5 -5
- package/dist/src/address-book.d.ts.map +1 -1
- package/dist/src/address-book.js +41 -25
- package/dist/src/address-book.js.map +1 -1
- package/dist/src/index.d.ts +6 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +11 -8
- package/dist/src/index.js.map +1 -1
- package/dist/src/key-book.d.ts +4 -4
- package/dist/src/key-book.d.ts.map +1 -1
- package/dist/src/key-book.js +16 -16
- package/dist/src/key-book.js.map +1 -1
- package/dist/src/metadata-book.d.ts +4 -4
- package/dist/src/metadata-book.d.ts.map +1 -1
- package/dist/src/metadata-book.js +24 -15
- package/dist/src/metadata-book.js.map +1 -1
- package/dist/src/proto-book.d.ts +4 -4
- package/dist/src/proto-book.d.ts.map +1 -1
- package/dist/src/proto-book.js +21 -12
- package/dist/src/proto-book.js.map +1 -1
- package/dist/src/store.d.ts +3 -3
- package/dist/src/store.d.ts.map +1 -1
- package/dist/src/store.js +16 -15
- package/dist/src/store.js.map +1 -1
- package/package.json +21 -13
- package/src/address-book.ts +47 -29
- package/src/index.ts +15 -11
- package/src/key-book.ts +19 -19
- package/src/metadata-book.ts +27 -17
- package/src/proto-book.ts +25 -15
- package/src/store.ts +21 -20
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { EventEmitter } from '
|
|
2
|
+
import { EventEmitter } from '@libp2p/interfaces'
|
|
3
3
|
import { PeerStoreAddressBook } from './address-book.js'
|
|
4
4
|
import { PeerStoreKeyBook } from './key-book.js'
|
|
5
5
|
import { PeerStoreMetadataBook } from './metadata-book.js'
|
|
6
6
|
import { PeerStoreProtoBook } from './proto-book.js'
|
|
7
7
|
import { PersistentStore, Store } from './store.js'
|
|
8
|
-
import type { PeerStore, Address, AddressBook, KeyBook, MetadataBook, ProtoBook } from '@libp2p/interfaces/peer-store'
|
|
8
|
+
import type { PeerStore, Address, AddressBook, KeyBook, MetadataBook, ProtoBook, PeerStoreEvents } from '@libp2p/interfaces/peer-store'
|
|
9
9
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
10
10
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
11
11
|
import type { Datastore } from 'interface-datastore'
|
|
@@ -30,14 +30,14 @@ export interface PeerStoreOptions {
|
|
|
30
30
|
/**
|
|
31
31
|
* An implementation of PeerStore that stores data in a Datastore
|
|
32
32
|
*/
|
|
33
|
-
export class
|
|
33
|
+
export class PeerStoreImpl extends EventEmitter<PeerStoreEvents> implements PeerStore {
|
|
34
34
|
public addressBook: AddressBook
|
|
35
35
|
public keyBook: KeyBook
|
|
36
36
|
public metadataBook: MetadataBook
|
|
37
37
|
public protoBook: ProtoBook
|
|
38
38
|
|
|
39
|
-
private peerId: PeerId
|
|
40
|
-
private store: Store
|
|
39
|
+
private readonly peerId: PeerId
|
|
40
|
+
private readonly store: Store
|
|
41
41
|
|
|
42
42
|
constructor (options: PeerStoreOptions) {
|
|
43
43
|
super()
|
|
@@ -47,10 +47,10 @@ export class DefaultPeerStore extends EventEmitter implements PeerStore {
|
|
|
47
47
|
this.peerId = peerId
|
|
48
48
|
this.store = new PersistentStore(datastore)
|
|
49
49
|
|
|
50
|
-
this.addressBook = new PeerStoreAddressBook(this.
|
|
51
|
-
this.keyBook = new PeerStoreKeyBook(this.
|
|
52
|
-
this.metadataBook = new PeerStoreMetadataBook(this.
|
|
53
|
-
this.protoBook = new PeerStoreProtoBook(this.
|
|
50
|
+
this.addressBook = new PeerStoreAddressBook(this.dispatchEvent.bind(this), this.store, addressFilter)
|
|
51
|
+
this.keyBook = new PeerStoreKeyBook(this.dispatchEvent.bind(this), this.store)
|
|
52
|
+
this.metadataBook = new PeerStoreMetadataBook(this.dispatchEvent.bind(this), this.store)
|
|
53
|
+
this.protoBook = new PeerStoreProtoBook(this.dispatchEvent.bind(this), this.store)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
async * getPeers () {
|
|
@@ -98,7 +98,7 @@ export class DefaultPeerStore extends EventEmitter implements PeerStore {
|
|
|
98
98
|
log('get got read lock')
|
|
99
99
|
|
|
100
100
|
try {
|
|
101
|
-
return this.store.load(peerId)
|
|
101
|
+
return await this.store.load(peerId)
|
|
102
102
|
} finally {
|
|
103
103
|
log('get release read lock')
|
|
104
104
|
release()
|
|
@@ -114,10 +114,14 @@ export class DefaultPeerStore extends EventEmitter implements PeerStore {
|
|
|
114
114
|
log('has got read lock')
|
|
115
115
|
|
|
116
116
|
try {
|
|
117
|
-
return this.store.has(peerId)
|
|
117
|
+
return await this.store.has(peerId)
|
|
118
118
|
} finally {
|
|
119
119
|
log('has release read lock')
|
|
120
120
|
release()
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
+
|
|
125
|
+
export function createPeerStore (opts: PeerStoreOptions): PeerStore {
|
|
126
|
+
return new PeerStoreImpl(opts)
|
|
127
|
+
}
|
package/src/key-book.ts
CHANGED
|
@@ -1,30 +1,26 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import errcode from 'err-code'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
|
-
import {
|
|
4
|
+
import { peerIdFromPeerId } from '@libp2p/peer-id'
|
|
5
5
|
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
|
|
6
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
6
7
|
import type { Store } from './store.js'
|
|
7
8
|
import type { PeerStore, KeyBook } from '@libp2p/interfaces/src/peer-store'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {import('./types').PeerStore} PeerStore
|
|
11
|
-
* @typedef {import('./types').KeyBook} KeyBook
|
|
12
|
-
* @typedef {import('libp2p-interfaces/src/keys/types').PublicKey} PublicKey
|
|
13
|
-
*/
|
|
9
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
14
10
|
|
|
15
11
|
const log = logger('libp2p:peer-store:key-book')
|
|
16
12
|
|
|
17
13
|
const EVENT_NAME = 'change:pubkey'
|
|
18
14
|
|
|
19
15
|
export class PeerStoreKeyBook implements KeyBook {
|
|
20
|
-
private
|
|
21
|
-
private store: Store
|
|
16
|
+
private readonly dispatchEvent: PeerStore['dispatchEvent']
|
|
17
|
+
private readonly store: Store
|
|
22
18
|
|
|
23
19
|
/**
|
|
24
20
|
* The KeyBook is responsible for keeping the known public keys of a peer
|
|
25
21
|
*/
|
|
26
|
-
constructor (
|
|
27
|
-
this.
|
|
22
|
+
constructor (dispatchEvent: PeerStore['dispatchEvent'], store: Store) {
|
|
23
|
+
this.dispatchEvent = dispatchEvent
|
|
28
24
|
this.store = store
|
|
29
25
|
}
|
|
30
26
|
|
|
@@ -32,10 +28,10 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
32
28
|
* Set the Peer public key
|
|
33
29
|
*/
|
|
34
30
|
async set (peerId: PeerId, publicKey: Uint8Array) {
|
|
35
|
-
peerId =
|
|
31
|
+
peerId = peerIdFromPeerId(peerId)
|
|
36
32
|
|
|
37
|
-
if (!publicKey) {
|
|
38
|
-
log.error('publicKey must be an instance of
|
|
33
|
+
if (!(publicKey instanceof Uint8Array)) {
|
|
34
|
+
log.error('publicKey must be an instance of Uint8Array to store data')
|
|
39
35
|
throw errcode(new Error('publicKey must be an instance of PublicKey'), codes.ERR_INVALID_PARAMETERS)
|
|
40
36
|
}
|
|
41
37
|
|
|
@@ -49,7 +45,7 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
49
45
|
try {
|
|
50
46
|
const existing = await this.store.load(peerId)
|
|
51
47
|
|
|
52
|
-
if (existing.pubKey && uint8arrayEquals(existing.pubKey, publicKey)) {
|
|
48
|
+
if ((existing.pubKey != null) && uint8arrayEquals(existing.pubKey, publicKey)) {
|
|
53
49
|
return
|
|
54
50
|
}
|
|
55
51
|
} catch (err: any) {
|
|
@@ -68,7 +64,9 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
if (updatedKey) {
|
|
71
|
-
this.
|
|
67
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
68
|
+
detail: { peerId, pubKey: publicKey }
|
|
69
|
+
}))
|
|
72
70
|
}
|
|
73
71
|
}
|
|
74
72
|
|
|
@@ -76,7 +74,7 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
76
74
|
* Get Public key of the given PeerId, if stored
|
|
77
75
|
*/
|
|
78
76
|
async get (peerId: PeerId) {
|
|
79
|
-
peerId =
|
|
77
|
+
peerId = peerIdFromPeerId(peerId)
|
|
80
78
|
|
|
81
79
|
log('get await write lock')
|
|
82
80
|
const release = await this.store.lock.readLock()
|
|
@@ -97,7 +95,7 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
97
95
|
}
|
|
98
96
|
|
|
99
97
|
async delete (peerId: PeerId) {
|
|
100
|
-
peerId =
|
|
98
|
+
peerId = peerIdFromPeerId(peerId)
|
|
101
99
|
|
|
102
100
|
log('delete await write lock')
|
|
103
101
|
const release = await this.store.lock.writeLock()
|
|
@@ -112,6 +110,8 @@ export class PeerStoreKeyBook implements KeyBook {
|
|
|
112
110
|
release()
|
|
113
111
|
}
|
|
114
112
|
|
|
115
|
-
this.
|
|
113
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
114
|
+
detail: { peerId, pubKey: undefined }
|
|
115
|
+
}))
|
|
116
116
|
}
|
|
117
117
|
}
|
package/src/metadata-book.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import errcode from 'err-code'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
|
-
import {
|
|
4
|
+
import { peerIdFromPeerId } from '@libp2p/peer-id'
|
|
5
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
6
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
6
7
|
import type { Store } from './store.js'
|
|
7
8
|
import type { PeerStore, MetadataBook } from '@libp2p/interfaces/src/peer-store'
|
|
9
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
8
10
|
|
|
9
11
|
const log = logger('libp2p:peer-store:metadata-book')
|
|
10
12
|
|
|
11
13
|
const EVENT_NAME = 'change:metadata'
|
|
12
14
|
|
|
13
15
|
export class PeerStoreMetadataBook implements MetadataBook {
|
|
14
|
-
private
|
|
15
|
-
private store: Store
|
|
16
|
+
private readonly dispatchEvent: PeerStore['dispatchEvent']
|
|
17
|
+
private readonly store: Store
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* The MetadataBook is responsible for keeping metadata
|
|
19
21
|
* about known peers
|
|
20
22
|
*/
|
|
21
|
-
constructor (
|
|
22
|
-
this.
|
|
23
|
+
constructor (dispatchEvent: PeerStore['dispatchEvent'], store: Store) {
|
|
24
|
+
this.dispatchEvent = dispatchEvent
|
|
23
25
|
this.store = store
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -27,7 +29,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
27
29
|
* Get the known data of a provided peer
|
|
28
30
|
*/
|
|
29
31
|
async get (peerId: PeerId) {
|
|
30
|
-
peerId =
|
|
32
|
+
peerId = peerIdFromPeerId(peerId)
|
|
31
33
|
|
|
32
34
|
log('get await read lock')
|
|
33
35
|
const release = await this.store.lock.readLock()
|
|
@@ -53,7 +55,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
53
55
|
* Get specific metadata value, if it exists
|
|
54
56
|
*/
|
|
55
57
|
async getValue (peerId: PeerId, key: string) {
|
|
56
|
-
peerId =
|
|
58
|
+
peerId = peerIdFromPeerId(peerId)
|
|
57
59
|
|
|
58
60
|
log('getValue await read lock')
|
|
59
61
|
const release = await this.store.lock.readLock()
|
|
@@ -74,9 +76,9 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
async set (peerId: PeerId, metadata: Map<string, Uint8Array>) {
|
|
77
|
-
peerId =
|
|
79
|
+
peerId = peerIdFromPeerId(peerId)
|
|
78
80
|
|
|
79
|
-
if (!
|
|
81
|
+
if (!(metadata instanceof Map)) {
|
|
80
82
|
log.error('valid metadata must be provided to store data')
|
|
81
83
|
throw errcode(new Error('valid metadata must be provided'), codes.ERR_INVALID_PARAMETERS)
|
|
82
84
|
}
|
|
@@ -94,14 +96,16 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
94
96
|
release()
|
|
95
97
|
}
|
|
96
98
|
|
|
97
|
-
this.
|
|
99
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
100
|
+
detail: { peerId, metadata }
|
|
101
|
+
}))
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
/**
|
|
101
105
|
* Set metadata key and value of a provided peer
|
|
102
106
|
*/
|
|
103
107
|
async setValue (peerId: PeerId, key: string, value: Uint8Array) {
|
|
104
|
-
peerId =
|
|
108
|
+
peerId = peerIdFromPeerId(peerId)
|
|
105
109
|
|
|
106
110
|
if (typeof key !== 'string' || !(value instanceof Uint8Array)) {
|
|
107
111
|
log.error('valid key and value must be provided to store data')
|
|
@@ -136,11 +140,13 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
136
140
|
release()
|
|
137
141
|
}
|
|
138
142
|
|
|
139
|
-
this.
|
|
143
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
144
|
+
detail: { peerId, metadata: updatedPeer.metadata }
|
|
145
|
+
}))
|
|
140
146
|
}
|
|
141
147
|
|
|
142
148
|
async delete (peerId: PeerId) {
|
|
143
|
-
peerId =
|
|
149
|
+
peerId = peerIdFromPeerId(peerId)
|
|
144
150
|
|
|
145
151
|
log('delete await write lock')
|
|
146
152
|
const release = await this.store.lock.writeLock()
|
|
@@ -162,12 +168,14 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
162
168
|
}
|
|
163
169
|
|
|
164
170
|
if (has) {
|
|
165
|
-
this.
|
|
171
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
172
|
+
detail: { peerId, metadata: new Map() }
|
|
173
|
+
}))
|
|
166
174
|
}
|
|
167
175
|
}
|
|
168
176
|
|
|
169
177
|
async deleteValue (peerId: PeerId, key: string) {
|
|
170
|
-
peerId =
|
|
178
|
+
peerId = peerIdFromPeerId(peerId)
|
|
171
179
|
|
|
172
180
|
log('deleteValue await write lock')
|
|
173
181
|
const release = await this.store.lock.writeLock()
|
|
@@ -193,8 +201,10 @@ export class PeerStoreMetadataBook implements MetadataBook {
|
|
|
193
201
|
release()
|
|
194
202
|
}
|
|
195
203
|
|
|
196
|
-
if (metadata) {
|
|
197
|
-
this.
|
|
204
|
+
if (metadata != null) {
|
|
205
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
206
|
+
detail: { peerId, metadata }
|
|
207
|
+
}))
|
|
198
208
|
}
|
|
199
209
|
}
|
|
200
210
|
}
|
package/src/proto-book.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import errcode from 'err-code'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
|
-
import {
|
|
4
|
+
import { peerIdFromPeerId } from '@libp2p/peer-id'
|
|
5
|
+
import { base58btc } from 'multiformats/bases/base58'
|
|
6
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
5
7
|
import type { Store } from './store.js'
|
|
6
8
|
import type { PeerStore, ProtoBook } from '@libp2p/interfaces/src/peer-store'
|
|
7
|
-
import {
|
|
9
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
8
10
|
|
|
9
11
|
const log = logger('libp2p:peer-store:proto-book')
|
|
10
12
|
|
|
11
13
|
const EVENT_NAME = 'change:protocols'
|
|
12
14
|
|
|
13
15
|
export class PeerStoreProtoBook implements ProtoBook {
|
|
14
|
-
private
|
|
15
|
-
private store: Store
|
|
16
|
+
private readonly dispatchEvent: PeerStore['dispatchEvent']
|
|
17
|
+
private readonly store: Store
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* The ProtoBook is responsible for keeping the known supported
|
|
19
21
|
* protocols of a peer
|
|
20
22
|
*/
|
|
21
|
-
constructor (
|
|
22
|
-
this.
|
|
23
|
+
constructor (dispatchEvent: PeerStore['dispatchEvent'], store: Store) {
|
|
24
|
+
this.dispatchEvent = dispatchEvent
|
|
23
25
|
this.store = store
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -45,7 +47,7 @@ export class PeerStoreProtoBook implements ProtoBook {
|
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
async set (peerId: PeerId, protocols: string[]) {
|
|
48
|
-
peerId =
|
|
50
|
+
peerId = peerIdFromPeerId(peerId)
|
|
49
51
|
|
|
50
52
|
if (!Array.isArray(protocols)) {
|
|
51
53
|
log.error('protocols must be provided to store data')
|
|
@@ -83,11 +85,13 @@ export class PeerStoreProtoBook implements ProtoBook {
|
|
|
83
85
|
release()
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
this.
|
|
88
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
89
|
+
detail: { peerId, protocols: updatedPeer.protocols }
|
|
90
|
+
}))
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
async add (peerId: PeerId, protocols: string[]) {
|
|
90
|
-
peerId =
|
|
94
|
+
peerId = peerIdFromPeerId(peerId)
|
|
91
95
|
|
|
92
96
|
if (!Array.isArray(protocols)) {
|
|
93
97
|
log.error('protocols must be provided to store data')
|
|
@@ -126,11 +130,13 @@ export class PeerStoreProtoBook implements ProtoBook {
|
|
|
126
130
|
release()
|
|
127
131
|
}
|
|
128
132
|
|
|
129
|
-
this.
|
|
133
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
134
|
+
detail: { peerId, protocols: updatedPeer.protocols }
|
|
135
|
+
}))
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
async remove (peerId: PeerId, protocols: string[]) {
|
|
133
|
-
peerId =
|
|
139
|
+
peerId = peerIdFromPeerId(peerId)
|
|
134
140
|
|
|
135
141
|
if (!Array.isArray(protocols)) {
|
|
136
142
|
log.error('protocols must be provided to store data')
|
|
@@ -171,11 +177,13 @@ export class PeerStoreProtoBook implements ProtoBook {
|
|
|
171
177
|
release()
|
|
172
178
|
}
|
|
173
179
|
|
|
174
|
-
this.
|
|
180
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
181
|
+
detail: { peerId, protocols: updatedPeer.protocols }
|
|
182
|
+
}))
|
|
175
183
|
}
|
|
176
184
|
|
|
177
185
|
async delete (peerId: PeerId) {
|
|
178
|
-
peerId =
|
|
186
|
+
peerId = peerIdFromPeerId(peerId)
|
|
179
187
|
|
|
180
188
|
log('delete await write lock')
|
|
181
189
|
const release = await this.store.lock.writeLock()
|
|
@@ -197,8 +205,10 @@ export class PeerStoreProtoBook implements ProtoBook {
|
|
|
197
205
|
release()
|
|
198
206
|
}
|
|
199
207
|
|
|
200
|
-
if (has) {
|
|
201
|
-
this.
|
|
208
|
+
if (has === true) {
|
|
209
|
+
this.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
210
|
+
detail: { peerId, protocols: [] }
|
|
211
|
+
}))
|
|
202
212
|
}
|
|
203
213
|
}
|
|
204
214
|
}
|
package/src/store.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import {
|
|
2
|
+
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
3
3
|
import errcode from 'err-code'
|
|
4
4
|
import { codes } from './errors.js'
|
|
5
5
|
import { Key } from 'interface-datastore/key'
|
|
@@ -10,6 +10,7 @@ import mortice from 'mortice'
|
|
|
10
10
|
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
|
|
11
11
|
import type { Peer } from '@libp2p/interfaces/peer-store'
|
|
12
12
|
import type { Datastore } from 'interface-datastore'
|
|
13
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
13
14
|
|
|
14
15
|
const log = logger('libp2p:peer-store:store')
|
|
15
16
|
|
|
@@ -19,7 +20,7 @@ export interface Store {
|
|
|
19
20
|
has: (peerId: PeerId) => Promise<boolean>
|
|
20
21
|
save: (peer: Peer) => Promise<Peer>
|
|
21
22
|
load: (peerId: PeerId) => Promise<Peer>
|
|
22
|
-
delete (peerId: PeerId)
|
|
23
|
+
delete: (peerId: PeerId) => Promise<void>
|
|
23
24
|
merge: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
|
|
24
25
|
mergeOrCreate: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
|
|
25
26
|
patch: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
|
|
@@ -32,9 +33,8 @@ export interface Store {
|
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
36
|
export class PersistentStore {
|
|
37
|
-
private datastore: Datastore
|
|
37
|
+
private readonly datastore: Datastore
|
|
38
38
|
public lock: any
|
|
39
39
|
|
|
40
40
|
constructor (datastore: Datastore) {
|
|
@@ -56,14 +56,14 @@ export class PersistentStore {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async has (peerId: PeerId) {
|
|
59
|
-
return this.datastore.has(this._peerIdToDatastoreKey(peerId))
|
|
59
|
+
return await this.datastore.has(this._peerIdToDatastoreKey(peerId))
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
async delete (peerId: PeerId) {
|
|
63
63
|
await this.datastore.delete(this._peerIdToDatastoreKey(peerId))
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
async load (peerId: PeerId) {
|
|
66
|
+
async load (peerId: PeerId): Promise<Peer> {
|
|
67
67
|
const buf = await this.datastore.get(this._peerIdToDatastoreKey(peerId))
|
|
68
68
|
const peer = PeerPB.decode(buf)
|
|
69
69
|
const metadata = new Map()
|
|
@@ -77,11 +77,12 @@ export class PersistentStore {
|
|
|
77
77
|
id: peerId,
|
|
78
78
|
addresses: peer.addresses.map(({ multiaddr, isCertified }) => ({
|
|
79
79
|
multiaddr: new Multiaddr(multiaddr),
|
|
80
|
-
isCertified: isCertified
|
|
80
|
+
isCertified: isCertified ?? false
|
|
81
81
|
})),
|
|
82
82
|
metadata,
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
pubKey: peer.pubKey ?? undefined,
|
|
84
|
+
peerRecordEnvelope: peer.peerRecordEnvelope ?? undefined
|
|
85
|
+
}
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
async save (peer: Peer) {
|
|
@@ -118,7 +119,7 @@ export class PersistentStore {
|
|
|
118
119
|
|
|
119
120
|
await this.datastore.put(this._peerIdToDatastoreKey(peer.id), buf)
|
|
120
121
|
|
|
121
|
-
return this.load(peer.id)
|
|
122
|
+
return await this.load(peer.id)
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
async patch (peerId: PeerId, data: Partial<Peer>) {
|
|
@@ -154,7 +155,7 @@ export class PersistentStore {
|
|
|
154
155
|
async merge (peerId: PeerId, data: Partial<Peer>) {
|
|
155
156
|
const peer = await this.load(peerId)
|
|
156
157
|
|
|
157
|
-
return this._merge(peerId, data, peer)
|
|
158
|
+
return await this._merge(peerId, data, peer)
|
|
158
159
|
}
|
|
159
160
|
|
|
160
161
|
async mergeOrCreate (peerId: PeerId, data: Partial<Peer>) {
|
|
@@ -180,13 +181,13 @@ export class PersistentStore {
|
|
|
180
181
|
/** @type {Map<string, boolean>} */
|
|
181
182
|
const addresses = new Map()
|
|
182
183
|
|
|
183
|
-
;(data.addresses
|
|
184
|
+
;(data.addresses ?? []).forEach(addr => {
|
|
184
185
|
addresses.set(addr.multiaddr.toString(), addr.isCertified)
|
|
185
186
|
})
|
|
186
187
|
|
|
187
188
|
peer.addresses.forEach(({ multiaddr, isCertified }) => {
|
|
188
189
|
const addrStr = multiaddr.toString()
|
|
189
|
-
addresses.set(addrStr, Boolean(addresses.
|
|
190
|
+
addresses.set(addrStr, Boolean(addresses.has(addrStr) ?? isCertified))
|
|
190
191
|
})
|
|
191
192
|
|
|
192
193
|
return await this.save({
|
|
@@ -198,15 +199,15 @@ export class PersistentStore {
|
|
|
198
199
|
}
|
|
199
200
|
}),
|
|
200
201
|
protocols: Array.from(new Set([
|
|
201
|
-
...(peer.protocols
|
|
202
|
-
...(data.protocols
|
|
202
|
+
...(peer.protocols ?? []),
|
|
203
|
+
...(data.protocols ?? [])
|
|
203
204
|
])),
|
|
204
205
|
metadata: new Map([
|
|
205
|
-
...(peer.metadata
|
|
206
|
-
...(data.metadata
|
|
206
|
+
...(peer.metadata?.entries() ?? []),
|
|
207
|
+
...(data.metadata?.entries() ?? [])
|
|
207
208
|
]),
|
|
208
|
-
pubKey: data.pubKey
|
|
209
|
-
peerRecordEnvelope: data.peerRecordEnvelope
|
|
209
|
+
pubKey: data.pubKey ?? (peer != null ? peer.pubKey : undefined),
|
|
210
|
+
peerRecordEnvelope: data.peerRecordEnvelope ?? (peer != null ? peer.peerRecordEnvelope : undefined)
|
|
210
211
|
})
|
|
211
212
|
}
|
|
212
213
|
|
|
@@ -218,7 +219,7 @@ export class PersistentStore {
|
|
|
218
219
|
const base32Str = key.toString().split('/')[2]
|
|
219
220
|
const buf = base32.decode(base32Str)
|
|
220
221
|
|
|
221
|
-
yield this.load(
|
|
222
|
+
yield this.load(peerIdFromBytes(buf))
|
|
222
223
|
}
|
|
223
224
|
}
|
|
224
225
|
}
|