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

Commit

Permalink
♻️ Save inclusionProofs on both block receive and generate
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Feb 19, 2024
1 parent 9a28042 commit 3b1c394
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
48 changes: 24 additions & 24 deletions framework/src/engine/consensus/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,30 +359,6 @@ 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 Expand Up @@ -598,6 +574,30 @@ export class Consensus {
this._metrics.finalizedHeight.set(this._chain.finalizedHeight);
this._metrics.maxHeightCertified.set(block.header.aggregateCommit.height);
this._metrics.maxHeightPrevoted.set(block.header.maxHeightPrevoted);

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.',
);
}
});
}

Expand Down
18 changes: 11 additions & 7 deletions framework/src/engine/endpoint/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,24 @@ export class ChainEndpoint {
return events.map(e => e.toJSON());
}

public async getInclusionProofsAtHeight(context: RequestContext): Promise<JSONObject<Proof>> {
public async getInclusionProofsAtHeight(
context: RequestContext,
): Promise<{ proof: JSONObject<Proof> }> {
const { height } = context.params;
if (typeof height !== 'number' || height < 0) {
throw new Error('Invalid parameters. height must be zero or a positive number.');
}
const inclusionProof = await this._chain.dataAccess.getInclusionProofs(height);

return {
queries: inclusionProof.queries.map(q => ({
bitmap: q.bitmap.toString('hex'),
key: q.key.toString('hex'),
value: q.value.toString('hex'),
})),
siblingHashes: inclusionProof.siblingHashes.map(h => h.toString('hex')),
proof: {
queries: inclusionProof.queries.map(q => ({
bitmap: q.bitmap.toString('hex'),
key: q.key.toString('hex'),
value: q.value.toString('hex'),
})),
siblingHashes: inclusionProof.siblingHashes.map(h => h.toString('hex')),
},
};
}

Expand Down

0 comments on commit 3b1c394

Please sign in to comment.