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