diff --git a/Cargo.lock b/Cargo.lock index 8244eb40b3fb..ae3e5903dc3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -471,6 +471,17 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "auto_impl" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffdcb70bdbc4d478427380519163274ac86e52916e10f0a8889adf0f96d3fee7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -3243,6 +3254,7 @@ dependencies = [ "async-fs", "async-trait", "asynchronous-codec", + "auto_impl", "axum", "backon", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index 455331394117..1685fa97e802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ async-compression = { version = "0.4", features = ["tokio", "zstd"] } async-fs = "2" async-trait = "0.1" asynchronous-codec = "0.7" +auto_impl = "1" axum = "0.8" backon = "1" base64 = "0.22" diff --git a/src/db/blockstore_with_read_cache.rs b/src/db/blockstore_with_read_cache.rs index 57cf953d50e2..cd1061257756 100644 --- a/src/db/blockstore_with_read_cache.rs +++ b/src/db/blockstore_with_read_cache.rs @@ -3,13 +3,11 @@ use cid::Cid; use fvm_ipld_blockstore::Blockstore; -use std::sync::{ - Arc, - atomic::{self, AtomicUsize}, -}; +use std::sync::atomic::{self, AtomicUsize}; use crate::utils::{cache::SizeTrackingLruCache, get_size}; +#[auto_impl::auto_impl(&, Arc)] pub trait BlockstoreReadCache { fn get(&self, k: &Cid) -> Option>; @@ -28,16 +26,6 @@ impl BlockstoreReadCache for SizeTrackingLruCache> } } -impl BlockstoreReadCache for Arc { - fn get(&self, k: &Cid) -> Option> { - self.as_ref().get(k) - } - - fn put(&self, k: Cid, block: Vec) { - self.as_ref().put(k, block) - } -} - pub trait BlockstoreReadCacheStats { fn hit(&self) -> usize; @@ -123,6 +111,7 @@ mod tests { use multihash_codetable::Code::Blake2b256; use multihash_codetable::MultihashDigest as _; use rand::Rng as _; + use std::sync::Arc; #[test] fn test_blockstore_read_cache() { diff --git a/src/db/car/forest/index/mod.rs b/src/db/car/forest/index/mod.rs index c35f1a9b91e5..9dbf4ae93f57 100644 --- a/src/db/car/forest/index/mod.rs +++ b/src/db/car/forest/index/mod.rs @@ -747,6 +747,7 @@ trait Readable { Self: Sized; } +#[auto_impl::auto_impl(&)] trait Writable { /// Must only return [`Err(_)`] if the underlying io fails. async fn write_to(&self, writer: &mut W) -> io::Result<()>; @@ -761,16 +762,6 @@ fn written_len(_: &T) -> u64 { T::LEN } -impl Writable for &T -where - T: Writable, -{ - async fn write_to(&self, writer: &mut W) -> io::Result<()> { - T::write_to(self, writer).await - } - const LEN: u64 = T::LEN; -} - // This lives in a module so its constructor can be private mod util { /// Like [`std::num::NonZeroU64`], but is never [`u64::MAX`] diff --git a/src/db/mod.rs b/src/db/mod.rs index 48cd6d9d4ff8..f833be73ca1c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -25,7 +25,6 @@ use cid::Cid; pub use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore}; use serde::Serialize; use serde::de::DeserializeOwned; -use std::sync::Arc; pub const CAR_DB_DIR_NAME: &str = "car_db"; @@ -38,6 +37,7 @@ pub mod setting_keys { /// Interface used to store and retrieve settings from the database. /// To store IPLD blocks, use the `BlockStore` trait. +#[auto_impl::auto_impl(&, Arc)] pub trait SettingsStore { /// Reads binary field from the Settings store. This should be used for /// non-serializable data. For serializable data, use [`SettingsStoreExt::read_obj`]. @@ -55,24 +55,6 @@ pub trait SettingsStore { fn setting_keys(&self) -> anyhow::Result>; } -impl SettingsStore for Arc { - fn read_bin(&self, key: &str) -> anyhow::Result>> { - SettingsStore::read_bin(self.as_ref(), key) - } - - fn write_bin(&self, key: &str, value: &[u8]) -> anyhow::Result<()> { - SettingsStore::write_bin(self.as_ref(), key, value) - } - - fn exists(&self, key: &str) -> anyhow::Result { - SettingsStore::exists(self.as_ref(), key) - } - - fn setting_keys(&self) -> anyhow::Result> { - SettingsStore::setting_keys(self.as_ref()) - } -} - /// Extension trait for the [`SettingsStore`] trait. It is implemented for all types that implement /// [`SettingsStore`]. /// It provides methods for writing and reading any serializable object from the store. @@ -106,6 +88,7 @@ impl SettingsStoreExt for T { /// Interface used to store and retrieve Ethereum mappings from the database. /// To store IPLD blocks, use the `BlockStore` trait. +#[auto_impl::auto_impl(&, Arc)] pub trait EthMappingsStore { /// Reads binary field from the `EthMappings` store. This should be used for /// non-serializable data. For serializable data, use [`EthMappingsStoreExt::read_obj`]. @@ -126,28 +109,6 @@ pub trait EthMappingsStore { fn delete(&self, keys: Vec) -> anyhow::Result<()>; } -impl EthMappingsStore for Arc { - fn read_bin(&self, key: &EthHash) -> anyhow::Result>> { - EthMappingsStore::read_bin(self.as_ref(), key) - } - - fn write_bin(&self, key: &EthHash, value: &[u8]) -> anyhow::Result<()> { - EthMappingsStore::write_bin(self.as_ref(), key, value) - } - - fn exists(&self, key: &EthHash) -> anyhow::Result { - EthMappingsStore::exists(self.as_ref(), key) - } - - fn get_message_cids(&self) -> anyhow::Result> { - EthMappingsStore::get_message_cids(self.as_ref()) - } - - fn delete(&self, keys: Vec) -> anyhow::Result<()> { - EthMappingsStore::delete(self.as_ref(), keys) - } -} - pub struct DummyStore {} const INDEXER_ERROR: &str = @@ -207,6 +168,7 @@ impl DBStatistics for std::sync::Arc { } /// A trait that allows for storing data that is not garbage collected. +#[auto_impl::auto_impl(&, Arc)] pub trait PersistentStore: Blockstore { /// Puts a keyed block with pre-computed CID into the database. /// @@ -223,18 +185,7 @@ impl PersistentStore for MemoryBlockstore { } } -impl PersistentStore for Arc { - fn put_keyed_persistent(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> { - PersistentStore::put_keyed_persistent(self.as_ref(), k, block) - } -} - -impl PersistentStore for &Arc { - fn put_keyed_persistent(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> { - PersistentStore::put_keyed_persistent(self.as_ref(), k, block) - } -} - +#[auto_impl::auto_impl(&, Arc)] pub trait HeaviestTipsetKeyProvider { /// Returns the currently tracked heaviest tipset. fn heaviest_tipset_key(&self) -> anyhow::Result; @@ -243,32 +194,13 @@ pub trait HeaviestTipsetKeyProvider { fn set_heaviest_tipset_key(&self, tsk: &TipsetKey) -> anyhow::Result<()>; } -impl HeaviestTipsetKeyProvider for Arc { - fn heaviest_tipset_key(&self) -> anyhow::Result { - self.as_ref().heaviest_tipset_key() - } - - fn set_heaviest_tipset_key(&self, tsk: &TipsetKey) -> anyhow::Result<()> { - self.as_ref().set_heaviest_tipset_key(tsk) - } -} - +#[auto_impl::auto_impl(&, Arc)] pub trait BlockstoreWriteOpsSubscribable { fn subscribe_write_ops(&self) -> tokio::sync::broadcast::Receiver<(Cid, Vec)>; fn unsubscribe_write_ops(&self); } -impl BlockstoreWriteOpsSubscribable for Arc { - fn subscribe_write_ops(&self) -> tokio::sync::broadcast::Receiver<(Cid, Vec)> { - self.as_ref().subscribe_write_ops() - } - - fn unsubscribe_write_ops(&self) { - self.as_ref().unsubscribe_write_ops() - } -} - pub mod db_engine { use std::path::{Path, PathBuf}; diff --git a/src/libp2p_bitswap/store.rs b/src/libp2p_bitswap/store.rs index 69deac8cf507..b41847c8d46b 100644 --- a/src/libp2p_bitswap/store.rs +++ b/src/libp2p_bitswap/store.rs @@ -3,10 +3,11 @@ use super::*; use multihash_derive::MultihashDigest; +use std::marker::PhantomData; use std::ops::Deref; -use std::{marker::PhantomData, sync::Arc}; /// Trait implemented by a block store for reading. +#[auto_impl::auto_impl(&, Arc)] pub trait BitswapStoreRead { /// A have query needs to know if the block store contains the block. fn contains(&self, cid: &Cid) -> anyhow::Result; @@ -16,6 +17,7 @@ pub trait BitswapStoreRead { } /// Trait implemented by a block store for reading and writing. +#[auto_impl::auto_impl(&, Arc)] pub trait BitswapStoreReadWrite: BitswapStoreRead + Send + Sync + 'static { /// The hashes parameters. type Hashes: MultihashDigest<64>; @@ -24,24 +26,6 @@ pub trait BitswapStoreReadWrite: BitswapStoreRead + Send + Sync + 'static { fn insert(&self, block: &Block64) -> anyhow::Result<()>; } -impl BitswapStoreRead for Arc { - fn contains(&self, cid: &Cid) -> anyhow::Result { - BitswapStoreRead::contains(self.as_ref(), cid) - } - - fn get(&self, cid: &Cid) -> anyhow::Result>> { - BitswapStoreRead::get(self.as_ref(), cid) - } -} - -impl BitswapStoreReadWrite for Arc { - type Hashes = ::Hashes; - - fn insert(&self, block: &Block64) -> anyhow::Result<()> { - BitswapStoreReadWrite::insert(self.as_ref(), block) - } -} - pub type Block64 = Block; /// Block