@libp2p/peer-store 7.0.1 → 8.0.0

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.
Files changed (76) hide show
  1. package/README.md +0 -170
  2. package/dist/index.min.js +14 -14
  3. package/dist/src/errors.d.ts +0 -1
  4. package/dist/src/errors.d.ts.map +1 -1
  5. package/dist/src/errors.js +1 -2
  6. package/dist/src/errors.js.map +1 -1
  7. package/dist/src/index.d.ts +23 -25
  8. package/dist/src/index.d.ts.map +1 -1
  9. package/dist/src/index.js +86 -88
  10. package/dist/src/index.js.map +1 -1
  11. package/dist/src/pb/peer.d.ts +28 -9
  12. package/dist/src/pb/peer.d.ts.map +1 -1
  13. package/dist/src/pb/peer.js +147 -31
  14. package/dist/src/pb/peer.js.map +1 -1
  15. package/dist/src/store.d.ts +18 -28
  16. package/dist/src/store.d.ts.map +1 -1
  17. package/dist/src/store.js +77 -147
  18. package/dist/src/store.js.map +1 -1
  19. package/dist/src/utils/bytes-to-peer.d.ts +4 -0
  20. package/dist/src/utils/bytes-to-peer.d.ts.map +1 -0
  21. package/dist/src/utils/bytes-to-peer.js +33 -0
  22. package/dist/src/utils/bytes-to-peer.js.map +1 -0
  23. package/dist/src/utils/dedupe-addresses.d.ts +6 -0
  24. package/dist/src/utils/dedupe-addresses.d.ts.map +1 -0
  25. package/dist/src/utils/dedupe-addresses.js +41 -0
  26. package/dist/src/utils/dedupe-addresses.js.map +1 -0
  27. package/dist/src/utils/peer-data-to-datastore-peer.d.ts +5 -0
  28. package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +1 -0
  29. package/dist/src/utils/peer-data-to-datastore-peer.js +92 -0
  30. package/dist/src/utils/peer-data-to-datastore-peer.js.map +1 -0
  31. package/dist/src/utils/peer-id-to-datastore-key.d.ts +5 -0
  32. package/dist/src/utils/peer-id-to-datastore-key.d.ts.map +1 -0
  33. package/dist/src/utils/peer-id-to-datastore-key.js +13 -0
  34. package/dist/src/utils/peer-id-to-datastore-key.js.map +1 -0
  35. package/dist/src/utils/to-peer-pb.d.ts +10 -0
  36. package/dist/src/utils/to-peer-pb.d.ts.map +1 -0
  37. package/dist/src/utils/to-peer-pb.js +182 -0
  38. package/dist/src/utils/to-peer-pb.js.map +1 -0
  39. package/dist/typedoc-urls.json +13 -2
  40. package/package.json +8 -10
  41. package/src/errors.ts +1 -2
  42. package/src/index.ts +97 -103
  43. package/src/pb/peer.proto +10 -7
  44. package/src/pb/peer.ts +187 -37
  45. package/src/store.ts +102 -189
  46. package/src/utils/bytes-to-peer.ts +41 -0
  47. package/src/utils/dedupe-addresses.ts +51 -0
  48. package/src/utils/peer-data-to-datastore-peer.ts +116 -0
  49. package/src/utils/peer-id-to-datastore-key.ts +15 -0
  50. package/src/utils/to-peer-pb.ts +237 -0
  51. package/dist/src/address-book.d.ts +0 -29
  52. package/dist/src/address-book.d.ts.map +0 -1
  53. package/dist/src/address-book.js +0 -304
  54. package/dist/src/address-book.js.map +0 -1
  55. package/dist/src/key-book.d.ts +0 -21
  56. package/dist/src/key-book.d.ts.map +0 -1
  57. package/dist/src/key-book.js +0 -121
  58. package/dist/src/key-book.js.map +0 -1
  59. package/dist/src/metadata-book.d.ts +0 -28
  60. package/dist/src/metadata-book.d.ts.map +0 -1
  61. package/dist/src/metadata-book.js +0 -211
  62. package/dist/src/metadata-book.js.map +0 -1
  63. package/dist/src/pb/tags.d.ts +0 -21
  64. package/dist/src/pb/tags.d.ts.map +0 -1
  65. package/dist/src/pb/tags.js +0 -111
  66. package/dist/src/pb/tags.js.map +0 -1
  67. package/dist/src/proto-book.d.ts +0 -18
  68. package/dist/src/proto-book.d.ts.map +0 -1
  69. package/dist/src/proto-book.js +0 -199
  70. package/dist/src/proto-book.js.map +0 -1
  71. package/src/address-book.ts +0 -367
  72. package/src/key-book.ts +0 -140
  73. package/src/metadata-book.ts +0 -244
  74. package/src/pb/tags.proto +0 -11
  75. package/src/pb/tags.ts +0 -145
  76. package/src/proto-book.ts +0 -234
package/README.md CHANGED
@@ -11,26 +11,6 @@
11
11
 
12
12
  - [Install](#install)
13
13
  - [Browser `<script>` tag](#browser-script-tag)
14
- - [Description](#description)
15
- - [Submitting records to the PeerStore](#submitting-records-to-the-peerstore)
16
- - [Identify](#identify)
17
- - [Peer Discovery](#peer-discovery)
18
- - [Dialer](#dialer)
19
- - [DHT](#dht)
20
- - [Retrieving records from the PeerStore](#retrieving-records-from-the-peerstore)
21
- - [Peer](#peer)
22
- - [Protocols](#protocols)
23
- - [Multiaddrs](#multiaddrs)
24
- - [PeerStore implementation](#peerstore-implementation)
25
- - [Components](#components)
26
- - [Address Book](#address-book)
27
- - [Key Book](#key-book)
28
- - [Protocol Book](#protocol-book)
29
- - [Metadata Book](#metadata-book)
30
- - [API](#api)
31
- - [Events](#events)
32
- - [Data Persistence](#data-persistence)
33
- - [Future Considerations](#future-considerations)
34
14
  - [API Docs](#api-docs)
35
15
  - [License](#license)
36
16
  - [Contribution](#contribution)
@@ -49,156 +29,6 @@ Loading this module through a script tag will make it's exports available as `Li
49
29
  <script src="https://unpkg.com/@libp2p/peer-store/dist/index.min.js"></script>
50
30
  ```
51
31
 
52
- ## Description
53
-
54
- Libp2p's PeerStore is responsible for keeping an updated register with the relevant information of the known peers. It should be the single source of truth for all peer data, where a subsystem can learn about peers' data and where someone can listen for updates. The PeerStore comprises four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`.
55
-
56
- The PeerStore manages the high level operations on its inner books. Moreover, the PeerStore should be responsible for notifying interested parties of relevant events, through its Event Emitter.
57
-
58
- ### Submitting records to the PeerStore
59
-
60
- Several libp2p subsystems will perform operations that might gather relevant information about peers.
61
-
62
- #### Identify
63
-
64
- - The Identify protocol automatically runs on every connection when multiplexing is enabled. The protocol will put the multiaddrs and protocols provided by the peer to the PeerStore.
65
- - In the background, the Identify Service is also waiting for protocol change notifications of peers via the IdentifyPush protocol. Peers may leverage the `identify-push` message to communicate protocol changes to all connected peers, so that their PeerStore can be updated with the updated protocols.
66
- - While it is currently not supported in js-libp2p, future iterations may also support the [IdentifyDelta protocol](https://github.com/libp2p/specs/pull/176).
67
- - Taking into account that the Identify protocol records are directly from the peer, they should be considered the source of truth and weighted accordingly.
68
-
69
- #### Peer Discovery
70
-
71
- - Libp2p discovery protocols aim to discover new peers in the network. In a typical discovery protocol, addresses of the peer are discovered along with its peer id. Once this happens, a libp2p discovery protocol should emit a `peer` event with the information of the discovered peer and this information will be added to the PeerStore by libp2p.
72
-
73
- #### Dialer
74
-
75
- - Libp2p API supports dialing a peer given a `multiaddr`, and no prior knowledge of the peer. If the node is able to establish a connection with the peer, it and its multiaddr is added to the PeerStore.
76
- - When a connection is being upgraded, more precisely after its encryption, or even in a discovery protocol, a libp2p node can get to know other parties public keys. In this scenario, libp2p will add the peer's public key to its `KeyBook`.
77
-
78
- #### DHT
79
-
80
- - On some DHT operations, such as finding providers for a given CID, nodes may exchange peer data as part of the query. This passive peer discovery should result in the DHT emitting the `peer` event in the same way [Peer Discovery](#peerdiscovery) does.
81
-
82
- ### Retrieving records from the PeerStore
83
-
84
- When data in the PeerStore is updated the PeerStore will emit events based on the changes, to allow applications and other subsystems to take action on those changes. Any subsystem interested in these notifications should subscribe the [`PeerStore events`][peer-store-events].
85
-
86
- #### Peer
87
-
88
- - Each time a new peer is discovered, the PeerStore should emit a [`peer` event][peer-store-events], so that interested parties can leverage this peer and establish a connection with it.
89
-
90
- #### Protocols
91
-
92
- - When the known protocols of a peer change, the PeerStore emits a [`change:protocols` event][peer-store-events].
93
-
94
- #### Multiaddrs
95
-
96
- - When the known listening `multiaddrs` of a peer change, the PeerStore emits a [`change:multiaddrs` event][peer-store-events].
97
-
98
- ### PeerStore implementation
99
-
100
- The PeerStore wraps four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`. Moreover, it provides a high level API for those components, as well as data events.
101
-
102
- ### Components
103
-
104
- #### Address Book
105
-
106
- The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each peer may change over time and the Address Book must account for this.
107
-
108
- `Map<string, Address>`
109
-
110
- A `peerId.toString()` identifier mapping to a `Address` object, which should have the following structure:
111
-
112
- ```js
113
- {
114
- multiaddr: <Multiaddr>
115
- }
116
- ```
117
-
118
- #### Key Book
119
-
120
- The `keyBook` tracks the public keys of the peers by keeping their [`PeerId`][peer-id].
121
-
122
- `Map<string, PeerId`
123
-
124
- A `peerId.toString()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
125
-
126
- #### Protocol Book
127
-
128
- The `protoBook` holds the identifiers of the protocols supported by each peer. The protocols supported by each peer are dynamic and will change over time.
129
-
130
- `Map<string, Set<string>>`
131
-
132
- A `peerId.toString()` identifier mapping to a `Set` of protocol identifier strings.
133
-
134
- #### Metadata Book
135
-
136
- The `metadataBook` keeps track of the known metadata of a peer. Its metadata is stored in a key value fashion, where a key identifier (`string`) represents a metadata value (`Uint8Array`).
137
-
138
- `Map<string, Map<string, Uint8Array>>`
139
-
140
- A `peerId.toString()` identifier mapping to the peer metadata Map.
141
-
142
- ### API
143
-
144
- For the complete API documentation, you should check the [API.md](https://libp2p.github.io/js-libp2p-peer-store).
145
-
146
- Access to its underlying books:
147
-
148
- - `peerStore.addressBook.*`
149
- - `peerStore.keyBook.*`
150
- - `peerStore.metadataBook.*`
151
- - `peerStore.protoBook.*`
152
-
153
- ### Events
154
-
155
- - `peer` - emitted when a new peer is added.
156
- - `change:multiaddrs` - emitted when a known peer has a different set of multiaddrs.
157
- - `change:protocols` - emitted when a known peer supports a different set of protocols.
158
- - `change:pubkey` - emitted when a peer's public key is known.
159
- - `change:metadata` - emitted when known metadata of a peer changes.
160
-
161
- ## Data Persistence
162
-
163
- The data stored in the PeerStore can be persisted if configured appropriately. Keeping a record of the peers already discovered by the peer, as well as their known data aims to improve the efficiency of peers joining the network after being offline.
164
-
165
- The libp2p node will need to receive a [datastore](https://github.com/ipfs/interface-datastore), in order to persist this data across restarts. A [datastore](https://github.com/ipfs/interface-datastore) stores its data in a key-value fashion. As a result, we need coherent keys so that we do not overwrite data.
166
-
167
- The PeerStore should not continuously update the datastore whenever data is changed. Instead, it should only store new data after reaching a certain threshold of "dirty" peers, as well as when the node is stopped, in order to batch writes to the datastore.
168
-
169
- The peer id will be appended to the datastore key for each data namespace. The namespaces were defined as follows:
170
-
171
- **AddressBook**
172
-
173
- All the known peer addresses are stored with a key pattern as follows:
174
-
175
- `/peers/addrs/<b32 peer id no padding>`
176
-
177
- **ProtoBook**
178
-
179
- All the known peer protocols are stored with a key pattern as follows:
180
-
181
- `/peers/protos/<b32 peer id no padding>`
182
-
183
- **KeyBook**
184
-
185
- All public keys are stored under the following pattern:
186
-
187
- ` /peers/keys/<b32 peer id no padding>`
188
-
189
- **MetadataBook**
190
-
191
- Metadata is stored under the following key pattern:
192
-
193
- `/peers/metadata/<b32 peer id no padding>/<key>`
194
-
195
- ## Future Considerations
196
-
197
- - If multiaddr TTLs are added, the PeerStore may schedule jobs to delete all addresses that exceed the TTL to prevent AddressBook bloating
198
- - Further API methods will probably need to be added in the context of multiaddr validity and confidence.
199
- - When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
200
- - When improving libp2p configuration, we should think about a possible way of allowing the configuration of Bootstrap to be influenced by the persisted peers, as a way to decrease the load on Bootstrap nodes.
201
-
202
32
  ## API Docs
203
33
 
204
34
  - <https://libp2p.github.io/js-libp2p-peer-store>