commonware_storage/adb/
mod.rs1use crate::{
14 journal::contiguous::fixed::Journal,
15 mmr::{journaled, Location},
16};
17use commonware_cryptography::Hasher;
18use commonware_runtime::{Clock, Metrics, Storage};
19use thiserror::Error;
20
21pub mod any;
22pub mod current;
23pub mod immutable;
24pub mod keyless;
25pub mod operation;
26pub mod store;
27pub mod sync;
28pub mod verify;
29use tracing::warn;
30pub use verify::{
31 create_multi_proof, create_proof, create_proof_store, create_proof_store_from_digests,
32 digests_required_for_proof, extract_pinned_nodes, verify_multi_proof, verify_proof,
33 verify_proof_and_extract_digests,
34};
35
36#[derive(Error, Debug)]
38pub enum Error {
39 #[error("mmr error: {0}")]
40 Mmr(#[from] crate::mmr::Error),
41
42 #[error("metadata error: {0}")]
43 Metadata(#[from] crate::metadata::Error),
44
45 #[error("journal error: {0}")]
46 Journal(#[from] crate::journal::Error),
47
48 #[error("runtime error: {0}")]
49 Runtime(#[from] commonware_runtime::Error),
50
51 #[error("operation pruned: {0}")]
52 OperationPruned(Location),
53
54 #[error("key not found")]
56 KeyNotFound,
57
58 #[error("key exists")]
60 KeyExists,
61
62 #[error("unexpected data at location: {0}")]
63 UnexpectedData(Location),
64
65 #[error("location out of bounds: {0} >= {1}")]
66 LocationOutOfBounds(Location, Location),
67
68 #[error("prune location {0} beyond last commit {1}")]
69 PruneBeyondCommit(Location, Location),
70
71 #[error("prune location {0} beyond inactivity floor {1}")]
72 PruneBeyondInactivityFloor(Location, Location),
73
74 #[error("uncommitted operations present")]
75 UncommittedOperations,
76}
77
78async fn align_mmr_and_locations<E: Storage + Clock + Metrics, H: Hasher>(
81 mmr: &mut journaled::Mmr<E, H>,
82 locations: &mut Journal<E, u32>,
83) -> Result<u64, Error> {
84 let aligned_size = {
85 let locations_size = locations.size().await;
86 let mmr_leaves = *mmr.leaves();
87 if locations_size > mmr_leaves {
88 warn!(
89 mmr_leaves,
90 locations_size, "rewinding misaligned locations journal"
91 );
92 locations.rewind(mmr_leaves).await?;
93 locations.sync().await?;
94 mmr_leaves
95 } else if mmr_leaves > locations_size {
96 warn!(mmr_leaves, locations_size, "rewinding misaligned mmr");
97 mmr.pop((mmr_leaves - locations_size) as usize).await?;
98 locations_size
99 } else {
100 locations_size }
102 };
103
104 assert_eq!(aligned_size, locations.size().await);
106 assert_eq!(aligned_size, mmr.leaves());
107
108 Ok(aligned_size)
109}