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

Commit

Permalink
✅ Add tests for get/set inclusion proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Feb 12, 2024
1 parent 963b22d commit 8e5c944
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('dataAccess.blocks', () => {
minBlockHeaderCache: 3,
maxBlockHeaderCache: 5,
keepEventsForHeights: -1,
keepInclusionProofsForHeights: -1,
});
// Prepare sample data
const block300 = await createValidDefaultBlock({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('dataAccess.transactions', () => {
minBlockHeaderCache: 3,
maxBlockHeaderCache: 5,
keepEventsForHeights: -1,
keepInclusionProofsForHeights: -1,
});
});

Expand Down
1 change: 1 addition & 0 deletions elements/lisk-chain/test/unit/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('chain', () => {
const constants = {
maxTransactionsSize: 15 * 1024,
keepEventsForHeights: 300,
keepInclusionProofsForHeights: 300,
};
const emptyEncodedDiff = codec.encode(stateDiffSchema, {
created: [],
Expand Down
57 changes: 56 additions & 1 deletion elements/lisk-chain/test/unit/data_access/data_access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand All @@ -45,6 +48,7 @@ describe('data_access', () => {
minBlockHeaderCache: 3,
maxBlockHeaderCache: 5,
keepEventsForHeights: 1,
keepInclusionProofsForHeights: 1,
});
block = await createValidDefaultBlock({ header: { height: 1 } });
});
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8e5c944

Please sign in to comment.