Skip to content

Commit

Permalink
feat: add op debug witness api (#12583)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Gimenez <fgimenez@users.noreply.github.com>
  • Loading branch information
mattsse and fgimenez authored Nov 15, 2024
1 parent f0b8b9b commit b31b1ea
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion crates/optimism/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ reth-rpc-server-types.workspace = true
reth-tasks = { workspace = true, features = ["rayon"] }
reth-transaction-pool.workspace = true
reth-rpc.workspace = true
reth-rpc-api.workspace = true
reth-node-api.workspace = true
reth-network-api.workspace = true
reth-node-builder.workspace = true
Expand All @@ -31,15 +32,18 @@ reth-chainspec.workspace = true
reth-optimism-chainspec.workspace = true
reth-optimism-consensus.workspace = true
reth-optimism-evm.workspace = true
reth-optimism-payload-builder.workspace = true
reth-optimism-forks.workspace = true

# ethereum
alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types-eth.workspace = true
alloy-rpc-types-debug.workspace = true
alloy-consensus.workspace = true
op-alloy-network.workspace = true
op-alloy-rpc-types.workspace = true
op-alloy-rpc-types-engine.workspace = true
op-alloy-consensus.workspace = true
revm.workspace = true

Expand All @@ -49,6 +53,7 @@ tokio.workspace = true
reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }

# rpc
jsonrpsee-core.workspace = true
jsonrpsee-types.workspace = true
serde_json.workspace = true

Expand All @@ -66,5 +71,6 @@ optimism = [
"reth-primitives/optimism",
"reth-provider/optimism",
"revm/optimism",
"reth-optimism-consensus/optimism"
"reth-optimism-consensus/optimism",
"reth-optimism-payload-builder/optimism"
]
1 change: 1 addition & 0 deletions crates/optimism/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
pub mod error;
pub mod eth;
pub mod sequencer;
pub mod witness;

pub use error::{OpEthApiError, OpInvalidTransactionError, SequencerClientError};
pub use eth::{OpEthApi, OpReceiptBuilder};
Expand Down
81 changes: 81 additions & 0 deletions crates/optimism/rpc/src/witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Support for optimism specific witness RPCs.
use alloy_consensus::Header;
use alloy_primitives::B256;
use alloy_rpc_types_debug::ExecutionWitness;
use jsonrpsee_core::RpcResult;
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use reth_chainspec::ChainSpecProvider;
use reth_evm::ConfigureEvm;
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_payload_builder::OpPayloadBuilder;
use reth_primitives::SealedHeader;
use reth_provider::{BlockReaderIdExt, ProviderError, ProviderResult, StateProviderFactory};
use reth_rpc_api::DebugExecutionWitnessApiServer;
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
use std::{fmt::Debug, sync::Arc};

/// An extension to the `debug_` namespace of the RPC API.
pub struct OpDebugWitnessApi<Provider, EvmConfig> {
inner: Arc<OpDebugWitnessApiInner<Provider, EvmConfig>>,
}

impl<Provider, EvmConfig> OpDebugWitnessApi<Provider, EvmConfig> {
/// Creates a new instance of the `OpDebugWitnessApi`.
pub fn new(provider: Provider, evm_config: EvmConfig) -> Self {
let builder = OpPayloadBuilder::new(evm_config);
let inner = OpDebugWitnessApiInner { provider, builder };
Self { inner: Arc::new(inner) }
}
}

impl<Provider, EvmConfig> OpDebugWitnessApi<Provider, EvmConfig>
where
Provider: BlockReaderIdExt,
{
/// Fetches the parent header by hash.
fn parent_header(&self, parent_block_hash: B256) -> ProviderResult<SealedHeader> {
self.inner
.provider
.sealed_header_by_hash(parent_block_hash)?
.ok_or_else(|| ProviderError::HeaderNotFound(parent_block_hash.into()))
}
}

impl<Provider, EvmConfig> DebugExecutionWitnessApiServer<OpPayloadAttributes>
for OpDebugWitnessApi<Provider, EvmConfig>
where
Provider: BlockReaderIdExt
+ StateProviderFactory
+ ChainSpecProvider<ChainSpec = OpChainSpec>
+ 'static,
EvmConfig: ConfigureEvm<Header = Header> + 'static,
{
fn execute_payload(
&self,
parent_block_hash: B256,
attributes: OpPayloadAttributes,
) -> RpcResult<ExecutionWitness> {
let parent_header = self.parent_header(parent_block_hash).to_rpc_result()?;
self.inner
.builder
.payload_witness(&self.inner.provider, parent_header, attributes)
.map_err(|err| internal_rpc_err(err.to_string()))
}
}

impl<Provider, EvmConfig> Clone for OpDebugWitnessApi<Provider, EvmConfig> {
fn clone(&self) -> Self {
Self { inner: Arc::clone(&self.inner) }
}
}
impl<Provider, EvmConfig> Debug for OpDebugWitnessApi<Provider, EvmConfig> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OpDebugWitnessApi").finish_non_exhaustive()
}
}

struct OpDebugWitnessApiInner<Provider, EvmConfig> {
provider: Provider,
builder: OpPayloadBuilder<EvmConfig>,
}
8 changes: 4 additions & 4 deletions crates/rpc/rpc-api/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ pub trait DebugExecutionWitnessApi<Attributes> {
/// hashed trie nodes to their preimages that were required during the execution of the block,
/// including during state root recomputation.
///
/// The first argument is the block number or block hash. The second argument is the payload
/// attributes for the new block. The third argument is a list of transactions to be included.
#[method(name = "executePayload")]
async fn execute_payload(
/// The first argument is the parent block hash. The second argument is the payload
/// attributes for the new block.
#[method(name = "executePayload", blocking)]
fn execute_payload(
&self,
parent_block_hash: B256,
attributes: Attributes,
Expand Down

0 comments on commit b31b1ea

Please sign in to comment.