Skip to main content

miden_node_store/state/writer/
apply_proof.rs

1use anyhow::{Context, ensure};
2use miden_node_tracing::miden_instrument;
3use miden_protocol::block::BlockNumber;
4use miden_protocol::vm::ExecutionProof;
5
6use crate::COMPONENT;
7use crate::state::{ProofNotification, ProofWriter};
8
9impl ProofWriter {
10    /// Saves a block proof, advances the proven-in-sequence tip, and notifies replica subscribers.
11    ///
12    /// # Errors
13    ///
14    /// - If proofs are not applied in strict ascending order (exactly one block past the proven tip)
15    /// - If the proof's corresponding block was not already committed
16    #[miden_instrument(
17        target = COMPONENT,
18        err,
19        fields(
20            block.number = block_num
21        ),
22    )]
23    pub async fn apply_proof(
24        &mut self,
25        block_num: BlockNumber,
26        proof_bytes: Vec<u8>,
27    ) -> anyhow::Result<()> {
28        let expected = self.state.proven_tip().child();
29        ensure!(
30            block_num == expected,
31            "out-of-sequence proof: expected block {expected}, got {block_num}",
32        );
33
34        let committed_tip = self.state.committed_tip();
35        ensure!(
36            block_num <= committed_tip,
37            "proof for uncommitted block {block_num} exceeds committed tip {committed_tip}",
38        );
39
40        verify_block_proof(block_num, &proof_bytes)?;
41
42        self.state.block_store.commit_proof(block_num, &proof_bytes).await?;
43        self.state
44            .proof_cache
45            .push(block_num, ProofNotification::new(block_num, proof_bytes))
46            .expect("proof cache receives sequential block numbers");
47        self.state.proven_tip.advance(block_num);
48        Ok(())
49    }
50}
51
52/// Verifies that `proof_bytes` is a valid [`ExecutionProof`] for the block at `block_num`.
53fn verify_block_proof(_block_num: BlockNumber, proof_bytes: &[u8]) -> anyhow::Result<()> {
54    let _proof = ExecutionProof::read_from_bytes(proof_bytes)
55        .context("failed to deserialize block proof")?;
56
57    // TODO: perform verification.
58    Ok(())
59}