|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +import "Health.spec"; |
| 3 | + |
| 4 | +// Check that without accruing interest, no interaction can put an healthy account into an unhealthy one. |
| 5 | +// The liquidate function times out in this rule, but has been checked separately. |
| 6 | +rule stayHealthy(env e, method f, calldataarg data) |
| 7 | +filtered { |
| 8 | + f -> !f.isView && |
| 9 | + f.selector != sig:liquidate(MorphoHarness.MarketParams, address, uint256, uint256, bytes).selector |
| 10 | +} |
| 11 | +{ |
| 12 | + MorphoHarness.MarketParams marketParams; |
| 13 | + MorphoHarness.Id id = libId(marketParams); |
| 14 | + address user; |
| 15 | + |
| 16 | + // Assume that the position is healthy before the interaction. |
| 17 | + require isHealthy(marketParams, user); |
| 18 | + // Safe require because of the invariants onlyEnabledLltv and lltvSmallerThanWad in ConsistentState.spec. |
| 19 | + require marketParams.lltv < 10^18; |
| 20 | + // Assumption to ensure that no interest is accumulated. |
| 21 | + require lastUpdate(id) == e.block.timestamp; |
| 22 | + |
| 23 | + f(e, data); |
| 24 | + |
| 25 | + // Safe require because of the invariant sumBorrowSharesCorrect. |
| 26 | + require borrowShares(id, user) <= totalBorrowShares(id); |
| 27 | + |
| 28 | + bool stillHealthy = isHealthy(marketParams, user); |
| 29 | + assert !priceChanged => stillHealthy; |
| 30 | +} |
| 31 | + |
| 32 | +// The liquidate case for the stayHealthy rule, assuming no bad debt realization, otherwise it times out. |
| 33 | +// This particular rule makes the following assumptions: |
| 34 | +// - the market of the liquidation is the market of the user, see the *DifferentMarkets rule, |
| 35 | +// - there is still some borrow on the market after liquidation, see the *LastBorrow rule. |
| 36 | +rule stayHealthyLiquidate(env e, MorphoHarness.MarketParams marketParams, address borrower, uint256 seizedAssets, bytes data) { |
| 37 | + MorphoHarness.Id id = libId(marketParams); |
| 38 | + address user; |
| 39 | + |
| 40 | + // Assume the invariant initially. |
| 41 | + require isHealthy(marketParams, user); |
| 42 | + |
| 43 | + uint256 debtSharesBefore = borrowShares(id, user); |
| 44 | + uint256 debtAssetsBefore = summaryMulDivUp(debtSharesBefore, virtualTotalBorrowAssets(id), virtualTotalBorrowShares(id)); |
| 45 | + // Safe require because of the invariants onlyEnabledLltv and lltvSmallerThanWad in ConsistentState.spec. |
| 46 | + require marketParams.lltv < 10^18; |
| 47 | + // Assumption to ensure that no interest is accumulated. |
| 48 | + require lastUpdate(id) == e.block.timestamp; |
| 49 | + |
| 50 | + liquidate(e, marketParams, borrower, seizedAssets, 0, data); |
| 51 | + require !priceChanged; |
| 52 | + |
| 53 | + // Safe require because of the invariant sumBorrowSharesCorrect. |
| 54 | + require borrowShares(id, user) <= totalBorrowShares(id); |
| 55 | + // Assume that there is still some borrow on the market after liquidation. |
| 56 | + require totalBorrowAssets(id) > 0; |
| 57 | + // Assume no bad debt realization. |
| 58 | + require collateral(id, borrower) > 0; |
| 59 | + |
| 60 | + assert user != borrower; |
| 61 | + assert debtSharesBefore == borrowShares(id, user); |
| 62 | + assert debtAssetsBefore >= summaryMulDivUp(debtSharesBefore, virtualTotalBorrowAssets(id), virtualTotalBorrowShares(id)); |
| 63 | + |
| 64 | + bool stillHealthy = isHealthy(marketParams, user); |
| 65 | + require !priceChanged; |
| 66 | + assert stillHealthy; |
| 67 | +} |
| 68 | + |
| 69 | +rule stayHealthyLiquidateDifferentMarkets(env e, MorphoHarness.MarketParams marketParams, address borrower, uint256 seizedAssets, bytes data) { |
| 70 | + MorphoHarness.Id id = libId(marketParams); |
| 71 | + address user; |
| 72 | + MorphoHarness.MarketParams liquidationMarketParams; |
| 73 | + |
| 74 | + // Assume the invariant initially. |
| 75 | + require isHealthy(marketParams, user); |
| 76 | + |
| 77 | + // Safe require because of the invariants onlyEnabledLltv and lltvSmallerThanWad in ConsistentState.spec. |
| 78 | + require marketParams.lltv < 10^18; |
| 79 | + // Assumption to ensure that no interest is accumulated. |
| 80 | + require lastUpdate(id) == e.block.timestamp; |
| 81 | + // Assume that the liquidation is on a different market. |
| 82 | + require liquidationMarketParams != marketParams; |
| 83 | + |
| 84 | + liquidate(e, liquidationMarketParams, borrower, seizedAssets, 0, data); |
| 85 | + require !priceChanged; |
| 86 | + |
| 87 | + // Safe require because of the invariant sumBorrowSharesCorrect. |
| 88 | + require borrowShares(id, user) <= totalBorrowShares(id); |
| 89 | + |
| 90 | + bool stillHealthy = isHealthy(marketParams, user); |
| 91 | + require !priceChanged; |
| 92 | + assert stillHealthy; |
| 93 | +} |
| 94 | + |
| 95 | +rule stayHealthyLiquidateLastBorrow(env e, MorphoHarness.MarketParams marketParams, address borrower, uint256 seizedAssets, bytes data) { |
| 96 | + MorphoHarness.Id id = libId(marketParams); |
| 97 | + address user; |
| 98 | + |
| 99 | + // Assume the invariant initially. |
| 100 | + require isHealthy(marketParams, user); |
| 101 | + |
| 102 | + // Safe require because of the invariants onlyEnabledLltv and lltvSmallerThanWad in ConsistentState.spec. |
| 103 | + require marketParams.lltv < 10^18; |
| 104 | + // Assumption to ensure that no interest is accumulated. |
| 105 | + require lastUpdate(id) == e.block.timestamp; |
| 106 | + |
| 107 | + liquidate(e, marketParams, borrower, seizedAssets, 0, data); |
| 108 | + require !priceChanged; |
| 109 | + |
| 110 | + // Safe require because of the invariant sumBorrowSharesCorrect. |
| 111 | + require borrowShares(id, user) <= totalBorrowShares(id); |
| 112 | + // Assume that there is no remaining borrow on the market after liquidation. |
| 113 | + require totalBorrowAssets(id) == 0; |
| 114 | + |
| 115 | + bool stillHealthy = isHealthy(marketParams, user); |
| 116 | + require !priceChanged; |
| 117 | + assert stillHealthy; |
| 118 | +} |
0 commit comments