Skip to content

Commit

Permalink
reth-prim no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
JackG-eth committed Jun 13, 2024
1 parent 73adc91 commit 6e52608
Show file tree
Hide file tree
Showing 31 changed files with 192 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

5 changes: 3 additions & 2 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ rayon.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile = { workspace = true, optional = true }
thiserror.workspace = true
thiserror-no-std = { workspace = true , default-features = false }
zstd = { version = "0.13", features = ["experimental"], optional = true }
roaring = "0.10.2"

Expand Down Expand Up @@ -96,7 +96,7 @@ pprof = { workspace = true, features = [
secp256k1.workspace = true

[features]
default = ["c-kzg", "zstd-codec", "alloy-compat"]
default = ["c-kzg", "zstd-codec", "alloy-compat", "std"]
asm-keccak = ["alloy-primitives/asm-keccak"]
arbitrary = [
"reth-primitives-traits/arbitrary",
Expand Down Expand Up @@ -126,6 +126,7 @@ optimism = [
"revm/optimism",
]
alloy-compat = ["alloy-rpc-types"]
std = ["thiserror-no-std/std"]
test-utils = ["dep:plain_hasher", "dep:hash-db"]

[[bench]]
Expand Down
4 changes: 4 additions & 0 deletions crates/primitives/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ use bytes::Buf;
use reth_codecs::Compact;
use revm_primitives::JumpTable;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::ops::Deref;

#[cfg(not(feature = "std"))]
use core::ops::Deref;

pub use reth_primitives_traits::Account;

/// Bytecode for an account.
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/alloy_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::{
use alloy_primitives::TxKind;
use alloy_rlp::Error as RlpError;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

impl TryFrom<alloy_rpc_types::Block> for Block {
type Error = alloy_rpc_types::ConversionError;

Expand Down
27 changes: 17 additions & 10 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ use alloy_rlp::{RlpDecodable, RlpEncodable};
use proptest::prelude::{any, prop_compose};
use reth_codecs::derive_arbitrary;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
#[cfg(feature = "std")]
use std::{mem, ops::Deref};

pub use alloy_eips::eip1898::{
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, RpcBlockHash,
};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
use core::{mem, ops::Deref};

// HACK(onbjerg): we need this to always set `requests` to `None` since we might otherwise generate
// a block with `None` withdrawals and `Some` requests, in which case we end up trying to decode the
// requests as withdrawals
Expand Down Expand Up @@ -175,9 +182,9 @@ impl Block {
pub fn size(&self) -> usize {
self.header.size() +
// take into account capacity
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * std::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * std::mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(std::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}
}

Expand Down Expand Up @@ -397,9 +404,9 @@ impl SealedBlock {
pub fn size(&self) -> usize {
self.header.size() +
// take into account capacity
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * std::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * std::mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(std::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}

/// Calculates the total gas used by blob transactions in the sealed block.
Expand Down Expand Up @@ -604,12 +611,12 @@ impl BlockBody {
#[inline]
pub fn size(&self) -> usize {
self.transactions.iter().map(TransactionSigned::size).sum::<usize>() +
self.transactions.capacity() * std::mem::size_of::<TransactionSigned>() +
self.transactions.capacity() * mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() +
self.ommers.capacity() * std::mem::size_of::<Header>() +
self.ommers.capacity() * mem::size_of::<Header>() +
self.withdrawals
.as_ref()
.map_or(std::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
.map_or(mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}
}

Expand Down
24 changes: 21 additions & 3 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ use crate::{
};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use std::{
collections::BTreeMap,
fmt,
fmt::{Display, Formatter},
sync::Arc,
};

pub use alloy_eips::eip1559::BaseFeeParams;

#[cfg(not(feature = "std"))]
use core::{
fmt,
fmt::{Display, Formatter},
};

#[cfg(not(feature = "std"))]
use alloc::{
collections::BTreeMap,
format,
string::{String, ToString},
sync::Arc,
vec::Vec,
};

#[cfg(feature = "optimism")]
pub(crate) use crate::{
constants::{
Expand Down Expand Up @@ -1476,7 +1494,7 @@ struct DisplayFork {
}

impl Display for DisplayFork {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let name_with_eip = if let Some(eip) = &self.eip {
format!("{} ({})", self.name, eip)
} else {
Expand Down Expand Up @@ -1550,13 +1568,13 @@ pub struct DisplayHardforks {
}

impl Display for DisplayHardforks {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn format(
header: &str,
forks: &[DisplayFork],
next_is_empty: bool,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
) -> fmt::Result {
writeln!(f, "{header}:")?;
let mut iter = forks.iter().peekable();
while let Some(fork) = iter.next() {
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/compression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{cell::RefCell, thread_local};
use zstd::bulk::{Compressor, Decompressor};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Compression/Decompression dictionary for `Receipt`.
pub static RECEIPT_DICTIONARY: &[u8] = include_bytes!("./receipt_dictionary.bin");
/// Compression/Decompression dictionary for `Transaction`.
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/constants/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod trusted_setup {
}

/// Error type for loading the trusted setup.
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror_no_std::Error)]
pub enum LoadKzgSettingsError {
/// Failed to create temp file to store bytes for loading [`KzgSettings`] via
/// [`KzgSettings::load_trusted_setup_file`].
Expand Down
5 changes: 5 additions & 0 deletions crates/primitives/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ use crate::{
revm_primitives::{address, b256},
B256, U256,
};

#[cfg(feature = "std")]
use std::time::Duration;

#[cfg(not(feature = "std"))]
use core::time::Duration;

#[cfg(feature = "optimism")]
use crate::chain::BaseFeeParams;

Expand Down
12 changes: 12 additions & 0 deletions crates/primitives/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#[cfg(feature = "std")]
use std::{
fmt,
ops::{Deref, DerefMut},
};

#[cfg(not(feature = "std"))]
use core::{
fmt,
ops::{Deref, DerefMut},
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

/// A pair of values, one of which is expected and one of which is actual.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GotExpected<T> {
Expand All @@ -18,6 +28,7 @@ impl<T: fmt::Display> fmt::Display for GotExpected<T> {
}
}

#[cfg(feature = "std")]
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpected<T> {}

impl<T> From<(T, T)> for GotExpected<T> {
Expand Down Expand Up @@ -55,6 +66,7 @@ impl<T: fmt::Display> fmt::Display for GotExpectedBoxed<T> {
}
}

#[cfg(feature = "std")]
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpectedBoxed<T> {}

impl<T> Deref for GotExpectedBoxed<T> {
Expand Down
7 changes: 6 additions & 1 deletion crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ use bytes::BufMut;
use proptest::prelude::*;
use reth_codecs::{add_arbitrary_tests, derive_arbitrary, main_codec, Compact};
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use std::{mem, ops::Deref};

#[cfg(not(feature = "std"))]
use core::{mem, ops::Deref};

/// Errors that can occur during header sanity checks.
#[derive(Debug, PartialEq, Eq)]
pub enum HeaderError {
Expand Down Expand Up @@ -518,7 +523,7 @@ impl Decodable for Header {
}

/// Errors that can occur during header sanity checks.
#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
#[derive(thiserror_no_std::Error, Debug, PartialEq, Eq, Clone)]
pub enum HeaderValidationError {
/// Error when the block number does not match the parent block number.
#[error(
Expand Down
11 changes: 9 additions & 2 deletions crates/primitives/src/integer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ use serde::{
ser::SerializeSeq,
Deserialize, Deserializer, Serialize, Serializer,
};
#[cfg(feature = "std")]
use std::{fmt, ops::Deref};

#[cfg(not(feature = "std"))]
use core::{fmt, ops::Deref};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Uses Roaring Bitmaps to hold a list of integers. It provides really good compression with the
/// capability to access its elements without decoding it.
#[derive(Clone, PartialEq, Default)]
Expand Down Expand Up @@ -105,7 +112,7 @@ struct IntegerListVisitor;
impl<'de> Visitor<'de> for IntegerListVisitor {
type Value = IntegerList;

fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a usize array")
}

Expand Down Expand Up @@ -144,7 +151,7 @@ impl<'a> Arbitrary<'a> for IntegerList {
}

/// Primitives error type.
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror_no_std::Error)]
pub enum RoaringBitmapError {
/// The provided input is invalid.
#[error("the provided input is invalid")]
Expand Down
4 changes: 4 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
// 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))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

mod account;
#[cfg(feature = "alloy-compat")]
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub use reth_network_peers::{NodeRecord, NodeRecordParseError, TrustedPeer};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

// Ethereum bootnodes come from <https://github.com/ledgerwatch/erigon/blob/devel/params/bootnodes.go>
// OP bootnodes come from <https://github.com/ethereum-optimism/op-geth/blob/optimism/params/bootnodes.go>

Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use alloy_eips::eip7685::Encodable7685;
use alloy_rlp::Encodable;
use itertools::Itertools;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Adjust the index of an item for rlp encoding.
pub const fn adjust_index_for_rlp(i: usize, len: usize) -> usize {
if i > 0x7f {
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/proofs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use alloy_trie::{
EMPTY_ROOT_HASH,
};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// The merkle proof with the relevant account info.
#[derive(PartialEq, Eq, Debug)]
pub struct AccountProof {
Expand Down
14 changes: 13 additions & 1 deletion crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ use proptest::strategy::Strategy;
#[cfg(feature = "zstd-codec")]
use reth_codecs::CompactZstd;
use reth_codecs::{add_arbitrary_tests, main_codec, Compact};

#[cfg(feature = "std")]
use std::{
cmp::Ordering,
ops::{Deref, DerefMut},
vec,
};

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

#[cfg(not(feature = "std"))]
use core::{
cmp::Ordering,
ops::{Deref, DerefMut},
};

/// Receipt containing result of transaction execution.
Expand Down Expand Up @@ -138,7 +150,7 @@ impl DerefMut for Receipts {

impl IntoIterator for Receipts {
type Item = Vec<Option<Receipt>>;
type IntoIter = std::vec::IntoIter<Self::Item>;
type IntoIter = vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.receipt_vec.into_iter()
Expand Down
7 changes: 6 additions & 1 deletion crates/primitives/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use alloy_eips::eip7685::{Decodable7685, Encodable7685};
use alloy_rlp::{Decodable, Encodable};
use reth_codecs::{main_codec, Compact};
use revm_primitives::Bytes;
#[cfg(feature = "std")]
use std::vec;

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

/// A list of EIP-7685 requests.
#[main_codec]
Expand All @@ -19,7 +24,7 @@ impl From<Vec<Request>> for Requests {

impl IntoIterator for Requests {
type Item = Request;
type IntoIter = std::vec::IntoIter<Request>;
type IntoIter = vec::IntoIter<Request>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand Down
Loading

0 comments on commit 6e52608

Please sign in to comment.