1use alloc::vec::Vec;
19use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
20use frame_support::{traits::ConstU32, BoundedVec};
21use scale_info::TypeInfo;
22use sp_runtime::RuntimeDebug;
23
24#[derive(
26 Clone,
27 PartialEq,
28 Eq,
29 PartialOrd,
30 Ord,
31 RuntimeDebug,
32 Encode,
33 Decode,
34 DecodeWithMemTracking,
35 TypeInfo,
36 MaxEncodedLen,
37)]
38pub struct Multihash {
39 pub code: u64,
41 pub digest: BoundedVec<u8, ConstU32<68>>, }
44
45impl Multihash {
46 pub fn size(&self) -> usize {
48 self.digest.len()
49 }
50}
51
52#[derive(
54 Clone,
55 Copy,
56 PartialEq,
57 Eq,
58 PartialOrd,
59 Ord,
60 RuntimeDebug,
61 Encode,
62 Decode,
63 DecodeWithMemTracking,
64 TypeInfo,
65 MaxEncodedLen,
66)]
67pub enum Version {
68 V0,
70 V1,
72}
73
74#[derive(
78 Clone,
79 PartialEq,
80 Eq,
81 PartialOrd,
82 Ord,
83 RuntimeDebug,
84 Encode,
85 Decode,
86 DecodeWithMemTracking,
87 TypeInfo,
88 MaxEncodedLen,
89)]
90pub struct Cid {
91 pub version: Version,
93 pub codec: u64,
95 pub hash: Multihash,
97}
98
99impl Cid {
100 pub fn new_v0(sha2_256_digest: impl Into<Vec<u8>>) -> Self {
102 const DAG_PB: u64 = 0x70;
104 const SHA2_256: u64 = 0x12;
106
107 let digest = sha2_256_digest.into();
108 assert_eq!(digest.len(), 32);
109
110 Self {
111 version: Version::V0,
112 codec: DAG_PB,
113 hash: Multihash { code: SHA2_256, digest: digest.try_into().expect("msg") },
114 }
115 }
116}
117
118#[derive(
120 Copy,
121 Clone,
122 Encode,
123 Decode,
124 DecodeWithMemTracking,
125 Eq,
126 PartialEq,
127 RuntimeDebug,
128 MaxEncodedLen,
129 TypeInfo,
130 Default,
131)]
132pub struct DisbandWitness {
133 #[codec(compact)]
135 pub(super) fellow_members: u32,
136 #[codec(compact)]
138 pub(super) ally_members: u32,
139}
140
141#[cfg(test)]
142impl DisbandWitness {
143 pub(super) fn new(fellow_members: u32, ally_members: u32) -> Self {
145 Self { fellow_members, ally_members }
146 }
147}
148
149impl DisbandWitness {
150 pub(super) fn is_zero(self) -> bool {
151 self == Self::default()
152 }
153}