Skip to content

Commit

Permalink
WIP: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Dec 10, 2024
1 parent 8ec9c9c commit 823b9a2
Show file tree
Hide file tree
Showing 35 changed files with 246 additions and 339 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

35 changes: 22 additions & 13 deletions crates/edr_evm/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub use self::l1::{EthBlockBuilder, EthBlockReceiptFactory};
use crate::{
blockchain::SyncBlockchain,
config::CfgEnv,
debug::DebugContextForChainSpec,
spec::RuntimeSpec,
state::{DatabaseComponentError, SyncState},
transaction::TransactionError,
DebugContext, MineBlockResultAndState,
MineBlockResultAndStateForChainSpec,
};

/// An error caused during construction of a block builder.
Expand All @@ -32,6 +33,10 @@ pub enum BlockBuilderCreationError<BlockchainErrorT, HardforkT, StateErrorT> {
UnsupportedHardfork(HardforkT),
}

/// Helper type for a chain-specific [`BlockBuilderCreationError`].
pub type BlockBuilderCreationErrorForChainSpec<BlockchainErrorT, ChainSpecT, StateErrorT> =
BlockBuilderCreationError<BlockchainErrorT, <ChainSpecT as ChainSpec>::Hardfork, StateErrorT>;

impl<BlockchainErrorT, HardforkT: Debug, StateErrorT>
From<DatabaseComponentError<BlockchainErrorT, StateErrorT>>
for BlockBuilderCreationError<BlockchainErrorT, HardforkT, StateErrorT>
Expand Down Expand Up @@ -61,6 +66,13 @@ where
Transaction(#[from] TransactionError<ChainSpecT, BlockchainErrorT, StateErrorT>),
}

/// Helper type for a [`BlockBuilderAndError`] with a [`BlockTransactionError`].
pub type BlockBuilderAndTransactionError<BlockBuilderT, BlockchainErrorT, ChainSpecT, StateErrorT> =
BlockBuilderAndError<
BlockBuilderT,
BlockTransactionError<ChainSpecT, BlockchainErrorT, StateErrorT>,
>;

/// A wrapper around a block builder and an error.
pub struct BlockBuilderAndError<BlockBuilderT, ErrorT> {
/// The block builder.
Expand Down Expand Up @@ -92,19 +104,22 @@ where
cfg: CfgEnv,
options: BlockOptions,
debug_context: Option<
DebugContext<
DebugContextForChainSpec<
'blockchain,
ChainSpecT,
Self::BlockchainError,
ChainSpecT,
DebugDataT,
Box<dyn SyncState<Self::StateError>>,
Self::StateError,
>,
>,
) -> Result<
Self,
BlockBuilderCreationError<Self::BlockchainError, ChainSpecT::Hardfork, Self::StateError>,
BlockBuilderCreationErrorForChainSpec<Self::BlockchainError, ChainSpecT, Self::StateError>,
>;

/// Returns the block's receipt factory.
fn block_receipt_factory(&self) -> ChainSpecT::BlockReceiptFactory;

/// Returns the block's [`PartialHeader`].
fn header(&self) -> &PartialHeader;

Expand All @@ -114,18 +129,12 @@ where
transaction: ChainSpecT::SignedTransaction,
) -> Result<
Self,
BlockBuilderAndError<
Self,
BlockTransactionError<ChainSpecT, Self::BlockchainError, Self::StateError>,
>,
BlockBuilderAndTransactionError<Self, Self::BlockchainError, ChainSpecT, Self::StateError>,
>;

/// Finalizes the block, applying rewards to the state.
fn finalize(
self,
rewards: Vec<(Address, U256)>,
) -> Result<
MineBlockResultAndState<ChainSpecT::HaltReason, ChainSpecT::LocalBlock, Self::StateError>,
Self::StateError,
>;
) -> Result<MineBlockResultAndStateForChainSpec<ChainSpecT, Self::StateError>, Self::StateError>;
}
36 changes: 21 additions & 15 deletions crates/edr_evm/src/block/builder/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ use edr_eth::{
};
use revm::Evm;

use super::{BlockBuilder, BlockBuilderAndError, BlockTransactionError};
use super::{
BlockBuilder, BlockBuilderAndError, BlockBuilderAndTransactionError, BlockTransactionError,
};
use crate::{
blockchain::SyncBlockchain,
config::{CfgEnv, Env},
debug::DebugContext,
debug::{DebugContext, DebugContextForChainSpec},
receipt::{ExecutionReceiptBuilder as _, ReceiptFactory},
spec::{BlockEnvConstructor as _, RuntimeSpec, SyncRuntimeSpec},
state::{AccountModifierFn, DatabaseComponents, StateDiff, SyncState, WrapDatabaseRef},
Expand All @@ -39,12 +41,12 @@ where
blockchain: &'blockchain dyn SyncBlockchain<ChainSpecT, BlockchainErrorT, StateErrorT>,
cfg: CfgEnv,
debug_context: Option<
DebugContext<
DebugContextForChainSpec<
'blockchain,
ChainSpecT,
BlockchainErrorT,
ChainSpecT,
DebugDataT,
Box<dyn SyncState<StateErrorT>>,
StateErrorT,
>,
>,
hardfork: ChainSpecT::Hardfork,
Expand Down Expand Up @@ -103,12 +105,12 @@ where
cfg: CfgEnv,
mut options: BlockOptions,
debug_context: Option<
DebugContext<
DebugContextForChainSpec<
'blockchain,
ChainSpecT,
BlockchainErrorT,
ChainSpecT,
DebugDataT,
Box<dyn SyncState<StateErrorT>>,
StateErrorT,
>,
>,
) -> Result<Self, BlockBuilderCreationError<BlockchainErrorT, ChainSpecT::Hardfork, StateErrorT>>
Expand Down Expand Up @@ -163,10 +165,7 @@ where
transaction: ChainSpecT::SignedTransaction,
) -> Result<
Self,
BlockBuilderAndError<
Self,
BlockTransactionError<ChainSpecT, BlockchainErrorT, StateErrorT>,
>,
BlockBuilderAndTransactionError<Self, BlockchainErrorT, ChainSpecT, StateErrorT>,
> {
// The transaction's gas limit cannot be greater than the remaining gas in the
// block
Expand Down Expand Up @@ -412,8 +411,11 @@ impl<'blockchain, BlockchainErrorT, ChainSpecT, DebugDataT, StateErrorT>
BlockBuilder<'blockchain, ChainSpecT, DebugDataT>
for EthBlockBuilder<'blockchain, BlockchainErrorT, ChainSpecT, DebugDataT, StateErrorT>
where
ChainSpecT:
SyncRuntimeSpec<Hardfork: Debug, LocalBlock: From<EthLocalBlockForChainSpec<ChainSpecT>>>,
ChainSpecT: SyncRuntimeSpec<
BlockReceiptFactory: Default,
Hardfork: Debug,
LocalBlock: From<EthLocalBlockForChainSpec<ChainSpecT>>,
>,
StateErrorT: Debug + Send,
{
type BlockchainError = BlockchainErrorT;
Expand Down Expand Up @@ -446,6 +448,10 @@ where
Self::new(blockchain, state, hardfork, cfg, options, debug_context)
}

fn block_receipt_factory(&self) -> ChainSpecT::BlockReceiptFactory {
ChainSpecT::BlockReceiptFactory::default()
}

fn header(&self) -> &PartialHeader {
self.header()
}
Expand All @@ -470,7 +476,7 @@ where
MineBlockResultAndState<ChainSpecT::HaltReason, ChainSpecT::LocalBlock, Self::StateError>,
Self::StateError,
> {
let receipt_factory = ChainSpecT::BlockReceiptFactory::default();
let receipt_factory = self.block_receipt_factory();

let MineBlockResultAndState {
block,
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_evm/src/block/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use edr_eth::{
block::{self, Header, PartialHeader},
keccak256, l1,
log::{ExecutionLog, FilterLog, FullBlockLog, ReceiptLog},
receipt::{ExecutionReceipt, MapReceiptLogs, ReceiptTrait, TransactionReceipt},
receipt::{MapReceiptLogs, ReceiptTrait, TransactionReceipt},
spec::{ChainSpec, HardforkTrait},
transaction::ExecutableTransaction,
trie,
Expand Down
7 changes: 2 additions & 5 deletions crates/edr_evm/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
hardfork::Activations,
spec::{RuntimeSpec, SyncRuntimeSpec},
state::{StateCommit, StateDiff, StateOverride, SyncState},
Block, BlockAndTotalDifficulty,
Block, BlockAndTotalDifficultyForChainSpec,
};

/// Helper type for a chain-specific [`BlockchainError`].
Expand Down Expand Up @@ -205,10 +205,7 @@ pub trait BlockchainMut<ChainSpecT: RuntimeSpec> {
&mut self,
block: ChainSpecT::LocalBlock,
state_diff: StateDiff,
) -> Result<
BlockAndTotalDifficulty<Arc<ChainSpecT::Block>, ChainSpecT::SignedTransaction>,
Self::Error,
>;
) -> Result<BlockAndTotalDifficultyForChainSpec<ChainSpecT>, Self::Error>;

/// Reserves the provided number of blocks, starting from the next block
/// number.
Expand Down
11 changes: 4 additions & 7 deletions crates/edr_evm/src/blockchain/forked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use tokio::runtime;
use super::{
compute_state_at_block,
remote::RemoteBlockchain,
storage::{self, ReservableSparseBlockchainStorage},
storage::{
self, ReservableSparseBlockchainStorage, ReservableSparseBlockchainStorageForChainSpec,
},
validate_next_block, BlockHash, Blockchain, BlockchainError, BlockchainErrorForChainSpec,
BlockchainMut,
};
Expand Down Expand Up @@ -98,12 +100,7 @@ pub struct ForkedBlockchain<ChainSpecT>
where
ChainSpecT: RuntimeSpec,
{
local_storage: ReservableSparseBlockchainStorage<
Arc<ChainSpecT::BlockReceipt>,
Arc<ChainSpecT::LocalBlock>,
ChainSpecT::Hardfork,
ChainSpecT::SignedTransaction,
>,
local_storage: ReservableSparseBlockchainStorageForChainSpec<ChainSpecT>,
// We can force caching here because we only fork from a safe block number.
remote: RemoteBlockchain<Arc<RemoteBlock<ChainSpecT>>, ChainSpecT, true>,
state_root_generator: Arc<Mutex<RandomHashGenerator>>,
Expand Down
20 changes: 7 additions & 13 deletions crates/edr_evm/src/blockchain/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ use edr_eth::{
};

use super::{
compute_state_at_block, storage::ReservableSparseBlockchainStorage, validate_next_block,
BlockHash, Blockchain, BlockchainError, BlockchainErrorForChainSpec, BlockchainMut,
compute_state_at_block,
storage::{ReservableSparseBlockchainStorage, ReservableSparseBlockchainStorageForChainSpec},
validate_next_block, BlockHash, Blockchain, BlockchainError, BlockchainErrorForChainSpec,
BlockchainMut,
};
use crate::{
block::EmptyBlock as _,
spec::SyncRuntimeSpec,
state::{
StateCommit as _, StateDebug, StateDiff, StateError, StateOverride, SyncState, TrieState,
},
Block as _, BlockAndTotalDifficulty, BlockReceipts,
Block as _, BlockAndTotalDifficulty, BlockAndTotalDifficultyForChainSpec, BlockReceipts,
};

/// An error that occurs upon creation of a [`LocalBlockchain`].
Expand Down Expand Up @@ -81,12 +83,7 @@ pub struct LocalBlockchain<ChainSpecT>
where
ChainSpecT: SyncRuntimeSpec,
{
storage: ReservableSparseBlockchainStorage<
Arc<ChainSpecT::BlockReceipt>,
Arc<ChainSpecT::LocalBlock>,
ChainSpecT::Hardfork,
ChainSpecT::SignedTransaction,
>,
storage: ReservableSparseBlockchainStorageForChainSpec<ChainSpecT>,
chain_id: u64,
hardfork: ChainSpecT::Hardfork,
}
Expand Down Expand Up @@ -358,10 +355,7 @@ where
&mut self,
block: ChainSpecT::LocalBlock,
state_diff: StateDiff,
) -> Result<
BlockAndTotalDifficulty<Arc<ChainSpecT::Block>, ChainSpecT::SignedTransaction>,
Self::Error,
> {
) -> Result<BlockAndTotalDifficultyForChainSpec<ChainSpecT>, Self::Error> {
let last_block = self.last_block()?;

validate_next_block::<ChainSpecT>(self.hardfork, &last_block, &block)?;
Expand Down
5 changes: 4 additions & 1 deletion crates/edr_evm/src/blockchain/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ mod sparse;
use edr_eth::B256;

pub use self::{
contiguous::ContiguousBlockchainStorage, reservable::ReservableSparseBlockchainStorage,
contiguous::ContiguousBlockchainStorage,
reservable::{
ReservableSparseBlockchainStorage, ReservableSparseBlockchainStorageForChainSpec,
},
sparse::SparseBlockchainStorage,
};

Expand Down
15 changes: 12 additions & 3 deletions crates/edr_evm/src/blockchain/storage/reservable.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use core::fmt::Debug;
use std::num::NonZeroU64;
use std::{num::NonZeroU64, sync::Arc};

use derive_where::derive_where;
use edr_eth::{
block::PartialHeader,
log::FilterLog,
receipt::{ExecutionReceipt, ReceiptTrait},
spec::HardforkTrait,
spec::{ChainSpec, HardforkTrait},
transaction::ExecutableTransaction,
Address, HashMap, HashSet, B256, U256,
};
use parking_lot::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};

use super::{sparse, InsertError, SparseBlockchainStorage};
use crate::{state::StateDiff, Block, BlockReceipts, EmptyBlock, LocalBlock};
use crate::{spec::RuntimeSpec, state::StateDiff, Block, BlockReceipts, EmptyBlock, LocalBlock};

/// A reservation for a sequence of blocks that have not yet been inserted into
/// storage.
Expand All @@ -29,6 +29,15 @@ struct Reservation<HardforkT: HardforkTrait> {
hardfork: HardforkT,
}

/// Helper type for a chain-specific [`ReservableSparseBlockchainStorage`].
pub type ReservableSparseBlockchainStorageForChainSpec<ChainSpecT> =
ReservableSparseBlockchainStorage<
Arc<<ChainSpecT as RuntimeSpec>::BlockReceipt>,
Arc<<ChainSpecT as RuntimeSpec>::LocalBlock>,
<ChainSpecT as ChainSpec>::Hardfork,
<ChainSpecT as ChainSpec>::SignedTransaction,
>;

/// A storage solution for storing a subset of a Blockchain's blocks in-memory,
/// while lazily loading blocks that have been reserved.
#[derive_where(Debug; BlockReceiptT, BlockT, HardforkT)]
Expand Down
6 changes: 5 additions & 1 deletion crates/edr_evm/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use auto_impl::auto_impl;
use crate::{
blockchain::SyncBlockchain,
spec::RuntimeSpec,
state::{DatabaseComponents, State, WrapDatabaseRef},
state::{DatabaseComponents, State, SyncState, WrapDatabaseRef},
};

/// Type for registering handles, specialised for EDR database component types.
Expand All @@ -24,6 +24,10 @@ pub type HandleRegister<'evm, ChainSpecT, BlockchainErrorT, DebugDataT, StateT>
>,
>;

/// Helper type for a chain-specific [`DebugContext`].
pub type DebugContextForChainSpec<'evm, BlockchainErrorT, ChainSpecT, DebugDataT, StateErrorT> =
DebugContext<'evm, ChainSpecT, BlockchainErrorT, DebugDataT, Box<dyn SyncState<StateErrorT>>>;

/// Type for encapsulating contextual data and handler registration in an
/// `EvmBuilder`.
pub struct DebugContext<'evm, ChainSpecT, BlockchainErrorT, DebugDataT, StateT>
Expand Down
8 changes: 8 additions & 0 deletions crates/edr_evm/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use edr_eth::{
block::{calculate_next_base_fee_per_blob_gas, BlockOptions},
result::{ExecutionResult, InvalidTransaction},
signature::SignatureError,
spec::ChainSpec,
transaction::{ExecutableTransaction as _, Transaction, TransactionValidation},
U256,
};
Expand Down Expand Up @@ -35,6 +36,13 @@ pub struct MineBlockResultAndState<HaltReasonT: HaltReasonTrait, LocalBlockT, St
pub transaction_results: Vec<ExecutionResult<HaltReasonT>>,
}

/// Helper type for a chain-specific [`MineBlockResultAndState`].
pub type MineBlockResultAndStateForChainSpec<ChainSpecT, StateErrorT> = MineBlockResultAndState<
<ChainSpecT as ChainSpec>::HaltReason,
<ChainSpecT as RuntimeSpec>::LocalBlock,
StateErrorT,
>;

/// The type of ordering to use when selecting blocks to mine.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum MineOrdering {
Expand Down
Loading

0 comments on commit 823b9a2

Please sign in to comment.