Skip to main content

miden_client/rpc/domain/
sync.rs

1use miden_protocol::block::{BlockHeader, BlockNumber};
2use miden_protocol::crypto::merkle::mmr::MmrDelta;
3
4use crate::rpc::domain::MissingFieldHelper;
5use crate::rpc::{RpcError, generated as proto};
6
7// SYNC TARGET
8// ================================================================================================
9
10/// Finality level to sync the chain MMR to.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum SyncTarget {
13    /// Sync up to the latest committed block (the chain tip).
14    CommittedChainTip,
15    /// Sync up to the latest proven block, which may be behind the committed tip.
16    ProvenChainTip,
17}
18
19impl From<SyncTarget> for proto::rpc::FinalityLevel {
20    fn from(target: SyncTarget) -> Self {
21        match target {
22            SyncTarget::CommittedChainTip => Self::Committed,
23            SyncTarget::ProvenChainTip => Self::Proven,
24        }
25    }
26}
27
28// CHAIN MMR INFO
29// ================================================================================================
30
31/// Represents the result of a `SyncChainMmr` RPC call, with fields converted into domain types.
32pub struct ChainMmrInfo {
33    /// The block number from which the delta starts (inclusive).
34    pub block_from: BlockNumber,
35    /// The block number up to which the delta covers (inclusive).
36    pub block_to: BlockNumber,
37    /// The MMR delta for the requested block range.
38    pub mmr_delta: MmrDelta,
39    /// The block header at `block_to`.
40    pub block_header: BlockHeader,
41}
42
43impl TryFrom<proto::rpc::SyncChainMmrResponse> for ChainMmrInfo {
44    type Error = RpcError;
45
46    fn try_from(value: proto::rpc::SyncChainMmrResponse) -> Result<Self, Self::Error> {
47        let block_range = value
48            .block_range
49            .ok_or(proto::rpc::SyncChainMmrResponse::missing_field(stringify!(block_range)))?;
50
51        let mmr_delta = value
52            .mmr_delta
53            .ok_or(proto::rpc::SyncChainMmrResponse::missing_field(stringify!(mmr_delta)))?
54            .try_into()?;
55
56        let block_header = value
57            .block_header
58            .ok_or(proto::rpc::SyncChainMmrResponse::missing_field(stringify!(block_header)))?
59            .try_into()?;
60
61        Ok(Self {
62            block_from: block_range.block_from.into(),
63            block_to: block_range.block_to.into(),
64            mmr_delta,
65            block_header,
66        })
67    }
68}