Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Aug 28, 2024
1 parent f230a76 commit 3f6c6e2
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl From<NonceHolderTestMode> for u8 {
#[test]
fn test_nonce_holder() {
let mut account = Account::random();
let hex_addr = hex::encode(account.address.to_fixed_bytes());
// let hex_addr = hex::encode(account.address.to_fixed_bytes());

let mut vm = VmTesterBuilder::new(HistoryEnabled)
.with_empty_in_memory_storage()
Expand Down
329 changes: 163 additions & 166 deletions core/lib/multivm/src/versions/vm_latest/tests/rollbacks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethabi::Token;
use zksync_contracts::{get_loadnext_contract, test_contracts::LoadnextContractExecutionParams};
use zksync_types::{get_nonce_key, Execute, Nonce, U256};
use zksync_types::{get_nonce_key, U256};

use crate::{
interface::{
Expand All @@ -10,176 +10,173 @@ use crate::{
},
tracers::dynamic::vm_1_5_0::DynTracer,
vm_latest::{
tests::{
tester::{DeployContractsTx, TransactionTestInfo, TxModifier, TxType, VmTesterBuilder},
utils::read_test_contract,
},
tests::tester::{DeployContractsTx, TxType, VmTesterBuilder},
types::internals::ZkSyncVmState,
BootloaderState, HistoryEnabled, HistoryMode, SimpleMemory, ToTracerPointer, VmTracer,
},
};

#[test]
fn test_vm_rollbacks() {
let mut vm = VmTesterBuilder::new(HistoryEnabled)
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.build();

let mut account = vm.rich_accounts[0].clone();
let counter = read_test_contract();
let tx_0 = account.get_deploy_tx(&counter, None, TxType::L2).tx;
let tx_1 = account.get_deploy_tx(&counter, None, TxType::L2).tx;
let tx_2 = account.get_deploy_tx(&counter, None, TxType::L2).tx;

let result_without_rollbacks = vm.execute_and_verify_txs(&vec![
TransactionTestInfo::new_processed(tx_0.clone(), false),
TransactionTestInfo::new_processed(tx_1.clone(), false),
TransactionTestInfo::new_processed(tx_2.clone(), false),
]);

// reset vm
vm.reset_with_empty_storage();

let result_with_rollbacks = vm.execute_and_verify_txs(&vec![
TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongSignatureLength.into()),
TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongMagicValue.into()),
TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongSignature.into()),
// The correct nonce is 0, this tx will fail
TransactionTestInfo::new_rejected(
tx_2.clone(),
TxModifier::WrongNonce(tx_2.nonce().unwrap(), Nonce(0)).into(),
),
// This tx will succeed
TransactionTestInfo::new_processed(tx_0.clone(), false),
// The correct nonce is 1, this tx will fail
TransactionTestInfo::new_rejected(
tx_0.clone(),
TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
),
// The correct nonce is 1, this tx will fail
TransactionTestInfo::new_rejected(
tx_2.clone(),
TxModifier::WrongNonce(tx_2.nonce().unwrap(), Nonce(1)).into(),
),
// This tx will succeed
TransactionTestInfo::new_processed(tx_1, false),
// The correct nonce is 2, this tx will fail
TransactionTestInfo::new_rejected(
tx_0.clone(),
TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
),
// This tx will succeed
TransactionTestInfo::new_processed(tx_2.clone(), false),
// This tx will fail
TransactionTestInfo::new_rejected(
tx_2.clone(),
TxModifier::NonceReused(tx_2.initiator_account(), tx_2.nonce().unwrap()).into(),
),
TransactionTestInfo::new_rejected(
tx_0.clone(),
TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
),
]);

// assert_eq!(result_without_rollbacks, result_with_rollbacks);
}

#[test]
fn test_vm_loadnext_rollbacks() {
let mut vm = VmTesterBuilder::new(HistoryEnabled)
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.build();
let mut account = vm.rich_accounts[0].clone();

let loadnext_contract = get_loadnext_contract();
let loadnext_constructor_data = &[Token::Uint(U256::from(100))];
let DeployContractsTx {
tx: loadnext_deploy_tx,
address,
..
} = account.get_deploy_tx_with_factory_deps(
&loadnext_contract.bytecode,
Some(loadnext_constructor_data),
loadnext_contract.factory_deps.clone(),
TxType::L2,
);

let loadnext_tx_1 = account.get_l2_tx_for_execute(
Execute {
contract_address: address,
calldata: LoadnextContractExecutionParams {
reads: 100,
writes: 100,
events: 100,
hashes: 500,
recursive_calls: 10,
deploys: 60,
}
.to_bytes(),
value: Default::default(),
factory_deps: vec![],
},
None,
);

let loadnext_tx_2 = account.get_l2_tx_for_execute(
Execute {
contract_address: address,
calldata: LoadnextContractExecutionParams {
reads: 100,
writes: 100,
events: 100,
hashes: 500,
recursive_calls: 10,
deploys: 60,
}
.to_bytes(),
value: Default::default(),
factory_deps: vec![],
},
None,
);

let result_without_rollbacks = vm.execute_and_verify_txs(&vec![
TransactionTestInfo::new_processed(loadnext_deploy_tx.clone(), false),
TransactionTestInfo::new_processed(loadnext_tx_1.clone(), false),
TransactionTestInfo::new_processed(loadnext_tx_2.clone(), false),
]);

// TODO: reset vm
// vm.reset_with_empty_storage();

// let result_with_rollbacks = vm.execute_and_verify_txs(&vec![
// TransactionTestInfo::new_processed(loadnext_deploy_tx.clone(), false),
// TransactionTestInfo::new_processed(loadnext_tx_1.clone(), true),
// TransactionTestInfo::new_rejected(
// loadnext_deploy_tx.clone(),
// TxModifier::NonceReused(
// loadnext_deploy_tx.initiator_account(),
// loadnext_deploy_tx.nonce().unwrap(),
// )
// .into(),
// ),
// TransactionTestInfo::new_processed(loadnext_tx_1, false),
// TransactionTestInfo::new_processed(loadnext_tx_2.clone(), true),
// TransactionTestInfo::new_processed(loadnext_tx_2.clone(), true),
// TransactionTestInfo::new_rejected(
// loadnext_deploy_tx.clone(),
// TxModifier::NonceReused(
// loadnext_deploy_tx.initiator_account(),
// loadnext_deploy_tx.nonce().unwrap(),
// )
// .into(),
// ),
// TransactionTestInfo::new_processed(loadnext_tx_2, false),
// ]);

// assert_eq!(result_without_rollbacks, result_with_rollbacks);
}
// #[test]
// fn test_vm_rollbacks() {
// let mut vm = VmTesterBuilder::new(HistoryEnabled)
// .with_empty_in_memory_storage()
// .with_execution_mode(TxExecutionMode::VerifyExecute)
// .with_random_rich_accounts(1)
// .build();

// let mut account = vm.rich_accounts[0].clone();
// let counter = read_test_contract();
// let tx_0 = account.get_deploy_tx(&counter, None, TxType::L2).tx;
// let tx_1 = account.get_deploy_tx(&counter, None, TxType::L2).tx;
// let tx_2 = account.get_deploy_tx(&counter, None, TxType::L2).tx;

// let result_without_rollbacks = vm.execute_and_verify_txs(&vec![
// TransactionTestInfo::new_processed(tx_0.clone(), false),
// TransactionTestInfo::new_processed(tx_1.clone(), false),
// TransactionTestInfo::new_processed(tx_2.clone(), false),
// ]);

// // reset vm
// vm.reset_with_empty_storage();

// let result_with_rollbacks = vm.execute_and_verify_txs(&vec![
// TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongSignatureLength.into()),
// TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongMagicValue.into()),
// TransactionTestInfo::new_rejected(tx_0.clone(), TxModifier::WrongSignature.into()),
// // The correct nonce is 0, this tx will fail
// TransactionTestInfo::new_rejected(
// tx_2.clone(),
// TxModifier::WrongNonce(tx_2.nonce().unwrap(), Nonce(0)).into(),
// ),
// // This tx will succeed
// TransactionTestInfo::new_processed(tx_0.clone(), false),
// // The correct nonce is 1, this tx will fail
// TransactionTestInfo::new_rejected(
// tx_0.clone(),
// TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
// ),
// // The correct nonce is 1, this tx will fail
// TransactionTestInfo::new_rejected(
// tx_2.clone(),
// TxModifier::WrongNonce(tx_2.nonce().unwrap(), Nonce(1)).into(),
// ),
// // This tx will succeed
// TransactionTestInfo::new_processed(tx_1, false),
// // The correct nonce is 2, this tx will fail
// TransactionTestInfo::new_rejected(
// tx_0.clone(),
// TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
// ),
// // This tx will succeed
// TransactionTestInfo::new_processed(tx_2.clone(), false),
// // This tx will fail
// TransactionTestInfo::new_rejected(
// tx_2.clone(),
// TxModifier::NonceReused(tx_2.initiator_account(), tx_2.nonce().unwrap()).into(),
// ),
// TransactionTestInfo::new_rejected(
// tx_0.clone(),
// TxModifier::NonceReused(tx_0.initiator_account(), tx_0.nonce().unwrap()).into(),
// ),
// ]);

// // assert_eq!(result_without_rollbacks, result_with_rollbacks);
// }

// #[test]
// fn test_vm_loadnext_rollbacks() {
// let mut vm = VmTesterBuilder::new(HistoryEnabled)
// .with_empty_in_memory_storage()
// .with_execution_mode(TxExecutionMode::VerifyExecute)
// .with_random_rich_accounts(1)
// .build();
// let mut account = vm.rich_accounts[0].clone();

// let loadnext_contract = get_loadnext_contract();
// let loadnext_constructor_data = &[Token::Uint(U256::from(100))];
// let DeployContractsTx {
// tx: loadnext_deploy_tx,
// address,
// ..
// } = account.get_deploy_tx_with_factory_deps(
// &loadnext_contract.bytecode,
// Some(loadnext_constructor_data),
// loadnext_contract.factory_deps.clone(),
// TxType::L2,
// );

// let loadnext_tx_1 = account.get_l2_tx_for_execute(
// Execute {
// contract_address: address,
// calldata: LoadnextContractExecutionParams {
// reads: 100,
// writes: 100,
// events: 100,
// hashes: 500,
// recursive_calls: 10,
// deploys: 60,
// }
// .to_bytes(),
// value: Default::default(),
// factory_deps: vec![],
// },
// None,
// );

// let loadnext_tx_2 = account.get_l2_tx_for_execute(
// Execute {
// contract_address: address,
// calldata: LoadnextContractExecutionParams {
// reads: 100,
// writes: 100,
// events: 100,
// hashes: 500,
// recursive_calls: 10,
// deploys: 60,
// }
// .to_bytes(),
// value: Default::default(),
// factory_deps: vec![],
// },
// None,
// );

// // let result_without_rollbacks = vm.execute_and_verify_txs(&vec![
// // TransactionTestInfo::new_processed(loadnext_deploy_tx.clone(), false),
// // TransactionTestInfo::new_processed(loadnext_tx_1.clone(), false),
// // TransactionTestInfo::new_processed(loadnext_tx_2.clone(), false),
// // ]);

// // TODO: reset vm
// // vm.reset_with_empty_storage();

// // let result_with_rollbacks = vm.execute_and_verify_txs(&vec![
// // TransactionTestInfo::new_processed(loadnext_deploy_tx.clone(), false),
// // TransactionTestInfo::new_processed(loadnext_tx_1.clone(), true),
// // TransactionTestInfo::new_rejected(
// // loadnext_deploy_tx.clone(),
// // TxModifier::NonceReused(
// // loadnext_deploy_tx.initiator_account(),
// // loadnext_deploy_tx.nonce().unwrap(),
// // )
// // .into(),
// // ),
// // TransactionTestInfo::new_processed(loadnext_tx_1, false),
// // TransactionTestInfo::new_processed(loadnext_tx_2.clone(), true),
// // TransactionTestInfo::new_processed(loadnext_tx_2.clone(), true),
// // TransactionTestInfo::new_rejected(
// // loadnext_deploy_tx.clone(),
// // TxModifier::NonceReused(
// // loadnext_deploy_tx.initiator_account(),
// // loadnext_deploy_tx.nonce().unwrap(),
// // )
// // .into(),
// // ),
// // TransactionTestInfo::new_processed(loadnext_tx_2, false),
// // ]);

// // assert_eq!(result_without_rollbacks, result_with_rollbacks);
// }

// Testing tracer that does not allow the recursion to go deeper than a certain limit
struct MaxRecursionTracer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use transaction_test_info::{ExpectedError, TransactionTestInfo, TxModifier};
pub(crate) use transaction_test_info::{ExpectedError, TransactionTestInfo};
pub(crate) use vm_tester::{
default_l1_batch, get_empty_storage, InMemoryStorageView, VmTester, VmTesterBuilder,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::{
vm_latest::{tests::tester::vm_tester::VmTester, HistoryEnabled},
};

// FIXME: remove the dead code allow
#[allow(unused_variables)]
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub(crate) enum TxModifier {
WrongSignatureLength,
Expand All @@ -17,6 +20,9 @@ pub(crate) enum TxModifier {
NonceReused(H160, Nonce),
}

// FIXME: remove the dead code allow
#[allow(unused_variables)]
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub(crate) enum TxExpectedResult {
Rejected { error: ExpectedError },
Expand Down Expand Up @@ -129,6 +135,8 @@ impl TransactionTestInfo {
}
}

// FIXME: remove allow dead code
#[allow(dead_code)]
pub(crate) fn new_processed(transaction: Transaction, should_be_rollbacked: bool) -> Self {
Self {
tx: transaction,
Expand Down Expand Up @@ -176,6 +184,8 @@ impl TransactionTestInfo {
}

impl VmTester<HistoryEnabled> {
// FIXME: remove allow dead code
#[allow(dead_code)]
pub(crate) fn execute_and_verify_txs(
&mut self,
txs: &[TransactionTestInfo],
Expand Down
Loading

0 comments on commit 3f6c6e2

Please sign in to comment.