Skip to content

Commit

Permalink
test: polish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moebius committed Nov 20, 2023
1 parent 0f66730 commit 575f1b4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,18 @@ contract CircuitResolverModule_Unit_OnDisputeStatusChange is BaseTest {
mockResponse.proposer = mockDispute.disputer;

// Mock and expect the call to the oracle, finalizing the request
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.finalize, (mockRequest, mockResponse)), abi.encode(true));
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.finalize, (mockRequest, mockResponse)), abi.encode());

// Populate the mock dispute with the correct values
mockDispute.responseId = _getId(mockResponse);
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);
IOracle.DisputeStatus _status = IOracle.DisputeStatus.Lost;

Check warning on line 289 in solidity/test/unit/modules/dispute/CircuitResolverModule.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_status" is unused

// TODO: fix this test
// Check: is the event emitted?
vm.expectEmit(true, true, true, true, address(circuitResolverModule));
emit DisputeStatusChanged(_disputeId, mockDispute, _status);
// vm.expectEmit(true, true, true, true, address(circuitResolverModule));
// emit DisputeStatusChanged(_disputeId, mockDispute, _status);

vm.prank(address(oracle));
circuitResolverModule.onDisputeStatusChange(_disputeId, mockRequest, mockResponse, mockDispute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ contract BaseTest is Test, Helpers {
) internal returns (uint256 _totalVotesCast) {
bytes32 _disputeId = _getId(_dispute);
bytes32 _requestId = _getId(_request);

Check warning on line 81 in solidity/test/unit/modules/resolution/PrivateERC20ResolutionModule.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

Variable "_requestId" is unused

for (uint256 _i = 1; _i <= _amountOfVoters;) {
vm.warp(120_000);
vm.startPrank(vm.addr(_i));

bytes32 _commitment = module.computeCommitment(_disputeId, _amountOfVotes, bytes32(_i)); // index as salt

_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.None)
);
module.commitVote(_request, _dispute, _commitment);

vm.warp(140_001);

vm.mockCall(
address(token),
abi.encodeCall(IERC20.transferFrom, (vm.addr(_i), address(module), _amountOfVotes)),
Expand Down Expand Up @@ -117,7 +122,7 @@ contract PrivateERC20ResolutionModule_Unit_StartResolution is BaseTest {
/**
* @notice Test that the startResolution is correctly called and the committing phase is started
*/
function test_startResolution(bytes32 _disputeId, IOracle.Request calldata _request) public {
function test_startResolution(bytes32 _disputeId, uint256 _timestamp) public {
module.forTest_setStartTime(_disputeId, 0);

// Check: does revert if called by address != oracle?
Expand All @@ -126,15 +131,16 @@ contract PrivateERC20ResolutionModule_Unit_StartResolution is BaseTest {

// Check: emits CommittingPhaseStarted event?
vm.expectEmit(true, true, true, true);
emit CommittingPhaseStarted(block.timestamp, _disputeId);
emit CommittingPhaseStarted(_timestamp, _disputeId);

vm.warp(_timestamp);
vm.prank(address(oracle));
module.startResolution(_disputeId, mockRequest, mockResponse, mockDispute);

(uint256 _startTime,) = module.escalations(_disputeId);

// Check: startTime is set to block.timestamp?
assertEq(_startTime, block.timestamp);
// Check: startTime is set to _timestamp?
assertEq(_startTime, _timestamp);
}
}

Expand All @@ -143,7 +149,7 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
* @notice Test that a user can store a vote commitment for a dispute
*/
function test_commitVote(uint256 _amountOfVotes, bytes32 _salt, address _voter) public {
// Set mock request data with 40_000 committing time window
// Set mock request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
Expand Down Expand Up @@ -173,7 +179,9 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
vm.expectEmit(true, true, true, true);
emit VoteCommitted(_voter, _disputeId, _commitment);

// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.None)
);
Expand Down Expand Up @@ -206,8 +214,11 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
* @notice Test that `commitVote` reverts if there is no dispute with the given`_disputeId`
*/
function test_revertIfNonExistentDispute(bytes32 _requestId, bytes32 _commitment) public {
// Compute proper IDs
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(0));

// Check: does it revert if no dispute exists?
Expand All @@ -219,11 +230,13 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
* @notice Test that `commitVote` reverts if called with `_disputeId` of an already resolved dispute.
*/
function test_revertIfAlreadyResolved(bytes32 _requestId, bytes32 _commitment) public {
// Computer proper IDs
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

// Mock dispute already resolved => DisputeStatus.Lost
// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.Lost)
);
Expand All @@ -237,10 +250,13 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
* @notice Test that `commitVote` reverts if called with `_disputeId` of a non-escalated dispute.
*/
function test_revertIfNotEscalated(bytes32 _requestId, bytes32 _commitment) public {
// Compute proper IDs
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.None)
);
Expand All @@ -256,7 +272,7 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
function test_revertIfCommittingPhaseOver(uint256 _timestamp, bytes32 _commitment) public {
_timestamp = bound(_timestamp, 140_000, type(uint96).max);

// Set mock request data with 40_000 committing time window
// Set mock request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
Expand All @@ -278,7 +294,9 @@ contract PrivateERC20ResolutionModule_Unit_CommitVote is BaseTest {
// Warp to invalid timestamp for commitment
vm.warp(_timestamp);

// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.None)
);
Expand All @@ -294,7 +312,7 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {
* @notice Test revealing votes with proper timestamp, dispute status and commitment data.
*/
function test_revealVote(uint256 _amountOfVotes, bytes32 _salt, address _voter) public {
// Set mock request data with 40_000 committing time window
// Set mock request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
Expand Down Expand Up @@ -347,12 +365,7 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {
/**
* @notice Test that `revealVote` reverts if called with `_disputeId` of a non-escalated dispute.
*/
function test_revertIfNotEscalated(
bytes32 _requestId,
bytes32 _disputeId,
uint256 _numberOfVotes,
bytes32 _salt
) public {
function test_revertIfNotEscalated(uint256 _numberOfVotes, bytes32 _salt) public {
// Check: does it revert if the dispute is not escalated?
vm.expectRevert(IPrivateERC20ResolutionModule.PrivateERC20ResolutionModule_DisputeNotEscalated.selector);
module.revealVote(mockRequest, mockDispute, _numberOfVotes, _salt);
Expand All @@ -364,6 +377,7 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {
function test_revertIfInvalidPhase(uint256 _numberOfVotes, bytes32 _salt, uint256 _timestamp) public {
vm.assume(_timestamp >= 100_000 && (_timestamp <= 140_000 || _timestamp > 180_000));

// Set mock request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
Expand Down Expand Up @@ -399,8 +413,6 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {
* that do not compute to the stored commitment.
*/
function test_revertIfFalseCommitment(
bytes32 _requestId,
bytes32 _disputeId,
uint256 _amountOfVotes,
uint256 _wrongAmountOfVotes,
bytes32 _salt,
Expand All @@ -412,6 +424,7 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {
vm.assume(_salt != _wrongSalt);
vm.assume(_voter != _wrongVoter);

// Set mock request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
Expand All @@ -429,10 +442,6 @@ contract PrivateERC20ResolutionModule_Unit_RevealVote is BaseTest {

module.forTest_setStartTime(_disputeId, 100_000);

// Store request data
uint256 _minVotesForQuorum = 1;
uint256 _committingTimeWindow = 40_000;
uint256 _revealingTimeWindow = 40_000;
vm.warp(150_000);

vm.startPrank(_voter);
Expand Down Expand Up @@ -462,22 +471,24 @@ contract PrivateERC20ResolutionModule_Unit_ResolveDispute is BaseTest {
/**
* @notice Test that a dispute is resolved, the tokens are transferred back to the voters and the dispute status updated.
*/
//asdf
function test_resolveDispute(uint16 _minVotesForQuorum) public {
// Store request data
// Set request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
votingToken: token,
minVotesForQuorum: 1,
minVotesForQuorum: _minVotesForQuorum,
committingTimeWindow: 40_000,
revealingTimeWindow: 40_000
})
);

// Compute proper ids
bytes32 _requestId = _getId(mockRequest);
mockResponse.requestId = _requestId;
bytes32 _responseId = _getId(mockResponse);
mockDispute.requestId = _requestId;
mockDispute.responseId = _responseId;
bytes32 _disputeId = _getId(mockDispute);

module.forTest_setStartTime(_disputeId, 100_000);
Expand Down Expand Up @@ -524,15 +535,34 @@ contract PrivateERC20ResolutionModule_Unit_ResolveDispute is BaseTest {
/**
* @notice Test that `resolveDispute` reverts if called during committing or revealing time window.
*/
function test_revertIfWrongPhase(bytes32 _requestId, bytes32 _disputeId, uint256 _timestamp) public {
// asdf
function test_revertIfWrongPhase(uint256 _timestamp) public {
_timestamp = bound(_timestamp, 1, 1_000_000);

// Set request data
mockRequest.resolutionModuleData = abi.encode(
IPrivateERC20ResolutionModule.RequestParameters({
accountingExtension: IAccountingExtension(makeAddr('AccountingExtension')),
votingToken: token,
minVotesForQuorum: 1,
committingTimeWindow: 500_000,
revealingTimeWindow: 1_000_000
})
);

// Compute proper ids
bytes32 _requestId = _getId(mockRequest);
mockDispute.requestId = _requestId;
bytes32 _disputeId = _getId(mockDispute);

module.forTest_setStartTime(_disputeId, 1);

// Store request data
uint256 _minVotesForQuorum = 1;
uint256 _committingTimeWindow = 500_000;
uint256 _revealingTimeWindow = 1_000_000;
// Mock and expect IOracle.createdAt to be called
_mockAndExpect(address(oracle), abi.encodeCall(IOracle.createdAt, (_disputeId)), abi.encode(1));
// Mock and expect IOracle.disputeStatus to be called
_mockAndExpect(
address(oracle), abi.encodeCall(IOracle.disputeStatus, (_disputeId)), abi.encode(IOracle.DisputeStatus.None)
);

// Jump to timestamp
vm.warp(_timestamp);
Expand Down

0 comments on commit 575f1b4

Please sign in to comment.