mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add a test to the coordinator for running a Tributary
Impls a LocalP2p for testing. Moves rebroadcasting into Tendermint, since it's what knows if a message is fully valid + original. Removes TributarySpec::validators() HashMap, as its non-determinism caused different instances to have different round robin schedules. It was already prior moved to a Vec for this issue, so I'm unsure why this remnant existed. Also renames the GH no-std workflow from the prior commit.
This commit is contained in:
@@ -1,25 +1,79 @@
|
||||
use core::fmt::Debug;
|
||||
use std::{
|
||||
sync::{Arc, RwLock},
|
||||
collections::VecDeque,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use tributary::P2p as TributaryP2p;
|
||||
pub use tributary::P2p as TributaryP2p;
|
||||
|
||||
// TODO
|
||||
#[async_trait]
|
||||
pub trait P2p: Send + Sync + Clone + Debug + TributaryP2p {}
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
pub enum P2pMessageKind {
|
||||
Tributary,
|
||||
}
|
||||
|
||||
// TODO
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LocalP2p {}
|
||||
impl P2pMessageKind {
|
||||
fn to_byte(self) -> u8 {
|
||||
match self {
|
||||
P2pMessageKind::Tributary => 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TributaryP2p for LocalP2p {
|
||||
async fn broadcast(&self, msg: Vec<u8>) {
|
||||
// TODO
|
||||
todo!()
|
||||
fn from_byte(byte: u8) -> Option<P2pMessageKind> {
|
||||
match byte {
|
||||
0 => Some(P2pMessageKind::Tributary),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
#[async_trait]
|
||||
impl P2p for LocalP2p {}
|
||||
pub trait P2p: Send + Sync + Clone + Debug + TributaryP2p {
|
||||
async fn broadcast(&self, msg: Vec<u8>);
|
||||
async fn receive(&self) -> Option<(P2pMessageKind, Vec<u8>)>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LocalP2p(usize, Arc<RwLock<Vec<VecDeque<Vec<u8>>>>>);
|
||||
|
||||
impl LocalP2p {
|
||||
pub fn new(validators: usize) -> Vec<LocalP2p> {
|
||||
let shared = Arc::new(RwLock::new(vec![VecDeque::new(); validators]));
|
||||
let mut res = vec![];
|
||||
for i in 0 .. validators {
|
||||
res.push(LocalP2p(i, shared.clone()));
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl P2p for LocalP2p {
|
||||
async fn broadcast(&self, msg: Vec<u8>) {
|
||||
for (i, msg_queue) in self.1.write().unwrap().iter_mut().enumerate() {
|
||||
if i == self.0 {
|
||||
continue;
|
||||
}
|
||||
msg_queue.push_back(msg.clone());
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive(&self) -> Option<(P2pMessageKind, Vec<u8>)> {
|
||||
let mut msg = self.1.write().unwrap()[self.0].pop_front()?;
|
||||
if msg.is_empty() {
|
||||
log::error!("empty p2p message");
|
||||
return None;
|
||||
}
|
||||
Some((P2pMessageKind::from_byte(msg.remove(0))?, msg))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TributaryP2p for LocalP2p {
|
||||
async fn broadcast(&self, mut msg: Vec<u8>) {
|
||||
msg.insert(0, P2pMessageKind::Tributary.to_byte());
|
||||
<Self as P2p>::broadcast(self, msg).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user