From ba3e803e2f451825bba30ed6ed0a8194ab68502d Mon Sep 17 00:00:00 2001 From: Ishan Date: Thu, 29 Feb 2024 20:23:47 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20unit=20tests=20for=20saving?= =?UTF-8?q?=20inclusion=20proofs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lisk-chain/src/data_access/storage.ts | 2 +- .../unit/engine/consensus/consensus.spec.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/elements/lisk-chain/src/data_access/storage.ts b/elements/lisk-chain/src/data_access/storage.ts index 9198907a01..5658605c18 100644 --- a/elements/lisk-chain/src/data_access/storage.ts +++ b/elements/lisk-chain/src/data_access/storage.ts @@ -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), diff --git a/framework/test/unit/engine/consensus/consensus.spec.ts b/framework/test/unit/engine/consensus/consensus.spec.ts index 52cec052a8..081b6d24b5 100644 --- a/framework/test/unit/engine/consensus/consensus.spec.ts +++ b/framework/test/unit/engine/consensus/consensus.spec.ts @@ -120,6 +120,7 @@ describe('consensus', () => { precommitThreshold: 0, certificateThreshold: 0, }), + prove: jest.fn(), } as never; consensus = new Consensus({ abi, @@ -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');