Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
fix(sdk): always send note to NoteService for it to deal with duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Feb 2, 2020
1 parent 02a2255 commit 253ee3f
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import async from 'async';
import NoteService from '~/background/services/NoteService';
import {
subscription as NoteSubscription,
saveNotes,
Expand Down Expand Up @@ -210,6 +211,23 @@ class Watcher {
return;
}

const {
createNotes,
updateNotes,
destroyNotes,
} = groupedNotes;
const newNotes = [
...createNotes,
...updateNotes,
...destroyNotes,
].filter(({ owner }) => owner === address);

NoteService.addNotes(
networkId,
address,
newNotes,
);

this.saveQueue.push({
name: 'Save Notes',
groupedNotes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function createBulkNotes(notes, networkId) {
let created;
Expand All @@ -15,17 +13,5 @@ export default async function createBulkNotes(notes, networkId) {
return null;
}

if (created && created.length) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
notes.filter(({ owner }) => owner === address),
);
}

return created;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function createNote(note, networkId) {
let newNote;
Expand All @@ -14,19 +12,5 @@ export default async function createNote(note, networkId) {
return null;
}

if (newNote) {
const {
address,
} = Web3Service.account;

if (newNote.owner === address) {
NoteService.addNotes(
networkId,
address,
[note],
);
}
}

return newNote;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function updateBulkNotes(notes, networkId) {
const updatedNotes = [];
Expand All @@ -21,17 +19,5 @@ export default async function updateBulkNotes(notes, networkId) {
}),
);

if (updatedNotes.length) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
updatedNotes.filter(({ owner }) => owner === address),
);
}

return updatedNotes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function updateNote(note, networkId) {
let updated;
Expand All @@ -14,17 +12,5 @@ export default async function updateNote(note, networkId) {
return null;
}

if (updated) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
[note],
);
}

return updated;
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('Asset.decryptNotes', () => {
valueFromViewingKeySpy.mockClear();
});

it('will not process note that is already in storage', async () => {
it('will still process note that is already in storage but will not affect balance', async () => {
const valueFromViewingKeySpy = jest.spyOn(valueFromViewingKey, 'default');
let balance = 0;
let maxBlockNumber = 0;
Expand All @@ -275,6 +275,7 @@ describe('Asset.decryptNotes', () => {

expect(asset.balance).toBe(testNotes[duplicatedIndex].value);
expect(valueFromViewingKeySpy).toHaveBeenCalledTimes(1);
valueFromViewingKeySpy.mockClear();

await asset.decryptNotes(notes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default class Asset {
const destroyed = isDestroyed(status);
let key = await get(noteHash);

if (destroyed ^ !!key) { // eslint-disable-line no-bitwise
if (destroyed && !key) {
if (blockNumber > this.lastSynced) {
this.lastSynced = blockNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default async function createNoteKey(noteHash) {
await lock(
counterKey,
async () => {
const prevNoteKey = await get(noteHash);
if (prevNoteKey) {
key = prevNoteKey;
return;
}

const count = await get(counterKey) || 0;
key = dataKey('note', { count: count + 1 });
await set({
Expand Down

0 comments on commit 253ee3f

Please sign in to comment.