Skip to content

perf(l1,l2): simple snapshot #3162

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions crates/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod smoke_test;
pub mod tracing;
pub mod vm;

use ::tracing::info;
use ::tracing::{error, info};
use constants::MAX_INITCODE_SIZE;
use error::MempoolError;
use error::{ChainError, InvalidBlockError};
Expand Down Expand Up @@ -86,6 +86,7 @@ impl Blockchain {
// Validate the block pre-execution
validate_block(block, &parent_header, &chain_config, ELASTICITY_MULTIPLIER)?;

println!("{}", block.header.parent_hash);
let vm_db = StoreVmDatabase::new(self.storage.clone(), block.header.parent_hash);
let mut vm = Evm::new(self.evm_engine, vm_db);
let execution_result = vm.execute_block(block)?;
Expand Down Expand Up @@ -127,7 +128,7 @@ impl Blockchain {
account_updates: &[AccountUpdate],
) -> Result<(), ChainError> {
// Apply the account updates over the last block's state and compute the new state root
let new_state_root = self
let (new_state_root, snapshot_updates) = self
.storage
.apply_account_updates(block.header.parent_hash, account_updates)
.await?
Expand All @@ -143,7 +144,11 @@ impl Blockchain {
self.storage
.add_receipts(block.hash(), execution_result.receipts)
.await
.map_err(ChainError::StoreError)
.map_err(ChainError::StoreError)?;
self.storage
.update_snapshot(snapshot_updates)
.map_err(ChainError::StoreError)?;
return Ok(());
}

pub async fn add_block(&self, block: &Block) -> Result<(), ChainError> {
Expand All @@ -160,6 +165,7 @@ impl Blockchain {
let interval = stored.duration_since(since).as_millis() as f64;
if interval != 0f64 {
let as_gigas = block.header.gas_used as f64 / 10_f64.powf(9_f64);
info!("Gas used: {}", block.header.gas_used);
let throughput = as_gigas / interval * 1000_f64;
let execution_time = executed.duration_since(since).as_millis() as f64;
let storage_time = stored.duration_since(executed).as_millis() as f64;
Expand Down Expand Up @@ -278,13 +284,12 @@ impl Blockchain {
};

// Apply the account updates over all blocks and compute the new state root
let new_state_root = self
let (new_state_root, snapshot_updates) = self
.storage
.apply_account_updates(first_block_header.parent_hash, &account_updates)
.await
.map_err(|e| (e.into(), None))?
.ok_or((ChainError::ParentStateNotFound, None))?;

// Check state root matches the one in block header
validate_state_root(&last_block.header, new_state_root).map_err(|e| (e, None))?;

Expand All @@ -296,7 +301,9 @@ impl Blockchain {
.add_receipts_for_blocks(all_receipts)
.await
.map_err(|e| (e.into(), None))?;

self.storage
.update_snapshot(snapshot_updates)
.map_err(|e| (e.into(), None))?;
let elapsed_total = interval.elapsed().as_millis();
let mut throughput = 0.0;
if elapsed_total != 0 && total_gas_used != 0 {
Expand Down
3 changes: 2 additions & 1 deletion crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,12 @@ impl Blockchain {
) -> Result<(), ChainError> {
let account_updates = context.vm.get_state_transitions()?;

context.payload.header.state_root = context
let (root, _) = context
.store
.apply_account_updates(context.parent_hash(), &account_updates)
.await?
.unwrap_or_default();
context.payload.header.state_root = root;
context.payload.header.transactions_root =
compute_transactions_root(&context.payload.body.transactions);
context.payload.header.receipts_root = compute_receipts_root(&context.receipts);
Expand Down
6 changes: 4 additions & 2 deletions crates/blockchain/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ impl VmDatabase for StoreVmDatabase {
}

fn get_storage_slot(&self, address: Address, key: H256) -> Result<Option<U256>, EvmError> {
self.store
let value = self
.store
.get_storage_at_hash(self.block_hash, address, key)
.map_err(|e| EvmError::DB(e.to_string()))
.map_err(|e| EvmError::DB(e.to_string()));
return value;
}

fn get_block_hash(&self, block_number: u64) -> Result<H256, EvmError> {
Expand Down
1 change: 1 addition & 0 deletions crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ redb = { workspace = true, optional = true }
# NOTE: intentionally avoiding the workspace dep as it brings "full" features, breaking the provers
# We only need the runtime for the blocking databases to spawn blocking tasks
tokio = { version = "1.41.1", optional = true, default-features = false, features = ["rt"] }
quick_cache = "0.6.14"

[features]
default = []
Expand Down
30 changes: 27 additions & 3 deletions crates/storage/api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use bytes::Bytes;
use ethereum_types::{H256, U256};
use ethrex_common::types::{
payload::PayloadBundle, AccountState, Block, BlockBody, BlockHash, BlockHeader, BlockNumber,
ChainConfig, Index, Receipt, Transaction,
payload::PayloadBundle, AccountInfo, AccountState, AccountUpdate, Block, BlockBody, BlockHash,
BlockHeader, BlockNumber, ChainConfig, Index, Receipt, Transaction,
};
use std::{collections::HashMap, fmt::Debug, panic::RefUnwindSafe};

use crate::{error::StoreError, store::STATE_TRIE_SEGMENTS};
use crate::{
error::StoreError,
rlp::Rlp,
store::{SnapshotUpdate, STATE_TRIE_SEGMENTS},
store_db::libmdbx::{AccountStorageKeyBytes, AccountStorageValueBytes},
};
use ethrex_trie::{Nibbles, Trie};

// We need async_trait because the stabilized feature lacks support for object safety
Expand Down Expand Up @@ -417,4 +422,23 @@ pub trait StoreEngine: Debug + Send + Sync + RefUnwindSafe {
&self,
block_number: BlockNumber,
) -> Result<Option<BlockHash>, StoreError>;

fn write_block_snapshot(
&self,
storages_to_update: Vec<(Rlp<H256>, AccountStorageKeyBytes, AccountStorageValueBytes)>,
storages_to_remove: Vec<Rlp<H256>>,
states_to_remove: Vec<Rlp<H256>>,
states_to_update: Vec<(Rlp<H256>, Rlp<AccountState>)>,
) -> Result<(), StoreError>;

fn state_snapshot_for_account(
&self,
hashed_address: &H256,
) -> Result<Option<AccountState>, StoreError>;

fn storage_snapshot_for_hash(
&self,
account_hash: &H256,
hashed_key: &H256,
) -> Result<Option<U256>, StoreError>;
}
2 changes: 1 addition & 1 deletion crates/storage/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod api;

#[cfg(any(feature = "libmdbx", feature = "redb"))]
mod rlp;
mod store;
pub mod store;
mod store_db;
mod trie_db;
mod utils;
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub type PayloadBundleRLP = Rlp<PayloadBundle>;
// Wrapper for tuples. Used mostly for indexed keys.
pub type TupleRLP<A, B> = Rlp<(A, B)>;

#[derive(Clone, Debug)]
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct Rlp<T>(Vec<u8>, PhantomData<T>);

impl<T: RLPEncode> From<T> for Rlp<T> {
Expand Down
24 changes: 24 additions & 0 deletions crates/storage/snapshot/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! An in memory cache.

use std::sync::Arc;

use ethrex_common::{types::AccountState, H256, U256};
use quick_cache::sync::Cache;

/// In-memory cache.
///
/// Can be cloned freely.
#[derive(Debug, Clone)]
pub struct DiskCache {
pub accounts: Arc<Cache<H256, Option<AccountState>>>,
pub storages: Arc<Cache<(H256, H256), Option<U256>>>,
}

impl DiskCache {
pub fn new(max_capacity_accounts: usize, max_capacity_storage: usize) -> Self {
Self {
accounts: Arc::new(Cache::new(max_capacity_accounts)),
storages: Arc::new(Cache::new(max_capacity_storage)),
}
}
}
2 changes: 2 additions & 0 deletions crates/storage/snapshot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cache;
pub mod disklayer;
Loading