chore: apply suggestions from code review

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
Vasco Santos
2020-05-06 16:55:43 +02:00
committed by Jacob Heun
parent ce38033c10
commit 7b8d01697d
10 changed files with 112 additions and 82 deletions

View File

@ -10,7 +10,9 @@ Several libp2p subsystems will perform operations, which will gather relevant in
In a libp2p node's life, it will discover peers through its discovery protocols. In a typical discovery protocol, addresses of the peer are discovered along with its peer id. Once this happens, the PeerStore should collect this information for future (or immediate) usage by other subsystems. When the information is stored, the PeerStore should inform interested parties of the peer discovered (`peer` event).
Taking into account a different scenario, a peer might perform/receive a dial request to/from a unkwown peer. In such a scenario, the PeerStore must store the peer's multiaddr once a connection is established.
Taking into account a different scenario, a peer might perform/receive a dial request to/from a unkwown peer. In such a scenario, the PeerStore must store the peer's multiaddr once a connection is established.
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`.
After a connection is established with a peer, the Identify protocol will run automatically. A stream is created and peers exchange their information (Multiaddrs, running protocols and their public key). Once this information is obtained, it should be added to the PeerStore. In this specific case, as we are speaking to the source of truth, we should ensure the PeerStore is prioritizing these records. If the recorded `multiaddrs` or `protocols` have changed, interested parties must be informed via the `change:multiaddrs` or `change:protocols` events respectively.
@ -42,7 +44,7 @@ The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each p
`Map<string, Address>`
A `peerId.toString()` identifier mapping to a `Address` object, which should have the following structure:
A `peerId.toB58String()` identifier mapping to a `Address` object, which should have the following structure:
```js
{
@ -52,11 +54,11 @@ A `peerId.toString()` identifier mapping to a `Address` object, which should hav
#### Key Book
The `keyBook` tracks the publick keys of the peers by keeping their [`PeerId`][peer-id].
The `keyBook` tracks the public keys of the peers by keeping their [`PeerId`][peer-id].
`Map<string, PeerId`
A `peerId.toString()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
A `peerId.toB58String()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
#### Protocol Book
@ -64,7 +66,7 @@ The `protoBook` holds the identifiers of the protocols supported by each peer. T
`Map<string, Set<string>>`
A `peerId.toString()` identifier mapping to a `Set` of protocol identifier strings.
A `peerId.toB58String()` identifier mapping to a `Set` of protocol identifier strings.
#### Metadata Book
@ -129,4 +131,4 @@ Metadata is stored under the following key pattern:
- When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
- 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.
[peer-id]: https://github.com/libp2p/js-peer-id
[peer-id]: https://github.com/libp2p/js-peer-id

View File

@ -86,6 +86,11 @@ class AddressBook extends Book {
this._setData(peerId, addresses)
log(`stored provided multiaddrs for ${id}`)
// Notify the existance of a new peer
if (!rec) {
this._ps.emit('peer', peerId)
}
return this
}
@ -125,6 +130,11 @@ class AddressBook extends Book {
log(`added provided multiaddrs for ${id}`)
// Notify the existance of a new peer
if (!rec) {
this._ps.emit('peer', peerId)
}
return this
}

View File

@ -58,11 +58,6 @@ class Book {
// Store data in memory
this.data.set(b58key, data)
// Store PeerId
if (!PeerId.isPeerId(data)) {
this._ps.keyBook.set(peerId)
}
// Emit event
emit && this._emit(peerId, data)
}

View File

@ -24,7 +24,7 @@ class KeyBook extends Book {
constructor (peerStore) {
super({
peerStore,
eventName: 'change:pubkey', // TODO: the name is not probably the best!?
eventName: 'change:pubkey',
eventProperty: 'pubkey',
eventTransformer: (data) => data.pubKey
})
@ -37,12 +37,13 @@ class KeyBook extends Book {
}
/**
* Set PeerId. If the peer was not known before, it will be added.
* Set the Peer public key.
* @override
* @param {PeerId} peerId
* @param {RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey} publicKey
* @return {KeyBook}
*/
set (peerId) {
*/
set (peerId, publicKey) {
if (!PeerId.isPeerId(peerId)) {
log.error('peerId must be an instance of peer-id to store data')
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
@ -51,12 +52,13 @@ class KeyBook extends Book {
const id = peerId.toB58String()
const recPeerId = this.data.get(id)
!recPeerId && this._ps.emit('peer', peerId)
// If no record available, or it is incomplete
if (!recPeerId || (peerId.pubKey && !recPeerId.pubKey)) {
this._setData(peerId, peerId, {
emit: Boolean(peerId.pubKey) // No persistence if no public key
})
// If no record available, and this is valid
if (!recPeerId && publicKey) {
// This might be unecessary, but we want to store the PeerId
// to avoid an async operation when reconstructing the PeerId
peerId.pubKey = publicKey
this._setData(peerId, peerId)
log(`stored provided public key for ${id}`)
}
@ -67,7 +69,7 @@ class KeyBook extends Book {
* Get Public key of the given PeerId, if stored.
* @override
* @param {PeerId} peerId
* @return {PublicKey}
* @return {RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey}
*/
get (peerId) {
if (!PeerId.isPeerId(peerId)) {

View File

@ -111,7 +111,7 @@ class PersistentPeerStore extends PeerStore {
log('create batch commit')
const batch = this._datastore.batch()
for (const peerIdStr of commitPeers) {
// PeerId (replace by keyBook)
// PeerId
const peerId = this.keyBook.data.get(peerIdStr) || PeerId.createFromB58String(peerIdStr)
// Address Book