Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Testing infrastructure improvements #82

Merged
merged 38 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dad1cc6
System contracts test preprocessing mode
AntonD3 Nov 2, 2023
d74d4b9
Mock dependencies, event writer asm contract test, refactoring
AntonD3 Nov 8, 2023
3e64d7d
Merge branch 'dev' into ad-better-sc-tests
AntonD3 Nov 8, 2023
11cc1d3
Merge branch 'dev' into ad-better-sc-tests
AntonD3 Dec 4, 2023
b186727
lint fix
AntonD3 Dec 4, 2023
56594d8
Small refactoring
AntonD3 Dec 7, 2023
819afe7
Change approach to use the test node
AntonD3 Dec 7, 2023
1498f0c
Add docs, comments
AntonD3 Dec 7, 2023
42f9393
lint readme
AntonD3 Dec 7, 2023
75baffa
Fix hashes
AntonD3 Dec 7, 2023
12576eb
Regenerate yarn.lock to fix lints
AntonD3 Dec 7, 2023
a626069
lint:fix
AntonD3 Dec 7, 2023
8b271c7
Fix lints
AntonD3 Dec 7, 2023
a9826ea
Restore lost tests
AntonD3 Dec 7, 2023
9d8d0c0
Fix lints
AntonD3 Dec 7, 2023
ceeb02e
Restore yarn.lock from dev
AntonD3 Dec 7, 2023
ee1dfbe
Update caches in workflows
AntonD3 Dec 8, 2023
9fb9ae7
Try to disable lint cache
AntonD3 Dec 8, 2023
215989d
Restore lint cache
AntonD3 Dec 8, 2023
9d15053
Cache contracts-preprocessed
AntonD3 Dec 8, 2023
ad03a05
try to debug lint
AntonD3 Dec 8, 2023
e629dcc
Regenerate yarn.lock from dev
AntonD3 Dec 8, 2023
ada10bd
Restore correct deps
AntonD3 Dec 8, 2023
8fcf0d6
Update lock
AntonD3 Dec 8, 2023
b9e0b2f
Proposed improvements/fixes
AntonD3 Dec 8, 2023
22078b2
Use fast-glob instead glob
AntonD3 Dec 8, 2023
8989aa9
Update bootloader_test artifact path
AntonD3 Dec 8, 2023
9a94e0d
Proposed improvements, update hashes
AntonD3 Dec 8, 2023
fcf6b3f
Implement some fixes and improvements
AntonD3 Dec 11, 2023
ed02aee
Fix lints
AntonD3 Dec 11, 2023
fab9f5c
Update zksync-era in bootloader tests
AntonD3 Dec 11, 2023
ccb925f
Fix imports
Deniallugo Dec 11, 2023
62e1c44
Update contracts/test-contracts/MockContract.sol
AntonD3 Dec 11, 2023
ab83368
Fix test infra
AntonD3 Dec 11, 2023
e091057
data -> input mock contract
AntonD3 Dec 11, 2023
1df6e96
Update SC hashes
AntonD3 Dec 11, 2023
a94c8a0
Update zksync-era in bootloader/test_infra
AntonD3 Dec 11, 2023
b01cd2c
Update again
AntonD3 Dec 11, 2023
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
12 changes: 4 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ jobs:
artifacts-zk
cache-zk
typechain-types
contracts/artifacts
contracts/precompiles/artifacts
contracts-preprocessed
bootloader/build

lint:
Expand Down Expand Up @@ -84,8 +83,7 @@ jobs:
artifacts-zk
cache-zk
typechain-types
contracts/artifacts
contracts/precompiles/artifacts
contracts-preprocessed
bootloader/build

- name: Check hashes
Expand Down Expand Up @@ -122,8 +120,7 @@ jobs:
artifacts-zk
cache-zk
typechain-types
contracts/artifacts
contracts/precompiles/artifacts
contracts-preprocessed
bootloader/build

- name: Run tests
Expand Down Expand Up @@ -155,8 +152,7 @@ jobs:
artifacts-zk
cache-zk
typechain-types
contracts/artifacts
contracts/precompiles/artifacts
contracts-preprocessed
bootloader/build

- name: Run bootloader tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
cache
artifacts
contracts-preprocessed
cache-zk
artifacts-zk
typechain-types
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
contracts/openzeppelin
contracts/Constants.sol
3 changes: 2 additions & 1 deletion .solhintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
contracts/openzeppelin
contracts/openzeppelin
contracts/Constants.sol
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,80 @@ Check the system contracts hashes: `yarn calculate-hashes:check`

Update the system contracts hashes: `yarn calculate-hashes:fix`

## Testing

### Run tests

The tests of the system contracts utilize the zkSync test node. In order to run the tests, execute the following commands in the root of the repository:

```
yarn test-node
```

It will run the test node, and you can see its logs in the output.
Then run tests in the separate terminal:

```
yarn test
```

Please note that you need to rerun the test node every time you are running the tests because, in the current version, tests will be affected by the state after the previous run.

### Testing infrastructure overview

#### Address space

During the tests, we deploy most of the system contracts on special "testing" addresses, which are equal to production addresses + `0x1000`.

This approach enhances test flexibility since we can manipulate contract states without impacting the system's functionality.
We can freely modify contract states, mock them, etc., without being constrained by the contracts used by the test node.
Additionally, these addresses are located in the kernel space, as required by the system contracts.

For this purpose special testing preprocessing mode exists, it's needed to change the address constants.
When some system contracts call others using these constants, they will actually get to testing addresses.

The exceptions are:

- [EventWriter.yul](contracts%2FEventWriter.yul)
- [precompiles](contracts%2Fprecompiles)

During preprocessing, we keep production addresses for them because we want other contracts to call them in tests rather than mock them. This simplifies the testing process.
Also, when testing these contracts, some of them should also be deployed on the original addresses:

- [EventWriter.yul](contracts%2FEventWriter.yul): should be on the original address because event logs are filtered by address
- [Ecrecover.yul](contracts%2Fprecompiles%2FEcrecover.yul): uses precompile call instruction, which is address-dependent
- [Keccak256.yul](contracts%2Fprecompiles%2FKeccak256.yul): uses precompile call instruction, which is address-dependent
- [SHA256.yul](contracts%2Fprecompiles%2FSHA256.yul): uses precompile call instruction, which is address-dependent

However, this is not the case for [EcAdd.yul](contracts%2Fprecompiles%2FEcAdd.yul) and [EcMul.yul](contracts%2Fprecompiles%2FEcMul.yul), so they can be deployed on any addresses, even outside kernel space.

#### Test contracts/features

The behavior of the contracts can depend on various factors: system call flag, extra ABI registers, `msg.sender`, `msg.value`(`context_u128` value), context, etc.
It is crucial to control these values during testing.

They are often interconnected, requiring the need to mock some of them.

To achieve this, the following contracts and features were used/implemented:

- [MockContract.sol](contracts%2Ftest-contracts%2FMockContract.sol) - a contract used for mocking.
- [ExtraAbiCaller.zasm](contracts%2Ftest-contracts%2FExtraAbiCaller.zasm) - a contract that allows to set the extra abi registers, `context_u128` value with the system flag for the call.
- [SystemCaller.sol](contracts%2Ftest-contracts%2FSystemCaller.sol) - a "proxy" that sets the system call flag.
In theory `ExtraAbiCaller` can be used instead, but this one is sometimes more convenient because it can be called with the destination contract ABI.
- `hardhat_stopImpersonatingAccount` - this API method is useful during the tests themselves.
Apart from that, it's used to force deploy the contracts on the specific addresses during the tests. See [ContractDeployer.sol](contracts%2FContractDeployer.sol):`forceDeployOnAddress`.

Only the main and most generic aspects are mentioned above, however, there are more features that can be found in the tests.
There are wrappers/helpers for these contracts and features, along with additional functionality in [shared](test%2Fshared), and [test-contracts](contracts%2Ftest-contracts).

#### Test cases

Currently, during specific contract testing, we aim to cover all external functions.
Typically, one test case corresponds to one main function call, possibly with additional calls to prepare the state.

Therefore, considering all the information above, we can say that it's almost unit tests over external functions.
Many examples can be found in [test](test).

## Update Process

System contracts handle core functionalities and play a critical role in maintaining the integrity of our protocol. To
Expand Down
Loading
Loading