Skip to content

Commit

Permalink
Fix simtest determinism (MystenLabs#16029)
Browse files Browse the repository at this point in the history
  • Loading branch information
mystenmark authored Jan 31, 2024
1 parent c08e356 commit 7c04547
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
13 changes: 10 additions & 3 deletions crates/sui-e2e-tests/tests/full_node_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,16 @@ async fn test_full_node_cold_sync() -> Result<(), anyhow::Error> {
}

#[sim_test]
async fn test_full_node_sync_flood() -> Result<(), anyhow::Error> {
async fn test_full_node_sync_flood() {
do_test_full_node_sync_flood().await
}

#[sim_test(check_determinism)]
async fn test_full_node_sync_flood_determinism() {
do_test_full_node_sync_flood().await
}

async fn do_test_full_node_sync_flood() {
let mut test_cluster = TestClusterBuilder::new().build().await;

// Start a new fullnode that is not on the write path
Expand Down Expand Up @@ -624,8 +633,6 @@ async fn test_full_node_sync_flood() -> Result<(), anyhow::Error> {
.notify_read_executed_effects(digests)
.await
.unwrap();

Ok(())
}

#[sim_test]
Expand Down
58 changes: 44 additions & 14 deletions narwhal/worker/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,39 @@ use tokio::time::{sleep, timeout, Duration};
use tracing::info;
use types::{Transaction, TxResponse};

/// Uses a map to allow running multiple Narwhal instances in the same process.
static LOCAL_NARWHAL_CLIENTS: Mutex<BTreeMap<Multiaddr, Arc<ArcSwap<LocalNarwhalClient>>>> =
Mutex::new(BTreeMap::new());
#[cfg(msim)]
mod static_client_cache {
use super::*;
thread_local! {
/// Uses a map to allow running multiple Narwhal instances in the same process.
static LOCAL_NARWHAL_CLIENTS: Mutex<BTreeMap<Multiaddr, Arc<ArcSwap<LocalNarwhalClient>>>> =
Mutex::new(BTreeMap::new());
}

pub(super) fn with_clients<T>(
f: impl FnOnce(&mut BTreeMap<Multiaddr, Arc<ArcSwap<LocalNarwhalClient>>>) -> T,
) -> T {
LOCAL_NARWHAL_CLIENTS.with(|clients| {
let mut clients = clients.lock().unwrap();
f(&mut clients)
})
}
}

#[cfg(not(msim))]
mod static_client_cache {
use super::*;
/// Uses a map to allow running multiple Narwhal instances in the same process.
static LOCAL_NARWHAL_CLIENTS: Mutex<BTreeMap<Multiaddr, Arc<ArcSwap<LocalNarwhalClient>>>> =
Mutex::new(BTreeMap::new());

pub(super) fn with_clients<T>(
f: impl FnOnce(&mut BTreeMap<Multiaddr, Arc<ArcSwap<LocalNarwhalClient>>>) -> T,
) -> T {
let mut clients = LOCAL_NARWHAL_CLIENTS.lock().unwrap();
f(&mut clients)
}
}

/// The maximum allowed size of transactions into Narwhal.
/// TODO: maybe move to TxValidator?
Expand Down Expand Up @@ -96,23 +126,23 @@ impl LocalNarwhalClient {
pub fn set_global(addr: Multiaddr, instance: Arc<Self>) {
info!("Narwhal worker client added ({})", addr);
let addr = Self::canonicalize_address_key(addr);
let mut clients = LOCAL_NARWHAL_CLIENTS.lock().unwrap();
match clients.entry(addr) {
Entry::Vacant(entry) => {
entry.insert(Arc::new(ArcSwap::from(instance)));
}
Entry::Occupied(mut entry) => {
entry.get_mut().store(instance);
}
};
static_client_cache::with_clients(|clients| {
match clients.entry(addr) {
Entry::Vacant(entry) => {
entry.insert(Arc::new(ArcSwap::from(instance)));
}
Entry::Occupied(mut entry) => {
entry.get_mut().store(instance);
}
};
});
}

/// Gets the instance of LocalNarwhalClient for the local address.
/// Address is only used as the key.
pub fn get_global(addr: &Multiaddr) -> Option<Arc<ArcSwap<Self>>> {
let addr = Self::canonicalize_address_key(addr.clone());
let clients = LOCAL_NARWHAL_CLIENTS.lock().unwrap();
clients.get(&addr).cloned()
static_client_cache::with_clients(|clients| clients.get(&addr).cloned())
}

/// Submits a transaction to the local Narwhal worker.
Expand Down

0 comments on commit 7c04547

Please sign in to comment.