Successfully compiling

This commit is contained in:
Luke Parker
2022-10-16 07:30:11 -04:00
parent f79321233d
commit 77ba1c00e2
6 changed files with 311 additions and 172 deletions

View File

@@ -1,7 +1,10 @@
use core::{hash::Hash, fmt::Debug};
use std::sync::Arc;
pub trait ValidatorId: Clone + Copy + PartialEq + Eq + Hash + Debug {}
impl<V: Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
use crate::Message;
pub trait ValidatorId: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug {}
impl<V: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
// Type aliases which are distinct according to the type system
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
@@ -17,23 +20,42 @@ pub enum BlockError {
Temporal,
}
pub trait Block: Clone + PartialEq {
type Id: Copy + Clone + PartialEq;
pub trait Block: Send + Sync + Clone + PartialEq + Debug {
type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
fn id(&self) -> Self::Id;
}
pub trait Network {
pub trait Weights: Send + Sync {
type ValidatorId: ValidatorId;
type Block: Block;
fn total_weight(&self) -> u64;
fn weight(&self, validator: Self::ValidatorId) -> u64;
fn threshold(&self) -> u64 {
((self.total_weight() * 2) / 3) + 1
}
fn fault_thresold(&self) -> u64 {
(self.total_weight() - self.threshold()) + 1
}
/// Weighted round robin function.
fn proposer(&self, number: BlockNumber, round: Round) -> Self::ValidatorId;
fn validate(&mut self, block: Self::Block) -> Result<(), BlockError>;
}
#[async_trait::async_trait]
pub trait Network: Send + Sync {
type ValidatorId: ValidatorId;
type Weights: Weights<ValidatorId = Self::ValidatorId>;
type Block: Block;
fn weights(&self) -> Arc<Self::Weights>;
async fn broadcast(&mut self, msg: Message<Self::ValidatorId, Self::Block>);
// TODO: Should this take a verifiable reason?
async fn slash(&mut self, validator: Self::ValidatorId);
fn validate(&mut self, block: &Self::Block) -> Result<(), BlockError>;
// Add a block and return the proposal for the next one
fn add_block(&mut self, block: Self::Block) -> Self::Block;
}