Skip to content

Commit f09756c

Browse files
committed
refactor(db): reorganize traits and impls
1 parent 77b3e3e commit f09756c

File tree

9 files changed

+1225
-1126
lines changed

9 files changed

+1225
-1126
lines changed

crates/proof/db/src/lib.rs

Lines changed: 26 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,28 @@
1-
//! Sled-based proof storage.
2-
3-
use std::fmt::Debug;
4-
5-
use strata_asm_proof_types::{AsmProof, L1Range, MohoProof, ProofId, RemoteProofId};
6-
use strata_identifiers::L1BlockCommitment;
7-
use zkaleido::RemoteProofStatus;
8-
1+
//! Persistence layer for ASM and Moho proofs.
2+
//!
3+
//! This crate defines three storage traits that together cover the full
4+
//! lifecycle of a proof — from submission to a remote prover, through status
5+
//! tracking, to final local storage:
6+
//!
7+
//! - [`ProofDb`] — stores and retrieves finalised ASM step proofs and Moho recursive proofs, keyed
8+
//! by their L1 block range or commitment.
9+
//! - [`RemoteProofMappingDb`] — maintains a bidirectional mapping between local
10+
//! [`ProofId`](strata_asm_proof_types::ProofId)s and opaque
11+
//! [`RemoteProofId`](strata_asm_proof_types::RemoteProofId)s assigned by the remote prover
12+
//! service.
13+
//! - [`RemoteProofStatusDb`] — tracks the execution status of in-flight remote proof jobs until
14+
//! their results are retrieved and stored locally.
15+
//!
16+
//! A sled-backed implementation ([`SledProofDb`]) is provided out of the box.
17+
18+
mod proof_db;
19+
mod remote_mapping;
20+
mod remote_status;
921
mod sled;
1022

11-
pub use self::sled::{RemoteProofMappingError, RemoteProofStatusError, SledProofDb};
12-
13-
/// Persistence interface for proof storage.
14-
pub trait ProofDb {
15-
/// The error type returned by the database operations.
16-
type Error: Debug;
17-
18-
/// Stores an ASM step proof for the given L1 range.
19-
fn store_asm_proof(
20-
&self,
21-
range: L1Range,
22-
proof: AsmProof,
23-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
24-
25-
/// Retrieves an ASM step proof for the given L1 range, if one exists.
26-
fn get_asm_proof(
27-
&self,
28-
range: L1Range,
29-
) -> impl Future<Output = Result<Option<AsmProof>, Self::Error>> + Send;
30-
31-
/// Stores a Moho recursive proof anchored at the given L1 block commitment.
32-
fn store_moho_proof(
33-
&self,
34-
l1ref: L1BlockCommitment,
35-
proof: MohoProof,
36-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
37-
38-
/// Retrieves a Moho proof for the given L1 block commitment, if one exists.
39-
fn get_moho_proof(
40-
&self,
41-
l1ref: L1BlockCommitment,
42-
) -> impl Future<Output = Result<Option<MohoProof>, Self::Error>> + Send;
43-
44-
/// Retrieves the latest (highest height) Moho proof and its L1 block commitment.
45-
///
46-
/// Returns `None` if no Moho proofs have been stored yet.
47-
///
48-
/// NOTE: Multiple proofs can exist at the same height (e.g. due to reorgs).
49-
/// In that case, the returned entry is determined by the underlying key
50-
/// ordering (height, then blkid bytes), which may be arbitrary. Callers that
51-
/// need the proof for a specific canonical block should use
52-
/// [`get_moho_proof`](Self::get_moho_proof) with the exact commitment.
53-
fn get_latest_moho_proof(
54-
&self,
55-
) -> impl Future<Output = Result<Option<(L1BlockCommitment, MohoProof)>, Self::Error>> + Send;
56-
57-
/// Prunes all proofs (both ASM and Moho) for blocks before the given height.
58-
///
59-
/// Deletes all entries with height strictly less than `before_height`.
60-
fn prune(&self, before_height: u32) -> impl Future<Output = Result<(), Self::Error>> + Send;
61-
}
62-
63-
/// Persistent bidirectional mapping between local [`ProofId`]s and
64-
/// [`RemoteProofId`]s assigned by the remote prover service.
65-
///
66-
/// Used to prevent duplicate proof submissions and to recover the association
67-
/// between local and remote identifiers after restarts.
68-
pub trait RemoteProofMappingDb {
69-
/// The error type returned by database operations.
70-
type Error: Debug;
71-
72-
/// Returns the remote proof ID associated with the given local proof ID, if one exists.
73-
fn get_remote_proof_id(
74-
&self,
75-
id: ProofId,
76-
) -> impl Future<Output = Result<Option<RemoteProofId>, Self::Error>> + Send;
77-
78-
/// Returns the local proof ID associated with the given remote proof ID, if one exists.
79-
fn get_proof_id(
80-
&self,
81-
remote_id: &RemoteProofId,
82-
) -> impl Future<Output = Result<Option<ProofId>, Self::Error>> + Send;
83-
84-
/// Stores a mapping between a local proof ID and a remote proof ID.
85-
///
86-
/// A single [`ProofId`] may be associated with multiple [`RemoteProofId`]s
87-
/// (e.g. when a proof is resubmitted), so calling this with the same
88-
/// `id` but a different `remote_id` is allowed — only the forward lookup
89-
/// (`ProofId → RemoteProofId`) is updated to point to the latest remote ID.
90-
///
91-
/// However, a [`RemoteProofId`] must map to exactly one [`ProofId`].
92-
/// If `remote_id` is already associated with a **different** proof ID,
93-
/// this method returns an error.
94-
fn put_remote_proof_id(
95-
&self,
96-
id: ProofId,
97-
remote_id: RemoteProofId,
98-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
99-
}
100-
101-
/// Persistent store for the execution status of remote proof jobs.
102-
///
103-
/// Tracks only proofs whose results have **not yet been retrieved and stored
104-
/// locally**. Once a proof is stored via [`ProofDb`], the corresponding status
105-
/// entry should be removed.
106-
pub trait RemoteProofStatusDb {
107-
/// The error type returned by database operations.
108-
type Error: Debug;
109-
110-
/// Inserts a new status entry for the given remote proof ID.
111-
///
112-
/// Returns an error if an entry already exists for this ID.
113-
fn put_status(
114-
&self,
115-
remote_id: &RemoteProofId,
116-
status: RemoteProofStatus,
117-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
118-
119-
/// Updates the status of an existing remote proof entry.
120-
///
121-
/// Returns an error if no entry exists for this ID.
122-
fn update_status(
123-
&self,
124-
remote_id: &RemoteProofId,
125-
status: RemoteProofStatus,
126-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
127-
128-
/// Returns the current status of the given remote proof, if tracked.
129-
fn get_status(
130-
&self,
131-
remote_id: &RemoteProofId,
132-
) -> impl Future<Output = Result<Option<RemoteProofStatus>, Self::Error>> + Send;
133-
134-
/// Returns all remote proofs that are currently active (i.e. `Requested` or `InProgress`).
135-
fn get_all_in_progress(
136-
&self,
137-
) -> impl Future<Output = Result<Vec<(RemoteProofId, RemoteProofStatus)>, Self::Error>> + Send;
138-
139-
/// Removes the status entry for the given remote proof ID.
140-
fn remove(
141-
&self,
142-
remote_id: &RemoteProofId,
143-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
144-
}
23+
pub use self::{
24+
proof_db::ProofDb,
25+
remote_mapping::RemoteProofMappingDb,
26+
remote_status::RemoteProofStatusDb,
27+
sled::{RemoteProofMappingError, RemoteProofStatusError, SledProofDb},
28+
};

crates/proof/db/src/proof_db.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! Storage trait for finalised ASM step proofs and Moho recursive proofs.
2+
//!
3+
//! Proofs are keyed by L1 block range (ASM) or L1 block commitment (Moho) and
4+
//! support height-based pruning to reclaim space for old entries.
5+
6+
use std::fmt::Debug;
7+
8+
use strata_asm_proof_types::{AsmProof, L1Range, MohoProof};
9+
use strata_identifiers::L1BlockCommitment;
10+
11+
/// Persistence interface for proof storage.
12+
pub trait ProofDb {
13+
/// The error type returned by the database operations.
14+
type Error: Debug;
15+
16+
/// Stores an ASM step proof for the given L1 range.
17+
fn store_asm_proof(
18+
&self,
19+
range: L1Range,
20+
proof: AsmProof,
21+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
22+
23+
/// Retrieves an ASM step proof for the given L1 range, if one exists.
24+
fn get_asm_proof(
25+
&self,
26+
range: L1Range,
27+
) -> impl Future<Output = Result<Option<AsmProof>, Self::Error>> + Send;
28+
29+
/// Stores a Moho recursive proof anchored at the given L1 block commitment.
30+
fn store_moho_proof(
31+
&self,
32+
l1ref: L1BlockCommitment,
33+
proof: MohoProof,
34+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
35+
36+
/// Retrieves a Moho proof for the given L1 block commitment, if one exists.
37+
fn get_moho_proof(
38+
&self,
39+
l1ref: L1BlockCommitment,
40+
) -> impl Future<Output = Result<Option<MohoProof>, Self::Error>> + Send;
41+
42+
/// Retrieves the latest (highest height) Moho proof and its L1 block commitment.
43+
///
44+
/// Returns `None` if no Moho proofs have been stored yet.
45+
///
46+
/// NOTE: Multiple proofs can exist at the same height (e.g. due to reorgs).
47+
/// In that case, the returned entry is determined by the underlying key
48+
/// ordering (height, then blkid bytes), which may be arbitrary. Callers that
49+
/// need the proof for a specific canonical block should use
50+
/// [`get_moho_proof`](Self::get_moho_proof) with the exact commitment.
51+
fn get_latest_moho_proof(
52+
&self,
53+
) -> impl Future<Output = Result<Option<(L1BlockCommitment, MohoProof)>, Self::Error>> + Send;
54+
55+
/// Prunes all proofs (both ASM and Moho) for blocks before the given height.
56+
///
57+
/// Deletes all entries with height strictly less than `before_height`.
58+
fn prune(&self, before_height: u32) -> impl Future<Output = Result<(), Self::Error>> + Send;
59+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Bidirectional mapping between local proof identifiers and remote prover
2+
//! identifiers.
3+
//!
4+
//! This mapping is used to prevent duplicate proof submissions and to recover
5+
//! the association between local and remote identifiers after restarts.
6+
7+
use std::fmt::Debug;
8+
9+
use strata_asm_proof_types::{ProofId, RemoteProofId};
10+
11+
/// Persistent bidirectional mapping between local [`ProofId`]s and
12+
/// [`RemoteProofId`]s assigned by the remote prover service.
13+
///
14+
/// Used to prevent duplicate proof submissions and to recover the association
15+
/// between local and remote identifiers after restarts.
16+
pub trait RemoteProofMappingDb {
17+
/// The error type returned by database operations.
18+
type Error: Debug;
19+
20+
/// Returns the remote proof ID associated with the given local proof ID, if one exists.
21+
fn get_remote_proof_id(
22+
&self,
23+
id: ProofId,
24+
) -> impl Future<Output = Result<Option<RemoteProofId>, Self::Error>> + Send;
25+
26+
/// Returns the local proof ID associated with the given remote proof ID, if one exists.
27+
fn get_proof_id(
28+
&self,
29+
remote_id: &RemoteProofId,
30+
) -> impl Future<Output = Result<Option<ProofId>, Self::Error>> + Send;
31+
32+
/// Stores a mapping between a local proof ID and a remote proof ID.
33+
///
34+
/// A single [`ProofId`] may be associated with multiple [`RemoteProofId`]s
35+
/// (e.g. when a proof is resubmitted), so calling this with the same
36+
/// `id` but a different `remote_id` is allowed — only the forward lookup
37+
/// (`ProofId → RemoteProofId`) is updated to point to the latest remote ID.
38+
///
39+
/// However, a [`RemoteProofId`] must map to exactly one [`ProofId`].
40+
/// If `remote_id` is already associated with a **different** proof ID,
41+
/// this method returns an error.
42+
fn put_remote_proof_id(
43+
&self,
44+
id: ProofId,
45+
remote_id: RemoteProofId,
46+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Status tracking for in-flight remote proof jobs.
2+
//!
3+
//! Entries are created when a proof is submitted to the remote prover and
4+
//! removed once the result has been retrieved and stored locally via
5+
//! [`ProofDb`](crate::ProofDb).
6+
7+
use std::fmt::Debug;
8+
9+
use strata_asm_proof_types::RemoteProofId;
10+
use zkaleido::RemoteProofStatus;
11+
12+
/// Persistent store for the execution status of remote proof jobs.
13+
///
14+
/// Tracks only proofs whose results have **not yet been retrieved and stored
15+
/// locally**. Once a proof is stored via [`ProofDb`](crate::ProofDb), the corresponding status
16+
/// entry should be removed.
17+
pub trait RemoteProofStatusDb {
18+
/// The error type returned by database operations.
19+
type Error: Debug;
20+
21+
/// Inserts a new status entry for the given remote proof ID.
22+
///
23+
/// Returns an error if an entry already exists for this ID.
24+
fn put_status(
25+
&self,
26+
remote_id: &RemoteProofId,
27+
status: RemoteProofStatus,
28+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
29+
30+
/// Updates the status of an existing remote proof entry.
31+
///
32+
/// Returns an error if no entry exists for this ID.
33+
fn update_status(
34+
&self,
35+
remote_id: &RemoteProofId,
36+
status: RemoteProofStatus,
37+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
38+
39+
/// Returns the current status of the given remote proof, if tracked.
40+
fn get_status(
41+
&self,
42+
remote_id: &RemoteProofId,
43+
) -> impl Future<Output = Result<Option<RemoteProofStatus>, Self::Error>> + Send;
44+
45+
/// Returns all remote proofs that are currently active (i.e. `Requested` or `InProgress`).
46+
fn get_all_in_progress(
47+
&self,
48+
) -> impl Future<Output = Result<Vec<(RemoteProofId, RemoteProofStatus)>, Self::Error>> + Send;
49+
50+
/// Removes the status entry for the given remote proof ID.
51+
fn remove(
52+
&self,
53+
remote_id: &RemoteProofId,
54+
) -> impl Future<Output = Result<(), Self::Error>> + Send;
55+
}

0 commit comments

Comments
 (0)