Skip to main content

diem_types/
state_proof.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    epoch_change::EpochChangeProof,
6    ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
7    proof::AccumulatorConsistencyProof,
8};
9#[cfg(any(test, feature = "fuzzing"))]
10use proptest_derive::Arbitrary;
11use serde::{Deserialize, Serialize};
12
13/// A convenience type for the collection of sub-proofs that consistitute a
14/// response to a `get_state_proof` request.
15///
16/// From a `StateProof` response, a client should be able to ratchet their
17/// [`TrustedState`] to the last epoch change LI in the [`EpochChangeProof`]
18/// or the latest [`LedgerInfoWithSignatures`] if the epoch changes get them into
19/// the most recent epoch.
20///
21/// [`TrustedState`]: crate::trusted_state::TrustedState
22#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
23#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
24pub struct StateProof {
25    latest_li_w_sigs: LedgerInfoWithSignatures,
26    epoch_changes: EpochChangeProof,
27    consistency_proof: AccumulatorConsistencyProof,
28}
29
30impl StateProof {
31    pub fn new(
32        latest_li_w_sigs: LedgerInfoWithSignatures,
33        epoch_changes: EpochChangeProof,
34        consistency_proof: AccumulatorConsistencyProof,
35    ) -> Self {
36        Self {
37            latest_li_w_sigs,
38            epoch_changes,
39            consistency_proof,
40        }
41    }
42
43    pub fn into_inner(
44        self,
45    ) -> (
46        LedgerInfoWithSignatures,
47        EpochChangeProof,
48        AccumulatorConsistencyProof,
49    ) {
50        (
51            self.latest_li_w_sigs,
52            self.epoch_changes,
53            self.consistency_proof,
54        )
55    }
56
57    pub fn as_inner(
58        &self,
59    ) -> (
60        &LedgerInfoWithSignatures,
61        &EpochChangeProof,
62        &AccumulatorConsistencyProof,
63    ) {
64        (
65            &self.latest_li_w_sigs,
66            &self.epoch_changes,
67            &self.consistency_proof,
68        )
69    }
70
71    #[inline]
72    pub fn latest_ledger_info(&self) -> &LedgerInfo {
73        self.latest_li_w_sigs.ledger_info()
74    }
75
76    #[inline]
77    pub fn latest_ledger_info_w_sigs(&self) -> &LedgerInfoWithSignatures {
78        &self.latest_li_w_sigs
79    }
80
81    #[inline]
82    pub fn epoch_changes(&self) -> &EpochChangeProof {
83        &self.epoch_changes
84    }
85
86    #[inline]
87    pub fn consistency_proof(&self) -> &AccumulatorConsistencyProof {
88        &self.consistency_proof
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use bcs::test_helpers::assert_canonical_encode_decode;
96    use proptest::prelude::*;
97
98    proptest! {
99        #![proptest_config(ProptestConfig::with_cases(20))]
100
101        #[test]
102        fn test_state_proof_canonical_serialization(proof in any::<StateProof>()) {
103            assert_canonical_encode_decode(proof);
104        }
105    }
106}