Skip to content

Commit f56b00d

Browse files
committed
chore: tweaks
1 parent c86f69f commit f56b00d

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

trace_decoder/src/core.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ pub fn entrypoint(
5353
checkpoint_state_trie_root,
5454
} = other;
5555

56-
// TODO(0xaatif): docs for the RPC field say this is gwei already:
57-
// https://docs.rs/alloy/0.3.1/alloy/eips/eip4895/struct.Withdrawal.html#structfield.amount
58-
// in any case, this shouldn't be our problem.
56+
// BUG?(0xaatif): https://github.com/0xPolygonZero/zk_evm/issues/618
5957
for (_, amt) in &mut withdrawals {
6058
*amt = eth_to_gwei(*amt)
6159
}
@@ -128,7 +126,8 @@ fn start(
128126
pre_images: BlockTraceTriePreImages,
129127
) -> anyhow::Result<(StateMpt, BTreeMap<H256, StorageTrie>, Hash2Code)> {
130128
Ok(match pre_images {
131-
// TODO(0xaatif): refactor our convoluted input types
129+
// TODO(0xaatif): https://github.com/0xPolygonZero/zk_evm/issues/401
130+
// refactor our convoluted input types
132131
BlockTraceTriePreImages::Separate(SeparateTriePreImages {
133132
state: SeparateTriePreImage::Direct(state),
134133
storage: SeparateStorageTriesPreImage::MultipleTries(storage),
@@ -205,7 +204,8 @@ fn batch(txns: Vec<TxnInfo>, batch_size_hint: usize) -> Vec<Vec<Option<TxnInfo>>
205204
.into_iter()
206205
.map(FromIterator::from_iter)
207206
.collect(),
208-
// not enough batches at `hint`, but enough real transactions
207+
// not enough batches at `hint`, but enough real transactions,
208+
// so just split them in half
209209
(2.., ..2) => {
210210
let second = txns.split_off(txns.len() / 2);
211211
vec![txns, second]
@@ -464,10 +464,13 @@ fn middle<StateTrieT: StateTrie + Clone>(
464464
..address!("000000000000000000000000000000000000000a");
465465

466466
if !precompiled_addresses.contains(&addr.compat()) {
467-
// TODO(0xaatif): this looks like an optimization,
468-
// but if it's omitted, the tests fail...
467+
// TODO(0xaatif): https://github.com/0xPolygonZero/zk_evm/pull/613
468+
// masking like this SHOULD be a space-saving optimization,
469+
// BUT if it's omitted, we actually get state root mismatches
469470
state_mask.insert(TrieKey::from_address(addr));
470-
}
471+
} // else we don't even need to include them,
472+
// because nodes will only emit a precompiled address if
473+
// the transaction calling them reverted.
471474
}
472475

473476
if do_increment_txn_ix {
@@ -500,9 +503,9 @@ fn middle<StateTrieT: StateTrie + Clone>(
500503
false => vec![],
501504
},
502505
before: {
503-
before.state.mask(state_mask)?;
504-
before.receipt.mask(batch_first_txn_ix..txn_ix)?;
505-
before.transaction.mask(batch_first_txn_ix..txn_ix)?;
506+
before.state.intersect(state_mask)?;
507+
before.receipt.intersect(batch_first_txn_ix..txn_ix)?;
508+
before.transaction.intersect(batch_first_txn_ix..txn_ix)?;
506509

507510
let keep = storage_masks
508511
.keys()
@@ -512,7 +515,7 @@ fn middle<StateTrieT: StateTrie + Clone>(
512515

513516
for (addr, mask) in storage_masks {
514517
if let Some(it) = before.storage.get_mut(&keccak_hash::keccak(addr)) {
515-
it.mask(mask)?
518+
it.intersect(mask)?
516519
} // else self_destructed
517520
}
518521
before

trace_decoder/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
//!
1414
//! **Prover perfomance is a high priority.**
1515
//!
16-
//! The aformentioned trie structures may have subtries _indirected_.
16+
//! The aformentioned trie structures may have subtries _hashed out_.
1717
//! That is, any node (and its children!) may be replaced by its hash,
1818
//! while maintaining provability of its contents:
19+
//!
1920
//! ```text
2021
//! A A
2122
//! / \ / \
@@ -25,9 +26,9 @@
2526
//! ```
2627
//! (where `H` is the hash of the `D/B\E` subtrie).
2728
//!
28-
//! The principle concern of this library is to step through the transactions,
29+
//! The principle concern of this module is to step through the transactions,
2930
//! and reproduce the _intermediate tries_,
30-
//! while indirecting all possible subtries to minimise prover load
31+
//! while hashing out all possible subtries to minimise prover load
3132
//! (since prover performance is sensitive to the size of the trie).
3233
//! The prover can therefore prove each batch of transactions independently.
3334
//!

trace_decoder/src/typed_mpt.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use u4::{AsNibbles, U4};
1111

1212
/// See <https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie>.
1313
///
14-
/// Portions of the trie may be indirected: see [`Self::insert_hash`].
14+
/// Portions of the trie may be _hashed out_: see [`Self::insert_hash`].
1515
#[derive(Debug, Clone, PartialEq, Eq)]
1616
struct TypedMpt<T> {
1717
inner: HashedPartialTrie,
@@ -28,6 +28,8 @@ impl<T> TypedMpt<T> {
2828
}
2929
}
3030
/// Insert a node which represents an out-of-band sub-trie.
31+
///
32+
/// See [module documentation](super) for more.
3133
fn insert_hash(&mut self, key: TrieKey, hash: H256) -> anyhow::Result<()> {
3234
self.inner.insert(key.into_nibbles(), hash)?;
3335
Ok(())
@@ -201,8 +203,8 @@ impl TransactionTrie {
201203
pub const fn as_hashed_partial_trie(&self) -> &mpt_trie::partial_trie::HashedPartialTrie {
202204
&self.untyped
203205
}
204-
/// Indirect (hash) parts of the trie that aren't in `txn_ixs`.
205-
pub fn mask(&mut self, txn_ixs: impl IntoIterator<Item = usize>) -> anyhow::Result<()> {
206+
/// _Hash out_ parts of the trie that aren't in `txn_ixs`.
207+
pub fn intersect(&mut self, txn_ixs: impl IntoIterator<Item = usize>) -> anyhow::Result<()> {
206208
self.untyped = mpt_trie::trie_subsets::create_trie_subset(
207209
&self.untyped,
208210
txn_ixs
@@ -246,8 +248,8 @@ impl ReceiptTrie {
246248
pub const fn as_hashed_partial_trie(&self) -> &mpt_trie::partial_trie::HashedPartialTrie {
247249
&self.untyped
248250
}
249-
/// Indirect (hash) parts of the trie that aren't in `txn_ixs`.
250-
pub fn mask(&mut self, txn_ixs: impl IntoIterator<Item = usize>) -> anyhow::Result<()> {
251+
/// _Hash out_ parts of the trie that aren't in `txn_ixs`.
252+
pub fn intersect(&mut self, txn_ixs: impl IntoIterator<Item = usize>) -> anyhow::Result<()> {
251253
self.untyped = mpt_trie::trie_subsets::create_trie_subset(
252254
&self.untyped,
253255
txn_ixs
@@ -273,7 +275,7 @@ pub trait StateTrie {
273275
fn insert_hash_by_key(&mut self, key: TrieKey, hash: H256) -> anyhow::Result<()>;
274276
fn get_by_address(&self, address: Address) -> Option<AccountRlp>;
275277
fn reporting_remove(&mut self, address: Address) -> anyhow::Result<Option<TrieKey>>;
276-
fn mask(&mut self, address: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()>;
278+
fn intersect(&mut self, address: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()>;
277279
fn iter(&self) -> impl Iterator<Item = (H256, AccountRlp)> + '_;
278280
fn root(&self) -> H256;
279281
}
@@ -322,7 +324,7 @@ impl StateTrie for StateMpt {
322324
#[expect(deprecated)]
323325
self.insert_by_hashed_address(keccak_hash::keccak(address), account)
324326
}
325-
/// Insert an indirected part of the trie
327+
/// Insert an _hashed out_ part of the trie
326328
fn insert_hash_by_key(&mut self, key: TrieKey, hash: H256) -> anyhow::Result<()> {
327329
self.typed.insert_hash(key, hash)
328330
}
@@ -338,7 +340,7 @@ impl StateTrie for StateMpt {
338340
TrieKey::from_address(address),
339341
)
340342
}
341-
fn mask(&mut self, addresses: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
343+
fn intersect(&mut self, addresses: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
342344
let inner = mpt_trie::trie_subsets::create_trie_subset(
343345
self.typed.as_hashed_partial_trie(),
344346
addresses.into_iter().map(TrieKey::into_nibbles),
@@ -370,7 +372,7 @@ impl From<StateMpt> for HashedPartialTrie {
370372

371373
pub struct StateSmt {
372374
address2state: BTreeMap<Address, AccountRlp>,
373-
indirected: BTreeMap<TrieKey, H256>,
375+
hashed_out: BTreeMap<TrieKey, H256>,
374376
}
375377

376378
impl StateTrie for StateSmt {
@@ -382,7 +384,7 @@ impl StateTrie for StateSmt {
382384
Ok(self.address2state.insert(address, account))
383385
}
384386
fn insert_hash_by_key(&mut self, key: TrieKey, hash: H256) -> anyhow::Result<()> {
385-
self.indirected.insert(key, hash);
387+
self.hashed_out.insert(key, hash);
386388
Ok(())
387389
}
388390
fn get_by_address(&self, address: Address) -> Option<AccountRlp> {
@@ -392,7 +394,7 @@ impl StateTrie for StateSmt {
392394
self.address2state.remove(&address);
393395
Ok(None)
394396
}
395-
fn mask(&mut self, address: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
397+
fn intersect(&mut self, address: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
396398
let _ = address;
397399
Ok(())
398400
}
@@ -440,8 +442,8 @@ impl StorageTrie {
440442
pub fn as_mut_hashed_partial_trie_unchecked(&mut self) -> &mut HashedPartialTrie {
441443
&mut self.untyped
442444
}
443-
/// Indirect (hash) the parts of the trie that aren't in `paths`.
444-
pub fn mask(&mut self, paths: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
445+
/// _Hash out_ the parts of the trie that aren't in `paths`.
446+
pub fn intersect(&mut self, paths: impl IntoIterator<Item = TrieKey>) -> anyhow::Result<()> {
445447
self.untyped = mpt_trie::trie_subsets::create_trie_subset(
446448
&self.untyped,
447449
paths.into_iter().map(TrieKey::into_nibbles),

0 commit comments

Comments
 (0)