Skip to content
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: support no_std for reth-primitives #8817

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -50,7 +50,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 @@ -87,7 +87,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 @@ -120,6 +120,7 @@ alloy-compat = [
"reth-primitives-traits/alloy-compat",
"alloy-rpc-types",
]
std = ["thiserror-no-std/std"]
test-utils = ["reth-primitives-traits/test-utils"]

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/benches/integer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ mod elias_fano {
}

/// Primitives error type.
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror_no_std::Error)]
pub enum EliasFanoError {
/// The provided input is invalid.
#[error("{0}")]
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
21 changes: 12 additions & 9 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use reth_codecs::derive_arbitrary;
pub use reth_primitives_traits::test_utils::{generate_valid_header, valid_header_strategy};
use serde::{Deserialize, Serialize};

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

// 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 @@ -177,9 +180,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() * core::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * core::mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(core::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}
}

Expand Down Expand Up @@ -392,9 +395,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() * core::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * core::mem::size_of::<Header>() +
self.withdrawals.as_ref().map_or(core::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}

/// Calculates the total gas used by blob transactions in the sealed block.
Expand Down Expand Up @@ -573,12 +576,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() * core::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() +
self.ommers.capacity() * std::mem::size_of::<Header>() +
self.ommers.capacity() * core::mem::size_of::<Header>() +
self.withdrawals
.as_ref()
.map_or(std::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
.map_or(core::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}
}

Expand Down
20 changes: 15 additions & 5 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@ use crate::{
Hardfork, Head, Header, NamedChain, NodeRecord, SealedHeader, B256, EMPTY_OMMER_ROOT_HASH,
MAINNET_DEPOSIT_CONTRACT, U256,
};
use core::{
fmt,
fmt::{Display, Formatter},
};
use derive_more::From;
use once_cell::sync::Lazy;
use reth_trie_common::root::state_root_ref_unhashed;
use serde::{Deserialize, Serialize};
use std::{

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

pub use alloy_eips::eip1559::BaseFeeParams;

Expand Down Expand Up @@ -1471,7 +1481,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 @@ -1545,13 +1555,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
7 changes: 6 additions & 1 deletion crates/primitives/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use 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 +21,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 +59,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
9 changes: 6 additions & 3 deletions crates/primitives/src/integer_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use bytes::BufMut;
use core::fmt;
use derive_more::Deref;
use roaring::RoaringTreemap;
use serde::{
de::{SeqAccess, Unexpected, Visitor},
ser::SerializeSeq,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt;

#[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.
Expand Down Expand Up @@ -98,7 +101,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 @@ -137,7 +140,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.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use reth_trie_common::root::{ordered_trie_root, ordered_trie_root_with_encoder};

use alloy_eips::eip7685::Encodable7685;

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

/// Calculate a transaction root.
///
/// `(rlp(index), encoded(tx))` pairs.
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use crate::{logs_bloom, Bloom, Bytes, TxType, B256};
use alloy_primitives::Log;
use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable};
use bytes::{Buf, BufMut};
use core::{cmp::Ordering, ops::Deref};
use derive_more::{Deref, DerefMut, From, IntoIterator};
#[cfg(any(test, feature = "arbitrary"))]
use proptest::strategy::Strategy;
#[cfg(feature = "zstd-codec")]
use reth_codecs::CompactZstd;
use reth_codecs::{add_arbitrary_tests, main_codec, Compact};
use std::{cmp::Ordering, ops::Deref};

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

/// Receipt containing result of transaction execution.
#[cfg_attr(feature = "zstd-codec", main_codec(no_arbitrary, zstd))]
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use derive_more::{Deref, DerefMut, From, IntoIterator};
use reth_codecs::{main_codec, Compact};
use revm_primitives::Bytes;

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

/// A list of EIP-7685 requests.
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Deref, DerefMut, From, IntoIterator)]
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/revm/compat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{revm_primitives::AccountInfo, Account, Address, TxKind, KECCAK_EMPTY, U256};
use revm::{interpreter::gas::validate_initial_tx_gas, primitives::SpecId};

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

/// Converts a Revm [`AccountInfo`] into a Reth [`Account`].
///
/// Sets `bytecode_hash` to `None` if `code_hash` is [`KECCAK_EMPTY`].
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/src/revm/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use alloy_eips::{eip4788::BEACON_ROOTS_ADDRESS, eip7002::WITHDRAWAL_REQUEST_PRED
#[cfg(feature = "optimism")]
use revm_primitives::OptimismFields;

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

/// Fill block environment from Block.
pub fn fill_block_env(
block_env: &mut BlockEnv,
Expand Down Expand Up @@ -73,7 +76,7 @@ pub fn block_coinbase(chain_spec: &ChainSpec, header: &Header, after_merge: bool
}

/// Error type for recovering Clique signer from a header.
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror_no_std::Error)]
pub enum CliqueSignerRecoveryError {
/// Header extradata is too short.
#[error("Invalid extra data length")]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::access_list::AccessList;
use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256};
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
use bytes::BytesMut;
use core::mem;
use reth_codecs::{main_codec, Compact};
use std::mem;

/// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)).
#[main_codec]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::access_list::AccessList;
use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256};
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
use bytes::BytesMut;
use core::mem;
use reth_codecs::{main_codec, Compact};
use std::mem;

/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)).
#[main_codec]
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use crate::{
B256, U256,
};
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
use core::mem;
use reth_codecs::{main_codec, Compact, CompactPlaceholder};
use std::mem;

#[cfg(feature = "c-kzg")]
use crate::kzg::KzgSettings;

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

/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction)
///
/// A transaction with blob hashes and max blob fee
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{GotExpectedBoxed, U256};

/// Represents error variants that can happen when trying to validate a
/// [Transaction](crate::Transaction)
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)]
pub enum InvalidTransactionError {
/// The sender does not have enough funds to cover the transaction fees
#[error(
Expand Down Expand Up @@ -55,7 +55,7 @@ pub enum InvalidTransactionError {

/// Represents error variants that can happen when trying to convert a transaction to
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement)
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)]
pub enum TransactionConversionError {
/// This error variant is used when a transaction cannot be converted into a
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement) because it is not supported
Expand All @@ -66,7 +66,7 @@ pub enum TransactionConversionError {

/// Represents error variants than can happen when trying to convert a
/// [`TransactionSignedEcRecovered`](crate::TransactionSignedEcRecovered) transaction.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)]
pub enum TryFromRecoveredTransactionError {
/// Thrown if the transaction type is unsupported.
#[error("Unsupported transaction type: {0}")]
Expand Down
Loading
Loading