pub use super::db::KeyValueProof;
use crate::{
index::ordered::Index,
journal::contiguous::fixed::Journal,
merkle::{Graftable, Location},
qmdb::{
any::{ordered::fixed::Operation, value::FixedEncoding, FixedValue},
current::FixedConfig as Config,
Error,
},
translator::Translator,
Context,
};
use commonware_cryptography::Hasher;
use commonware_utils::Array;
pub type Db<F, E, K, V, H, T, const N: usize> = super::db::Db<
F,
E,
Journal<E, Operation<F, K, V>>,
K,
FixedEncoding<V>,
Index<T, Location<F>>,
H,
N,
>;
impl<
F: Graftable,
E: Context,
K: Array,
V: FixedValue,
H: Hasher,
T: Translator,
const N: usize,
> Db<F, E, K, V, H, T, N>
{
pub async fn init(context: E, config: Config<T>) -> Result<Self, Error<F>> {
crate::qmdb::current::init(context, config).await
}
}
pub mod partitioned {
use super::*;
use crate::index::partitioned::ordered::Index;
pub type Db<F, E, K, V, H, T, const P: usize, const N: usize> =
crate::qmdb::current::ordered::db::Db<
F,
E,
Journal<E, Operation<F, K, V>>,
K,
FixedEncoding<V>,
Index<T, Location<F>, P>,
H,
N,
>;
impl<
F: Graftable,
E: Context,
K: Array,
V: FixedValue,
H: Hasher,
T: Translator,
const P: usize,
const N: usize,
> Db<F, E, K, V, H, T, P, N>
{
pub async fn init(context: E, config: Config<T>) -> Result<Self, Error<F>> {
crate::qmdb::current::init(context, config).await
}
}
}
#[cfg(test)]
pub mod test {
use super::*;
use crate::{
mmr,
qmdb::{
current::{ordered::tests as shared, tests::fixed_config},
Error,
},
translator::OneCap,
};
use commonware_cryptography::{sha256::Digest, Sha256};
use commonware_macros::test_traced;
use commonware_runtime::{deterministic, Metrics, Runner as _};
use commonware_utils::{
bitmap::{Prunable as BitMap, Readable as _},
NZU64,
};
type CurrentTest = Db<mmr::Family, deterministic::Context, Digest, Digest, Sha256, OneCap, 32>;
async fn open_db(context: deterministic::Context, partition_prefix: String) -> CurrentTest {
let cfg = fixed_config::<OneCap>(&partition_prefix, &context);
CurrentTest::init(context, cfg).await.unwrap()
}
#[test_traced("DEBUG")]
pub fn test_current_db_verify_proof_over_bits_in_uncommitted_chunk() {
shared::test_verify_proof_over_bits_in_uncommitted_chunk(open_db);
}
#[test_traced("DEBUG")]
pub fn test_current_db_range_proofs() {
shared::test_range_proofs(open_db);
}
#[test_traced("DEBUG")]
pub fn test_range_proof_returns_error_on_pruned_chunks() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let partition = "range-proofs-pruned".to_string();
let mut hasher = Sha256::new();
let mut db = open_db(context.with_label("db"), partition).await;
let chunk_bits = BitMap::<32>::CHUNK_SIZE_BITS;
let key = Sha256::fill(0x11);
for i in 0..chunk_bits + 10 {
let value = Sha256::hash(&i.to_be_bytes());
let merkleized = db
.new_batch()
.write(key, Some(value))
.merkleize(&db, None)
.await
.unwrap();
db.apply_batch(merkleized).await.unwrap();
}
let floor = db.any.inactivity_floor_loc;
db.prune(floor).await.unwrap();
assert!(
db.status.pruned_chunks() > 0,
"expected at least one pruned chunk"
);
let result = db
.range_proof(&mut hasher, Location::new(0), NZU64!(1))
.await;
assert!(
matches!(result, Err(Error::OperationPruned(_))),
"expected OperationPruned, got {result:?}"
);
db.destroy().await.unwrap();
});
}
#[test_traced("DEBUG")]
pub fn test_current_db_key_value_proof() {
shared::test_key_value_proof(open_db);
}
#[test_traced("WARN")]
pub fn test_current_db_proving_repeated_updates() {
shared::test_proving_repeated_updates(open_db);
}
#[test_traced("DEBUG")]
pub fn test_current_db_exclusion_proofs() {
shared::test_exclusion_proofs(open_db);
}
}