Skip to content

Commit 01d39a5

Browse files
committed
fix: fill state candidates
1 parent 039ce0d commit 01d39a5

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

crates/bundle/src/send/driver.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ where
4040
pub bundle_orders: AggregateOrders,
4141
}
4242

43-
impl<Db, Insp> From<signet_evm::EvmNeedsTx<Db, Insp>> for DriverOutput<Db, Insp>
44-
where
45-
Db: Database,
46-
Insp: Inspector<Ctx<Db>>,
47-
{
48-
fn from(evm: signet_evm::EvmNeedsTx<Db, Insp>) -> Self {
49-
Self {
50-
host_evm: Some(evm),
51-
total_gas_used: 0,
52-
beneficiary_balance_increase: U256::ZERO,
53-
bundle_fills: AggregateFills::default(),
54-
bundle_orders: AggregateOrders::default(),
55-
}
56-
}
57-
}
58-
5943
impl<Db, Insp> DriverOutput<Db, Insp>
6044
where
6145
Db: Database,
@@ -141,9 +125,8 @@ where
141125
/// The bundle to apply.
142126
bundle: &'a SignetEthBundle,
143127

144-
/// Reference to the fill state to check against. When we modify it, we'll
145-
/// clone and take a running total.
146-
fill_state: Cow<'b, AggregateFills>,
128+
/// Reference to the fill state to check against.
129+
pub fill_state: Cow<'b, AggregateFills>,
147130

148131
/// Execution deadline for this bundle. This limits the total WALLCLOCK
149132
/// time spent simulating the bundle.
@@ -178,7 +161,18 @@ where
178161
deadline: std::time::Instant,
179162
fill_state: Cow<'b, AggregateFills>,
180163
) -> Self {
181-
Self { bundle, fill_state, deadline, output: host_evm.into() }
164+
Self {
165+
bundle,
166+
fill_state,
167+
deadline,
168+
output: DriverOutput {
169+
host_evm: Some(host_evm),
170+
total_gas_used: 0,
171+
beneficiary_balance_increase: U256::ZERO,
172+
bundle_fills: AggregateFills::default(),
173+
bundle_orders: AggregateOrders::default(),
174+
},
175+
}
182176
}
183177

184178
/// Get a reference to the bundle.
@@ -258,7 +252,6 @@ where
258252
// We simply run all host transactions first, accumulating their state
259253
// changes into the host_evm's state. If any reverts, we error out the
260254
// simulation.
261-
let mut host_bundle_fills = AggregateFills::default();
262255
for tx in host_txs.into_iter() {
263256
self.output.host_evm = Some(trevm_try!(
264257
self.output
@@ -275,13 +268,14 @@ where
275268
EVMError::Custom("host transaction reverted".to_string())
276269
);
277270

271+
// The host fills go in the bundle fills.
278272
let host_fills = htrevm
279273
.inner_mut_unchecked()
280274
.inspector
281275
.as_mut_detector()
282276
.take_aggregates()
283277
.0;
284-
host_bundle_fills.absorb(&host_fills);
278+
self.output.bundle_fills.absorb(&host_fills);
285279

286280
Ok(htrevm.accept_state())
287281
})
@@ -292,8 +286,6 @@ where
292286
trevm
293287
));
294288
}
295-
// Absorb the host fills into our running total.
296-
self.fill_state.to_mut().absorb(&host_bundle_fills);
297289

298290
// -- ROLLUP PORTION --
299291
for tx in txs.into_iter() {
@@ -324,13 +316,18 @@ where
324316
let (tx_fills, tx_orders) =
325317
t.inner_mut_unchecked().inspector.as_mut_detector().take_aggregates();
326318

319+
// These clones are inefficient. We can optimize later if
320+
// needed.
321+
let mut candidate_fills = self.output.bundle_fills.clone();
322+
let mut candidate_orders = self.output.bundle_orders.clone();
323+
324+
// The candidate is the updated
325+
candidate_fills.absorb(&tx_fills);
326+
candidate_orders.absorb(&tx_orders);
327+
327328
// Then we check that the fills are sufficient against the
328329
// provided fill state. This does nothing on error.
329-
if self
330-
.fill_state
331-
.to_mut()
332-
.checked_remove_ru_tx_events(&tx_fills, &tx_orders)
333-
.is_err()
330+
if self.fill_state.check_ru_tx_events(&candidate_fills, &candidate_orders).is_err()
334331
{
335332
if self.bundle.reverting_tx_hashes().contains(tx_hash) {
336333
debug!("transaction marked as revertible, reverting");
@@ -342,9 +339,9 @@ where
342339
}
343340
}
344341

345-
// now we absorb the fills and orders into our running totals.
346-
self.output.absorb(&tx_fills, &tx_orders);
347-
self.output.use_gas(gas_used);
342+
// Now we accept the fills and order candidates
343+
self.output.bundle_fills = candidate_fills;
344+
self.output.bundle_orders = candidate_orders;
348345
} else {
349346
// EVM Execution did not succeed.
350347
// If not success, we are in a revert or halt. If the tx is
@@ -354,11 +351,11 @@ where
354351
debug!("transaction reverted, not marked as revertible");
355352
return Err(t.errored(BundleError::BundleReverted.into()));
356353
}
357-
self.output.use_gas(gas_used);
358354
}
359355

360356
// If we did not shortcut return/continue, we accept the state
361357
// changes from this transaction.
358+
self.output.use_gas(gas_used);
362359
trevm = t.accept_state()
363360
}
364361

crates/sim/src/env/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ where
100100
Arc::get_mut(&mut self.inner)
101101
.expect("sims dropped already")
102102
.rollup_mut()
103-
.accept_aggregates(&outcome.fills, &outcome.orders)
103+
.accept_aggregates(&outcome.bundle_fills, &outcome.bundle_orders)
104104
.expect("checked during simulation");
105105

106106
Some(SimulatedItem { gas_used: outcome.gas_used, score: outcome.score, item })

crates/sim/src/env/sim_env.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ where
136136
// insufficient, we error out. Otherwise we'll return them as
137137
// part of the `SimOutcomeWithCache`, to allow _later_ stages to
138138
// process them (e.g., to update the fill state).
139-
let (fills, orders) =
139+
let (bundle_fills, bundle_orders) =
140140
trevm.inner_mut_unchecked().inspector.as_mut_detector().take_aggregates();
141141

142-
self.rollup.fill_state().check_ru_tx_events(&fills, &orders)?;
142+
self.rollup.fill_state().check_ru_tx_events(&bundle_fills, &bundle_orders)?;
143143

144144
// We will later commit these to the trevm DB when the
145145
// SimOutcome is accepted.
@@ -169,8 +169,8 @@ where
169169
rollup_cache: cache,
170170
host_cache: Default::default(),
171171
gas_used,
172-
fills,
173-
orders,
172+
bundle_fills,
173+
bundle_orders,
174174
})
175175
}
176176
Err(e) => Err(SignetEthBundleError::from(e.into_error())),
@@ -206,7 +206,12 @@ where
206206
let score = driver.beneficiary_balance_increase();
207207
let outputs = driver.into_outputs();
208208

209-
let rollup_cache = trevm.into_db().into_cache();
209+
// This is redundant with the driver, however, we double check here.
210+
// If perf is hit too much we can remove.
211+
self.rollup
212+
.fill_state()
213+
.check_ru_tx_events(&outputs.bundle_fills, &outputs.bundle_orders)?;
214+
210215
let host_cache = outputs.host_evm.map(|evm| evm.into_db().into_cache()).unwrap_or_default();
211216
trace!(
212217
gas_used = outputs.total_gas_used,
@@ -217,11 +222,11 @@ where
217222
Ok(SimOutcomeWithCache {
218223
cache_rank,
219224
score: score.to(),
220-
rollup_cache,
225+
rollup_cache: trevm.into_db().into_cache(),
221226
host_cache,
222227
gas_used: outputs.total_gas_used,
223-
fills: outputs.bundle_fills,
224-
orders: outputs.bundle_orders,
228+
bundle_fills: outputs.bundle_fills,
229+
bundle_orders: outputs.bundle_orders,
225230
})
226231
}
227232

@@ -254,13 +259,13 @@ where
254259
let _og = outer.enter();
255260

256261
// to be used in the scope
257-
let this_ref = &self;
262+
let this_ref = self.clone();
258263

259-
std::thread::scope(move |scope| {
264+
std::thread::scope(|scope| {
260265
// Spawn a thread per bundle to simulate.
261266
for (cache_rank, item) in active_sim.into_iter() {
262267
let c = candidates.clone();
263-
268+
let this_ref = this_ref.clone();
264269
scope.spawn(move || {
265270
let identifier = item.identifier();
266271
let _ig = trace_span!(parent: outer_ref, "sim_task", %identifier).entered();
@@ -288,7 +293,6 @@ where
288293
// Drop the TX so that the channel is closed when all threads
289294
// are done.
290295
drop(candidates);
291-
292296
// Wait for each thread to finish. Find the best outcome.
293297
while let Some(candidate) = candidates_rx.blocking_recv() {
294298
// Update the best score and send it to the channel.

crates/sim/src/outcome.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
use crate::SimItem;
12
use alloy::primitives::U256;
23
use signet_types::{AggregateFills, AggregateOrders};
34
use trevm::revm::database::Cache;
45

5-
use crate::SimItem;
6-
76
/// A simulation outcome that includes the score, gas used, and a cache of
87
/// state changes.
98
#[derive(Debug, Clone)]
@@ -27,10 +26,10 @@ pub struct SimOutcomeWithCache {
2726
pub host_cache: Cache,
2827

2928
/// The aggregate fills after simulation.
30-
pub fills: AggregateFills,
29+
pub bundle_fills: AggregateFills,
3130

3231
/// The aggregate orders after simulation.
33-
pub orders: AggregateOrders,
32+
pub bundle_orders: AggregateOrders,
3433
}
3534

3635
/// An item after simulation, containing the score and gas used.

0 commit comments

Comments
 (0)