jam_std_common/
availability.rs1use super::{core_count, hash_encoded, ValSignature};
2use bounded_collections::Get;
3use jam_types::{opaque, CoreIndex, FixedVec, HeaderHash};
4use scale::{ConstEncodedLen, Decode, Encode, MaxEncodedLen};
5
6pub fn bitfield_byte_count() -> usize {
7 (core_count() + 7) as usize / 8
8}
9#[derive(Copy, Clone, Eq, PartialEq, Default, Debug)]
10pub struct BitfieldByteCount;
11impl Get<u32> for BitfieldByteCount {
12 fn get() -> u32 {
13 bitfield_byte_count() as u32
14 }
15}
16
17#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, MaxEncodedLen)]
18pub struct AvailabilityBitfield(FixedVec<u8, BitfieldByteCount>);
19
20impl AvailabilityBitfield {
21 pub fn new() -> Self {
22 Self(FixedVec::default())
23 }
24 pub fn set(&mut self, i: CoreIndex) {
25 self.0[i as usize / 8] |= 1 << (i % 8);
26 }
27 pub fn clear(&mut self, i: CoreIndex) {
28 self.0[i as usize / 8] &= !(1 << (i % 8));
29 }
30 pub fn get(&self, i: CoreIndex) -> bool {
31 self.0[i as usize / 8] & (1 << (i % 8)) != 0
32 }
33
34 pub fn is_all_unset(&self) -> bool {
35 self.0.iter().all(|&b| b == 0)
36 }
37 pub fn count_ones(&self) -> u32 {
38 self.0.iter().map(|&b| b.count_ones()).sum()
39 }
40}
41
42impl ConstEncodedLen for AvailabilityBitfield {}
43
44impl<T: std::borrow::Borrow<[CoreIndex]>> From<T> for AvailabilityBitfield {
45 fn from(value: T) -> Self {
46 let mut r = Self::new();
47 for i in value.borrow() {
48 r.set(*i);
49 }
50 r
51 }
52}
53
54#[derive(Clone, Encode, Decode, Debug, PartialEq, Eq, MaxEncodedLen)]
57pub struct AvailabilityStatement {
58 pub anchor: HeaderHash,
60 pub bitfield: AvailabilityBitfield,
63}
64
65impl ConstEncodedLen for AvailabilityStatement {}
66
67opaque! { pub struct AvailabilityStatementHash(pub [u8; 32]); }
68
69impl AvailabilityStatement {
70 pub fn hash(&self) -> AvailabilityStatementHash {
71 hash_encoded(self).into()
72 }
73}
74
75#[derive(Clone, Encode, Decode, Debug, MaxEncodedLen)]
77pub struct AvailabilityAssurance {
78 pub statement: AvailabilityStatement,
80 pub signature: ValSignature,
82}
83
84impl ConstEncodedLen for AvailabilityAssurance {}
85
86opaque! { pub struct ErasureRoot(pub [u8; 32]); }