From 72fdb7f9eceacb430709176f5628082749f20979 Mon Sep 17 00:00:00 2001 From: Udoagwa Franklin <54338168+frankudoags@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:59:50 +0100 Subject: [PATCH] feat: Make db-models no-std (#14459) Co-authored-by: Matthias Seitz Co-authored-by: Arsenii Kulikov Co-authored-by: alpharush <0xalpharush@protonmail.com> Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Co-authored-by: Alexey Shekhirin <5773434+shekhirin@users.noreply.github.com> Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com> Co-authored-by: Federico Gimenez Co-authored-by: Roman Krasiuk Co-authored-by: Ryan Schneider Co-authored-by: Bilog WEB3 <155262265+Bilogweb3@users.noreply.github.com> Co-authored-by: Vitalyr <158586577+Vitaliyr888@users.noreply.github.com> Co-authored-by: Yohann Kazoula Co-authored-by: Shourya Chaudhry <149008800+18aaddy@users.noreply.github.com> Co-authored-by: Poulav Bhowmick Co-authored-by: urb Co-authored-by: Emilia Hane --- .github/assets/check_rv32imac.sh | 1 + crates/storage/db-api/Cargo.toml | 2 +- crates/storage/db-models/Cargo.toml | 32 ++++++++++++++----- crates/storage/db-models/src/accounts.rs | 9 +++--- crates/storage/db-models/src/blocks.rs | 20 ++++++------ .../storage/db-models/src/client_version.rs | 7 ++-- crates/storage/db-models/src/lib.rs | 13 +++++++- 7 files changed, 58 insertions(+), 26 deletions(-) diff --git a/.github/assets/check_rv32imac.sh b/.github/assets/check_rv32imac.sh index 4be9b1d7e..18a260a3a 100755 --- a/.github/assets/check_rv32imac.sh +++ b/.github/assets/check_rv32imac.sh @@ -15,6 +15,7 @@ crates_to_check=( reth-storage-errors reth-execution-errors reth-execution-types + reth-db-models ## ethereum reth-ethereum-forks diff --git a/crates/storage/db-api/Cargo.toml b/crates/storage/db-api/Cargo.toml index c298e0e4c..ce6fb7fd2 100644 --- a/crates/storage/db-api/Cargo.toml +++ b/crates/storage/db-api/Cargo.toml @@ -14,7 +14,7 @@ workspace = true [dependencies] # reth reth-codecs.workspace = true -reth-db-models.workspace = true +reth-db-models = { workspace = true, features = ["reth-codec"] } reth-primitives = { workspace = true, features = ["reth-codec"] } reth-primitives-traits = { workspace = true, features = ["serde", "reth-codec"] } reth-stages-types = { workspace = true, features = ["serde", "reth-codec"] } diff --git a/crates/storage/db-models/Cargo.toml b/crates/storage/db-models/Cargo.toml index 0997c08b7..da134c5c1 100644 --- a/crates/storage/db-models/Cargo.toml +++ b/crates/storage/db-models/Cargo.toml @@ -13,28 +13,30 @@ workspace = true [dependencies] # reth -reth-codecs.workspace = true -reth-primitives-traits = { workspace = true, features = ["serde", "reth-codec"] } +reth-codecs = { workspace = true, optional = true } +reth-primitives-traits = { workspace = true, features = ["serde"] } # ethereum alloy-primitives.workspace = true alloy-eips.workspace = true # codecs -modular-bitfield.workspace = true +modular-bitfield = { workspace = true, optional = true } serde = { workspace = true, default-features = false } # misc -bytes.workspace = true +bytes = { workspace = true, optional = true } # arbitrary utils arbitrary = { workspace = true, features = ["derive"], optional = true } -proptest = { workspace = true, optional = true } [dev-dependencies] # reth reth-primitives-traits = { workspace = true, features = ["arbitrary"] } reth-codecs.workspace = true + +bytes.workspace = true +modular-bitfield.workspace = true arbitrary = { workspace = true, features = ["derive"] } proptest.workspace = true @@ -42,16 +44,30 @@ proptest-arbitrary-interop.workspace = true test-fuzz.workspace = true [features] +default = ["std"] +std = [ + "alloy-eips/std", + "alloy-primitives/std", + "reth-primitives-traits/std", + "bytes?/std", + "serde/std", +] test-utils = [ "reth-primitives-traits/test-utils", "arbitrary", - "reth-codecs/test-utils", + "reth-codecs?/test-utils", ] arbitrary = [ + "std", "reth-primitives-traits/arbitrary", "dep:arbitrary", - "dep:proptest", "alloy-primitives/arbitrary", "alloy-eips/arbitrary", - "reth-codecs/arbitrary", + "reth-codecs?/arbitrary", +] +reth-codec = [ + "dep:reth-codecs", + "dep:modular-bitfield", + "dep:bytes", + "reth-primitives-traits/reth-codec", ] diff --git a/crates/storage/db-models/src/accounts.rs b/crates/storage/db-models/src/accounts.rs index 29a5cf305..eeefccaec 100644 --- a/crates/storage/db-models/src/accounts.rs +++ b/crates/storage/db-models/src/accounts.rs @@ -1,7 +1,6 @@ -use reth_codecs::{add_arbitrary_tests, Compact}; use serde::Serialize; -use alloy_primitives::{bytes::Buf, Address}; +use alloy_primitives::Address; use reth_primitives_traits::Account; /// Account as it is saved in the database. @@ -9,7 +8,7 @@ use reth_primitives_traits::Account; /// [`Address`] is the subkey. #[derive(Debug, Default, Clone, Eq, PartialEq, Serialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary, serde::Deserialize))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] pub struct AccountBeforeTx { /// Address for the account. Acts as `DupSort::SubKey`. pub address: Address, @@ -20,7 +19,8 @@ pub struct AccountBeforeTx { // NOTE: Removing reth_codec and manually encode subkey // and compress second part of the value. If we have compression // over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey -impl Compact for AccountBeforeTx { +#[cfg(feature = "reth-codec")] +impl reth_codecs::Compact for AccountBeforeTx { fn to_compact(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]>, @@ -36,6 +36,7 @@ impl Compact for AccountBeforeTx { } fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) { + use bytes::Buf; let address = Address::from_slice(&buf[..20]); buf.advance(20); diff --git a/crates/storage/db-models/src/blocks.rs b/crates/storage/db-models/src/blocks.rs index c87316d0e..3557e24bf 100644 --- a/crates/storage/db-models/src/blocks.rs +++ b/crates/storage/db-models/src/blocks.rs @@ -1,9 +1,7 @@ use alloy_eips::eip4895::Withdrawals; use alloy_primitives::TxNumber; -use bytes::Buf; -use reth_codecs::{add_arbitrary_tests, Compact}; +use core::ops::Range; use serde::{Deserialize, Serialize}; -use std::ops::Range; /// Total number of transactions. pub type NumTransactions = u64; @@ -12,9 +10,10 @@ pub type NumTransactions = u64; /// /// It has the pointer to the transaction Number of the first /// transaction in the block and the total number of transactions. -#[derive(Debug, Default, Eq, PartialEq, Clone, Copy, Serialize, Deserialize, Compact)] +#[derive(Debug, Default, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] pub struct StoredBlockBodyIndices { /// The number of the first transaction in this block /// @@ -68,9 +67,10 @@ impl StoredBlockBodyIndices { } /// The storage representation of block withdrawals. -#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize, Compact)] +#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] pub struct StoredBlockWithdrawals { /// The block withdrawals. pub withdrawals: Withdrawals, @@ -80,13 +80,14 @@ pub struct StoredBlockWithdrawals { /// represents a pre-merge block. #[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] pub struct StaticFileBlockWithdrawals { /// The block withdrawals. A `None` value represents a pre-merge block. pub withdrawals: Option, } -impl Compact for StaticFileBlockWithdrawals { +#[cfg(feature = "reth-codec")] +impl reth_codecs::Compact for StaticFileBlockWithdrawals { fn to_compact(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]>, @@ -98,6 +99,7 @@ impl Compact for StaticFileBlockWithdrawals { 1 } fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) { + use bytes::Buf; if buf.get_u8() == 1 { let (w, buf) = Withdrawals::from_compact(buf, buf.len()); (Self { withdrawals: Some(w) }, buf) diff --git a/crates/storage/db-models/src/client_version.rs b/crates/storage/db-models/src/client_version.rs index a28e7385f..9fe87bd8f 100644 --- a/crates/storage/db-models/src/client_version.rs +++ b/crates/storage/db-models/src/client_version.rs @@ -1,12 +1,12 @@ //! Client version model. -use reth_codecs::{add_arbitrary_tests, Compact}; +use alloc::string::String; use serde::{Deserialize, Serialize}; /// Client version that accessed the database. #[derive(Clone, Eq, PartialEq, Debug, Default, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] pub struct ClientVersion { /// Client version pub version: String, @@ -23,7 +23,8 @@ impl ClientVersion { } } -impl Compact for ClientVersion { +#[cfg(feature = "reth-codec")] +impl reth_codecs::Compact for ClientVersion { fn to_compact(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]>, diff --git a/crates/storage/db-models/src/lib.rs b/crates/storage/db-models/src/lib.rs index fd97574a6..87a1b3f62 100644 --- a/crates/storage/db-models/src/lib.rs +++ b/crates/storage/db-models/src/lib.rs @@ -1,4 +1,15 @@ -//! Models used in storage module +//! Models used in storage module. + +#![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))] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; /// Accounts pub mod accounts;