-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Migrate trie account type and state root functions from alloy (#65
) * feat: Migrate trie account type and state root functions from alloy * Rename to TrieAccount / Use simple u64 serialize/deserialize * fix cargo hack warnings * Remove itertools and handle sort internally * fix cfg_attr for TrieAccount * Feature gate TrieAccount related code behind `ethereum` * fix import * chore: move into ethereum crate --------- Co-authored-by: Matthias Seitz <[email protected]>
- Loading branch information
Showing
4 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::{EMPTY_ROOT_HASH, KECCAK_EMPTY}; | ||
use alloy_primitives::{keccak256, B256, U256}; | ||
use alloy_rlp::{RlpDecodable, RlpEncodable}; | ||
|
||
/// Represents an TrieAccount in the account trie. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, RlpDecodable, RlpEncodable)] | ||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
pub struct TrieAccount { | ||
/// The account's nonce. | ||
#[cfg_attr(feature = "serde", serde(with = "quantity"))] | ||
pub nonce: u64, | ||
/// The account's balance. | ||
pub balance: U256, | ||
/// The hash of the storage account data. | ||
pub storage_root: B256, | ||
/// The hash of the code of the account. | ||
pub code_hash: B256, | ||
} | ||
|
||
impl Default for TrieAccount { | ||
fn default() -> Self { | ||
Self { | ||
nonce: 0, | ||
balance: U256::ZERO, | ||
storage_root: EMPTY_ROOT_HASH, | ||
code_hash: KECCAK_EMPTY, | ||
} | ||
} | ||
} | ||
|
||
impl TrieAccount { | ||
/// Compute hash as committed to in the MPT trie without memorizing. | ||
pub fn trie_hash_slow(&self) -> B256 { | ||
keccak256(alloy_rlp::encode(self)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
mod quantity { | ||
use alloy_primitives::U64; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// Serializes a primitive number as a "quantity" hex string. | ||
pub(crate) fn serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
U64::from(*value).serialize(serializer) | ||
} | ||
|
||
/// Deserializes a primitive number from a "quantity" hex string. | ||
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
U64::deserialize(deserializer).map(|value| value.to()) | ||
} | ||
} |
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