Skip to content

Commit

Permalink
refactor(state): add a DiskWriteBatch wrapper for RocksDB writes
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Feb 18, 2022
1 parent 08d6526 commit f2b4184
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
15 changes: 8 additions & 7 deletions zebra-state/src/service/finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use crate::{
service::{
check,
finalized_state::{
disk_db::{DiskDb, ReadDisk, WriteDisk},
disk_format::{FromDisk, IntoDisk, TransactionLocation},
disk_db::{DiskDb, DiskWriteBatch, ReadDisk, WriteDisk},
disk_format::{FromDisk, TransactionLocation},
},
QueuedFinalized,
},
Expand Down Expand Up @@ -299,8 +299,8 @@ impl FinalizedState {
// the genesis case.
// If the closure returns an error it will be propagated and the batch will not be written
// to the BD afterwards.
let prepare_commit = || -> Result<rocksdb::WriteBatch, BoxError> {
let mut batch = rocksdb::WriteBatch::default();
let prepare_commit = || -> Result<DiskWriteBatch, BoxError> {
let mut batch = DiskWriteBatch::new();

// Index the block
batch.zs_insert(hash_by_height, height, hash);
Expand Down Expand Up @@ -366,7 +366,7 @@ impl FinalizedState {
if let Some(utxo) = self.utxo(outpoint) {
all_utxos_spent_by_block.insert(*outpoint, utxo);
}
batch.delete_cf(utxo_by_outpoint, outpoint.as_bytes());
batch.zs_delete(utxo_by_outpoint, outpoint);
}
// Coinbase inputs represent new coins,
// so there are no UTXOs to mark as spent.
Expand Down Expand Up @@ -726,8 +726,9 @@ impl FinalizedState {
#[cfg(any(test, feature = "proptest-impl"))]
#[allow(dead_code)]
pub fn set_current_value_pool(&self, fake_value_pool: ValueBalance<NonNegative>) {
let mut batch = rocksdb::WriteBatch::default();
let mut batch = DiskWriteBatch::new();
let value_pool_cf = self.db.cf_handle("tip_chain_value_pool").unwrap();

batch.zs_insert(value_pool_cf, (), fake_value_pool);
self.db.write(batch).unwrap();
}
Expand All @@ -736,7 +737,7 @@ impl FinalizedState {
/// referenced in a block, for testing purposes _only_.
#[cfg(test)]
pub fn populate_with_anchors(&self, block: &Block) {
let mut batch = rocksdb::WriteBatch::default();
let mut batch = DiskWriteBatch::new();

let sprout_anchors = self.db.cf_handle("sprout_anchors").unwrap();
let sapling_anchors = self.db.cf_handle("sapling_anchors").unwrap();
Expand Down
29 changes: 20 additions & 9 deletions zebra-state/src/service/finalized_state/disk_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use crate::{
Config,
};

/// Wrapper struct to ensure low-level disk reads go through
/// the correct API.
/// Wrapper struct to ensure low-level database access goes through the correct API.
pub struct DiskDb {
/// The inner RocksDB database.
db: rocksdb::DB,
Expand All @@ -32,6 +31,12 @@ pub struct DiskDb {
ephemeral: bool,
}

/// Wrapper struct to ensure low-level database writes go through the correct API.
pub struct DiskWriteBatch {
/// The inner RocksDB write batch.
batch: rocksdb::WriteBatch,
}

/// Helper trait for inserting (Key, Value) pairs into rocksdb with a consistently
/// defined format
pub trait WriteDisk {
Expand All @@ -48,23 +53,23 @@ pub trait WriteDisk {
K: IntoDisk + Debug;
}

impl WriteDisk for rocksdb::WriteBatch {
impl WriteDisk for DiskWriteBatch {
fn zs_insert<K, V>(&mut self, cf: &rocksdb::ColumnFamily, key: K, value: V)
where
K: IntoDisk + Debug,
V: IntoDisk,
{
let key_bytes = key.as_bytes();
let value_bytes = value.as_bytes();
self.put_cf(cf, key_bytes, value_bytes);
self.batch.put_cf(cf, key_bytes, value_bytes);
}

fn zs_delete<K>(&mut self, cf: &rocksdb::ColumnFamily, key: K)
where
K: IntoDisk + Debug,
{
let key_bytes = key.as_bytes();
self.delete_cf(cf, key_bytes);
self.batch.delete_cf(cf, key_bytes);
}
}

Expand Down Expand Up @@ -117,6 +122,14 @@ impl ReadDisk for DiskDb {
}
}

impl DiskWriteBatch {
pub fn new() -> Self {
DiskWriteBatch {
batch: rocksdb::WriteBatch::default(),
}
}
}

impl DiskDb {
/// The ideal open file limit for Zebra
const IDEAL_OPEN_FILE_LIMIT: u64 = 1024;
Expand Down Expand Up @@ -216,11 +229,9 @@ impl DiskDb {
}

/// Writes `batch` to the database.
///
/// TODO: replace with type wrapper in next PR.
pub fn write(&self, batch: rocksdb::WriteBatch) -> Result<(), rocksdb::Error> {
pub fn write(&self, batch: DiskWriteBatch) -> Result<(), rocksdb::Error> {
// TODO: move writing to the database to a blocking thread (#2188)
self.db.write(batch)
self.db.write(batch.batch)
}

/// Returns the database options for the finalized state database.
Expand Down

0 comments on commit f2b4184

Please sign in to comment.