@@ -21,6 +21,8 @@ import type { Registrar } from '@libp2p/interface-registrar'
2121import type { ConnectionManager } from '@libp2p/interface-connection-manager'
2222import type { PeerStore } from '@libp2p/interface-peer-store'
2323import { MemoryDatastore } from 'datastore-core'
24+ import { EventEmitter } from '@libp2p/interfaces/events'
25+ import type { Libp2pEvents } from '@libp2p/interface-libp2p'
2426
2527describe ( 'Routing Table' , ( ) => {
2628 let table : RoutingTable
@@ -29,18 +31,22 @@ describe('Routing Table', () => {
2931 beforeEach ( async function ( ) {
3032 this . timeout ( 20 * 1000 )
3133
34+ const events = new EventEmitter < Libp2pEvents > ( )
35+
3236 components = {
3337 peerId : await createPeerId ( ) ,
3438 connectionManager : stubInterface < ConnectionManager > ( ) ,
3539 peerStore : stubInterface < PeerStore > ( )
3640 }
3741 components . connectionManager = mockConnectionManager ( {
3842 ...components ,
39- registrar : stubInterface < Registrar > ( )
43+ registrar : stubInterface < Registrar > ( ) ,
44+ events
4045 } )
4146 components . peerStore = new PersistentPeerStore ( {
4247 ...components ,
43- datastore : new MemoryDatastore ( )
48+ datastore : new MemoryDatastore ( ) ,
49+ events
4450 } )
4551
4652 table = new RoutingTable ( components , {
@@ -229,7 +235,7 @@ describe('Routing Table', () => {
229235
230236 it ( 'tags newly found kad-close peers' , async ( ) => {
231237 const remotePeer = await createEd25519PeerId ( )
232- const tagPeerSpy = sinon . spy ( components . peerStore , 'tagPeer ' )
238+ const tagPeerSpy = sinon . spy ( components . peerStore , 'merge ' )
233239
234240 await table . add ( remotePeer )
235241
@@ -241,8 +247,11 @@ describe('Routing Table', () => {
241247
242248 expect ( tagPeerSpy . callCount ) . to . equal ( 1 , 'did not tag kad-close peer' )
243249 expect ( tagPeerSpy . getCall ( 0 ) . args [ 0 ] . toString ( ) ) . to . equal ( remotePeer . toString ( ) )
244- expect ( tagPeerSpy . getCall ( 0 ) . args [ 1 ] ) . to . equal ( KAD_CLOSE_TAG_NAME )
245- expect ( tagPeerSpy . getCall ( 0 ) . args [ 2 ] ) . to . have . property ( 'value' , KAD_CLOSE_TAG_VALUE )
250+ expect ( tagPeerSpy . getCall ( 0 ) . args [ 1 ] . tags ) . to . deep . equal ( {
251+ [ KAD_CLOSE_TAG_NAME ] : {
252+ value : KAD_CLOSE_TAG_VALUE
253+ }
254+ } )
246255 } )
247256
248257 it ( 'removes tags from kad-close peers when closer peers are found' , async ( ) => {
@@ -251,10 +260,9 @@ describe('Routing Table', () => {
251260 await components . peerStore . all ( ) ,
252261 async function * ( source ) {
253262 for await ( const peer of source ) {
254- const tags = await components . peerStore . getTags ( peer . id )
255- const kadCloseTags = tags . filter ( tag => tag . name === KAD_CLOSE_TAG_NAME )
263+ const peerData = await components . peerStore . get ( peer . id )
256264
257- if ( kadCloseTags . length > 0 ) {
265+ if ( peerData . tags . has ( KAD_CLOSE_TAG_NAME ) ) {
258266 yield peer . id
259267 }
260268 }
@@ -263,8 +271,7 @@ describe('Routing Table', () => {
263271 ) )
264272 }
265273
266- const tagPeerSpy = sinon . spy ( components . peerStore , 'tagPeer' )
267- const unTagPeerSpy = sinon . spy ( components . peerStore , 'unTagPeer' )
274+ const tagPeerSpy = sinon . spy ( components . peerStore , 'merge' )
268275 const localNodeId = await kadUtils . convertPeerId ( components . peerId )
269276 const sortedPeerList = await sortClosestPeers (
270277 await Promise . all (
@@ -305,7 +312,7 @@ describe('Routing Table', () => {
305312
306313 // wait for tag new peer and untag old peer
307314 await pWaitFor ( ( ) => {
308- return tagPeerSpy . callCount === 1 && unTagPeerSpy . callCount === 1
315+ return tagPeerSpy . callCount === 2
309316 } )
310317
311318 // should have updated list of tagged peers
0 commit comments