Skip to content

Commit

Permalink
fix status destructure error (#672)
Browse files Browse the repository at this point in the history
* fix status destructure error

* actually remove the index from the array

* changeset

* add tests
  • Loading branch information
Sladuca authored Dec 4, 2023
1 parent 9834400 commit 3281e2c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-parrots-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/client": patch
---

fix db not removing top-level index entry for op history
1 change: 1 addition & 0 deletions packages/client/src/NocturneDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
68 changes: 68 additions & 0 deletions packages/client/test/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(arr: T[]): WithTotalEntityIndex<T>[] {
Expand Down

0 comments on commit 3281e2c

Please sign in to comment.