-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
134 additions
and
145 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,74 @@ | ||
use alloy_rlp::{RlpEncodable, RlpMaxEncodedLen}; | ||
use hash_db::Hasher; | ||
use plain_hasher::PlainHasher; | ||
use primitive_types::{H160, H256}; | ||
use revm::{ | ||
db::PlainAccount, | ||
primitives::{keccak256, Address, Bytes, Log, B256, U256}, | ||
primitives::{keccak256, Address, Log, B256, U256}, | ||
}; | ||
use rlp::RlpStream; | ||
use triehash::sec_trie_root; | ||
|
||
// TODO(dani): move rlp | ||
|
||
pub fn log_rlp_hash(logs: Vec<Log>) -> B256 { | ||
//https://github.com/ethereum/go-ethereum/blob/356bbe343a30789e77bb38f25983c8f2f2bfbb47/cmd/evm/internal/t8ntool/execution.go#L255 | ||
let mut stream = RlpStream::new(); | ||
stream.begin_unbounded_list(); | ||
for log in logs { | ||
stream.begin_list(3); | ||
stream.append(&&log.address[..]); | ||
stream.begin_unbounded_list(); | ||
for topic in log.topics { | ||
stream.append(&&topic[..]); | ||
} | ||
stream.finalize_unbounded_list(); | ||
stream.append(&&log.data[..]); | ||
} | ||
stream.finalize_unbounded_list(); | ||
let out = stream.out().freeze(); | ||
|
||
pub fn log_rlp_hash(logs: &[Log]) -> B256 { | ||
let mut out = Vec::with_capacity(alloy_rlp::list_length(logs)); | ||
alloy_rlp::encode_list(logs, &mut out); | ||
keccak256(&out) | ||
} | ||
|
||
pub fn state_merkle_trie_root<'a>( | ||
accounts: impl IntoIterator<Item = (Address, &'a PlainAccount)>, | ||
) -> B256 { | ||
let vec = accounts | ||
.into_iter() | ||
.map(|(address, info)| { | ||
let acc_root = trie_account_rlp(info); | ||
(H160::from(address.0 .0), acc_root) | ||
}) | ||
.collect(); | ||
trie_root(accounts.into_iter().map(|(address, acc)| { | ||
( | ||
address, | ||
alloy_rlp::encode_fixed_size(&TrieAccount::new(acc)), | ||
) | ||
})) | ||
} | ||
|
||
trie_root(vec) | ||
#[derive(RlpEncodable, RlpMaxEncodedLen)] | ||
struct TrieAccount { | ||
nonce: u64, | ||
balance: U256, | ||
root_hash: B256, | ||
code_hash: B256, | ||
} | ||
|
||
/// Returns the RLP for this account. | ||
pub fn trie_account_rlp(acc: &PlainAccount) -> Bytes { | ||
let mut stream = RlpStream::new_list(4); | ||
stream.append(&acc.info.nonce); | ||
stream.append(&acc.info.balance); | ||
stream.append(&{ | ||
sec_trie_root::<KeccakHasher, _, _, _>( | ||
acc.storage | ||
.iter() | ||
.filter(|(_k, &v)| v != U256::ZERO) | ||
.map(|(&k, v)| (H256::from(k.to_be_bytes()), rlp::encode(v))), | ||
) | ||
}); | ||
stream.append(&acc.info.code_hash.0.as_ref()); | ||
stream.out().freeze().into() | ||
impl TrieAccount { | ||
#[inline(always)] | ||
fn new(acc: &PlainAccount) -> Self { | ||
Self { | ||
nonce: acc.info.nonce, | ||
balance: acc.info.balance, | ||
root_hash: sec_trie_root::<KeccakHasher, _, _, _>( | ||
acc.storage | ||
.iter() | ||
.filter(|(_k, &v)| v != U256::ZERO) | ||
.map(|(k, v)| (k.to_be_bytes::<32>(), alloy_rlp::encode_fixed_size(v))), | ||
), | ||
code_hash: acc.info.code_hash, | ||
} | ||
} | ||
} | ||
|
||
pub fn trie_root(acc_data: Vec<(H160, Bytes)>) -> B256 { | ||
B256::new(sec_trie_root::<KeccakHasher, _, _, _>(acc_data).0) | ||
#[inline] | ||
pub fn trie_root<I, A, B>(input: I) -> B256 | ||
where | ||
I: IntoIterator<Item = (A, B)>, | ||
A: AsRef<[u8]>, | ||
B: AsRef<[u8]>, | ||
{ | ||
sec_trie_root::<KeccakHasher, _, _, _>(input) | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
pub struct KeccakHasher; | ||
|
||
impl Hasher for KeccakHasher { | ||
type Out = H256; | ||
type Out = B256; | ||
type StdHasher = PlainHasher; | ||
const LENGTH: usize = 32; | ||
|
||
#[inline] | ||
fn hash(x: &[u8]) -> Self::Out { | ||
keccak256(x).0.into() | ||
keccak256(x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.