Skip to content

Commit 5eaa4ee

Browse files
authored
new OrderStatistics logs (#639)
## πŸ“ Summary New simple logs about numbers of orders available, considered and failed. ## βœ… I have completed the following steps: * [X] Run `make lint` * [X] Run `make test` * [ ] Added tests (if applicable)
1 parent 2036625 commit 5eaa4ee

File tree

9 files changed

+99
-10
lines changed

9 files changed

+99
-10
lines changed

β€Žcrates/rbuilder/src/bin/dummy-builder.rsβ€Ž

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ use rbuilder::{
3434
LiveBuilder,
3535
},
3636
mev_boost::RelayClient,
37-
primitives::{mev_boost::MevBoostRelaySlotInfoProvider, SimulatedOrder},
37+
primitives::{
38+
mev_boost::MevBoostRelaySlotInfoProvider, order_statistics::OrderStatistics, SimulatedOrder,
39+
},
3840
provider::StateProviderFactory,
3941
utils::{ProviderFactoryReopener, Signer},
4042
};
@@ -213,13 +215,17 @@ impl DummyBuildingAlgorithm {
213215
let block_state = provider
214216
.history_by_block_hash(ctx.attributes.parent)?
215217
.into();
216-
218+
let mut order_statistics = OrderStatistics::new();
219+
for sim_order in &orders {
220+
order_statistics.add(&sim_order.order);
221+
}
217222
let mut block_building_helper = BlockBuildingHelperFromProvider::new(
218223
block_state,
219224
ctx.clone(),
220225
&mut local_ctx,
221226
BUILDER_NAME.to_string(),
222227
false,
228+
order_statistics,
223229
CancellationToken::new(),
224230
)?;
225231

β€Žcrates/rbuilder/src/building/block_orders/prioritized_order_store.rsβ€Ž

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloy_primitives::Address;
55
use priority_queue::PriorityQueue;
66

77
use crate::{
8-
primitives::{AccountNonce, Nonce, OrderId, SimulatedOrder},
8+
primitives::{order_statistics::OrderStatistics, AccountNonce, Nonce, OrderId, SimulatedOrder},
99
telemetry::mark_order_not_ready_for_immediate_inclusion,
1010
};
1111

@@ -39,6 +39,8 @@ pub struct PrioritizedOrderStore<OrderPriorityType> {
3939
pending_orders: HashMap<AccountNonce, Vec<OrderId>>,
4040
/// Id -> order for all orders we manage. Carefully maintained by remove/insert
4141
orders: HashMap<OrderId, Arc<SimulatedOrder>>,
42+
/// Everything in orders
43+
orders_statistics: OrderStatistics,
4244
}
4345

4446
impl<OrderPriorityType: OrderPriority> PrioritizedOrderStore<OrderPriorityType> {
@@ -53,9 +55,14 @@ impl<OrderPriorityType: OrderPriority> PrioritizedOrderStore<OrderPriorityType>
5355
onchain_nonces,
5456
pending_orders: HashMap::default(),
5557
orders: HashMap::default(),
58+
orders_statistics: Default::default(),
5659
}
5760
}
5861

62+
pub fn orders_statistics(&self) -> OrderStatistics {
63+
self.orders_statistics.clone()
64+
}
65+
5966
pub fn pop_order(&mut self) -> Option<Arc<SimulatedOrder>> {
6067
let (id, _) = self.main_queue.pop()?;
6168

@@ -67,7 +74,7 @@ impl<OrderPriorityType: OrderPriority> PrioritizedOrderStore<OrderPriorityType>
6774

6875
/// Clean up after some order was removed from main_queue
6976
fn remove_poped_order(&mut self, id: &OrderId) -> Option<Arc<SimulatedOrder>> {
70-
let sim_order = self.orders.remove(id)?;
77+
let sim_order = self.remove_from_orders(id)?;
7178
for Nonce { address, .. } in sim_order.order.nonces() {
7279
match self.main_queue_nonces.entry(address) {
7380
Entry::Occupied(mut entry) => {
@@ -135,7 +142,7 @@ impl<OrderPriorityType: OrderPriority> PrioritizedOrderStore<OrderPriorityType>
135142
if let Some(pending) = self.pending_orders.remove(new_nonce) {
136143
let orders = pending
137144
.iter()
138-
.filter_map(|id| self.orders.remove(id))
145+
.filter_map(|id| self.remove_from_orders(id))
139146
.collect::<Vec<_>>();
140147
for order in orders {
141148
self.insert_order(order);
@@ -147,6 +154,15 @@ impl<OrderPriorityType: OrderPriority> PrioritizedOrderStore<OrderPriorityType>
147154
pub fn get_all_orders(&self) -> Vec<Arc<SimulatedOrder>> {
148155
self.orders.values().cloned().collect()
149156
}
157+
158+
/// Removes from self.orders and updates statistics
159+
fn remove_from_orders(&mut self, id: &OrderId) -> Option<Arc<SimulatedOrder>> {
160+
let res = self.orders.remove(id);
161+
if let Some(sim_order) = &res {
162+
self.orders_statistics.remove(&sim_order.order);
163+
}
164+
res
165+
}
150166
}
151167

152168
impl<OrderPriorityType: OrderPriority> SimulatedOrderSink
@@ -196,6 +212,8 @@ impl<OrderPriorityType: OrderPriority> SimulatedOrderSink
196212
}
197213
}
198214
}
215+
self.orders_statistics.add(&sim_order.order);
216+
// We don't check the result to update orders_statistics since we already checked !self.orders.contains_key
199217
self.orders.insert(sim_order.id(), sim_order);
200218
}
201219

@@ -204,6 +222,6 @@ impl<OrderPriorityType: OrderPriority> SimulatedOrderSink
204222
if self.main_queue.remove(&id).is_some() {
205223
self.remove_poped_order(&id);
206224
}
207-
self.orders.remove(&id)
225+
self.remove_from_orders(&id)
208226
}
209227
}

β€Žcrates/rbuilder/src/building/builders/block_building_helper.rsβ€Ž

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
EstimatePayoutGasErr, ExecutionError, ExecutionResult, FinalizeError, FinalizeResult,
1717
PartialBlock, ThreadBlockBuildingContext,
1818
},
19-
primitives::{SimValue, SimulatedOrder},
19+
primitives::{order_statistics::OrderStatistics, SimValue, SimulatedOrder},
2020
telemetry::{self, add_block_fill_time, add_order_simulation_time},
2121
utils::{check_block_hash_reader_health, elapsed_ms, HistoricalBlockError},
2222
};
@@ -192,6 +192,7 @@ impl BlockBuildingHelperFromProvider {
192192
local_ctx: &mut ThreadBlockBuildingContext,
193193
builder_name: String,
194194
discard_txs: bool,
195+
available_orders_statistics: OrderStatistics,
195196
cancel_on_fatal_error: CancellationToken,
196197
) -> Result<Self, BlockBuildingHelperError> {
197198
let last_committed_block = building_ctx.block() - 1;
@@ -220,14 +221,16 @@ impl BlockBuildingHelperFromProvider {
220221
Some(payout_tx_gas)
221222
};
222223

224+
let mut built_block_trace = BuiltBlockTrace::new();
225+
built_block_trace.available_orders_statistics = available_orders_statistics;
223226
Ok(Self {
224227
_fee_recipient_balance_start: fee_recipient_balance_start,
225228
block_state,
226229
partial_block,
227230
payout_tx_gas,
228231
builder_name,
229232
building_ctx,
230-
built_block_trace: BuiltBlockTrace::new(),
233+
built_block_trace,
231234
cancel_on_fatal_error,
232235
})
233236
}
@@ -338,6 +341,7 @@ impl BlockBuildingHelper for BlockBuildingHelperFromProvider {
338341
order: &SimulatedOrder,
339342
result_filter: &dyn Fn(&SimValue) -> Result<(), ExecutionError>,
340343
) -> Result<Result<&ExecutionResult, ExecutionError>, CriticalCommitOrderError> {
344+
self.built_block_trace.add_considered_order(order);
341345
let start = Instant::now();
342346
let result = self.partial_block.commit_order(
343347
order,
@@ -359,6 +363,7 @@ impl BlockBuildingHelper for BlockBuildingHelperFromProvider {
359363
Err(err) => {
360364
self.built_block_trace
361365
.modify_payment_when_no_signer_error(&err);
366+
self.built_block_trace.add_failed_order(order);
362367
(Ok(Err(err)), false)
363368
}
364369
},

β€Žcrates/rbuilder/src/building/builders/ordering_builder.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ impl OrderingBuilderContext {
253253
&mut self.local_ctx,
254254
self.builder_name.clone(),
255255
self.config.discard_txs,
256+
block_orders.orders_statistics(),
256257
cancel_block,
257258
)?;
258259

β€Žcrates/rbuilder/src/building/builders/parallel_builder/block_building_result_assembler.rsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
},
2121
BlockBuildingContext, ThreadBlockBuildingContext,
2222
},
23+
primitives::order_statistics::OrderStatistics,
2324
telemetry::mark_builder_considers_order,
2425
utils::elapsed_ms,
2526
};
@@ -200,6 +201,7 @@ impl BlockBuildingResultAssembler {
200201
&mut self.local_ctx,
201202
self.builder_name.clone(),
202203
self.discard_txs,
204+
OrderStatistics::default(),
203205
self.cancellation_token.clone(),
204206
)?;
205207
block_building_helper.set_trace_orders_closed_at(orders_closed_at);
@@ -272,6 +274,7 @@ impl BlockBuildingResultAssembler {
272274
&mut self.local_ctx,
273275
String::from("backtest_builder"),
274276
self.discard_txs,
277+
OrderStatistics::default(),
275278
CancellationToken::new(),
276279
)?;
277280

β€Žcrates/rbuilder/src/building/built_block_trace.rsβ€Ž

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::{BundleErr, ExecutionError, ExecutionResult, OrderErr};
2-
use crate::primitives::{Order, OrderId, OrderReplacementKey};
2+
use crate::primitives::{
3+
order_statistics::OrderStatistics, Order, OrderId, OrderReplacementKey, SimulatedOrder,
4+
};
35
use ahash::{AHasher, HashMap, HashSet};
46
use alloy_primitives::{Address, TxHash, U256};
57
use std::{collections::hash_map, hash::Hasher, time::Duration};
@@ -8,7 +10,7 @@ use time::OffsetDateTime;
810
/// Structs for recording data about a built block, such as what bundles were included, and where txs came from.
911
/// Trace can be used to verify bundle invariants.
1012
11-
#[derive(Debug, Clone, PartialEq, Eq)]
13+
#[derive(Debug, Clone)]
1214
pub struct BuiltBlockTrace {
1315
pub included_orders: Vec<ExecutionResult>,
1416
/// How much we bid (pay to the validator)
@@ -28,6 +30,12 @@ pub struct BuiltBlockTrace {
2830
pub root_hash_time: Duration,
2931
/// Value we saw in the competition when we decided to make this bid.
3032
pub seen_competition_bid: Option<U256>,
33+
/// Orders we had available to build the block (we might have not use all of them because of timeouts)
34+
pub available_orders_statistics: OrderStatistics,
35+
/// Every call to BlockBuildingHelper::commit_order impacts here.
36+
pub considered_orders_statistics: OrderStatistics,
37+
/// Anything we call BlockBuildingHelper::commit_order on but didn't include (redundant with considered_orders_statistics-included_orders)
38+
pub failed_orders_statistics: OrderStatistics,
3139
}
3240

3341
impl Default for BuiltBlockTrace {
@@ -62,6 +70,9 @@ impl BuiltBlockTrace {
6270
finalize_time: Duration::from_secs(0),
6371
root_hash_time: Duration::from_secs(0),
6472
seen_competition_bid: None,
73+
considered_orders_statistics: Default::default(),
74+
failed_orders_statistics: Default::default(),
75+
available_orders_statistics: Default::default(),
6576
}
6677
}
6778

@@ -77,6 +88,16 @@ impl BuiltBlockTrace {
7788
self.included_orders.push(execution_result);
7889
}
7990

91+
/// Call before commit_order
92+
pub fn add_considered_order(&mut self, sim_order: &SimulatedOrder) {
93+
self.considered_orders_statistics.add(&sim_order.order);
94+
}
95+
96+
/// Call after a commit_order Err
97+
pub fn add_failed_order(&mut self, sim_order: &SimulatedOrder) {
98+
self.failed_orders_statistics.add(&sim_order.order);
99+
}
100+
80101
/// Call after a commit_order error
81102
pub fn modify_payment_when_no_signer_error(&mut self, err: &ExecutionError) {
82103
if let ExecutionError::OrderError(OrderErr::Bundle(BundleErr::NoSigner)) = err {

β€Žcrates/rbuilder/src/live_builder/block_output/relay_submit.rsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ async fn run_submit_to_relays_job(
242242
);
243243
info!(
244244
parent: &submission_span,
245+
available_orders_statistics = ?block.trace.available_orders_statistics,
246+
considered_orders_statistics = ?block.trace.considered_orders_statistics,
247+
failed_orders_statistics = ?block.trace.failed_orders_statistics,
245248
"Submitting bid",
246249
);
247250
let relay_filter = get_relay_filter_and_update_metrics(

β€Žcrates/rbuilder/src/primitives/mod.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub mod fmt;
44
pub mod mev_boost;
55
pub mod order_builder;
6+
pub mod order_statistics;
67
pub mod serialize;
78
mod test_data_generator;
89

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::primitives::Order;
2+
3+
/// Simple struct to count orders by type.
4+
#[derive(Clone, Debug, Default)]
5+
pub struct OrderStatistics {
6+
tx_count: i32,
7+
bundle_count: i32,
8+
sbundle_count: i32,
9+
}
10+
11+
impl OrderStatistics {
12+
pub fn new() -> Self {
13+
Self::default()
14+
}
15+
16+
pub fn add(&mut self, order: &Order) {
17+
match order {
18+
Order::Bundle(_) => self.tx_count += 1,
19+
Order::Tx(_) => self.bundle_count += 1,
20+
Order::ShareBundle(_) => self.sbundle_count += 1,
21+
}
22+
}
23+
24+
pub fn remove(&mut self, order: &Order) {
25+
match order {
26+
Order::Bundle(_) => self.tx_count -= 1,
27+
Order::Tx(_) => self.bundle_count -= 1,
28+
Order::ShareBundle(_) => self.sbundle_count -= 1,
29+
}
30+
}
31+
}

0 commit comments

Comments
Β (0)