Skip to content
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

test: initialize echidna for fuzz testing #208

Merged
merged 67 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
1f2625e
initialize gateway
lumtis Jun 18, 2024
bd71007
add receiver example
lumtis Jun 18, 2024
3553a8f
add test examples
lumtis Jun 18, 2024
791834b
add entry for test prototype
lumtis Jun 18, 2024
c1bd47d
typechain
lumtis Jun 18, 2024
6aed43c
change B to non-payable
lumtis Jun 18, 2024
8a973c1
generate
lumtis Jun 18, 2024
d562400
initialize custody contract
lumtis Jun 19, 2024
555ff9d
add execute with erc20 in gateway
lumtis Jun 19, 2024
21b8ace
add test
lumtis Jun 19, 2024
484096a
add some more tests
lumtis Jun 19, 2024
a0c8e5a
initalize uniswap test
lumtis Jun 19, 2024
5b893d2
finish test for uniswap
lumtis Jun 19, 2024
776caef
generate
lumtis Jun 19, 2024
704382b
use custom error
lumtis Jun 19, 2024
5e4766c
add some more comment
lumtis Jun 19, 2024
ce87491
a bit more comments
lumtis Jun 19, 2024
049f956
Merge branch 'main' into init-prototypes
skosito Jun 20, 2024
278c327
chore: change Gateway contract to be upgradable (#175)
skosito Jun 26, 2024
9303311
conflicts
lumtis Jun 27, 2024
cb0ca6c
fix yarn.lock
lumtis Jun 27, 2024
b9e5bfd
feat: inbound evm prototype (#178)
skosito Jun 27, 2024
9a785bc
feat: prototype inbound zevm (#183)
skosito Jul 1, 2024
430992a
feat: prototype outbound zevm (#191)
skosito Jul 1, 2024
a09cc0c
start worker script to deploy evm and zevm contracts on local hardhat…
skosito Jul 1, 2024
b39bc57
add simple script to deposit to gateway evm and listen to event in wo…
skosito Jul 1, 2024
4ee9023
Merge branch 'main' into init-prototypes
skosito Jul 1, 2024
d784b8e
Merge branch 'init-prototypes' into prototype-localnet
skosito Jul 1, 2024
7e3ad3e
fix coderabbit comments
skosito Jul 2, 2024
0b1f894
fix coderabbit comments
skosito Jul 2, 2024
7db93dd
fix yarn generate
skosito Jul 2, 2024
69d5763
Merge branch 'main' into init-prototypes
skosito Jul 2, 2024
022fee2
Merge branch 'init-prototypes' into prototype-localnet
skosito Jul 2, 2024
1e8af88
generate
skosito Jul 2, 2024
d94d8ff
renamings
skosito Jul 2, 2024
290a84e
add simple task to call receiver
skosito Jul 2, 2024
5817d9a
use receiver from call event in worker script
skosito Jul 2, 2024
f7d18fd
add zcontract localnet task
skosito Jul 2, 2024
51ff6a2
cleanup
skosito Jul 2, 2024
b857b9c
rename localnet script to worker
skosito Jul 2, 2024
cbd7dcf
run localnode and worker concurrently
skosito Jul 3, 2024
da9c0cd
different colors and prefix for node and worker
skosito Jul 3, 2024
6bd8c16
Merge branch 'main' into prototype-localnet
skosito Jul 3, 2024
f2a3371
fixes after merge
skosito Jul 3, 2024
cc93e3e
fixes after merge
skosito Jul 3, 2024
5a5f76b
fix tests
skosito Jul 3, 2024
941949e
fix worker after merge
skosito Jul 3, 2024
6275282
add more tasks
skosito Jul 3, 2024
8c33743
wait on hardhat node before starting worker
skosito Jul 3, 2024
fa96e30
remove redundant hardhat config
skosito Jul 3, 2024
b5a665a
generate
skosito Jul 3, 2024
badd4f8
add tasks to coderabbit
skosito Jul 3, 2024
d158c71
improve instructions
skosito Jul 3, 2024
49223b2
exclude in coderabbit
skosito Jul 3, 2024
4275805
test config changes
skosito Jul 3, 2024
caae687
revert
skosito Jul 3, 2024
0c1b370
add try catch
skosito Jul 3, 2024
76f284d
Merge branch 'main' into prototype-localnet
skosito Jul 3, 2024
e62b051
cleanup
skosito Jul 3, 2024
b69926f
add basic property based invariants for gateway evm
skosito Jul 3, 2024
bb27a54
PR comments
skosito Jul 4, 2024
d80596e
yarn generate
skosito Jul 4, 2024
e559c31
Merge branch 'prototype-localnet' into fuzz-tests-poc
skosito Jul 4, 2024
d0ad2f5
add simple assertion tests
skosito Jul 4, 2024
354e45a
move to test folder
skosito Jul 4, 2024
3190026
Merge branch 'main' into fuzz-tests-poc
skosito Jul 4, 2024
4ec838c
Merge branch 'main' into fuzz-tests-poc
lumtis Jul 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ cache
dist
node_modules
typechain-types
docs
docs
crytic-export
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ coverage.json
scripts/slither-results/*
!scripts/slither-results/.gitkeep

abi
abi

crytic-export
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/ERC20CustodyNew.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
// For this, it passes through the Gateway contract, it transfers the tokens to the Gateway contract and then calls the contract
// TODO: Finalize access control
// https://github.com/zeta-chain/protocol-contracts/issues/204
function withdrawAndCall(address token, address to, uint256 amount, bytes calldata data) external nonReentrant {
function withdrawAndCall(address token, address to, uint256 amount, bytes calldata data) public nonReentrant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under which circumstances is withdrawAndCall and executeWithERC20 be called from within ERC20CustodyNew? This impacts the amount of gas consumed by these functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by circumstances?
withdrawAndCall is called by the TSS address when handling an outbound

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably will be clearer when we add access control

// Transfer the tokens to the Gateway contract
IERC20(token).safeTransfer(address(gateway), amount);

// Forward the call to the Gateway contract
gateway.executeWithERC20(token, to, amount, data);

emit WithdrawAndCall(token, to, amount, data);
}

Check warning

Code scanning / Slither

Unused return Medium

Expand Down
6 changes: 3 additions & 3 deletions contracts/prototypes/evm/GatewayEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable {
error InsufficientERC20Amount();
error ZeroAddress();
error ApprovalFailed();
error CustodyInitialized();

address public custody;
address public tssAddress;
Expand All @@ -27,9 +28,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable {
event Deposit(address indexed sender, address indexed receiver, uint256 amount, address asset, bytes payload);
event Call(address indexed sender, address indexed receiver, bytes payload);

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize(address _tssAddress) public initializer {
Expand Down Expand Up @@ -71,7 +70,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable {
address to,
uint256 amount,
bytes calldata data
) external returns (bytes memory) {
) public returns (bytes memory) {
if (amount == 0) revert InsufficientETHAmount();
// Approve the target contract to spend the tokens
if(!resetApproval(token, to)) revert ApprovalFailed();
Expand Down Expand Up @@ -136,6 +135,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable {
}

function setCustody(address _custody) external {
if (custody != address(0)) revert CustodyInitialized();
custody = _custody;
}

Expand Down
6 changes: 6 additions & 0 deletions echidna.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# provide solc remappings to crytic-compile
cryticArgs: ['--solc-remaps', '@=node_modules/@']
testMode: "assertion"
testLimit: 50000
seqLen: 10000
allContracts: false
Comment on lines +4 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix trailing spaces and add a newline at the end of the file.

There are trailing spaces on lines 4 and 5, and the file is missing a newline at the end.

- testLimit: 50000  
- seqLen: 10000 
+ testLimit: 50000
+ seqLen: 10000

+ 
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
testLimit: 50000
seqLen: 10000
allContracts: false
testLimit: 50000
seqLen: 10000
allContracts: false
Tools
yamllint

[error] 4-4: trailing spaces

(trailing-spaces)


[error] 5-5: trailing spaces

(trailing-spaces)


[error] 6-6: no new line character at the end of file

(new-line-at-end-of-file)

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/contracts/prototypes/evm/gatewayevm.sol/gatewayevm.go

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/fuzz/ERC20CustodyNewEchidnaTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "../../contracts/prototypes/evm/TestERC20.sol";
import "../../contracts/prototypes/evm/ERC20CustodyNew.sol";
import "../../contracts/prototypes/evm/GatewayEVM.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract ERC20CustodyNewEchidnaTest is ERC20CustodyNew {
using SafeERC20 for IERC20;

TestERC20 public testERC20;
address public echidnaCaller = msg.sender;

GatewayEVM testGateway = new GatewayEVM();

constructor() ERC20CustodyNew(address(testGateway)) {
testGateway.initialize(echidnaCaller);
testERC20 = new TestERC20("test", "TEST");
testGateway.setCustody(address(this));
}

function testWithdrawAndCall(address to, uint256 amount, bytes calldata data) public {
// mint more than amount
testERC20.mint(address(this), amount + 5);
// transfer overhead to gateway
testERC20.transfer(address(testGateway), 5);

withdrawAndCall(address(testERC20), to, amount, data);

// Assertion to ensure no ERC20 tokens are left in the gateway contract after execution
assert(testERC20.balanceOf(address(gateway)) == 0);
}
}
27 changes: 27 additions & 0 deletions test/fuzz/GatewayEVMEchidnaTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "../../contracts/prototypes/evm/GatewayEVM.sol";
import "../../contracts/prototypes/evm/TestERC20.sol";
import "../../contracts/prototypes/evm/ERC20CustodyNew.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract GatewayEVMEchidnaTest is GatewayEVM {
using SafeERC20 for IERC20;

TestERC20 public testERC20;
address public echidnaCaller = msg.sender;

constructor() {
initialize(echidnaCaller);
testERC20 = new TestERC20("test", "TEST");
custody = address(new ERC20CustodyNew(address(this)));
}

function testExecuteWithERC20(address to, uint256 amount, bytes calldata data) public {
testERC20.mint(address(this), amount);

executeWithERC20(address(testERC20), to, amount, data);

// Assertion to ensure no ERC20 tokens are left in the contract after execution
assert(testERC20.balanceOf(address(this)) == 0);
}
}
13 changes: 13 additions & 0 deletions test/fuzz/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Setup echidna

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify language for fenced code blocks and add a newline at the end of the file.

Fenced code blocks should have a language specified, and the file should end with a newline.

- ```
+ ```sh

- ```
+ ```sh

+ 

Also applies to: 10-10, 13-13

Tools
Markdownlint

3-3: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

brew install echidna
solc-select use 0.8.7
```

## Execute contract tests

```
echidna test/fuzz/ERC20CustodyNewEchidnaTest.sol --contract ERC20CustodyNewEchidnaTest --config echidna.yaml
echidna test/fuzz/GatewayEVMEchidnaTest.sol --contract GatewayEVMEchidnaTest --config echidna.yaml
```
Loading
Loading