Skip to content

Commit f2b2006

Browse files
authored
refactor(l1): make improvements to public functions used by ethrex_replay (#5416)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> **Description** <!-- A clear and concise general description of the changes this PR introduces --> - from_nodes doesn't decode the nodes anymore, this is useful because when called multiple times with same set of nodes we were decoding them more than once unnecessarily. - `execution_witness_from_rpc_chain_config` now gets the initial state root from the rpc_witness itself. This should ALWAYS be in the block headers, otherwise it returns an error. Related Replay PR: lambdaclass/ethrex-replay#50
1 parent 3fb2e90 commit f2b2006

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

crates/common/trie/db.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ethereum_types::H256;
2-
use ethrex_rlp::{decode::RLPDecode, encode::RLPEncode, error::RLPDecodeError};
2+
use ethrex_rlp::encode::RLPEncode;
33

4-
use crate::{Nibbles, Node, NodeRLP, Trie, error::TrieError};
4+
use crate::{Nibbles, Node, Trie, error::TrieError};
55
use std::{
66
collections::BTreeMap,
77
sync::{Arc, Mutex},
@@ -62,13 +62,9 @@ impl InMemoryTrieDB {
6262
// Do not remove or make private as we use this in ethrex-replay
6363
pub fn from_nodes(
6464
root_hash: H256,
65-
state_nodes: &BTreeMap<H256, NodeRLP>,
65+
state_nodes: &BTreeMap<H256, Node>,
6666
) -> Result<Self, TrieError> {
67-
let state_nodes: BTreeMap<_, _> = state_nodes
68-
.iter()
69-
.map(|(h, b)| Ok((*h, Node::decode(b)?)))
70-
.collect::<Result<_, RLPDecodeError>>()?;
71-
let mut embedded_root = Trie::get_embedded_root(&state_nodes, root_hash)?;
67+
let mut embedded_root = Trie::get_embedded_root(state_nodes, root_hash)?;
7268
let mut hashed_nodes = vec![];
7369
embedded_root.commit(Nibbles::default(), &mut hashed_nodes);
7470

crates/networking/rpc/debug/execution_witness.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bytes::Bytes;
44
use ethrex_common::{
55
Address, H256, serde_utils,
66
types::{
7-
AccountState, ChainConfig,
7+
AccountState, BlockHeader, ChainConfig,
88
block_execution_witness::{ExecutionWitness, GuestProgramStateError},
99
},
1010
utils::keccak,
@@ -70,8 +70,24 @@ pub fn execution_witness_from_rpc_chain_config(
7070
rpc_witness: RpcExecutionWitness,
7171
chain_config: ChainConfig,
7272
first_block_number: u64,
73-
initial_state_root: H256,
7473
) -> Result<ExecutionWitness, GuestProgramStateError> {
74+
let mut initial_state_root = None;
75+
76+
for h in &rpc_witness.headers {
77+
let header = BlockHeader::decode(h)?;
78+
if header.number == first_block_number - 1 {
79+
initial_state_root = Some(header.state_root);
80+
break;
81+
}
82+
}
83+
84+
let initial_state_root = initial_state_root.ok_or_else(|| {
85+
GuestProgramStateError::Custom(format!(
86+
"header for block {} not found",
87+
first_block_number - 1
88+
))
89+
})?;
90+
7591
let nodes: BTreeMap<H256, Node> = rpc_witness
7692
.state
7793
.into_iter()

0 commit comments

Comments
 (0)