Skip to content
Merged
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
5 changes: 2 additions & 3 deletions examples/custom-platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use {
ethereum::primitives::SignedTransaction,
optimism::primitives::OpTransactionSigned,
primitives::Recovered,
providers::StateProvider,
revm::db::BundleState,
},
serde::{Deserialize, Serialize},
Expand Down Expand Up @@ -57,12 +56,12 @@ impl Platform for CustomPlatform {

fn build_payload<P>(
payload: Checkpoint<P>,
provider: &dyn StateProvider,
provider_factory: types::ProviderFactory<P>,
) -> Result<types::BuiltPayload<Self>, PayloadBuilderError>
where
P: traits::PlatformExecCtxBounds<Self>,
{
Optimism::build_payload::<P>(payload, provider)
Optimism::build_payload::<P>(payload, provider_factory)
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/payload/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use {
prelude::*,
reth::{
api::ConfigureEvm,
errors::ProviderError,
evm::{block::BlockExecutionError, execute::BlockBuilder},
primitives::SealedHeader,
providers::{StateProvider, StateProviderBox},
providers::{StateProvider, StateProviderBox, StateProviderFactory},
revm::{State, database::StateProviderDatabase},
},
},
Expand All @@ -20,6 +21,9 @@ pub enum Error<P: Platform> {

#[error("Block execution error: {0}")]
BlockExecution(#[from] BlockExecutionError),

#[error("Provider error: {0}")]
Provider(#[from] ProviderError),
}

/// This type represents the beginning of the payload building process. It
Expand Down Expand Up @@ -67,7 +71,7 @@ impl<P: Platform> BlockContext<P> {
pub fn new(
parent: SealedHeader<types::Header<P>>,
attribs: types::PayloadBuilderAttributes<P>,
base_state: StateProviderBox,
provider_factory: types::ProviderFactory<P>,
chainspec: Arc<types::ChainSpec<P>>,
cached: Option<ExecutionCache>,
) -> Result<Self, Error<P>> {
Expand All @@ -83,11 +87,15 @@ impl<P: Platform> BlockContext<P> {
.next_evm_env(&parent, &block_env)
.map_err(Error::EvmEnv)?;

let state_provider = provider_factory
.state_by_block_hash(parent.hash())
.map_err(Error::Provider)?;

let execution_cached = cached.unwrap_or_default();
let provider =
CachedStateProvider::new_with_caches(base_state, execution_cached);
let state_provider =
CachedStateProvider::new_with_caches(state_provider, execution_cached);
let mut base_state = State::builder()
.with_database(StateProviderDatabase(provider))
.with_database(StateProviderDatabase::new(state_provider))
.with_bundle_update()
.build();

Expand All @@ -106,6 +114,7 @@ impl<P: Platform> BlockContext<P> {
base_state,
evm_config,
chainspec,
provider_factory,
}),
})
}
Expand All @@ -132,6 +141,12 @@ impl<P: Platform> BlockContext<P> {
self.inner.base_state.database.as_ref()
}

/// Returns the state provider factory used to create state providers for the
/// block being built.
pub fn provider_factory(&self) -> types::ProviderFactory<P> {
self.inner.provider_factory.clone()
}

/// Returns the EVM configuration used to create EVM instances for executing
/// transactions in the payload under construction.
pub fn evm_config(&self) -> &P::EvmConfig {
Expand Down Expand Up @@ -223,6 +238,10 @@ struct BlockContextInner<P: Platform> {
/// are used to configure the EVM environment and the next block environment
/// for the block that is being built.
chainspec: Arc<types::ChainSpec<P>>,

/// The state provider factory used to create state providers for the block
/// being built.
provider_factory: types::ProviderFactory<P>,
}

impl<P: Platform> core::fmt::Debug for BlockContext<P> {
Expand Down
4 changes: 2 additions & 2 deletions src/payload/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl<P: Platform> Checkpoint<P> {
pub fn build_payload(
&self,
) -> Result<types::BuiltPayload<P>, PayloadBuilderError> {
P::build_payload(self.clone(), self.block().base_state())
P::build_payload(self.clone(), self.block().provider_factory())
}
}

Expand Down Expand Up @@ -751,7 +751,7 @@ mod tests {
#[test]
fn test_build_payload() {
let block = BlockContext::<Ethereum>::mocked();
let provider = block.base_state();
let provider = block.provider_factory();

let root = block.start();
let txs = test_txs::<Ethereum>(0, 0, 10);
Expand Down
25 changes: 18 additions & 7 deletions src/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,24 +362,35 @@ pub mod traits {
{
}

pub trait ProviderBounds<P: Platform>:
/// Bounds for the provider factory required to build payloads.
pub trait ProviderFactoryBounds<P: Platform>:
StateProviderFactory
+ ChainSpecProvider<ChainSpec = types::ChainSpec<P>>
+ HeaderProvider<Header = types::Header<P>>
+ Clone
+ Send
+ Sync
+ 'static
{
}

impl<T, P: Platform> ProviderBounds<P> for T where
impl<T, P: Platform> ProviderFactoryBounds<P> for T where
T: StateProviderFactory
+ ChainSpecProvider<ChainSpec = types::ChainSpec<P>>
+ HeaderProvider<Header = types::Header<P>>
+ Clone
+ Send
+ Sync
{
}

pub trait ProviderBounds<P: Platform>:
ProviderFactoryBounds<P>
+ HeaderProvider<Header = types::Header<P>>
+ Clone
+ 'static
{
}

impl<T, P: Platform> ProviderBounds<P> for T where
T: ProviderFactoryBounds<P>
+ HeaderProvider<Header = types::Header<P>>
+ Clone
+ 'static
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/pipelines/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ where
})?;

let hash = header.hash();
let base_state = self.service.provider().state_by_block_hash(hash)?;
let factory: Arc<Provider> = Arc::new(self.service.provider().clone());

// This is the beginning of the state manipulation API usage from within
// the pipelines API.
let block_ctx = BlockContext::new(
header,
attribs,
base_state,
factory,
self.service.chain_spec().clone(),
self.maybe_pre_cached(hash),
)
Expand Down
Loading
Loading