Skip to content

Commit

Permalink
feat: quorum factory modification and quorum factory tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Ortega committed Oct 9, 2023
1 parent 3f5cede commit d1c0601
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 35 deletions.
Binary file removed onchain/.yarn/install-state.gz
Binary file not shown.
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);
}
}
10 changes: 7 additions & 3 deletions onchain/rollups/contracts/consensus/quorum/QuorumFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract QuorumFactory is IQuorumFactory {
uint256[] calldata _shares,
IHistory _history
) external override returns (Quorum) {
Quorum quorum = new Quorum(_quorumValidators,_shares,_history);
Quorum quorum = new Quorum(_quorumValidators, _shares, _history);

emit QuorumCreated(_quorumValidators, quorum);

Expand All @@ -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 {
)
);
}
}
}
102 changes: 72 additions & 30 deletions onchain/rollups/test/foundry/consensus/quorum/QuorumFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ import {TestBase} from "../../util/TestBase.sol";
import "forge-std/console.sol";

contract QuorumFactoryTest is TestBase {

QuorumFactory factory;

event QuorumCreated(address[] quorumValidators, Quorum quorum);

struct QuorumCreatedEvent {
address[] quorumValidators;
Quorum quorum;
}

function setUp() public {
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 @@ -41,40 +43,50 @@ contract QuorumFactoryTest is TestBase {

emit QuorumCreated(quorumValidators, quorum);

//assertEq(quorum.getHistory() == bytes32(history);
//decodeFactoryLogs(quorumValidators, quorum);
decodeFactoryLogs(quorumValidators, quorum);
}

function testNewQuorumDeterministic(
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);

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

function testAlreadyDeployedNewQuorumDeterministic(
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 @@ -87,38 +99,68 @@ contract QuorumFactoryTest is TestBase {
vm.expectRevert();
factory.newQuorum(quorumValidators, shares, history, _salt);
}

// HELPER FUNCTIONS
function generateValidators(uint256 _numValidators) 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) 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();

address[] memory a;
address b;
(a, b) = abi.decode(entries[0].data, (address[], address));
assertEq(_quorumValidators, a); //entry.emitter == address(factory)
assertEq(address(quorum), b);
}*/
for (uint i = 0; i < entries.length; i++) {
Vm.Log memory entry = entries[i];

if (
entry.topics[0] == QuorumCreated.selector &&
entry.emitter == address(factory)
) {
QuorumCreatedEvent memory quorumEvent;
(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
);

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

//Compare quorum address
assertEq(address(quorum), address(quorumEvent.quorum));
}
}
}
}


0 comments on commit d1c0601

Please sign in to comment.