sui_sdk_types/crypto/
validator.rs

1use super::Bls12381PublicKey;
2use super::Bls12381Signature;
3use crate::checkpoint::EpochId;
4use crate::checkpoint::StakeUnit;
5
6/// The Validator Set for a particular epoch.
7///
8/// # BCS
9///
10/// The BCS serialized form for this type is defined by the following ABNF:
11///
12/// ```text
13/// validator-committee = u64 ; epoch
14///                       (vector validator-committee-member)
15/// ```
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[cfg_attr(
18    feature = "serde",
19    derive(serde_derive::Serialize, serde_derive::Deserialize)
20)]
21#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
22pub struct ValidatorCommittee {
23    pub epoch: EpochId,
24    pub members: Vec<ValidatorCommitteeMember>,
25}
26
27/// A member of a Validator Committee
28///
29/// # BCS
30///
31/// The BCS serialized form for this type is defined by the following ABNF:
32///
33/// ```text
34/// validator-committee-member = bls-public-key
35///                              u64 ; stake
36/// ```
37#[derive(Clone, Debug, PartialEq, Eq)]
38#[cfg_attr(
39    feature = "serde",
40    derive(serde_derive::Serialize, serde_derive::Deserialize)
41)]
42#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
43pub struct ValidatorCommitteeMember {
44    pub public_key: Bls12381PublicKey,
45    pub stake: StakeUnit,
46}
47
48/// An aggregated signature from multiple Validators.
49///
50/// # BCS
51///
52/// The BCS serialized form for this type is defined by the following ABNF:
53///
54/// ```text
55/// validator-aggregated-signature = u64               ; epoch
56///                                  bls-signature
57///                                  roaring-bitmap
58/// roaring-bitmap = bytes  ; where the contents of the bytes are valid
59///                         ; according to the serialized spec for
60///                         ; roaring bitmaps
61/// ```
62///
63/// See [here](https://github.com/RoaringBitmap/RoaringFormatSpec) for the specification for the
64/// serialized format of RoaringBitmaps.
65#[derive(Clone, Debug, PartialEq)]
66#[cfg_attr(
67    feature = "serde",
68    derive(serde_derive::Serialize, serde_derive::Deserialize)
69)]
70#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
71pub struct ValidatorAggregatedSignature {
72    pub epoch: EpochId,
73    pub signature: Bls12381Signature,
74    #[cfg_attr(feature = "serde", serde(with = "RoaringBitMapSerialization"))]
75    #[cfg_attr(
76        feature = "proptest",
77        strategy(proptest::strategy::Just(roaring::RoaringBitmap::default()))
78    )]
79    pub bitmap: roaring::RoaringBitmap,
80}
81
82#[cfg(feature = "serde")]
83type RoaringBitMapSerialization = ::serde_with::As<
84    ::serde_with::IfIsHumanReadable<
85        crate::_serde::Base64RoaringBitmap,
86        crate::_serde::BinaryRoaringBitmap,
87    >,
88>;
89
90/// A signature from a Validator
91///
92/// # BCS
93///
94/// The BCS serialized form for this type is defined by the following ABNF:
95///
96/// ```text
97/// validator-signature = u64               ; epoch
98///                       bls-public-key
99///                       bls-signature
100/// ```
101#[derive(Clone, Debug, PartialEq, Eq)]
102#[cfg_attr(
103    feature = "serde",
104    derive(serde_derive::Serialize, serde_derive::Deserialize)
105)]
106#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
107pub struct ValidatorSignature {
108    pub epoch: EpochId,
109    pub public_key: Bls12381PublicKey,
110    pub signature: Bls12381Signature,
111}
112
113#[cfg(test)]
114mod test {
115    use super::*;
116
117    #[cfg(target_arch = "wasm32")]
118    use wasm_bindgen_test::wasm_bindgen_test as test;
119
120    #[cfg(feature = "serde")]
121    #[test]
122    fn aggregated_signature_fixture() {
123        use base64ct::Base64;
124        use base64ct::Encoding;
125
126        const FIXTURE: &str = "CgAAAAAAAACZrBcXiqa0ttztfwrBxKzQRzIRnZhbmsQV7tqNXwiZQrRC+dVDbdua1Ety9uy2pCUSOjAAAAEAAAAAAAAAEAAAAAAA";
127        let bcs = Base64::decode_vec(FIXTURE).unwrap();
128
129        let signature: ValidatorAggregatedSignature = bcs::from_bytes(&bcs).unwrap();
130        let bytes = bcs::to_bytes(&signature).unwrap();
131        assert_eq!(bcs, bytes);
132    }
133}