Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Jul 3, 2024
1 parent 73c764d commit 38b2229
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 20 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.

1 change: 1 addition & 0 deletions crates/rpc/rpc-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ reth-tasks = { workspace = true, features = ["rayon"] }
reth-transaction-pool.workspace = true
reth-evm.workspace = true
reth-engine-primitives.workspace = true
reth-trie.workspace = true

# rpc/net
jsonrpsee = { workspace = true, features = ["server"] }
Expand Down
4 changes: 4 additions & 0 deletions crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ use reth_rpc_eth_types::{EthStateCache, EthSubscriptionIdProvider};
use reth_rpc_layer::{AuthLayer, Claims, JwtAuthValidator, JwtSecret};
use reth_tasks::{pool::BlockingTaskGuard, TaskSpawner, TokioTaskExecutor};
use reth_transaction_pool::{noop::NoopTransactionPool, TransactionPool};
use reth_trie::trie_cursor::TrieCursorFactory;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand Down Expand Up @@ -244,6 +245,7 @@ where
+ EvmEnvProvider
+ ChainSpecProvider
+ ChangeSetReader
+ TrieCursorFactory
+ Clone
+ Unpin
+ 'static,
Expand Down Expand Up @@ -435,6 +437,7 @@ where
+ EvmEnvProvider
+ ChainSpecProvider
+ ChangeSetReader
+ TrieCursorFactory
+ Clone
+ Unpin
+ 'static,
Expand Down Expand Up @@ -760,6 +763,7 @@ where
+ EvmEnvProvider
+ ChainSpecProvider
+ ChangeSetReader
+ TrieCursorFactory
+ Clone
+ Unpin
+ 'static,
Expand Down
107 changes: 88 additions & 19 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use alloy_rlp::{Decodable, Encodable};
use async_trait::async_trait;
use jsonrpsee::core::RpcResult;
use reth_chainspec::EthereumHardforks;
use reth_errors::ProviderError;
use reth_errors::{DatabaseError, ProviderError};
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{
Address, Block, BlockId, BlockNumberOrTag, Bytes, TransactionSignedEcRecovered, Withdrawals,
B256, U256,
};
use reth_provider::{
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, HeaderProvider, StateProvider,
StateProviderFactory, StateRootProvider, TransactionVariant,
errors::db::DatabaseErrorInfo, BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider,
HeaderProvider, StateProvider, StateProviderFactory, StateRootProvider, TransactionVariant,
};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::DebugApiServer;
Expand All @@ -26,7 +26,11 @@ use reth_rpc_types::{
BlockError, Bundle, RichBlock, StateContext, TransactionRequest,
};
use reth_tasks::pool::BlockingTaskGuard;
use reth_trie::updates::TrieOp;
use reth_trie::{
trie_cursor::{DatabaseAccountTrieCursor, TrieCursor, TrieCursorFactory},
updates::{TrieKey, TrieOp},
Nibbles,
};
use revm::{
db::{states::bundle_state::BundleRetention, CacheDB},
primitives::{db::DatabaseCommit, BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg},
Expand Down Expand Up @@ -71,6 +75,7 @@ where
+ ChainSpecProvider
+ StateProviderFactory
+ EvmEnvProvider
+ TrieCursorFactory
+ 'static,
Eth: TraceExt + 'static,
{
Expand All @@ -90,7 +95,7 @@ where
) -> EthResult<Vec<TraceResult>> {
if transactions.is_empty() {
// nothing to trace
return Ok(Vec::new())
return Ok(Vec::new());
}

// replay all transactions of the block
Expand Down Expand Up @@ -302,7 +307,7 @@ where
Ok(inspector)
})
.await?;
return Ok(FourByteFrame::from(inspector).into())
return Ok(FourByteFrame::from(inspector).into());
}
GethDebugBuiltInTracerType::CallTracer => {
let call_config = tracer_config
Expand All @@ -324,7 +329,7 @@ where
Ok(frame.into())
})
.await?;
return Ok(frame)
return Ok(frame);
}
GethDebugBuiltInTracerType::PreStateTracer => {
let prestate_config = tracer_config
Expand All @@ -350,7 +355,7 @@ where
Ok(frame)
})
.await?;
return Ok(frame.into())
return Ok(frame.into());
}
GethDebugBuiltInTracerType::NoopTracer => Ok(NoopFrame::default().into()),
GethDebugBuiltInTracerType::MuxTracer => {
Expand All @@ -374,7 +379,7 @@ where
Ok(frame.into())
})
.await?;
return Ok(frame)
return Ok(frame);
}
},
GethDebugTracerType::JsTracer(code) => {
Expand All @@ -399,7 +404,7 @@ where

Ok(GethTrace::JS(res))
}
}
};
}

// default structlog tracer
Expand Down Expand Up @@ -432,7 +437,7 @@ where
opts: Option<GethDebugTracingCallOptions>,
) -> EthResult<Vec<Vec<GethTrace>>> {
if bundles.is_empty() {
return Err(EthApiError::InvalidParams(String::from("bundles are empty.")))
return Err(EthApiError::InvalidParams(String::from("bundles are empty.")));
}

let StateContext { transaction_index, block_number } = state_context.unwrap_or_default();
Expand Down Expand Up @@ -560,6 +565,8 @@ where
.with_bundle_update()
.build();

// TODO: 4788 call

// Re-execute all of the transactions in the block to load all touched accounts into
// the cache DB.
for tx in block.raw_transactions() {
Expand Down Expand Up @@ -647,16 +654,77 @@ where
.filter(|(_, op)| matches!(op, TrieOp::Delete))
.map(|(path, _)| path)
.collect::<Vec<_>>();
for _path in trie_deletions {
for path in trie_deletions {
// Fetch parent `BranchNodeCompact` from the DB. It must be a branch node, as
// extension nodes never point to single leaves.
let (mut cursor, deleted_node_nibble, path): (
Box<dyn TrieCursor>,
u8,
Nibbles,
) = match path {
TrieKey::AccountNode(nibbles) => {
let cursor = this
.inner
.provider
.account_trie_cursor()
.map_err(ProviderError::Database)?;

(Box::new(cursor), nibbles[0], nibbles.slice(1..))
}
TrieKey::StorageNode(hashed_address, nibbles) => {
let cursor = this
.inner
.provider
.storage_trie_cursor(hashed_address)
.map_err(ProviderError::Database)?;

(Box::new(cursor), nibbles[0], nibbles.slice(1..))
}
TrieKey::StorageTrie(_) => {
// Ignore storage trie root updates; These are not required for this portion of the
// witness.
continue;
}
};

// Iterate through all of the hashes in the parent branch node. If there is only
// a single non-empty hash left, take the path to that
// sibling node.
// Fetch the parent branch node from the database.
let (_, branch) = cursor
.seek_exact(path.clone())
.map_err(ProviderError::Database)?
.ok_or(ProviderError::Database(DatabaseError::Other(
"Failed to seek to branch node".to_string(),
)))?;

// Check if there are only two children within the parent branch. If there are, one is the deleted
// leaf, and the other is the sibling that we need to unblind. Otherwise, we can continue, as
// the branch node will remain after the deletion.
let sibling_nibble = if branch.state_mask.count_ones() != 2 {
continue;
} else {
// Find the first set bit.
let first_bit_index = branch.state_mask.trailing_zeros() as u8;

// Create a new mask, clearing the first set bit.
let mask = branch.state_mask.get() & (branch.state_mask.get() - 1);

// Find the second set bit.
let second_bit_index = mask.trailing_zeros() as u8;

if first_bit_index == deleted_node_nibble {
second_bit_index
} else {
first_bit_index
}
};
let _sibling_path = Nibbles::from_nibbles_unchecked(
std::iter::once(sibling_nibble)
.chain(path.iter().copied())
.collect::<Vec<_>>(),
);

// Rebuild the sub-trie rooted at the sibling node using `TrieWalker` +
// `HashBuilder`.
// TODO

// Add the preimage of the sibling node to the witness.
}
Expand Down Expand Up @@ -708,7 +776,7 @@ where
.into_geth_builder()
.geth_call_traces(call_config, res.result.gas_used());

return Ok((frame.into(), res.state))
return Ok((frame.into(), res.state));
}
GethDebugBuiltInTracerType::PreStateTracer => {
let prestate_config = tracer_config
Expand All @@ -726,7 +794,7 @@ where
db,
)?;

return Ok((frame.into(), res.state))
return Ok((frame.into(), res.state));
}
GethDebugBuiltInTracerType::NoopTracer => {
Ok((NoopFrame::default().into(), Default::default()))
Expand All @@ -740,7 +808,7 @@ where

let (res, _) = self.eth_api().inspect(&mut *db, env, &mut inspector)?;
let frame = inspector.try_into_mux_frame(&res, db)?;
return Ok((frame.into(), res.state))
return Ok((frame.into(), res.state));
}
},
GethDebugTracerType::JsTracer(code) => {
Expand All @@ -756,7 +824,7 @@ where
let result = inspector.json_result(res, &env, db)?;
Ok((GethTrace::JS(result), state))
}
}
};
}

// default structlog tracer
Expand All @@ -781,6 +849,7 @@ where
+ ChainSpecProvider
+ StateProviderFactory
+ EvmEnvProvider
+ TrieCursorFactory
+ 'static,
Eth: EthApiSpec + EthTransactions + TraceExt + 'static,
{
Expand Down
18 changes: 17 additions & 1 deletion crates/storage/provider/src/test_utils/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use reth_primitives::{
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{updates::TrieUpdates, AccountProof};
use reth_trie::{trie_cursor::{noop::{NoopAccountTrieCursor, NoopStorageTrieCursor}, TrieCursorFactory}, updates::TrieUpdates, AccountProof};
use revm::{
db::BundleState,
primitives::{BlockEnv, CfgEnvWithHandlerCfg},
Expand Down Expand Up @@ -308,6 +308,22 @@ impl ChangeSetReader for NoopProvider {
}
}

impl TrieCursorFactory for NoopProvider {
type AccountTrieCursor = NoopAccountTrieCursor;
type StorageTrieCursor = NoopStorageTrieCursor;

fn account_trie_cursor(&self) -> Result<Self::AccountTrieCursor, reth_db::DatabaseError> {
unimplemented!()
}

fn storage_trie_cursor(
&self,
_hashed_address: B256,
) -> Result<Self::StorageTrieCursor, reth_db::DatabaseError> {
unimplemented!()
}
}

impl StateRootProvider for NoopProvider {
fn state_root(&self, _state: &BundleState) -> ProviderResult<B256> {
Ok(B256::default())
Expand Down

0 comments on commit 38b2229

Please sign in to comment.