Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Ortega committed Oct 9, 2023
1 parent 622aa9b commit 1987720
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pragma solidity ^0.8.8;
import {Quorum} from "./Quorum.sol";
import {IHistory} from "../../history/IHistory.sol";


/// @title Quorum factory interface
interface IQuorumFactory {
// Events
Expand Down Expand Up @@ -53,4 +52,4 @@ interface IQuorumFactory {
IHistory _history,
bytes32 _salt
) external view returns (address);
}
}
8 changes: 6 additions & 2 deletions onchain/rollups/contracts/consensus/quorum/QuorumFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ contract QuorumFactory is IQuorumFactory {
IHistory _history,
bytes32 _salt
) external override returns (Quorum) {
Quorum quorum = new Quorum{salt: _salt}(_quorumValidators, _shares, _history);
Quorum quorum = new Quorum{salt: _salt}(
_quorumValidators,
_shares,
_history
);

emit QuorumCreated(_quorumValidators, quorum);

Expand All @@ -54,4 +58,4 @@ contract QuorumFactory is IQuorumFactory {
)
);
}
}
}
76 changes: 50 additions & 26 deletions onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {TestBase} from "../../util/TestBase.sol";
import "forge-std/console.sol";

contract QuorumFactoryTest is TestBase {

QuorumFactory factory;

event QuorumCreated(address[] quorumValidators, Quorum quorum);
Expand All @@ -29,11 +28,9 @@ contract QuorumFactoryTest is TestBase {
factory = new QuorumFactory();
}

function testNewQuorum(
uint256 _numValidators
) public {
vm.assume(_numValidators>1);
vm.assume(_numValidators<50);
function testNewQuorum(uint256 _numValidators) public {
vm.assume(_numValidators > 1);
vm.assume(_numValidators < 50);

address[] memory quorumValidators = generateValidators(_numValidators);
uint256[] memory shares = generateShares(quorumValidators);
Expand All @@ -53,19 +50,29 @@ contract QuorumFactoryTest is TestBase {
uint256 _numValidators,
bytes32 _salt
) public {
vm.assume(_numValidators>1);
vm.assume(_numValidators<50);
vm.assume(_numValidators > 1);
vm.assume(_numValidators < 50);

address[] memory quorumValidators = generateValidators(_numValidators);
uint256[] memory shares = generateShares(quorumValidators);

IHistory history = new History(msg.sender);

address precalculatedAddress = factory.calculateQuorumAddress(quorumValidators, shares, history, _salt);
address precalculatedAddress = factory.calculateQuorumAddress(
quorumValidators,
shares,
history,
_salt
);

vm.recordLogs();

Quorum quorum = factory.newQuorum(quorumValidators, shares, history, _salt);
Quorum quorum = factory.newQuorum(
quorumValidators,
shares,
history,
_salt
);

emit QuorumCreated(quorumValidators, quorum);

Expand All @@ -78,8 +85,8 @@ contract QuorumFactoryTest is TestBase {
uint256 _numValidators,
bytes32 _salt
) public {
vm.assume(_numValidators>1);
vm.assume(_numValidators<50);
vm.assume(_numValidators > 1);
vm.assume(_numValidators < 50);

address[] memory quorumValidators = generateValidators(_numValidators);
uint256[] memory shares = generateShares(quorumValidators);
Expand All @@ -92,44 +99,63 @@ contract QuorumFactoryTest is TestBase {
vm.expectRevert();
factory.newQuorum(quorumValidators, shares, history, _salt);
}

// HELPER FUNCTIONS
function generateValidators(uint256 _numValidators) pure internal returns(address[] memory){
function generateValidators(
uint256 _numValidators
) internal pure returns (address[] memory) {
address[] memory validators = new address[](_numValidators);
for (uint256 i = 0; i < _numValidators; i++) {
validators[i] = vm.addr(i+1);
validators[i] = vm.addr(i + 1);
}
return validators;
}

function generateShares(address[] memory validators) pure internal returns(uint256[] memory){
function generateShares(
address[] memory validators
) internal pure returns (uint256[] memory) {
//generate a random number of shares for each validator
uint256[] memory shares = new uint256[](validators.length);
for (uint256 i; i < shares.length; ++i) {
uint256 share = uint256(
keccak256(abi.encodePacked(i, validators[i]))) % 100;
keccak256(abi.encodePacked(i, validators[i]))
) % 100;
shares[i] = (share > 0) ? share : validators.length;
}
return shares;
}

function decodeFactoryLogs(address[] memory _quorumValidators, Quorum quorum) internal {
function decodeFactoryLogs(
address[] memory _quorumValidators,
Quorum quorum
) internal {
Vm.Log[] memory entries = vm.getRecordedLogs();


for (uint i=0;i<entries.length;i++) {
for (uint i = 0; i < entries.length; i++) {
Vm.Log memory entry = entries[i];

if (entry.topics[0] == QuorumCreated.selector && entry.emitter == address(factory)) {
if (
entry.topics[0] == QuorumCreated.selector &&
entry.emitter == address(factory)
) {
QuorumCreatedEvent memory quorumEvent;
(quorumEvent.quorumValidators, quorumEvent.quorum) = abi.decode(entry.data, (address[], Quorum));
(quorumEvent.quorumValidators, quorumEvent.quorum) = abi.decode(
entry.data,
(address[], Quorum)
);

//Validators length in decoded data should match the data passed to the event
assertEq(_quorumValidators.length, quorumEvent.quorumValidators.length);
assertEq(
_quorumValidators.length,
quorumEvent.quorumValidators.length
);

//Compare each validator
for (uint j = 0; j < quorumEvent.quorumValidators.length; j++) {
assertEq(_quorumValidators[j], quorumEvent.quorumValidators[j]);
assertEq(
_quorumValidators[j],
quorumEvent.quorumValidators[j]
);
}

//Compare quorum address
Expand All @@ -138,5 +164,3 @@ contract QuorumFactoryTest is TestBase {
}
}
}


0 comments on commit 1987720

Please sign in to comment.