@@ -4,7 +4,6 @@ const assert = require('assert')
4
4
const debug = require ( 'debug' )
5
5
const log = debug ( 'libp2p:peer-store' )
6
6
log . error = debug ( 'libp2p:peer-store:error' )
7
- const errCode = require ( 'err-code' )
8
7
9
8
const { EventEmitter } = require ( 'events' )
10
9
@@ -60,18 +59,43 @@ class PeerStore extends EventEmitter {
60
59
add ( peerInfo ) {
61
60
assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of peer-info' )
62
61
63
- this . peers . set ( peerInfo . id . toB58String ( ) , peerInfo )
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 true
85
+ }
86
+ } )
87
+
88
+ this . peers . set ( peerInfo . id . toB58String ( ) , peerProxy )
64
89
}
65
90
66
91
/**
67
92
* Updates an already known peer.
68
- * If already exist, updates ids info if outdated.
69
93
* @param {PeerInfo } peerInfo
70
94
*/
71
95
update ( peerInfo ) {
72
96
assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of peer-info' )
73
-
74
- const recorded = this . peers . get ( peerInfo . id . toB58String ( ) )
97
+ const id = peerInfo . id . toB58String ( )
98
+ const recorded = this . peers . get ( id )
75
99
76
100
// pass active connection state
77
101
const ma = peerInfo . isConnected ( )
@@ -81,22 +105,41 @@ class PeerStore extends EventEmitter {
81
105
82
106
// Verify new multiaddrs
83
107
// TODO: better track added and removed multiaddrs
84
- if ( peerInfo . multiaddrs . size || recorded . multiaddrs . size ) {
85
- recorded . multiaddrs = peerInfo . 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
+ }
86
120
87
121
this . emit ( 'change:multiaddrs' , {
88
- peerInfo : recorded ,
89
- multiaddrs : Array . from ( recorded . multiaddrs )
122
+ peerInfo : peerInfo ,
123
+ multiaddrs : recorded . multiaddrs . toArray ( )
90
124
} )
91
125
}
92
126
93
127
// Update protocols
94
128
// TODO: better track added and removed protocols
95
- if ( peerInfo . protocols . size || recorded . protocols . size ) {
96
- recorded . protocols = new Set ( peerInfo . 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
+ }
97
140
98
141
this . emit ( 'change:protocols' , {
99
- peerInfo : recorded ,
142
+ peerInfo : peerInfo ,
100
143
protocols : Array . from ( recorded . protocols )
101
144
} )
102
145
}
@@ -105,6 +148,8 @@ class PeerStore extends EventEmitter {
105
148
if ( ! recorded . id . pubKey && peerInfo . id . pubKey ) {
106
149
recorded . id . pubKey = peerInfo . id . pubKey
107
150
}
151
+
152
+ // this.peers.set(id, recorded)
108
153
}
109
154
110
155
/**
@@ -119,19 +164,11 @@ class PeerStore extends EventEmitter {
119
164
return peerInfo
120
165
}
121
166
122
- throw errCode ( new Error ( 'PeerInfo was not found' ) , 'ERR_NO_PEER_INFO' )
123
- }
124
-
125
- /**
126
- * Get an array with all peers known.
127
- * @returns {Array<PeerInfo> }
128
- */
129
- getAllArray ( ) {
130
- return Array . from ( this . peers . values ( ) )
167
+ return undefined
131
168
}
132
169
133
170
/**
134
- * Remove the info of the peer with the given id.
171
+ * Removes the Peer with the matching `peerId` from the PeerStore
135
172
* @param {string } peerId b58str id
136
173
* @returns {boolean } true if found and removed
137
174
*/
@@ -140,7 +177,7 @@ class PeerStore extends EventEmitter {
140
177
}
141
178
142
179
/**
143
- * Replace the info stored of the given peer.
180
+ * Completely replaces the existing peers metadata with the given `peerInfo`
144
181
* @param {PeerInfo } peerInfo
145
182
* @returns {void }
146
183
*/
0 commit comments