From 42f3f8111fbe03393c9ce6f95203a53bcc5220c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Calv=C3=ADn=20Garc=C3=ADa?= Date: Tue, 26 Mar 2024 20:34:00 +0100 Subject: [PATCH] feat: Add blobs execution to example (#210) * add config files * execute scenario with 20 accounts and 200 deploy tx per account * rename methods and add transfer_eth and withdraw_eth methods * add deploy_erc20 scenario * add mint_erc20 scenario and transfer_erc20 scenario --- .gitignore | 2 + Makefile | 4 +- etc/env/main_demo_rollup_blobs.toml | 13 ++ etc/env/main_demo_validium_blobs.toml | 12 ++ validium_mode_example/README.md | 2 +- validium_mode_example/src/constants.rs | 4 +- validium_mode_example/src/helpers/mod.rs | 54 +++++++-- validium_mode_example/src/helpers/tx_kind.rs | 6 +- validium_mode_example/src/main.rs | 6 +- validium_mode_example/src/scenario.rs | 121 ++++++++++++++++++- 10 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 etc/env/main_demo_rollup_blobs.toml create mode 100644 etc/env/main_demo_validium_blobs.toml diff --git a/.gitignore b/.gitignore index c9540aea1036..82a8d53eb07e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,8 @@ Cargo.lock !/etc/env/base !/etc/env/dev.toml !/etc/env/dev_validium.toml +!/etc/env/main_demo_rollup_blobs.toml +!/etc/env/main_demo_validium_blobs.toml !/etc/env/dev_validium_docker.toml !/etc/env/docker.toml !/etc/env/ext-node.toml diff --git a/Makefile b/Makefile index f889a3cf16b6..59e04aee4995 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ demo_validium_calldata: zk && zk clean --all && zk env validium_calldata && zk init --validium-mode --run-observability && zk server demo_validium_blobs: - zk && zk clean --all && zk env validium_blobs && zk init --validium-mode --run-observability && zk server + zk && zk clean --all && zk env main_demo_validium_blobs && zk init --validium-mode --run-observability && zk server demo_rollup_calldata: zk && zk clean --all && zk env rollup_calldata && zk init --run-observability && zk server demo_rollup_blobs: - zk && zk clean --all && zk env rollup_blobs && zk init --run-observability && zk server + zk && zk clean --all && zk env main_demo_rollup_blobs && zk init --run-observability && zk server diff --git a/etc/env/main_demo_rollup_blobs.toml b/etc/env/main_demo_rollup_blobs.toml new file mode 100644 index 000000000000..9a0421635322 --- /dev/null +++ b/etc/env/main_demo_rollup_blobs.toml @@ -0,0 +1,13 @@ +[chain.state_keeper] +transaction_slots=5000 +block_commit_deadline_ms=1800000 +max_pubdata_per_batch=240000 + +[eth_sender.gas_adjuster] +internal_enforced_l1_gas_price=45000000000 + +[eth_sender.sender] +pubdata_sending_mode="Blobs" + +[_metadata] +base = ["dev.toml"] diff --git a/etc/env/main_demo_validium_blobs.toml b/etc/env/main_demo_validium_blobs.toml new file mode 100644 index 000000000000..2d0152f2e332 --- /dev/null +++ b/etc/env/main_demo_validium_blobs.toml @@ -0,0 +1,12 @@ +[chain.state_keeper] +transaction_slots=5000 +block_commit_deadline_ms=1800000 + +[eth_sender.gas_adjuster] +internal_enforced_l1_gas_price=45000000000 + +[eth_sender.sender] +pubdata_sending_mode="Blobs" + +[_metadata] +base = ["dev_validium.toml"] diff --git a/validium_mode_example/README.md b/validium_mode_example/README.md index 55e9cac9e952..a8a2dbab52ce 100644 --- a/validium_mode_example/README.md +++ b/validium_mode_example/README.md @@ -23,7 +23,7 @@ In this example we're going to run some transactions. Once the server is running, run this command in other terminal: ```sh -cargo run --release --bin zksync_full_stack +cargo run --release --bin validium_mode_example ``` This test does the following: diff --git a/validium_mode_example/src/constants.rs b/validium_mode_example/src/constants.rs index a3daf0ab5575..7aa687559418 100644 --- a/validium_mode_example/src/constants.rs +++ b/validium_mode_example/src/constants.rs @@ -1,7 +1,7 @@ pub static ERA_PROVIDER_URL: &str = "http://127.0.0.1:3050"; pub static PRIVATE_KEY: &str = "7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110"; -pub static CONTRACT_BIN: &str = include_str!("../ERC20.bin"); -pub static CONTRACT_ABI: &str = include_str!("../ERC20.abi"); +pub static ERC20_BIN: &str = include_str!("../ERC20.bin"); +pub static ERC20_ABI: &str = include_str!("../ERC20.abi"); pub static L1_URL: &str = "http://localhost:8545"; diff --git a/validium_mode_example/src/helpers/mod.rs b/validium_mode_example/src/helpers/mod.rs index c9106d7c0b94..5c2e52744043 100644 --- a/validium_mode_example/src/helpers/mod.rs +++ b/validium_mode_example/src/helpers/mod.rs @@ -26,7 +26,7 @@ use zksync_web3_rs::{ providers::{Middleware, Provider}, signers::{LocalWallet, Signer}, zks_provider::ZKSProvider, - zks_wallet::{DeployRequest, DepositRequest}, + zks_wallet::{DeployRequest, DepositRequest, TransferRequest, WithdrawRequest}, ZKSWallet, }; @@ -61,7 +61,7 @@ pub async fn zks_wallet( .unwrap() } -pub async fn deposit( +pub async fn deposit_eth( from: Arc, SigningKey>>, to: Arc, SigningKey>>, ) { @@ -77,10 +77,12 @@ pub async fn deposit( .expect("Failed to perform deposit transaction"); } -pub async fn deploy(zks_wallet: Arc, SigningKey>>) -> TransactionReceipt { +pub async fn deploy_erc20( + zks_wallet: Arc, SigningKey>>, +) -> TransactionReceipt { // Read both files from disk: - let abi = Abi::load(constants::CONTRACT_ABI.as_bytes()).unwrap(); - let contract_bin = hex::decode(constants::CONTRACT_BIN).unwrap().to_vec(); + let abi = Abi::load(constants::ERC20_ABI.as_bytes()).unwrap(); + let contract_bin = hex::decode(constants::ERC20_BIN).unwrap().to_vec(); // DeployRequest sets the parameters for the constructor call and the deployment transaction. let request = DeployRequest::with( @@ -106,7 +108,7 @@ pub async fn deploy(zks_wallet: Arc, SigningKey>>) -> T l2_deploy_tx_receipt } -pub async fn mint( +pub async fn mint_erc20( zks_wallet: Arc, SigningKey>>, erc20_address: H160, ) -> TransactionReceipt { @@ -134,7 +136,24 @@ pub async fn mint( .unwrap() } -pub async fn transfer( +pub async fn transfer_eth( + from: Arc, SigningKey>>, + to: Arc, SigningKey>>, +) { + println!( + "{}", + format!("Transfer eth in {:?}", to.l2_address()).bright_yellow() + ); + let amount = parse_units("11", "ether").unwrap(); + let mut request = TransferRequest::new(amount.into()); + request.to = to.l2_address(); + + from.transfer(&request, None) + .await + .expect("Failed to perform transfer transaction"); +} + +pub async fn transfer_erc20( zks_wallet: Arc, SigningKey>>, erc20_address: H160, ) -> TransactionReceipt { @@ -188,6 +207,23 @@ pub async fn wait_for_l2_tx_details(tx_hash: H256) -> TransactionDetails { } } +pub async fn withdraw_eth( + from: Arc, SigningKey>>, + to: Arc, SigningKey>>, +) { + println!( + "{}", + format!("Withdrawing in {:?}", to.l2_address()).bright_yellow() + ); + let amount = parse_units("11", "ether").unwrap(); + let mut request = WithdrawRequest::new(amount.into()); + request.to = to.l2_address(); + + from.withdraw(&request) + .await + .expect("Failed to perform withdraw transaction"); +} + pub async fn wait_for_tx_receipt(client: Arc>, tx_hash: H256) -> TransactionReceipt { loop { let receipt = client @@ -240,7 +276,7 @@ pub async fn create_funded_account( main_wallet: Arc, SigningKey>>, ) -> Arc, SigningKey>> { let to = create_account(l1_provider, l2_provider).await; - let _ = deposit(main_wallet, to.clone()).await; + let _ = deposit_eth(main_wallet, to.clone()).await; to } @@ -256,7 +292,7 @@ pub async fn create_funded_accounts( let from = main_wallet.clone(); let to = create_account(l1_provider, l2_provider).await; accounts.push(to.clone()); - let _ = deposit(from, to).await; + let _ = deposit_eth(from, to).await; } accounts } diff --git a/validium_mode_example/src/helpers/tx_kind.rs b/validium_mode_example/src/helpers/tx_kind.rs index b317196f2c4a..56e5cd4e68fe 100644 --- a/validium_mode_example/src/helpers/tx_kind.rs +++ b/validium_mode_example/src/helpers/tx_kind.rs @@ -30,9 +30,9 @@ impl TxKind { .bright_yellow() ); match self { - TxKind::Deploy => super::deploy(zks_wallet).await, - TxKind::Mint => super::mint(zks_wallet, erc20_address).await, - TxKind::Transfer => super::transfer(zks_wallet, erc20_address).await, + TxKind::Deploy => super::deploy_erc20(zks_wallet).await, + TxKind::Mint => super::mint_erc20(zks_wallet, erc20_address).await, + TxKind::Transfer => super::transfer_erc20(zks_wallet, erc20_address).await, } } } diff --git a/validium_mode_example/src/main.rs b/validium_mode_example/src/main.rs index 296a5432efb3..411e9971ed95 100644 --- a/validium_mode_example/src/main.rs +++ b/validium_mode_example/src/main.rs @@ -2,6 +2,8 @@ use validium_mode_example::scenario; #[tokio::main(flavor = "current_thread")] async fn main() { - //scenario::run(1, 1, helpers::TxKind::Deploy).await; - scenario::basic().await; + //scenario::run(20, 200, TxKind::Deploy).await; + //scenario::basic().await; + scenario::mint_erc20(5, 5).await; + scenario::transfer_erc20(5, 5).await; } diff --git a/validium_mode_example/src/scenario.rs b/validium_mode_example/src/scenario.rs index bfeb93607b18..c7acb3c8d9a6 100644 --- a/validium_mode_example/src/scenario.rs +++ b/validium_mode_example/src/scenario.rs @@ -119,7 +119,7 @@ pub async fn run(accounts_count: usize, txs_per_account: usize, txs_kind: helper // In case that the txs_type is not Deploy we need the contract deployed. println!("{}", "Initial deploy".bright_yellow()); - let erc20_address = helpers::deploy(main_wallet.clone()) + let erc20_address = helpers::deploy_erc20(main_wallet.clone()) .await .contract_address .unwrap(); @@ -140,6 +140,119 @@ pub async fn run(accounts_count: usize, txs_per_account: usize, txs_kind: helper } } +// Each account will deploy txs_per_account ERC20 contracts +pub async fn deploy_erc20(n_accounts: usize, txs_per_account: usize) { + println!("{}", "Running deploy_erc20 scenario\n".bright_red().bold()); + let l1_provider = helpers::l1_provider(); + let l2_provider = helpers::l2_provider(); + let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await); + let accounts = helpers::create_funded_accounts( + n_accounts, + &l1_provider, + &l2_provider, + main_wallet.clone(), + ) + .await; + + let l2_txs_hashes = helpers::send_transactions( + helpers::TxKind::Deploy, + Default::default(), + accounts, + txs_per_account, + ) + .await; + + let data = ScenarioData::collect( + l2_txs_hashes, + l1_provider, + l2_provider, + main_wallet.get_era_provider().unwrap(), + ) + .await; + + for (_batch_number, batch_data) in data.0.iter() { + println!("{batch_data}"); + } +} + +pub async fn mint_erc20(n_accounts: usize, txs_per_account: usize) { + println!("{}", "Running mint_erc20 scenario\n".bright_red().bold()); + let l1_provider = helpers::l1_provider(); + let l2_provider = helpers::l2_provider(); + let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await); + let accounts = helpers::create_funded_accounts( + n_accounts, + &l1_provider, + &l2_provider, + main_wallet.clone(), + ) + .await; + + let deploy_receipt = helpers::deploy_erc20(main_wallet.clone()).await; + let erc20_address = deploy_receipt.contract_address.unwrap(); + + let l2_txs_hashes = helpers::send_transactions( + helpers::TxKind::Mint, + erc20_address, + accounts, + txs_per_account, + ) + .await; + + let data = ScenarioData::collect( + l2_txs_hashes, + l1_provider, + l2_provider, + main_wallet.get_era_provider().unwrap(), + ) + .await; + + for (_batch_number, batch_data) in data.0.iter() { + println!("{batch_data}"); + } +} + +pub async fn transfer_erc20(n_accounts: usize, txs_per_account: usize) { + println!( + "{}", + "Running transfer_erc20 scenario\n".bright_red().bold() + ); + let l1_provider = helpers::l1_provider(); + let l2_provider = helpers::l2_provider(); + let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await); + let accounts = helpers::create_funded_accounts( + n_accounts, + &l1_provider, + &l2_provider, + main_wallet.clone(), + ) + .await; + + let deploy_receipt = helpers::deploy_erc20(main_wallet.clone()).await; + let erc20_address = deploy_receipt.contract_address.unwrap(); + let _ = helpers::mint_erc20(main_wallet.clone(), erc20_address).await; + + let l2_txs_hashes = helpers::send_transactions( + helpers::TxKind::Transfer, + erc20_address, + accounts, + txs_per_account, + ) + .await; + + let data = ScenarioData::collect( + l2_txs_hashes, + l1_provider, + l2_provider, + main_wallet.get_era_provider().unwrap(), + ) + .await; + + for (_batch_number, batch_data) in data.0.iter() { + println!("{batch_data}"); + } +} + pub async fn basic() { println!( "{}", @@ -153,10 +266,10 @@ pub async fn basic() { let account = helpers::create_funded_account(&l1_provider, &l2_provider, main_wallet.clone()).await; - let deploy_receipt = helpers::deploy(account.clone()).await; + let deploy_receipt = helpers::deploy_erc20(account.clone()).await; let erc20_address = deploy_receipt.contract_address.unwrap(); - let mint_receipt = helpers::mint(account.clone(), erc20_address).await; - let transfer_receipt = helpers::transfer(account.clone(), erc20_address).await; + let mint_receipt = helpers::mint_erc20(account.clone(), erc20_address).await; + let transfer_receipt = helpers::transfer_erc20(account.clone(), erc20_address).await; println!( "{}",