mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: update interface datastore (#663)
* chore: update interface datastore Updates to v1.x.x to not have multiple versions of this module in the ipfs browser bundle. * fix: let batch commits complete before continuing tests Batch commits are async but the tests weren't waiting for them to complete, mainly because they are triggered by events. There's no way that I can see of waiting for the batch commit to finish so I've added delays to the tests. Not great but a start. * chore: use error log * test: wait for commit spies to complete * chore: bump interface-datastore Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
parent
1e51295150
commit
d60a3215d0
@ -50,7 +50,7 @@
|
|||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"events": "^3.1.0",
|
"events": "^3.1.0",
|
||||||
"hashlru": "^2.3.0",
|
"hashlru": "^2.3.0",
|
||||||
"interface-datastore": "^0.8.3",
|
"interface-datastore": "^1.0.4",
|
||||||
"ipfs-utils": "^2.2.0",
|
"ipfs-utils": "^2.2.0",
|
||||||
"it-all": "^1.0.1",
|
"it-all": "^1.0.1",
|
||||||
"it-buffer": "^0.1.2",
|
"it-buffer": "^0.1.2",
|
||||||
|
@ -96,7 +96,9 @@ class PersistentPeerStore extends PeerStore {
|
|||||||
|
|
||||||
if (this._dirtyPeers.size >= this.threshold) {
|
if (this._dirtyPeers.size >= this.threshold) {
|
||||||
// Commit current data
|
// 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) {
|
if (this._dirtyPeers.size >= this.threshold) {
|
||||||
// Commit current data
|
// Commit current data
|
||||||
this._commitData()
|
this._commitData().catch(err => {
|
||||||
|
log.error('error committing data', err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ const chai = require('chai')
|
|||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
|
|
||||||
const PeerStore = require('../../src/peer-store/persistent')
|
const PeerStore = require('../../src/peer-store/persistent')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const { MemoryDatastore } = require('interface-datastore')
|
const { MemoryDatastore } = require('interface-datastore')
|
||||||
@ -62,6 +61,7 @@ describe('Persisted PeerStore', () => {
|
|||||||
const protocols = ['/ping/1.0.0']
|
const protocols = ['/ping/1.0.0']
|
||||||
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
|
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
|
||||||
const spyDs = sinon.spy(datastore, 'batch')
|
const spyDs = sinon.spy(datastore, 'batch')
|
||||||
|
const commitSpy = sinon.spy(peerStore, '_commitData')
|
||||||
|
|
||||||
await peerStore.start()
|
await peerStore.start()
|
||||||
|
|
||||||
@ -71,12 +71,18 @@ describe('Persisted PeerStore', () => {
|
|||||||
expect(spyDirty).to.have.property('callCount', 1) // Address
|
expect(spyDirty).to.have.property('callCount', 1) // Address
|
||||||
expect(spyDs).to.have.property('callCount', 1)
|
expect(spyDs).to.have.property('callCount', 1)
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// ProtoBook
|
// ProtoBook
|
||||||
peerStore.protoBook.set(peer, protocols)
|
peerStore.protoBook.set(peer, protocols)
|
||||||
|
|
||||||
expect(spyDirty).to.have.property('callCount', 2) // Protocol
|
expect(spyDirty).to.have.property('callCount', 2) // Protocol
|
||||||
expect(spyDs).to.have.property('callCount', 2)
|
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
|
// Should have three peer records stored in the datastore
|
||||||
const queryParams = {
|
const queryParams = {
|
||||||
prefix: '/peers/'
|
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 () => {
|
it('should load content to the peerStore when restart but not put in datastore again', async () => {
|
||||||
const spyDs = sinon.spy(datastore, 'batch')
|
const spyDs = sinon.spy(datastore, 'batch')
|
||||||
const peers = await peerUtils.createPeerId({ number: 2 })
|
const peers = await peerUtils.createPeerId({ number: 2 })
|
||||||
|
const commitSpy = sinon.spy(peerStore, '_commitData')
|
||||||
const multiaddrs = [
|
const multiaddrs = [
|
||||||
multiaddr('/ip4/156.10.1.22/tcp/1000'),
|
multiaddr('/ip4/156.10.1.22/tcp/1000'),
|
||||||
multiaddr('/ip4/156.10.1.23/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[0], [multiaddrs[0]])
|
||||||
peerStore.addressBook.set(peers[1], [multiaddrs[1]])
|
peerStore.addressBook.set(peers[1], [multiaddrs[1]])
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// KeyBook
|
// KeyBook
|
||||||
peerStore.keyBook.set(peers[0], peers[0].pubKey)
|
peerStore.keyBook.set(peers[0], peers[0].pubKey)
|
||||||
peerStore.keyBook.set(peers[1], peers[1].pubKey)
|
peerStore.keyBook.set(peers[1], peers[1].pubKey)
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// ProtoBook
|
// ProtoBook
|
||||||
peerStore.protoBook.set(peers[0], protocols)
|
peerStore.protoBook.set(peers[0], protocols)
|
||||||
peerStore.protoBook.set(peers[1], protocols)
|
peerStore.protoBook.set(peers[1], protocols)
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// MetadataBook
|
// MetadataBook
|
||||||
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
|
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(spyDs).to.have.property('callCount', 7) // 2 Address + 2 Key + 2 Proto + 1 Metadata
|
||||||
expect(peerStore.peers.size).to.equal(2)
|
expect(peerStore.peers.size).to.equal(2)
|
||||||
|
|
||||||
@ -148,6 +167,7 @@ describe('Persisted PeerStore', () => {
|
|||||||
const [peer] = await peerUtils.createPeerId()
|
const [peer] = await peerUtils.createPeerId()
|
||||||
const multiaddrs = [multiaddr('/ip4/156.10.1.22/tcp/1000')]
|
const multiaddrs = [multiaddr('/ip4/156.10.1.22/tcp/1000')]
|
||||||
const protocols = ['/ping/1.0.0']
|
const protocols = ['/ping/1.0.0']
|
||||||
|
const commitSpy = sinon.spy(peerStore, '_commitData')
|
||||||
|
|
||||||
await peerStore.start()
|
await peerStore.start()
|
||||||
|
|
||||||
@ -158,6 +178,9 @@ describe('Persisted PeerStore', () => {
|
|||||||
// MetadataBook
|
// MetadataBook
|
||||||
peerStore.metadataBook.set(peer, 'location', Buffer.from('earth'))
|
peerStore.metadataBook.set(peer, 'location', Buffer.from('earth'))
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
const spyDs = sinon.spy(datastore, 'batch')
|
const spyDs = sinon.spy(datastore, 'batch')
|
||||||
const spyAddressBook = sinon.spy(peerStore.addressBook, 'delete')
|
const spyAddressBook = sinon.spy(peerStore.addressBook, 'delete')
|
||||||
const spyKeyBook = sinon.spy(peerStore.keyBook, 'delete')
|
const spyKeyBook = sinon.spy(peerStore.keyBook, 'delete')
|
||||||
@ -166,6 +189,10 @@ describe('Persisted PeerStore', () => {
|
|||||||
|
|
||||||
// Delete from PeerStore
|
// Delete from PeerStore
|
||||||
peerStore.delete(peer)
|
peerStore.delete(peer)
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
await peerStore.stop()
|
await peerStore.stop()
|
||||||
|
|
||||||
expect(spyAddressBook).to.have.property('callCount', 1)
|
expect(spyAddressBook).to.have.property('callCount', 1)
|
||||||
@ -197,6 +224,7 @@ describe('Persisted PeerStore', () => {
|
|||||||
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
|
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
|
||||||
const spyDirtyMetadata = sinon.spy(peerStore, '_addDirtyPeerMetadata')
|
const spyDirtyMetadata = sinon.spy(peerStore, '_addDirtyPeerMetadata')
|
||||||
const spyDs = sinon.spy(datastore, 'batch')
|
const spyDs = sinon.spy(datastore, 'batch')
|
||||||
|
const commitSpy = sinon.spy(peerStore, '_commitData')
|
||||||
|
|
||||||
const peers = await peerUtils.createPeerId({ number: 2 })
|
const peers = await peerUtils.createPeerId({ number: 2 })
|
||||||
|
|
||||||
@ -213,9 +241,15 @@ describe('Persisted PeerStore', () => {
|
|||||||
peerStore.protoBook.set(peers[0], protocols)
|
peerStore.protoBook.set(peers[0], protocols)
|
||||||
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
|
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// Remove data from the same Peer
|
// Remove data from the same Peer
|
||||||
peerStore.addressBook.delete(peers[0])
|
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(spyDirty).to.have.property('callCount', 3) // 2 AddrBook ops, 1 ProtoBook op
|
||||||
expect(spyDirtyMetadata).to.have.property('callCount', 1) // 1 MetadataBook op
|
expect(spyDirtyMetadata).to.have.property('callCount', 1) // 1 MetadataBook op
|
||||||
expect(peerStore._dirtyPeers.size).to.equal(1)
|
expect(peerStore._dirtyPeers.size).to.equal(1)
|
||||||
@ -231,6 +265,9 @@ describe('Persisted PeerStore', () => {
|
|||||||
// Add data for second book
|
// Add data for second book
|
||||||
peerStore.addressBook.set(peers[1], multiaddrs)
|
peerStore.addressBook.set(peers[1], multiaddrs)
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
expect(spyDirty).to.have.property('callCount', 4)
|
expect(spyDirty).to.have.property('callCount', 4)
|
||||||
expect(spyDirtyMetadata).to.have.property('callCount', 1)
|
expect(spyDirtyMetadata).to.have.property('callCount', 1)
|
||||||
expect(spyDs).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 () => {
|
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 peers = await peerUtils.createPeerId({ number: 2 })
|
||||||
const multiaddrs = [
|
const multiaddrs = [
|
||||||
multiaddr('/ip4/156.10.1.22/tcp/1000'),
|
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[0], [multiaddrs[0]])
|
||||||
libp2p.peerStore.addressBook.set(peers[1], [multiaddrs[1]])
|
libp2p.peerStore.addressBook.set(peers[1], [multiaddrs[1]])
|
||||||
|
|
||||||
|
// let batch commit complete
|
||||||
|
await Promise.all(commitSpy.returnValues)
|
||||||
|
|
||||||
// ProtoBook
|
// ProtoBook
|
||||||
libp2p.peerStore.protoBook.set(peers[0], protocols)
|
libp2p.peerStore.protoBook.set(peers[0], protocols)
|
||||||
libp2p.peerStore.protoBook.set(peers[1], 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)
|
expect(libp2p.peerStore.peers.size).to.equal(2)
|
||||||
|
|
||||||
await libp2p.stop()
|
await libp2p.stop()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user