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

feat: add state override for gas estimates #1358

Merged
merged 49 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
cff1757
feat: add state override for gas estimates
aon Mar 5, 2024
9b68b44
Merge branch 'main' into aon-add-override-estimate-gas-tracer
aon Mar 5, 2024
77f8d21
chore: run zk format
aon Mar 5, 2024
e2ad0f4
fix: change state overrides to use storage_view
aon Mar 6, 2024
aa58304
feat: improve `StateOverride` with custom serializer
aon Mar 7, 2024
0327102
fix: remove pub from hashmap
aon Mar 7, 2024
9f9e328
feat: add storage overrides struct
aon Mar 11, 2024
df87370
fix: read value on overrided state
aon Mar 11, 2024
c0b37c4
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 12, 2024
d1bee52
minor changes
Mar 12, 2024
dbab472
Merge branch 'aon-add-override-estimate-gas-tracer' of github.com:mat…
Mar 12, 2024
671689c
create new state_override file and add tests
Mar 13, 2024
0335fb2
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 14, 2024
a640cee
linter and spellcheck
Mar 14, 2024
f6db295
Merge branch 'aon-add-override-estimate-gas-tracer' of github.com:mat…
Mar 14, 2024
1a39ddd
Cargo lock
Mar 14, 2024
2deec8c
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 14, 2024
f62e51e
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 15, 2024
31384e4
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 15, 2024
dbc177e
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Mar 18, 2024
c32cf4f
fix merge conflicts
Jun 6, 2024
5c56d18
zk fmt
Jun 6, 2024
93aa395
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jun 6, 2024
945946f
Revert submodule update to match main
Jun 6, 2024
da57e99
contracts
Jun 6, 2024
fd8af63
fix CI
Jun 10, 2024
f6ee13d
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jun 10, 2024
7182490
change test function signature
Jun 10, 2024
422e69f
merge with main
Jun 13, 2024
b8b663f
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jun 17, 2024
4ac87e0
Move StorageOverrides inside StorageView
Jun 19, 2024
f4c0189
Apply earlier the storageOverrides
Jun 19, 2024
f023a17
fix spelling
Jun 19, 2024
8ec5256
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jun 24, 2024
20932d4
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jun 27, 2024
4fee27a
storageview implement overrideStorage trait
Jun 28, 2024
299f3f0
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jun 28, 2024
bc4b245
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Deniallugo Jul 1, 2024
9479f03
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jul 1, 2024
157577b
add logic for stateDiff and State with tests, and added state_overrid…
Jul 1, 2024
9649f59
remove toml
Jul 1, 2024
5cb963e
outdated comment
Jul 2, 2024
241991e
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jul 2, 2024
a0627cb
add logic for testing stateDiff and State differences and fix nits
Jul 11, 2024
33a83c8
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jul 11, 2024
cf7b8cb
fix error on merge
Jul 12, 2024
b074154
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jul 16, 2024
3685e54
fix tests
Jul 17, 2024
3d3c469
Merge branch 'main' into aon-add-override-estimate-gas-tracer
Jrigada Jul 22, 2024
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
9 changes: 9 additions & 0 deletions core/lib/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use std::{cell::RefCell, collections::HashMap, fmt, rc::Rc};

use zksync_types::{
api::state_override::StateOverride,
get_known_code_key,
storage::{StorageKey, StorageValue},
H256,
Expand All @@ -24,6 +25,7 @@ mod postgres;
mod rocksdb;
mod shadow_storage;
mod storage_factory;
mod storage_overrides;
mod storage_view;
#[cfg(test)]
mod test_utils;
Expand All @@ -40,6 +42,7 @@ pub use self::{
},
shadow_storage::ShadowStorage,
storage_factory::{BatchDiff, PgOrRocksdbStorage, ReadStorageFactory, RocksdbWithMemory},
storage_overrides::StorageOverrides,
storage_view::{StorageView, StorageViewMetrics},
};

Expand Down Expand Up @@ -87,3 +90,9 @@ pub trait WriteStorage: ReadStorage {

/// Smart pointer to [`WriteStorage`].
pub type StoragePtr<S> = Rc<RefCell<S>>;

/// Functionality to override the storage state.
pub trait OverrideStorage {
/// Apply state override to the storage.
fn apply_state_override(&mut self, overrides: &StateOverride);
}
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
129 changes: 129 additions & 0 deletions core/lib/state/src/storage_overrides.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use std::{cell::RefCell, collections::HashMap, fmt, rc::Rc};

use zksync_types::{
api::state_override::{OverrideState, StateOverride},
get_code_key, get_nonce_key,
utils::{decompose_full_nonce, nonces_to_full_nonce, storage_key_for_eth_balance},
AccountTreeId, StorageKey, StorageValue, H256, U256,
};
use zksync_utils::{bytecode::hash_bytecode, h256_to_u256, u256_to_h256};

use crate::{OverrideStorage, ReadStorage};

/// A storage view that allows to override some of the storage values.
#[derive(Debug)]
pub struct StorageOverrides<S> {
storage_handle: S,
overrided_factory_deps: HashMap<H256, Vec<u8>>,
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
overrided_account_state: HashMap<AccountTreeId, HashMap<H256, H256>>,
overrided_balance: HashMap<StorageKey, U256>,
overrided_nonce: HashMap<StorageKey, U256>,
overrided_code: HashMap<StorageKey, H256>,
}

impl<S: ReadStorage + fmt::Debug> StorageOverrides<S> {
/// Creates a new storage view based on the underlying storage.
pub fn new(storage: S) -> Self {
Self {
storage_handle: storage,
overrided_factory_deps: HashMap::new(),
overrided_account_state: HashMap::new(),
overrided_balance: HashMap::new(),
overrided_nonce: HashMap::new(),
overrided_code: HashMap::new(),
}
}

/// Overrides a factory dependency code.
pub fn store_factory_dep(&mut self, hash: H256, code: Vec<u8>) {
self.overrided_factory_deps.insert(hash, code);
}

/// Overrides an account entire state.
pub fn override_account_state(&mut self, account: AccountTreeId, state: HashMap<H256, H256>) {
self.overrided_account_state.insert(account, state);
}

/// Make a Rc RefCell ptr to the storage
pub fn to_rc_ptr(self) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(self))
}
}

impl<S: ReadStorage + fmt::Debug> ReadStorage for StorageOverrides<S> {
fn read_value(&mut self, key: &StorageKey) -> StorageValue {
if let Some(balance) = self.overrided_balance.get(key) {
return u256_to_h256(*balance);
}
if let Some(code) = self.overrided_code.get(key) {
return *code;
}

if let Some(nonce) = self.overrided_nonce.get(key) {
return u256_to_h256(*nonce);
}
// If the account is overridden, return the overridden value if any or zero.
// Otherwise, return the value from the underlying storage.
self.overrided_account_state.get(key.account()).map_or_else(
|| self.storage_handle.read_value(key),
|state| state.get(key.key()).copied().unwrap_or_else(H256::zero),
)
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
}

fn is_write_initial(&mut self, key: &StorageKey) -> bool {
self.storage_handle.is_write_initial(key)
}

fn load_factory_dep(&mut self, hash: H256) -> Option<Vec<u8>> {
self.overrided_factory_deps
.get(&hash)
.cloned()
.or_else(|| self.storage_handle.load_factory_dep(hash))
}

fn get_enumeration_index(&mut self, key: &StorageKey) -> Option<u64> {
self.storage_handle.get_enumeration_index(key)
}
}

impl<S: ReadStorage + fmt::Debug> OverrideStorage for StorageOverrides<S> {
fn apply_state_override(&mut self, state_override: &StateOverride) {
for (account, overrides) in state_override.iter() {
if let Some(balance) = overrides.balance {
let balance_key = storage_key_for_eth_balance(account);
self.overrided_balance.insert(balance_key, balance);
}

if let Some(nonce) = overrides.nonce {
let nonce_key = get_nonce_key(account);
let full_nonce = self.read_value(&nonce_key);
let (_, deployment_nonce) = decompose_full_nonce(h256_to_u256(full_nonce));
let new_full_nonce = nonces_to_full_nonce(nonce, deployment_nonce);
self.overrided_nonce.insert(nonce_key, new_full_nonce);
}

if let Some(code) = &overrides.code {
let code_key = get_code_key(account);
let code_hash = hash_bytecode(&code.0);
self.overrided_code.insert(code_key, code_hash);
self.store_factory_dep(code_hash, code.0.clone());
}

match &overrides.state {
Some(OverrideState::State(state)) => {
self.override_account_state(AccountTreeId::new(*account), state.clone());
}
Some(OverrideState::StateDiff(state_diff)) => {
for (key, value) in state_diff {
let account_state = self
.overrided_account_state
.entry(AccountTreeId::new(*account))
.or_default();
account_state.insert(*key, *value);
}
}
None => {}
}
}
}
}
17 changes: 15 additions & 2 deletions core/lib/state/src/storage_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::{
time::{Duration, Instant},
};

use zksync_types::{StorageKey, StorageValue, H256};
use zksync_types::{api::state_override::StateOverride, StorageKey, StorageValue, H256};

use crate::{ReadStorage, WriteStorage};
use crate::{OverrideStorage, ReadStorage, WriteStorage};

/// Metrics for [`StorageView`].
#[derive(Debug, Default, Clone, Copy)]
Expand Down Expand Up @@ -52,6 +52,13 @@ pub struct StorageView<S> {
metrics: StorageViewMetrics,
}

impl<S> StorageView<S> {
/// Returns the modified storage keys
pub fn modified_storage_keys(&self) -> &HashMap<StorageKey, StorageValue> {
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
&self.modified_storage_keys
}
}

impl<S> ReadStorage for Box<S>
where
S: ReadStorage + ?Sized,
Expand Down Expand Up @@ -197,6 +204,12 @@ impl<S: ReadStorage + fmt::Debug> WriteStorage for StorageView<S> {
}
}

impl<S: ReadStorage + fmt::Debug + OverrideStorage> OverrideStorage for StorageView<S> {
fn apply_state_override(&mut self, state_override: &StateOverride) {
self.storage_handle.apply_state_override(state_override);
}
}

#[cfg(test)]
mod test {
use zksync_types::{AccountTreeId, Address, H256};
Expand Down
1 change: 1 addition & 0 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
};

pub mod en;
pub mod state_override;

/// Block Number
#[derive(Copy, Clone, Debug, PartialEq, Display)]
Expand Down
70 changes: 70 additions & 0 deletions core/lib/types/src/api/state_override.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::{collections::HashMap, ops::Deref};

use serde::{Deserialize, Deserializer, Serialize};
use zksync_basic_types::{web3::Bytes, H256, U256};

use crate::Address;

/// Collection of overridden accounts, useful for `eth_estimateGas`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateOverride(HashMap<Address, OverrideAccount>);

/// Account override for `eth_estimateGas`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OverrideAccount {
pub balance: Option<U256>,
pub nonce: Option<U256>,
pub code: Option<Bytes>,
#[serde(flatten, deserialize_with = "state_deserializer")]
pub state: Option<OverrideState>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OverrideState {
State(HashMap<H256, H256>),
StateDiff(HashMap<H256, H256>),
}

fn state_deserializer<'de, D>(deserializer: D) -> Result<Option<OverrideState>, D::Error>
where
D: Deserializer<'de>,
{
let val = serde_json::Value::deserialize(deserializer)?;
let state: Option<HashMap<H256, H256>> = match val.get("state") {
Some(val) => serde_json::from_value(val.clone()).map_err(serde::de::Error::custom)?,
None => None,
};
let state_diff: Option<HashMap<H256, H256>> = match val.get("stateDiff") {
Some(val) => serde_json::from_value(val.clone()).map_err(serde::de::Error::custom)?,
None => None,
};

match (state, state_diff) {
(Some(state), None) => Ok(Some(OverrideState::State(state))),
(None, Some(state_diff)) => Ok(Some(OverrideState::StateDiff(state_diff))),
(None, None) => Ok(None),
_ => Err(serde::de::Error::custom(
"Both 'state' and 'stateDiff' cannot be set simultaneously",
)),
}
}

impl StateOverride {
pub fn new(state: HashMap<Address, OverrideAccount>) -> Self {
Self(state)
}

pub fn get(&self, address: &Address) -> Option<&OverrideAccount> {
self.0.get(address)
}
}

impl Deref for StateOverride {
type Target = HashMap<Address, OverrideAccount>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
4 changes: 3 additions & 1 deletion core/lib/types/src/transaction_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ impl TransactionRequest {
}

// returns packed eth signature if it is present
fn get_packed_signature(&self) -> Result<PackedEthSignature, SerializationTransactionError> {
pub fn get_packed_signature(
&self,
) -> Result<PackedEthSignature, SerializationTransactionError> {
let packed_v = self
.v
.ok_or(SerializationTransactionError::IncompleteSignature)?
Expand Down
11 changes: 6 additions & 5 deletions core/lib/vm_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use zksync_multivm::{
vm_latest::HistoryEnabled,
VmInstance,
};
use zksync_state::{PostgresStorage, StoragePtr, StorageView, WriteStorage};
use zksync_state::{PostgresStorage, StorageOverrides, StoragePtr, StorageView, WriteStorage};
use zksync_types::{L1BatchNumber, L2ChainId, Transaction};

use crate::storage::L1BatchParamsProvider;

pub type VmAndStorage<'a> = (
VmInstance<StorageView<PostgresStorage<'a>>, HistoryEnabled>,
StoragePtr<StorageView<PostgresStorage<'a>>>,
VmInstance<StorageView<StorageOverrides<PostgresStorage<'a>>>, HistoryEnabled>,
StoragePtr<StorageView<StorageOverrides<PostgresStorage<'a>>>>,
);

pub fn create_vm(
rt_handle: Handle,
l1_batch_number: L1BatchNumber,
mut connection: Connection<'_, Core>,
l2_chain_id: L2ChainId,
) -> anyhow::Result<VmAndStorage> {
) -> anyhow::Result<VmAndStorage<'_>> {
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
let l1_batch_params_provider = rt_handle
.block_on(L1BatchParamsProvider::new(&mut connection))
.context("failed initializing L1 batch params provider")?;
Expand Down Expand Up @@ -51,7 +51,8 @@ pub fn create_vm(
let storage_l2_block_number = first_l2_block_in_batch.number() - 1;
let pg_storage =
PostgresStorage::new(rt_handle.clone(), connection, storage_l2_block_number, true);
let storage_view = StorageView::new(pg_storage).to_rc_ptr();
let storage_overrides = StorageOverrides::new(pg_storage);
let storage_view = StorageView::new(storage_overrides).to_rc_ptr();
let vm = VmInstance::new(l1_batch_env, system_env, storage_view.clone());

Ok((vm, storage_view))
Expand Down
12 changes: 10 additions & 2 deletions core/lib/web3_decl/src/namespaces/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use zksync_types::{
api::{BlockId, BlockIdVariant, BlockNumber, Transaction, TransactionVariant},
api::{
state_override::StateOverride, BlockId, BlockIdVariant, BlockNumber, Transaction,
TransactionVariant,
},
transaction_request::CallRequest,
Address, H256,
};
Expand Down Expand Up @@ -34,7 +37,12 @@ pub trait EthNamespace {
async fn call(&self, req: CallRequest, block: Option<BlockIdVariant>) -> RpcResult<Bytes>;

#[method(name = "estimateGas")]
async fn estimate_gas(&self, req: CallRequest, _block: Option<BlockNumber>) -> RpcResult<U256>;
async fn estimate_gas(
&self,
req: CallRequest,
_block: Option<BlockNumber>,
state_override: Option<StateOverride>,
) -> RpcResult<U256>;

#[method(name = "gasPrice")]
async fn gas_price(&self) -> RpcResult<U256>;
Expand Down
Loading
Loading