Skip to content

Commit 9ad34b1

Browse files
unify hasher construction for qmdb
1 parent 76762fb commit 9ad34b1

20 files changed

Lines changed: 109 additions & 126 deletions

File tree

storage/src/qmdb/any/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ pub(crate) mod test {
179179
use super::*;
180180
use crate::{
181181
journal::contiguous::{fixed::Config as FConfig, variable::Config as VConfig},
182-
qmdb::any::{FixedConfig, MerkleConfig, VariableConfig},
182+
qmdb::{
183+
self,
184+
any::{FixedConfig, MerkleConfig, VariableConfig},
185+
},
183186
translator::OneCap,
184187
};
185188
use commonware_codec::{Codec, CodecShared};
@@ -258,7 +261,7 @@ pub(crate) mod test {
258261
use crate::{
259262
index::Unordered as UnorderedIndex,
260263
journal::contiguous::Mutable,
261-
merkle::{self, mmr},
264+
merkle::mmr,
262265
qmdb::any::{
263266
db::Db as AnyDb,
264267
operation::{update::Update as UpdateTrait, Operation as AnyOperation},
@@ -627,7 +630,7 @@ pub(crate) mod test {
627630
V: CodecShared + Clone + Eq + std::hash::Hash + std::fmt::Debug,
628631
<D as Provable<mmr::Family>>::Operation: Codec,
629632
{
630-
use crate::{mmr::StandardHasher, qmdb::verify_proof};
633+
use crate::qmdb::verify_proof;
631634

632635
const ELEMENTS: u64 = 1000;
633636

@@ -689,7 +692,7 @@ pub(crate) mod test {
689692
}
690693
}
691694

692-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
695+
let hasher = qmdb::hasher::<Sha256>();
693696
let bounds = db.bounds().await;
694697
let inactivity_floor = db.inactivity_floor_loc().await;
695698
for loc in *inactivity_floor..*bounds.end {
@@ -749,7 +752,7 @@ pub(crate) mod test {
749752
D: DbAny<mmr::Family, Key = Digest, Value = V, Digest = Digest> + Provable<mmr::Family>,
750753
<D as Provable<mmr::Family>>::Operation: Codec + PartialEq + std::fmt::Debug,
751754
{
752-
use crate::{mmr::StandardHasher, qmdb::verify_proof};
755+
use crate::qmdb::verify_proof;
753756
use commonware_utils::NZU64;
754757

755758
// Add some operations
@@ -779,7 +782,7 @@ pub(crate) mod test {
779782
assert_eq!(historical_proof.leaves, regular_proof.leaves);
780783
assert_eq!(historical_proof.digests, regular_proof.digests);
781784
assert_eq!(historical_ops, regular_ops);
782-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
785+
let hasher = qmdb::hasher::<Sha256>();
783786
assert!(verify_proof(
784787
&hasher,
785788
&historical_proof,
@@ -828,7 +831,7 @@ pub(crate) mod test {
828831
D: DbAny<mmr::Family, Key = Digest, Value = V, Digest = Digest> + Provable<mmr::Family>,
829832
<D as Provable<mmr::Family>>::Operation: Codec + PartialEq + std::fmt::Debug + Clone,
830833
{
831-
use crate::{mmr::StandardHasher, qmdb::verify_proof};
834+
use crate::qmdb::verify_proof;
832835
use commonware_utils::NZU64;
833836

834837
// Apply two single-write batches and capture the commit-boundary size after the
@@ -858,7 +861,7 @@ pub(crate) mod test {
858861
assert_eq!(proof.leaves, historical_op_count);
859862
assert_eq!(ops.len(), expected_ops_len);
860863

861-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
864+
let hasher = qmdb::hasher::<Sha256>();
862865

863866
// Changing the proof digests should cause verification to fail
864867
{

storage/src/qmdb/any/ordered/fixed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ pub(crate) mod test {
126126
use crate::{
127127
index::Unordered as _,
128128
merkle::{
129-
self,
130-
mmr::{self, Location, StandardHasher as Standard},
129+
mmr::{self, Location},
131130
Location as GenericLocation,
132131
},
133132
qmdb::{
133+
self,
134134
any::{
135135
ordered::{
136136
test::{
@@ -367,7 +367,7 @@ pub(crate) mod test {
367367
// confirm that the end state of the db matches that of an identically updated hashmap.
368368
const ELEMENTS: u64 = 1000;
369369
executor.start(|context| async move {
370-
let hasher = Standard::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
370+
let hasher = qmdb::hasher::<Sha256>();
371371
let mut db = open_db(context.with_label("first")).await;
372372

373373
let mut map = HashMap::<Digest, Digest>::default();
@@ -676,7 +676,7 @@ pub(crate) mod test {
676676
let mut db = create_test_db(context.clone()).await;
677677
let ops = create_test_ops(20);
678678
apply_ops(&mut db, ops.clone()).await;
679-
let hasher = Standard::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
679+
let hasher = qmdb::hasher::<Sha256>();
680680
let root_hash = db.root();
681681
let original_op_count = db.bounds().await.end;
682682

@@ -730,7 +730,7 @@ pub(crate) mod test {
730730
fn test_ordered_any_fixed_db_historical_proof_edge_cases() {
731731
let executor = deterministic::Runner::default();
732732
executor.start(|context| async move {
733-
let hasher = Standard::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
733+
let hasher = qmdb::hasher::<Sha256>();
734734

735735
let mut db = create_test_db(context.with_label("first")).await;
736736
// Apply ops in multiple batches; each apply_ops ends in a commit, so the size
@@ -801,7 +801,7 @@ pub(crate) mod test {
801801
let ops = create_test_ops(100);
802802
apply_ops(&mut db, ops.clone()).await;
803803

804-
let hasher = Standard::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
804+
let hasher = qmdb::hasher::<Sha256>();
805805
let root = db.root();
806806

807807
let start_loc = Location::new(20);

storage/src/qmdb/any/sync/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
//! Contains implementation of [crate::qmdb::sync::Database] for all [Db] variants
33
//! (ordered/unordered, fixed/variable).
44
//!
5-
//! Callers verifying `any` sync proofs directly should use the same Merkle hasher configuration as
6-
//! the sync engine.
5+
//! Callers verifying `any` sync proofs directly should use `qmdb::hasher`.
76
87
use crate::{
98
index::Factory as IndexFactory,
109
journal::{
1110
authenticated,
1211
contiguous::{fixed, variable, Mutable, Reader as _},
1312
},
14-
merkle::{self, full, hasher::Standard as StandardHasher, Location},
13+
merkle::{self, full, Location},
1514
qmdb::{
1615
self,
1716
any::{
@@ -64,15 +63,14 @@ pub async fn has_local_target_state<F, E, H, S>(
6463
merkle_config: full::Config<S>,
6564
target: &qmdb::sync::Target<F, H::Digest>,
6665
inactive_peaks: usize,
67-
bagging: merkle::Bagging,
6866
) -> bool
6967
where
7068
F: merkle::Family,
7169
E: Context,
7270
H: Hasher,
7371
S: Strategy,
7472
{
75-
let hasher = StandardHasher::<H>::with_bagging(bagging);
73+
let hasher = qmdb::hasher::<H>();
7674
let peek = full::Merkle::<F, _, _, S>::peek_root(
7775
context.with_label("local_target_probe"),
7876
merkle_config,
@@ -111,7 +109,7 @@ where
111109
S: Strategy,
112110
Operation<F, U>: Codec + Committable + CodecShared,
113111
{
114-
let hasher = merkle::hasher::Standard::<H>::with_bagging(merkle::Bagging::BackwardFold);
112+
let hasher = qmdb::hasher::<H>();
115113

116114
let merkle = full::Merkle::<F, _, _, S>::init_sync(
117115
context.with_label("merkle"),
@@ -209,7 +207,6 @@ macro_rules! impl_sync_database {
209207
config.merkle_config.clone(),
210208
target,
211209
inactive_peaks,
212-
merkle::Bagging::BackwardFold,
213210
)
214211
.await
215212
}

storage/src/qmdb/any/unordered/fixed.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ pub(crate) mod test {
123123
use crate::{
124124
index::Unordered as _,
125125
merkle::{
126-
self,
127-
mmr::{self, Location, StandardHasher},
126+
mmr::{self, Location},
128127
Location as GenericLocation,
129128
},
130129
qmdb::{
130+
self,
131131
any::{
132132
test::fixed_db_config,
133133
unordered::{fixed::Operation, Update},
@@ -592,7 +592,7 @@ pub(crate) mod test {
592592
assert_eq!(historical_proof.leaves, regular_proof.leaves);
593593
assert_eq!(historical_proof.digests, regular_proof.digests);
594594
assert_eq!(historical_ops, regular_ops);
595-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
595+
let hasher = qmdb::hasher::<Sha256>();
596596
assert!(verify_proof(
597597
&hasher,
598598
&historical_proof,
@@ -640,7 +640,7 @@ pub(crate) mod test {
640640
fn test_any_fixed_db_historical_proof_edge_cases() {
641641
let executor = deterministic::Runner::default();
642642
executor.start(|context| async move {
643-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
643+
let hasher = qmdb::hasher::<Sha256>();
644644

645645
let mut db = create_test_db(context.with_label("first")).await;
646646
// Apply ops in multiple batches; each apply_ops ends in a commit, so the size
@@ -708,7 +708,7 @@ pub(crate) mod test {
708708
let executor = deterministic::Runner::default();
709709
executor.start(|context| async move {
710710
let ops = create_test_ops(100);
711-
let hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
711+
let hasher = qmdb::hasher::<Sha256>();
712712
let start_loc = Location::new(2);
713713
let max_ops = NZU64!(10);
714714

storage/src/qmdb/compact_witness.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
//! its proof on every serve.
2424
2525
use crate::{
26-
merkle::{self, compact, Family, Location, Proof},
26+
merkle::{compact, Family, Location, Proof},
2727
metadata::Metadata,
28-
qmdb::{sync::compact::Target, Error},
28+
qmdb::{self, sync::compact::Target, Error},
2929
Context,
3030
};
3131
use commonware_codec::{Decode as _, Encode as _, FixedSize};
@@ -236,7 +236,7 @@ where
236236

237237
let inactive_peaks =
238238
F::inactive_peaks(F::location_to_position(leaf_count), inactivity_floor_loc);
239-
let hasher = merkle::hasher::Standard::<H>::with_bagging(merkle::Bagging::BackwardFold);
239+
let hasher = qmdb::hasher::<H>();
240240
let root = merkle
241241
.root(&hasher, inactive_peaks)
242242
.map_err(|_| Error::DataCorrupted("failed to compute compact witness root"))?;
@@ -279,7 +279,7 @@ where
279279
H: Hasher,
280280
S: Strategy,
281281
{
282-
let hasher = merkle::hasher::Standard::<H>::with_bagging(merkle::Bagging::BackwardFold);
282+
let hasher = qmdb::hasher::<H>();
283283
let batch = {
284284
let batch = merkle.new_batch().add(&hasher, &commit_op_bytes);
285285
merkle.with_mem(|mem| batch.merkleize(mem, &hasher))
@@ -360,7 +360,7 @@ where
360360
H: Hasher,
361361
S: Strategy,
362362
{
363-
let hasher = merkle::hasher::Standard::<H>::with_bagging(merkle::Bagging::BackwardFold);
363+
let hasher = qmdb::hasher::<H>();
364364
let last_commit_loc = source.last_commit_loc();
365365
let inactivity_floor_loc = source.inactivity_floor_loc();
366366
let commit_op_bytes = source.encode_current_commit_op();

storage/src/qmdb/current/batch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{
66
index::Unordered as UnorderedIndex,
77
journal::contiguous::{Contiguous, Mutable},
88
merkle::{
9-
self, batch::MerkleizedBatch as GenericMerkleizedBatch, hasher::Standard as StandardHasher,
10-
mem::Mem, storage::Storage as MerkleStorage, Bagging, Graftable, Location, Position,
11-
Readable,
9+
self, batch::MerkleizedBatch as GenericMerkleizedBatch, mem::Mem,
10+
storage::Storage as MerkleStorage, Graftable, Location, Position, Readable,
1211
},
1312
qmdb::{
13+
self,
1414
any::{
1515
self,
1616
batch::{lookup_sorted, DiffEntry},
@@ -610,7 +610,7 @@ where
610610
(idx, chunk)
611611
});
612612

613-
let hasher = StandardHasher::<H>::with_bagging(Bagging::BackwardFold);
613+
let hasher = qmdb::hasher::<H>();
614614
let new_leaves = compute_grafted_leaves::<F, H, S, N>(
615615
&hasher,
616616
&ops_tree_adapter,

storage/src/qmdb/current/db.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use crate::{
1010
},
1111
merkle::{
1212
self, hasher::Standard as StandardHasher, mem::Mem, storage::Storage as MerkleStorage,
13-
Bagging, Location, Position,
13+
Location, Position,
1414
},
1515
metadata::{Config as MConfig, Metadata},
1616
qmdb::{
17+
self,
1718
any::{
1819
self,
1920
operation::{update::Update, Operation},
@@ -153,7 +154,7 @@ where
153154
&self.grafted_tree,
154155
grafting::height::<N>(),
155156
&self.any.log.merkle,
156-
StandardHasher::<H>::with_bagging(Bagging::BackwardFold),
157+
qmdb::hasher::<H>(),
157158
)
158159
}
159160

@@ -290,8 +291,7 @@ where
290291
///
291292
/// Unlike [`range_proof`](Self::range_proof) which returns grafted proofs incorporating the
292293
/// activity bitmap, this returns ops-tree Merkle proofs suitable for state sync. Direct
293-
/// verifiers should use the same Merkle hasher configuration as QMDB sync:
294-
/// [`Bagging::BackwardFold`].
294+
/// verifiers should use the same Merkle hasher configuration as QMDB sync.
295295
pub async fn ops_historical_proof(
296296
&self,
297297
historical_size: Location<F>,
@@ -562,7 +562,7 @@ where
562562
// the caller; reads through them now return inconsistent data.
563563
self.any.rewind(size).await?;
564564

565-
let hasher = StandardHasher::<H>::with_bagging(Bagging::BackwardFold);
565+
let hasher = qmdb::hasher::<H>();
566566
let grafted_tree = build_grafted_tree::<F, H, S, N>(
567567
&hasher,
568568
self.any.bitmap.as_ref(),
@@ -1164,7 +1164,7 @@ mod tests {
11641164
next_idx += 1;
11651165
}
11661166

1167-
let mut hasher = StandardHasher::<Sha256>::with_bagging(Bagging::BackwardFold);
1167+
let mut hasher = qmdb::hasher::<Sha256>();
11681168
let witness = db.ops_root_witness(&mut hasher).await.unwrap();
11691169
let ops_root = db.ops_root();
11701170
let canonical_root = db.root();
@@ -1196,7 +1196,7 @@ mod tests {
11961196
.unwrap();
11971197
populate_fixed_db::<mmb::Family, _>(&mut db, 0, 260).await;
11981198

1199-
let mut hasher = StandardHasher::<Sha256>::with_bagging(merkle::Bagging::BackwardFold);
1199+
let mut hasher = qmdb::hasher::<Sha256>();
12001200
let witness = db.ops_root_witness(&mut hasher).await.unwrap();
12011201
let ops_root = db.ops_root();
12021202
let canonical_root = db.root();
@@ -1245,7 +1245,7 @@ mod tests {
12451245
"test requires at least one pruned chunk to exercise the zero-chunk path"
12461246
);
12471247

1248-
let mut hasher = StandardHasher::<Sha256>::with_bagging(Bagging::BackwardFold);
1248+
let mut hasher = qmdb::hasher::<Sha256>();
12491249
let witness = db.ops_root_witness(&mut hasher).await.unwrap();
12501250
let ops_root = db.ops_root();
12511251
let canonical_root = db.root();
@@ -1272,7 +1272,7 @@ mod tests {
12721272
.await
12731273
.unwrap();
12741274

1275-
let mut hasher = StandardHasher::<Sha256>::with_bagging(Bagging::BackwardFold);
1275+
let mut hasher = qmdb::hasher::<Sha256>();
12761276
let witness = db.ops_root_witness(&mut hasher).await.unwrap();
12771277
let ops_root = db.ops_root();
12781278
let canonical_root = db.root();

storage/src/qmdb/current/grafting.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,6 @@ pub(super) fn grafted_root<
230230

231231
/// Compute the grafted root by folding peak digests with multi-peak chunk grafting.
232232
///
233-
/// For MMR this produces the same result as
234-
/// `hasher.root(leaves, 0, peaks).expect("zero inactive peaks is always valid")` because every
235-
/// chunk has a single peak at the grafting height. For MMB, chunks that span multiple
236-
/// sub-grafting-height peaks are folded together and combined with the bitmap chunk.
237-
///
238233
/// This custom folding process is necessary to ensure every bit of activity state from the bitmap
239234
/// is cryptographically incorporated into the root. Because MMB structures can have "incomplete"
240235
/// right edges, a single complete bitmap chunk block might logically cover several smaller,

0 commit comments

Comments
 (0)