use thiserror::Error;
use crate::components::consensus::traits::Context;
use super::validators::ValidatorIndex;
#[derive(Debug, Error, PartialEq)]
pub(crate) enum EndorsementError {
#[error("The creator is not a validator.")]
Creator,
#[error("The signature is invalid.")]
Signature,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Endorsement<C: Context> {
vote: C::Hash,
creator: ValidatorIndex,
}
impl<C: Context> Endorsement<C> {
pub(crate) fn new(vhash: C::Hash, creator: ValidatorIndex) -> Self {
Endorsement {
vote: vhash,
creator,
}
}
pub(crate) fn hash(&self) -> C::Hash {
<C as Context>::hash(
&bincode::serialize(&(self.vote, self.creator)).expect("serialize endorsement"),
)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SignedEndorsement<C: Context> {
endorsement: Endorsement<C>,
signature: C::Signature,
}
impl<C: Context> SignedEndorsement<C> {
pub fn new(endorsement: Endorsement<C>, signature: C::Signature) -> Self {
SignedEndorsement {
endorsement,
signature,
}
}
pub(crate) fn vote(&self) -> &C::Hash {
&self.endorsement.vote
}
pub(crate) fn validator_idx(&self) -> ValidatorIndex {
self.endorsement.creator
}
pub(crate) fn signature(&self) -> &C::Signature {
&self.signature
}
}