2022-10-16 03:29:55 -04:00
|
|
|
use core::{hash::Hash, fmt::Debug};
|
|
|
|
|
|
|
|
|
|
pub trait ValidatorId: Clone + Copy + PartialEq + Eq + Hash + Debug {}
|
|
|
|
|
impl<V: Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
// Type aliases which are distinct according to the type system
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
|
pub struct BlockNumber(pub u32);
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
|
pub struct Round(pub u16);
|
|
|
|
|
|
2022-10-16 03:29:55 -04:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
|
pub enum BlockError {
|
|
|
|
|
// Invalid behavior entirely
|
|
|
|
|
Fatal,
|
|
|
|
|
// Potentially valid behavior dependent on unsynchronized state
|
|
|
|
|
Temporal,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait Block: Clone + PartialEq {
|
|
|
|
|
type Id: Copy + Clone + PartialEq;
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> Self::Id;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
pub trait Network {
|
|
|
|
|
type ValidatorId: ValidatorId;
|
|
|
|
|
type Block: Block;
|
|
|
|
|
|
2022-10-16 03:29:55 -04:00
|
|
|
fn total_weight(&self) -> u64;
|
2022-10-16 03:55:39 -04:00
|
|
|
fn weight(&self, validator: Self::ValidatorId) -> u64;
|
2022-10-16 03:29:55 -04:00
|
|
|
fn threshold(&self) -> u64 {
|
|
|
|
|
((self.total_weight() * 2) / 3) + 1
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
fn proposer(&self, number: BlockNumber, round: Round) -> Self::ValidatorId;
|
|
|
|
|
|
|
|
|
|
fn validate(&mut self, block: Self::Block) -> Result<(), BlockError>;
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|