//! Types for the `commonware-reshare` example application. use bytes::{Buf, BufMut}; use commonware_codec::{Encode, EncodeSize, Error as CodecError, Read, ReadExt, Write}; use commonware_consensus::{types::Height, Block as ConsensusBlock, Heightable}; use commonware_cryptography::{ bls12381::{dkg::SignedDealerLog, primitives::variant::Variant}, Committable, Digest, Digestible, Hasher, Signer, }; use std::num::NonZeroU32; /// A block in the reshare chain. #[derive(Clone)] pub struct Block where H: Hasher, C: Signer, V: Variant, { /// The parent digest. pub parent: H::Digest, /// The current height. pub height: Height, /// An optional outcome of a dealing operation. pub log: Option>, } impl Block where H: Hasher, C: Signer, V: Variant, { /// Create a new [Block]. pub const fn new( parent: H::Digest, height: Height, log: Option>, ) -> Self { Self { parent, height, log, } } } impl Write for Block where H: Hasher, C: Signer, V: Variant, { fn write(&self, buf: &mut impl BufMut) { self.parent.write(buf); self.height.write(buf); self.log.write(buf); } } impl EncodeSize for Block where H: Hasher, C: Signer, V: Variant, { fn encode_size(&self) -> usize { self.parent.encode_size() + self.height.encode_size() + self.log.encode_size() } } impl Read for Block where H: Hasher, C: Signer, V: Variant, { // The consensus quorum type Cfg = NonZeroU32; fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { Ok(Self { parent: H::Digest::read(buf)?, height: Height::read(buf)?, log: Read::read_cfg(buf, cfg)?, }) } } impl Digestible for Block where H: Hasher, C: Signer, V: Variant, { type Digest = H::Digest; fn digest(&self) -> H::Digest { H::hash(&self.encode()) } } impl Committable for Block where H: Hasher, C: Signer, V: Variant, { type Commitment = H::Digest; fn commitment(&self) -> H::Digest { self.digest() } } impl Heightable for Block where H: Hasher, C: Signer, V: Variant, { fn height(&self) -> Height { self.height } } impl ConsensusBlock for Block where H: Hasher, C: Signer, V: Variant, { fn parent(&self) -> Self::Commitment { self.parent } } /// Returns the genesis block. pub const fn genesis_block() -> Block where H: Hasher, C: Signer, V: Variant, { Block::new( <::Digest as Digest>::EMPTY, Height::zero(), None, ) }