-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Certora] Check supply followed by withdraw is not profitable #345
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,4 @@ jobs: | |
- verifyBlueExitLiquidity.sh | ||
- verifyBlueTransfer.sh | ||
- verifyReentrancy.sh | ||
- verifyDifficultMath.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
|
||
set -euxo pipefail | ||
|
||
certoraRun \ | ||
certora/harness/MorphoHarness.sol \ | ||
src/mocks/OracleMock.sol \ | ||
--verify MorphoHarness:certora/specs/DifficultMath.spec \ | ||
--loop_iter 3 \ | ||
--optimistic_loop \ | ||
--msg "Morpho Difficult Math" \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
methods { | ||
function getMarketId(MorphoHarness.Market) external returns MorphoHarness.Id envfree; | ||
function getVirtualTotalSupply(MorphoHarness.Id) external returns uint256 envfree; | ||
function getVirtualTotalSupplyShares(MorphoHarness.Id) external returns uint256 envfree; | ||
function MathLib.mulDivDown(uint256 a, uint256 b, uint256 c) internal returns uint256 => summaryMulDivDown(a,b,c); | ||
function MathLib.mulDivUp(uint256 a, uint256 b, uint256 c) internal returns uint256 => summaryMulDivUp(a,b,c); | ||
function SafeTransferLib.safeTransfer(address token, address to, uint256 value) internal => NONDET; | ||
function SafeTransferLib.safeTransferFrom(address token, address from, address to, uint256 value) internal => NONDET; | ||
function _.onMorphoSupply(uint256 assets, bytes data) external => HAVOC_ECF; | ||
} | ||
|
||
function summaryMulDivUp(uint256 x, uint256 y, uint256 d) returns uint256 { | ||
return require_uint256((x * y + (d - 1)) / d); | ||
} | ||
|
||
function summaryMulDivDown(uint256 x, uint256 y, uint256 d) returns uint256 { | ||
return require_uint256((x * y) / d); | ||
} | ||
|
||
/* There should be no profit from supply followed immediately by withdraw */ | ||
rule supplyWithdraw() { | ||
MorphoHarness.Market market; | ||
uint256 assets; | ||
uint256 shares; | ||
uint256 suppliedAssets; | ||
uint256 withdrawnAssets; | ||
uint256 suppliedShares; | ||
uint256 withdrawnShares; | ||
address onbehalf; | ||
address receiver; | ||
bytes data; | ||
env e1; | ||
env e2; | ||
|
||
require e1.block.timestamp == e2.block.timestamp; | ||
|
||
suppliedAssets, suppliedShares = supply(e1, market, assets, shares, onbehalf, data); | ||
|
||
MorphoHarness.Id id = getMarketId(market); | ||
assert suppliedAssets * (getVirtualTotalSupplyShares(id) - suppliedShares) >= suppliedShares * (getVirtualTotalSupply(id) - suppliedAssets); | ||
assert suppliedAssets * getVirtualTotalSupplyShares(id) >= suppliedShares * getVirtualTotalSupply(id); | ||
|
||
withdrawnAssets, withdrawnShares = withdraw(e2, market, 0, suppliedShares, onbehalf, receiver); | ||
|
||
assert withdrawnShares == suppliedShares; | ||
assert withdrawnAssets <= suppliedAssets; | ||
} | ||
|
||
/* There should be no profit from withdraw followed immediately by supply */ | ||
rule withdrawSupply() { | ||
MorphoHarness.Market market; | ||
uint256 assets; | ||
uint256 shares; | ||
uint256 suppliedAssets; | ||
uint256 withdrawnAssets; | ||
uint256 suppliedShares; | ||
uint256 withdrawnShares; | ||
address onbehalf; | ||
address receiver; | ||
bytes data; | ||
env e1; | ||
env e2; | ||
|
||
require e1.block.timestamp == e2.block.timestamp; | ||
|
||
withdrawnAssets, withdrawnShares = withdraw(e2, market, assets, shares, onbehalf, receiver); | ||
|
||
MorphoHarness.Id id = getMarketId(market); | ||
assert withdrawnAssets * (getVirtualTotalSupplyShares(id) + withdrawnShares) <= withdrawnShares * (getVirtualTotalSupply(id) + withdrawnAssets); | ||
assert withdrawnAssets * getVirtualTotalSupplyShares(id) <= withdrawnShares * getVirtualTotalSupply(id); | ||
|
||
suppliedAssets, suppliedShares = supply(e1, market, withdrawnAssets, 0, onbehalf, data); | ||
|
||
assert suppliedAssets == withdrawnAssets && withdrawnShares >= suppliedShares; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we may want to also verify that property by letting the user choose the amount to withdraw. This seems out of reach for now because of the heavy math involved in these rules, making the prover timeout