Skip to content

Commit

Permalink
test: fix invariant tests after changing contract behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
danielattilasimon committed Aug 29, 2024
1 parent 3f77b6b commit c98e957
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
8 changes: 2 additions & 6 deletions contracts/src/HintHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ contract HintHelpers is IHintHelpers {
IActivePool _activePool,
LatestTroveData memory _trove,
LatestBatchData memory _batch
)
internal
view
returns (uint256)
{
) internal view returns (uint256) {
TroveChange memory newBatchTroveChange;
newBatchTroveChange.appliedRedistBoldDebtGain = _trove.redistBoldDebtGain;
newBatchTroveChange.batchAccruedManagementFee = _batch.accruedManagementFee;
Expand All @@ -265,7 +261,7 @@ contract HintHelpers is IHintHelpers {

if (
_newInterestRate == batch.annualInterestRate
|| block.timestamp >= batch.lastInterestRateAdjTime + INTEREST_RATE_ADJ_COOLDOWN
|| block.timestamp >= trove.lastInterestRateAdjTime + INTEREST_RATE_ADJ_COOLDOWN
) {
return 0;
}
Expand Down
28 changes: 18 additions & 10 deletions contracts/src/test/TestContracts/InvariantsTestHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
address batchManager = _batchManagerOf[j][troveId];
Trove storage trove = _troves[j][troveId];

if (batchManager == address(0)) _timeSinceLastTroveInterestRateAdjustment[j][troveId] += timeDelta;
_timeSinceLastTroveInterestRateAdjustment[j][troveId] += timeDelta;
if (isShutdown[j]) continue; // shutdown branches stop accruing interest & batch management fees

uint256 interest = trove.accrueInterest(timeDelta);
Expand Down Expand Up @@ -872,8 +872,11 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
v.trove = _troves[i][v.troveId];
v.wasActive = _isActive(i, v.troveId);
v.premature = _timeSinceLastTroveInterestRateAdjustment[i][v.troveId] < INTEREST_RATE_ADJ_COOLDOWN;
v.upfrontFee = hintHelpers.predictAdjustInterestRateUpfrontFee(i, v.troveId, newInterestRate);
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");

if (v.batchManager == address(0)) {
v.upfrontFee = hintHelpers.predictAdjustInterestRateUpfrontFee(i, v.troveId, newInterestRate);
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");
}

info("upper hint: ", _hintToString(i, v.upperHint));
info("lower hint: ", _hintToString(i, v.lowerHint));
Expand Down Expand Up @@ -1875,8 +1878,11 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
v.wasOpen = _isOpen(i, v.troveId);
v.wasActive = _isActive(i, v.troveId);
v.premature = _timeSinceLastTroveInterestRateAdjustment[i][v.troveId] < INTEREST_RATE_ADJ_COOLDOWN;
v.upfrontFee = hintHelpers.predictJoinBatchInterestRateUpfrontFee(i, v.troveId, v.newBatchManager);
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");

if (_batchManagerOf[i][v.troveId] == address(0)) {
v.upfrontFee = hintHelpers.predictJoinBatchInterestRateUpfrontFee(i, v.troveId, v.newBatchManager);
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");
}

info("batch manager: ", vm.getLabel(v.newBatchManager));
info("upper hint: ", _hintToString(i, v.upperHint));
Expand Down Expand Up @@ -1991,11 +1997,12 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
v.batchManager = _batchManagerOf[i][v.troveId];
v.batchManagementFee = v.c.troveManager.getLatestBatchData(v.batchManager).accruedManagementFee;
v.wasActive = _isActive(i, v.troveId);
v.premature = _timeSinceLastBatchInterestRateAdjustment[i][v.batchManager] < INTEREST_RATE_ADJ_COOLDOWN;
v.upfrontFee = v.batchManager != address(0)
? hintHelpers.predictRemoveFromBatchUpfrontFee(i, v.troveId, newInterestRate)
: 0;
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");
v.premature = _timeSinceLastTroveInterestRateAdjustment[i][v.troveId] < INTEREST_RATE_ADJ_COOLDOWN;

if (v.batchManager != address(0)) {
v.upfrontFee = hintHelpers.predictRemoveFromBatchUpfrontFee(i, v.troveId, newInterestRate);
if (v.upfrontFee > 0) assertTrue(v.premature, "Only premature adjustment should incur upfront fee");
}

Trove memory trove = _troves[i][v.troveId];
Batch storage batch = _batches[i][v.batchManager];
Expand Down Expand Up @@ -2035,6 +2042,7 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
trove.interestRate = newInterestRate;
trove.batchManagementRate = 0;
_troves[i][v.troveId] = trove;
_timeSinceLastTroveInterestRateAdjustment[i][v.troveId] = 0;
delete _batchManagerOf[i][v.troveId];

// Effects (batch)
Expand Down

0 comments on commit c98e957

Please sign in to comment.