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: Benchmark ERC20 token transfers #3281

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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: 9 additions & 3 deletions core/tests/vm-benchmark/benches/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughpu
use rand::{rngs::StdRng, Rng, SeedableRng};
use vm_benchmark::{
criterion::{is_test_mode, BenchmarkGroup, BenchmarkId, CriterionExt, MeteredTime},
get_deploy_tx_with_gas_limit, get_heavy_load_test_tx, get_load_test_deploy_tx,
get_load_test_tx, get_realistic_load_test_tx, get_transfer_tx, BenchmarkingVm,
BenchmarkingVmFactory, Bytecode, Fast, Legacy, LoadTestParams,
get_deploy_tx_with_gas_limit, get_erc20_deploy_tx, get_erc20_transfer_tx,
get_heavy_load_test_tx, get_load_test_deploy_tx, get_load_test_tx, get_realistic_load_test_tx,
get_transfer_tx, BenchmarkingVm, BenchmarkingVmFactory, Bytecode, Fast, Legacy, LoadTestParams,
};
use zksync_types::Transaction;

Expand Down Expand Up @@ -146,6 +146,12 @@ fn bench_fill_bootloader<VM: BenchmarkingVmFactory, const FULL: bool>(
run_vm::<VM, FULL>(&mut group, "load_test_heavy", &txs);
drop(txs);

// ERC-20 token transfers
let txs = (1..=max_txs).map(get_erc20_transfer_tx);
let txs: Vec<_> = iter::once(get_erc20_deploy_tx()).chain(txs).collect();
run_vm::<VM, FULL>(&mut group, "erc20_transfer", &txs);
drop(txs);

// Base token transfers
let txs: Vec<_> = (0..max_txs).map(get_transfer_tx).collect();
run_vm::<VM, FULL>(&mut group, "transfer", &txs);
Expand Down
6 changes: 3 additions & 3 deletions core/tests/vm-benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use zksync_types::Transaction;

pub use crate::{
transaction::{
get_deploy_tx, get_deploy_tx_with_gas_limit, get_heavy_load_test_tx,
get_load_test_deploy_tx, get_load_test_tx, get_realistic_load_test_tx, get_transfer_tx,
LoadTestParams,
get_deploy_tx, get_deploy_tx_with_gas_limit, get_erc20_deploy_tx, get_erc20_transfer_tx,
get_heavy_load_test_tx, get_load_test_deploy_tx, get_load_test_tx,
get_realistic_load_test_tx, get_transfer_tx, LoadTestParams,
},
vm::{BenchmarkingVm, BenchmarkingVmFactory, CountInstructions, Fast, Legacy, VmLabel},
};
Expand Down
64 changes: 63 additions & 1 deletion core/tests/vm-benchmark/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use zksync_contracts::{deployer_contract, TestContract};
use zksync_multivm::utils::get_max_gas_per_pubdata_byte;
use zksync_types::{
bytecode::BytecodeHash,
ethabi::{encode, Token},
ethabi::{self, encode, Token},
fee::Fee,
l2::L2Tx,
utils::deployed_address_create,
Expand All @@ -14,12 +14,17 @@ use zksync_types::{

const LOAD_TEST_MAX_READS: usize = 3000;

const ERC20_CONTRACT_PATH: &str =
"etc/contracts-test-data/artifacts-zk/contracts/transfer/ERC20.sol/TestERC20.json";

pub(crate) static PRIVATE_KEY: Lazy<K256PrivateKey> =
Lazy::new(|| K256PrivateKey::from_bytes(H256([42; 32])).expect("invalid key bytes"));
static LOAD_TEST_CONTRACT_ADDRESS: Lazy<Address> =
Lazy::new(|| deployed_address_create(PRIVATE_KEY.address(), 0.into()));

static LOAD_TEST_CONTRACT: Lazy<TestContract> = Lazy::new(zksync_contracts::get_loadnext_contract);
static ERC20_TEST_CONTRACT: Lazy<ethabi::Contract> =
Lazy::new(|| zksync_contracts::load_contract(ERC20_CONTRACT_PATH));

static CREATE_FUNCTION_SIGNATURE: Lazy<[u8; 4]> = Lazy::new(|| {
deployer_contract()
Expand Down Expand Up @@ -92,6 +97,63 @@ pub fn get_transfer_tx(nonce: u32) -> Transaction {
signed.into()
}

pub fn get_erc20_transfer_tx(nonce: u32) -> Transaction {
let transfer_fn = ERC20_TEST_CONTRACT.function("transfer").unwrap();
let calldata = transfer_fn
.encode_input(&[
Token::Address(Address::repeat_byte(1)),
Token::Uint(1.into()),
])
.unwrap();

let mut signed = L2Tx::new_signed(
Some(*LOAD_TEST_CONTRACT_ADDRESS),
calldata,
Nonce(nonce),
tx_fee(1_000_000),
0.into(), // value
L2ChainId::from(270),
&PRIVATE_KEY,
vec![], // factory deps
Default::default(), // paymaster params
)
.expect("should create a signed execute transaction");

signed.set_input(H256::random().as_bytes().to_vec(), H256::random());
signed.into()
}

pub fn get_erc20_deploy_tx() -> Transaction {
let bytecode = zksync_contracts::read_bytecode(ERC20_CONTRACT_PATH);
let calldata = [Token::Uint(U256::one() << 128)]; // initial token amount minted to the deployer
let params = [
Token::FixedBytes(vec![0_u8; 32]),
Token::FixedBytes(BytecodeHash::for_bytecode(&bytecode).value().0.to_vec()),
Token::Bytes(encode(&calldata)),
];
let create_calldata = CREATE_FUNCTION_SIGNATURE
.iter()
.cloned()
.chain(encode(&params))
.collect();

let mut signed = L2Tx::new_signed(
Some(CONTRACT_DEPLOYER_ADDRESS),
create_calldata,
Nonce(0),
tx_fee(500_000_000),
U256::zero(),
L2ChainId::from(270),
&PRIVATE_KEY,
vec![bytecode],
Default::default(),
)
.expect("should create a signed execute transaction");

signed.set_input(H256::random().as_bytes().to_vec(), H256::random());
signed.into()
}

pub fn get_load_test_deploy_tx() -> Transaction {
let calldata = [Token::Uint(LOAD_TEST_MAX_READS.into())];
let params = [
Expand Down
16 changes: 15 additions & 1 deletion core/tests/vm-benchmark/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ mod tests {
use super::*;
use crate::{
get_deploy_tx, get_heavy_load_test_tx, get_load_test_deploy_tx, get_load_test_tx,
get_realistic_load_test_tx, get_transfer_tx, LoadTestParams, BYTECODES,
get_realistic_load_test_tx, get_transfer_tx,
transaction::{get_erc20_deploy_tx, get_erc20_transfer_tx},
LoadTestParams, BYTECODES,
};

#[test]
Expand All @@ -261,6 +263,18 @@ mod tests {
assert_matches!(res.result, ExecutionResult::Success { .. });
}

#[test]
fn can_erc20_transfer() {
let mut vm = BenchmarkingVm::new();
let res = vm.run_transaction(&get_erc20_deploy_tx());
assert_matches!(res.result, ExecutionResult::Success { .. });

for nonce in 1..=5 {
let res = vm.run_transaction(&get_erc20_transfer_tx(nonce));
assert_matches!(res.result, ExecutionResult::Success { .. });
}
}

#[test]
fn can_load_test() {
let mut vm = BenchmarkingVm::new();
Expand Down
11 changes: 11 additions & 0 deletions etc/contracts-test-data/contracts/transfer/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract TestERC20 is ERC20("Test", "TEST") {
constructor(uint256 _toMint) {
_mint(msg.sender, _toMint);
}
}
Loading