Skip to content

Commit

Permalink
Use different implementations of DB in test and in default builds
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Aug 22, 2024
1 parent 738a66d commit 27f3e51
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --benches --tests --no-default-features
args: --workspace --benches --tests
env:
RUSTFLAGS: "-C instrument-coverage"
LLVM_PROFILE_FILE: "profile-%p-%m.profraw"
Expand Down
16 changes: 5 additions & 11 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

use crate::cli::CliOpts;
use avail_core::AppId;
#[cfg(not(feature = "rocksdb"))]
use avail_light_core::data::MemoryDB as DB;
#[cfg(feature = "rocksdb")]
use avail_light_core::data::RocksDB as DB;
use avail_light_core::{
api,
consts::EXPECTED_SYSTEM_VERSION,
data::{ClientIdKey, Database, IsFinalitySyncedKey, IsSyncedKey, LatestHeaderKey},
data::{self, ClientIdKey, Database, IsFinalitySyncedKey, IsSyncedKey, LatestHeaderKey, DB},
network::{
self,
p2p::{self, BOOTSTRAP_LIST_EMPTY_MESSAGE},
Expand Down Expand Up @@ -51,7 +47,6 @@ static GLOBAL: Jemalloc = Jemalloc;
async fn run(
cfg: RuntimeConfig,
identity_cfg: IdentityConfig,

db: DB,
shutdown: Controller<String>,
client_id: Uuid,
Expand Down Expand Up @@ -99,7 +94,7 @@ async fn run(
false,
shutdown.clone(),
#[cfg(feature = "rocksdb")]
db.inner(),
db.clone(),
)
.await?;

Expand Down Expand Up @@ -397,11 +392,10 @@ pub async fn main() -> Result<()> {
fs::remove_dir_all(&cfg.avail_path).wrap_err("Failed to remove local state directory")?;
}

#[cfg(feature = "rocksdb")]
let db = DB::open(&cfg.avail_path).expect("Avail Light could not initialize database");

#[cfg(not(feature = "rocksdb"))]
let db = DB::default();
let db = data::DB::default();
#[cfg(feature = "rocksdb")]
let db = data::DB::open(&cfg.avail_path)?;

let client_id = db.get(ClientIdKey).unwrap_or_else(|| {
let client_id = Uuid::new_v4();
Expand Down
5 changes: 1 addition & 4 deletions compatibility-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#[cfg(not(feature = "rocksdb"))]
use avail_light_core::data::MemoryDB as DB;
#[cfg(feature = "rocksdb")]
use avail_light_core::data::RocksDB as DB;
use avail_light_core::{
data::DB,
network::rpc::{
self,
configuration::{ExponentialConfig, RPCConfig, RetryConfig},
Expand Down
7 changes: 4 additions & 3 deletions core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ use sp_core::ed25519;
mod keys;
use keys::*;

#[cfg(not(feature = "rocksdb"))]
mod mem_db;
#[cfg(not(feature = "rocksdb"))]
pub use mem_db::*;

#[cfg(feature = "rocksdb")]
mod rocks_db;

#[cfg(not(feature = "rocksdb"))]
pub type DB = mem_db::MemoryDB;
#[cfg(feature = "rocksdb")]
pub use rocks_db::*;
pub type DB = rocks_db::RocksDB;

/// Column family for application state
pub const APP_STATE_CF: &str = "app_state_cf";
Expand Down
4 changes: 1 addition & 3 deletions core/src/maintenance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use color_eyre::{eyre::WrapErr, Result};
use std::sync::Arc;
#[cfg(not(feature = "rocksdb"))]
use std::time::Instant;
use tokio::sync::broadcast;
use tracing::{debug, error, info};
Expand All @@ -18,8 +17,7 @@ pub async fn process_block(
maintenance_config: MaintenanceConfig,
metrics: &Arc<impl Metrics>,
) -> Result<()> {
#[cfg(not(feature = "rocksdb"))]
if block_number % maintenance_config.pruning_interval == 0 {
if cfg!(not(feature = "rocksdb")) && block_number % maintenance_config.pruning_interval == 0 {
info!(block_number, "Pruning...");
match p2p_client.prune_expired_records(Instant::now()).await {
Ok(pruned) => info!(block_number, pruned, "Pruning finished"),
Expand Down
14 changes: 5 additions & 9 deletions core/src/network/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,30 @@ use multihash::{self, Hasher};
use rand::thread_rng;
use semver::Version;
use serde::{Deserialize, Serialize};
#[cfg(feature = "rocksdb")]
use std::sync::Arc;
use std::{fmt, net::Ipv4Addr, str::FromStr, time::Duration};
use tokio::sync::{broadcast, mpsc, oneshot};
use tracing::info;

#[cfg(feature = "network-analysis")]
pub mod analyzer;
mod client;
pub mod configuration;
mod event_loop;
mod kad_mem_providers;

#[cfg(not(feature = "rocksdb"))]
mod kad_mem_store;
#[cfg(feature = "rocksdb")]
mod kad_rocksdb_store;

#[cfg(not(feature = "rocksdb"))]
pub use kad_mem_store::MemoryStoreConfig;
#[cfg(feature = "rocksdb")]
pub use kad_rocksdb_store::ExpirationCompactionFilterFactory;
#[cfg(feature = "rocksdb")]
pub use kad_rocksdb_store::RocksDBStoreConfig;

#[cfg(feature = "rocksdb")]
pub type Store = kad_rocksdb_store::RocksDBStore;
#[cfg(not(feature = "rocksdb"))]
pub type Store = kad_mem_store::MemoryStore;
#[cfg(feature = "rocksdb")]
pub type Store = kad_rocksdb_store::RocksDBStore;

use crate::{
data::{Database, P2PKeypairKey},
Expand Down Expand Up @@ -220,7 +216,7 @@ pub async fn init(
genesis_hash: &str,
is_fat: bool,
shutdown: Controller<String>,
#[cfg(feature = "rocksdb")] db: Arc<rocksdb::DB>,
#[cfg(feature = "rocksdb")] db: crate::data::DB,
) -> Result<(Client, EventLoop, broadcast::Receiver<OutputEvent>)> {
// create sender channel for P2P event loop commands
let (command_sender, command_receiver) = mpsc::unbounded_channel();
Expand All @@ -235,7 +231,7 @@ pub async fn init(
id_keys.public().to_peer_id(),
(&cfg).into(),
#[cfg(feature = "rocksdb")]
db,
db.inner(),
);
// create Swarm
let swarm = build_swarm(&cfg, version, genesis_hash, &id_keys, store)
Expand Down
8 changes: 2 additions & 6 deletions core/src/network/p2p/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::ProvidersConfig;
#[cfg(feature = "rocksdb")]
use super::RocksDBStoreConfig;
#[cfg(not(feature = "rocksdb"))]
use crate::network::p2p::MemoryStoreConfig;
use crate::types::{duration_seconds_format, KademliaMode, MultiaddrConfig, SecretKey};
use libp2p::{kad, multiaddr::Protocol, Multiaddr};
Expand Down Expand Up @@ -217,7 +214,6 @@ impl From<&LibP2PConfig> for kad::Config {
}
}

#[cfg(not(feature = "rocksdb"))]
impl From<&LibP2PConfig> for MemoryStoreConfig {
fn from(cfg: &LibP2PConfig) -> Self {
MemoryStoreConfig {
Expand All @@ -232,9 +228,9 @@ impl From<&LibP2PConfig> for MemoryStoreConfig {
}

#[cfg(feature = "rocksdb")]
impl From<&LibP2PConfig> for RocksDBStoreConfig {
impl From<&LibP2PConfig> for super::RocksDBStoreConfig {
fn from(cfg: &LibP2PConfig) -> Self {
RocksDBStoreConfig {
super::RocksDBStoreConfig {
max_value_bytes: cfg.kademlia.max_kad_record_size + 1,
providers: ProvidersConfig {
max_providers_per_key: usize::from(cfg.kademlia.record_replication_factor), // Needs to match the replication factor, per libp2p docs
Expand Down
8 changes: 2 additions & 6 deletions crawler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#[cfg(not(feature = "rocksdb"))]
use avail_light_core::data::MemoryDB as DB;
#[cfg(feature = "rocksdb")]
use avail_light_core::data::RocksDB as DB;
use avail_light_core::{
crawl_client,
data::{Database, LatestHeaderKey},
data::{Database, LatestHeaderKey, DB},
network::{p2p, rpc, Network},
shutdown::Controller,
telemetry::{otlp, MetricCounter, Metrics},
Expand Down Expand Up @@ -100,7 +96,7 @@ async fn run(config: Config, db: DB, shutdown: Controller<String>) -> Result<()>
true,
shutdown.clone(),
#[cfg(feature = "rocksdb")]
db.inner(),
db.clone(),
)
.await?;

Expand Down
8 changes: 2 additions & 6 deletions fat/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::{fs, path::Path, sync::Arc};

#[cfg(not(feature = "rocksdb"))]
use avail_light_core::data::MemoryDB as DB;
#[cfg(feature = "rocksdb")]
use avail_light_core::data::RocksDB as DB;
use avail_light_core::{
data::{Database, LatestHeaderKey},
data::{Database, LatestHeaderKey, DB},
fat_client,
network::{p2p, rpc, Network},
shutdown::Controller,
Expand Down Expand Up @@ -100,7 +96,7 @@ async fn run(config: Config, db: DB, shutdown: Controller<String>) -> Result<()>
true,
shutdown.clone(),
#[cfg(feature = "rocksdb")]
db.inner(),
db.clone(),
)
.await?;

Expand Down

0 comments on commit 27f3e51

Please sign in to comment.