Skip to content

Commit cfb8e2b

Browse files
authored
[storage] Parameterize resolver impls over Strategy (#3750)
1 parent 7fbc91b commit cfb8e2b

3 files changed

Lines changed: 253 additions & 29 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ impl_current_sync_database!(
381381

382382
macro_rules! impl_current_resolver {
383383
($db:ident, $op:ident, $val_bound:ident, $key_bound:path $(; $($where_extra:tt)+)?) => {
384-
impl<F, E, K, V, H, T, const N: usize> crate::qmdb::sync::Resolver
385-
for std::sync::Arc<$db<F, E, K, V, H, T, N>>
384+
impl<F, E, K, V, H, T, const N: usize, S> crate::qmdb::sync::Resolver
385+
for std::sync::Arc<$db<F, E, K, V, H, T, N, S>>
386386
where
387387
F: Graftable,
388388
E: Context,
@@ -391,6 +391,7 @@ macro_rules! impl_current_resolver {
391391
H: Hasher,
392392
T: Translator + Send + Sync + 'static,
393393
T::Key: Send + Sync,
394+
S: Strategy,
394395
$($($where_extra)+)?
395396
{
396397
type Family = F;
@@ -423,10 +424,10 @@ macro_rules! impl_current_resolver {
423424
}
424425
}
425426

426-
impl<F, E, K, V, H, T, const N: usize> crate::qmdb::sync::Resolver
427+
impl<F, E, K, V, H, T, const N: usize, S> crate::qmdb::sync::Resolver
427428
for std::sync::Arc<
428429
commonware_utils::sync::AsyncRwLock<
429-
$db<F, E, K, V, H, T, N>,
430+
$db<F, E, K, V, H, T, N, S>,
430431
>,
431432
>
432433
where
@@ -437,6 +438,7 @@ macro_rules! impl_current_resolver {
437438
H: Hasher,
438439
T: Translator + Send + Sync + 'static,
439440
T::Key: Send + Sync,
441+
S: Strategy,
440442
$($($where_extra)+)?
441443
{
442444
type Family = F;
@@ -470,10 +472,10 @@ macro_rules! impl_current_resolver {
470472
}
471473
}
472474

473-
impl<F, E, K, V, H, T, const N: usize> crate::qmdb::sync::Resolver
475+
impl<F, E, K, V, H, T, const N: usize, S> crate::qmdb::sync::Resolver
474476
for std::sync::Arc<
475477
commonware_utils::sync::AsyncRwLock<
476-
Option<$db<F, E, K, V, H, T, N>>,
478+
Option<$db<F, E, K, V, H, T, N, S>>,
477479
>,
478480
>
479481
where
@@ -484,6 +486,7 @@ macro_rules! impl_current_resolver {
484486
H: Hasher,
485487
T: Translator + Send + Sync + 'static,
486488
T::Key: Send + Sync,
489+
S: Strategy,
487490
$($($where_extra)+)?
488491
{
489492
type Family = F;

storage/src/qmdb/sync/compact.rs

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use commonware_codec::{
7171
Encode, EncodeSize, Error as CodecError, RangeCfg, Read, ReadExt as _, Write,
7272
};
7373
use commonware_cryptography::{Digest, Hasher};
74+
use commonware_parallel::Strategy;
7475
use commonware_runtime::{Buf, BufMut, Clock, Metrics, Storage};
7576
use commonware_utils::{sync::AsyncRwLock, Array};
7677
use std::{future::Future, num::NonZeroU64, sync::Arc};
@@ -401,12 +402,13 @@ where
401402
// historical tip proof and current frontier pins from the full source.
402403
macro_rules! impl_compact_resolver_keyless {
403404
($db:ident, $op:ident, $val_bound:ident) => {
404-
impl<F, E, V, H> Resolver for Arc<$db<F, E, V, H>>
405+
impl<F, E, V, H, S> Resolver for Arc<$db<F, E, V, H, S>>
405406
where
406407
F: Family,
407408
E: crate::Context,
408409
V: $val_bound + Send + Sync + 'static,
409410
H: Hasher,
411+
S: Strategy,
410412
{
411413
type Family = F;
412414
type Digest = H::Digest;
@@ -438,12 +440,13 @@ macro_rules! impl_compact_resolver_keyless {
438440
}
439441
}
440442

441-
impl<F, E, V, H> Resolver for Arc<AsyncRwLock<$db<F, E, V, H>>>
443+
impl<F, E, V, H, S> Resolver for Arc<AsyncRwLock<$db<F, E, V, H, S>>>
442444
where
443445
F: Family,
444446
E: crate::Context,
445447
V: $val_bound + Send + Sync + 'static,
446448
H: Hasher,
449+
S: Strategy,
447450
{
448451
type Family = F;
449452
type Digest = H::Digest;
@@ -476,12 +479,13 @@ macro_rules! impl_compact_resolver_keyless {
476479
}
477480
}
478481

479-
impl<F, E, V, H> Resolver for Arc<AsyncRwLock<Option<$db<F, E, V, H>>>>
482+
impl<F, E, V, H, S> Resolver for Arc<AsyncRwLock<Option<$db<F, E, V, H, S>>>>
480483
where
481484
F: Family,
482485
E: crate::Context,
483486
V: $val_bound + Send + Sync + 'static,
484487
H: Hasher,
488+
S: Strategy,
485489
{
486490
type Family = F;
487491
type Digest = H::Digest;
@@ -521,7 +525,7 @@ macro_rules! impl_compact_resolver_keyless {
521525
// translator parameters carried by immutable variants.
522526
macro_rules! impl_compact_resolver_immutable {
523527
($db:ident, $op:ident, $val_bound:ident, $key_bound:path) => {
524-
impl<F, E, K, V, H, T> Resolver for Arc<$db<F, E, K, V, H, T>>
528+
impl<F, E, K, V, H, T, S> Resolver for Arc<$db<F, E, K, V, H, T, S>>
525529
where
526530
F: Family,
527531
E: crate::Context,
@@ -530,6 +534,7 @@ macro_rules! impl_compact_resolver_immutable {
530534
H: Hasher,
531535
T: Translator + Send + Sync + 'static,
532536
T::Key: Send + Sync,
537+
S: Strategy,
533538
{
534539
type Family = F;
535540
type Digest = H::Digest;
@@ -561,7 +566,7 @@ macro_rules! impl_compact_resolver_immutable {
561566
}
562567
}
563568

564-
impl<F, E, K, V, H, T> Resolver for Arc<AsyncRwLock<$db<F, E, K, V, H, T>>>
569+
impl<F, E, K, V, H, T, S> Resolver for Arc<AsyncRwLock<$db<F, E, K, V, H, T, S>>>
565570
where
566571
F: Family,
567572
E: crate::Context,
@@ -570,6 +575,7 @@ macro_rules! impl_compact_resolver_immutable {
570575
H: Hasher,
571576
T: Translator + Send + Sync + 'static,
572577
T::Key: Send + Sync,
578+
S: Strategy,
573579
{
574580
type Family = F;
575581
type Digest = H::Digest;
@@ -602,7 +608,7 @@ macro_rules! impl_compact_resolver_immutable {
602608
}
603609
}
604610

605-
impl<F, E, K, V, H, T> Resolver for Arc<AsyncRwLock<Option<$db<F, E, K, V, H, T>>>>
611+
impl<F, E, K, V, H, T, S> Resolver for Arc<AsyncRwLock<Option<$db<F, E, K, V, H, T, S>>>>
606612
where
607613
F: Family,
608614
E: crate::Context,
@@ -611,6 +617,7 @@ macro_rules! impl_compact_resolver_immutable {
611617
H: Hasher,
612618
T: Translator + Send + Sync + 'static,
613619
T::Key: Send + Sync,
620+
S: Strategy,
614621
{
615622
type Family = F;
616623
type Digest = H::Digest;
@@ -650,14 +657,15 @@ macro_rules! impl_compact_resolver_immutable {
650657
// is just a target check over the current witness rather than reconstructing anything from history.
651658
macro_rules! impl_compact_resolver_compact_keyless {
652659
($db:ident, $op:ident) => {
653-
impl<F, E, V, H, C> Resolver for Arc<$db<F, E, V, H, C>>
660+
impl<F, E, V, H, C, S> Resolver for Arc<$db<F, E, V, H, C, S>>
654661
where
655662
F: Family,
656663
E: crate::Context,
657664
V: ValueEncoding + Send + Sync + 'static,
658665
H: Hasher,
659666
$op<F, V>: Encode + Read<Cfg = C>,
660667
C: Clone + Send + Sync + 'static,
668+
S: Strategy,
661669
{
662670
type Family = F;
663671
type Digest = H::Digest;
@@ -672,14 +680,15 @@ macro_rules! impl_compact_resolver_compact_keyless {
672680
}
673681
}
674682

675-
impl<F, E, V, H, C> Resolver for Arc<AsyncRwLock<$db<F, E, V, H, C>>>
683+
impl<F, E, V, H, C, S> Resolver for Arc<AsyncRwLock<$db<F, E, V, H, C, S>>>
676684
where
677685
F: Family,
678686
E: crate::Context,
679687
V: ValueEncoding + Send + Sync + 'static,
680688
H: Hasher,
681689
$op<F, V>: Encode + Read<Cfg = C>,
682690
C: Clone + Send + Sync + 'static,
691+
S: Strategy,
683692
{
684693
type Family = F;
685694
type Digest = H::Digest;
@@ -695,14 +704,15 @@ macro_rules! impl_compact_resolver_compact_keyless {
695704
}
696705
}
697706

698-
impl<F, E, V, H, C> Resolver for Arc<AsyncRwLock<Option<$db<F, E, V, H, C>>>>
707+
impl<F, E, V, H, C, S> Resolver for Arc<AsyncRwLock<Option<$db<F, E, V, H, C, S>>>>
699708
where
700709
F: Family,
701710
E: crate::Context,
702711
V: ValueEncoding + Send + Sync + 'static,
703712
H: Hasher,
704713
$op<F, V>: Encode + Read<Cfg = C>,
705714
C: Clone + Send + Sync + 'static,
715+
S: Strategy,
706716
{
707717
type Family = F;
708718
type Digest = H::Digest;
@@ -725,7 +735,7 @@ macro_rules! impl_compact_resolver_compact_keyless {
725735
// persisted witness directly instead of rebuilding it from a full operation log.
726736
macro_rules! impl_compact_resolver_compact_immutable {
727737
($db:ident, $op:ident) => {
728-
impl<F, E, K, V, H, C> Resolver for Arc<$db<F, E, K, V, H, C>>
738+
impl<F, E, K, V, H, C, S> Resolver for Arc<$db<F, E, K, V, H, C, S>>
729739
where
730740
F: Family,
731741
E: crate::Context,
@@ -734,6 +744,7 @@ macro_rules! impl_compact_resolver_compact_immutable {
734744
H: Hasher,
735745
$op<F, K, V>: Encode + Read<Cfg = C>,
736746
C: Clone + Send + Sync + 'static,
747+
S: Strategy,
737748
{
738749
type Family = F;
739750
type Digest = H::Digest;
@@ -748,7 +759,7 @@ macro_rules! impl_compact_resolver_compact_immutable {
748759
}
749760
}
750761

751-
impl<F, E, K, V, H, C> Resolver for Arc<AsyncRwLock<$db<F, E, K, V, H, C>>>
762+
impl<F, E, K, V, H, C, S> Resolver for Arc<AsyncRwLock<$db<F, E, K, V, H, C, S>>>
752763
where
753764
F: Family,
754765
E: crate::Context,
@@ -757,6 +768,7 @@ macro_rules! impl_compact_resolver_compact_immutable {
757768
H: Hasher,
758769
$op<F, K, V>: Encode + Read<Cfg = C>,
759770
C: Clone + Send + Sync + 'static,
771+
S: Strategy,
760772
{
761773
type Family = F;
762774
type Digest = H::Digest;
@@ -772,7 +784,7 @@ macro_rules! impl_compact_resolver_compact_immutable {
772784
}
773785
}
774786

775-
impl<F, E, K, V, H, C> Resolver for Arc<AsyncRwLock<Option<$db<F, E, K, V, H, C>>>>
787+
impl<F, E, K, V, H, C, S> Resolver for Arc<AsyncRwLock<Option<$db<F, E, K, V, H, C, S>>>>
776788
where
777789
F: Family,
778790
E: crate::Context,
@@ -781,6 +793,7 @@ macro_rules! impl_compact_resolver_compact_immutable {
781793
H: Hasher,
782794
$op<F, K, V>: Encode + Read<Cfg = C>,
783795
C: Clone + Send + Sync + 'static,
796+
S: Strategy,
784797
{
785798
type Family = F;
786799
type Digest = H::Digest;
@@ -811,8 +824,63 @@ impl_compact_resolver_immutable!(ImmutableVariableDb, ImmutableVariableOp, Varia
811824
mod tests {
812825
use super::Target;
813826
use crate::merkle::mmr;
814-
use commonware_codec::{DecodeExt as _, Encode as _};
827+
use commonware_codec::{DecodeExt as _, Encode as _, RangeCfg};
815828
use commonware_cryptography::{sha256::Digest, Hasher as _};
829+
use commonware_parallel::Rayon;
830+
use commonware_runtime::deterministic;
831+
use commonware_utils::sync::AsyncRwLock;
832+
use std::sync::Arc;
833+
834+
macro_rules! assert_resolver_variants {
835+
($db:ty) => {
836+
assert_resolver::<Arc<$db>>();
837+
assert_resolver::<Arc<AsyncRwLock<$db>>>();
838+
assert_resolver::<Arc<AsyncRwLock<Option<$db>>>>();
839+
};
840+
}
841+
842+
fn assert_resolver<R: super::Resolver>() {}
843+
844+
#[test]
845+
fn test_all_compact_qmdb_variants_implement_strategy_resolvers() {
846+
type KeylessFixedCompactDb = crate::qmdb::keyless::fixed::CompactDb<
847+
mmr::Family,
848+
deterministic::Context,
849+
Digest,
850+
commonware_cryptography::Sha256,
851+
Rayon,
852+
>;
853+
type KeylessVariableCompactDb = crate::qmdb::keyless::variable::CompactDb<
854+
mmr::Family,
855+
deterministic::Context,
856+
Vec<u8>,
857+
commonware_cryptography::Sha256,
858+
(RangeCfg<usize>, ()),
859+
Rayon,
860+
>;
861+
type ImmutableFixedCompactDb = crate::qmdb::immutable::fixed::CompactDb<
862+
mmr::Family,
863+
deterministic::Context,
864+
Digest,
865+
Digest,
866+
commonware_cryptography::Sha256,
867+
Rayon,
868+
>;
869+
type ImmutableVariableCompactDb = crate::qmdb::immutable::variable::CompactDb<
870+
mmr::Family,
871+
deterministic::Context,
872+
Digest,
873+
Vec<u8>,
874+
commonware_cryptography::Sha256,
875+
((), (RangeCfg<usize>, ())),
876+
Rayon,
877+
>;
878+
879+
assert_resolver_variants!(KeylessFixedCompactDb);
880+
assert_resolver_variants!(KeylessVariableCompactDb);
881+
assert_resolver_variants!(ImmutableFixedCompactDb);
882+
assert_resolver_variants!(ImmutableVariableCompactDb);
883+
}
816884

817885
#[test]
818886
fn test_target_decode_rejects_zero_leaf_count() {

0 commit comments

Comments
 (0)