Skip to content

Commit

Permalink
Use parity-db in current Dockerfiles (#455)
Browse files Browse the repository at this point in the history
* Use redb and in Dockerfiles

The motivation for redb was to remove the multiple rocksdb compile times from
CI.

* Correct feature flagging of coordinator and message-queue in Dockerfiles

* Correct message-queue DB type alias

* Use consistent table typing in redb

* Correct rebase artifacts

* Correct removal of binaries feature from message-queue

* Correct processor feature flagging

* Replace redb with parity-db

It still has much better compile times yet doesn't block when creating multiple
transactions. It also is actively maintained and doesn't grow our tree. The MPT
aspects are irrelevant.

* Correct stray Redb

* clippy warning

* Correct txn get
  • Loading branch information
kayabaNerve authored Nov 30, 2023
1 parent d1122a6 commit b823413
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 149 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions common/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
parity-db = { version = "0.4", default-features = false, optional = true }
rocksdb = { version = "0.21", default-features = false, features = ["lz4"], optional = true }

[features]
parity-db = ["dep:parity-db"]
rocksdb = ["dep:rocksdb"]
5 changes: 5 additions & 0 deletions common/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ mod rocks;
#[cfg(feature = "rocksdb")]
pub use rocks::{RocksDB, new_rocksdb};

#[cfg(feature = "parity-db")]
mod parity_db;
#[cfg(feature = "parity-db")]
pub use parity_db::{ParityDb, new_parity_db};

/// An object implementing get.
pub trait Get {
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>>;
Expand Down
46 changes: 46 additions & 0 deletions common/db/src/parity_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::sync::Arc;

pub use ::parity_db::{Options, Db as ParityDb};

use crate::*;

pub struct Transaction<'a>(&'a Arc<ParityDb>, Vec<(u8, Vec<u8>, Option<Vec<u8>>)>);

impl Get for Transaction<'_> {
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
let mut res = self.0.get(&key);
for change in &self.1 {
if change.1 == key.as_ref() {
res = change.2.clone();
}
}
res
}
}
impl DbTxn for Transaction<'_> {
fn put(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) {
self.1.push((0, key.as_ref().to_vec(), Some(value.as_ref().to_vec())))
}
fn del(&mut self, key: impl AsRef<[u8]>) {
self.1.push((0, key.as_ref().to_vec(), None))
}
fn commit(self) {
self.0.commit(self.1).unwrap()
}
}

impl Get for Arc<ParityDb> {
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
ParityDb::get(self, 0, key.as_ref()).unwrap()
}
}
impl Db for Arc<ParityDb> {
type Transaction<'a> = Transaction<'a>;
fn txn(&mut self) -> Self::Transaction<'_> {
Transaction(self, vec![])
}
}

pub fn new_parity_db(path: &str) -> Arc<ParityDb> {
Arc::new(ParityDb::open_or_create(&Options::with_columns(std::path::Path::new(path), 1)).unwrap())
}
6 changes: 5 additions & 1 deletion coordinator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ frost-schnorrkel = { path = "../crypto/schnorrkel" }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["std", "derive"] }

serai-db = { path = "../common/db", features = ["rocksdb"] }
serai-db = { path = "../common/db" }
serai-env = { path = "../common/env" }

processor-messages = { package = "serai-processor-messages", path = "../processor/messages" }
Expand All @@ -55,3 +55,7 @@ futures = { version = "0.3", default-features = false, features = ["std"] }
tributary = { package = "tributary-chain", path = "./tributary", features = ["tests"] }
sp-application-crypto = { git = "https://github.com/serai-dex/substrate", default-features = false, features = ["std"] }
sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features = false, features = ["std"] }

[features]
parity-db = ["serai-db/parity-db"]
rocksdb = ["serai-db/rocksdb"]
14 changes: 12 additions & 2 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use schnorr::SchnorrSignature;
use frost::Participant;

use serai_db::{DbTxn, Db};
use serai_env as env;

use scale::Encode;
use serai_client::{
Expand Down Expand Up @@ -1199,7 +1198,18 @@ async fn main() {

log::info!("starting coordinator service...");

let db = serai_db::new_rocksdb(&env::var("DB_PATH").expect("path to DB wasn't specified"));
#[allow(unused_variables, unreachable_code)]
let db = {
#[cfg(all(feature = "parity-db", feature = "rocksdb"))]
panic!("built with parity-db and rocksdb");
#[cfg(all(feature = "parity-db", not(feature = "rocksdb")))]
let db =
serai_db::new_parity_db(&serai_env::var("DB_PATH").expect("path to DB wasn't specified"));
#[cfg(feature = "rocksdb")]
let db =
serai_db::new_rocksdb(&serai_env::var("DB_PATH").expect("path to DB wasn't specified"));
db
};

let key = {
let mut key_hex = serai_env::var("SERAI_KEY").expect("Serai key wasn't provided");
Expand Down
5 changes: 3 additions & 2 deletions message-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ env_logger = { version = "0.10", default-features = false, features = ["humantim
# Uses a single threaded runtime since this shouldn't ever be CPU-bound
tokio = { version = "1", default-features = false, features = ["rt", "time", "io-util", "net", "macros"] }

serai-db = { path = "../common/db", features = ["rocksdb"], optional = true }
serai-db = { path = "../common/db", optional = true }

serai-env = { path = "../common/env" }

serai-primitives = { path = "../substrate/primitives", features = ["borsh"] }

[features]
binaries = ["serai-db"]
parity-db = ["serai-db/parity-db"]
rocksdb = ["serai-db/rocksdb"]
Loading

0 comments on commit b823413

Please sign in to comment.