Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Types used to communicate with the bidding service via iceoryx.
//! They are mostly mappings of the originals but supporting ZeroCopySend.
use alloy_primitives::U256;
use alloy_primitives::{I256, U256};
use bid_scraper::types::ScrapedRelayBlockBid;
use iceoryx2::prelude::ZeroCopySend;
use iceoryx2_bb_container::byte_string::FixedSizeByteString;
Expand Down Expand Up @@ -196,6 +196,7 @@ pub struct SlotBidderSealBidCommandRPC {
pub session_id: u64,
pub block_id: u64,
pub payout_tx_value: [u8; U256_DATA_LENGTH],
pub subsidy: [u8; U256_DATA_LENGTH],
pub seen_competition_bid: Option<[u8; U256_DATA_LENGTH]>,
/// When this bid is a reaction so some event (eg: new block, new competition bid) we put here
/// the creation time of that event so we can measure our reaction time.
Expand All @@ -213,6 +214,7 @@ impl From<SlotBidderSealBidCommandWithSessionId> for SlotBidderSealBidCommandRPC
.0
.trigger_creation_time
.map(offset_datetime_to_timestamp_us),
subsidy: value.0.subsidy.to_le_bytes(),
}
}
}
Expand All @@ -222,6 +224,7 @@ impl From<SlotBidderSealBidCommandRPC> for SlotBidderSealBidCommand {
SlotBidderSealBidCommand {
block_id: BuiltBlockId(val.block_id),
payout_tx_value: U256::from_le_bytes(val.payout_tx_value),
subsidy: I256::from_le_bytes(val.subsidy),
seen_competition_bid: val.seen_competition_bid.map(|k| U256::from_le_bytes(k)),
trigger_creation_time: val
.trigger_creation_time_us
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl BidObserver for BestTrueValueObserver {
slot_data.slot(),
built_block_trace.true_bid_value,
built_block_trace.bid_value,
built_block_trace.subsidy,
builder_name,
slot_data.timestamp().unix_timestamp() as u64,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! This module is responsible for syncing the best true value bid between the local state and redis.

use alloy_primitives::U256;
use alloy_primitives::{I256, U256};

use parking_lot::Mutex;
use rbuilder::utils::{
i256decimal_serde_helper,
reconnect::{run_loop_with_reconnect, RunCommand},
u256decimal_serde_helper,
};
Expand All @@ -27,6 +28,9 @@ pub struct BuiltBlockInfo {
/// Bid we made to the relay.
#[serde(with = "u256decimal_serde_helper")]
pub bid: U256,
#[serde(with = "i256decimal_serde_helper")]
pub subsidy: I256,

pub builder: String,
pub slot_end_timestamp: u64,
}
Expand All @@ -37,6 +41,7 @@ impl BuiltBlockInfo {
slot_number: u64,
best_true_value: U256,
bid: U256,
subsidy: I256,
builder: String,
slot_end_timestamp: u64,
) -> Self {
Expand All @@ -46,6 +51,7 @@ impl BuiltBlockInfo {
slot_number,
best_true_value,
bid,
subsidy,
builder,
slot_end_timestamp,
}
Expand Down
27 changes: 24 additions & 3 deletions crates/rbuilder/src/building/builders/block_building_helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{utils::format_ether, Address, TxHash, U256};
use alloy_primitives::{utils::format_ether, Address, TxHash, I256, U256};
use reth_provider::StateProvider;
use std::{
cmp::max,
Expand Down Expand Up @@ -68,10 +68,12 @@ pub trait BlockBuildingHelper: Send + Sync {
/// Finalize block for submission.
/// if adjust_finalized_block is implemented, finalize_blocks should prepare helper
/// for faster adjustments.
/// subsidy is how much of payout_tx_value we consider to be subsidy.
fn finalize_block(
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError>;

Expand All @@ -96,6 +98,7 @@ pub trait BlockBuildingHelper: Send + Sync {
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError>;
}
Expand Down Expand Up @@ -337,6 +340,7 @@ impl<
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
adjust_finalized_block: bool,
finalize_revert_state: &mut FinalizeRevertStateCurrentIteration,
) -> Result<(), BlockBuildingHelperError> {
Expand Down Expand Up @@ -364,6 +368,7 @@ impl<
.unwrap_or_default();

self.built_block_trace.bid_value = max(bid_value, fee_recipient_balance_diff);
self.built_block_trace.subsidy = subsidy;
self.built_block_trace.true_bid_value = true_value;
self.built_block_trace.mev_blocker_price = self.building_context().mev_blocker_price;
Ok(())
Expand All @@ -373,6 +378,7 @@ impl<
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
adjust_finalized_block: bool,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
Expand Down Expand Up @@ -403,6 +409,7 @@ impl<
self.finalize_block_execution(
local_ctx,
payout_tx_value,
subsidy,
adjust_finalized_block,
&mut finalize_adjustment_state.revert_state,
)?;
Expand Down Expand Up @@ -590,9 +597,16 @@ impl<
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
self.finalize_block_impl(local_ctx, payout_tx_value, seen_competition_bid, false)
self.finalize_block_impl(
local_ctx,
payout_tx_value,
subsidy,
seen_competition_bid,
false,
)
}

fn built_block_trace(&self) -> &BuiltBlockTrace {
Expand Down Expand Up @@ -624,8 +638,15 @@ impl<
&mut self,
local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
self.finalize_block_impl(local_ctx, payout_tx_value, seen_competition_bid, true)
self.finalize_block_impl(
local_ctx,
payout_tx_value,
subsidy,
seen_competition_bid,
true,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::{Duration, Instant},
};

use alloy_primitives::{utils::format_ether, U256};
use alloy_primitives::{utils::format_ether, I256, U256};

use crate::{
building::{builders::block_building_helper::BlockBuildingHelper, ThreadBlockBuildingContext},
Expand Down Expand Up @@ -208,6 +208,7 @@ impl BlockBuildingHelper for BlockBuildingHelperStatsLogger<'_> {
&mut self,
_local_ctx: &mut crate::building::ThreadBlockBuildingContext,
_payout_tx_value: alloy_primitives::U256,
_subsidy: alloy_primitives::I256,
_seen_competition_bid: Option<alloy_primitives::U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
panic!("finalize_block not implemented. This is only for testing.");
Expand Down Expand Up @@ -238,6 +239,7 @@ impl BlockBuildingHelper for BlockBuildingHelperStatsLogger<'_> {
&mut self,
_local_ctx: &mut ThreadBlockBuildingContext,
_payout_tx_value: U256,
_subsidy: I256,
_seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
unimplemented!()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
provider::RootHasher,
roothash::RootHashError,
};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_primitives::{Address, Bytes, B256, I256, U256};
use eth_sparse_mpt::utils::{HashMap, HashSet};
use rbuilder_primitives::{order_statistics::OrderStatistics, SimValue, SimulatedOrder};
use reth_primitives::SealedBlock;
Expand Down Expand Up @@ -85,11 +85,13 @@ impl BlockBuildingHelper for MockBlockBuildingHelper {
&mut self,
_local_ctx: &mut ThreadBlockBuildingContext,
payout_tx_value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
self.built_block_trace.update_orders_sealed_at();
self.built_block_trace.seen_competition_bid = seen_competition_bid;
self.built_block_trace.bid_value = payout_tx_value;
self.built_block_trace.subsidy = subsidy;
let block = Block {
builder_name: "BlockBuildingHelper".to_string(),
trace: self.built_block_trace.clone(),
Expand Down Expand Up @@ -127,6 +129,7 @@ impl BlockBuildingHelper for MockBlockBuildingHelper {
&mut self,
_local_ctx: &mut ThreadBlockBuildingContext,
_payout_tx_value: U256,
_subsidy: I256,
_seen_competition_bid: Option<U256>,
) -> Result<FinalizeBlockResult, BlockBuildingHelperError> {
unimplemented!()
Expand Down
3 changes: 2 additions & 1 deletion crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
utils::NonceCache,
};
use ahash::{HashMap, HashSet};
use alloy_primitives::I256;
use derivative::Derivative;
use rbuilder_primitives::{AccountNonce, OrderId, SimValue, SimulatedOrder};
use reth_provider::StateProvider;
Expand Down Expand Up @@ -198,7 +199,7 @@ where

let payout_tx_value = block_builder.true_block_value()?;
let finalize_block_result =
block_builder.finalize_block(&mut local_ctx, payout_tx_value, None)?;
block_builder.finalize_block(&mut local_ctx, payout_tx_value, I256::ZERO, None)?;
Ok(finalize_block_result.block)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod order_intake_store;
pub mod results_aggregator;
pub mod simulation_cache;
pub mod task;
use alloy_primitives::I256;
pub use groups::*;

use ahash::HashMap;
Expand Down Expand Up @@ -377,6 +378,7 @@ where
let finalize_block_result = block_building_helper.finalize_block(
&mut block_building_result_assembler.local_ctx,
payout_tx_value,
I256::ZERO,
None,
)?;
let building_duration = building_start.elapsed();
Expand Down
5 changes: 4 additions & 1 deletion crates/rbuilder/src/building/built_block_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::building::builders::BuiltBlockId;

use super::ExecutionResult;
use ahash::{AHasher, HashMap, HashSet};
use alloy_primitives::{Address, TxHash, U256};
use alloy_primitives::{Address, TxHash, I256, U256};
use rbuilder_primitives::{
order_statistics::OrderStatistics, Order, OrderId, OrderReplacementKey, SimulatedOrder,
};
Expand All @@ -18,6 +18,8 @@ pub struct BuiltBlockTrace {
pub included_orders: Vec<ExecutionResult>,
/// How much we bid (pay to the validator)
pub bid_value: U256,
/// Subsidy used in the bid.
pub subsidy: I256,
/// coinbase balance delta before the payout tx.
pub coinbase_reward: U256,
/// True block value (coinbase balance delta) excluding the cost of the payout to validator
Expand Down Expand Up @@ -96,6 +98,7 @@ impl BuiltBlockTrace {
sent_to_sealer: OffsetDateTime::now_utc(),
picked_by_sealer_at: OffsetDateTime::now_utc(),
build_block_id,
subsidy: I256::ZERO,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use alloy_primitives::{BlockHash, BlockNumber, U256};
use alloy_primitives::{BlockHash, BlockNumber, I256, U256};
use bid_scraper::{
bid_sender::{BidSender, BidSenderError},
types::ScrapedRelayBlockBid,
Expand Down Expand Up @@ -104,6 +104,8 @@ impl BuiltBlockDescriptorForSlotBidder {
pub struct SlotBidderSealBidCommand {
pub block_id: BuiltBlockId,
pub payout_tx_value: U256,
/// Subsidy used in the bid.
pub subsidy: I256,
pub seen_competition_bid: Option<U256>,
/// When this bid is a reaction so some event (eg: new block, new competition bid) we put here
/// the creation time of that event so we can measure our reaction time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl SlotBidder for NewTrueBlockValueSlotBidder {
payout_tx_value: block_descriptor.true_block_value + self.subsidy,
seen_competition_bid: None,
trigger_creation_time: Some(time::OffsetDateTime::now_utc()),
subsidy: self.subsidy.try_into().unwrap(),
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
///
/// Alternatively if configured (adjust_finalized_blocks = true) to run using old flow `prefinalize_worker` would not do anything with the block
/// and `finalize_worker` would do full finalization instead of adjustment of the finalize block.
use alloy_primitives::{utils::format_ether, U256};
use alloy_primitives::{utils::format_ether, I256, U256};
use derivative::Derivative;
use parking_lot::Mutex;
use std::sync::Arc;
Expand Down Expand Up @@ -155,19 +155,20 @@ impl PrefinalizedBlockInner {
fn finalize_block(
&mut self,
value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
adjust_finalized_blocks: bool,
) -> Result<Option<FinalizeBlockResult>, BlockBuildingHelperError> {
if let Some(local_ctx) = self.local_ctx.as_mut() {
if adjust_finalized_blocks {
self.block_building_helper
.adjust_finalized_block(local_ctx, value, seen_competition_bid)
.adjust_finalized_block(local_ctx, value, subsidy, seen_competition_bid)
.map(Some)
} else {
// we clone here because finalizing block multiple times is not supported
self.block_building_helper
.box_clone()
.finalize_block(local_ctx, value, seen_competition_bid)
.finalize_block(local_ctx, value, subsidy, seen_competition_bid)
.map(Some)
}
} else {
Expand Down Expand Up @@ -207,6 +208,7 @@ impl PrefinalizedBlock {
struct FinalizeCommand {
prefinalized_block: PrefinalizedBlock,
value: U256,
subsidy: I256,
seen_competition_bid: Option<U256>,
/// Bid received from the bidder (UnfinishedBuiltBlocksInput::seal_command)
bid_received_at: OffsetDateTime,
Expand Down Expand Up @@ -326,6 +328,7 @@ impl UnfinishedBuiltBlocksInput {
seen_competition_bid: bid.seen_competition_bid,
bid_received_at,
sent_to_sealer,
subsidy: bid.subsidy,
};
self.last_finalize_command.set(finalize_command);
} else {
Expand Down Expand Up @@ -382,7 +385,8 @@ impl UnfinishedBuiltBlocksInput {
continue;
}
};
match block_building_helper.finalize_block(&mut local_ctx, value, None) {
match block_building_helper.finalize_block(&mut local_ctx, value, I256::ZERO, None)
{
Ok(_) => {
trace!("Prefinalized block");
}
Expand Down Expand Up @@ -437,6 +441,7 @@ impl UnfinishedBuiltBlocksInput {

let mut result = match command.finalize_block(
finalize_command.value,
finalize_command.subsidy,
finalize_command.seen_competition_bid,
self.adjust_finalized_blocks,
) {
Expand Down
Loading
Loading