From 23ffe38a689f3a840bbc1ec2102c32cfdf68a07e Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Thu, 26 Jun 2025 15:18:01 +0200 Subject: [PATCH 1/7] clarify between fee_score and identifier --- crates/sim/src/cache.rs | 45 ++++++++++++++++++------------- crates/sim/src/env.rs | 56 ++++++++++++++++++++------------------- crates/sim/src/outcome.rs | 2 +- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/crates/sim/src/cache.rs b/crates/sim/src/cache.rs index 1be9528b..ed78d854 100644 --- a/crates/sim/src/cache.rs +++ b/crates/sim/src/cache.rs @@ -42,7 +42,14 @@ impl SimCache { /// Get an iterator over the best items in the cache. pub fn read_best(&self, n: usize) -> Vec<(u128, SimItem)> { - self.inner.read().items.iter().rev().take(n).map(|(k, item)| (*k, item.clone())).collect() + self.inner + .read() + .items + .iter() + .rev() + .take(n) + .map(|(fee_score, item)| (*fee_score, item.clone())) + .collect() } /// Get the number of items in the cache. @@ -56,14 +63,14 @@ impl SimCache { } /// Get an item by key. - pub fn get(&self, key: u128) -> Option { - self.inner.read().items.get(&key).cloned() + pub fn get(&self, fee_score: u128) -> Option { + self.inner.read().items.get(&fee_score).cloned() } /// Remove an item by key. - pub fn remove(&self, key: u128) -> Option { + pub fn remove(&self, fee_score: u128) -> Option { let mut inner = self.inner.write(); - if let Some(item) = inner.items.remove(&key) { + if let Some(item) = inner.items.remove(&fee_score) { inner.seen.remove(item.identifier().as_bytes()); Some(item) } else { @@ -71,15 +78,15 @@ impl SimCache { } } - fn add_inner(inner: &mut CacheInner, mut score: u128, item: SimItem, capacity: usize) { + fn add_inner(inner: &mut CacheInner, mut fee_score: u128, item: SimItem, capacity: usize) { // Check if we've already seen this item - if so, don't add it if !inner.seen.insert(item.identifier_owned()) { return; } - // If it has the same score, we decrement (prioritizing earlier items) - while inner.items.contains_key(&score) && score != 0 { - score = score.saturating_sub(1); + // If it has the same fee_score, we decrement (prioritizing earlier items) + while inner.items.contains_key(&fee_score) && fee_score != 0 { + fee_score = fee_score.saturating_sub(1); } if inner.items.len() >= capacity { @@ -89,7 +96,7 @@ impl SimCache { } } - inner.items.insert(score, item.clone()); + inner.items.insert(fee_score, item.clone()); } /// Add a bundle to the cache. @@ -100,10 +107,10 @@ impl SimCache { } let item = SimItem::try_from(bundle)?; - let score = item.calculate_total_fee(basefee); + let fee_score = item.calculate_total_fee(basefee); let mut inner = self.inner.write(); - Self::add_inner(&mut inner, score, item, self.capacity); + Self::add_inner(&mut inner, fee_score, item, self.capacity); Ok(()) } @@ -124,8 +131,8 @@ impl SimCache { // Skip invalid bundles continue; }; - let score = item.calculate_total_fee(basefee); - Self::add_inner(&mut inner, score, item, self.capacity); + let fee_score = item.calculate_total_fee(basefee); + Self::add_inner(&mut inner, fee_score, item, self.capacity); } Ok(()) @@ -134,10 +141,10 @@ impl SimCache { /// Add a transaction to the cache. pub fn add_tx(&self, tx: TxEnvelope, basefee: u64) { let item = SimItem::from(tx); - let score = item.calculate_total_fee(basefee); + let fee_score = item.calculate_total_fee(basefee); let mut inner = self.inner.write(); - Self::add_inner(&mut inner, score, item, self.capacity); + Self::add_inner(&mut inner, fee_score, item, self.capacity); } /// Add an iterator of transactions to the cache. This locks the cache only once @@ -149,8 +156,8 @@ impl SimCache { for item in item.into_iter() { let item = SimItem::from(item); - let score = item.calculate_total_fee(basefee); - Self::add_inner(&mut inner, score, item, self.capacity); + let fee_score = item.calculate_total_fee(basefee); + Self::add_inner(&mut inner, fee_score, item, self.capacity); } } @@ -196,7 +203,9 @@ impl SimCache { /// Internal cache data, meant to be protected by a lock. struct CacheInner { + /// Key is the fee_score, the calculated gas fees from the transaction. Value is SimItem itself. items: BTreeMap, + /// Key is the unique identifier for the SimItem. UUID for bundles, tx hash for transactions. seen: HashSet>, } diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index 1d27a775..c1b78382 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -85,12 +85,13 @@ where /// Run a simulation round, returning the best item. #[instrument(skip(self))] pub async fn sim_round(&mut self, max_gas: u64) -> Option { - let (best_tx, mut best_watcher) = watch::channel(None); + let (best_tx_channel, mut best_tx_watcher) = watch::channel(None); let this = self.inner.clone(); // Spawn a blocking task to run the simulations. - let sim_task = tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx)); + let sim_task = + tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_channel)); // Either simulation is done, or we time out select! { @@ -103,7 +104,7 @@ where } // Check what the current best outcome is. - let best = best_watcher.borrow_and_update(); + let best = best_tx_watcher.borrow_and_update(); trace!(score = %best.as_ref().map(|candidate| candidate.score).unwrap_or_default(), "Read outcome from channel"); let outcome = best.as_ref()?; @@ -264,10 +265,10 @@ where /// /// This function runs the simulation in a separate thread and waits for /// the result or the deadline to expire. - #[instrument(skip_all, fields(identifier, tx_hash = %transaction.hash()))] + #[instrument(skip_all, fields(fee_score, tx_hash = %transaction.hash()))] fn simulate_tx( &self, - identifier: u128, + fee_score: u128, transaction: &TxEnvelope, ) -> Result>> { let trevm = self.create_with_block(&self.cfg, &self.block).unwrap(); @@ -302,7 +303,8 @@ where let score = beneficiary_balance.saturating_sub(initial_beneficiary_balance); trace!( - ?identifier, + ?fee_score, + tx_hash = %transaction.hash(), gas_used = gas_used, score = %score, reverted = !success, @@ -313,17 +315,17 @@ where ); // Create the outcome - Ok(SimOutcomeWithCache { identifier, score, cache, gas_used }) + Ok(SimOutcomeWithCache { identifier: fee_score, score, cache, gas_used }) } Err(e) => Err(SignetEthBundleError::from(e.into_error())), } } /// Simulates a bundle on the current environment. - #[instrument(skip_all, fields(identifier, uuid = bundle.replacement_uuid()))] + #[instrument(skip_all, fields(fee_score, uuid = bundle.replacement_uuid()))] fn simulate_bundle( &self, - identifier: u128, + fee_score: u128, bundle: &SignetEthBundle, ) -> Result>> where @@ -344,24 +346,24 @@ where let cache = trevm.into_db().into_cache(); trace!( - ?identifier, + ?fee_score, gas_used = gas_used, score = %score, "Bundle simulation successful" ); - Ok(SimOutcomeWithCache { identifier, score, cache, gas_used }) + Ok(SimOutcomeWithCache { identifier: fee_score, score, cache, gas_used }) } /// Simulates a transaction or bundle in the context of a block. fn simulate( &self, - identifier: u128, + fee_score: u128, item: &SimItem, ) -> Result>> { match item { - SimItem::Bundle(bundle) => self.simulate_bundle(identifier, bundle), - SimItem::Tx(tx) => self.simulate_tx(identifier, tx), + SimItem::Bundle(bundle) => self.simulate_bundle(fee_score, bundle), + SimItem::Tx(tx) => self.simulate_tx(fee_score, tx), } } @@ -369,7 +371,7 @@ where fn sim_round( self: Arc, max_gas: u64, - best_tx: watch::Sender>, + best_tx_channel: watch::Sender>, ) { // Pull the `n` best items from the cache. let active_sim = self.sim_items.read_best(self.concurrency_limit); @@ -386,16 +388,16 @@ where std::thread::scope(move |scope| { // Spawn a thread per bundle to simulate. - for (identifier, item) in active_sim.into_iter() { + for (fee_score, item) in active_sim.into_iter() { let c = candidates.clone(); scope.spawn(move || { - let _ig = trace_span!(parent: outer_ref, "sim_task", identifier = %identifier) - .entered(); + let identifier = item.identifier(); + let _ig = trace_span!(parent: outer_ref, "sim_task", %identifier).entered(); // If simulation is succesful, send the outcome via the // channel. - match this_ref.simulate(identifier, &item) { + match this_ref.simulate(fee_score, &item) { Ok(candidate) => { if candidate.gas_used <= max_gas { // shortcut return on success @@ -410,7 +412,7 @@ where }; // fall through applies to all errors, occurs if // the simulation fails or the gas limit is exceeded. - this_ref.sim_items.remove(identifier); + this_ref.sim_items.remove(fee_score); }); } // Drop the TX so that the channel is closed when all threads @@ -420,17 +422,17 @@ where // Wait for each thread to finish. Find the best outcome. while let Some(candidate) = candidates_rx.blocking_recv() { // Update the best score and send it to the channel. - let _ = best_tx.send_if_modified(|current| { - let best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); - let current_id = current.as_ref().map(|c| c.identifier); + let _ = best_tx_channel.send_if_modified(|current| { + let current_best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); + let current_fee_score = current.as_ref().map(|c| c.identifier); - let changed = candidate.score > best_score; + let changed = candidate.score > current_best_score; if changed { trace!( - old_best = ?best_score, - old_identifier = current_id, + old_best = ?current_best_score, + old_fee_score = current_fee_score, new_best = %candidate.score, - identifier = candidate.identifier, + new_fee_score = candidate.identifier, "Found better candidate" ); *current = Some(candidate); diff --git a/crates/sim/src/outcome.rs b/crates/sim/src/outcome.rs index 67a96555..b1080bbb 100644 --- a/crates/sim/src/outcome.rs +++ b/crates/sim/src/outcome.rs @@ -7,7 +7,7 @@ use crate::SimItem; /// state changes. #[derive(Debug, Clone)] pub struct SimOutcomeWithCache { - /// The transaction or bundle that was simulated, as in the cache. + /// The fee score calculated for the transaction or bundle that was simulated; also its key in the cache. pub identifier: u128, /// The score of the simulation, a [`U256`] value that represents the From 787a9351e72a3a6fa6f4329fa85cfee37836ecde Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Thu, 26 Jun 2025 15:36:34 +0200 Subject: [PATCH 2/7] sender/receiver --- crates/sim/src/env.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index c1b78382..097c2356 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -85,13 +85,13 @@ where /// Run a simulation round, returning the best item. #[instrument(skip(self))] pub async fn sim_round(&mut self, max_gas: u64) -> Option { - let (best_tx_channel, mut best_tx_watcher) = watch::channel(None); + let (best_tx_sender, mut best_tx_receiver) = watch::channel(None); let this = self.inner.clone(); // Spawn a blocking task to run the simulations. let sim_task = - tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_channel)); + tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_sender)); // Either simulation is done, or we time out select! { @@ -104,7 +104,7 @@ where } // Check what the current best outcome is. - let best = best_tx_watcher.borrow_and_update(); + let best = best_tx_receiver.borrow_and_update(); trace!(score = %best.as_ref().map(|candidate| candidate.score).unwrap_or_default(), "Read outcome from channel"); let outcome = best.as_ref()?; @@ -371,7 +371,7 @@ where fn sim_round( self: Arc, max_gas: u64, - best_tx_channel: watch::Sender>, + best_tx_sender: watch::Sender>, ) { // Pull the `n` best items from the cache. let active_sim = self.sim_items.read_best(self.concurrency_limit); @@ -422,7 +422,7 @@ where // Wait for each thread to finish. Find the best outcome. while let Some(candidate) = candidates_rx.blocking_recv() { // Update the best score and send it to the channel. - let _ = best_tx_channel.send_if_modified(|current| { + let _ = best_tx_sender.send_if_modified(|current| { let current_best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); let current_fee_score = current.as_ref().map(|c| c.identifier); From 833cdd87f69d4f665ac6abce7d34258aaa3847fc Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Thu, 26 Jun 2025 15:58:19 +0200 Subject: [PATCH 3/7] fee_score -> cache_rank --- crates/sim/src/cache.rs | 38 +++++++++++++++++++------------------- crates/sim/src/env.rs | 39 +++++++++++++++++++-------------------- crates/sim/src/outcome.rs | 4 ++-- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/crates/sim/src/cache.rs b/crates/sim/src/cache.rs index ed78d854..911ea7bb 100644 --- a/crates/sim/src/cache.rs +++ b/crates/sim/src/cache.rs @@ -48,7 +48,7 @@ impl SimCache { .iter() .rev() .take(n) - .map(|(fee_score, item)| (*fee_score, item.clone())) + .map(|(cache_rank, item)| (*cache_rank, item.clone())) .collect() } @@ -63,14 +63,14 @@ impl SimCache { } /// Get an item by key. - pub fn get(&self, fee_score: u128) -> Option { - self.inner.read().items.get(&fee_score).cloned() + pub fn get(&self, cache_rank: u128) -> Option { + self.inner.read().items.get(&cache_rank).cloned() } /// Remove an item by key. - pub fn remove(&self, fee_score: u128) -> Option { + pub fn remove(&self, cache_rank: u128) -> Option { let mut inner = self.inner.write(); - if let Some(item) = inner.items.remove(&fee_score) { + if let Some(item) = inner.items.remove(&cache_rank) { inner.seen.remove(item.identifier().as_bytes()); Some(item) } else { @@ -78,15 +78,15 @@ impl SimCache { } } - fn add_inner(inner: &mut CacheInner, mut fee_score: u128, item: SimItem, capacity: usize) { + fn add_inner(inner: &mut CacheInner, mut cache_rank: u128, item: SimItem, capacity: usize) { // Check if we've already seen this item - if so, don't add it if !inner.seen.insert(item.identifier_owned()) { return; } - // If it has the same fee_score, we decrement (prioritizing earlier items) - while inner.items.contains_key(&fee_score) && fee_score != 0 { - fee_score = fee_score.saturating_sub(1); + // If it has the same cache_rank, we decrement (prioritizing earlier items) + while inner.items.contains_key(&cache_rank) && cache_rank != 0 { + cache_rank = cache_rank.saturating_sub(1); } if inner.items.len() >= capacity { @@ -96,7 +96,7 @@ impl SimCache { } } - inner.items.insert(fee_score, item.clone()); + inner.items.insert(cache_rank, item.clone()); } /// Add a bundle to the cache. @@ -107,10 +107,10 @@ impl SimCache { } let item = SimItem::try_from(bundle)?; - let fee_score = item.calculate_total_fee(basefee); + let cache_rank = item.calculate_total_fee(basefee); let mut inner = self.inner.write(); - Self::add_inner(&mut inner, fee_score, item, self.capacity); + Self::add_inner(&mut inner, cache_rank, item, self.capacity); Ok(()) } @@ -131,8 +131,8 @@ impl SimCache { // Skip invalid bundles continue; }; - let fee_score = item.calculate_total_fee(basefee); - Self::add_inner(&mut inner, fee_score, item, self.capacity); + let cache_rank = item.calculate_total_fee(basefee); + Self::add_inner(&mut inner, cache_rank, item, self.capacity); } Ok(()) @@ -141,10 +141,10 @@ impl SimCache { /// Add a transaction to the cache. pub fn add_tx(&self, tx: TxEnvelope, basefee: u64) { let item = SimItem::from(tx); - let fee_score = item.calculate_total_fee(basefee); + let cache_rank = item.calculate_total_fee(basefee); let mut inner = self.inner.write(); - Self::add_inner(&mut inner, fee_score, item, self.capacity); + Self::add_inner(&mut inner, cache_rank, item, self.capacity); } /// Add an iterator of transactions to the cache. This locks the cache only once @@ -156,8 +156,8 @@ impl SimCache { for item in item.into_iter() { let item = SimItem::from(item); - let fee_score = item.calculate_total_fee(basefee); - Self::add_inner(&mut inner, fee_score, item, self.capacity); + let cache_rank = item.calculate_total_fee(basefee); + Self::add_inner(&mut inner, cache_rank, item, self.capacity); } } @@ -203,7 +203,7 @@ impl SimCache { /// Internal cache data, meant to be protected by a lock. struct CacheInner { - /// Key is the fee_score, the calculated gas fees from the transaction. Value is SimItem itself. + /// Key is the cache_rank, unique ID within the cache && the item's order in the cache. Value is SimItem itself. items: BTreeMap, /// Key is the unique identifier for the SimItem. UUID for bundles, tx hash for transactions. seen: HashSet>, diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index 097c2356..80c89a73 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -90,8 +90,7 @@ where let this = self.inner.clone(); // Spawn a blocking task to run the simulations. - let sim_task = - tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_sender)); + let sim_task = tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_sender)); // Either simulation is done, or we time out select! { @@ -109,7 +108,7 @@ where let outcome = best.as_ref()?; // Remove the item from the cache. - let item = self.sim_items.remove(outcome.identifier)?; + let item = self.sim_items.remove(outcome.cache_rank)?; // Accept the cache from the simulation. Arc::get_mut(&mut self.inner) .expect("sims dropped already") @@ -265,10 +264,10 @@ where /// /// This function runs the simulation in a separate thread and waits for /// the result or the deadline to expire. - #[instrument(skip_all, fields(fee_score, tx_hash = %transaction.hash()))] + #[instrument(skip_all, fields(cache_rank, tx_hash = %transaction.hash()))] fn simulate_tx( &self, - fee_score: u128, + cache_rank: u128, transaction: &TxEnvelope, ) -> Result>> { let trevm = self.create_with_block(&self.cfg, &self.block).unwrap(); @@ -303,7 +302,7 @@ where let score = beneficiary_balance.saturating_sub(initial_beneficiary_balance); trace!( - ?fee_score, + ?cache_rank, tx_hash = %transaction.hash(), gas_used = gas_used, score = %score, @@ -315,17 +314,17 @@ where ); // Create the outcome - Ok(SimOutcomeWithCache { identifier: fee_score, score, cache, gas_used }) + Ok(SimOutcomeWithCache { cache_rank, score, cache, gas_used }) } Err(e) => Err(SignetEthBundleError::from(e.into_error())), } } /// Simulates a bundle on the current environment. - #[instrument(skip_all, fields(fee_score, uuid = bundle.replacement_uuid()))] + #[instrument(skip_all, fields(cache_rank, uuid = bundle.replacement_uuid()))] fn simulate_bundle( &self, - fee_score: u128, + cache_rank: u128, bundle: &SignetEthBundle, ) -> Result>> where @@ -346,24 +345,24 @@ where let cache = trevm.into_db().into_cache(); trace!( - ?fee_score, + ?cache_rank, gas_used = gas_used, score = %score, "Bundle simulation successful" ); - Ok(SimOutcomeWithCache { identifier: fee_score, score, cache, gas_used }) + Ok(SimOutcomeWithCache { cache_rank, score, cache, gas_used }) } /// Simulates a transaction or bundle in the context of a block. fn simulate( &self, - fee_score: u128, + cache_rank: u128, item: &SimItem, ) -> Result>> { match item { - SimItem::Bundle(bundle) => self.simulate_bundle(fee_score, bundle), - SimItem::Tx(tx) => self.simulate_tx(fee_score, tx), + SimItem::Bundle(bundle) => self.simulate_bundle(cache_rank, bundle), + SimItem::Tx(tx) => self.simulate_tx(cache_rank, tx), } } @@ -388,7 +387,7 @@ where std::thread::scope(move |scope| { // Spawn a thread per bundle to simulate. - for (fee_score, item) in active_sim.into_iter() { + for (cache_rank, item) in active_sim.into_iter() { let c = candidates.clone(); scope.spawn(move || { @@ -397,7 +396,7 @@ where // If simulation is succesful, send the outcome via the // channel. - match this_ref.simulate(fee_score, &item) { + match this_ref.simulate(cache_rank, &item) { Ok(candidate) => { if candidate.gas_used <= max_gas { // shortcut return on success @@ -412,7 +411,7 @@ where }; // fall through applies to all errors, occurs if // the simulation fails or the gas limit is exceeded. - this_ref.sim_items.remove(fee_score); + this_ref.sim_items.remove(cache_rank); }); } // Drop the TX so that the channel is closed when all threads @@ -424,15 +423,15 @@ where // Update the best score and send it to the channel. let _ = best_tx_sender.send_if_modified(|current| { let current_best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); - let current_fee_score = current.as_ref().map(|c| c.identifier); + let current_cache_rank = current.as_ref().map(|c| c.cache_rank); let changed = candidate.score > current_best_score; if changed { trace!( old_best = ?current_best_score, - old_fee_score = current_fee_score, + old_cache_rank = current_cache_rank, new_best = %candidate.score, - new_fee_score = candidate.identifier, + new_cache_rank = candidate.cache_rank, "Found better candidate" ); *current = Some(candidate); diff --git a/crates/sim/src/outcome.rs b/crates/sim/src/outcome.rs index b1080bbb..57de1c6a 100644 --- a/crates/sim/src/outcome.rs +++ b/crates/sim/src/outcome.rs @@ -7,8 +7,8 @@ use crate::SimItem; /// state changes. #[derive(Debug, Clone)] pub struct SimOutcomeWithCache { - /// The fee score calculated for the transaction or bundle that was simulated; also its key in the cache. - pub identifier: u128, + /// The key for the item in the SimCache. + pub cache_rank: u128, /// The score of the simulation, a [`U256`] value that represents the /// increase in the beneficiary's balance. From 2b3491f98b7045486ee1d376f583bb1239176238 Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Thu, 26 Jun 2025 17:40:53 +0200 Subject: [PATCH 4/7] comments, logging --- crates/sim/src/cache.rs | 2 +- crates/sim/src/env.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/sim/src/cache.rs b/crates/sim/src/cache.rs index 911ea7bb..18124afc 100644 --- a/crates/sim/src/cache.rs +++ b/crates/sim/src/cache.rs @@ -205,7 +205,7 @@ impl SimCache { struct CacheInner { /// Key is the cache_rank, unique ID within the cache && the item's order in the cache. Value is SimItem itself. items: BTreeMap, - /// Key is the unique identifier for the SimItem. UUID for bundles, tx hash for transactions. + /// Key is the unique identifier for the SimItem - the UUID for bundles, tx hash for transactions. seen: HashSet>, } diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index 80c89a73..8baa44fd 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -346,6 +346,7 @@ where trace!( ?cache_rank, + uuid = %bundle.replacement_uuid().unwrap_or_default(), gas_used = gas_used, score = %score, "Bundle simulation successful" From 74c89aaa200d126f71c33edfe226d0cb99316640 Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Fri, 27 Jun 2025 14:08:03 +0200 Subject: [PATCH 5/7] logs, expect --- crates/sim/src/env.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index 8baa44fd..97394d03 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -310,7 +310,7 @@ where halted, halt_reason = ?if halted { halt_reason } else { None }, revert_reason = if !success { reason } else { None }, - "Simulation complete" + "Transaction simulation successful" ); // Create the outcome @@ -346,7 +346,7 @@ where trace!( ?cache_rank, - uuid = %bundle.replacement_uuid().unwrap_or_default(), + uuid = %bundle.replacement_uuid().expect("Bundle must have a replacement UUID"), gas_used = gas_used, score = %score, "Bundle simulation successful" @@ -404,7 +404,7 @@ where let _ = c.blocking_send(candidate); return; } - trace!(gas_used = candidate.gas_used, max_gas, "Gas limit exceeded"); + trace!(gas_used = candidate.gas_used, max_gas, %identifier, "Gas limit exceeded"); } Err(e) => { trace!(?identifier, %e, "Simulation failed"); From a2c3f11cfe5dd8e71424f5ad92746c18b4ac45c4 Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Fri, 27 Jun 2025 17:47:25 +0200 Subject: [PATCH 6/7] pr comments --- crates/sim/src/env.rs | 18 +++++++++--------- crates/sim/src/outcome.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/sim/src/env.rs b/crates/sim/src/env.rs index 97394d03..4db08282 100644 --- a/crates/sim/src/env.rs +++ b/crates/sim/src/env.rs @@ -85,12 +85,12 @@ where /// Run a simulation round, returning the best item. #[instrument(skip(self))] pub async fn sim_round(&mut self, max_gas: u64) -> Option { - let (best_tx_sender, mut best_tx_receiver) = watch::channel(None); + let (best_tx, mut best_watcher) = watch::channel(None); let this = self.inner.clone(); // Spawn a blocking task to run the simulations. - let sim_task = tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx_sender)); + let sim_task = tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx)); // Either simulation is done, or we time out select! { @@ -103,7 +103,7 @@ where } // Check what the current best outcome is. - let best = best_tx_receiver.borrow_and_update(); + let best = best_watcher.borrow_and_update(); trace!(score = %best.as_ref().map(|candidate| candidate.score).unwrap_or_default(), "Read outcome from channel"); let outcome = best.as_ref()?; @@ -310,7 +310,7 @@ where halted, halt_reason = ?if halted { halt_reason } else { None }, revert_reason = if !success { reason } else { None }, - "Transaction simulation successful" + "Transaction simulation complete" ); // Create the outcome @@ -371,7 +371,7 @@ where fn sim_round( self: Arc, max_gas: u64, - best_tx_sender: watch::Sender>, + best_tx: watch::Sender>, ) { // Pull the `n` best items from the cache. let active_sim = self.sim_items.read_best(self.concurrency_limit); @@ -422,14 +422,14 @@ where // Wait for each thread to finish. Find the best outcome. while let Some(candidate) = candidates_rx.blocking_recv() { // Update the best score and send it to the channel. - let _ = best_tx_sender.send_if_modified(|current| { - let current_best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); + let _ = best_tx.send_if_modified(|current| { + let best_score = current.as_ref().map(|c| c.score).unwrap_or_default(); let current_cache_rank = current.as_ref().map(|c| c.cache_rank); - let changed = candidate.score > current_best_score; + let changed = candidate.score > best_score; if changed { trace!( - old_best = ?current_best_score, + old_best = ?best_score, old_cache_rank = current_cache_rank, new_best = %candidate.score, new_cache_rank = candidate.cache_rank, diff --git a/crates/sim/src/outcome.rs b/crates/sim/src/outcome.rs index 57de1c6a..05942993 100644 --- a/crates/sim/src/outcome.rs +++ b/crates/sim/src/outcome.rs @@ -7,7 +7,7 @@ use crate::SimItem; /// state changes. #[derive(Debug, Clone)] pub struct SimOutcomeWithCache { - /// The key for the item in the SimCache. + /// The key for the item in the [`SimCache`]. pub cache_rank: u128, /// The score of the simulation, a [`U256`] value that represents the From ba6c12efcd41830ddcaf03d21bd32ef7c1608ac9 Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Fri, 27 Jun 2025 17:50:16 +0200 Subject: [PATCH 7/7] docs --- crates/sim/src/cache.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sim/src/cache.rs b/crates/sim/src/cache.rs index 18124afc..3223a0cc 100644 --- a/crates/sim/src/cache.rs +++ b/crates/sim/src/cache.rs @@ -203,9 +203,9 @@ impl SimCache { /// Internal cache data, meant to be protected by a lock. struct CacheInner { - /// Key is the cache_rank, unique ID within the cache && the item's order in the cache. Value is SimItem itself. + /// Key is the cache_rank, unique ID within the cache && the item's order in the cache. Value is [`SimItem`] itself. items: BTreeMap, - /// Key is the unique identifier for the SimItem - the UUID for bundles, tx hash for transactions. + /// Key is the unique identifier for the [`SimItem`] - the UUID for bundles, tx hash for transactions. seen: HashSet>, }