-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: alloy migration #535
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 bytes::Bytes; | ||
use alloy_rlp::{RlpEncodable, RlpMaxEncodedLen}; | ||
use hash_db::Hasher; | ||
use plain_hasher::PlainHasher; | ||
use primitive_types::{H160, H256}; | ||
use revm::{ | ||
db::PlainAccount, | ||
primitives::{keccak256, Log, B160, B256, U256}, | ||
primitives::{keccak256, Address, Log, B256, U256}, | ||
}; | ||
use rlp::RlpStream; | ||
use sha3::{Digest, Keccak256}; | ||
use triehash::sec_trie_root; | ||
|
||
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.0.as_ref()); | ||
stream.begin_unbounded_list(); | ||
for topic in log.topics { | ||
stream.append(&topic.0.as_ref()); | ||
} | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, this is nice! |
||
keccak256(&out) | ||
} | ||
|
||
pub fn state_merkle_trie_root<'a>( | ||
accounts: impl IntoIterator<Item = (B160, &'a PlainAccount)>, | ||
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), 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() | ||
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(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 { | ||
let out = Keccak256::digest(x); | ||
H256::from_slice(out.as_slice()) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,28 @@ | ||
use std::str::FromStr; | ||
|
||
use bytes::Bytes; | ||
use revm::primitives::{B160, U256}; | ||
use serde::{ | ||
de::{self, Error}, | ||
Deserialize, | ||
}; | ||
use revm::primitives::Address; | ||
use serde::{de, Deserialize}; | ||
|
||
pub fn deserialize_str_as_u64<'de, D>(deserializer: D) -> Result<u64, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let string = String::deserialize(deserializer)?; | ||
|
||
let output = if let Some(stripped) = string.strip_prefix("0x") { | ||
u64::from_str_radix(stripped, 16).unwrap() | ||
if let Some(stripped) = string.strip_prefix("0x") { | ||
u64::from_str_radix(stripped, 16) | ||
} else { | ||
string.parse().unwrap() | ||
}; | ||
|
||
Ok(output) | ||
} | ||
|
||
pub fn deserialize_str_as_u256<'de, D>(deserializer: D) -> Result<U256, D::Error> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let string = String::deserialize(deserializer)?; | ||
let output = string.parse().unwrap(); | ||
|
||
Ok(output) | ||
} | ||
|
||
pub fn deserialize_vec_as_vec_bytes<'de, D>(deserializer: D) -> Result<Vec<Bytes>, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let strings: Vec<String> = Vec::<String>::deserialize(deserializer)?; | ||
|
||
let mut out = Vec::new(); | ||
for string in strings { | ||
out.push( | ||
hex::decode(string.strip_prefix("0x").unwrap_or(&string)) | ||
.map_err(D::Error::custom)? | ||
.into(), | ||
) | ||
string.parse() | ||
} | ||
Ok(out) | ||
.map_err(serde::de::Error::custom) | ||
} | ||
|
||
pub fn deserialize_maybe_empty<'de, D>(deserializer: D) -> Result<Option<B160>, D::Error> | ||
pub fn deserialize_maybe_empty<'de, D>(deserializer: D) -> Result<Option<Address>, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let string: String = String::deserialize(deserializer)?; | ||
let string = String::deserialize(deserializer)?; | ||
if string.is_empty() { | ||
return Ok(None); | ||
Ok(None) | ||
} else { | ||
string.parse().map_err(de::Error::custom).map(Some) | ||
} | ||
Ok(Some(B160::from_str(&string).map_err(D::Error::custom)?)) | ||
} | ||
|
||
pub fn deserialize_str_as_bytes<'de, D>(deserializer: D) -> Result<Bytes, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
|
||
Ok(hex::decode(s.strip_prefix("0x").unwrap_or(&s)) | ||
.map_err(D::Error::custom)? | ||
.into()) | ||
} | ||
|
||
pub fn deserialize_opt_str_as_bytes<'de, D>(deserializer: D) -> Result<Option<Bytes>, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
#[derive(Debug, Deserialize)] | ||
struct WrappedValue(#[serde(deserialize_with = "deserialize_str_as_bytes")] Bytes); | ||
|
||
Option::<WrappedValue>::deserialize(deserializer) | ||
.map(|opt_wrapped: Option<WrappedValue>| opt_wrapped.map(|wrapped: WrappedValue| wrapped.0)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to reexport it from alloy so the version and all other things are tied to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ruint is already reexported so it can be removed, but rlp needs the "arrayvec" feature flag