From aaf4895fa94957ce03dacf2f11b5b92ec6c13524 Mon Sep 17 00:00:00 2001 From: Joel Torstensson Date: Thu, 1 Aug 2019 10:32:54 +0200 Subject: [PATCH 1/2] fix: refine rootstore sync logic --- package.json | 2 +- src/3box.js | 26 +++++++++++++++++++++++--- src/__tests__/3box.test.js | 11 +++++------ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index ee117012..791d71d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "3box", - "version": "1.10.4", + "version": "1.10.5-beta.2", "description": "Interact with user data", "main": "lib/3box.js", "directories": { diff --git a/src/3box.js b/src/3box.js index 359ef746..73e7807b 100644 --- a/src/3box.js +++ b/src/3box.js @@ -145,6 +145,26 @@ class Box { setTimeout(() => { spaceMessageFilterActive = false }, 3000) } if (data.type === 'HAS_ENTRIES') { + if (data.odbAddress === rootStoreAddress) { + if (data.numEntries === 0) { + await this._createRootStore(rootStoreAddress, privStoreAddress, pubStoreAddress) + this._boxSynced = true + this._onSyncDoneCB() + } else { + const numRemoteEntries = data.numEntries + const numEntriesDefined = !(numRemoteEntries === null || numRemoteEntries === undefined) + syncPromises.push(new Promise((resolve, reject) => { + if (numEntriesDefined && numRemoteEntries <= this._rootStore._oplog.values.length) resolve() + this._rootStore.events.on('replicated', () => { + if (numRemoteEntries <= this._rootStore._oplog.values.length) { + resolve() + this._rootStore.events.removeAllListeners('replicated') + this._rootStore.events.removeAllListeners('replicate.progress') + } + }) + })) + } + } if (data.odbAddress === privStoreAddress && !hasResponse[privStoreAddress]) { syncPromises.push(this.private._sync(data.numEntries)) hasResponse[privStoreAddress] = true @@ -156,10 +176,11 @@ class Box { if (spaceMessageFilterActive && data.odbAddress.includes('space') === true) { this.spacesPubSubMessages[data.odbAddress] = data } - if (syncPromises.length === 2) { + if (syncPromises.length === 3) { const promises = syncPromises syncPromises = [] await Promise.all(promises) + await this._createRootStore(rootStoreAddress, privStoreAddress, pubStoreAddress) this._boxSynced = true this._onSyncDoneCB() // this._pubsub.unsubscribe(PINNING_ROOM) @@ -169,11 +190,10 @@ class Box { this._pubsub.subscribe(PINNING_ROOM, onMessageRes, onNewPeer) - await this._createRootStore(rootStoreAddress, privStoreAddress, pubStoreAddress, this.pinningNode) + await this._rootStore.load() } async _createRootStore (rootStoreAddress, privOdbAddress, pubOdbAddress) { - await this._rootStore.load() const entries = await this._rootStore.iterator({ limit: -1 }).collect() if (!entries.find(e => e.payload.value.odbAddress === pubOdbAddress)) { await this._rootStore.add({ odbAddress: pubOdbAddress }) diff --git a/src/__tests__/3box.test.js b/src/__tests__/3box.test.js index 5f9e7221..e88c7e7c 100644 --- a/src/__tests__/3box.test.js +++ b/src/__tests__/3box.test.js @@ -228,10 +228,13 @@ describe('3Box', () => { const syncPromise = new Promise((resolve, reject) => { box.onSyncDone(resolve) }) pubsub.publish('3box-pinning', { type: 'HAS_ENTRIES', odbAddress: '/orbitdb/Qmasdf/08a7.public', numEntries: 4 }) pubsub.publish('3box-pinning', { type: 'HAS_ENTRIES', odbAddress: '/orbitdb/Qmfdsa/08a7.private', numEntries: 5 }) + const rootStoreAddress = box._rootStore.address.toString() + pubsub.publish('3box-pinning', { type: 'HAS_ENTRIES', odbAddress: rootStoreAddress, numEntries: 0 }) await syncPromise + await new Promise((resolve, reject) => { setTimeout(resolve, 500) }) expect(mockedUtils.fetchJson).toHaveBeenCalledTimes(1) expect(mockedUtils.fetchJson.mock.calls[0][0]).toEqual('address-server/odbAddress') - expect(didJWT.decodeJWT(mockedUtils.fetchJson.mock.calls[0][1].address_token).payload.rootStoreAddress).toEqual(box._rootStore.address.toString()) + expect(didJWT.decodeJWT(mockedUtils.fetchJson.mock.calls[0][1].address_token).payload.rootStoreAddress).toEqual(rootStoreAddress) pubsub.unsubscribe('3box-pinning') await box.close() @@ -496,8 +499,7 @@ describe('3Box', () => { expect(mocked3id.getIdFromEthAddress).toHaveBeenCalledWith('0xabcde', 'web3prov', boxOpts.ipfs, boxOpts) await box2._linkProfile() - expect(mockedUtils.fetchJson).toHaveBeenCalledTimes(2) - expect(mockedUtils.fetchJson).toHaveBeenNthCalledWith(2, 'address-server/link', { + expect(mockedUtils.fetchJson).toHaveBeenCalledWith('address-server/link', { message: `I agree to stuff,${didMuPort2}`, signature: "0xSuchRealSig,0xabcde", timestamp: 111, @@ -506,9 +508,6 @@ describe('3Box', () => { }) expect(mockedUtils.getLinkConsent).toHaveBeenCalledTimes(1) await publishPromise - expect(mockedUtils.fetchJson).toHaveBeenCalledTimes(2) - expect(mockedUtils.fetchJson.mock.calls[0][0]).toEqual('address-server/odbAddress') - expect(didJWT.decodeJWT(mockedUtils.fetchJson.mock.calls[0][1].address_token).payload.rootStoreAddress).toEqual(box2._rootStore.address.toString()) pubsub.unsubscribe('3box-pinning') box2.close() }) From 41f356b54e7572bc02f948d040a33c880c5ce75d Mon Sep 17 00:00:00 2001 From: Joel Torstensson Date: Fri, 2 Aug 2019 14:24:08 +0200 Subject: [PATCH 2/2] Release v1.10.5 --- RELEASE-NOTES.md | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index a6b0a150..97cd86a2 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,5 +1,8 @@ # Release Notes +## v1.10.5 - 2019-08-02 +* fix: made onSyncDone logic more robust + ## v1.10.4 - 2019-07-31 * fix: solved issue with joining multiple threads diff --git a/package.json b/package.json index 791d71d8..bc594d73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "3box", - "version": "1.10.5-beta.2", + "version": "1.10.5", "description": "Interact with user data", "main": "lib/3box.js", "directories": {