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

13
Cargo.lock generated
View File

@@ -256,6 +256,17 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "async-recursion"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cda8f4bcc10624c4e85bc66b3f452cca98cfa5ca002dc83a16aad2367641bea"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "async-std" name = "async-std"
version = "1.12.0" version = "1.12.0"
@@ -8857,6 +8868,8 @@ dependencies = [
name = "tendermint-machine" name = "tendermint-machine"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-recursion",
"async-trait",
"tokio", "tokio",
] ]

View File

@@ -8,4 +8,6 @@ authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021" edition = "2021"
[dependencies] [dependencies]
tokio = "1" async-recursion = "1.0"
async-trait = "0.1"
tokio = { version = "1", features = ["macros", "rt", "sync"] }

View File

@@ -1,7 +1,10 @@
use core::{hash::Hash, fmt::Debug}; use core::{hash::Hash, fmt::Debug};
use std::sync::Arc;
pub trait ValidatorId: Clone + Copy + PartialEq + Eq + Hash + Debug {} use crate::Message;
impl<V: Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
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 // Type aliases which are distinct according to the type system
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
@@ -17,23 +20,42 @@ pub enum BlockError {
Temporal, Temporal,
} }
pub trait Block: Clone + PartialEq { pub trait Block: Send + Sync + Clone + PartialEq + Debug {
type Id: Copy + Clone + PartialEq; type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
fn id(&self) -> Self::Id; fn id(&self) -> Self::Id;
} }
pub trait Network { pub trait Weights: Send + Sync {
type ValidatorId: ValidatorId; type ValidatorId: ValidatorId;
type Block: Block;
fn total_weight(&self) -> u64; fn total_weight(&self) -> u64;
fn weight(&self, validator: Self::ValidatorId) -> u64; fn weight(&self, validator: Self::ValidatorId) -> u64;
fn threshold(&self) -> u64 { fn threshold(&self) -> u64 {
((self.total_weight() * 2) / 3) + 1 ((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 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;
} }

View File

@@ -1,7 +1,18 @@
use std::{sync::Arc, time::Instant, collections::HashMap};
use tokio::{
task::{JoinHandle, yield_now},
sync::{
RwLock,
mpsc::{self, error::TryRecvError},
},
};
pub mod ext; pub mod ext;
use ext::*; use ext::*;
mod message_log; mod message_log;
use message_log::MessageLog;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum Step { enum Step {
@@ -10,9 +21,9 @@ enum Step {
Precommit, Precommit,
} }
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq, Debug)]
enum Data<B: Block> { enum Data<B: Block> {
Proposal(Option<u32>, B), Proposal(Option<Round>, B),
Prevote(Option<B::Id>), Prevote(Option<B::Id>),
Precommit(Option<B::Id>), Precommit(Option<B::Id>),
} }
@@ -27,8 +38,8 @@ impl<B: Block> Data<B> {
} }
} }
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq, Debug)]
struct Message<V: ValidatorId, B: Block> { pub struct Message<V: ValidatorId, B: Block> {
sender: V, sender: V,
number: BlockNumber, number: BlockNumber,
@@ -38,158 +49,155 @@ struct Message<V: ValidatorId, B: Block> {
} }
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum TendermintError<V: ValidatorId> { pub enum TendermintError<V: ValidatorId> {
Malicious(V), Malicious(V),
Offline(V),
Temporal, Temporal,
} }
/* pub struct TendermintMachine<N: Network> {
use std::collections::HashMap; network: Arc<RwLock<N>>,
weights: Arc<N::Weights>,
proposer: N::ValidatorId,
use tokio::{ number: BlockNumber,
task::{JoinHandle, spawn}, personal_proposal: N::Block,
sync::mpsc,
};
#[derive(Debug)] log: MessageLog<N>,
struct TendermintMachine { round: Round,
proposer: ValidatorId,
personal_proposal: Option<Block>,
number: u32,
log_map: HashMap<u32, HashMap<ValidatorId, HashMap<Step, Data>>>,
precommitted: HashMap<ValidatorId, Hash>,
round: u32,
step: Step, step: Step,
locked: Option<(u32, Block)>,
valid: Option<(u32, Block)>,
timeouts: Arc<RwLock<HashMap<Step, Instant>>>, // TODO: Remove Arc RwLock locked: Option<(Round, N::Block)>,
valid: Option<(Round, N::Block)>,
timeouts: HashMap<Step, Instant>,
} }
#[derive(Debug)] pub struct TendermintHandle<N: Network> {
struct TendermintHandle { // Messages received
block: Arc<RwLock<Option<Block>>>, pub messages: mpsc::Sender<Message<N::ValidatorId, N::Block>>,
messages: mpsc::Sender<Message>, // Async task executing the machine
broadcast: mpsc::Receiver<Message>, pub handle: JoinHandle<()>,
handle: JoinHandle<()>,
} }
impl TendermintMachine { impl<N: Network + 'static> TendermintMachine<N> {
fn broadcast(&self, data: Data) -> Option<Block> { fn timeout(&self, step: Step) -> Instant {
todo!()
}
#[async_recursion::async_recursion]
async fn broadcast(&mut self, data: Data<N::Block>) -> Option<N::Block> {
let msg = Message { sender: self.proposer, number: self.number, round: self.round, data }; let msg = Message { sender: self.proposer, number: self.number, round: self.round, data };
let res = self.message(msg).unwrap(); let res = self.message(msg.clone()).await.unwrap();
self.broadcast.send(msg).unwrap(); self.network.write().await.broadcast(msg).await;
res res
} }
// 14-21 // 14-21
fn round_propose(&mut self) { async fn round_propose(&mut self) {
// This will happen if it's a new block and propose hasn't been called yet if self.weights.proposer(self.number, self.round) == self.proposer {
if self.personal_proposal.is_none() { let (round, block) = if let Some((round, block)) = &self.valid {
// Ensure it's actually a new block. Else, the caller failed to provide necessary data yet (Some(*round), block.clone())
// is still executing the machine
debug_assert_eq!(self.round, 0);
return;
}
if proposer(self.number, self.round) == self.proposer {
let (round, block) = if let Some((round, block)) = self.valid {
(Some(round), block)
} else { } else {
(None, self.personal_proposal.unwrap()) (None, self.personal_proposal.clone())
}; };
debug_assert!(self.broadcast(Data::Proposal(round, block)).is_none()); debug_assert!(self.broadcast(Data::Proposal(round, block)).await.is_none());
} else { } else {
self.timeouts.write().unwrap().insert(Step::Propose, self.timeout(Step::Propose)); self.timeouts.insert(Step::Propose, self.timeout(Step::Propose));
} }
} }
// 11-13 // 11-13
fn round(&mut self, round: u32) { async fn round(&mut self, round: Round) {
self.round = round; self.round = round;
self.step = Step::Propose; self.step = Step::Propose;
self.round_propose(); self.round_propose().await;
}
/// Called whenever a new block occurs
fn propose(&mut self, block: Block) {
self.personal_proposal = Some(block);
self.round_propose();
} }
// 1-9 // 1-9
fn reset(&mut self) { async fn reset(&mut self, proposal: N::Block) {
self.personal_proposal = None; self.number.0 += 1;
self.personal_proposal = proposal;
self.number += 1; self.log = MessageLog::new(self.network.read().await.weights());
self.log_map = HashMap::new();
self.precommitted = HashMap::new();
self.locked = None; self.locked = None;
self.valid = None; self.valid = None;
self.round(0); self.timeouts = HashMap::new();
self.round(Round(0)).await;
} }
// 10 // 10
pub fn new(proposer: ValidatorId, number: u32) -> TendermintHandle { pub fn new(
let block = Arc::new(RwLock::new(None)); network: N,
proposer: N::ValidatorId,
number: BlockNumber,
proposal: N::Block,
) -> TendermintHandle<N> {
let (msg_send, mut msg_recv) = mpsc::channel(100); // Backlog to accept. Currently arbitrary let (msg_send, mut msg_recv) = mpsc::channel(100); // Backlog to accept. Currently arbitrary
let (broadcast_send, broadcast_recv) = mpsc::channel(5);
TendermintHandle { TendermintHandle {
block: block.clone(),
messages: msg_send, messages: msg_send,
broadcast: broadcast_recv, handle: tokio::spawn(async move {
handle: tokio::spawn(async { let weights = network.weights();
let machine = TendermintMachine { let network = Arc::new(RwLock::new(network));
let mut machine = TendermintMachine {
network,
weights: weights.clone(),
proposer, proposer,
personal_proposal: None,
number, number,
personal_proposal: proposal,
log_map: HashMap::new(), log: MessageLog::new(weights),
precommitted: HashMap::new(), round: Round(0),
step: Step::Propose,
locked: None, locked: None,
valid: None, valid: None,
round: 0, timeouts: HashMap::new(),
step: Step::Propose,
}; };
dbg!("Proposing");
machine.round_propose().await;
loop { loop {
if self.personal_proposal.is_none() { // Check if any timeouts have been triggered
let block = block.lock().unwrap();
if block.is_some() {
self.personal_proposal = Some(block.take());
} else {
tokio::yield_now().await;
continue;
}
}
let now = Instant::now(); let now = Instant::now();
let (t1, t2, t3) = { let (t1, t2, t3) = {
let timeouts = self.timeouts.read().unwrap(); let ready = |step| machine.timeouts.get(&step).unwrap_or(&now) < &now;
let ready = |step| timeouts.get(step).unwrap_or(now) < now;
(ready(Step::Propose), ready(Step::Prevote), ready(Step::Precommit)) (ready(Step::Propose), ready(Step::Prevote), ready(Step::Precommit))
}; };
if t1 { // Propose timeout // Propose timeout
} if t1 {
if t2 { // Prevote timeout todo!()
}
if t3 { // Precommit timeout
} }
match recv.try_recv() { // Prevote timeout
Ok(msg) => machine.message(msg), if t2 {
Err(TryRecvError::Empty) => tokio::yield_now().await, todo!()
}
// Precommit timeout
if t3 {
todo!()
}
// If there's a message, handle it
match msg_recv.try_recv() {
Ok(msg) => match machine.message(msg).await {
Ok(None) => (),
Ok(Some(block)) => {
let proposal = machine.network.write().await.add_block(block);
machine.reset(proposal).await
}
Err(TendermintError::Malicious(validator)) => {
machine.network.write().await.slash(validator).await
}
Err(TendermintError::Temporal) => (),
},
Err(TryRecvError::Empty) => yield_now().await,
Err(TryRecvError::Disconnected) => break, Err(TryRecvError::Disconnected) => break,
} }
} }
@@ -198,32 +206,27 @@ impl TendermintMachine {
} }
// 49-54 // 49-54
fn check_committed(&mut self, round_num: u32) -> Option<Block> { fn check_committed(&mut self, round: Round) -> Option<N::Block> {
let proposer = proposer(self.number, round_num); let proposer = self.weights.proposer(self.number, round);
// Safe as we only check for rounds which we received a message for
let round = self.log_map[&round_num];
// Get the proposal // Get the proposal
if let Some(proposal) = round.get(&proposer).map(|p| p.get(&Step::Propose)).flatten() { if let Some(proposal) = self.log.get(round, proposer, Step::Propose) {
// Destructure // Destructure
debug_assert!(matches!(proposal, Data::Proposal(..))); debug_assert!(matches!(proposal, Data::Proposal(..)));
if let Data::Proposal(_, block) = proposal { if let Data::Proposal(_, block) = proposal {
// Check if it has gotten a sufficient amount of precommits // Check if it has gotten a sufficient amount of precommits
let (participants, weight) = let (participants, weight) =
self.message_instances(round_num, Data::Precommit(Some(block.hash))); self.log.message_instances(round, Data::Precommit(Some(block.id())));
let threshold = ((VALIDATORS / 3) * 2) + 1; let threshold = self.weights.threshold();
if weight >= threshold.into() { if weight >= threshold {
self.reset(); return Some(block.clone());
return Some(*block);
} }
// 47-48 // 47-48
if participants >= threshold.into() { if participants >= threshold {
let map = self.timeouts.write().unwrap(); let timeout = self.timeout(Step::Precommit);
if !map.contains_key(Step::Precommit) { self.timeouts.entry(Step::Precommit).or_insert(timeout);
map.insert(Step::Precommit, self.timeout(Step::Precommit));
}
} }
} }
} }
@@ -231,16 +234,21 @@ impl TendermintMachine {
None None
} }
fn message(&mut self, msg: Message) -> Result<Option<Block>, TendermintError> { async fn message(
&mut self,
msg: Message<N::ValidatorId, N::Block>,
) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> {
if msg.number != self.number { if msg.number != self.number {
Err(TendermintError::Temporal)?; Err(TendermintError::Temporal)?;
} }
if matches!(msg.data, Data::Proposal(..)) && (msg.sender != proposer(msg.height, msg.round)) { if matches!(msg.data, Data::Proposal(..)) &&
(msg.sender != self.weights.proposer(msg.number, msg.round))
{
Err(TendermintError::Malicious(msg.sender))?; Err(TendermintError::Malicious(msg.sender))?;
}; };
if !self.log(msg)? { if !self.log.log(msg.clone())? {
return Ok(None); return Ok(None);
} }
@@ -254,36 +262,38 @@ impl TendermintMachine {
} }
// Else, check if we need to jump ahead // Else, check if we need to jump ahead
let round = self.log_map[&self.round]; if msg.round.0 < self.round.0 {
if msg.round < self.round {
return Ok(None); return Ok(None);
} else if msg.round > self.round { } else if msg.round.0 > self.round.0 {
// 55-56 // 55-56
// TODO: Move to weight if self.log.round_participation(self.round) > self.weights.fault_thresold() {
if round.len() > ((VALIDATORS / 3) + 1).into() {
self.round(msg.round); self.round(msg.round);
} else { } else {
return Ok(None); return Ok(None);
} }
} }
let proposal = self
.log
.get(self.round, self.weights.proposer(self.number, self.round), Step::Propose)
.cloned();
if self.step == Step::Propose { if self.step == Step::Propose {
if let Some(proposal) = if let Some(proposal) = &proposal {
round.get(&proposer(self.number, self.round)).map(|p| p.get(&Step::Propose)).flatten()
{
debug_assert!(matches!(proposal, Data::Proposal(..))); debug_assert!(matches!(proposal, Data::Proposal(..)));
if let Data::Proposal(vr, block) = proposal { if let Data::Proposal(vr, block) = proposal {
if let Some(vr) = vr { if let Some(vr) = vr {
// 28-33 // 28-33
let vr = *vr; if (vr.0 < self.round.0) && self.log.has_consensus(*vr, Data::Prevote(Some(block.id())))
if (vr < self.round) && self.has_consensus(vr, Data::Prevote(Some(block.hash))) { {
debug_assert!(self debug_assert!(self
.broadcast(Data::Prevote(Some(block.hash).filter(|_| { .broadcast(Data::Prevote(Some(block.id()).filter(|_| {
self self
.locked .locked
.map(|(round, value)| (round <= vr) || (block == &value)) .as_ref()
.map(|(round, value)| (round.0 <= vr.0) || (block.id() == value.id()))
.unwrap_or(true) .unwrap_or(true)
}))) })))
.await
.is_none()); .is_none());
self.step = Step::Prevote; self.step = Step::Prevote;
} else { } else {
@@ -291,11 +301,16 @@ impl TendermintMachine {
} }
} else { } else {
// 22-27 // 22-27
valid(&block).map_err(|_| TendermintError::Malicious(msg.sender))?; self
.network
.write()
.await
.validate(block)
.map_err(|_| TendermintError::Malicious(msg.sender))?;
debug_assert!(self debug_assert!(self
.broadcast(Data::Prevote(Some(block.hash).filter( .broadcast(Data::Prevote(Some(block.id()).filter(|_| self.locked.is_none() ||
|_| self.locked.is_none() || self.locked.map(|locked| &locked.1) == Some(block) self.locked.as_ref().map(|locked| locked.1.id()) == Some(block.id()))))
))) .await
.is_none()); .is_none());
self.step = Step::Prevote; self.step = Step::Prevote;
} }
@@ -304,23 +319,36 @@ impl TendermintMachine {
} }
if self.step == Step::Prevote { if self.step == Step::Prevote {
let (participation, weight) = self.message_instances(self.round, Data::Prevote(None)); let (participation, weight) = self.log.message_instances(self.round, Data::Prevote(None));
// 34-35 // 34-35
if participation > (((VALIDATORS / 3) * 2) + 1).into() { if participation > self.weights.threshold() {
let map = self.timeouts.write().unwrap(); let timeout = self.timeout(Step::Prevote);
if !map.contains_key(Step::Prevote) { self.timeouts.entry(Step::Prevote).or_insert(timeout);
map.insert(Step::Prevote, self.timeout(Step::Prevote))
}
} }
// 44-46 // 44-46
if (weight > (((VALIDATORS / 3) * 2) + 1).into()) && first { if weight > self.weights.threshold() {
debug_assert!(self.broadcast(Data::Precommit(None)).is_none()); debug_assert!(self.broadcast(Data::Precommit(None)).await.is_none());
self.step = Step::Precommit; self.step = Step::Precommit;
} }
} }
if (self.valid.is_none()) && ((self.step == Step::Prevote) || (self.step == Step::Precommit)) {
if let Some(proposal) = proposal {
debug_assert!(matches!(proposal, Data::Proposal(..)));
if let Data::Proposal(_, block) = proposal {
if self.log.has_consensus(self.round, Data::Prevote(Some(block.id()))) {
self.valid = Some((self.round, block.clone()));
if self.step == Step::Prevote {
self.locked = self.valid.clone();
self.step = Step::Precommit;
return Ok(self.broadcast(Data::Precommit(Some(block.id()))).await);
}
}
}
}
}
Ok(None) Ok(None)
} }
} }
*/

View File

@@ -3,14 +3,14 @@ use std::{sync::Arc, collections::HashMap};
use crate::{ext::*, Round, Step, Data, Message, TendermintError}; use crate::{ext::*, Round, Step, Data, Message, TendermintError};
pub(crate) struct MessageLog<N: Network> { pub(crate) struct MessageLog<N: Network> {
network: Arc<N>, weights: Arc<N::Weights>,
precommitted: HashMap<N::ValidatorId, <N::Block as Block>::Id>, precommitted: HashMap<N::ValidatorId, <N::Block as Block>::Id>,
log: HashMap<Round, HashMap<N::ValidatorId, HashMap<Step, Data<N::Block>>>>, log: HashMap<Round, HashMap<N::ValidatorId, HashMap<Step, Data<N::Block>>>>,
} }
impl<N: Network> MessageLog<N> { impl<N: Network> MessageLog<N> {
pub(crate) fn new(network: Arc<N>) -> MessageLog<N> { pub(crate) fn new(weights: Arc<N::Weights>) -> MessageLog<N> {
MessageLog { network, precommitted: HashMap::new(), log: HashMap::new() } MessageLog { weights, precommitted: HashMap::new(), log: HashMap::new() }
} }
// Returns true if it's a new message // Returns true if it's a new message
@@ -51,7 +51,7 @@ impl<N: Network> MessageLog<N> {
let mut weight = 0; let mut weight = 0;
for (participant, msgs) in &self.log[&round] { for (participant, msgs) in &self.log[&round] {
if let Some(msg) = msgs.get(&data.step()) { if let Some(msg) = msgs.get(&data.step()) {
let validator_weight = self.network.weight(*participant); let validator_weight = self.weights.weight(*participant);
participating += validator_weight; participating += validator_weight;
if &data == msg { if &data == msg {
weight += validator_weight; weight += validator_weight;
@@ -61,6 +61,17 @@ impl<N: Network> MessageLog<N> {
(participating, weight) (participating, weight)
} }
// Get the participation in a given round
pub(crate) fn round_participation(&self, round: Round) -> u64 {
let mut weight = 0;
if let Some(round) = self.log.get(&round) {
for participant in round.keys() {
weight += self.weights.weight(*participant);
}
};
weight
}
// Get the participation in a given round for a given step. // Get the participation in a given round for a given step.
pub(crate) fn participation(&self, round: Round, step: Step) -> u64 { pub(crate) fn participation(&self, round: Round, step: Step) -> u64 {
let (participating, _) = self.message_instances( let (participating, _) = self.message_instances(
@@ -76,13 +87,13 @@ impl<N: Network> MessageLog<N> {
// Check if there's been a BFT level of participation // Check if there's been a BFT level of participation
pub(crate) fn has_participation(&self, round: Round, step: Step) -> bool { pub(crate) fn has_participation(&self, round: Round, step: Step) -> bool {
self.participation(round, step) >= self.network.threshold() self.participation(round, step) >= self.weights.threshold()
} }
// Check if consensus has been reached on a specific piece of data // Check if consensus has been reached on a specific piece of data
pub(crate) fn has_consensus(&self, round: Round, data: Data<N::Block>) -> bool { pub(crate) fn has_consensus(&self, round: Round, data: Data<N::Block>) -> bool {
let (_, weight) = self.message_instances(round, data); let (_, weight) = self.message_instances(round, data);
weight >= self.network.threshold() weight >= self.weights.threshold()
} }
pub(crate) fn get( pub(crate) fn get(

View File

@@ -1,36 +1,99 @@
use tendermint_machine::ext::*; use std::sync::Arc;
#[derive(Clone, PartialEq)] use tokio::sync::{RwLock, mpsc};
use tendermint_machine::{ext::*, Message, TendermintMachine, TendermintHandle};
type TestValidatorId = u16;
type TestBlockId = u32;
#[derive(Clone, PartialEq, Debug)]
struct TestBlock { struct TestBlock {
id: u32, id: TestBlockId,
valid: Result<(), BlockError>, valid: Result<(), BlockError>,
} }
impl Block for TestBlock { impl Block for TestBlock {
type Id = u32; type Id = TestBlockId;
fn id(&self) -> u32 { fn id(&self) -> TestBlockId {
self.id self.id
} }
} }
struct TestNetwork; struct TestWeights;
impl Network for TestNetwork { impl Weights for TestWeights {
type ValidatorId = u16; type ValidatorId = TestValidatorId;
type Block = TestBlock;
fn total_weight(&self) -> u64 { fn total_weight(&self) -> u64 {
5 5
} }
fn weight(&self, id: u16) -> u64 { fn weight(&self, id: TestValidatorId) -> u64 {
[1, 1, 1, 1, 1][usize::try_from(id).unwrap()] [1, 1, 1, 1, 1][usize::try_from(id).unwrap()]
} }
fn proposer(&self, number: BlockNumber, round: Round) -> u16 { fn proposer(&self, number: BlockNumber, round: Round) -> TestValidatorId {
u16::try_from((number.0 + u32::from(round.0)) % 5).unwrap() TestValidatorId::try_from((number.0 + u32::from(round.0)) % 5).unwrap()
} }
}
fn validate(&mut self, block: TestBlock) -> Result<(), BlockError> {
block.valid struct TestNetwork(Arc<RwLock<Vec<TendermintHandle<Self>>>>);
#[async_trait::async_trait]
impl Network for TestNetwork {
type ValidatorId = TestValidatorId;
type Weights = TestWeights;
type Block = TestBlock;
fn weights(&self) -> Arc<TestWeights> {
Arc::new(TestWeights)
}
async fn broadcast(&mut self, msg: Message<TestValidatorId, Self::Block>) {
for handle in self.0.write().await.iter_mut() {
handle.messages.send(msg.clone()).await.unwrap();
}
}
async fn slash(&mut self, validator: TestValidatorId) {
dbg!("Slash");
todo!()
}
fn validate(&mut self, block: &TestBlock) -> Result<(), BlockError> {
block.valid
}
fn add_block(&mut self, block: TestBlock) -> TestBlock {
dbg!("Adding ", &block);
assert!(block.valid.is_ok());
TestBlock { id: block.id + 1, valid: Ok(()) }
}
}
impl TestNetwork {
async fn new(validators: usize) -> Arc<RwLock<Vec<TendermintHandle<Self>>>> {
let arc = Arc::new(RwLock::new(vec![]));
{
let mut write = arc.write().await;
for i in 0 .. validators {
write.push(TendermintMachine::new(
TestNetwork(arc.clone()),
u16::try_from(i).unwrap(),
BlockNumber(1),
TestBlock { id: 1, valid: Ok(()) },
));
}
}
dbg!("Created all machines");
arc
}
}
#[tokio::test]
async fn test() {
TestNetwork::new(4).await;
loop {
tokio::task::yield_now().await;
} }
} }