|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('assert') |
| 4 | +const debug = require('debug') |
| 5 | +const log = debug('libp2p:peer-store') |
| 6 | +log.error = debug('libp2p:peer-store:error') |
| 7 | + |
| 8 | +const { EventEmitter } = require('events') |
| 9 | + |
| 10 | +const PeerInfo = require('peer-info') |
| 11 | + |
| 12 | +/** |
| 13 | + * Responsible for managing known peers, as well as their addresses and metadata |
| 14 | + * @fires PeerStore#peer Emitted when a peer is connected to this node |
| 15 | + * @fires PeerStore#change:protocols |
| 16 | + * @fires PeerStore#change:multiaddrs |
| 17 | + */ |
| 18 | +class PeerStore extends EventEmitter { |
| 19 | + constructor () { |
| 20 | + super() |
| 21 | + |
| 22 | + /** |
| 23 | + * Map of peers |
| 24 | + * |
| 25 | + * @type {Map<string, PeerInfo>} |
| 26 | + */ |
| 27 | + this.peers = new Map() |
| 28 | + |
| 29 | + // TODO: Track ourselves. We should split `peerInfo` up into its pieces so we get better |
| 30 | + // control and observability. This will be the initial step for removing PeerInfo |
| 31 | + // https://github.com/libp2p/go-libp2p-core/blob/master/peerstore/peerstore.go |
| 32 | + // this.addressBook = new Map() |
| 33 | + // this.protoBook = new Map() |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Stores the peerInfo of a new peer. |
| 38 | + * If already exist, its info is updated. |
| 39 | + * @param {PeerInfo} peerInfo |
| 40 | + */ |
| 41 | + put (peerInfo) { |
| 42 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 43 | + |
| 44 | + // Already know the peer? |
| 45 | + if (this.peers.has(peerInfo.id.toB58String())) { |
| 46 | + this.update(peerInfo) |
| 47 | + } else { |
| 48 | + this.add(peerInfo) |
| 49 | + |
| 50 | + // Emit the new peer found |
| 51 | + this.emit('peer', peerInfo) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Add a new peer to the store. |
| 57 | + * @param {PeerInfo} peerInfo |
| 58 | + */ |
| 59 | + add (peerInfo) { |
| 60 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 61 | + |
| 62 | + // Create new instance and add values to it |
| 63 | + const newPeerInfo = new PeerInfo(peerInfo.id) |
| 64 | + |
| 65 | + peerInfo.multiaddrs.forEach((ma) => newPeerInfo.multiaddrs.add(ma)) |
| 66 | + peerInfo.protocols.forEach((p) => newPeerInfo.protocols.add(p)) |
| 67 | + |
| 68 | + const connectedMa = peerInfo.isConnected() |
| 69 | + connectedMa && newPeerInfo.connect(connectedMa) |
| 70 | + |
| 71 | + const peerProxy = new Proxy(newPeerInfo, { |
| 72 | + set: (obj, prop, value) => { |
| 73 | + if (prop === 'multiaddrs') { |
| 74 | + this.emit('change:multiaddrs', { |
| 75 | + peerInfo: obj, |
| 76 | + multiaddrs: value.toArray() |
| 77 | + }) |
| 78 | + } else if (prop === 'protocols') { |
| 79 | + this.emit('change:protocols', { |
| 80 | + peerInfo: obj, |
| 81 | + protocols: Array.from(value) |
| 82 | + }) |
| 83 | + } |
| 84 | + return Reflect.set(...arguments) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + this.peers.set(peerInfo.id.toB58String(), peerProxy) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Updates an already known peer. |
| 93 | + * @param {PeerInfo} peerInfo |
| 94 | + */ |
| 95 | + update (peerInfo) { |
| 96 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 97 | + const id = peerInfo.id.toB58String() |
| 98 | + const recorded = this.peers.get(id) |
| 99 | + |
| 100 | + // pass active connection state |
| 101 | + const ma = peerInfo.isConnected() |
| 102 | + if (ma) { |
| 103 | + recorded.connect(ma) |
| 104 | + } |
| 105 | + |
| 106 | + // Verify new multiaddrs |
| 107 | + // TODO: better track added and removed multiaddrs |
| 108 | + const multiaddrsIntersection = [ |
| 109 | + ...recorded.multiaddrs.toArray() |
| 110 | + ].filter((m) => peerInfo.multiaddrs.has(m)) |
| 111 | + |
| 112 | + if (multiaddrsIntersection.length !== peerInfo.multiaddrs.size || |
| 113 | + multiaddrsIntersection.length !== recorded.multiaddrs.size) { |
| 114 | + // recorded.multiaddrs = peerInfo.multiaddrs |
| 115 | + recorded.multiaddrs.clear() |
| 116 | + |
| 117 | + for (const ma of peerInfo.multiaddrs.toArray()) { |
| 118 | + recorded.multiaddrs.add(ma) |
| 119 | + } |
| 120 | + |
| 121 | + this.emit('change:multiaddrs', { |
| 122 | + peerInfo: peerInfo, |
| 123 | + multiaddrs: recorded.multiaddrs.toArray() |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + // Update protocols |
| 128 | + // TODO: better track added and removed protocols |
| 129 | + const protocolsIntersection = new Set( |
| 130 | + [...recorded.protocols].filter((p) => peerInfo.protocols.has(p)) |
| 131 | + ) |
| 132 | + |
| 133 | + if (protocolsIntersection.size !== peerInfo.protocols.size || |
| 134 | + protocolsIntersection.size !== recorded.protocols.size) { |
| 135 | + recorded.protocols.clear() |
| 136 | + |
| 137 | + for (const protocol of peerInfo.protocols) { |
| 138 | + recorded.protocols.add(protocol) |
| 139 | + } |
| 140 | + |
| 141 | + this.emit('change:protocols', { |
| 142 | + peerInfo: peerInfo, |
| 143 | + protocols: Array.from(recorded.protocols) |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + // Add the public key if missing |
| 148 | + if (!recorded.id.pubKey && peerInfo.id.pubKey) { |
| 149 | + recorded.id.pubKey = peerInfo.id.pubKey |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get the info to the given id. |
| 155 | + * @param {string} peerId b58str id |
| 156 | + * @returns {PeerInfo} |
| 157 | + */ |
| 158 | + get (peerId) { |
| 159 | + const peerInfo = this.peers.get(peerId) |
| 160 | + |
| 161 | + if (peerInfo) { |
| 162 | + return peerInfo |
| 163 | + } |
| 164 | + |
| 165 | + return undefined |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Removes the Peer with the matching `peerId` from the PeerStore |
| 170 | + * @param {string} peerId b58str id |
| 171 | + * @returns {boolean} true if found and removed |
| 172 | + */ |
| 173 | + remove (peerId) { |
| 174 | + return this.peers.delete(peerId) |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Completely replaces the existing peers metadata with the given `peerInfo` |
| 179 | + * @param {PeerInfo} peerInfo |
| 180 | + * @returns {void} |
| 181 | + */ |
| 182 | + replace (peerInfo) { |
| 183 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 184 | + |
| 185 | + this.remove(peerInfo.id.toB58String()) |
| 186 | + this.add(peerInfo) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +module.exports = PeerStore |
0 commit comments