Skip to content

Commit

Permalink
test: fix flaky tests by removing bogosort (#163)
Browse files Browse the repository at this point in the history
* test: fix flaky tests by removing bogosort

* test: fix flaky test by rejecting empty addr inputs
  • Loading branch information
wadealexc authored Jan 31, 2024
1 parent cc086d3 commit 1807458
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion test/unit/BLSApkRegistryUnit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,14 @@ contract BLSApkRegistryUnitTests_registerBLSPublicKey is BLSApkRegistryUnitTests
address operator,
address operator2
) public {
cheats.assume(operator != address(0));
cheats.assume(operator != operator2);
BN254.G1Point memory messageHash =
registryCoordinator.pubkeyRegistrationMessageHash(operator);
pubkeyRegistrationParams.pubkeyRegistrationSignature = _signMessage(operator);

cheats.startPrank(address(registryCoordinator));
blsApkRegistry.registerBLSPublicKey(operator, pubkeyRegistrationParams, messageHash);
blsApkRegistry.registerBLSPublicKey(operator, pubkeyRegistrationParams, messageHash);

cheats.expectRevert("BLSApkRegistry.registerBLSPublicKey: public key already registered");
blsApkRegistry.registerBLSPublicKey(operator2, pubkeyRegistrationParams, messageHash);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/BLSSignatureCheckerUnit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract BLSSignatureCheckerUnitTests is BLSMockAVSDeployer {
// this test checks that a valid signature from maxOperatorsToRegister with a random number of nonsigners is checked
// correctly on the BLSSignatureChecker contract when all operators are only regsitered for a single quorum and
// the signature is only checked for stakes on that quorum
function test_checkSignatures_SingleQuorum(uint256 pseudoRandomNumber) public {
function testFuzz_checkSignatures_SingleQuorum(uint256 pseudoRandomNumber) public {
uint256 numNonSigners = pseudoRandomNumber % (maxOperatorsToRegister - 1);
uint256 quorumBitmap = 1;
bytes memory quorumNumbers = BitmapUtils.bitmapToBytesArray(quorumBitmap);
Expand Down
25 changes: 19 additions & 6 deletions test/utils/BLSMockAVSDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,22 @@ contract BLSMockAVSDeployer is MockAVSDeployer {
uint256[] memory nonSignerPrivateKeys = new uint256[](numNonSigners);
for (uint i = 0; i < numNonSigners; i++) {
nonSignerPrivateKeys[i] = uint256(keccak256(abi.encodePacked("nonSignerPrivateKey", pseudoRandomNumber, i))) % BN254.FR_MODULUS;
uint256 j = 0;
if(i != 0) {
while(BN254.generatorG1().scalar_mul(nonSignerPrivateKeys[i]).hashG1Point() <= BN254.generatorG1().scalar_mul(nonSignerPrivateKeys[i-1]).hashG1Point()){
nonSignerPrivateKeys[i] = uint256(keccak256(abi.encodePacked("nonSignerPrivateKey", pseudoRandomNumber, j))) % BN254.FR_MODULUS;
j++;
}
}

// Sort nonSignerPrivateKeys in order of ascending pubkeyHash
// Uses insertion sort to sort array in place
for (uint i = 1; i < nonSignerPrivateKeys.length; i++) {
uint privateKey = nonSignerPrivateKeys[i];
bytes32 pubkeyHash = _toPubkeyHash(privateKey);
uint j = i;

// Move elements of nonSignerPrivateKeys[0..i-1] that are greater than the current key
// to one position ahead of their current position
while (j > 0 && _toPubkeyHash(nonSignerPrivateKeys[j - 1]) > pubkeyHash) {
nonSignerPrivateKeys[j] = nonSignerPrivateKeys[j - 1];
j--;
}
nonSignerPrivateKeys[j] = privateKey;
}

return (signerPrivateKeys, nonSignerPrivateKeys);
Expand Down Expand Up @@ -135,4 +144,8 @@ contract BLSMockAVSDeployer is MockAVSDeployer {

return (referenceBlockNumber, nonSignerStakesAndSignature);
}

function _toPubkeyHash(uint privKey) internal view returns (bytes32) {
return BN254.generatorG1().scalar_mul(privKey).hashG1Point();
}
}

0 comments on commit 1807458

Please sign in to comment.