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

Commit

Permalink
✅ Fix and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Oct 6, 2023
1 parent e70c483 commit ac57123
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 20 deletions.
2 changes: 1 addition & 1 deletion framework/src/engine/legacy/legacy_chain_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class LegacyChainHandler {
* If last block height equals bracket.startHeight, simply save bracket with `lastBlockHeight: lastBlock?.header.height`
*/
// eslint-disable-next-line @typescript-eslint/member-ordering
public async _syncBlocks(
private async _syncBlocks(
bracket: LegacyBlockBracket,
lastBlockID: Buffer,
failedAttempts = 0,
Expand Down
106 changes: 87 additions & 19 deletions framework/test/unit/engine/legacy/legacy_chain_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,6 @@ describe('Legacy Chain Handler', () => {
jest
.spyOn(legacyChainHandler['_storage'], 'getBlockByHeight')
.mockReturnValueOnce(encodeBlock(legacyBlock16270316) as any); // we want to return blocks from this height ONCE

// `getLegacyBlocksFromId` should return blocks in DESC order (starting from 16270316 (excluding) till 16270306)
const reversedFixtures = blockFixtures
.slice(0, blockFixtures.length - 1)
.sort((a, b) => b.header.height - a.header.height);
const encodedBlocks = reversedFixtures.map(block => encodeBlock(block));

jest
.spyOn(network, 'requestFromPeer')
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: encodedBlocks }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any);
});

describe('constructor', () => {
Expand All @@ -106,20 +88,106 @@ describe('Legacy Chain Handler', () => {
});

describe('sync', () => {
beforeEach(() => {
// `getLegacyBlocksFromId` should return blocks in DESC order (starting from 16270316 (excluding) till 16270306)
const reversedFixtures = blockFixtures
.slice(0, blockFixtures.length - 1)
.sort((a, b) => b.header.height - a.header.height);
const encodedBlocks = reversedFixtures.map(block => encodeBlock(block));

jest
.spyOn(network, 'requestFromPeer')
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: encodedBlocks }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any);
});
it('should sync blocks in range for given config brackets', async () => {
jest.spyOn(legacyChainHandler['_storage'], 'saveBlock');
jest.spyOn(legacyChainHandler['_storage'], 'setBracketInfo');
jest.spyOn(legacyChainHandler['_network'], 'applyNodeInfo');
jest.spyOn(legacyChainHandler as any, '_trySyncBlocks');

await legacyChainHandler.sync();

// starting from 16270316 (excluding) till 16270306 = 10,
// but we save blocks only if ```block.header.height > bracket.startHeight```
expect(legacyChainHandler['_trySyncBlocks']).toHaveBeenCalledTimes(1);
});
});

describe('_syncBlocks', () => {
let reversedFixtures;
let encodedBlocks: any[];
beforeEach(() => {
reversedFixtures = blockFixtures
.slice(0, blockFixtures.length - 1)
.sort((a, b) => b.header.height - a.header.height);
encodedBlocks = reversedFixtures.map(block => encodeBlock(block));
});
it('should sync blocks in range for given config brackets', async () => {
jest
.spyOn(network, 'requestFromPeer')
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: encodedBlocks }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any)
.mockReturnValueOnce({
data: codec.encode(getBlocksFromIdResponseSchema, { blocks: [] }),
} as any);
jest.spyOn(legacyChainHandler['_storage'], 'saveBlock');
jest.spyOn(legacyChainHandler['_storage'], 'setBracketInfo');
jest.spyOn(legacyChainHandler['_network'], 'applyNodeInfo');
jest.spyOn(legacyChainHandler as any, '_trySyncBlocks');
await legacyChainHandler['_syncBlocks'](
legacyConfig.brackets[0],
legacyBlock16270316.header.id as Buffer,
0,
);

expect(legacyChainHandler['_storage'].saveBlock).toHaveBeenCalledTimes(10);

// should be 1, since if `lastBlock.header.height > bracket.startHeight` is skipped
// & only the final `_updateBracketInfo(...)` is called
expect(legacyChainHandler['_storage'].setBracketInfo).toHaveBeenCalledTimes(1);
});

it('should throw error when no peers are found', async () => {
jest.spyOn(legacyChainHandler['_network'], 'getConnectedPeers').mockImplementation(() => []);
await expect(
legacyChainHandler['_syncBlocks'](
legacyConfig.brackets[0],
legacyBlock16270316.header.id as Buffer,
0,
),
).rejects.toThrow('No peer found with legacy info.: Attempting to sync again after 12000 ms');
});

it('should throw error when no peers are found with legacy data', async () => {
jest.spyOn(legacyChainHandler['_network'], 'getConnectedPeers').mockImplementation(
() =>
[
{
peerId: 'peerId-1',
options: {
legacy: [randomSnapshotBlockID.toString('hex')],
},
},
] as any,
);
await expect(
legacyChainHandler['_syncBlocks'](
legacyConfig.brackets[0],
legacyBlock16270316.header.id as Buffer,
0,
),
).rejects.toThrow('No peer found with legacy info.: Attempting to sync again after 12000 ms');
});
});
});

0 comments on commit ac57123

Please sign in to comment.