Skip to content

Commit

Permalink
feat: improve quorum and factory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Jan 18, 2024
1 parent 1247e9f commit 49905e9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 64 deletions.
28 changes: 8 additions & 20 deletions onchain/rollups/test/foundry/consensus/quorum/Quorum.t.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.8;
pragma solidity ^0.8.22;

import {InputRange} from "contracts/common/InputRange.sol";
import {Quorum} from "contracts/consensus/quorum/Quorum.sol";
import {IConsensus} from "contracts/consensus/IConsensus.sol";

import {TestBase} from "../../util/TestBase.sol";

Expand Down Expand Up @@ -51,21 +52,8 @@ library LibClaim {
contract QuorumTest is TestBase {
using LibClaim for Quorum;

event ClaimSubmission(
address indexed submitter,
address indexed dapp,
InputRange inputRange,
bytes32 epochHash
);

event ClaimAcceptance(
address indexed dapp,
InputRange inputRange,
bytes32 epochHash
);

function testConstructor(uint8 numOfValidators) external {
address[] memory validators = generateAddresses(numOfValidators);
address[] memory validators = _generateAddresses(numOfValidators);

Quorum quorum = new Quorum(validators);

Expand Down Expand Up @@ -101,13 +89,13 @@ contract QuorumTest is TestBase {
}

function testValidatorId(uint8 numOfValidators, address addr) external {
address[] memory validators = generateAddresses(numOfValidators);
address[] memory validators = _generateAddresses(numOfValidators);

Quorum quorum = new Quorum(validators);

uint256 id = quorum.validatorId(addr);

if (contains(validators, addr)) {
if (_contains(validators, addr)) {
assertLe(1, id);
assertLe(id, numOfValidators);
} else {
Expand Down Expand Up @@ -194,7 +182,7 @@ contract QuorumTest is TestBase {
// ------------------

function _deployQuorum(uint256 numOfValidators) internal returns (Quorum) {
return new Quorum(generateAddresses(numOfValidators));
return new Quorum(_generateAddresses(numOfValidators));
}

function _checkSubmitted(
Expand Down Expand Up @@ -235,7 +223,7 @@ contract QuorumTest is TestBase {

if (
entry.emitter == address(quorum) &&
entry.topics[0] == ClaimSubmission.selector
entry.topics[0] == IConsensus.ClaimSubmission.selector
) {
address submitter = address(uint160(uint256(entry.topics[1])));
address dapp = address(uint160(uint256(entry.topics[2])));
Expand All @@ -256,7 +244,7 @@ contract QuorumTest is TestBase {

if (
entry.emitter == address(quorum) &&
entry.topics[0] == ClaimAcceptance.selector
entry.topics[0] == IConsensus.ClaimAcceptance.selector
) {
address dapp = address(uint160(uint256(entry.topics[1])));

Expand Down
85 changes: 43 additions & 42 deletions onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,65 @@
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

/// @title Quorum Factory Test
pragma solidity ^0.8.8;
pragma solidity ^0.8.22;

import {QuorumFactory} from "contracts/consensus/quorum/QuorumFactory.sol";
import {QuorumFactory, IQuorumFactory} from "contracts/consensus/quorum/QuorumFactory.sol";
import {Quorum} from "contracts/consensus/quorum/Quorum.sol";
import {Vm} from "forge-std/Vm.sol";

import {TestBase} from "../../util/TestBase.sol";

contract QuorumFactoryTest is TestBase {
QuorumFactory factory;
uint256 internal constant QUORUM_MAX_SIZE = 50;

event QuorumCreated(Quorum quorum);
uint256 constant _QUORUM_MAX_SIZE = 50;
QuorumFactory _factory;

function setUp() public {
factory = new QuorumFactory();
_factory = new QuorumFactory();
}

function testNewQuorum(uint256 seed) public {
uint256 numOfValidators = bound(seed, 1, QUORUM_MAX_SIZE);
address[] memory validators = generateAddresses(numOfValidators);
uint256 numOfValidators = bound(seed, 1, _QUORUM_MAX_SIZE);
address[] memory validators = _generateAddresses(numOfValidators);

vm.recordLogs();

Quorum quorum = _factory.newQuorum(validators);

_testNewQuorumAux(validators, quorum);
}

function testNewQuorumDeterministic(uint256 seed, bytes32 salt) public {
uint256 numOfValidators = bound(seed, 1, _QUORUM_MAX_SIZE);
address[] memory validators = _generateAddresses(numOfValidators);

address precalculatedAddress = _factory.calculateQuorumAddress(
validators,
salt
);

vm.recordLogs();

Quorum quorum = factory.newQuorum(validators);
Quorum quorum = _factory.newQuorum(validators, salt);

_testNewQuorumAux(validators, quorum);

// Precalculated address must match actual address
assertEq(precalculatedAddress, address(quorum));

precalculatedAddress = _factory.calculateQuorumAddress(
validators,
salt
);

// Precalculated address must STILL match actual address
assertEq(precalculatedAddress, address(quorum));

testNewQuorumAux(validators, quorum);
// Cannot deploy a quorum with the same salt twice
vm.expectRevert();
_factory.newQuorum(validators, salt);
}

function testNewQuorumAux(
function _testNewQuorumAux(
address[] memory validators,
Quorum quorum
) internal {
Expand All @@ -42,8 +71,8 @@ contract QuorumFactoryTest is TestBase {
Vm.Log memory entry = entries[i];

if (
entry.emitter == address(factory) &&
entry.topics[0] == QuorumCreated.selector
entry.emitter == address(_factory) &&
entry.topics[0] == IQuorumFactory.QuorumCreated.selector
) {
++numQuorumCreated;
Quorum eventQuorum = abi.decode(entry.data, (Quorum));
Expand All @@ -58,32 +87,4 @@ contract QuorumFactoryTest is TestBase {
assertEq(validators[i], quorum.validatorById(i + 1));
}
}

function testNewQuorumDeterministic(uint256 seed, bytes32 salt) public {
uint256 numOfValidators = bound(seed, 1, QUORUM_MAX_SIZE);
address[] memory validators = generateAddresses(numOfValidators);

address precalculatedAddress = factory.calculateQuorumAddress(
validators,
salt
);

vm.recordLogs();

Quorum quorum = factory.newQuorum(validators, salt);

testNewQuorumAux(validators, quorum);

// Precalculated address must match actual address
assertEq(precalculatedAddress, address(quorum));

precalculatedAddress = factory.calculateQuorumAddress(validators, salt);

// Precalculated address must STILL match actual address
assertEq(precalculatedAddress, address(quorum));

// Cannot deploy a quorum with the same salt twice
vm.expectRevert();
factory.newQuorum(validators, salt);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/test/foundry/util/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract TestBase is Test {
_;
}

function contains(
function _contains(
address[] memory array,
address elem
) internal pure returns (bool) {
Expand All @@ -30,7 +30,7 @@ contract TestBase is Test {
return false;
}

function generateAddresses(
function _generateAddresses(
uint256 n
) internal pure returns (address[] memory) {
address[] memory array = new address[](n);
Expand Down

0 comments on commit 49905e9

Please sign in to comment.