From a345956b262c3f6932091f1e4bd8a1b7a00d73c9 Mon Sep 17 00:00:00 2001 From: Gas One Cent <86567384+gas1cent@users.noreply.github.com> Date: Mon, 11 Dec 2023 17:48:04 +0400 Subject: [PATCH] test: uncomment and refactor `pledgeForDispute` tests --- .../BondEscalationResolutionModule.t.sol | 296 ++++++++++-------- 1 file changed, 161 insertions(+), 135 deletions(-) diff --git a/solidity/test/unit/modules/resolution/BondEscalationResolutionModule.t.sol b/solidity/test/unit/modules/resolution/BondEscalationResolutionModule.t.sol index baf9f54e..2373ca7c 100644 --- a/solidity/test/unit/modules/resolution/BondEscalationResolutionModule.t.sol +++ b/solidity/test/unit/modules/resolution/BondEscalationResolutionModule.t.sol @@ -112,7 +112,7 @@ contract BaseTest is Test, Helpers { /** * @notice Deploy the target and mock oracle+accounting extension */ - function setUp() public { + function setUp() public virtual { oracle = IOracle(makeAddr('Oracle')); vm.etch(address(oracle), hex'069420'); @@ -219,6 +219,40 @@ contract BondEscalationResolutionModule_Unit_StartResolution is BaseTest { } contract BondEscalationResolutionModule_Unit_PledgeForDispute is BaseTest { + uint256 _percentageDiff = 20; + uint256 _timeUntilDeadline = 1001; + + // block.timestamp < _inequalityData.time + _timeToBreakInequality + uint256 _timeToBreakInequality = 5000; + uint128 _startTime; + + bytes32 _disputeId; + bytes32 _requestId; + + function setUp() public override { + super.setUp(); + + // Start at a later time to be able to travel back + vm.warp(block.timestamp + _timeToBreakInequality + 1); + + // block.timestamp < _startTime + _timeUntilDeadline + _startTime = uint128(block.timestamp - 1000); + + mockRequest.resolutionModuleData = abi.encode( + IBondEscalationResolutionModule.RequestParameters({ + accountingExtension: accounting, + bondToken: token, + percentageDiff: _percentageDiff, + pledgeThreshold: 1, + timeUntilDeadline: _timeUntilDeadline, + timeToBreakInequality: _timeToBreakInequality + }) + ); + + _disputeId = _getId(mockDispute); + _requestId = mockDispute.requestId; + } + function test_reverts( uint256 _pledgeAmount, IBondEscalationResolutionModule.RequestParameters memory _params @@ -226,9 +260,9 @@ contract BondEscalationResolutionModule_Unit_PledgeForDispute is BaseTest { /////////////////////////////////////////////////////// BondEscalationResolutionModule_NotEscalated mockRequest.resolutionModuleData = abi.encode(_params); - bytes32 _requestId = _getId(mockRequest); + _requestId = _getId(mockRequest); mockDispute.requestId = _requestId; - bytes32 _disputeId = _getId(mockDispute); + _disputeId = _getId(mockDispute); // Mock escalation with start time 0 module.forTest_setEscalation(_disputeId, IBondEscalationResolutionModule.Resolution.Unresolved, 0, 0, 0); @@ -303,10 +337,6 @@ contract BondEscalationResolutionModule_Unit_PledgeForDispute is BaseTest { vm.assume(_pledgeAmount < type(uint256).max - 1000); IBondEscalationResolutionModule.Resolution _resolution = IBondEscalationResolutionModule.Resolution.Unresolved; - // block.timestamp < _startTime + _timeUntilDeadline - uint128 _startTime = uint128(block.timestamp - 1000); - _params.timeUntilDeadline = 1001; - // _pledgeThreshold > _updatedTotalVotes; uint256 _pledgesFor = 1000; uint256 _pledgesAgainst = 1000; @@ -314,19 +344,18 @@ contract BondEscalationResolutionModule_Unit_PledgeForDispute is BaseTest { // block.timestamp < _inequalityData.time + _timeToBreakInequality _params.timeToBreakInequality = 5000; + _params.timeUntilDeadline = 1001; mockRequest.resolutionModuleData = abi.encode(_params); - bytes32 _requestId = _getId(mockRequest); + _requestId = _getId(mockRequest); mockDispute.requestId = _requestId; - bytes32 _disputeId = _getId(mockDispute); - - // assuming the threshold has not passed, this is the only valid state - IBondEscalationResolutionModule.InequalityStatus _inequalityStatus = - IBondEscalationResolutionModule.InequalityStatus.Equalized; + _disputeId = _getId(mockDispute); - // set all data + // Set all data module.forTest_setEscalation(_disputeId, _resolution, _startTime, _pledgesFor, _pledgesAgainst); - module.forTest_setInequalityData(_disputeId, _inequalityStatus, block.timestamp); + module.forTest_setInequalityData( + _disputeId, IBondEscalationResolutionModule.InequalityStatus.Equalized, block.timestamp + ); // Mock and expect IBondEscalationAccounting.pledge to be called _mockAndExpect( @@ -355,147 +384,144 @@ contract BondEscalationResolutionModule_Unit_PledgeForDispute is BaseTest { assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.Equalized)); } - // TODO: Break this test into several smaller functions - // function test_percentageDifferences(bytes32 _requestId, bytes32 _disputeId, uint256 _pledgeAmount, IOracle.Request calldata _request) public { - // ////////////////////////////////////////////////////////////////////// - // // START TEST _forPercentageDifference >= _scaledPercentageDiffAsInt - // ////////////////////////////////////////////////////////////////////// - - // _pledgeAmount = bound(_pledgeAmount, 1, type(uint192).max); - // IBondEscalationResolutionModule.Resolution _resolution = IBondEscalationResolutionModule.Resolution.Unresolved; - - // // block.timestamp < _startTime + _timeUntilDeadline - // uint128 _startTime = uint128(block.timestamp - 1000); - // uint256 _timeUntilDeadline = 1001; - - // // I'm setting the values so that the percentage diff is 20% in favor of pledgesFor. - // // In this case, _pledgeAmount will be the entirety of pledgesFor, as if it were the first pledge. - // // Therefore, _pledgeAmount must be 60% of total votes, _pledgesAgainst then should be 40% - // // 40 = 60 * 2 / 3 -> thats why I'm multiplying by 200 and dividing by 300 - // uint256 _pledgesFor = 0; - // uint256 _pledgesAgainst = _pledgeAmount * 200 / 300; - // uint256 _percentageDiff = 20; - - // // block.timestamp < _inequalityData.time + _timeToBreakInequality - // uint256 _timeToBreakInequality = 5000; - - // IBondEscalationResolutionModule.InequalityStatus _inequalityStatus = - // IBondEscalationResolutionModule.InequalityStatus.Equalized; - - // // Set all data - // module.forTest_setEscalation(_disputeId, _resolution, _startTime, _pledgesFor, _pledgesAgainst); - // module.forTest_setInequalityData(_disputeId, _inequalityStatus, block.timestamp); - - // // Mock and expect IBondEscalationAccounting.pledge to be called - // _mockAndExpect( - // address(accounting), - // abi.encodeCall(IBondEscalationAccounting.pledge, (pledgerFor, _requestId, _disputeId, token, _pledgeAmount)), - // abi.encode() - // ); - - // // Check: is the event emitted? - // vm.expectEmit(true, true, true, true, address(module)); - // emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); - - // vm.startPrank(pledgerFor); - // module.pledgeForDispute(_request, mockDispute, _pledgeAmount); - - // (,, uint256 _realPledgesFor,) = module.escalations(_disputeId); - // (IBondEscalationResolutionModule.InequalityStatus _status, uint256 _timer) = module.inequalityData(_disputeId); - - // // Check: is the pledge amount added to the total? - // assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); - // // Check: is the pledge for dispute amount updated? - // assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); - // // Check: is the status properly updated? - // assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.AgainstTurnToEqualize)); - // // Check: is the timer properly updated to current timestamp? - // assertEq(uint256(_timer), block.timestamp); + /** + * @notice Testing _forPercentageDifference >= _scaledPercentageDiffAsInt + */ + function test_changesStatusIfForSideIsWinning(uint256 _pledgeAmount) public { + _pledgeAmount = bound(_pledgeAmount, 1, type(uint192).max); - // //////////////////////////////////////////////////////////////////// - // // END TEST _forPercentageDifference >= _scaledPercentageDiffAsInt - // //////////////////////////////////////////////////////////////////// + // I'm setting the values so that the percentage diff is 20% in favor of pledgesFor. + // In this case, _pledgeAmount will be the entirety of pledgesFor, as if it were the first pledge. + // Therefore, _pledgeAmount must be 60% of total votes, _pledgesAgainst then should be 40% + // 40 = 60 * 2 / 3 -> thats why I'm multiplying by 200 and dividing by 300 + uint256 _pledgesFor = 0; + uint256 _pledgesAgainst = _pledgeAmount * 200 / 300; - // //----------------------------------------------------------------------// + // Set all data + module.forTest_setEscalation( + _disputeId, IBondEscalationResolutionModule.Resolution.Unresolved, _startTime, _pledgesFor, _pledgesAgainst + ); + module.forTest_setInequalityData( + _disputeId, IBondEscalationResolutionModule.InequalityStatus.Equalized, block.timestamp + ); - // ////////////////////////////////////////////////////////////////////////// - // // START TEST _againstPercentageDifference >= _scaledPercentageDiffAsInt - // ///////////////////////////////////////////////////////////////////////// + // Mock and expect IBondEscalationAccounting.pledge to be called + _mockAndExpect( + address(accounting), + abi.encodeCall(IBondEscalationAccounting.pledge, (pledgerFor, _requestId, _disputeId, token, _pledgeAmount)), + abi.encode() + ); - // // Resetting status changed by previous test - // _inequalityStatus = IBondEscalationResolutionModule.InequalityStatus.ForTurnToEqualize; - // module.forTest_setInequalityData(_disputeId, _inequalityStatus, block.timestamp); - // module.forTest_setPledgesFor(_disputeId, pledgerFor, 0); + // Check: is the event emitted? + vm.expectEmit(true, true, true, true, address(module)); + emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); - // // Making the against percentage 60% of the total as percentageDiff is 20% - // // Note: I'm using 301 to account for rounding down errors. I'm also setting some _pledgesFor - // // to avoid the case when pledges are at 0 and someone just pledges 1 token - // // which is not realistic due to the pledgeThreshold forbidding the lines tested here - // // to be reached. - // _pledgesFor = 100_000; - // _pledgesAgainst = (_pledgeAmount + _pledgesFor) * 301 / 200; + vm.startPrank(pledgerFor); + module.pledgeForDispute(mockRequest, mockDispute, _pledgeAmount); - // // Resetting the pledges values - // module.forTest_setEscalation(_disputeId, _resolution, _startTime, _pledgesFor, _pledgesAgainst); + (,, uint256 _realPledgesFor,) = module.escalations(_disputeId); + (IBondEscalationResolutionModule.InequalityStatus _status, uint256 _timer) = module.inequalityData(_disputeId); - // // Check: is the event emitted? - // vm.expectEmit(true, true, true, true, address(module)); - // emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); + // Check: is the pledge amount added to the total? + assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); + // Check: is the pledge for dispute amount updated? + assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); + // Check: is the status properly updated? + assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.AgainstTurnToEqualize)); + // Check: is the timer properly updated to current timestamp? + assertEq(uint256(_timer), block.timestamp); + } - // module.pledgeForDispute(_request, mockDispute, _pledgeAmount); + /** + * @notice Testing _againstPercentageDifference >= _scaledPercentageDiffAsInt + */ + function test_changesStatusIfAgainstSideIsWinning(uint256 _pledgeAmount) public { + _pledgeAmount = bound(_pledgeAmount, 1, type(uint192).max); + + // Making the against percentage 60% of the total as percentageDiff is 20% + // Note: I'm using 301 to account for rounding down errors. I'm also setting some _pledgesFor + // to avoid the case when pledges are at 0 and someone just pledges 1 token + // which is not realistic due to the pledgeThreshold forbidding the lines tested here + // to be reached. + uint256 _pledgesFor = 100_000; + uint256 _pledgesAgainst = (_pledgeAmount + _pledgesFor) * 301 / 200; + + // Set the data + module.forTest_setEscalation( + _disputeId, IBondEscalationResolutionModule.Resolution.Unresolved, _startTime, _pledgesFor, _pledgesAgainst + ); + module.forTest_setInequalityData( + _disputeId, IBondEscalationResolutionModule.InequalityStatus.ForTurnToEqualize, block.timestamp + ); - // (,, _realPledgesFor,) = module.escalations(_disputeId); - // (_status, _timer) = module.inequalityData(_disputeId); + // Mock and expect IBondEscalationAccounting.pledge to be called + _mockAndExpect( + address(accounting), + abi.encodeCall(IBondEscalationAccounting.pledge, (pledgerFor, _requestId, _disputeId, token, _pledgeAmount)), + abi.encode() + ); - // // Check: is the pledge amount added to the total? - // assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); - // // Check: is the pledges for amount updated? - // assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); - // // Check: is the status properly updated? - // assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.ForTurnToEqualize)); + // Check: is the event emitted? + vm.expectEmit(true, true, true, true, address(module)); + emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); - // //////////////////////////////////////////////////////////////////// - // // END TEST _forPercentageDifference >= _scaledPercentageDiffAsInt - // //////////////////////////////////////////////////////////////////// + vm.prank(pledgerFor); + module.pledgeForDispute(mockRequest, mockDispute, _pledgeAmount); - // //----------------------------------------------------------------------// + (,, uint256 _realPledgesFor,) = module.escalations(_disputeId); + (IBondEscalationResolutionModule.InequalityStatus _status, uint256 _timer) = module.inequalityData(_disputeId); - // ////////////////////////////////////////////////////////////////////////// - // // START TEST _status == forTurnToEqualize && both diffs < percentageDiff - // ///////////////////////////////////////////////////////////////////////// + // Check: is the pledge amount added to the total? + assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); + // Check: is the pledges for amount updated? + assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); + // Check: is the status properly updated? + assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.ForTurnToEqualize)); + // Check: is the timer properly updated to current timestamp? + assertEq(uint256(_timer), block.timestamp); + } - // // Resetting status changed by previous test - // module.forTest_setPledgesFor(_disputeId, pledgerFor, 0); + /** + * @notice Testing _status == forTurnToEqualize && both diffs < percentageDiff + */ + function test_changesStatusIfSidesAreEqual(uint256 _pledgeAmount) public { + _pledgeAmount = bound(_pledgeAmount, 1, type(uint192).max); - // // Making both the same so the percentage diff is not reached - // _pledgesFor = 100_000; - // _pledgesAgainst = (_pledgeAmount + _pledgesFor); + // Making both the same so the percentage diff is not reached + uint256 _pledgesFor = 100_000; + uint256 _pledgesAgainst = (_pledgeAmount + _pledgesFor); - // // Resetting the pledges values - // module.forTest_setEscalation(_disputeId, _resolution, _startTime, _pledgesFor, _pledgesAgainst); + // Resetting the pledges values + module.forTest_setEscalation( + _disputeId, IBondEscalationResolutionModule.Resolution.Unresolved, _startTime, _pledgesFor, _pledgesAgainst + ); - // // Check: is the event emitted? - // vm.expectEmit(true, true, true, true, address(module)); - // emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); + // Mock and expect IBondEscalationAccounting.pledge to be called + _mockAndExpect( + address(accounting), + abi.encodeCall(IBondEscalationAccounting.pledge, (pledgerFor, _requestId, _disputeId, token, _pledgeAmount)), + abi.encode() + ); - // module.pledgeForDispute(_request, mockDispute, _pledgeAmount); + // Check: is the event emitted? + vm.expectEmit(true, true, true, true, address(module)); + emit PledgedForDispute(pledgerFor, _requestId, _disputeId, _pledgeAmount); - // (,, _realPledgesFor,) = module.escalations(_disputeId); - // (_status, _timer) = module.inequalityData(_disputeId); + vm.prank(pledgerFor); + module.pledgeForDispute(mockRequest, mockDispute, _pledgeAmount); - // // Check: is the pledge amount added to the total? - // assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); - // // Check: is the pledges for amount updated? - // assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); - // // Check: is the status properly updated? - // assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.Equalized)); - // // Check: is the timer reset? - // assertEq(_timer, 0); + (,, uint256 _realPledgesFor,) = module.escalations(_disputeId); + (IBondEscalationResolutionModule.InequalityStatus _status, uint256 _timer) = module.inequalityData(_disputeId); - // ////////////////////////////////////////////////////////////////////////// - // // END TEST _status == forTurnToEqualize && both diffs < percentageDiff - // ///////////////////////////////////////////////////////////////////////// - // } + // Check: is the pledge amount added to the total? + assertEq(_realPledgesFor, _pledgesFor + _pledgeAmount); + // Check: is the pledges for amount updated? + assertEq(module.pledgesForDispute(_disputeId, pledgerFor), _pledgeAmount); + // Check: is the status properly updated? + assertEq(uint256(_status), uint256(IBondEscalationResolutionModule.InequalityStatus.Equalized)); + // Check: is the timer reset? + assertEq(_timer, 0); + } } contract BondEscalationResolutionModule_Unit_PledgeAgainstDispute is BaseTest {