node: add more tests for refs relay

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier
2022-10-14 18:51:53 +02:00
parent ba95725028
commit d9517cc966
5 changed files with 152 additions and 27 deletions
+13 -7
View File
@@ -14,6 +14,7 @@ use log::*;
use nakamoto_net as nakamoto;
use nakamoto_net::{Link, LocalDuration, LocalTime};
use crate::crypto::Signer;
use crate::service::reactor::Io;
use crate::service::{DisconnectReason, Envelope, Event};
use crate::storage::WriteStorage;
@@ -29,7 +30,9 @@ pub const MAX_EVENTS: usize = 2048;
type NodeId = std::net::IpAddr;
/// A simulated peer. Service instances have to be wrapped in this type to be simulated.
pub trait Peer<S>: Deref<Target = Service<S>> + DerefMut<Target = Service<S>> + 'static {
pub trait Peer<S, G>:
Deref<Target = Service<S, G>> + DerefMut<Target = Service<S, G>> + 'static
{
/// Initialize the peer. This should at minimum initialize the service with the
/// current time.
fn init(&mut self);
@@ -166,7 +169,7 @@ impl Default for Options {
}
/// A peer-to-peer node simulation.
pub struct Simulation<S> {
pub struct Simulation<S, G> {
/// Inbox of inputs to be delivered by the simulation.
inbox: Inbox,
/// Events emitted during simulation.
@@ -191,9 +194,11 @@ pub struct Simulation<S> {
rng: fastrand::Rng,
/// Storage type.
storage: PhantomData<S>,
/// Signer type.
signer: PhantomData<G>,
}
impl<S: WriteStorage + 'static> Simulation<S> {
impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
/// Create a new simulation.
pub fn new(time: LocalTime, rng: fastrand::Rng, opts: Options) -> Self {
Self {
@@ -211,6 +216,7 @@ impl<S: WriteStorage + 'static> Simulation<S> {
time,
rng,
storage: PhantomData,
signer: PhantomData,
}
}
@@ -267,7 +273,7 @@ impl<S: WriteStorage + 'static> Simulation<S> {
/// Initialize peers.
pub fn initialize<'a, P>(self, peers: impl IntoIterator<Item = &'a mut P>) -> Self
where
P: Peer<S>,
P: Peer<S, G>,
{
for peer in peers.into_iter() {
peer.init();
@@ -281,7 +287,7 @@ impl<S: WriteStorage + 'static> Simulation<S> {
peers: impl IntoIterator<Item = &'a mut P>,
pred: impl Fn(&Self) -> bool,
) where
P: Peer<S>,
P: Peer<S, G>,
{
let mut nodes: BTreeMap<_, _> = peers.into_iter().map(|p| (p.addr().ip(), p)).collect();
@@ -295,12 +301,12 @@ impl<S: WriteStorage + 'static> Simulation<S> {
/// Process one scheduled input from the inbox, using the provided peers.
/// This function should be called until it returns `false`, or some desired state is reached.
/// Returns `true` if there are more messages to process.
pub fn step<'a, P: Peer<S>>(&mut self, peers: impl IntoIterator<Item = &'a mut P>) -> bool {
pub fn step<'a, P: Peer<S, G>>(&mut self, peers: impl IntoIterator<Item = &'a mut P>) -> bool {
let mut nodes: BTreeMap<_, _> = peers.into_iter().map(|p| (p.addr().ip(), p)).collect();
self.step_(&mut nodes)
}
fn step_<P: Peer<S>>(&mut self, nodes: &mut BTreeMap<NodeId, &mut P>) -> bool {
fn step_<P: Peer<S, G>>(&mut self, nodes: &mut BTreeMap<NodeId, &mut P>) -> bool {
if !self.opts.latency.is_empty() {
// Configure latencies.
for (i, from) in nodes.keys().enumerate() {