-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: vm2 account validation (#2863)
Implements an account validation gas limit and the validation tracer for vm2, along with better tests for validation. Instead of a second gas limit like in vm_latest, the normal gas limit is used. Unfortunately this means that the VM is not safe to use in the sequencer until we forbid the use of gasleft. I didn't do it here because it requires something like taint analysis and could break existing contracts that didn't know that gasleft is forbidden. --------- Co-authored-by: Alex Ostrovski <[email protected]> Co-authored-by: Alex Ostrovski <[email protected]>
- Loading branch information
1 parent
18e4307
commit af149a0
Showing
33 changed files
with
1,220 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/lib/multivm/src/versions/testonly/account_validation_rules.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use assert_matches::assert_matches; | ||
use zksync_test_contracts::TestContract; | ||
use zksync_types::{u256_to_h256, AccountTreeId, Address, StorageKey}; | ||
use zksync_vm_interface::tracer::ViolatedValidationRule; | ||
|
||
use super::{ | ||
get_empty_storage, require_eip712::make_aa_transaction, tester::VmTesterBuilder, | ||
ContractToDeploy, TestedVm, TestedVmForValidation, | ||
}; | ||
use crate::interface::TxExecutionMode; | ||
|
||
/// Checks that every limitation imposed on account validation results in an appropriate error. | ||
/// The actual misbehavior cases are found in "validation-rule-breaker.sol". | ||
pub(crate) fn test_account_validation_rules<VM: TestedVm + TestedVmForValidation>() { | ||
assert_matches!(test_rule::<VM>(0), None); | ||
assert_matches!( | ||
test_rule::<VM>(1), | ||
Some(ViolatedValidationRule::TouchedDisallowedStorageSlots(_, _)) | ||
); | ||
assert_matches!( | ||
test_rule::<VM>(2), | ||
Some(ViolatedValidationRule::CalledContractWithNoCode(_)) | ||
); | ||
assert_matches!(test_rule::<VM>(3), None); | ||
assert_matches!( | ||
test_rule::<VM>(4), | ||
Some(ViolatedValidationRule::TookTooManyComputationalGas(_)) | ||
) | ||
} | ||
|
||
fn test_rule<VM: TestedVm + TestedVmForValidation>(rule: u32) -> Option<ViolatedValidationRule> { | ||
let aa_address = Address::repeat_byte(0x10); | ||
let beneficiary_address = Address::repeat_byte(0x20); | ||
|
||
// Set the type of misbehaviour of the AA contract | ||
let mut storage_with_rule_break_set = get_empty_storage(); | ||
storage_with_rule_break_set.set_value( | ||
StorageKey::new(AccountTreeId::new(aa_address), u256_to_h256(0.into())), | ||
u256_to_h256(rule.into()), | ||
); | ||
|
||
let bytecode = TestContract::validation_test().bytecode.to_vec(); | ||
let mut vm = VmTesterBuilder::new() | ||
.with_empty_in_memory_storage() | ||
.with_custom_contracts(vec![ | ||
ContractToDeploy::account(bytecode, aa_address).funded() | ||
]) | ||
.with_storage(storage_with_rule_break_set) | ||
.with_execution_mode(TxExecutionMode::VerifyExecute) | ||
.with_rich_accounts(1) | ||
.build::<VM>(); | ||
|
||
let private_account = vm.rich_accounts[0].clone(); | ||
|
||
vm.vm.run_validation( | ||
make_aa_transaction(aa_address, beneficiary_address, &private_account), | ||
55, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
core/lib/multivm/src/versions/vm_fast/tests/account_validation_rules.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use super::TestedFastVm; | ||
use crate::versions::testonly::account_validation_rules::test_account_validation_rules; | ||
|
||
#[test] | ||
fn test_account_validation_rules_fast() { | ||
test_account_validation_rules::<TestedFastVm<(), _>>(); | ||
} |
Oops, something went wrong.