1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.
// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.
//! In memory network simulator
//!
//! This module provides an in-memory only simulation of an actual network, useful for unit and
//! integration tests.
use core::time::Duration;
use std::{
fmt::Debug,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use async_lock::{Mutex, RwLock};
use async_trait::async_trait;
use dashmap::DashMap;
use hotshot_types::{
boxed_sync,
traits::{
network::{
AsyncGenerator, BroadcastDelay, ConnectedNetwork, TestableNetworkingImplementation,
Topic,
},
node_implementation::NodeType,
signature_key::SignatureKey,
},
BoxSyncFuture,
};
use tokio::{
spawn,
sync::mpsc::{channel, error::SendError, Receiver, Sender},
};
use tracing::{debug, error, info, info_span, instrument, trace, warn, Instrument};
use super::{NetworkError, NetworkReliability};
/// Shared state for in-memory mock networking.
///
/// This type is responsible for keeping track of the channels to each [`MemoryNetwork`], and is
/// used to group the [`MemoryNetwork`] instances.
#[derive(derive_more::Debug)]
pub struct MasterMap<K: SignatureKey> {
/// The list of `MemoryNetwork`s
#[debug(skip)]
map: DashMap<K, MemoryNetwork<K>>,
/// The list of `MemoryNetwork`s aggregated by topic
subscribed_map: DashMap<Topic, Vec<(K, MemoryNetwork<K>)>>,
}
impl<K: SignatureKey> MasterMap<K> {
/// Create a new, empty, `MasterMap`
#[must_use]
pub fn new() -> Arc<MasterMap<K>> {
Arc::new(MasterMap {
map: DashMap::new(),
subscribed_map: DashMap::new(),
})
}
}
/// Internal state for a `MemoryNetwork` instance
#[derive(Debug)]
struct MemoryNetworkInner<K: SignatureKey> {
/// Input for messages
input: RwLock<Option<Sender<Vec<u8>>>>,
/// Output for messages
output: Mutex<Receiver<Vec<u8>>>,
/// The master map
master_map: Arc<MasterMap<K>>,
/// Count of messages that are in-flight (send but not processed yet)
in_flight_message_count: AtomicUsize,
/// config to introduce unreliability to the network
reliability_config: Option<Box<dyn NetworkReliability>>,
}
/// In memory only network simulator.
///
/// This provides an in memory simulation of a networking implementation, allowing nodes running on
/// the same machine to mock networking while testing other functionality.
///
/// Under the hood, this simply maintains mpmc channels to every other `MemoryNetwork` instance of the
/// same group.
#[derive(Clone)]
pub struct MemoryNetwork<K: SignatureKey> {
/// The actual internal state
inner: Arc<MemoryNetworkInner<K>>,
}
impl<K: SignatureKey> Debug for MemoryNetwork<K> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MemoryNetwork")
.field("inner", &"inner")
.finish()
}
}
impl<K: SignatureKey> MemoryNetwork<K> {
/// Creates a new `MemoryNetwork` and hooks it up to the group through the provided `MasterMap`
pub fn new(
pub_key: &K,
master_map: &Arc<MasterMap<K>>,
subscribed_topics: &[Topic],
reliability_config: Option<Box<dyn NetworkReliability>>,
) -> MemoryNetwork<K> {
info!("Attaching new MemoryNetwork");
let (input, mut task_recv) = channel(128);
let (task_send, output) = channel(128);
let in_flight_message_count = AtomicUsize::new(0);
trace!("Channels open, spawning background task");
spawn(
async move {
debug!("Starting background task");
trace!("Entering processing loop");
while let Some(vec) = task_recv.recv().await {
trace!(?vec, "Incoming message");
// Attempt to decode message
let ts = task_send.clone();
let res = ts.send(vec).await;
if res.is_ok() {
trace!("Passed message to output queue");
} else {
error!("Output queue receivers are shutdown");
}
}
}
.instrument(info_span!("MemoryNetwork Background task", map = ?master_map)),
);
trace!("Notifying other networks of the new connected peer");
trace!("Task spawned, creating MemoryNetwork");
let mn = MemoryNetwork {
inner: Arc::new(MemoryNetworkInner {
input: RwLock::new(Some(input)),
output: Mutex::new(output),
master_map: Arc::clone(master_map),
in_flight_message_count,
reliability_config,
}),
};
// Insert our public key into the master map
master_map.map.insert(pub_key.clone(), mn.clone());
// Insert our subscribed topics into the master map
for topic in subscribed_topics {
master_map
.subscribed_map
.entry(topic.clone())
.or_default()
.push((pub_key.clone(), mn.clone()));
}
mn
}
/// Send a [`Vec<u8>`] message to the inner `input`
async fn input(&self, message: Vec<u8>) -> Result<(), SendError<Vec<u8>>> {
self.inner
.in_flight_message_count
.fetch_add(1, Ordering::Relaxed);
let input = self.inner.input.read().await;
if let Some(input) = &*input {
input.send(message).await
} else {
Err(SendError(message))
}
}
}
impl<TYPES: NodeType> TestableNetworkingImplementation<TYPES>
for MemoryNetwork<TYPES::SignatureKey>
{
fn generator(
_expected_node_count: usize,
_num_bootstrap: usize,
_network_id: usize,
da_committee_size: usize,
reliability_config: Option<Box<dyn NetworkReliability>>,
_secondary_network_delay: Duration,
) -> AsyncGenerator<Arc<Self>> {
let master: Arc<_> = MasterMap::new();
// We assign known_nodes' public key and stake value rather than read from config file since it's a test
Box::pin(move |node_id| {
let privkey = TYPES::SignatureKey::generated_from_seed_indexed([0u8; 32], node_id).1;
let pubkey = TYPES::SignatureKey::from_private(&privkey);
// Subscribe to topics based on our index
let subscribed_topics = if node_id < da_committee_size as u64 {
// DA node
vec![Topic::Da, Topic::Global]
} else {
// Non-DA node
vec![Topic::Global]
};
let net = MemoryNetwork::new(
&pubkey,
&master,
&subscribed_topics,
reliability_config.clone(),
);
Box::pin(async move { net.into() })
})
}
fn in_flight_message_count(&self) -> Option<usize> {
Some(self.inner.in_flight_message_count.load(Ordering::Relaxed))
}
}
// TODO instrument these functions
#[async_trait]
impl<K: SignatureKey + 'static> ConnectedNetwork<K> for MemoryNetwork<K> {
#[instrument(name = "MemoryNetwork::ready_blocking")]
async fn wait_for_ready(&self) {}
fn pause(&self) {
unimplemented!("Pausing not implemented for the Memory network");
}
fn resume(&self) {
unimplemented!("Resuming not implemented for the Memory network");
}
#[instrument(name = "MemoryNetwork::shut_down")]
fn shut_down<'a, 'b>(&'a self) -> BoxSyncFuture<'b, ()>
where
'a: 'b,
Self: 'b,
{
let closure = async move {
*self.inner.input.write().await = None;
};
boxed_sync(closure)
}
#[instrument(name = "MemoryNetwork::broadcast_message")]
async fn broadcast_message(
&self,
message: Vec<u8>,
topic: Topic,
_broadcast_delay: BroadcastDelay,
) -> Result<(), NetworkError> {
trace!(?message, "Broadcasting message");
for node in self
.inner
.master_map
.subscribed_map
.entry(topic)
.or_default()
.iter()
{
// TODO delay/drop etc here
let (key, node) = node;
trace!(?key, "Sending message to node");
if let Some(ref config) = &self.inner.reliability_config {
{
let node2 = node.clone();
let fut = config.chaos_send_msg(
message.clone(),
Arc::new(move |msg: Vec<u8>| {
let node3 = (node2).clone();
boxed_sync(async move {
let _res = node3.input(msg).await;
// NOTE we're dropping metrics here but this is only for testing
// purposes. I think that should be okay
})
}),
);
spawn(fut);
}
} else {
let res = node.input(message.clone()).await;
match res {
Ok(()) => {
trace!(?key, "Delivered message to remote");
}
Err(e) => {
warn!(?e, ?key, "Error sending broadcast message to node");
}
}
}
}
Ok(())
}
#[instrument(name = "MemoryNetwork::da_broadcast_message")]
async fn da_broadcast_message(
&self,
message: Vec<u8>,
recipients: Vec<K>,
_broadcast_delay: BroadcastDelay,
) -> Result<(), NetworkError> {
trace!(?message, "Broadcasting message to DA");
for node in self
.inner
.master_map
.subscribed_map
.entry(Topic::Da)
.or_default()
.iter()
{
if !recipients.contains(&node.0) {
tracing::error!("Skipping node because not in recipient list: {:?}", &node.0);
continue;
}
// TODO delay/drop etc here
let (key, node) = node;
trace!(?key, "Sending message to node");
if let Some(ref config) = &self.inner.reliability_config {
{
let node2 = node.clone();
let fut = config.chaos_send_msg(
message.clone(),
Arc::new(move |msg: Vec<u8>| {
let node3 = (node2).clone();
boxed_sync(async move {
let _res = node3.input(msg).await;
// NOTE we're dropping metrics here but this is only for testing
// purposes. I think that should be okay
})
}),
);
spawn(fut);
}
} else {
let res = node.input(message.clone()).await;
match res {
Ok(()) => {
trace!(?key, "Delivered message to remote");
}
Err(e) => {
warn!(?e, ?key, "Error sending broadcast message to node");
}
}
}
}
Ok(())
}
#[instrument(name = "MemoryNetwork::direct_message")]
async fn direct_message(&self, message: Vec<u8>, recipient: K) -> Result<(), NetworkError> {
// debug!(?message, ?recipient, "Sending direct message");
// Bincode the message
trace!("Message bincoded, finding recipient");
if let Some(node) = self.inner.master_map.map.get(&recipient) {
let node = node.value().clone();
if let Some(ref config) = &self.inner.reliability_config {
{
let fut = config.chaos_send_msg(
message.clone(),
Arc::new(move |msg: Vec<u8>| {
let node2 = node.clone();
boxed_sync(async move {
let _res = node2.input(msg).await;
// NOTE we're dropping metrics here but this is only for testing
// purposes. I think that should be okay
})
}),
);
spawn(fut);
}
Ok(())
} else {
let res = node.input(message).await;
match res {
Ok(()) => {
trace!(?recipient, "Delivered message to remote");
Ok(())
}
Err(e) => Err(NetworkError::MessageSendError(format!(
"error sending direct message to node: {e}",
))),
}
}
} else {
Err(NetworkError::MessageSendError(
"node does not exist".to_string(),
))
}
}
/// Receive one or many messages from the underlying network.
///
/// # Errors
/// If the other side of the channel is closed
#[instrument(name = "MemoryNetwork::recv_messages", skip_all)]
async fn recv_message(&self) -> Result<Vec<u8>, NetworkError> {
let ret = self
.inner
.output
.lock()
.await
.recv()
.await
.ok_or(NetworkError::ShutDown)?;
self.inner
.in_flight_message_count
.fetch_sub(1, Ordering::Relaxed);
Ok(ret)
}
}