Skip to content

Commit

Permalink
[M-02] uninitialized counters in KeyGenHistory fix
Browse files Browse the repository at this point in the history
  • Loading branch information
axel-muller committed Jan 21, 2025
1 parent 9d2f4de commit cbfc765
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions contracts/KeyGenHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ contract KeyGenHistory is Initializable, OwnableUpgradeable, IKeyGenHistory {
}

currentKeyGenRound = 1;
numberOfPartsWritten = uint128(_validators.length);
numberOfAcksWritten = uint128(_validators.length);
}

/// @dev Clears the state (acks and parts of previous validators.
Expand Down
47 changes: 44 additions & 3 deletions test/KeyGenHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('KeyGenHistory', () => {
if (!logOutput) {
return;
}

const epoch = await stakingHbbft.stakingEpoch();
const keyGenRound = await keyGenHistory.currentKeyGenRound();
const isEarlyEpochEnd = await blockRewardHbbft.earlyEpochEnd();
Expand Down Expand Up @@ -480,6 +480,47 @@ describe('KeyGenHistory', () => {
acks
)).to.be.revertedWithCustomError(contract, "InvalidInitialization");
});

it('should initialize and set parts, acks', async () => {
const KeyGenFactoryFactory = await ethers.getContractFactory("KeyGenHistory");
const keyGenHistory = await upgrades.deployProxy(
KeyGenFactoryFactory,
[
owner.address,
await validatorSetHbbft.getAddress(),
initializingMiningAddresses,
parts,
acks
],
{ initializer: 'initialize' }
) as unknown as KeyGenHistory;

await keyGenHistory.waitForDeployment();

let actualPartsCount = 0;
let actualAcksCount = 0;
for (const miningAddress of initializingMiningAddresses) {
const storedPart = await keyGenHistory.getPart(miningAddress);
const storedAcksLength = await keyGenHistory.getAcksLength(miningAddress);

if (storedPart.length > 0) {
actualPartsCount++;
}

if (storedAcksLength > 0) {
actualAcksCount++;
}
}

const [
numberOfPartsWritten,
numberOfAcksWritten
] = await keyGenHistory.getNumberOfKeyFragmentsWritten();

expect(await keyGenHistory.getCurrentKeyGenRound()).to.eq(1n);
expect(numberOfPartsWritten).to.eq(actualPartsCount);
expect(numberOfAcksWritten).to.eq(actualAcksCount);
});
});

describe('contract functions', async () => {
Expand Down Expand Up @@ -707,8 +748,8 @@ describe('KeyGenHistory', () => {

const connectivityTrackerCaller = await ethers.getImpersonatedSigner(await connectivityTracker.getAddress());

await blockRewardHbbft.connect(connectivityTrackerCaller).notifyEarlyEpochEnd({ gasPrice: "0"});

await blockRewardHbbft.connect(connectivityTrackerCaller).notifyEarlyEpochEnd({ gasPrice: "0" });

await printEarlyEpochEndInfo();

Expand Down

0 comments on commit cbfc765

Please sign in to comment.