feat(primitives): improve no_std with manual errors and alloc::String (#9990)

This commit is contained in:
Alexey Shekhirin
2024-08-01 15:58:50 +01:00
committed by GitHub
parent 8e67053de0
commit cd0ec5703a
4 changed files with 39 additions and 26 deletions

View File

@ -45,7 +45,7 @@ once_cell.workspace = true
rayon.workspace = true rayon.workspace = true
serde.workspace = true serde.workspace = true
tempfile = { workspace = true, optional = true } tempfile = { workspace = true, optional = true }
thiserror-no-std = { workspace = true, default-features = false } thiserror-no-std = { workspace = true, default-features = false, optional = true }
zstd = { workspace = true, features = ["experimental"], optional = true } zstd = { workspace = true, features = ["experimental"], optional = true }
# arbitrary utils # arbitrary utils
@ -85,7 +85,7 @@ secp256k1.workspace = true
[features] [features]
default = ["c-kzg", "alloy-compat", "std", "reth-codec"] default = ["c-kzg", "alloy-compat", "std", "reth-codec"]
std = ["thiserror-no-std/std", "reth-primitives-traits/std"] std = ["thiserror-no-std?/std", "reth-primitives-traits/std"]
reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield"] reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield"]
asm-keccak = ["alloy-primitives/asm-keccak"] asm-keccak = ["alloy-primitives/asm-keccak"]
arbitrary = [ arbitrary = [
@ -100,7 +100,7 @@ arbitrary = [
"dep:proptest", "dep:proptest",
"reth-codec", "reth-codec",
] ]
c-kzg = ["dep:c-kzg", "revm-primitives/c-kzg", "dep:tempfile", "alloy-eips/kzg"] c-kzg = ["dep:c-kzg", "revm-primitives/c-kzg", "dep:tempfile", "alloy-eips/kzg", "dep:thiserror-no-std"]
optimism = [ optimism = [
"reth-chainspec/optimism", "reth-chainspec/optimism",
"reth-ethereum-forks/optimism", "reth-ethereum-forks/optimism",

View File

@ -8,7 +8,7 @@ pub use alloy_eips::eip4844::{
TARGET_BLOBS_PER_BLOCK, TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG, TARGET_BLOBS_PER_BLOCK, TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG,
}; };
#[cfg(feature = "c-kzg")] #[cfg(all(feature = "c-kzg", feature = "std"))]
mod trusted_setup { mod trusted_setup {
use crate::kzg::KzgSettings; use crate::kzg::KzgSettings;
use std::io::Write; use std::io::Write;

View File

@ -3,9 +3,13 @@
/// Re-export from `alloy_eips`. /// Re-export from `alloy_eips`.
#[doc(inline)] #[doc(inline)]
pub use alloy_eips::eip2930::{AccessList, AccessListItem}; pub use alloy_eips::eip2930::{AccessList, AccessListItem};
use revm_primitives::U256; use revm_primitives::U256;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::string::String;
/// `AccessListResult` for handling errors from `eth_createAccessList` /// `AccessListResult` for handling errors from `eth_createAccessList`
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AccessListResult { pub struct AccessListResult {

View File

@ -2,79 +2,88 @@ use crate::{GotExpectedBoxed, U256};
/// Represents error variants that can happen when trying to validate a /// Represents error variants that can happen when trying to validate a
/// [Transaction](crate::Transaction) /// [Transaction](crate::Transaction)
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)] #[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
pub enum InvalidTransactionError { pub enum InvalidTransactionError {
/// The sender does not have enough funds to cover the transaction fees /// The sender does not have enough funds to cover the transaction fees
#[error( #[display(
"sender does not have enough funds ({}) to cover transaction fees: {}", _0.got, _0.expected fmt = "sender does not have enough funds ({}) to cover transaction fees: {}", _0.got, _0.expected
)] )]
InsufficientFunds(GotExpectedBoxed<U256>), InsufficientFunds(GotExpectedBoxed<U256>),
/// The nonce is lower than the account's nonce, or there is a nonce gap present. /// The nonce is lower than the account's nonce, or there is a nonce gap present.
/// ///
/// This is a consensus error. /// This is a consensus error.
#[error("transaction nonce is not consistent")] #[display(fmt = "transaction nonce is not consistent")]
NonceNotConsistent, NonceNotConsistent,
/// The transaction is before Spurious Dragon and has a chain ID. /// The transaction is before Spurious Dragon and has a chain ID.
#[error("transactions before Spurious Dragon should not have a chain ID")] #[display(fmt = "transactions before Spurious Dragon should not have a chain ID")]
OldLegacyChainId, OldLegacyChainId,
/// The chain ID in the transaction does not match the current network configuration. /// The chain ID in the transaction does not match the current network configuration.
#[error("transaction's chain ID does not match")] #[display(fmt = "transaction's chain ID does not match")]
ChainIdMismatch, ChainIdMismatch,
/// The transaction requires EIP-2930 which is not enabled currently. /// The transaction requires EIP-2930 which is not enabled currently.
#[error("EIP-2930 transactions are disabled")] #[display(fmt = "EIP-2930 transactions are disabled")]
Eip2930Disabled, Eip2930Disabled,
/// The transaction requires EIP-1559 which is not enabled currently. /// The transaction requires EIP-1559 which is not enabled currently.
#[error("EIP-1559 transactions are disabled")] #[display(fmt = "EIP-1559 transactions are disabled")]
Eip1559Disabled, Eip1559Disabled,
/// The transaction requires EIP-4844 which is not enabled currently. /// The transaction requires EIP-4844 which is not enabled currently.
#[error("EIP-4844 transactions are disabled")] #[display(fmt = "EIP-4844 transactions are disabled")]
Eip4844Disabled, Eip4844Disabled,
/// The transaction requires EIP-7702 which is not enabled currently. /// The transaction requires EIP-7702 which is not enabled currently.
#[error("EIP-7702 transactions are disabled")] #[display(fmt = "EIP-7702 transactions are disabled")]
Eip7702Disabled, Eip7702Disabled,
/// Thrown if a transaction is not supported in the current network configuration. /// Thrown if a transaction is not supported in the current network configuration.
#[error("transaction type not supported")] #[display(fmt = "transaction type not supported")]
TxTypeNotSupported, TxTypeNotSupported,
/// The calculated gas of the transaction exceeds `u64::MAX`. /// The calculated gas of the transaction exceeds `u64::MAX`.
#[error("gas overflow (maximum of u64)")] #[display(fmt = "gas overflow (maximum of u64)")]
GasUintOverflow, GasUintOverflow,
/// The transaction is specified to use less gas than required to start the invocation. /// The transaction is specified to use less gas than required to start the invocation.
#[error("intrinsic gas too low")] #[display(fmt = "intrinsic gas too low")]
GasTooLow, GasTooLow,
/// The transaction gas exceeds the limit /// The transaction gas exceeds the limit
#[error("intrinsic gas too high")] #[display(fmt = "intrinsic gas too high")]
GasTooHigh, GasTooHigh,
/// Thrown to ensure no one is able to specify a transaction with a tip higher than the total /// Thrown to ensure no one is able to specify a transaction with a tip higher than the total
/// fee cap. /// fee cap.
#[error("max priority fee per gas higher than max fee per gas")] #[display(fmt = "max priority fee per gas higher than max fee per gas")]
TipAboveFeeCap, TipAboveFeeCap,
/// Thrown post London if the transaction's fee is less than the base fee of the block. /// Thrown post London if the transaction's fee is less than the base fee of the block.
#[error("max fee per gas less than block base fee")] #[display(fmt = "max fee per gas less than block base fee")]
FeeCapTooLow, FeeCapTooLow,
/// Thrown if the sender of a transaction is a contract. /// Thrown if the sender of a transaction is a contract.
#[error("transaction signer has bytecode set")] #[display(fmt = "transaction signer has bytecode set")]
SignerAccountHasBytecode, SignerAccountHasBytecode,
} }
#[cfg(feature = "std")]
impl std::error::Error for InvalidTransactionError {}
/// Represents error variants that can happen when trying to convert a transaction to /// Represents error variants that can happen when trying to convert a transaction to
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement) /// [`PooledTransactionsElement`](crate::PooledTransactionsElement)
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)] #[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
pub enum TransactionConversionError { pub enum TransactionConversionError {
/// This error variant is used when a transaction cannot be converted into a /// This error variant is used when a transaction cannot be converted into a
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement) because it is not supported /// [`PooledTransactionsElement`](crate::PooledTransactionsElement) because it is not supported
/// for P2P network. /// for P2P network.
#[error("Transaction is not supported for p2p")] #[display(fmt = "Transaction is not supported for p2p")]
UnsupportedForP2P, UnsupportedForP2P,
} }
#[cfg(feature = "std")]
impl std::error::Error for TransactionConversionError {}
/// Represents error variants than can happen when trying to convert a /// Represents error variants than can happen when trying to convert a
/// [`TransactionSignedEcRecovered`](crate::TransactionSignedEcRecovered) transaction. /// [`TransactionSignedEcRecovered`](crate::TransactionSignedEcRecovered) transaction.
#[derive(Debug, Clone, Eq, PartialEq, thiserror_no_std::Error)] #[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
pub enum TryFromRecoveredTransactionError { pub enum TryFromRecoveredTransactionError {
/// Thrown if the transaction type is unsupported. /// Thrown if the transaction type is unsupported.
#[error("Unsupported transaction type: {0}")] #[display(fmt = "Unsupported transaction type: {_0}")]
UnsupportedTransactionType(u8), UnsupportedTransactionType(u8),
/// This error variant is used when a blob sidecar is missing. /// This error variant is used when a blob sidecar is missing.
#[error("Blob sidecar missing for an EIP-4844 transaction")] #[display(fmt = "Blob sidecar missing for an EIP-4844 transaction")]
BlobSidecarMissing, BlobSidecarMissing,
} }
#[cfg(feature = "std")]
impl std::error::Error for TryFromRecoveredTransactionError {}