-
Notifications
You must be signed in to change notification settings - Fork 90
feat: add block metering RPC endpoints #216
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
Open
niran
wants to merge
7
commits into
main
Choose a base branch
from
meter-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a245ea
feat: add block metering RPC endpoints
niran 4145029
style: apply rustfmt formatting
niran 26517a5
test: add tests for block metering RPC endpoints
niran 468eb75
feat: measure signer recovery time separately from execution
niran 67b1fcc
docs: document state root timing caveats for block metering
niran 9da0d29
test: fix empty block signer recovery time assertion
niran 90be610
refactor: simplify meter_block API by accepting provider directly
niran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use std::{sync::Arc, time::Instant}; | ||
|
|
||
| use alloy_consensus::{BlockHeader, Header, transaction::SignerRecoverable}; | ||
| use alloy_primitives::B256; | ||
| use eyre::{Result as EyreResult, eyre}; | ||
| use reth::revm::db::State; | ||
| use reth_evm::{ConfigureEvm, execute::BlockBuilder}; | ||
| use reth_optimism_chainspec::OpChainSpec; | ||
| use reth_optimism_evm::{OpEvmConfig, OpNextBlockEnvAttributes}; | ||
| use reth_optimism_primitives::OpBlock; | ||
| use reth_primitives_traits::Block as BlockT; | ||
| use reth_provider::{HeaderProvider, StateProviderFactory}; | ||
|
|
||
| use crate::types::{MeterBlockResponse, MeterBlockTransactions}; | ||
|
|
||
| /// Re-executes a block and meters execution time, state root calculation time, and total time. | ||
| /// | ||
| /// Takes a provider, the chain spec, and the block to meter. | ||
| /// | ||
| /// Returns `MeterBlockResponse` containing: | ||
| /// - Block hash | ||
| /// - Signer recovery time (can be parallelized) | ||
| /// - EVM execution time for all transactions | ||
| /// - State root calculation time | ||
| /// - Total time | ||
| /// - Per-transaction timing information | ||
| /// | ||
| /// # Note | ||
| /// | ||
| /// If the parent block's state has been pruned, this function will return an error. | ||
| /// | ||
| /// State root calculation timing is most accurate for recent blocks where state tries are | ||
| /// cached. For older blocks, trie nodes may not be cached, which can significantly inflate | ||
| /// the `state_root_time_us` value. | ||
| pub fn meter_block<P>( | ||
| provider: P, | ||
| chain_spec: Arc<OpChainSpec>, | ||
| block: &OpBlock, | ||
| ) -> EyreResult<MeterBlockResponse> | ||
| where | ||
| P: StateProviderFactory + HeaderProvider<Header = Header>, | ||
| { | ||
| let block_hash = block.header().hash_slow(); | ||
| let block_number = block.header().number(); | ||
| let transactions: Vec<_> = block.body().transactions().cloned().collect(); | ||
| let tx_count = transactions.len(); | ||
|
|
||
| // Get parent header | ||
| let parent_hash = block.header().parent_hash(); | ||
| let parent_header = provider | ||
| .sealed_header_by_hash(parent_hash)? | ||
| .ok_or_else(|| eyre!("Parent header not found: {}", parent_hash))?; | ||
|
|
||
| // Get state provider at parent block | ||
| let state_provider = provider.state_by_block_hash(parent_hash)?; | ||
|
|
||
| // Create state database from parent state | ||
| let state_db = reth::revm::database::StateProviderDatabase::new(&state_provider); | ||
| let mut db = State::builder().with_database(state_db).with_bundle_update().build(); | ||
|
|
||
| // Set up block attributes from the actual block header | ||
| let attributes = OpNextBlockEnvAttributes { | ||
| timestamp: block.header().timestamp(), | ||
| suggested_fee_recipient: block.header().beneficiary(), | ||
| prev_randao: block.header().mix_hash().unwrap_or(B256::random()), | ||
| gas_limit: block.header().gas_limit(), | ||
| parent_beacon_block_root: block.header().parent_beacon_block_root(), | ||
| extra_data: block.header().extra_data().clone(), | ||
| }; | ||
|
|
||
| // Recover signers first (this can be parallelized in production) | ||
| let signer_recovery_start = Instant::now(); | ||
| let recovered_transactions: Vec<_> = transactions | ||
| .iter() | ||
| .map(|tx| { | ||
| let tx_hash = tx.tx_hash(); | ||
| let signer = tx | ||
| .recover_signer() | ||
| .map_err(|e| eyre!("Failed to recover signer for tx {}: {}", tx_hash, e))?; | ||
| Ok(alloy_consensus::transaction::Recovered::new_unchecked(tx.clone(), signer)) | ||
| }) | ||
| .collect::<EyreResult<Vec<_>>>()?; | ||
| let signer_recovery_time = signer_recovery_start.elapsed().as_micros(); | ||
|
|
||
| // Execute transactions and measure time | ||
| let mut transaction_times = Vec::with_capacity(tx_count); | ||
|
|
||
| let evm_start = Instant::now(); | ||
| { | ||
| let evm_config = OpEvmConfig::optimism(chain_spec); | ||
| let mut builder = evm_config.builder_for_next_block(&mut db, &parent_header, attributes)?; | ||
|
|
||
| builder.apply_pre_execution_changes()?; | ||
|
|
||
| for recovered_tx in recovered_transactions { | ||
| let tx_start = Instant::now(); | ||
| let tx_hash = recovered_tx.tx_hash(); | ||
|
|
||
| let gas_used = builder | ||
| .execute_transaction(recovered_tx) | ||
| .map_err(|e| eyre!("Transaction {} execution failed: {}", tx_hash, e))?; | ||
|
|
||
| let execution_time = tx_start.elapsed().as_micros(); | ||
|
|
||
| transaction_times.push(MeterBlockTransactions { | ||
| tx_hash, | ||
| gas_used, | ||
| execution_time_us: execution_time, | ||
| }); | ||
| } | ||
| } | ||
| let execution_time = evm_start.elapsed().as_micros(); | ||
|
|
||
| // Calculate state root and measure time | ||
| let state_root_start = Instant::now(); | ||
| let bundle_state = db.bundle_state.clone(); | ||
| let hashed_state = state_provider.hashed_post_state(&bundle_state); | ||
| let _state_root = state_provider | ||
| .state_root(hashed_state) | ||
| .map_err(|e| eyre!("Failed to calculate state root: {}", e))?; | ||
| let state_root_time = state_root_start.elapsed().as_micros(); | ||
|
|
||
| let total_time = signer_recovery_time + execution_time + state_root_time; | ||
|
|
||
| Ok(MeterBlockResponse { | ||
| block_hash, | ||
| block_number, | ||
| signer_recovery_time_us: signer_recovery_time, | ||
| execution_time_us: execution_time, | ||
| state_root_time_us: state_root_time, | ||
| total_time_us: total_time, | ||
| transactions: transaction_times, | ||
| }) | ||
| } | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| mod meter; | ||
| mod block; | ||
| mod bundle; | ||
| mod rpc; | ||
| #[cfg(test)] | ||
| mod tests; | ||
| mod types; | ||
|
|
||
| pub use meter::meter_bundle; | ||
| pub use block::meter_block; | ||
| pub use bundle::meter_bundle; | ||
| pub use rpc::{MeteringApiImpl, MeteringApiServer}; | ||
| pub use tips_core::types::{Bundle, MeterBundleResponse, TransactionResult}; | ||
| pub use types::{MeterBlockResponse, MeterBlockTransactions}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.