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

Commit

Permalink
♻️ Move inclusion proof collection to consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Feb 11, 2024
1 parent 522ad5f commit 2cace9d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion elements/lisk-chain/src/data_access/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export class Storage {
}
}

if (this._keepEventsForHeights > -1) {
if (this._keepInclusionProofsForHeights > -1) {
// events are removed only if finalized and below height - keepEventsForHeights
const minInclusionProofDeleteHeight = Math.min(
finalizedHeight,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"system": {
"dataPath": "~/.lisk/pos-mainchain-node-one",
"keepEventsForHeights": 300,
"logLevel": "info",
"inclusionProofKeys": [
"83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46"
]
"logLevel": "info"
},
"rpc": {
"modes": ["ipc"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"system": {
"dataPath": "~/.lisk/pos-mainchain-node-two",
"keepEventsForHeights": 300,
"logLevel": "info",
"inclusionProofKeys": [
"83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46"
]
"logLevel": "info"
},
"rpc": {
"modes": ["ipc"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"system": {
"dataPath": "~/.lisk/pos-mainchain-fast",
"keepEventsForHeights": 300,
"keepEventsForHeights": -1,
"keepInclusionProofsForHeights": -1,
"inclusionProofKeys": [
"83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46"
],
Expand Down
31 changes: 30 additions & 1 deletion framework/src/engine/consensus/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
NETWORK_RPC_GET_LAST_BLOCK,
NETWORK_LEGACY_GET_BLOCKS_FROM_ID,
} from './constants';
import { GenesisConfig } from '../../types';
import { GenesisConfig, SystemConfig } from '../../types';
import { AggregateCommit } from './types';
import { forkChoice, ForkStatus } from './fork_choice/fork_choice_rule';
import { CommitPool } from './certificate_generation/commit_pool';
Expand All @@ -69,6 +69,7 @@ interface ConsensusArgs {
chain: Chain;
network: Network;
genesisConfig: GenesisConfig;
systemConfig: SystemConfig;
abi: ABI;
bft: BFTModule;
}
Expand Down Expand Up @@ -107,6 +108,8 @@ export class Consensus {
private readonly _mutex: jobHandlers.Mutex;
private readonly _bft: BFTModule;
private readonly _genesisConfig: GenesisConfig;
private readonly _systemConfig: SystemConfig;
private readonly _inclusionProofKeys: Buffer[];

// init parameters
private _logger!: Logger;
Expand Down Expand Up @@ -139,6 +142,8 @@ export class Consensus {
this._mutex = new jobHandlers.Mutex();
this._bft = args.bft;
this._genesisConfig = args.genesisConfig;
this._systemConfig = args.systemConfig;
this._inclusionProofKeys = args.systemConfig.inclusionProofKeys.map(k => Buffer.from(k, 'hex'));
}

public async init(args: InitArgs): Promise<void> {
Expand Down Expand Up @@ -354,6 +359,30 @@ export class Consensus {
await this._abi.clear({});
this._logger.error({ err: error as Error }, 'Fail to execute block.');
}

try {
// Save inclusion proof when keys are provided
if (
(this._systemConfig.keepInclusionProofsForHeights > 0 ||
this._systemConfig.keepInclusionProofsForHeights === -1) &&
this._inclusionProofKeys.length > 0
) {
this._logger.info(`Starting saving inclusion proof at height ${block.header.height}`);
const result = await this._abi.prove({
keys: this._inclusionProofKeys,
stateRoot: block.header.stateRoot as Buffer,
});

await this._chain.dataAccess.setInclusionProofs(result.proof, block.header.height);
this._logger.info(`Successfully set inclusion proof at height ${block.header.height}`);
}
} catch (error) {
// Handle the error so that it doesn't affect block execute as it's outside block execution process
this._logger.error(
{ err: error as Error },
'Failed to save inclusion proof for the given keys.',
);
}
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down
17 changes: 1 addition & 16 deletions framework/src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,10 @@ export class Engine {
private _blockchainDB!: Database;
private _legacyDB!: Database;
private _chainID!: Buffer;
private readonly _inclusionProofKeys: Buffer[];

public constructor(abi: ABI, config: EngineConfig) {
this._abi = abi;
this._config = config;
this._inclusionProofKeys = this._config.system.inclusionProofKeys.map(k =>
Buffer.from(k, 'hex'),
);
}

public async generateBlock(input: BlockGenerateInput): Promise<Block> {
Expand Down Expand Up @@ -169,6 +165,7 @@ export class Engine {
network: this._network,
chain: this._chain,
genesisConfig: this._config.genesis,
systemConfig: this._config.system,
bft: this._bftModule,
});
this._generator = new Generator({
Expand Down Expand Up @@ -305,18 +302,6 @@ export class Engine {
this._consensus.events.on(CONSENSUS_EVENT_BLOCK_NEW, ({ block }: { block: Block }) => {
this._generator.onNewBlock(block);

if (this._config.system.inclusionProofKeys.length > 0) {
this._abi
.prove({ keys: this._inclusionProofKeys, stateRoot: block.header.stateRoot as Buffer })
.then(result => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
this._chain.dataAccess
.setInclusionProofs(result.proof, block.header.height)
.catch((err: Error) => this._logger.error({ err }, 'Failed to set inclusion proof'));
})
.catch(err => this._logger.error({ err: err as Error }, 'Failed to query prove'));
}

Promise.all([
this._rpcServer.publish(EVENT_CHAIN_BLOCK_NEW, { blockHeader: block.header.toJSON() }),
this._rpcServer.publish(EVENT_NETWORK_BLOCK_NEW, { blockHeader: block.header.toJSON() }),
Expand Down
4 changes: 3 additions & 1 deletion framework/test/integration/node/genesis_block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Chain } from '@liskhq/lisk-chain';
import { address } from '@liskhq/lisk-cryptography';
import * as testing from '../../../src/testing';
import { createTransferTransaction, defaultTokenID } from '../../utils/mocks/transaction';
import { TokenModule } from '../../../src';
import { TokenModule, applicationConfigSchema } from '../../../src';
import { genesisTokenStoreSchema } from '../../../src/modules/token';
import { GenesisTokenStore } from '../../../src/modules/token/types';
import { Consensus } from '../../../src/engine/consensus';
Expand Down Expand Up @@ -131,6 +131,7 @@ describe('genesis block', () => {
const chain = new Chain({
maxTransactionsSize: 15 * 1024,
keepEventsForHeights: -1,
keepInclusionProofsForHeights: -1,
});
const newConsensus = new Consensus({
abi: consensus['_abi'],
Expand All @@ -141,6 +142,7 @@ describe('genesis block', () => {
registerEndpoint: () => {},
registerHandler: () => {},
} as unknown as Network,
systemConfig: applicationConfigSchema.default.system,
});
chain.init({
db: processEnv.getBlockchainDB(),
Expand Down
2 changes: 2 additions & 0 deletions framework/test/unit/engine/consensus/consensus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
CONSENSUS_EVENT_BLOCK_BROADCAST,
NETWORK_EVENT_POST_BLOCK,
} from '../../../../src/engine/consensus/constants';
import { applicationConfigSchema } from '../../../../src';

describe('consensus', () => {
const genesis = genesisBlock() as unknown as Block;
Expand Down Expand Up @@ -128,6 +129,7 @@ describe('consensus', () => {
genesisConfig: {
blockTime: 10,
} as any,
systemConfig: applicationConfigSchema.default.system,
});
dbMock = {
get: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('block_synchronization_mechanism', () => {
chainModule = new Chain({
maxTransactionsSize: 15000,
keepEventsForHeights: -1,
keepInclusionProofsForHeights: -1,
});
chainModule.init({
db: new InMemoryDatabase(),
Expand Down

0 comments on commit 2cace9d

Please sign in to comment.