mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Successfully compiling
This commit is contained in:
@@ -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 {
|
||||
id: u32,
|
||||
id: TestBlockId,
|
||||
valid: Result<(), BlockError>,
|
||||
}
|
||||
|
||||
impl Block for TestBlock {
|
||||
type Id = u32;
|
||||
type Id = TestBlockId;
|
||||
|
||||
fn id(&self) -> u32 {
|
||||
fn id(&self) -> TestBlockId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
struct TestNetwork;
|
||||
impl Network for TestNetwork {
|
||||
type ValidatorId = u16;
|
||||
type Block = TestBlock;
|
||||
struct TestWeights;
|
||||
impl Weights for TestWeights {
|
||||
type ValidatorId = TestValidatorId;
|
||||
|
||||
fn total_weight(&self) -> u64 {
|
||||
5
|
||||
}
|
||||
fn weight(&self, id: u16) -> u64 {
|
||||
fn weight(&self, id: TestValidatorId) -> u64 {
|
||||
[1, 1, 1, 1, 1][usize::try_from(id).unwrap()]
|
||||
}
|
||||
|
||||
fn proposer(&self, number: BlockNumber, round: Round) -> u16 {
|
||||
u16::try_from((number.0 + u32::from(round.0)) % 5).unwrap()
|
||||
}
|
||||
|
||||
fn validate(&mut self, block: TestBlock) -> Result<(), BlockError> {
|
||||
block.valid
|
||||
fn proposer(&self, number: BlockNumber, round: Round) -> TestValidatorId {
|
||||
TestValidatorId::try_from((number.0 + u32::from(round.0)) % 5).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user