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(l2): make deposits for the rich accounts #2095

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7918094
first approach
tomip01 Feb 26, 2025
4e9a279
make deposit after initialize
tomip01 Feb 26, 2025
5c2f4b2
advances
tomip01 Feb 26, 2025
29f9a04
fix
tomip01 Feb 27, 2025
1a5baa8
remove semi colon
tomip01 Feb 27, 2025
7fc9c89
Merge branch 'main' into l2/deposit-rich-accounts
tomip01 Feb 28, 2025
7bf25fb
parse correctly address
tomip01 Feb 28, 2025
75253d7
add volumes docker
tomip01 Feb 28, 2025
c097f15
Merge branch 'main' into l2/deposit-rich-accounts
tomip01 Feb 28, 2025
088aa6a
change genesis l2
tomip01 Feb 28, 2025
b7870bb
fix test
tomip01 Mar 5, 2025
ab81a36
Merge branch 'main' into l2/deposit-rich-accounts
tomip01 Mar 5, 2025
6619d58
use old genesis
tomip01 Mar 5, 2025
63319b8
refactor
tomip01 Mar 5, 2025
1b1dea5
revert genesis
tomip01 Mar 5, 2025
07d3399
remove dbg2
tomip01 Mar 5, 2025
7cf6e98
new rlp for test load
tomip01 Mar 5, 2025
d396388
refactor
tomip01 Mar 5, 2025
7d6b08c
display hash
tomip01 Mar 5, 2025
4094c84
change deposit value
tomip01 Mar 6, 2025
adf5165
fix integration test
tomip01 Mar 6, 2025
f03a3bf
change genesis
tomip01 Mar 6, 2025
5509d6b
change parse address
tomip01 Mar 6, 2025
53ae3b5
fix clippy
tomip01 Mar 6, 2025
c7bdaea
Merge branch 'main' into l2/deposit-rich-accounts
tomip01 Mar 6, 2025
dd4b420
Merge branch 'main' into l2/deposit-rich-accounts
tomip01 Mar 7, 2025
9ee11bc
change genesis l2
tomip01 Mar 7, 2025
38ae5b0
add missing accounts
tomip01 Mar 7, 2025
2399cb4
add parse utils
tomip01 Mar 7, 2025
bbe7b5f
improve message from deployer
tomip01 Mar 7, 2025
9436218
Merge branch 'main' into l2/deposit-rich-accounts
jrchatruc Mar 7, 2025
44baef8
Add a separate flag for deposits so we don't break deployments to dev…
jrchatruc Mar 7, 2025
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
1 change: 0 additions & 1 deletion cmd/ethrex_l2/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ async fn transfer_from(
calldata.clone(),
Overrides {
chain_id: Some(cfg.network.l2_chain_id),
nonce: Some(i),
value: if calldata.is_empty() {
Some(value)
} else {
Expand Down
11 changes: 9 additions & 2 deletions crates/l2/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: help init down clean restart cli update-cli-contracts init-local-l1 init-l1 down-local-l1 restart-local-l1 rm-db-l1 clean-contract-deps restart-contract-deps deploy-l1 init-l2 down-l2 restart-l2 init-prover rm-db-l2 purge_prover_state ci_test test
.PHONY: help init down clean restart cli update-cli-contracts init-local-l1 init-l1 down-local-l1 restart-local-l1 rm-db-l1 clean-contract-deps restart-contract-deps deploy-l1 init-l2 down-l2 restart-l2 init-prover rm-db-l2 purge_prover_state ci_test test init-devnet deploy-l1-devnet

.DEFAULT_GOAL := help

Expand All @@ -9,7 +9,10 @@ L1_GENESIS_FILE_PATH=../../test_data/genesis-l1-dev.json
help: ## 📚 Show help for each of the Makefile recipes
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

init: init-local-l1 deploy-l1 update-system-contracts init-l2 ## 🚀 Initializes a localnet with Lambda ethrex client as both L1 and L2
init: init-local-l1 deploy-l1 update-system-contracts init-l2 ## 🚀 Initializes a localnet with Lambda ethrex client as both L1 and L2

## Same as init but does not do deposits for rich accounts since that doesn't make sense for deployments to devnets
init-devnet: init-local-l1 deploy-l1-devnet update-system-contracts init-l2

down: down-local-l1 down-l2 down-metrics## 🛑 Shuts down the localnet

Expand Down Expand Up @@ -101,6 +104,10 @@ clean-contract-deps: ## 🧹 Cleans the dependencies for the L1 contracts.
restart-contract-deps: clean-contract-deps ## 🔄 Restarts the dependencies for the L1 contracts.

deploy-l1: ## 📜 Deploys the L1 contracts
DEPLOYER_CONTRACTS_PATH=contracts CONFIG_FILE=config.toml cargo run --release --bin ethrex_l2_l1_deployer --manifest-path ${ethrex_L2_CONTRACTS_PATH}/Cargo.toml -- --deposit_rich

## Same as deploy-l1 but does not do deposits for rich accounts since that doesn't make sense for deployments to devnets
deploy-l1-devnet: ## 📜 Deploys the L1 contracts
DEPLOYER_CONTRACTS_PATH=contracts CONFIG_FILE=config.toml cargo run --release --bin ethrex_l2_l1_deployer --manifest-path ${ethrex_L2_CONTRACTS_PATH}/Cargo.toml

update-system-contracts:
Expand Down
79 changes: 79 additions & 0 deletions crates/l2/contracts/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bytes::Bytes;
use colored::Colorize;
use ethereum_types::{Address, H160, H256};
use ethrex_common::U256;
use ethrex_l2::utils::config::errors;
use ethrex_l2::utils::config::{read_env_as_lines, read_env_file, write_env};
use ethrex_l2::utils::test_data_io::read_genesis_file;
use ethrex_l2_sdk::calldata::{encode_calldata, Value};
use ethrex_l2_sdk::get_address_from_secret_key;
use ethrex_rpc::clients::eth::{
errors::{CalldataEncodeError, EthClientError},
eth_sender::Overrides,
Expand All @@ -12,6 +15,7 @@ use ethrex_rpc::clients::eth::{
use keccak_hash::keccak;
use secp256k1::SecretKey;
use spinoff::{spinner, spinners, Color, Spinner};
use std::fs;
use std::{
path::{Path, PathBuf},
process::Command,
Expand Down Expand Up @@ -121,6 +125,13 @@ If running locally, a reasonable value would be CONFIG_FILE=config.toml",
&setup_result.eth_client,
)
.await?;
let args = std::env::args().collect::<Vec<String>>();

if let Some(arg) = args.get(1) {
if arg == "--deposit_rich" {
make_deposits(bridge_address, &setup_result.eth_client).await?;
}
}

let env_lines = read_env_as_lines().map_err(DeployError::EnvFileError)?;

Expand Down Expand Up @@ -617,6 +628,74 @@ async fn wait_for_transaction_receipt(
Ok(())
}

async fn make_deposits(bridge: Address, eth_client: &EthClient) -> Result<(), DeployError> {
let genesis_l1_path = std::env::var("GENESIS_L1_PATH")
.unwrap_or("../../test_data/genesis-l1-dev.json".to_string());
let pks_path = std::env::var("PRIVATE_KEYS_PATH")
.unwrap_or("../../test_data/private_keys_l1.txt".to_string());
let genesis = read_genesis_file(&genesis_l1_path);
let pks = fs::read_to_string(&pks_path).map_err(|_| DeployError::FailedToGetStringFromPath)?;
let private_keys: Vec<String> = pks
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| line.trim().to_string())
.collect();

for pk in private_keys.iter() {
let secret_key = pk
.strip_prefix("0x")
.unwrap_or(pk)
.parse::<SecretKey>()
.map_err(|_| {
DeployError::DecodingError("Error while parsing private key".to_string())
})?;
let address = get_address_from_secret_key(&secret_key)?;
let values = vec![Value::Address(address)];
let calldata = encode_calldata("deposit(address)", &values)?;
let Some(acc) = genesis.alloc.get(&address) else {
println!(
"Skipping deposit for address {:?} as it is not in the genesis file",
address
);
continue;
};
let value_to_deposit = acc
.balance
.checked_div(U256::from_str("2").unwrap_or(U256::zero()))
.unwrap_or(U256::zero());
let overrides = Overrides {
value: Some(value_to_deposit),
from: Some(address),
gas_limit: Some(21000 * 5),
..Overrides::default()
};

let build = eth_client
.build_eip1559_transaction(bridge, address, Bytes::from(calldata), overrides, 1)
.await?;

match eth_client
.send_eip1559_transaction(&build, &secret_key)
.await
{
Ok(hash) => {
println!(
"Deposit transaction sent to L1 from {:?} with value {:?} and hash {:?}",
address, value_to_deposit, hash
);
}
Err(e) => {
println!(
"Failed to deposit to {:?} with value {:?}",
address, value_to_deposit
);
return Err(DeployError::EthClientError(e));
}
}
}
Ok(())
}

#[allow(clippy::unwrap_used)]
#[allow(clippy::expect_used)]
#[allow(clippy::panic)]
Expand Down
4 changes: 4 additions & 0 deletions crates/l2/docker-compose-l2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ services:
- ./contracts:${CI_ETHREX_WORKDIR}/contracts
- ./config.toml:${CI_ETHREX_WORKDIR}/config.toml
- ./.env:${CI_ETHREX_WORKDIR}/.env
- ../../test_data/genesis-l1-dev.json:/test_data/genesis-l1-dev.json
- ../../test_data/private_keys_l1.txt:/test_data/private_keys_l1.txt
environment:
- ETH_RPC_URL=http://ethrex_l1:8545
# NOTE: The paths in the env variables must match those
# specified in the `volumes:` section
- DEPLOYER_CONTRACTS_PATH=${CI_ETHREX_WORKDIR}/contracts
- ENV_FILE=${CI_ETHREX_WORKDIR}/.env
- CONFIG_FILE=${CI_ETHREX_WORKDIR}/config.toml
- GENESIS_L1_PATH=/test_data/genesis-l1-dev.json
- PRIVATE_KEYS_PATH=/test_data/private_keys_l1.txt
depends_on:
- ethrex
restart: on-failure:3
Expand Down
31 changes: 14 additions & 17 deletions crates/l2/proposer/l1_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::{
proposer::errors::L1WatcherError,
utils::config::{errors::ConfigError, eth::EthConfig, l1_watcher::L1WatcherConfig},
utils::{
config::{errors::ConfigError, eth::EthConfig, l1_watcher::L1WatcherConfig},
parse::hash_to_address,
},
};
use bytes::Bytes;
use ethereum_types::{Address, BigEndianHash, H256, U256};
use ethereum_types::{Address, H256, U256};
use ethrex_blockchain::{constants::TX_GAS_COST, Blockchain};
use ethrex_common::types::{Signable, Transaction};
use ethrex_rpc::clients::eth::{errors::EthClientError, eth_sender::Overrides, EthClient};
Expand Down Expand Up @@ -190,21 +193,15 @@ impl L1Watcher {
"Failed to parse mint value from log: {e:#?}"
))
})?;
let beneficiary_uint = log
.log
.topics
.get(2)
.ok_or(L1WatcherError::FailedToDeserializeLog(
"Failed to parse beneficiary from log: log.topics[2] out of bounds".to_owned(),
))?
.into_uint();
let beneficiary = format!("{beneficiary_uint:#x}")
.parse::<Address>()
.map_err(|e| {
L1WatcherError::FailedToDeserializeLog(format!(
"Failed to parse beneficiary from log: {e:#?}"
))
})?;
let beneficiary_hash =
log.log
.topics
.get(2)
.ok_or(L1WatcherError::FailedToDeserializeLog(
"Failed to parse beneficiary from log: log.topics[2] out of bounds"
.to_owned(),
))?;
let beneficiary = hash_to_address(*beneficiary_hash);

let deposit_id =
log.log
Expand Down
2 changes: 1 addition & 1 deletion crates/l2/prover/tests/perf_zkvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async fn setup() -> (ProgramInput, Block) {
store: store.clone(),
block_hash: block_to_prove.header.parent_hash,
};
let db = store.to_exec_db(&block_to_prove).unwrap();
let db = store.to_exec_db(block_to_prove).unwrap();

let input = ProgramInput {
block: block_to_prove.clone(),
Expand Down
19 changes: 18 additions & 1 deletion crates/l2/prover/zkvm/interface/sp1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions crates/l2/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ async fn l2_integration_test() -> Result<(), Box<dyn std::error::Error>> {
let l1_rich_wallet_address = l1_rich_wallet_address();

let l1_initial_balance = eth_client.get_balance(l1_rich_wallet_address).await?;
let l2_initial_balance = proposer_client.get_balance(l1_rich_wallet_address).await?;
let mut l2_initial_balance = proposer_client.get_balance(l1_rich_wallet_address).await?;
println!("Waiting for L2 to update for initial deposit");
let mut retries = 0;
while retries < 30 && l2_initial_balance.is_zero() {
std::thread::sleep(std::time::Duration::from_secs(2));
println!("[{retries}/30] Waiting for L2 balance to update");
l2_initial_balance = proposer_client.get_balance(l1_rich_wallet_address).await?;
retries += 1;
}
assert_ne!(retries, 30, "L2 balance is zero");
let common_bridge_initial_balance = eth_client.get_balance(common_bridge_address()).await?;

println!("L1 initial balance: {l1_initial_balance}");
println!("L2 initial balance: {l2_initial_balance}");
println!("Common Bridge initial balance: {common_bridge_initial_balance}");

let recoverable_fees_vault_balance = proposer_client.get_balance(fees_vault()).await?;
println!(
Expand Down Expand Up @@ -111,7 +122,10 @@ async fn l2_integration_test() -> Result<(), Box<dyn std::error::Error>> {

let common_bridge_locked_balance = eth_client.get_balance(common_bridge_address()).await?;
// Check that the deposit amount is the amount locked by the CommonBridge
assert_eq!(common_bridge_locked_balance, deposit_value);
assert_eq!(
common_bridge_locked_balance,
common_bridge_initial_balance + deposit_value
);

println!("L2 deposit received");

Expand Down Expand Up @@ -303,15 +317,17 @@ async fn l2_integration_test() -> Result<(), Box<dyn std::error::Error>> {
// Check that we only have the amount left after the withdrawal
assert_eq!(
common_bridge_locked_balance,
deposit_value - withdraw_value,
common_bridge_initial_balance + deposit_value - withdraw_value,
"Amount after withdrawal differs"
);

// Check that the total_locked_l2_value_with_recoverable_fees matches the common_bridge_locked_balance - burned_fees
// Check that we only have the amount left after the withdrawal
assert_eq!(
common_bridge_locked_balance,
total_locked_l2_value_with_recoverable_fees + total_burned_fees,
common_bridge_initial_balance
+ total_locked_l2_value_with_recoverable_fees
+ total_burned_fees,
"Amount calculated after withdrawal differs"
);

Expand Down
1 change: 1 addition & 0 deletions crates/l2/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod config;
pub mod error;
pub mod parse;
pub mod prover;
pub mod test_data_io;
5 changes: 5 additions & 0 deletions crates/l2/utils/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use ethereum_types::{Address, H256};

pub fn hash_to_address(hash: H256) -> Address {
Address::from_slice(&hash.as_fixed_bytes()[12..])
}
Loading
Loading