forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: all handlers & properties against a single-element merkle tree
- Loading branch information
1 parent
95e57d1
commit 1b694c5
Showing
7 changed files
with
135 additions
and
60 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
packages/contracts-bedrock/contracts/test/invariants/balance-claimer/FuzzTest.t.sol
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 |
---|---|---|
@@ -1,11 +1,8 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.15; | ||
|
||
import "forge-std/console.sol"; | ||
|
||
import {BalanceClaimerGuidedHandlers} from "./handlers/guided/BalanceClaimer.t.sol"; | ||
import {BalanceClaimerUnguidedHandlers} from "./handlers/unguided/BalanceClaimer.t.sol"; | ||
import {BalanceClaimerProperties} from "./properties/BalanceClaimer.t.sol"; | ||
import {IErc20BalanceWithdrawer} from "contracts/L1/interfaces/winddown/IErc20BalanceWithdrawer.sol"; | ||
|
||
contract FuzzTest is BalanceClaimerGuidedHandlers, BalanceClaimerUnguidedHandlers, BalanceClaimerProperties {} |
21 changes: 18 additions & 3 deletions
21
...ts-bedrock/contracts/test/invariants/balance-claimer/handlers/guided/BalanceClaimer.t.sol
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 |
---|---|---|
@@ -1,9 +1,24 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.15; | ||
|
||
import {BalanceClaimerSetup} from '../../setup/BalanceClaimer.t.sol'; | ||
import {IErc20BalanceWithdrawer} from "contracts/L1/interfaces/winddown/IErc20BalanceWithdrawer.sol"; | ||
import {BalanceClaimerSetup} from "../../setup/BalanceClaimer.t.sol"; | ||
|
||
contract BalanceClaimerGuidedHandlers is BalanceClaimerSetup { | ||
function handler_fooGuided() external { | ||
} | ||
function handler_claim(uint256 claimIndex) external { | ||
claimIndex = bound(claimIndex, 0, ghost_validClaims.length - 1); | ||
Claim memory claim = ghost_validClaims[claimIndex]; | ||
bytes32[] memory proof = new bytes32[](0); | ||
bytes32 hashedClaim = _hashClaim(claim); | ||
vm.prank(msg.sender); | ||
try balanceClaimer.claim(proof, claim.user, claim.ethAmount, _claimToErc20ClaimArray(claim)) { | ||
ghost_claimed[hashedClaim] = true; | ||
ghost_claimedEther += claim.ethAmount; | ||
for (uint256 i = 0; i < claim.tokens.length; i++) { | ||
ghost_claimedTokens[claim.tokens[i]] += claim.tokenAmounts[i]; | ||
} | ||
} catch { | ||
assert(ghost_claimed[hashedClaim]); | ||
} | ||
} | ||
} |
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
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
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
63 changes: 63 additions & 0 deletions
63
packages/contracts-bedrock/contracts/test/invariants/balance-claimer/setup/Claims.t.sol
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,63 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.15; | ||
|
||
import {IErc20BalanceWithdrawer} from "contracts/L1/interfaces/winddown/IErc20BalanceWithdrawer.sol"; | ||
import {Tokens} from "./Tokens.t.sol"; | ||
|
||
contract Claims is Tokens { | ||
struct Claim { | ||
address user; | ||
uint256 ethAmount; | ||
address[] tokens; | ||
uint256[] tokenAmounts; | ||
} | ||
|
||
bytes32[] internal tree; | ||
Claim[] internal ghost_validClaims; | ||
mapping(bytes32 => bool) internal ghost_claimed; | ||
mapping(bytes32 => bool) internal ghost_claimInTree; | ||
|
||
constructor() { | ||
address user = 0x0000000000000000000000000000000000010000; | ||
address[] memory tokens = new address[](1); | ||
tokens[0] = address(supportedTokens[0]); | ||
uint256[] memory amounts = new uint256[](1); | ||
amounts[0] = 1 ether; | ||
Claim memory claim = Claim({user: user, ethAmount: 1 ether, tokens: tokens, tokenAmounts: amounts}); | ||
ghost_validClaims.push(claim); | ||
ghost_claimInTree[_hashClaim(claim)] = true; | ||
tree.push(_hashClaim(claim)); | ||
} | ||
|
||
function _hashClaim(Claim memory claim) internal pure returns (bytes32) { | ||
IErc20BalanceWithdrawer.Erc20BalanceClaim[] memory erc20Claims = | ||
new IErc20BalanceWithdrawer.Erc20BalanceClaim[](claim.tokens.length); | ||
for (uint256 i = 0; i < claim.tokens.length; i++) { | ||
erc20Claims[i].token = claim.tokens[i]; | ||
erc20Claims[i].balance = claim.tokenAmounts[i]; | ||
} | ||
return keccak256(bytes.concat(keccak256(abi.encode(claim.user, claim.ethAmount, erc20Claims)))); | ||
} | ||
|
||
function _hashClaim(address user, uint256 ethAmount, IErc20BalanceWithdrawer.Erc20BalanceClaim[] memory erc20Claims) | ||
internal | ||
pure | ||
returns (bytes32) | ||
{ | ||
return keccak256(bytes.concat(keccak256(abi.encode(user, ethAmount, erc20Claims)))); | ||
} | ||
|
||
function _claimToErc20ClaimArray(Claim memory claim) | ||
internal | ||
pure | ||
returns (IErc20BalanceWithdrawer.Erc20BalanceClaim[] memory) | ||
{ | ||
IErc20BalanceWithdrawer.Erc20BalanceClaim[] memory erc20Claims = | ||
new IErc20BalanceWithdrawer.Erc20BalanceClaim[](claim.tokens.length); | ||
for (uint256 i = 0; i < claim.tokens.length; i++) { | ||
erc20Claims[i].token = claim.tokens[i]; | ||
erc20Claims[i].balance = claim.tokenAmounts[i]; | ||
} | ||
return erc20Claims; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/contracts-bedrock/contracts/test/invariants/balance-claimer/setup/Tokens.t.sol
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,30 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.15; | ||
|
||
import {MockERC20} from "forge-std/mocks/MockERC20.sol"; | ||
import {IERC20} from "forge-std/interfaces/IERC20.sol"; | ||
|
||
contract FuzzERC20 is MockERC20 { | ||
function mint(address _to, uint256 _amount) public { | ||
_mint(_to, _amount); | ||
} | ||
} | ||
|
||
contract Tokens { | ||
uint8 internal constant TOKENS = 4; | ||
uint256 internal constant INITIAL_BALANCE = 100000e18; | ||
IERC20[] internal supportedTokens; | ||
|
||
mapping(address => uint256) internal ghost_claimedTokens; | ||
uint256 internal ghost_claimedEther; | ||
|
||
constructor() { | ||
for (uint256 i = 0; i < TOKENS; i++) { | ||
// TODO: use bytecode from production tokens | ||
FuzzERC20 token = new FuzzERC20(); | ||
// TODO: use 6 decimals for usdt | ||
token.initialize("name", "symbol", 18); | ||
supportedTokens.push(token); | ||
} | ||
} | ||
} |