mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: move GotExpected, log and gas_units to reth-primitives-traits (#8958)
This commit is contained in:
8
crates/primitives-traits/src/constants/gas_units.rs
Normal file
8
crates/primitives-traits/src/constants/gas_units.rs
Normal file
@ -0,0 +1,8 @@
|
||||
/// Represents one Kilogas, or `1_000` gas.
|
||||
pub const KILOGAS: u64 = 1_000;
|
||||
|
||||
/// Represents one Megagas, or `1_000_000` gas.
|
||||
pub const MEGAGAS: u64 = KILOGAS * 1_000;
|
||||
|
||||
/// Represents one Gigagas, or `1_000_000_000` gas.
|
||||
pub const GIGAGAS: u64 = MEGAGAS * 1_000;
|
||||
@ -3,6 +3,9 @@
|
||||
use alloy_primitives::{address, b256, Address, B256, U256};
|
||||
use core::time::Duration;
|
||||
|
||||
/// Gas units, for example [`GIGAGAS`](gas_units::GIGAGAS).
|
||||
pub mod gas_units;
|
||||
|
||||
/// The client version: `reth/v{major}.{minor}.{patch}`
|
||||
pub const RETH_CLIENT_VERSION: &str = concat!("reth/v", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
93
crates/primitives-traits/src/error.rs
Normal file
93
crates/primitives-traits/src/error.rs
Normal file
@ -0,0 +1,93 @@
|
||||
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> {
|
||||
/// The actual value.
|
||||
pub got: T,
|
||||
/// The expected value.
|
||||
pub expected: T,
|
||||
}
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for GotExpected<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "got {}, expected {}", self.got, self.expected)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpected<T> {}
|
||||
|
||||
impl<T> From<(T, T)> for GotExpected<T> {
|
||||
#[inline]
|
||||
fn from((got, expected): (T, T)) -> Self {
|
||||
Self::new(got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GotExpected<T> {
|
||||
/// Creates a new error from a pair of values.
|
||||
#[inline]
|
||||
pub const fn new(got: T, expected: T) -> Self {
|
||||
Self { got, expected }
|
||||
}
|
||||
}
|
||||
|
||||
/// A pair of values, one of which is expected and one of which is actual.
|
||||
///
|
||||
/// Same as [`GotExpected`], but [`Box`]ed for smaller size.
|
||||
///
|
||||
/// Prefer instantiating using [`GotExpected`], and then using `.into()` to convert to this type.
|
||||
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct GotExpectedBoxed<T>(pub Box<GotExpected<T>>);
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for GotExpectedBoxed<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for GotExpectedBoxed<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpectedBoxed<T> {}
|
||||
|
||||
impl<T> Deref for GotExpectedBoxed<T> {
|
||||
type Target = GotExpected<T>;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for GotExpectedBoxed<T> {
|
||||
#[inline(always)]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<(T, T)> for GotExpectedBoxed<T> {
|
||||
#[inline]
|
||||
fn from(value: (T, T)) -> Self {
|
||||
Self(Box::new(GotExpected::from(value)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GotExpected<T>> for GotExpectedBoxed<T> {
|
||||
#[inline]
|
||||
fn from(value: GotExpected<T>) -> Self {
|
||||
Self(Box::new(value))
|
||||
}
|
||||
}
|
||||
@ -30,6 +30,12 @@ pub use request::{Request, Requests};
|
||||
mod withdrawal;
|
||||
pub use withdrawal::{Withdrawal, Withdrawals};
|
||||
|
||||
mod error;
|
||||
pub use error::{GotExpected, GotExpectedBoxed};
|
||||
|
||||
mod log;
|
||||
pub use log::{logs_bloom, Log};
|
||||
|
||||
mod storage;
|
||||
pub use storage::StorageEntry;
|
||||
|
||||
|
||||
76
crates/primitives-traits/src/log.rs
Normal file
76
crates/primitives-traits/src/log.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use alloy_primitives::Bloom;
|
||||
pub use alloy_primitives::Log;
|
||||
|
||||
/// Calculate receipt logs bloom.
|
||||
pub fn logs_bloom<'a>(logs: impl IntoIterator<Item = &'a Log>) -> Bloom {
|
||||
let mut bloom = Bloom::ZERO;
|
||||
for log in logs {
|
||||
bloom.m3_2048(log.address.as_slice());
|
||||
for topic in log.topics() {
|
||||
bloom.m3_2048(topic.as_slice());
|
||||
}
|
||||
}
|
||||
bloom
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use alloy_primitives::{Address, Bytes, Log as AlloyLog, B256};
|
||||
use alloy_rlp::{RlpDecodable, RlpEncodable};
|
||||
use proptest::proptest;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
|
||||
/// This type is kept for compatibility tests after the codec support was added to
|
||||
/// alloy-primitives Log type natively
|
||||
#[main_codec(rlp)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, RlpDecodable, RlpEncodable, Default)]
|
||||
struct Log {
|
||||
/// Contract that emitted this log.
|
||||
address: Address,
|
||||
/// Topics of the log. The number of logs depend on what `LOG` opcode is used.
|
||||
#[cfg_attr(
|
||||
any(test, feature = "arbitrary"),
|
||||
proptest(
|
||||
strategy = "proptest::collection::vec(proptest::arbitrary::any::<B256>(), 0..=5)"
|
||||
)
|
||||
)]
|
||||
topics: Vec<B256>,
|
||||
/// Arbitrary length data.
|
||||
data: Bytes,
|
||||
}
|
||||
|
||||
impl From<AlloyLog> for Log {
|
||||
fn from(mut log: AlloyLog) -> Self {
|
||||
Self {
|
||||
address: log.address,
|
||||
topics: std::mem::take(log.data.topics_mut_unchecked()),
|
||||
data: log.data.data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Log> for AlloyLog {
|
||||
fn from(log: Log) -> Self {
|
||||
Self::new_unchecked(log.address, log.topics, log.data)
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn test_roundtrip_conversion_between_log_and_alloy_log(log: Log) {
|
||||
// Convert log to buffer and then create alloy_log from buffer and compare
|
||||
let mut compacted_log = Vec::<u8>::new();
|
||||
let len = log.clone().to_compact(&mut compacted_log);
|
||||
|
||||
let alloy_log = AlloyLog::from_compact(&compacted_log, len).0;
|
||||
assert_eq!(log, alloy_log.into());
|
||||
|
||||
// Create alloy_log from log and then convert it to buffer and compare compacted_alloy_log and compacted_log
|
||||
let alloy_log = AlloyLog::new_unchecked(log.address, log.topics, log.data);
|
||||
let mut compacted_alloy_log = Vec::<u8>::new();
|
||||
let alloy_len = alloy_log.to_compact(&mut compacted_alloy_log);
|
||||
assert_eq!(len, alloy_len);
|
||||
assert_eq!(compacted_log, compacted_alloy_log);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user