Skip to content

Commit

Permalink
fix: extend allowed storage slots for validation as per EIP-7562 (mat…
Browse files Browse the repository at this point in the history
…ter-labs#3166)

## What ❔

[EIP-7562](https://eips.ethereum.org/EIPS/eip-7562#validation-rules)
allows reading `keccak256(address || x) + n` where `x` is `bytes32` and
`n` is `0..128`.

This PR adds support for the `+ n` as we didn't have it before.

## Why ❔

To support reading larger-than-1-slot structs from mappings, during
validation

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.

---------

Co-authored-by: Vlad Bochok <[email protected]>
  • Loading branch information
ly0va and vladbochok authored Oct 25, 2024
1 parent 3815252 commit c76da16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
22 changes: 17 additions & 5 deletions core/lib/multivm/src/tracers/validator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{collections::HashSet, marker::PhantomData, sync::Arc};
use std::{
collections::{BTreeSet, HashSet},
marker::PhantomData,
sync::Arc,
};

use once_cell::sync::OnceCell;
use zksync_system_constants::{
Expand All @@ -8,7 +12,7 @@ use zksync_system_constants::{
use zksync_types::{
vm::VmVersion, web3::keccak256, AccountTreeId, Address, StorageKey, H256, U256,
};
use zksync_utils::{be_bytes_to_safe_address, u256_to_account_address, u256_to_h256};
use zksync_utils::{address_to_u256, be_bytes_to_safe_address, u256_to_h256};

use self::types::{NewTrustedValidationItems, ValidationTracerMode};
use crate::{
Expand All @@ -32,7 +36,7 @@ mod vm_virtual_blocks;
#[derive(Debug, Clone)]
pub struct ValidationTracer<H> {
validation_mode: ValidationTracerMode,
auxilary_allowed_slots: HashSet<H256>,
auxilary_allowed_slots: BTreeSet<H256>,

user_address: Address,
#[allow(dead_code)]
Expand All @@ -51,6 +55,8 @@ pub struct ValidationTracer<H> {
type ValidationRoundResult = Result<NewTrustedValidationItems, ViolatedValidationRule>;

impl<H> ValidationTracer<H> {
const MAX_ALLOWED_SLOT_OFFSET: u32 = 127;

pub fn new(
params: ValidationParams,
vm_version: VmVersion,
Expand Down Expand Up @@ -131,9 +137,15 @@ impl<H> ValidationTracer<H> {
}

// The user is allowed to touch its own slots or slots semantically related to him.
let from = u256_to_h256(key.saturating_sub(Self::MAX_ALLOWED_SLOT_OFFSET.into()));
let to = u256_to_h256(key);
let valid_users_slot = address == self.user_address
|| u256_to_account_address(&key) == self.user_address
|| self.auxilary_allowed_slots.contains(&u256_to_h256(key));
|| key == address_to_u256(&self.user_address)
|| self
.auxilary_allowed_slots
.range(from..=to)
.next()
.is_some();
if valid_users_slot {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions etc/env/base/chain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fee_model_version = "V2"
validation_computational_gas_limit = 300000
save_call_traces = true

bootloader_hash = "0x010008c37ecadea8b003884eb9d81fdfb7161b3b309504e5318f15da19c500d8"
default_aa_hash = "0x0100055da70d970f98ca4677a4b2fcecef5354f345cc5c6d13a78339e5fd87a9"
bootloader_hash = "0x010008c3be57ae5800e077b6c2056d9d75ad1a7b4f0ce583407961cc6fe0b678"
default_aa_hash = "0x0100055dba11508480be023137563caec69debc85f826cb3a4b68246a7cabe30"

protective_reads_persistence_enabled = false

Expand Down
4 changes: 2 additions & 2 deletions etc/env/base/contracts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ RECURSION_NODE_LEVEL_VK_HASH = "0x1186ec268d49f1905f8d9c1e9d39fc33e98c74f91d91a2
RECURSION_LEAF_LEVEL_VK_HASH = "0x101e08b00193e529145ee09823378ef51a3bc8966504064f1f6ba3f1ba863210"
RECURSION_CIRCUITS_SET_VKS_HASH = "0x18c1639094f58177409186e8c48d9f577c9410901d2f1d486b3e7d6cf553ae4c"
GENESIS_TX_HASH = "0xb99ebfea46cbe05a21cd80fe5597d97b204befc52a16303f579c607dc1ac2e2e"
GENESIS_ROOT = "0x28a7e67393021f957572495f8fdadc2c477ae3f4f413ae18c16cff6ee65680e2"
GENESIS_BATCH_COMMITMENT = "0xc57085380434970021d87774b377ce1bb12f5b6064af11595e70011965747def"
GENESIS_ROOT = "0x7275936e5a0063b159d5d22734931fea07871e8d57e564d61ef56e4a6ee23e5c"
GENESIS_BATCH_COMMITMENT = "0xf5f9a5abe62e8a6e0cb2d34d27435c3e5a8fbd7e2e54ca1d108fc58cb86c708a"
PRIORITY_TX_MAX_GAS_LIMIT = 72000000
DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT = 10000000
GENESIS_ROLLUP_LEAF_INDEX = "54"
Expand Down

0 comments on commit c76da16

Please sign in to comment.