Skip to content

Commit

Permalink
Merge branch 'develop' into feat/continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Aug 22, 2024
2 parents a0dcf0e + c7a1641 commit 3951e53
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 370 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.

6 changes: 3 additions & 3 deletions mpt_trie/src/debug_tools/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ pub struct DebugQueryParamsBuilder {

impl DebugQueryParamsBuilder {
/// Defaults to `true`.
pub const fn print_key_pieces(mut self, enabled: bool) -> Self {
pub const fn include_key_pieces(mut self, enabled: bool) -> Self {
self.params.include_key_piece_per_node = enabled;
self
}

/// Defaults to `true`.
pub const fn print_node_type(mut self, enabled: bool) -> Self {
pub const fn include_node_type(mut self, enabled: bool) -> Self {
self.params.include_node_type = enabled;
self
}

/// Defaults to `false`.
pub const fn print_node_specific_values(mut self, enabled: bool) -> Self {
pub const fn include_node_specific_values(mut self, enabled: bool) -> Self {
self.params.include_node_specific_values = enabled;
self
}
Expand Down
34 changes: 21 additions & 13 deletions mpt_trie/src/trie_subsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use log::trace;
use thiserror::Error;

use crate::{
debug_tools::query::{get_path_from_query, DebugQueryOutput, DebugQueryParamsBuilder},
nibbles::Nibbles,
partial_trie::{Node, PartialTrie, WrappedNode},
trie_hashing::EncodedNode,
Expand All @@ -21,13 +22,10 @@ use crate::{
/// The output type of trie_subset operations.
pub type SubsetTrieResult<T> = Result<T, SubsetTrieError>;

/// Errors that may occur when creating a subset [`PartialTrie`].
/// We encountered a `hash` node when marking nodes during sub-trie creation.
#[derive(Clone, Debug, Error, Hash)]
pub enum SubsetTrieError {
#[error("Tried to mark nodes in a tracked trie for a key that does not exist! (Key: {0}, trie: {1})")]
/// The key does not exist in the trie.
UnexpectedKey(Nibbles, String),
}
#[error("Encountered a hash node when marking nodes to not hash when traversing a key to not hash!\nPath: {0}")]
pub struct SubsetTrieError(DebugQueryOutput);

#[derive(Debug)]
enum TrackedNodeIntern<N: PartialTrie> {
Expand Down Expand Up @@ -256,8 +254,17 @@ where
N: PartialTrie,
K: Into<Nibbles>,
{
for k in keys_involved {
mark_nodes_that_are_needed(tracked_trie, &mut k.into())?;
for mut k in keys_involved.map(|k| k.into()) {
mark_nodes_that_are_needed(tracked_trie, &mut k).map_err(|_| {
// We need to unwind back to this callsite in order to produce the actual error.
let query = DebugQueryParamsBuilder::default()
.include_node_specific_values(true)
.build(k);

let res = get_path_from_query(&tracked_trie.info.underlying_node, query);

SubsetTrieError(res)
})?;
}

Ok(create_partial_trie_subset_from_tracked_trie(tracked_trie))
Expand All @@ -270,10 +277,14 @@ where
/// - For the key `0x1`, the marked nodes would be [B(0x), B(0x1)].
/// - For the key `0x12`, the marked nodes still would be [B(0x), B(0x1)].
/// - For the key `0x123`, the marked nodes would be [B(0x), B(0x1), L(0x123)].
///
/// Also note that we can't construct the error until we back out of this
/// recursive function. We need to know the full key that hit the hash
/// node, and that's only available at the initial call site.
fn mark_nodes_that_are_needed<N: PartialTrie>(
trie: &mut TrackedNode<N>,
curr_nibbles: &mut Nibbles,
) -> SubsetTrieResult<()> {
) -> Result<(), ()> {
trace!(
"Sub-trie marking at {:x}, (type: {})",
curr_nibbles,
Expand All @@ -286,10 +297,7 @@ fn mark_nodes_that_are_needed<N: PartialTrie>(
}
TrackedNodeIntern::Hash => match curr_nibbles.is_empty() {
false => {
return Err(SubsetTrieError::UnexpectedKey(
*curr_nibbles,
format!("{:?}", trie),
));
return Err(());
}
true => {
trie.info.touched = true;
Expand Down
51 changes: 17 additions & 34 deletions trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use mpt_trie::{
use crate::{
hash,
processed_block_trace::{
NodesUsedByTxn, ProcessedBlockTrace, ProcessedTxnInfo, StateTrieWrites, TxnMetaState,
NodesUsedByTxn, ProcessedBlockTrace, ProcessedTxnInfo, StateWrite, TxnMetaState,
},
typed_mpt::{ReceiptTrie, StateTrie, StorageTrie, TransactionTrie, TrieKey},
OtherBlockData, PartialTriePreImages,
Expand Down Expand Up @@ -202,15 +202,12 @@ fn update_txn_and_receipt_tries(
meta: &TxnMetaState,
txn_idx: usize,
) -> anyhow::Result<()> {
if meta.is_dummy() {
// This is a dummy payload, that does not mutate these tries.
return Ok(());
}

trie_state.txn.insert(txn_idx, meta.txn_bytes())?;
trie_state
.receipt
.insert(txn_idx, meta.receipt_node_bytes.clone())?;
if let Some(bytes) = &meta.txn_bytes {
trie_state.txn.insert(txn_idx, bytes.clone())?;
trie_state
.receipt
.insert(txn_idx, meta.receipt_node_bytes.clone())?;
} // else it's just a dummy
Ok(())
}

Expand All @@ -220,11 +217,11 @@ fn update_txn_and_receipt_tries(
fn init_any_needed_empty_storage_tries<'a>(
storage_tries: &mut HashMap<H256, StorageTrie>,
accounts_with_storage: impl Iterator<Item = &'a H256>,
state_accounts_with_no_accesses_but_storage_tries: &'a HashMap<H256, H256>,
accts_with_unaccessed_storage: &HashMap<H256, H256>,
) {
for h_addr in accounts_with_storage {
if !storage_tries.contains_key(h_addr) {
let trie = state_accounts_with_no_accesses_but_storage_tries
let trie = accts_with_unaccessed_storage
.get(h_addr)
.map(|s_root| {
let mut it = StorageTrie::default();
Expand Down Expand Up @@ -537,9 +534,7 @@ fn process_txn_info(
init_any_needed_empty_storage_tries(
&mut curr_block_tries.storage,
txn_info.nodes_used_by_txn.storage_accesses.keys(),
&txn_info
.nodes_used_by_txn
.state_accounts_with_no_accesses_but_storage_tries,
&txn_info.nodes_used_by_txn.accts_with_unaccessed_storage,
);

// For each non-dummy txn, we increment `txn_number_after` and
Expand Down Expand Up @@ -594,8 +589,7 @@ fn process_txn_info(
signed_txns: txn_info
.meta
.iter()
.filter(|t| t.txn_bytes.is_some())
.map(|tx| tx.txn_bytes())
.filter_map(|t| t.txn_bytes.clone())
.collect::<Vec<_>>(),
withdrawals: Vec::default(), /* Only ever set in a dummy txn at the end of
* the block (see `[add_withdrawals_to_txns]`
Expand All @@ -607,7 +601,11 @@ fn process_txn_info(
receipts_root: curr_block_tries.receipt.root(),
},
checkpoint_state_trie_root: extra_data.checkpoint_state_trie_root,
contract_code: txn_info.contract_code_accessed,
contract_code: txn_info
.contract_code_accessed
.into_iter()
.map(|code| (hash(&code), code))
.collect(),
block_metadata: other_data.b_data.b_meta.clone(),
block_hashes: other_data.b_data.b_hashes.clone(),
global_exit_roots: vec![],
Expand All @@ -621,7 +619,7 @@ fn process_txn_info(
Ok(gen_inputs)
}

impl StateTrieWrites {
impl StateWrite {
fn apply_writes_to_state_node(
&self,
state_node: &mut AccountRlp,
Expand Down Expand Up @@ -708,21 +706,6 @@ fn create_trie_subset_wrapped(
.context(format!("missing keys when creating {}", trie_type))
}

impl TxnMetaState {
/// Outputs a boolean indicating whether this `TxnMetaState`
/// represents a dummy payload or an actual transaction.
const fn is_dummy(&self) -> bool {
self.txn_bytes.is_none()
}

fn txn_bytes(&self) -> Vec<u8> {
match self.txn_bytes.as_ref() {
Some(v) => v.clone(),
None => Vec::default(),
}
}
}

fn eth_to_gwei(eth: U256) -> U256 {
// 1 ether = 10^9 gwei.
eth * U256::from(10).pow(9.into())
Expand Down
37 changes: 14 additions & 23 deletions trace_decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,6 @@ pub enum ContractCodeUsage {
Write(#[serde(with = "crate::hex")] Vec<u8>),
}

impl ContractCodeUsage {
fn get_code_hash(&self) -> H256 {
match self {
ContractCodeUsage::Read(hash) => *hash,
ContractCodeUsage::Write(bytes) => hash(bytes),
}
}
}

/// Other data that is needed for proof gen.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OtherBlockData {
Expand Down Expand Up @@ -398,15 +389,17 @@ pub fn entrypoint(
.map(|(addr, data)| (addr.into_hash_left_padded(), data))
.collect::<Vec<_>>();

let code_db = {
let mut code_db = code_db.unwrap_or_default();
if let Some(code_mappings) = pre_images.extra_code_hash_mappings {
code_db.extend(code_mappings);
}
code_db
};

let mut code_hash_resolver = Hash2Code::new(code_db);
// Note we discard any user-provided hashes.
let mut hash2code = code_db
.unwrap_or_default()
.into_values()
.chain(
pre_images
.extra_code_hash_mappings
.unwrap_or_default()
.into_values(),
)
.collect::<Hash2Code>();

let last_tx_idx = txn_info.len().saturating_sub(1) / batch_size;

Expand All @@ -432,7 +425,7 @@ pub fn entrypoint(
&pre_images.tries,
&all_accounts_in_pre_images,
&extra_state_accesses,
&mut code_hash_resolver,
&mut hash2code,
)
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down Expand Up @@ -460,8 +453,6 @@ struct PartialTriePreImages {

/// Like `#[serde(with = "hex")`, but tolerates and emits leading `0x` prefixes
mod hex {
use std::{borrow::Cow, fmt};

use serde::{de::Error as _, Deserialize as _, Deserializer, Serializer};

pub fn serialize<S: Serializer, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -475,9 +466,9 @@ mod hex {
pub fn deserialize<'de, D: Deserializer<'de>, T>(deserializer: D) -> Result<T, D::Error>
where
T: hex::FromHex,
T::Error: fmt::Display,
T::Error: std::fmt::Display,
{
let s = Cow::<str>::deserialize(deserializer)?;
let s = String::deserialize(deserializer)?;
match s.strip_prefix("0x") {
Some(rest) => T::from_hex(rest),
None => T::from_hex(&*s),
Expand Down
Loading

0 comments on commit 3951e53

Please sign in to comment.