Skip to content
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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 3 additions & 14 deletions src/db/blockstore_with_read_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>>;

Expand All @@ -28,16 +26,6 @@ impl BlockstoreReadCache for SizeTrackingLruCache<get_size::CidWrapper, Vec<u8>>
}
}

impl<T: BlockstoreReadCache> BlockstoreReadCache for Arc<T> {
fn get(&self, k: &Cid) -> Option<Vec<u8>> {
self.as_ref().get(k)
}

fn put(&self, k: Cid, block: Vec<u8>) {
self.as_ref().put(k, block)
}
}

pub trait BlockstoreReadCacheStats {
fn hit(&self) -> usize;

Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 1 addition & 10 deletions src/db/car/forest/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: AsyncWrite + Unpin>(&self, writer: &mut W) -> io::Result<()>;
Expand All @@ -761,16 +762,6 @@ fn written_len<T: Writable>(_: &T) -> u64 {
T::LEN
}

impl<T> Writable for &T
where
T: Writable,
{
async fn write_to<W: AsyncWrite + Unpin>(&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`]
Expand Down
78 changes: 5 additions & 73 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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`].
Expand All @@ -55,24 +55,6 @@ pub trait SettingsStore {
fn setting_keys(&self) -> anyhow::Result<Vec<String>>;
}

impl<T: SettingsStore> SettingsStore for Arc<T> {
fn read_bin(&self, key: &str) -> anyhow::Result<Option<Vec<u8>>> {
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<bool> {
SettingsStore::exists(self.as_ref(), key)
}

fn setting_keys(&self) -> anyhow::Result<Vec<String>> {
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.
Expand Down Expand Up @@ -106,6 +88,7 @@ impl<T: ?Sized + SettingsStore> 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`].
Expand All @@ -126,28 +109,6 @@ pub trait EthMappingsStore {
fn delete(&self, keys: Vec<EthHash>) -> anyhow::Result<()>;
}

impl<T: EthMappingsStore> EthMappingsStore for Arc<T> {
fn read_bin(&self, key: &EthHash) -> anyhow::Result<Option<Vec<u8>>> {
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<bool> {
EthMappingsStore::exists(self.as_ref(), key)
}

fn get_message_cids(&self) -> anyhow::Result<Vec<(Cid, u64)>> {
EthMappingsStore::get_message_cids(self.as_ref())
}

fn delete(&self, keys: Vec<EthHash>) -> anyhow::Result<()> {
EthMappingsStore::delete(self.as_ref(), keys)
}
}

pub struct DummyStore {}

const INDEXER_ERROR: &str =
Expand Down Expand Up @@ -207,6 +168,7 @@ impl<DB: DBStatistics> DBStatistics for std::sync::Arc<DB> {
}

/// 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.
///
Expand All @@ -223,18 +185,7 @@ impl PersistentStore for MemoryBlockstore {
}
}

impl<T: PersistentStore> PersistentStore for Arc<T> {
fn put_keyed_persistent(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
PersistentStore::put_keyed_persistent(self.as_ref(), k, block)
}
}

impl<T: PersistentStore> PersistentStore for &Arc<T> {
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<TipsetKey>;
Expand All @@ -243,32 +194,13 @@ pub trait HeaviestTipsetKeyProvider {
fn set_heaviest_tipset_key(&self, tsk: &TipsetKey) -> anyhow::Result<()>;
}

impl<T: HeaviestTipsetKeyProvider> HeaviestTipsetKeyProvider for Arc<T> {
fn heaviest_tipset_key(&self) -> anyhow::Result<TipsetKey> {
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<u8>)>;

fn unsubscribe_write_ops(&self);
}

impl<T: BlockstoreWriteOpsSubscribable> BlockstoreWriteOpsSubscribable for Arc<T> {
fn subscribe_write_ops(&self) -> tokio::sync::broadcast::Receiver<(Cid, Vec<u8>)> {
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};

Expand Down
22 changes: 3 additions & 19 deletions src/libp2p_bitswap/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>;
Expand All @@ -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>;
Expand All @@ -24,24 +26,6 @@ pub trait BitswapStoreReadWrite: BitswapStoreRead + Send + Sync + 'static {
fn insert(&self, block: &Block64<Self::Hashes>) -> anyhow::Result<()>;
}

impl<T: BitswapStoreRead> BitswapStoreRead for Arc<T> {
fn contains(&self, cid: &Cid) -> anyhow::Result<bool> {
BitswapStoreRead::contains(self.as_ref(), cid)
}

fn get(&self, cid: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
BitswapStoreRead::get(self.as_ref(), cid)
}
}

impl<T: BitswapStoreReadWrite> BitswapStoreReadWrite for Arc<T> {
type Hashes = <T as BitswapStoreReadWrite>::Hashes;

fn insert(&self, block: &Block64<Self::Hashes>) -> anyhow::Result<()> {
BitswapStoreReadWrite::insert(self.as_ref(), block)
}
}

pub type Block64<H> = Block<H, 64>;

/// Block
Expand Down
Loading