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

Commit

Permalink
Merge pull request #6517 from LiskHQ/6516-fix_random_seed_cal
Browse files Browse the repository at this point in the history
Fix random seed calculation - Closes #6516
  • Loading branch information
shuse2 authored Jul 1, 2021
2 parents 3a1be1e + bed2be2 commit b19714e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion framework/src/modules/dpos/random_seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const bitwiseXOR = (bufferArray: Buffer[]): Buffer => {
return result;
};

const findPreviousHeaderOfDelegate = (
export const findPreviousHeaderOfDelegate = (
header: BlockHeader,
searchTillHeight: number,
headersMap: HeadersMap,
Expand All @@ -65,6 +65,10 @@ const findPreviousHeaderOfDelegate = (
const searchTill = Math.max(searchTillHeight, 1);

for (let i = height - 1; i >= searchTill; i -= 1) {
const foundHeader = headersMap[i];
if (!foundHeader) {
return undefined;
}
if (headersMap[i].generatorPublicKey.equals(generatorPublicKey)) {
return headersMap[i];
}
Expand Down
51 changes: 50 additions & 1 deletion framework/test/unit/modules/dpos/random_seed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
*/

import { BlockHeader } from '@liskhq/lisk-chain';
import { getRandomBytes } from '@liskhq/lisk-cryptography';
import * as randomSeedFirstRound from '../../../fixtures/dpos_random_seed_generation/dpos_random_seed_generation_first_round.json';
import * as randomSeedsMultipleRounds from '../../../fixtures/dpos_random_seed_generation/dpos_random_seed_generation_other_rounds.json';
import * as randomSeedsInvalidSeedReveal from '../../../fixtures/dpos_random_seed_generation/dpos_random_seed_generation_invalid_seed_reveal.json';
import * as randomSeedsNotForgedEarlier from '../../../fixtures/dpos_random_seed_generation/dpos_random_seed_generation_not_forged_earlier.json';

import { generateRandomSeeds } from '../../../../src/modules/dpos/random_seed';
import {
generateRandomSeeds,
findPreviousHeaderOfDelegate,
} from '../../../../src/modules/dpos/random_seed';
import { Rounds } from '../../../../src/modules/dpos/rounds';

const generateHeadersFromTest = (blocks: any): BlockHeader[] =>
Expand Down Expand Up @@ -70,4 +74,49 @@ describe('random_seed', () => {
},
);
});

describe('findPreviousHeaderOfDelegate', () => {
describe('when previously generated block exist in header map', () => {
it('should return the header', () => {
const generatorPublicKey = Buffer.from('some-generator', 'utf8');
const expectedHeader = { generatorPublicKey, height: 320 } as BlockHeader;
const result = findPreviousHeaderOfDelegate(
{ height: 321, generatorPublicKey } as any,
320,
{ [expectedHeader.height]: expectedHeader },
);
expect(result).toBe(expectedHeader);
});
});
describe('when previously generated block does not exist in header map', () => {
it('should return undefined', () => {
const generatorPublicKey = Buffer.from('some-generator', 'utf8');
const result = findPreviousHeaderOfDelegate(
{ height: 321, generatorPublicKey } as any,
319,
{
320: { generatorPublicKey: getRandomBytes(32), height: 320 } as BlockHeader,
319: { generatorPublicKey: getRandomBytes(32), height: 319 } as BlockHeader,
},
);
expect(result).toBeUndefined();
});
});

describe('when the header map does not include all the height until searchTill', () => {
it('should return undefined', () => {
const generatorPublicKey = Buffer.from('some-generator', 'utf8');
const result = findPreviousHeaderOfDelegate(
{ height: 321, generatorPublicKey } as any,
300,
{
320: { generatorPublicKey: getRandomBytes(32), height: 320 } as BlockHeader,
319: { generatorPublicKey: getRandomBytes(32), height: 319 } as BlockHeader,
318: { generatorPublicKey: getRandomBytes(32), height: 319 } as BlockHeader,
},
);
expect(result).toBeUndefined();
});
});
});
});

0 comments on commit b19714e

Please sign in to comment.