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

Fix genesis block aggregate commit #9048

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions elements/lisk-chain/src/block_header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ export class BlockHeader {
});
}

if (header.aggregateCommit.height !== 0) {
if (header.aggregateCommit.height !== header.height) {
errors.push({
message: 'Genesis block header aggregateCommit.height must equal 0',
message: 'Genesis block header aggregateCommit.height must equal to the genesis height',
keyword: 'const',
dataPath: 'aggregateCommit.height',
schemaPath: 'properties.aggregateCommit.height',
params: { allowedValue: 0 },
params: { allowedValue: header.height },
});
}

Expand Down
2 changes: 1 addition & 1 deletion elements/lisk-chain/test/unit/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('block', () => {
maxHeightGenerated: 0,
validatorsHash: utils.hash(Buffer.alloc(0)),
aggregateCommit: {
height: 0,
height: 1009988,
aggregationBits: Buffer.alloc(0),
certificateSignature: EMPTY_BUFFER,
},
Expand Down
4 changes: 2 additions & 2 deletions elements/lisk-chain/test/unit/block_header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ describe('block_header', () => {
);
});

it('should throw error if aggregateCommit.height is not equal to 0', () => {
it('should throw error if aggregateCommit.height is not equal to the height', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
aggregateCommit: { ...block.aggregateCommit, height: 10 },
});

expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header aggregateCommit.height must equal 0',
'Genesis block header aggregateCommit.height must equal to the genesis height',
);
});

Expand Down
2 changes: 1 addition & 1 deletion framework/src/engine/consensus/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export class Consensus {
const finalizedBlockHeader = await this._chain.dataAccess.getBlockHeaderByHeight(
this._chain.finalizedHeight,
);
return finalizedBlockHeader.aggregateCommit.height;
return Math.max(finalizedBlockHeader.aggregateCommit.height, this._chain.genesisHeight);
}

private async _execute(block: Block, peerID: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/genesis_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const generateGenesisBlock = async (
impliesMaxPrevotes: true,
assetRoot,
aggregateCommit: {
height: 0,
height,
aggregationBits: EMPTY_BUFFER,
certificateSignature: EMPTY_BUFFER,
},
Expand Down
45 changes: 45 additions & 0 deletions framework/test/unit/engine/consensus/consensus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('consensus', () => {
dataAccess: {
getBlockHeaderByHeight: jest.fn().mockResolvedValue(lastBlock.header),
},
genesisHeight: 0,
} as unknown as Chain;
network = {
registerEndpoint: jest.fn(),
Expand Down Expand Up @@ -221,6 +222,50 @@ describe('consensus', () => {
});
});

describe('getMaxRemovalHeight', () => {
it('should return genesis height if the finalizedBlock.aggregateCommit.height is smaller', async () => {
const finalizedBlock = await createValidDefaultBlock({
header: {
height: 1,
aggregateCommit: {
height: 0,
aggregationBits: Buffer.alloc(0),
certificateSignature: Buffer.alloc(0),
},
},
});
(chain.dataAccess.getBlockHeaderByHeight as jest.Mock).mockResolvedValue(
finalizedBlock.header,
);
const genesisHeight = 25519;
(chain as any).genesisHeight = genesisHeight;

await expect(consensus.getMaxRemovalHeight()).resolves.toEqual(genesisHeight);
});

it('should return finalizedBlock.aggregateCommit.height if the genesis height is smaller', async () => {
const finalizedBlock = await createValidDefaultBlock({
header: {
height: 1,
aggregateCommit: {
height: 25520,
aggregationBits: Buffer.alloc(0),
certificateSignature: Buffer.alloc(0),
},
},
});
(chain.dataAccess.getBlockHeaderByHeight as jest.Mock).mockResolvedValue(
finalizedBlock.header,
);
const genesisHeight = 25519;
(chain as any).genesisHeight = genesisHeight;

await expect(consensus.getMaxRemovalHeight()).resolves.toEqual(
finalizedBlock.header.aggregateCommit.height,
);
});
});

describe('certifySingleCommit', () => {
const passphrase = Mnemonic.generateMnemonic(256);
const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase);
Expand Down
1 change: 1 addition & 0 deletions framework/test/unit/genesis_block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('generateGenesisBlock', () => {
expect(result.header.validatorsHash).toHaveLength(32);
expect(result.header.eventRoot).toHaveLength(32);
expect(result.header.version).toBe(0);
expect(result.header.aggregateCommit.height).toBe(30);

expect(stateMachine.executeGenesisBlock).toHaveBeenCalledTimes(1);
});
Expand Down