chore: adds reth-primitives-traits & Account (#8722)

This commit is contained in:
joshieDo
2024-06-11 18:01:45 +02:00
committed by GitHub
parent b8759733d4
commit 95719da049
7 changed files with 131 additions and 49 deletions

View File

@ -0,0 +1,43 @@
[package]
name = "reth-primitives-traits"
version.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
description = "Common types in reth."
[lints]
workspace = true
[dependencies]
reth-codecs.workspace = true
alloy-genesis.workspace = true
alloy-primitives.workspace = true
alloy-consensus.workspace = true
# required by reth-codecs
modular-bitfield.workspace = true
bytes.workspace = true
serde.workspace = true
# arbitrary utils
arbitrary = { workspace = true, features = ["derive"], optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }
[dev-dependencies]
arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
proptest-derive.workspace = true
test-fuzz.workspace = true
[features]
arbitrary = [
"dep:arbitrary",
"dep:proptest",
"dep:proptest-derive"
]

View File

@ -0,0 +1,47 @@
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, B256, U256};
use reth_codecs::{main_codec, Compact};
/// An Ethereum account.
#[main_codec]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct Account {
/// Account nonce.
pub nonce: u64,
/// Account balance.
pub balance: U256,
/// Hash of the account's bytecode.
pub bytecode_hash: Option<B256>,
}
impl Account {
/// Whether the account has bytecode.
pub const fn has_bytecode(&self) -> bool {
self.bytecode_hash.is_some()
}
/// After `SpuriousDragon` empty account is defined as account with nonce == 0 && balance == 0
/// && bytecode = None (or hash is [`KECCAK_EMPTY`]).
pub fn is_empty(&self) -> bool {
self.nonce == 0 &&
self.balance.is_zero() &&
self.bytecode_hash.map_or(true, |hash| hash == KECCAK_EMPTY)
}
/// Makes an [Account] from [`GenesisAccount`] type
pub fn from_genesis_account(value: &GenesisAccount) -> Self {
Self {
// nonce must exist, so we default to zero when converting a genesis account
nonce: value.nonce.unwrap_or_default(),
balance: value.balance,
bytecode_hash: value.code.as_ref().map(keccak256),
}
}
/// Returns an account bytecode's hash.
/// In case of no bytecode, returns [`KECCAK_EMPTY`].
pub fn get_bytecode_hash(&self) -> B256 {
self.bytecode_hash.unwrap_or(KECCAK_EMPTY)
}
}

View File

@ -0,0 +1,15 @@
//! Common abstracted types in reth.
#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged
#![allow(unknown_lints, non_local_definitions)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
/// Minimal account
pub mod account;
pub use account::Account;