Skip to content

Commit

Permalink
Add option pattern for Optimistic Relay mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Dec 31, 2024
1 parent 821a88c commit 7216898
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
73 changes: 49 additions & 24 deletions crates/rbuilder/src/live_builder/block_output/relay_submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,17 @@ pub struct SubmissionConfig {
pub dry_run: bool,
pub validation_api: ValidationAPIClient,

pub optimistic_enabled: bool,
pub optimistic_signer: BLSBlockSigner,
pub optimistic_max_bid_value: U256,
pub optimistic_prevalidate_optimistic_blocks: bool,

pub optimisitic_config: Option<OptimisticConfig>,
pub bid_observer: Box<dyn BidObserver + Send + Sync>,
}

#[derive(Debug, Clone)]
pub struct OptimisticConfig {
pub signer: BLSBlockSigner,
pub max_bid_value: U256,
pub prevalidate_optimistic_blocks: bool,
}

/// Values from [`BuiltBlockTrace`]
struct BuiltBlockInfo {
pub bid_value: U256,
Expand Down Expand Up @@ -186,8 +189,20 @@ async fn run_submit_to_relays_job(
.iter()
.filter(|o| !o.order.is_tx())
.count();
let submission_optimistic =
config.optimistic_enabled && block.trace.bid_value < config.optimistic_max_bid_value;

// Only enable the optimistic config for this block if the bid value is below the max bid value
let optimistic_config =
config
.optimisitic_config
.clone()
.map_or(None, |optimistic_config| {
if block.trace.bid_value < optimistic_config.max_bid_value {
Some(optimistic_config)
} else {
None
}
});

let best_bid_value = best_bid_sync_source.best_bid_value().unwrap_or_default();
let submission_span = info_span!(
"bid",
Expand All @@ -207,7 +222,7 @@ async fn run_submit_to_relays_job(
parent: &submission_span,
"Submitting bid",
);
inc_initiated_submissions(submission_optimistic);
inc_initiated_submissions(optimistic_config.is_some());

let (normal_signed_submission, optimistic_signed_submission) = {
let normal_signed_submission = match sign_block_for_relay(
Expand All @@ -226,22 +241,30 @@ async fn run_submit_to_relays_job(
continue 'submit;
}
};
let optimistic_signed_submission = match sign_block_for_relay(
&config.optimistic_signer,
&block.sealed_block,
&block.txs_blobs_sidecars,
&block.execution_requests,
&config.chain_spec,
&slot_data.payload_attributes_event.data,
slot_data.slot_data.pubkey,
block.trace.bid_value,
) {
Ok(res) => res,
Err(err) => {
error!(parent: &submission_span, err = ?err, "Error signing block for relay");
continue 'submit;

let optimistic_signed_submission = if let Some(optimistic_config) =
optimistic_config.clone()
{
match sign_block_for_relay(
&optimistic_config.signer,
&block.sealed_block,
&block.txs_blobs_sidecars,
&block.execution_requests,
&config.chain_spec,
&slot_data.payload_attributes_event.data,
slot_data.slot_data.pubkey,
block.trace.bid_value,
) {
Ok(res) => Some(res),
Err(err) => {
error!(parent: &submission_span, err = ?err, "Error signing block for relay");
continue 'submit;
}
}
} else {
None
};

(normal_signed_submission, optimistic_signed_submission)
};

Expand Down Expand Up @@ -274,8 +297,10 @@ async fn run_submit_to_relays_job(
);
}

if submission_optimistic {
let can_submit = if config.optimistic_prevalidate_optimistic_blocks {
if let Some(optimistic_signed_submission) = &optimistic_signed_submission {
let optimistic_config = optimistic_config.expect("optimistic config is not None");

let can_submit = if optimistic_config.prevalidate_optimistic_blocks {
validate_block(
&slot_data,
&optimistic_signed_submission,
Expand Down
36 changes: 21 additions & 15 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
wallet_balance_watcher::WalletBalanceWatcher,
},
block_sealing_bidder_factory::BlockSealingBidderFactory,
relay_submit::{RelaySubmitSinkFactory, SubmissionConfig},
relay_submit::{OptimisticConfig, RelaySubmitSinkFactory, SubmissionConfig},
},
};
use crate::{
Expand Down Expand Up @@ -242,15 +242,22 @@ impl L1Config {

let signer = self.bls_signer(&chain_spec)?;

let optimistic_config = if self.optimistic_enabled {
Some(OptimisticConfig {
signer: optimistic_signer,
max_bid_value: parse_ether(&self.optimistic_max_bid_value_eth)?,
prevalidate_optimistic_blocks: self.optimistic_prevalidate_optimistic_blocks,
})
} else {
None
};

Ok(SubmissionConfig {
chain_spec,
signer,
dry_run: self.dry_run,
validation_api,
optimistic_enabled: self.optimistic_enabled,
optimistic_signer,
optimistic_max_bid_value: parse_ether(&self.optimistic_max_bid_value_eth)?,
optimistic_prevalidate_optimistic_blocks: self.optimistic_prevalidate_optimistic_blocks,
optimisitic_config: optimistic_config,
bid_observer,
})
}
Expand All @@ -266,16 +273,15 @@ impl L1Config {
"Builder mev boost normal relay pubkey: {:?}",
submission_config.signer.pub_key()
);
info!(
"Builder mev boost optimistic relay pubkey: {:?}",
submission_config.optimistic_signer.pub_key()
);
info!(
"Optimistic mode, enabled: {}, prevalidate: {}, max_value: {}",
submission_config.optimistic_enabled,
submission_config.optimistic_prevalidate_optimistic_blocks,
format_ether(submission_config.optimistic_max_bid_value),
);

if let Some(optimitic_config) = submission_config.optimisitic_config.as_ref() {
info!(
"Optimistic mode enabled, relay pubkey {:?}, prevalidate: {}, max_value: {}",
optimitic_config.signer.pub_key(),
optimitic_config.prevalidate_optimistic_blocks,
format_ether(optimitic_config.max_bid_value),
);
};

let relays = self.create_relays()?;
let sink_factory: Box<dyn BuilderSinkFactory> = Box::new(RelaySubmitSinkFactory::new(
Expand Down

0 comments on commit 7216898

Please sign in to comment.