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

Commit

Permalink
♻️ Improve chain connector plugin
Browse files Browse the repository at this point in the history
- Improve storage to retrieve by key/val and delete efficiently instead of managing full array
- Reduce number of RPC calls during new block event
- Improve cleanup
  • Loading branch information
ishantiw committed Feb 16, 2024
1 parent 963b22d commit d04c588
Show file tree
Hide file tree
Showing 12 changed files with 1,490 additions and 1,092 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,31 @@
* Removal or modification of this copyright notice is prohibited.
*/
/* eslint-disable no-bitwise */
import {
ActiveValidator,
Certificate,
LastCertificate,
utils,
ActiveValidatorsUpdate,
} from 'lisk-sdk';
import { ActiveValidator, utils, ActiveValidatorsUpdate } from 'lisk-sdk';
import { ValidatorsData } from './types';

/**
* @see https://github.com/LiskHQ/lips/blob/main/proposals/lip-0053.md#computing-the-validators-update
*/

export const calculateActiveValidatorsUpdate = (
certificate: Certificate,
validatorsHashPreimage: ValidatorsData[],
lastCertificate: LastCertificate,
validatorsDataAtLastCertificate: ValidatorsData,
validatorsDataAtNewCertificate: ValidatorsData,
): { activeValidatorsUpdate: ActiveValidatorsUpdate; certificateThreshold: bigint } => {
let certificateThreshold;
const validatorDataAtCertificate = validatorsHashPreimage.find(validatorsData =>
validatorsData.validatorsHash.equals(certificate.validatorsHash),
);

if (!validatorDataAtCertificate) {
throw new Error('No validators data found for the certificate height.');
}

const validatorDataAtLastCertificate = validatorsHashPreimage.find(validatorsData =>
validatorsData.validatorsHash.equals(lastCertificate.validatorsHash),
);

if (!validatorDataAtLastCertificate) {
throw new Error('No validators data found for the given last certificate height.');
}

// If the certificate threshold is not changed from last certificate then we assign zero
if (
validatorDataAtCertificate.certificateThreshold ===
validatorDataAtLastCertificate.certificateThreshold
validatorsDataAtNewCertificate.certificateThreshold ===
validatorsDataAtLastCertificate.certificateThreshold
) {
certificateThreshold = validatorDataAtLastCertificate.certificateThreshold;
certificateThreshold = validatorsDataAtLastCertificate.certificateThreshold;
} else {
certificateThreshold = validatorDataAtCertificate.certificateThreshold;
certificateThreshold = validatorsDataAtNewCertificate.certificateThreshold;
}

const activeValidatorsUpdate = getActiveValidatorsUpdate(
validatorDataAtLastCertificate.validators,
validatorDataAtCertificate.validators,
validatorsDataAtLastCertificate.validators,
validatorsDataAtNewCertificate.validators,
);

return { activeValidatorsUpdate, certificateThreshold };
Expand Down
Loading

0 comments on commit d04c588

Please sign in to comment.