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

feat(foundry): publish L2 state in @celo/devchain-anvil #11107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
17dc558
refactor(scripts/foundry): moves `run_e2e_tests...sh` file
arthurgousset Jul 3, 2024
7ec412e
feat(package.json): adds yarn commands for e2e and integration tests
arthurgousset Jul 3, 2024
ccb8962
refactor(migrations_sol): groups imports for better readability
arthurgousset Jul 3, 2024
d772383
feat(scripts/foundry): adds MVP L2 migration script
arthurgousset Jul 3, 2024
4a6b28a
feat(package.json): adds two helper yarn commands
arthurgousset Jul 3, 2024
884523e
chore(migrations_sol): fix typo in function name
arthurgousset Jul 3, 2024
6c50678
chore(migrations_sol): nit simplifies code comment for better readabi…
arthurgousset Jul 4, 2024
651c9b7
feat(scripts/foundry): activates CeloDistributionSchedule
arthurgousset Jul 4, 2024
1b90853
refactor(scripts/foundry): moves variables to `constants.sh`
arthurgousset Jul 4, 2024
4a7606b
refactor(scripts/foundry): moves more variables to `constants.sh`
arthurgousset Jul 4, 2024
2fc22ef
refactor(constants.sh): read bytecode from Foundry instead of Truffle
arthurgousset Jul 4, 2024
7315121
chore(scripts/foundry): adds TODO comment
arthurgousset Jul 4, 2024
3ea0477
Merge branch 'release/core-contracts/12' into arthurgousset/feat/publ…
arthurgousset Jul 4, 2024
563ec74
refactor(scripts/foundry): rename to `create_and_migrate_anvil_l2_dev…
arthurgousset Jul 5, 2024
a5c8bf0
style(migrations_sol): linting
arthurgousset Jul 5, 2024
fca75d4
chore(scripts/foundry): remove debugging statements
arthurgousset Jul 5, 2024
209ca83
refactor(constants): adds L1 and L2 state file names
arthurgousset Jul 5, 2024
e4671eb
feat(scripts/foundry): successfully dumps L2 state
arthurgousset Jul 5, 2024
92c1dd8
refactor(constants): uses `$ANVIL_RPC_URL` instead of hardcoded URL
arthurgousset Jul 5, 2024
ed9a6c0
refactor(constants): moves libraries to constants
arthurgousset Jul 5, 2024
9e04ad9
fix(constants): adds `$PWD` to prevent errors on CI
arthurgousset Jul 5, 2024
ad5d1ba
chore(workflows): debugging `jq` error on CI
arthurgousset Jul 5, 2024
9215e50
fix(constants): moves bytecode operation back into scripts
arthurgousset Jul 5, 2024
05f3532
chore(workflows): debugging `jq` CI error
arthurgousset Jul 5, 2024
95d5a6d
fix(scripts/foundry): uses Foundry `out/` directory
arthurgousset Jul 5, 2024
dea18dd
chore(scripts/foundry): removes debugging statement
arthurgousset Jul 5, 2024
5e60f18
feat(package.json): updates yarn command names
arthurgousset Jul 5, 2024
05579f6
docs(CONTRIBUTING): updates yarn command docs
arthurgousset Jul 5, 2024
c505eba
chore(workflows): add back NPM install step
arthurgousset Jul 5, 2024
276b569
chore(workflows): removes code comment
arthurgousset Jul 5, 2024
1e4cc56
feat(workflows): publishes L1 and L2 state
arthurgousset Jul 5, 2024
6311800
fix(scripts/foundry): remove unnecessary `stop_anvil` call
arthurgousset Jul 5, 2024
ec192f3
refactor(workflows): replace CI env with variable from `constants.sh`
arthurgousset Jul 5, 2024
58a1fd0
fix(workflows): adds back `stop_anvil.sh` and installs `lsof` explicitly
arthurgousset Jul 5, 2024
fa243ca
chore(scripts/foundry): removes code comment
arthurgousset Jul 5, 2024
ab7f5d0
fix(workflows): install `lsof` in the same step
arthurgousset Jul 8, 2024
28464b6
fix(workflows): hacky fix to run L2 anvil
arthurgousset Jul 8, 2024
d29b220
refactor(scripts/foundry): simplifying `lsof` query to find PID
arthurgousset Jul 8, 2024
cd6ca05
fix(workflows): try to terminate anvil server again
arthurgousset Jul 8, 2024
a906048
fix(workflows): hacky workaround to prevent Anvil bug when loading state
arthurgousset Jul 8, 2024
b88b337
Update .github/workflows/protocol-devchain-anvil.yml
arthurgousset Jul 12, 2024
ff291e0
Update .github/workflows/protocol-devchain-anvil.yml
arthurgousset Jul 12, 2024
06e7fe7
feat(constants): set CeloDistributionSchedule initial balance to 700M…
arthurgousset Jul 12, 2024
85895b5
docs(README): improves README
arthurgousset Jul 12, 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
31 changes: 22 additions & 9 deletions .github/workflows/protocol-devchain-anvil.yml
arthurgousset marked this conversation as resolved.
Show resolved Hide resolved
arthurgousset marked this conversation as resolved.
Show resolved Hide resolved
arthurgousset marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ env:
FOUNDRY_CACHE_KEY: 1
# Supported Foundry version defined at celo-org (GitHub organisation) level, for consistency across workflows.
SUPPORTED_FOUNDRY_VERSION: ${{ vars.SUPPORTED_FOUNDRY_VERSION }}
ANVIL_PORT: 8546

jobs:
build:
Expand Down Expand Up @@ -124,17 +123,31 @@ jobs:
- name: 'Install packages'
shell: bash
run: yarn

- name: Generate migrations and run devchain
if: success() || failure()
run: ./scripts/foundry/create_and_migrate_anvil_devchain.sh

- name: Run integration tests against local anvil devchain
# Starting L1 from scratch instead of JSON state to circumvent this Anvil bug https://github.com/foundry-rs/foundry/issues/7502
# Install `lsof` dependency, because it's not readily available on CI, but is required by
# `create_and_migrate_anvil_l2_devchain.sh`, because it uses `stop_anvil.sh` to kill
# existing anvil servers.
- name: Generate L1 migrations and run integration tests against L1 devchain
if: success() || failure()
run: |
sudo apt-get update
sudo apt-get install -y lsof

source ./scripts/foundry/constants.sh

echo "Starting L1 from scratch to circumvent Anvil bug"
source ./scripts/foundry/create_and_migrate_anvil_devchain.sh

forge test -vvv \
--match-path "test-sol/integration/*" \
--fork-url http://127.0.0.1:${{ env.ANVIL_PORT }}
--fork-url $ANVIL_RPC_URL

./scripts/foundry/stop_anvil.sh

- name: Generate L2 migration
if: success() || failure()
run: ./scripts/foundry/create_and_migrate_anvil_l2_devchain.sh
arthurgousset marked this conversation as resolved.
Show resolved Hide resolved

- name: Sanitize ref name
id: sanitize-ref-name
Expand All @@ -158,7 +171,7 @@ jobs:
env:
RELEASE_TYPE: ${{ env.RELEASE_TYPE }}
RELEASE_VERSION: ${{ env.RELEASE_VERSION }}

- name: Upload devchain as artifact
uses: actions/upload-artifact@v4
with:
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/protocol_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ env:
FOUNDRY_CACHE_KEY: 2
# Supported Foundry version defined at celo-org (GitHub organisation) level, for consistency across workflows.
SUPPORTED_FOUNDRY_VERSION: ${{ vars.SUPPORTED_FOUNDRY_VERSION }}
ANVIL_PORT: 8546

jobs:
check:
Expand Down Expand Up @@ -129,13 +128,17 @@ jobs:
- name: Run integration tests against local anvil devchain
if: success() || failure()
run: |
source ./scripts/foundry/constants.sh

forge test -vvv \
--match-path "test-sol/integration/*" \
--fork-url http://127.0.0.1:${{ env.ANVIL_PORT }}
--fork-url $ANVIL_RPC_URL

- name: Run e2e tests against local anvil devchain
if: success() || failure()
run: |
source ./scripts/foundry/constants.sh

forge test -vvv \
--match-path "test-sol/e2e/*" \
--fork-url http://127.0.0.1:${{ env.ANVIL_PORT }}
--fork-url $ANVIL_RPC_URL
56 changes: 33 additions & 23 deletions packages/protocol/migrations_sol/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,51 @@ This devchain is useful for testing and development of the Celo protocol.

## Usage

### Start devchain
### Start L1 devchain

```sh
$ yarn anvil-devchain:start
$ yarn anvil-devchain:start-L1
```

Starts a new anvil devchain serving at localhost (default port 8546).

For example:
You can now run commands against the local devchain. For example:

```sh
$ yarn anvil-devchain:start

yarn run v1.22.22
$ ./scripts/foundry/create_and_migrate_anvil_devchain.sh
# ...
Total elapsed time: 193 seconds
✨ Done in 193.09s.
cast block-number \
--rpc-url http://127.0.0.1:8546
266
```

You can now run commands against the local devchain.
### Start L2 devchain

```sh
$ yarn anvil-devchain:start-L2
```

For example:
Starts a new anvil devchain serving at localhost (default port 8546).

You can now run commands against the local devchain. For example:

```sh
cast block-number \
--rpc-url http://127.0.0.1:8546
266
# Call `isL2()` on `CeloDistributionSchedule.sol`
cast call \
0xA16cF67AFa80BB9Ce7a325597F80057c6B290fD4 \
"isL2()(bool)" \
--rpc-url=http://127.0.0.1:8546
true
```

### Check if devchain is running

```sh
$ yarn anvil-devchain:status

# If devchain is running
Devchain is serving at http://localhost:8546

# If devchain is not running
Devchain is not running.
```

### Stop devchain
Expand All @@ -41,17 +58,10 @@ cast block-number \
$ yarn anvil-devchain:stop
```

Terminates any anvil nodes serving at localhost.

For example:
Terminates any anvil nodes serving at localhost. For example:

```sh
# in packages/protocol/ directory
$ yarn anvil-devchain:stop

yarn run v1.22.22
$ ./scripts/foundry/stop_anvil.sh
Connection to localhost port 8546 [tcp/*] succeeded!
Killed Anvil
✨ Done in 0.11s.
```
51 changes: 25 additions & 26 deletions packages/protocol/migrations_sol/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ pragma solidity >=0.8.7 <0.8.20;

import { Script } from "forge-std-8/Script.sol";

// Foundry imports
import "forge-std/console.sol";
import "forge-std/StdJson.sol";

import "@celo-contracts/common/interfaces/IProxyFactory.sol";
// Helper contract imports
import "@migrations-sol/HelperInterFaces.sol";
import { Constants } from "@migrations-sol/constants.sol";
import "@openzeppelin/contracts8/utils/math/Math.sol";

// Initializer of the core contracts
// Core contract imports on Solidity 0.5
import "@celo-contracts/common/interfaces/IProxy.sol";
import "@celo-contracts/common/interfaces/IProxyFactory.sol";
import "@celo-contracts/common/interfaces/IRegistry.sol";
import "@celo-contracts/common/interfaces/IRegistryInitializer.sol";
import "@celo-contracts/common/interfaces/IFreezer.sol";
import "@celo-contracts/common/interfaces/IFreezerInitializer.sol";
import "@celo-contracts-8/common/interfaces/IFeeCurrencyDirectoryInitializer.sol";
import "@celo-contracts/common/interfaces/ICeloTokenInitializer.sol";
import "@celo-contracts/common/interfaces/IAccountsInitializer.sol";
import "@celo-contracts/common/interfaces/IFeeHandlerSellerInitializer.sol";
import "@celo-contracts/common/interfaces/IFeeHandler.sol";
import "@celo-contracts/common/interfaces/IFeeHandlerInitializer.sol";
import "@celo-contracts/common/interfaces/IFeeCurrencyWhitelist.sol";
import "@celo-contracts/common/interfaces/IAccounts.sol";
import "@celo-contracts/governance/interfaces/ILockedGoldInitializer.sol";
import "@celo-contracts/governance/interfaces/IValidatorsInitializer.sol";
import "@celo-contracts/governance/interfaces/IElectionInitializer.sol";
Expand All @@ -24,34 +37,22 @@ import "@celo-contracts/governance/interfaces/IDoubleSigningSlasherInitializer.s
import "@celo-contracts/governance/interfaces/IDowntimeSlasherInitializer.sol";
import "@celo-contracts/governance/interfaces/IGovernanceApproverMultiSigInitializer.sol";
import "@celo-contracts/governance/interfaces/IGovernanceInitializer.sol";
import "@celo-contracts/common/interfaces/IAccountsInitializer.sol";
import "@celo-contracts/common/interfaces/IFeeHandlerSellerInitializer.sol";
import "@celo-contracts/common/interfaces/IFeeHandlerInitializer.sol";
import "@celo-contracts/governance/interfaces/ILockedGold.sol";
import "@celo-contracts/governance/interfaces/IGovernance.sol";
import "@celo-contracts/identity/interfaces/IRandomInitializer.sol";
import "@celo-contracts/identity/interfaces/IEscrowInitializer.sol";
import "@celo-contracts/identity/interfaces/IOdisPaymentsInitializer.sol";
import "@celo-contracts/identity/interfaces/IFederatedAttestationsInitializer.sol";
import "@celo-contracts/stability/interfaces/ISortedOraclesInitializer.sol";
import "@celo-contracts/stability/interfaces/ISortedOracles.sol";

// Core contract imports on Solidity 0.8
import "@celo-contracts-8/common/interfaces/IFeeCurrencyDirectoryInitializer.sol";
import "@celo-contracts-8/common/interfaces/IGasPriceMinimumInitializer.sol";
import "@celo-contracts-8/common/interfaces/ICeloDistributionScheduleInitializer.sol";

import "@celo-contracts/common/interfaces/IProxy.sol";
import "@celo-contracts/common/interfaces/IRegistry.sol";
import "@celo-contracts/common/interfaces/IFreezer.sol";
import "@celo-contracts/common/interfaces/IFeeCurrencyWhitelist.sol";
import "@celo-contracts-8/common/interfaces/IFeeCurrencyDirectory.sol";
import "@celo-contracts/common/interfaces/IAccounts.sol";
import "@celo-contracts/governance/interfaces/ILockedGold.sol";
import "@celo-contracts/governance/interfaces/IGovernance.sol";
import "@celo-contracts/common/interfaces/IFeeHandler.sol";
import "@celo-contracts/stability/interfaces/ISortedOracles.sol";

import "@migrations-sol/HelperInterFaces.sol";
import "@openzeppelin/contracts8/utils/math/Math.sol";
import "@celo-contracts-8/common/UsingRegistry.sol";

import { Constants } from "@migrations-sol/constants.sol";

contract ForceTx {
// event to trigger so a tx can be processed
event VanillaEvent(string);
Expand All @@ -66,9 +67,7 @@ contract ForceTx {
contract Migration is Script, UsingRegistry, Constants {
using stdJson for string;

/**
* This is Anvil's default account
*/
// This is Anvil's default account
address constant deployerAccount = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

IProxyFactory proxyFactory;
Expand Down Expand Up @@ -980,10 +979,10 @@ contract Migration is Script, UsingRegistry, Constants {
);

_setConstitution(governanceProxyAddress, json);
_transferOwnerShipCoreContact(governanceProxyAddress, json);
_transferOwnerShipCoreContract(governanceProxyAddress, json);
}

function _transferOwnerShipCoreContact(address governanceAddress, string memory json) public {
function _transferOwnerShipCoreContract(address governanceAddress, string memory json) public {
bool skipTransferOwnership = abi.decode(
json.parseRaw(".governance.skipTransferOwnership"),
(bool)
Expand Down
89 changes: 84 additions & 5 deletions packages/protocol/migrations_sol/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,96 @@
# @celo/devchain-anvil

Anvil state with Celo core contracts for local testing and development.
This package contains anvil state that allows you to start an [anvil](https://book.getfoundry.sh/reference/anvil/) instance in seconds.

# Usage
This anvil instance serves at `localhost:8545` on your machine, and comes pre-configured with core contracts like the [`Registry`](https://github.com/celo-org/celo-monorepo/blob/master/packages/protocol/contracts/common/Registry.sol) contract (at `0x000000000000000000000000000000000000ce10`). You can make RPC calls against this anvil instance as if you were interacting with Celo on `Alfajores` or `Mainnet`.

## Usage

```bash
npm install --save-dev @celo/devchain-anvil
anvil --state <path_to_devchain.json>
```

Files in this package:

1. Use `devchain.json` for a Celo L1-like devchain.
1. Use `l2-devchain.json` for a Celo L2-like devchain.

### Example

1. Make a test directory

```sh
# Create a demo directory
$ mkdir ~/Documents/local-anvil-demo

# Move into the demo directory
$ cd ~/Documents/local-anvil-demo
```

2. Install the package

```sh
$ npm install --save-dev @celo/devchain-anvil
```

3. Start an anvil instance with the state file from the package

```sh
$ anvil --state node_modules/@celo/devchain-anvil/devchain.json

_ _
(_) | |
__ _ _ __ __ __ _ | |
/ _` | | '_ \ \ \ / / | | | |
| (_| | | | | | \ V / | | | |
\__,_| |_| |_| \_/ |_| |_|

0.2.0 (f625d0f 2024-04-02T00:16:42.824772000Z)
https://github.com/foundry-rs/foundry

Available Accounts
==================

(0) 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000.000000000000000000 ETH)

# ...
Listening on 127.0.0.1:8545
```

4. Make RPC calls against the anvil instance serving at `http://127.0.0.1:8545`

## Background

[Foundry](https://book.getfoundry.sh/reference/anvil/) allows you to start anvil instances with state you read from `json` files.
The idea is to save the state of an anvil instance at a certain point in time, and then load it back up later.

```
$ anvil --state <PATH>
This is an alias for both --load-state and --dump-state.

It initializes the chain with the state and block environment stored at the file, if it
exists, and dumps the chain's state on exit.
```

Source: [Anvil docs](https://book.getfoundry.sh/reference/cli/anvil?highlight=--state#anvil)

We use this feature to pre-configure an anvil instance with core contracts and other state, and then start it up in seconds for testing.
We call this a "devchain". The scripts we use to configure the devchain are here: [`celo-org/celo-monorepo/` > `packages/protocol/migrations_sol`](https://github.com/celo-org/celo-monorepo/tree/master/packages/protocol/migrations_sol)

## Limitations

The anvil instance is not a full Celo node. It is a lightweight, in-memory instance that you can use for testing.

1. It does not sync with the Celo network ❌
1. It does not persist data between sessions ❌
1. It does not support all RPC methods ❌
1. It does not support fee currency transactions (like [CIP64](https://github.com/celo-org/celo-proposals/blob/master/CIPs/cip-0064.md) or [CIP66](https://github.com/celo-org/celo-proposals/blob/master/CIPs/cip-0066.md)) ❌
1. It does not support all Celo pre-compiles ❌

## How we work

We are a GitHub-first team, which means we have a strong preference for communicating via GitHub.
We are a GitHub-first team, which means we have a strong preference for communicating via GitHub.
Please use GitHub to:

🐞 [File a bug report](https://github.com/celo-org/celo-monorepo/issues/new/choose)
Expand All @@ -25,6 +104,6 @@ Please use GitHub to:
🚔 [Report a security vulnerability](https://github.com/celo-org/celo-monorepo/issues/new/choose)

> [!TIP]
>
> Please avoid messaging us via Slack, Telegram, or email. We are more likely to respond to you on
>
> Please avoid messaging us via Slack, Telegram, or email. We are more likely to respond to you on
> GitHub than if you message us anywhere else. We actively monitor GitHub, and will get back to you shortly 🌟
6 changes: 5 additions & 1 deletion packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@
"truffle:migrate": "truffle migrate",
"devchain": "yarn ts-node scripts/devchain.ts",
"devchain:reset": "yarn devchain generate-tar .tmp/devchain.tar.gz --upto 29",
"anvil-devchain:start": "./scripts/foundry/create_and_migrate_anvil_devchain.sh",
"anvil-devchain:start-L1": "./scripts/foundry/create_and_migrate_anvil_devchain.sh",
"anvil-devchain:start-L2": "./scripts/foundry/create_and_migrate_anvil_l2_devchain.sh",
"anvil-devchain:status": "if nc -z localhost 8546; then echo 'Devchain is serving at http://localhost:8546'; else echo 'Devchain is not running.'; fi",
"anvil-devchain:stop": "./scripts/foundry/stop_anvil.sh",
"anvil-devchain:e2e-tests": "./scripts/foundry/run_e2e_tests_in_anvil.sh",
"anvil-devchain:integration-tests": "./scripts/foundry/run_integration_tests_in_anvil.sh",
"view-tags": "git for-each-ref 'refs/tags/core-contracts.*' --sort=-committerdate --format='%(color:magenta)%(committerdate:short) %(color:blue)%(tree) %(color:green)github.com/celo-org/celo-monorepo/releases/tag/%(color:yellow)%(refname:short)'",
"generate-stabletoken-files": "yarn ts-node ./scripts/generate-stabletoken-files.ts",
"truffle-verify": "yarn truffle run verify"
Expand Down
Loading
Loading