sos_protocol/bindings/
scan.rs

1include!(concat!(env!("OUT_DIR"), "/scan.rs"));
2
3use crate::{Error, ProtoBinding, Result};
4use sos_core::{commit::CommitProof, events::EventLogType};
5
6/// Request commit proofs from an event log.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct ScanRequest {
9    /// Type of event log to load commit hashes from.
10    pub log_type: EventLogType,
11    /// Number of proofs to fetch.
12    ///
13    /// Server implementations should restrict this to
14    /// a sensible amount; the default server implementation
15    /// imposes a limit of 256 proofs.
16    pub limit: u16,
17    /// Offset from a previous scan used as a hint to
18    /// continue scanning.
19    pub offset: u64,
20}
21
22impl ProtoBinding for ScanRequest {
23    type Inner = WireScanRequest;
24}
25
26impl TryFrom<WireScanRequest> for ScanRequest {
27    type Error = Error;
28
29    fn try_from(value: WireScanRequest) -> Result<Self> {
30        Ok(Self {
31            log_type: value.log_type.unwrap().try_into()?,
32            limit: value.limit.unwrap() as u16,
33            offset: value.offset,
34        })
35    }
36}
37
38impl From<ScanRequest> for WireScanRequest {
39    fn from(value: ScanRequest) -> WireScanRequest {
40        Self {
41            log_type: Some(value.log_type.into()),
42            limit: Some(value.limit as u32),
43            offset: value.offset,
44        }
45    }
46}
47
48/// Commit proofs from an event log.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct ScanResponse {
51    /// Proof for the first item in the event log.
52    pub first_proof: Option<CommitProof>,
53    /// List of commit proofs.
54    ///
55    /// Proofs are listed in the order they
56    /// appear in the event log.
57    pub proofs: Vec<CommitProof>,
58    /// Offset that can be used to continue scanning.
59    pub offset: u64,
60}
61
62impl ProtoBinding for ScanResponse {
63    type Inner = WireScanResponse;
64}
65
66impl TryFrom<WireScanResponse> for ScanResponse {
67    type Error = Error;
68
69    fn try_from(value: WireScanResponse) -> Result<Self> {
70        let first_proof = if let Some(first_proof) = value.first_proof {
71            Some(first_proof.try_into()?)
72        } else {
73            None
74        };
75
76        let mut proofs = Vec::with_capacity(value.proofs.len());
77        for proof in value.proofs {
78            proofs.push(proof.try_into()?);
79        }
80
81        Ok(Self {
82            first_proof,
83            proofs,
84            offset: value.offset,
85        })
86    }
87}
88
89impl From<ScanResponse> for WireScanResponse {
90    fn from(value: ScanResponse) -> WireScanResponse {
91        Self {
92            first_proof: value.first_proof.map(|p| p.into()),
93            proofs: value.proofs.into_iter().map(|p| p.into()).collect(),
94            offset: value.offset,
95        }
96    }
97}