Skip to content

Commit

Permalink
Merge pull request #14 from mir-protocol/trie_builder_fixes
Browse files Browse the repository at this point in the history
Couple trie builder fixes
  • Loading branch information
dlubarov authored Feb 25, 2023
2 parents b1b4dfc + 2eb7fb6 commit bcb11ce
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions eth_test_parser/src/trie_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rlp::Encodable;
use rlp_derive::{RlpDecodable, RlpEncodable};

use crate::{
deserialize::{Env, PreAccount, TestBody},
deserialize::{Env, TestBody},
fs_scaffolding::get_test_files,
};

Expand Down Expand Up @@ -90,9 +90,8 @@ impl TestBody {

let contract_code: HashMap<_, _> = self
.pre
.into_iter()
.filter(|(_, pre)| pre.code.0.is_empty())
.map(|(_, pre)| (hash(&pre.code.0), pre.code.0.clone()))
.into_values()
.map(|pre| (hash(&pre.code.0), pre.code.0.clone()))
.collect();

let signed_txns: Vec<Vec<_>> = self.post.merge.into_iter().map(|x| x.txbytes.0).collect();
Expand All @@ -108,7 +107,6 @@ impl TestBody {
fn get_storage_tries(&self) -> Vec<(H160, PartialTrie)> {
self.pre
.iter()
.filter(|(_, pre_acc)| !pre_acc.code.0.is_empty())
.map(|(acc_key, pre_acc)| {
let storage_trie = pre_acc
.storage
Expand All @@ -131,8 +129,8 @@ impl TestBody {
.iter()
.map(|(acc_key, pre_acc)| {
let addr_hash = hash(acc_key.as_bytes());
let (code_hash, storage_hash) =
get_pre_account_hashes(acc_key, pre_acc, storage_tries);
let code_hash = hash(&pre_acc.code.0);
let storage_hash = get_storage_hash(acc_key, storage_tries);

let rlp = AccountRlp {
nonce: pre_acc.nonce,
Expand Down Expand Up @@ -169,23 +167,13 @@ impl From<TestBody> for GenerationInputs {
}
}

fn get_pre_account_hashes(
account_address: &H160,
account: &PreAccount,
storage_tries: &[(H160, PartialTrie)],
) -> (H256, H256) {
match account.code.0.is_empty() {
false => (
hash(&account.code.0),
storage_tries
.iter()
.find(|(addr, _)| account_address == addr)
.unwrap()
.1
.calc_hash(),
),
true => (H256::zero(), H256::zero()),
}
fn get_storage_hash(account_address: &H160, storage_tries: &[(H160, PartialTrie)]) -> H256 {
storage_tries
.iter()
.find(|(addr, _)| account_address == addr)
.unwrap()
.1
.calc_hash()
}

fn u256_to_be_bytes(x: U256) -> [u8; 32] {
Expand Down

0 comments on commit bcb11ce

Please sign in to comment.