From 3281e2cd9b4ab2b43f00a927999b2fdcf7973d42 Mon Sep 17 00:00:00 2001 From: Sebastien La Duca Date: Mon, 4 Dec 2023 12:02:01 -0500 Subject: [PATCH] fix status destructure error (#672) * fix status destructure error * actually remove the index from the array * changeset * add tests --- .changeset/strange-parrots-study.md | 5 +++ packages/client/src/NocturneDB.ts | 1 + packages/client/test/db.test.ts | 68 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .changeset/strange-parrots-study.md diff --git a/.changeset/strange-parrots-study.md b/.changeset/strange-parrots-study.md new file mode 100644 index 000000000..219b26601 --- /dev/null +++ b/.changeset/strange-parrots-study.md @@ -0,0 +1,5 @@ +--- +"@nocturne-xyz/client": patch +--- + +fix db not removing top-level index entry for op history diff --git a/packages/client/src/NocturneDB.ts b/packages/client/src/NocturneDB.ts index 7ad1a92c1..b9ebbca9f 100644 --- a/packages/client/src/NocturneDB.ts +++ b/packages/client/src/NocturneDB.ts @@ -260,6 +260,7 @@ export class NocturneDB { const index = history.indexOf(digest); if (index !== -1) { + history.splice(index, 1); await this.setHistoryArray(history); } else { console.warn("tried to remove op from history that was not in history"); diff --git a/packages/client/test/db.test.ts b/packages/client/test/db.test.ts index 91c6e06b3..56470c022 100644 --- a/packages/client/test/db.test.ts +++ b/packages/client/test/db.test.ts @@ -344,6 +344,74 @@ describe("NocturneDB", async () => { const allNotes = await db.getAllNotes({ includeUncommitted: true }); expect(allNotes.size).to.eql(0); }); + + it("removes ops from history", async () => { + const op1 = dummyOp(5, shitcoin); + const op2 = dummyOp(5, shitcoin); + await db.addOpToHistory(op1, { items: [] }); + await db.addOpToHistory(op2, { items: [] }); + + await db.removeOpFromHistory(OperationTrait.computeDigest(op1)); + + const history = await db.getHistory(true); + expect(history.length).to.eql(1); + expect(history[0].digest).to.eql(OperationTrait.computeDigest(op2)); + }); + + it("sets op status in history", async () => { + const op = dummyOp(5, shitcoin); + await db.addOpToHistory(op, { items: [] }); + + { + const history = await db.getHistory(true); + expect(history.length).to.eql(1); + expect(history[0].status).to.be.undefined; + } + + await db.setStatusForOp( + OperationTrait.computeDigest(op), + OperationStatus.EXECUTED_SUCCESS + ); + + { + const history = await db.getHistory(true); + expect(history.length).to.eql(1); + expect(history[0].status).to.eql(OperationStatus.EXECUTED_SUCCESS); + } + }); + + it("removes optimistic nullifiers when op marked as failed in history", async () => { + // add an op with 5 joinsplits to history + const op = dummyOp(5, shitcoin); + const { oldNotes } = getNotesAndNfsFromOp(op); + await db.storeNotes(withDummyTotalEntityIndices(oldNotes)); + + await db.addOpToHistory(op, { items: [] }); + // expect there to be optimistic NFs in db + { + const optimisticNfs = await db.getAllOptimisticNFRecords(); + expect(optimisticNfs.size).to.eql(oldNotes.length); + + const allNotes = await db.getAllNotes({ includeUncommitted: true }); + const notes = [...allNotes.values()].flat(); + expect(notes.length).to.eql(0); + } + + await db.setStatusForOp( + OperationTrait.computeDigest(op), + OperationStatus.BUNDLE_REVERTED + ); + + // expect optimistic NFs to be removed + { + const optimisticNfs = await db.getAllOptimisticNFRecords(); + expect(optimisticNfs.size).to.eql(0); + + const allNotes = await db.getAllNotes({ includeUncommitted: true }); + const notes = [...allNotes.values()].flat(); + expect(notes.length).to.eql(oldNotes.length); + } + }); }); function withDummyTotalEntityIndices(arr: T[]): WithTotalEntityIndex[] {