Skip to content

Commit

Permalink
chore: adds reth-primitives-traits & Account (#8722)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Jun 11, 2024
1 parent b875973 commit 95719da
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 49 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ members = [
"crates/payload/primitives/",
"crates/payload/validator/",
"crates/primitives/",
"crates/primitives-traits/",
"crates/prune/prune",
"crates/prune/types",
"crates/revm/",
Expand Down Expand Up @@ -302,6 +303,7 @@ reth-payload-builder = { path = "crates/payload/builder" }
reth-payload-primitives = { path = "crates/payload/primitives" }
reth-payload-validator = { path = "crates/payload/validator" }
reth-primitives = { path = "crates/primitives" }
reth-primitives-traits = { path = "crates/primitives-traits" }
reth-provider = { path = "crates/storage/provider" }
reth-prune = { path = "crates/prune/prune" }
reth-prune-types = { path = "crates/prune/types" }
Expand Down
43 changes: 43 additions & 0 deletions crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
]

47 changes: 47 additions & 0 deletions crates/primitives-traits/src/account.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
15 changes: 15 additions & 0 deletions crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true

[dependencies]
# reth
reth-primitives-traits.workspace = true
reth-codecs.workspace = true
reth-ethereum-forks.workspace = true
reth-network-peers.workspace = true
Expand Down Expand Up @@ -98,6 +99,7 @@ secp256k1.workspace = true
default = ["c-kzg", "zstd-codec", "alloy-compat"]
asm-keccak = ["alloy-primitives/asm-keccak"]
arbitrary = [
"reth-primitives-traits/arbitrary",
"revm-primitives/arbitrary",
"reth-ethereum-forks/arbitrary",
"nybbles/arbitrary",
Expand Down
53 changes: 4 additions & 49 deletions crates/primitives/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,12 @@
use crate::{
keccak256,
revm_primitives::{Bytecode as RevmBytecode, Bytes},
GenesisAccount, B256, KECCAK_EMPTY, U256,
};
use crate::revm_primitives::{Bytecode as RevmBytecode, Bytes};
use byteorder::{BigEndian, ReadBytesExt};
use bytes::Buf;
use reth_codecs::{main_codec, Compact};
use reth_codecs::Compact;
use revm_primitives::JumpTable;
use serde::{Deserialize, Serialize};
use std::ops::Deref;

/// 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)
}
}
pub use reth_primitives_traits::Account;

/// Bytecode for an account.
///
Expand Down Expand Up @@ -135,7 +90,7 @@ impl Compact for Bytecode {
#[cfg(test)]
mod tests {
use super::*;
use crate::hex_literal::hex;
use crate::{hex_literal::hex, B256, KECCAK_EMPTY, U256};
use revm_primitives::LegacyAnalyzedBytecode;

#[test]
Expand Down

0 comments on commit 95719da

Please sign in to comment.