Skip to content

Commit

Permalink
reduce repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Nov 14, 2024
1 parent 40484a7 commit 9df63c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 55 deletions.
49 changes: 7 additions & 42 deletions core/lib/multivm/src/versions/testonly/account_validation_rules.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use assert_matches::assert_matches;
use zksync_types::{
fee::Fee, l2::L2Tx, transaction_request::TransactionRequest, u256_to_h256, AccountTreeId,
Address, Eip712Domain, L2ChainId, StorageKey, U256,
};
use zksync_types::{u256_to_h256, AccountTreeId, Address, StorageKey};
use zksync_vm_interface::tracer::ViolatedValidationRule;

use super::{
get_empty_storage, read_validation_test_contract, tester::VmTesterBuilder, ContractToDeploy,
TestedVm, TestedVmForValidation,
get_empty_storage, read_validation_test_contract, require_eip712::make_aa_transaction,
tester::VmTesterBuilder, ContractToDeploy, TestedVm, TestedVmForValidation,
};
use crate::interface::TxExecutionMode;

Expand Down Expand Up @@ -54,40 +51,8 @@ fn test_rule<VM: TestedVm + TestedVmForValidation>(rule: u32) -> Option<Violated

let private_account = vm.rich_accounts[0].clone();

// Use account abstraction
let chain_id: u32 = 270;

let tx_712 = L2Tx::new(
Some(beneficiary_address),
vec![],
private_account.nonce,
Fee {
gas_limit: U256::from(1000000000),
max_fee_per_gas: U256::from(1000000000),
max_priority_fee_per_gas: U256::from(1000000000),
gas_per_pubdata_limit: U256::from(1000000000),
},
aa_address,
U256::from(28374938),
vec![],
Default::default(),
);

let mut transaction_request: TransactionRequest = tx_712.into();
transaction_request.chain_id = Some(chain_id.into());

let domain = Eip712Domain::new(L2ChainId::from(chain_id));
let signature = private_account
.get_pk_signer()
.sign_typed_data(&domain, &transaction_request)
.unwrap();
let encoded_tx = transaction_request.get_signed_bytes(&signature).unwrap();

let (aa_txn_request, aa_hash) =
TransactionRequest::from_bytes(&encoded_tx, L2ChainId::from(chain_id)).unwrap();

let mut l2_tx = L2Tx::from_request(aa_txn_request, 100000, false).unwrap();
l2_tx.set_input(encoded_tx, aa_hash);

vm.vm.run_validation(l2_tx, 55)
vm.vm.run_validation(
make_aa_transaction(aa_address, beneficiary_address, &private_account),
55,
)
}
38 changes: 25 additions & 13 deletions core/lib/multivm/src/versions/testonly/require_eip712.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ethabi::Token;
use zksync_eth_signer::TransactionParameters;
use zksync_test_account::Account;
use zksync_types::{
fee::Fee, l2::L2Tx, transaction_request::TransactionRequest, Address, Eip712Domain, Execute,
L2ChainId, Nonce, Transaction, U256,
Expand Down Expand Up @@ -31,7 +32,6 @@ pub(crate) fn test_require_eip712<VM: TestedVm>() {
.with_rich_accounts(1)
.build::<VM>();
assert_eq!(vm.get_eth_balance(beneficiary_address), U256::from(0));
let chain_id: u32 = 270;
let mut private_account = vm.rich_accounts[0].clone();

// First, let's set the owners of the AA account to the `private_address`.
Expand Down Expand Up @@ -99,6 +99,29 @@ pub(crate) fn test_require_eip712<VM: TestedVm>() {
);

// // Now send the 'classic' EIP712 transaction

let transaction: Transaction =
make_aa_transaction(aa_address, beneficiary_address, &private_account).into();
vm.vm.push_transaction(transaction);
vm.vm.execute(InspectExecutionMode::OneTx);

assert_eq!(
vm.get_eth_balance(beneficiary_address),
U256::from(916375026)
);
assert_eq!(
private_account_balance,
vm.get_eth_balance(private_account.address)
);
}

pub(crate) fn make_aa_transaction(
aa_address: Address,
beneficiary_address: Address,
private_account: &Account,
) -> L2Tx {
let chain_id: u32 = 270;

let tx_712 = L2Tx::new(
Some(beneficiary_address),
vec![],
Expand Down Expand Up @@ -131,16 +154,5 @@ pub(crate) fn test_require_eip712<VM: TestedVm>() {
let mut l2_tx = L2Tx::from_request(aa_txn_request, 100000, false).unwrap();
l2_tx.set_input(encoded_tx, aa_hash);

let transaction: Transaction = l2_tx.into();
vm.vm.push_transaction(transaction);
vm.vm.execute(InspectExecutionMode::OneTx);

assert_eq!(
vm.get_eth_balance(beneficiary_address),
U256::from(916375026)
);
assert_eq!(
private_account_balance,
vm.get_eth_balance(private_account.address)
);
l2_tx
}

0 comments on commit 9df63c2

Please sign in to comment.