From 49905e97ed4dc20e3461ac794e286724e0ffb926 Mon Sep 17 00:00:00 2001 From: Zehui Zheng Date: Fri, 12 Jan 2024 21:07:06 +0800 Subject: [PATCH] feat: improve quorum and factory tests --- .../foundry/consensus/quorum/Quorum.t.sol | 28 ++---- .../consensus/quorum/QuorumFactory.t.sol | 85 ++++++++++--------- .../rollups/test/foundry/util/TestBase.sol | 4 +- 3 files changed, 53 insertions(+), 64 deletions(-) diff --git a/onchain/rollups/test/foundry/consensus/quorum/Quorum.t.sol b/onchain/rollups/test/foundry/consensus/quorum/Quorum.t.sol index a6630278..396319bc 100644 --- a/onchain/rollups/test/foundry/consensus/quorum/Quorum.t.sol +++ b/onchain/rollups/test/foundry/consensus/quorum/Quorum.t.sol @@ -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"; @@ -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); @@ -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 { @@ -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( @@ -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]))); @@ -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]))); diff --git a/onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol b/onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol index 15667952..e4217427 100644 --- a/onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol +++ b/onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol @@ -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 { @@ -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)); @@ -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); - } } diff --git a/onchain/rollups/test/foundry/util/TestBase.sol b/onchain/rollups/test/foundry/util/TestBase.sol index 073bacf6..58048443 100644 --- a/onchain/rollups/test/foundry/util/TestBase.sol +++ b/onchain/rollups/test/foundry/util/TestBase.sol @@ -18,7 +18,7 @@ contract TestBase is Test { _; } - function contains( + function _contains( address[] memory array, address elem ) internal pure returns (bool) { @@ -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);