Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #547 from 3box/hotfix/v1.10.5
Browse files Browse the repository at this point in the history
Hotfix/v1.10.5
  • Loading branch information
oed authored Aug 2, 2019
2 parents 5f55b31 + 41f356b commit 8798c91
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3box",
"version": "1.10.4",
"version": "1.10.5",
"description": "Interact with user data",
"main": "lib/3box.js",
"directories": {
Expand Down
26 changes: 23 additions & 3 deletions src/3box.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 })
Expand Down
11 changes: 5 additions & 6 deletions src/__tests__/3box.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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()
})
Expand Down

0 comments on commit 8798c91

Please sign in to comment.