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: add integration tests for Anvil migrations #11002

Merged
merged 41 commits into from
Jun 5, 2024

Conversation

arthurgousset
Copy link
Member

@arthurgousset arthurgousset commented May 21, 2024

Description

  1. adds test to assert that all core contracts on the devchain have a registered address in the Registry.
  2. adds test to assert that all core contracts on the devchain have the correct runtime bytecode, excluding those that depend on linked libraries.

Note

We cannot currently test contracts that depend on linked libraries because vm.getDeployedCode() cannot fetch bytecode of contracts that depend on linked libraries. I created a follow-up issue with context on why this can't be done in this PR:

Other changes

  1. adds a constant in constants.sol that defines the list of contracts that are expected to be in Registry.sol. Refactors the migration and integration test scripts to import that list instead of defining the same list twice.
  2. deletes integration_tests.sh, and refactors the integration test script (run_integration_tests_in_anvil.sh) to include commands from integration_tests.sh instead.
  3. downgrades the foundry version to nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9 (2024-04-02). This is the last foundry version that doesn't have a regression that affects the integration tests.
  4. reverts how ECDSAHelper.sol is deployed to comply with the downgraded foundry version nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9. This reverts a change made in Deprecate random.sol #10997.

Note

For context
1. nightly-fbad377ab26a432e48444cf324feee1195a30960 (2024-06-04) has the regression ❌
2. nightly-0fc39763d46b8ffab5fa4eaeb2f65ae078fa07de (2024-06-03) has the regression ❌
3. nightly-5ac78a9cd4b94dc53d1fe5e0f42372b28b5a7559 (2024-06-02) has the regression ❌
4. nightly-f479e945c6be78bb902df12f9d683c3bb55e3fb0 (2024-06-01) has the regression ❌
5. nightly-cafc2606a2187a42b236df4aa65f4e8cdfcea970 (2024-05-02) has the regression ❌
6. nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9 (2024-04-02) doesn't have the regression ✅

Tested

Yes, these are all tests. The tests run during CI when migrations are generated and separately.

Related issues

Backwards compatibility

Yes, only adds additional tests.

Documentation

Documentation in docstrings in code.

@arthurgousset
Copy link
Member Author

arthurgousset commented May 21, 2024

MVP test: Assert that existing tests that are known to pass also pass when run against the devchain.

  1. Run Anvil with migrations (in another terminal)

    ./migrations_sol/create_and_migrate_anvil_devchain.sh
  2. Run existing tests against local Anvil instance with --rpc-url flag. Existing tests can be found in the CI workflow protocol_tests.yml

    forge test \ 
    -vvv \ 
    --match-path "test-sol/common/*" \
    --rpc-url=http://127.0.0.1:8546
    
    # ... test logs
    Ran 84 test suites: 409 tests passed, 0 failed, 0 skipped (409 total tests)
    forge test \
    -vvv \
    --block-gas-limit 50000000 \
    --match-path "test-sol/governance/network/*" \
    --rpc-url=http://127.0.0.1:8546
    
    Failing tests:
    Encountered 1 failing test in test-sol/governance/network/BlockchainParameters.t.sol:BlockchainParametersTest_initialize
    [FAIL. Reason: log != expected log] test_Emits_UptimeLookbackWindowSet() (gas: 124192)
    
    Encountered 1 failing test in test-sol/governance/network/BlockchainParameters.t.sol:BlockchainParametersTest_setUptimeLookbackWindow
    [FAIL. Reason: log != expected log] test_Emits_UptimeLookbackWindowSet() (gas: 72044)
    
    Encountered a total of 2 failing tests, 413 tests succeeded

This is done in cd987f3

@arthurgousset
Copy link
Member Author

arthurgousset commented May 21, 2024

Ideas for invariants in the integration tests:

  1. assert that the core number or list of contracts are deployed
  2. assert that they are initialized correctly
  3. assert that they are registered in the Registry
  • Check if there are integration tests in the Ganache devchain setup

@arthurgousset arthurgousset force-pushed the arthurgousset/chore/anvil-integration-tests branch from a12fb19 to cd987f3 Compare May 21, 2024 14:35
This is a test run, to see how `setUp()` and contract definitions work.

1. run devchain

```sh
./migrations_sol/create_and_migrate_anvil_devchain.sh
```

2. run test against devchain

```sh
forge test \
--match-path test-sol/integration/Integration.t.sol \
--match-contract GoldTokenTest_General \
-vvv \
--fork-url http://127.0.0.1:8546
```

Tests pass

Output:

```sh
[⠰] Compiling...
No files changed, compilation skipped

Running 3 tests for test-sol/integration/Integration.t.sol:GoldTokenTest_General
[PASS] test_decimals() (gas: 10837)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

[PASS] test_name() (gas: 12537)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

[PASS] test_symbol() (gas: 12579)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 135.40ms

Ran 1 test suites: 3 tests passed, 0 failed, 0 skipped (3 total tests)
```
@arthurgousset
Copy link
Member Author

arthurgousset commented May 28, 2024

As of ee24877, I'm able to run end-to-end tests against a local devchain:

MVP test

This is a test run, to see how setUp() and contract definitions work using GoldToken.sol as an example.

  1. run devchain locally (in a separate terminal)
./migrations_sol/create_and_migrate_anvil_devchain.sh
  1. run MVP test against local devchain
forge test \
--match-path test-sol/integration/Integration.t.sol \
--match-contract GoldTokenTest_General \
-vvv \
--fork-url http://127.0.0.1:8546/

The tests pass

Output:

[⠰] Compiling...
No files changed, compilation skipped

Running 3 tests for test-sol/integration/Integration.t.sol:GoldTokenTest_General
[PASS] test_decimals() (gas: 10837)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

[PASS] test_name() (gas: 12537)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

[PASS] test_symbol() (gas: 12579)
Logs:
  GoldToken address is: 0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448

Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 135.40ms

Ran 1 test suites: 3 tests passed, 0 failed, 0 skipped (3 total tests)

This is to allow me to use the `.code` property on deployed smart contracts, which is only supported on Solidity 0.8 and above.
In Solidity versions 0.7.0 and later, constructors are implicitly internal, meaning they can only be called within the contract itself or derived contracts, making the visibility keyword redundant.
The state mutability of a function can be restricted to view if the function does not modify the state of the blockchain. A view function promises not to alter the state, which allows the Ethereum Virtual Machine (EVM) to optimize how it handles the function.

The function does not modify any state; it only reads from the registry and logs some information.
@arthurgousset
Copy link
Member Author

As of 714bc75, I'm asserting that a list of contract names is registered in Registry.sol on the devchain.

[PASS] test_shouldHaveAddressInRegistry() (gas: 200293)
Logs:
  Accounts address in Registry is:  0x8f24d37a4697e49AECd08d2b197E6968D2f007d3
  BlockchainParameters address in Registry is:  0xFEB14C5787fd63a417612bF8Bc2232e169A7034F
  DoubleSigningSlasher address in Registry is:  0xA920a6fF4249f9A4CDdE3D67ff6275EdA6A1D89f
  DowntimeSlasher address in Registry is:  0xAADB35CAD2f922180106F50BF7ead66C5AD0f101
  Election address in Registry is:  0x5E386A280030a076528aD192BA94D7e31090F461
  EpochRewards address in Registry is:  0xfff723D9B8f466Cd9e11BF4aaca171550aCF18Fc
  Escrow address in Registry is:  0xd773882801F417427aE5C7A032296D93Fcf11DA9
  FederatedAttestations address in Registry is:  0x93f2E9307e3003a0a10A5171478CE18796aA2333
  FeeCurrencyWhitelist address in Registry is:  0x99f389e8a9903aF72ba481e8f000e8E229e529dA
  FeeCurrencyDirectory address in Registry is:  0x42Fe5a2A61ed9705eb2F08a04A58CEB606D22f6a
  Freezer address in Registry is:  0x91dfD4C1B1262fad0f75A38D955B42b4bC586Bc1
  FeeHandler address in Registry is:  0x04dE09026097347E2D540b6fbD9dC4aae3Ab0A90
  GoldToken address in Registry is:  0xfE8CbC1cFA1b3b8256f310bdfd40E60597083448
  Governance address in Registry is:  0xcF636ab1f15d3FB684A82cB1c69977DAfDFc6a0d
  GovernanceSlasher address in Registry is:  0xE41a630372DE5B890a81B148a982b464Aa3B3625
  LockedGold address in Registry is:  0xc7F0E00681356896c06d5c810F0333ab30fBB8D1
  OdisPayments address in Registry is:  0xf7EfD92E61aB144483738B5F0f9cd46E6E9190BC
  Random address in Registry is:  0x8982140ccFb38d7Dc439f953b37829f019A3e6e5
  Registry address in Registry is:  0x000000000000000000000000000000000000ce10
  SortedOracles address in Registry is:  0xa2204011717369e044106e3bC93599E02538d65b
  UniswapFeeHandlerSeller address in Registry is:  0x27D282cBE154FD738de3242F80005cc2ca183981
  MentoFeeHandlerSeller address in Registry is:  0x677e4E735a36A7Ed935D424fCCe57a33831BF0Dc
  Validators address in Registry is:  0xA6aaDC309AA8E134d4a150eb4b58254801353FDc

@arthurgousset
Copy link
Member Author

As of 130523e:

  1. I refactored the migration and integration test scripts, so that both import a list of contract names from constants.sol
  2. I refactored the integration test script run_integration_tests_in_anvil.sh and deleted integration_tests.sh

The only thing stopping this PR from being ready for review is the failing bytecode test.

Because I'm downgrading to a previous foundry version, I have to undo the change to this line made in this PR: #10997
I always forget to run `yarn prettify` in the top-level directory
@arthurgousset
Copy link
Member Author

As of dcb09e1, the PR is all green and ready for review ✅

To explain what flags and arguments are used.
@arthurgousset arthurgousset force-pushed the arthurgousset/chore/anvil-integration-tests branch from 8248fdf to 19faa9b Compare June 4, 2024 15:03
Adds function in `Integration.t.sol` directly to avoid 0.5.x version problems.
…r function

Because the script is now on 0.8, we can use the `.code` property to fetch the runtime bytecode of a smart contract deployed on the devchain. Previously, we needed to load the bytecode from storage with inline assembly.
Functionally this should be the same, but the configs and flags are more readable.
@arthurgousset arthurgousset merged commit 2908c52 into master Jun 5, 2024
22 checks passed
@arthurgousset arthurgousset deleted the arthurgousset/chore/anvil-integration-tests branch June 5, 2024 11:41
@arthurgousset arthurgousset changed the title Integration tests for Anvil migrations Add integration tests for Anvil migrations Jun 5, 2024
@arthurgousset arthurgousset changed the title Add integration tests for Anvil migrations test: add integration tests for Anvil migrations Jun 5, 2024
@arthurgousset arthurgousset self-assigned this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add integration tests for Anvil migrations
3 participants