From 8e5c944928d16cd5b69a4e772c6640d6623351dc Mon Sep 17 00:00:00 2001 From: Ishan Date: Mon, 12 Feb 2024 19:46:01 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20get/set=20inclu?= =?UTF-8?q?sion=20proofs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/data_access/blocks.spec.ts | 1 + .../data_access/transactions.spec.ts | 1 + elements/lisk-chain/test/unit/chain.spec.ts | 1 + .../test/unit/data_access/data_access.spec.ts | 57 ++++++++++++++++++- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/elements/lisk-chain/test/integration/data_access/blocks.spec.ts b/elements/lisk-chain/test/integration/data_access/blocks.spec.ts index 779bf64b6c0..f7c59855a7b 100644 --- a/elements/lisk-chain/test/integration/data_access/blocks.spec.ts +++ b/elements/lisk-chain/test/integration/data_access/blocks.spec.ts @@ -63,6 +63,7 @@ describe('dataAccess.blocks', () => { minBlockHeaderCache: 3, maxBlockHeaderCache: 5, keepEventsForHeights: -1, + keepInclusionProofsForHeights: -1, }); // Prepare sample data const block300 = await createValidDefaultBlock({ diff --git a/elements/lisk-chain/test/integration/data_access/transactions.spec.ts b/elements/lisk-chain/test/integration/data_access/transactions.spec.ts index 2a702359a94..59e047ec63a 100644 --- a/elements/lisk-chain/test/integration/data_access/transactions.spec.ts +++ b/elements/lisk-chain/test/integration/data_access/transactions.spec.ts @@ -34,6 +34,7 @@ describe('dataAccess.transactions', () => { minBlockHeaderCache: 3, maxBlockHeaderCache: 5, keepEventsForHeights: -1, + keepInclusionProofsForHeights: -1, }); }); diff --git a/elements/lisk-chain/test/unit/chain.spec.ts b/elements/lisk-chain/test/unit/chain.spec.ts index d86980c6ff5..3f40c5f2ea1 100644 --- a/elements/lisk-chain/test/unit/chain.spec.ts +++ b/elements/lisk-chain/test/unit/chain.spec.ts @@ -38,6 +38,7 @@ describe('chain', () => { const constants = { maxTransactionsSize: 15 * 1024, keepEventsForHeights: 300, + keepInclusionProofsForHeights: 300, }; const emptyEncodedDiff = codec.encode(stateDiffSchema, { created: [], diff --git a/elements/lisk-chain/test/unit/data_access/data_access.spec.ts b/elements/lisk-chain/test/unit/data_access/data_access.spec.ts index a8592f51444..a7f912ed985 100644 --- a/elements/lisk-chain/test/unit/data_access/data_access.spec.ts +++ b/elements/lisk-chain/test/unit/data_access/data_access.spec.ts @@ -13,7 +13,8 @@ */ import { Readable } from 'stream'; import { when } from 'jest-when'; -import { NotFoundError, InMemoryDatabase } from '@liskhq/lisk-db'; +import { NotFoundError, InMemoryDatabase, Proof } from '@liskhq/lisk-db'; +import { codec } from '@liskhq/lisk-codec'; import { utils } from '@liskhq/lisk-cryptography'; import { DataAccess } from '../../../src/data_access'; import { createFakeBlockHeader, createValidDefaultBlock } from '../../utils/block'; @@ -24,11 +25,13 @@ import { DB_KEY_BLOCKS_ID, DB_KEY_TRANSACTIONS_ID, DB_KEY_BLOCK_EVENTS, + DB_KEY_INCLUSION_PROOFS, } from '../../../src/db_keys'; import { Block } from '../../../src/block'; import { Event } from '../../../src/event'; import { BlockAssets, BlockHeader } from '../../../src'; import { encodeByteArray } from '../../../src/data_access/storage'; +import { inclusionProofSchema } from '../../../src/schema'; jest.mock('@liskhq/lisk-db'); @@ -45,6 +48,7 @@ describe('data_access', () => { minBlockHeaderCache: 3, maxBlockHeaderCache: 5, keepEventsForHeights: 1, + keepInclusionProofsForHeights: 1, }); block = await createValidDefaultBlock({ header: { height: 1 } }); }); @@ -400,6 +404,57 @@ describe('data_access', () => { }); }); + describe('#getInclusionProofs', () => { + it('should get empty array if the inclusionProofs does not exist', async () => { + db.get.mockRejectedValue(new NotFoundError()); + + const resp = await dataAccess.getInclusionProofs(30); + expect(db.get).toHaveBeenCalledWith(concatDBKeys(DB_KEY_INCLUSION_PROOFS, uint32BE(30))); + expect(resp).toEqual({ + siblingHashes: [], + queries: [], + }); + }); + + it('should get the inclusion proofs related to heights', async () => { + const original = { + siblingHashes: [Buffer.alloc(3)], + queries: [ + { + key: Buffer.alloc(2), + value: Buffer.alloc(2), + bitmap: Buffer.alloc(1), + }, + ], + }; + db.get.mockResolvedValue(codec.encode(inclusionProofSchema, original) as never); + + const resp = await dataAccess.getInclusionProofs(30); + expect(db.get).toHaveBeenCalledWith(concatDBKeys(DB_KEY_INCLUSION_PROOFS, uint32BE(30))); + expect(resp).toEqual(original); + }); + }); + + describe('#setInclusionProofs', () => { + it('should set inclusionProofs for a given height', async () => { + const proofs: Proof = { + siblingHashes: [Buffer.alloc(3)], + queries: [ + { + key: Buffer.alloc(2), + value: Buffer.alloc(2), + bitmap: Buffer.alloc(1), + }, + ], + }; + await dataAccess.setInclusionProofs(proofs, 30); + expect(db.set).toHaveBeenCalledWith( + concatDBKeys(DB_KEY_INCLUSION_PROOFS, uint32BE(30)), + codec.encode(inclusionProofSchema, proofs), + ); + }); + }); + describe('#isBlockPersisted', () => { it('should call check if the id exists in the database', async () => { // Act