Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
✅ Add unit tests for saving inclusion proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Feb 29, 2024
1 parent 3b1c394 commit ba3e803
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion elements/lisk-chain/src/data_access/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export class Storage {
}

if (this._keepInclusionProofsForHeights > -1) {
// events are removed only if finalized and below height - keepEventsForHeights
// inclusion proofs are removed only if finalized and below height - keepInclusionProofsForHeights
const minInclusionProofDeleteHeight = Math.min(
finalizedHeight,
Math.max(0, currentHeight - this._keepInclusionProofsForHeights),
Expand Down
41 changes: 41 additions & 0 deletions framework/test/unit/engine/consensus/consensus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('consensus', () => {
precommitThreshold: 0,
certificateThreshold: 0,
}),
prove: jest.fn(),
} as never;
consensus = new Consensus({
abi,
Expand Down Expand Up @@ -645,6 +646,46 @@ describe('consensus', () => {
});
});

it('should save inclusion proofs for the given keys', async () => {
jest.spyOn(abi, 'prove').mockResolvedValue({
proof: {
queries: [
{
bitmap: Buffer.alloc(2),
key: Buffer.alloc(2),
value: Buffer.alloc(2),
},
],
siblingHashes: [Buffer.alloc(2)],
},
});
(consensus as any)['_inclusionProofKeys'] = [Buffer.alloc(32)];
consensus['_systemConfig'].keepInclusionProofsForHeights = 300;

await consensus['_execute'](block, '127.0.0.1:7667');

expect(abi.prove).toHaveBeenCalledWith({
keys: [Buffer.alloc(32)],
stateRoot: block.header.stateRoot,
});
});

it('should throw error when saving of inclusions fail', async () => {
jest.spyOn(abi, 'prove').mockRejectedValue(new Error('Failed to save inclusion'));
jest.spyOn(loggerMock, 'error');
(consensus as any)['_inclusionProofKeys'] = [Buffer.alloc(32)];
consensus['_systemConfig'].keepInclusionProofsForHeights = 300;

await consensus['_execute'](block, '127.0.0.1:7667');

expect(loggerMock.error).toHaveBeenCalledWith(
{
err: new Error('Failed to save inclusion'),
},
'Failed to save inclusion proof for the given keys.',
);
});

it('should emit CONSENSUS_EVENT_BLOCK_BROADCAST event', async () => {
jest.spyOn(consensus.events, 'emit');

Expand Down

0 comments on commit ba3e803

Please sign in to comment.