diff --git a/elements/lisk-chain/src/data_access/storage.ts b/elements/lisk-chain/src/data_access/storage.ts index ed6f60f1a9..9198907a01 100644 --- a/elements/lisk-chain/src/data_access/storage.ts +++ b/elements/lisk-chain/src/data_access/storage.ts @@ -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, diff --git a/examples/interop/pos-mainchain-fast/config/custom_config_node_one.json b/examples/interop/pos-mainchain-fast/config/custom_config_node_one.json index 873c80e71a..736e377a30 100644 --- a/examples/interop/pos-mainchain-fast/config/custom_config_node_one.json +++ b/examples/interop/pos-mainchain-fast/config/custom_config_node_one.json @@ -1,11 +1,7 @@ { "system": { "dataPath": "~/.lisk/pos-mainchain-node-one", - "keepEventsForHeights": 300, - "logLevel": "info", - "inclusionProofKeys": [ - "83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46" - ] + "logLevel": "info" }, "rpc": { "modes": ["ipc"], diff --git a/examples/interop/pos-mainchain-fast/config/custom_config_node_two.json b/examples/interop/pos-mainchain-fast/config/custom_config_node_two.json index 4fe6f6be17..99c18dcc72 100644 --- a/examples/interop/pos-mainchain-fast/config/custom_config_node_two.json +++ b/examples/interop/pos-mainchain-fast/config/custom_config_node_two.json @@ -1,11 +1,7 @@ { "system": { "dataPath": "~/.lisk/pos-mainchain-node-two", - "keepEventsForHeights": 300, - "logLevel": "info", - "inclusionProofKeys": [ - "83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46" - ] + "logLevel": "info" }, "rpc": { "modes": ["ipc"], diff --git a/examples/interop/pos-mainchain-fast/config/default/config.json b/examples/interop/pos-mainchain-fast/config/default/config.json index da604b014f..38ecf0f438 100644 --- a/examples/interop/pos-mainchain-fast/config/default/config.json +++ b/examples/interop/pos-mainchain-fast/config/default/config.json @@ -1,7 +1,8 @@ { "system": { "dataPath": "~/.lisk/pos-mainchain-fast", - "keepEventsForHeights": 300, + "keepEventsForHeights": -1, + "keepInclusionProofsForHeights": -1, "inclusionProofKeys": [ "83ed0d250000160811fdaf692ba77eabfbfc3a6bb3c4cf6a87beafd28cfe90b5dc64cb20ab46" ], diff --git a/framework/src/engine/consensus/consensus.ts b/framework/src/engine/consensus/consensus.ts index 6030b233ed..d886b2ec06 100644 --- a/framework/src/engine/consensus/consensus.ts +++ b/framework/src/engine/consensus/consensus.ts @@ -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'; @@ -69,6 +69,7 @@ interface ConsensusArgs { chain: Chain; network: Network; genesisConfig: GenesisConfig; + systemConfig: SystemConfig; abi: ABI; bft: BFTModule; } @@ -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; @@ -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 { @@ -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 diff --git a/framework/src/engine/engine.ts b/framework/src/engine/engine.ts index a0fe9fce78..ef444e5a01 100644 --- a/framework/src/engine/engine.ts +++ b/framework/src/engine/engine.ts @@ -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 { @@ -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({ @@ -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() }),