diff --git a/package.json b/package.json index 2e681b9..ca57e2a 100644 --- a/package.json +++ b/package.json @@ -141,7 +141,7 @@ "dependencies": { "@libp2p/interface-peer-discovery": "^1.0.1", "@libp2p/interface-peer-info": "^1.0.7", - "@libp2p/interface-peer-store": "^1.2.2", + "@libp2p/interface-peer-store": "^2.0.0", "@libp2p/interfaces": "^3.0.3", "@libp2p/logger": "^2.0.1", "@libp2p/peer-id": "^2.0.0", @@ -151,10 +151,7 @@ "devDependencies": { "@libp2p/interface-peer-discovery-compliance-tests": "^2.0.0", "@libp2p/interface-peer-id": "^2.0.0", - "@libp2p/peer-id-factory": "^2.0.0", - "@libp2p/peer-store": "^7.0.0", "aegir": "^38.1.7", - "datastore-core": "^9.0.3", - "delay": "^5.0.0" + "sinon-ts": "^1.0.0" } } diff --git a/src/index.ts b/src/index.ts index da1adba..5bf9857 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { multiaddr } from '@multiformats/multiaddr' import { P2P } from '@multiformats/mafmt' -import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' +import { EventEmitter } from '@libp2p/interfaces/events' import { logger } from '@libp2p/logger' import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' import type { PeerInfo } from '@libp2p/interface-peer-info' @@ -133,9 +133,13 @@ class Bootstrap extends EventEmitter implements PeerDiscove } for (const peerData of this.list) { - await this.components.peerStore.tagPeer(peerData.id, this._init.tagName ?? DEFAULT_BOOTSTRAP_TAG_NAME, { - value: this._init.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE, - ttl: this._init.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL + await this.components.peerStore.merge(peerData.id, { + tags: { + [this._init.tagName ?? DEFAULT_BOOTSTRAP_TAG_NAME]: { + value: this._init.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE, + ttl: this._init.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL + } + } }) // check we are still running @@ -143,7 +147,7 @@ class Bootstrap extends EventEmitter implements PeerDiscove return } - this.dispatchEvent(new CustomEvent('peer', { detail: peerData })) + this.safeDispatchEvent('peer', { detail: peerData }) } } diff --git a/test/bootstrap.spec.ts b/test/bootstrap.spec.ts index d67922d..c712f02 100644 --- a/test/bootstrap.spec.ts +++ b/test/bootstrap.spec.ts @@ -6,23 +6,21 @@ import { bootstrap, BootstrapComponents } from '../src/index.js' import peerList from './fixtures/default-peers.js' import partialValidPeerList from './fixtures/some-invalid-peers.js' import { isPeerId } from '@libp2p/interface-peer-id' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { MemoryDatastore } from 'datastore-core' import { multiaddr } from '@multiformats/multiaddr' import { peerIdFromString } from '@libp2p/peer-id' -import delay from 'delay' import { start, stop } from '@libp2p/interfaces/startable' -import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { StubbedInstance, stubInterface } from 'sinon-ts' +import type { PeerStore } from '@libp2p/interface-peer-store' describe('bootstrap', () => { let components: BootstrapComponents + let peerStore: StubbedInstance beforeEach(async () => { + peerStore = stubInterface() + components = { - peerStore: new PersistentPeerStore({ - peerId: await createEd25519PeerId(), - datastore: new MemoryDatastore() - }) + peerStore } }) @@ -83,17 +81,18 @@ describe('bootstrap', () => { const bootstrapper0PeerId = peerIdFromString(bootstrapper0PeerIdStr) - const tags = await components.peerStore.getTags(bootstrapper0PeerId) - - expect(tags).to.have.lengthOf(1, 'bootstrap tag was not set') - expect(tags).to.have.nested.property('[0].name', tagName, 'bootstrap tag had incorrect name') - expect(tags).to.have.nested.property('[0].value', tagValue, 'bootstrap tag had incorrect value') - - await delay(tagTTL * 2) - - const tags2 = await components.peerStore.getTags(bootstrapper0PeerId) - - expect(tags2).to.have.lengthOf(0, 'bootstrap tag did not expire') + expect(peerStore.merge).to.have.property('called', true) + + const call = peerStore.merge.getCall(0) + expect(call).to.have.deep.nested.property('args[0]', bootstrapper0PeerId) + expect(call).to.have.deep.nested.property('args[1]', { + tags: { + [tagName]: { + value: tagValue, + ttl: tagTTL + } + } + }) await stop(r) }) diff --git a/test/compliance.spec.ts b/test/compliance.spec.ts index 2d923a1..8e0534b 100644 --- a/test/compliance.spec.ts +++ b/test/compliance.spec.ts @@ -3,18 +3,14 @@ import tests from '@libp2p/interface-peer-discovery-compliance-tests' import { bootstrap } from '../src/index.js' import peerList from './fixtures/default-peers.js' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { MemoryDatastore } from 'datastore-core' -import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import type { PeerStore } from '@libp2p/interface-peer-store' +import { stubInterface } from 'sinon-ts' describe('compliance tests', () => { tests({ async setup () { const components = { - peerStore: new PersistentPeerStore({ - peerId: await createEd25519PeerId(), - datastore: new MemoryDatastore() - }) + peerStore: stubInterface() } return bootstrap({