Skip to content

Commit

Permalink
Merge pull request #26 from matter-labs/bh-EVM-216-move-l1-tests-to-f…
Browse files Browse the repository at this point in the history
…oundry

test: Move l1 tests to foundry Part-1
  • Loading branch information
vladbochok authored Sep 28, 2023
2 parents 8faf111 + 024971a commit 6101b93
Show file tree
Hide file tree
Showing 36 changed files with 2,702 additions and 1,575 deletions.
45 changes: 44 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
ethereum/cache
ethereum/typechain
test:
test-hardhat:
needs: [build, lint]
runs-on: ubuntu-latest

Expand Down Expand Up @@ -103,3 +103,46 @@ jobs:
- name: Run tests
run: yarn test --no-compile

test-foundry:
needs: [build, lint]
runs-on: ubuntu-latest

defaults:
run:
working-directory: ethereum

steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: "recursive"

- name: "Install Foundry"
uses: "foundry-rs/foundry-toolchain@v1"

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 16.15.1
cache: yarn
cache-dependency-path: ethereum/yarn.lock

- name: Install yarn
run: npm install -g yarn

- name: Install dependencies
run: yarn install

- name: Restore artifacts cache
uses: actions/cache/restore@v3
with:
fail-on-cache-miss: true
key: artifacts-${{ github.sha }}
path: |
ethereum/artifacts
ethereum/cache
ethereum/typechain
- name: Run tests
run: forge test
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ethereum/lib/forge-std"]
path = ethereum/lib/forge-std
url = https://github.com/foundry-rs/forge-std
2 changes: 2 additions & 0 deletions ethereum/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/typechain
node_modules
./contracts/DS_Store
/artifacts-forge
/cache-forge
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.13;
import "../../zksync/libraries/Diamond.sol";
import "../../zksync/facets/Getters.sol";

contract DiamondCutTest is GettersFacet {
contract DiamondCutTestContract is GettersFacet {
function diamondCut(Diamond.DiamondCutData memory _diamondCut) external {
Diamond.diamondCut(_diamondCut);
}
Expand Down
1 change: 1 addition & 0 deletions ethereum/contracts/zksync/DiamondInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Diamond} from "./libraries/Diamond.sol";
import {Base} from "./facets/Base.sol";
import {Verifier} from "./Verifier.sol";
import {VerifierParams} from "./Storage.sol";
/* solhint-disable max-line-length */
import {L2_TO_L1_LOG_SERIALIZE_SIZE, EMPTY_STRING_KECCAK, DEFAULT_L2_LOGS_TREE_ROOT_HASH, L2_TX_MAX_GAS_LIMIT} from "./Config.sol";

/// @author Matter Labs
Expand Down
8 changes: 8 additions & 0 deletions ethereum/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[profile.default]
src = 'cache/solpp-generated-contracts'
out = 'artifacts-forge'
libs = ['node_modules', 'lib']
cache_path = 'cache-forge'
test = 'test/foundry'

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
13 changes: 9 additions & 4 deletions ethereum/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '@nomiclabs/hardhat-waffle';
import '@nomiclabs/hardhat-solpp';
import '@nomiclabs/hardhat-ethers';
import '@nomiclabs/hardhat-etherscan';
import 'hardhat-typechain';
import '@nomiclabs/hardhat-solpp';
import '@nomiclabs/hardhat-waffle';
import 'hardhat-contract-sizer';
import 'solidity-coverage';
import 'hardhat-gas-reporter';
import 'hardhat-typechain';
import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/task-names";
import { task } from 'hardhat/config';
import 'solidity-coverage';
import { getNumberFromEnv } from './scripts/utils';

// If no network is specified, use the default config
Expand Down Expand Up @@ -111,3 +113,6 @@ export default {
enabled: true
}
};

task('solpp', 'Preprocess Solidity source files')
.setAction(async (_, hre) => hre.run(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS));
1 change: 1 addition & 0 deletions ethereum/lib/forge-std
Submodule forge-std added at 705263
13 changes: 10 additions & 3 deletions ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@
"build": "hardhat compile",
"clean": "hardhat clean",
"test": "CONTRACT_TESTS=1 yarn hardhat test test/unit_tests/*.spec.ts --network hardhat",
"test:foundry": "hardhat solpp && forge test",
"test:fork": "CONTRACT_TESTS=1 TEST_CONTRACTS_FORK=1 yarn run hardhat test test/unit_tests/*.fork.ts --network hardhat",
"lint": "yarn lint:sol && yarn prettier:check",
"lint:sol": "solhint --max-warnings 29 contracts/**/*.sol",
"prettier:check": "prettier --check contracts/**/*.sol",
"prettier:write": "prettier --write contracts/**/*.sol",
"lint:sol-contracts": "solhint --max-warnings 30 contracts/**/*.sol",
"lint:sol-tests": "solhint --max-warnings 0 test/**/*.sol",
"lint:sol": "yarn lint:sol-contracts && yarn lint:sol-tests",
"prettier:check-contracts": "prettier --check contracts/**/*.sol",
"prettier:check-tests": "prettier --check test/**/*.sol",
"prettier:check": "yarn prettier:check-contracts && yarn prettier:check-tests",
"prettier:write-contracts": "prettier --write contracts/**/*.sol",
"prettier:write-tests": "prettier --write test/**/*.sol",
"prettier:write": "yarn prettier:write-contracts && yarn prettier:write-tests",
"deploy-no-build": "ts-node scripts/deploy.ts",
"deploy-weth-bridges": "ts-node scripts/deploy-weth-bridges.ts",
"initialize-weth-bridges": "ts-node scripts/initialize-weth-bridges.ts",
Expand Down
6 changes: 6 additions & 0 deletions ethereum/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ensdomains/=node_modules/@ensdomains/
@openzeppelin/=node_modules/@openzeppelin/
ds-test/=lib/forge-std/lib/ds-test/src/
eth-gas-reporter/=node_modules/eth-gas-reporter/
forge-std/=lib/forge-std/src/
hardhat/=node_modules/hardhat/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import {AccessModeTest} from "./_AccessMode_Shared.t.sol";
import {IAllowList} from "../../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol";

contract DepositLimitTest is AccessModeTest {
address private l1token = makeAddr("l1token");

function test_RevertWhen_NonOwner() public {
vm.expectRevert(abi.encodePacked("Ownable: caller is not the owner"));
vm.prank(randomSigner);
allowList.setDepositLimit(l1token, true, 1000);
}

function test_Owner() public {
vm.prank(owner);
allowList.setDepositLimit(l1token, true, 1000);

IAllowList.Deposit memory deposit = allowList.getTokenDepositLimitData(l1token);
assertEq(deposit.depositLimitation, true, "depositLimitation should be true");
assertEq(deposit.depositCap, 1000, "depositCap should be 1000");
}

function test_UnlimitedToken() public {
address unlimitedToken = makeAddr("unlimitedToken");

IAllowList.Deposit memory deposit = allowList.getTokenDepositLimitData(unlimitedToken);

assertEq(deposit.depositLimitation, false, "depositLimitation should be false");
assertEq(deposit.depositCap, 0, "depositCap should be 0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import {AccessModeTest} from "./_AccessMode_Shared.t.sol";
import {IAllowList} from "../../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol";

contract SetAccessModeTest is AccessModeTest {
function test_RevertWhen_NonOwner() public {
vm.expectRevert(abi.encodePacked("Ownable: caller is not the owner"));
vm.prank(randomSigner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);
}

function test_Owner() public {
vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);
}

function test_OwnerTwice() public {
vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);

vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);
}

function test_AccessModeBefore() public {
bool hasSpecialAccessToCall = allowList.hasSpecialAccessToCall(owner, target, functionSig);
assertEq(hasSpecialAccessToCall, false, "hasSpecialAccessToCall should be false");

IAllowList.AccessMode accessMode = allowList.getAccessMode(target);
bool isClosed = accessMode == IAllowList.AccessMode.Closed;
assertEq(isClosed, true, "AccessMode should be Closed");

bool canCall = allowList.canCall(owner, target, functionSig);
assertEq(canCall, false, "canCall should be false");
}

function test_AccessModeAfter() public {
vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);

bool hasSpecialAccessToCall = allowList.hasSpecialAccessToCall(owner, target, functionSig);
assertEq(hasSpecialAccessToCall, false, "hasSpecialAccessToCall should be false");

IAllowList.AccessMode accessMode = allowList.getAccessMode(target);
bool isPublic = accessMode == IAllowList.AccessMode.Public;
assertEq(isPublic, true, "AccessMode should be Public");

bool canCall = allowList.canCall(owner, target, functionSig);
assertEq(canCall, true, "canCall should be true");
}

function test_RemovePermission() public {
vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Closed);

vm.prank(owner);
allowList.setAccessMode(target, IAllowList.AccessMode.Public);

bool hasSpecialAccessToCall = allowList.hasSpecialAccessToCall(owner, target, functionSig);
assertEq(hasSpecialAccessToCall, false, "hasSpecialAccessToCall should be false");

IAllowList.AccessMode accessMode = allowList.getAccessMode(target);
bool isPublic = accessMode == IAllowList.AccessMode.Public;
assertEq(isPublic, true, "AccessMode should be Public");

bool canCall = allowList.canCall(owner, target, functionSig);
assertEq(canCall, true, "canCall should be true");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import {AccessModeTest} from "./_AccessMode_Shared.t.sol";
import {IAllowList} from "../../../../../../cache/solpp-generated-contracts/common/interfaces/IAllowList.sol";

contract SetBatchAccessModeTest is AccessModeTest {
function test_RevertWhen_NonOwner() public {
address[] memory targets = new address[](2);
targets[0] = target;
targets[1] = target;

IAllowList.AccessMode[] memory accessModes = new IAllowList.AccessMode[](2);
accessModes[0] = IAllowList.AccessMode.Public;
accessModes[1] = IAllowList.AccessMode.Public;

vm.expectRevert("Ownable: caller is not the owner");
vm.prank(randomSigner);
allowList.setBatchAccessMode(targets, accessModes);
}

function test_Owner() public {
address[] memory targets = new address[](2);
targets[0] = target;
targets[1] = target;

IAllowList.AccessMode[] memory accessModes = new IAllowList.AccessMode[](2);
accessModes[0] = IAllowList.AccessMode.Public;
accessModes[1] = IAllowList.AccessMode.Public;

vm.prank(owner);
allowList.setBatchAccessMode(targets, accessModes);
}

function test_RevertWhen_ArrayLengthNotEqual() public {
address[] memory targets = new address[](1);
targets[0] = target;

IAllowList.AccessMode[] memory accessModes = new IAllowList.AccessMode[](2);
accessModes[0] = IAllowList.AccessMode.Public;
accessModes[1] = IAllowList.AccessMode.Public;

vm.expectRevert(abi.encodePacked("yg"));
vm.prank(owner);
allowList.setBatchAccessMode(targets, accessModes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import {AllowListTest} from "../_AllowList_Shared.t.sol";

contract AccessModeTest is AllowListTest {
address internal target = 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045;
bytes4 internal functionSig = 0xdeadbeaf;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import {PermissionTest} from "./_Permission_Shared.t.sol";

contract SetBatchPermissionToCall is PermissionTest {
function test_RevertWhen_NonOwner() public {
address[] memory callers = new address[](2);
callers[0] = owner;
callers[1] = owner;

address[] memory targets = new address[](2);
targets[0] = target;
targets[1] = target;

bytes4[] memory functionSigs = new bytes4[](2);
functionSigs[0] = functionSig;
functionSigs[1] = functionSig;

bool[] memory enables = new bool[](2);
enables[0] = true;
enables[1] = true;

vm.expectRevert("Ownable: caller is not the owner");
vm.prank(randomSigner);
allowList.setBatchPermissionToCall(callers, targets, functionSigs, enables);
}

function test_Owner() public {
address[] memory callers = new address[](2);
callers[0] = owner;
callers[1] = owner;

address[] memory targets = new address[](2);
targets[0] = target;
targets[1] = target;

bytes4[] memory functionSigs = new bytes4[](2);
functionSigs[0] = functionSig;
functionSigs[1] = functionSig;

bool[] memory enables = new bool[](2);
enables[0] = true;
enables[1] = true;

vm.prank(owner);
allowList.setBatchPermissionToCall(callers, targets, functionSigs, enables);
}

function test_RevertWhen_ArrayLengthNotEqual() public {
address[] memory callers = new address[](1);
callers[0] = owner;

address[] memory targets = new address[](2);
targets[0] = target;
targets[1] = target;

bytes4[] memory functionSigs = new bytes4[](2);
functionSigs[0] = functionSig;
functionSigs[1] = functionSig;

bool[] memory enables = new bool[](2);
enables[0] = true;
enables[1] = true;

vm.expectRevert(abi.encodePacked("yw"));
vm.prank(owner);
allowList.setBatchPermissionToCall(callers, targets, functionSigs, enables);
}
}
Loading

0 comments on commit 6101b93

Please sign in to comment.