diff --git a/package.json b/package.json index 09ad2741..08875339 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "err-code": "^2.0.0", "events": "^3.1.0", "hashlru": "^2.3.0", - "interface-datastore": "^0.8.3", + "interface-datastore": "^1.0.4", "ipfs-utils": "^2.2.0", "it-all": "^1.0.1", "it-buffer": "^0.1.2", diff --git a/src/peer-store/persistent/index.js b/src/peer-store/persistent/index.js index 2bcd38db..0cf54c0d 100644 --- a/src/peer-store/persistent/index.js +++ b/src/peer-store/persistent/index.js @@ -96,7 +96,9 @@ class PersistentPeerStore extends PeerStore { if (this._dirtyPeers.size >= this.threshold) { // Commit current data - this._commitData() + this._commitData().catch(err => { + log.error('error committing data', err) + }) } } @@ -120,7 +122,9 @@ class PersistentPeerStore extends PeerStore { if (this._dirtyPeers.size >= this.threshold) { // Commit current data - this._commitData() + this._commitData().catch(err => { + log.error('error committing data', err) + }) } } diff --git a/test/peer-store/persisted-peer-store.spec.js b/test/peer-store/persisted-peer-store.spec.js index d786f4fd..23f68bc3 100644 --- a/test/peer-store/persisted-peer-store.spec.js +++ b/test/peer-store/persisted-peer-store.spec.js @@ -5,7 +5,6 @@ const chai = require('chai') chai.use(require('dirty-chai')) const { expect } = chai const sinon = require('sinon') - const PeerStore = require('../../src/peer-store/persistent') const multiaddr = require('multiaddr') const { MemoryDatastore } = require('interface-datastore') @@ -62,6 +61,7 @@ describe('Persisted PeerStore', () => { const protocols = ['/ping/1.0.0'] const spyDirty = sinon.spy(peerStore, '_addDirtyPeer') const spyDs = sinon.spy(datastore, 'batch') + const commitSpy = sinon.spy(peerStore, '_commitData') await peerStore.start() @@ -71,12 +71,18 @@ describe('Persisted PeerStore', () => { expect(spyDirty).to.have.property('callCount', 1) // Address expect(spyDs).to.have.property('callCount', 1) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // ProtoBook peerStore.protoBook.set(peer, protocols) expect(spyDirty).to.have.property('callCount', 2) // Protocol expect(spyDs).to.have.property('callCount', 2) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // Should have three peer records stored in the datastore const queryParams = { prefix: '/peers/' @@ -98,6 +104,7 @@ describe('Persisted PeerStore', () => { it('should load content to the peerStore when restart but not put in datastore again', async () => { const spyDs = sinon.spy(datastore, 'batch') const peers = await peerUtils.createPeerId({ number: 2 }) + const commitSpy = sinon.spy(peerStore, '_commitData') const multiaddrs = [ multiaddr('/ip4/156.10.1.22/tcp/1000'), multiaddr('/ip4/156.10.1.23/tcp/1000') @@ -110,17 +117,29 @@ describe('Persisted PeerStore', () => { peerStore.addressBook.set(peers[0], [multiaddrs[0]]) peerStore.addressBook.set(peers[1], [multiaddrs[1]]) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // KeyBook peerStore.keyBook.set(peers[0], peers[0].pubKey) peerStore.keyBook.set(peers[1], peers[1].pubKey) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // ProtoBook peerStore.protoBook.set(peers[0], protocols) peerStore.protoBook.set(peers[1], protocols) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // MetadataBook peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth')) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + expect(spyDs).to.have.property('callCount', 7) // 2 Address + 2 Key + 2 Proto + 1 Metadata expect(peerStore.peers.size).to.equal(2) @@ -148,6 +167,7 @@ describe('Persisted PeerStore', () => { const [peer] = await peerUtils.createPeerId() const multiaddrs = [multiaddr('/ip4/156.10.1.22/tcp/1000')] const protocols = ['/ping/1.0.0'] + const commitSpy = sinon.spy(peerStore, '_commitData') await peerStore.start() @@ -158,6 +178,9 @@ describe('Persisted PeerStore', () => { // MetadataBook peerStore.metadataBook.set(peer, 'location', Buffer.from('earth')) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + const spyDs = sinon.spy(datastore, 'batch') const spyAddressBook = sinon.spy(peerStore.addressBook, 'delete') const spyKeyBook = sinon.spy(peerStore.keyBook, 'delete') @@ -166,6 +189,10 @@ describe('Persisted PeerStore', () => { // Delete from PeerStore peerStore.delete(peer) + + // let batch commit complete + await Promise.all(commitSpy.returnValues) + await peerStore.stop() expect(spyAddressBook).to.have.property('callCount', 1) @@ -197,6 +224,7 @@ describe('Persisted PeerStore', () => { const spyDirty = sinon.spy(peerStore, '_addDirtyPeer') const spyDirtyMetadata = sinon.spy(peerStore, '_addDirtyPeerMetadata') const spyDs = sinon.spy(datastore, 'batch') + const commitSpy = sinon.spy(peerStore, '_commitData') const peers = await peerUtils.createPeerId({ number: 2 }) @@ -213,9 +241,15 @@ describe('Persisted PeerStore', () => { peerStore.protoBook.set(peers[0], protocols) peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth')) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // Remove data from the same Peer peerStore.addressBook.delete(peers[0]) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + expect(spyDirty).to.have.property('callCount', 3) // 2 AddrBook ops, 1 ProtoBook op expect(spyDirtyMetadata).to.have.property('callCount', 1) // 1 MetadataBook op expect(peerStore._dirtyPeers.size).to.equal(1) @@ -231,6 +265,9 @@ describe('Persisted PeerStore', () => { // Add data for second book peerStore.addressBook.set(peers[1], multiaddrs) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + expect(spyDirty).to.have.property('callCount', 4) expect(spyDirtyMetadata).to.have.property('callCount', 1) expect(spyDs).to.have.property('callCount', 1) @@ -333,6 +370,7 @@ describe('libp2p.peerStore (Persisted)', () => { }) it('should load content to the peerStore when a new node is started with the same datastore', async () => { + const commitSpy = sinon.spy(libp2p.peerStore, '_commitData') const peers = await peerUtils.createPeerId({ number: 2 }) const multiaddrs = [ multiaddr('/ip4/156.10.1.22/tcp/1000'), @@ -346,10 +384,16 @@ describe('libp2p.peerStore (Persisted)', () => { libp2p.peerStore.addressBook.set(peers[0], [multiaddrs[0]]) libp2p.peerStore.addressBook.set(peers[1], [multiaddrs[1]]) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + // ProtoBook libp2p.peerStore.protoBook.set(peers[0], protocols) libp2p.peerStore.protoBook.set(peers[1], protocols) + // let batch commit complete + await Promise.all(commitSpy.returnValues) + expect(libp2p.peerStore.peers.size).to.equal(2) await libp2p.stop()