Skip to content

Commit f02a06a

Browse files
ZanCorDXdvush
andauthored
Multi bid (#794)
New more versatile bidding core. --------- Co-authored-by: Vitaly Drogan <vitalii.drohan@gmail.com>
1 parent 4399ee4 commit f02a06a

File tree

19 files changed

+826
-191
lines changed

19 files changed

+826
-191
lines changed

crates/eth-sparse-mpt/src/v2/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl SharedCacheV2 {
4747
}
4848
}
4949

50-
#[derive(Debug, PartialEq, Eq)]
50+
#[derive(Debug, Clone, PartialEq, Eq)]
5151
enum StorageTrieStatus {
5252
InsertsNotProcessed,
5353
InsertsProcessed,
@@ -64,7 +64,6 @@ impl StorageTrieStatus {
6464
}
6565
}
6666

67-
/// WARN: Clone will not clone changed tries
6867
#[derive(Debug, Default)]
6968
pub struct RootHashCalculator {
7069
storage: DashMap<Address, Arc<Mutex<StorageCalculator>>, FxBuildHasher>,
@@ -81,8 +80,20 @@ pub struct RootHashCalculator {
8180
impl Clone for RootHashCalculator {
8281
fn clone(&self) -> Self {
8382
Self {
83+
storage: self
84+
.storage
85+
.iter()
86+
.map(|entry| {
87+
(
88+
*entry.key(),
89+
Arc::new(Mutex::new(entry.value().lock().clone())),
90+
)
91+
})
92+
.collect(),
93+
changed_account: Arc::new(RwLock::new(self.changed_account.read().clone())),
94+
account_trie: self.account_trie.clone(),
8495
shared_cache: self.shared_cache.clone(),
85-
..Default::default()
96+
incremental_account_change: self.incremental_account_change.clone(),
8697
}
8798
}
8899
}

crates/rbuilder-operator/src/bidding_service_wrapper/bidding_service.rs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
// This file is @generated by prost-build.
2-
/// Mapping of build_info::Version
3-
#[derive(Clone, PartialEq, ::prost::Message)]
4-
pub struct BidderVersionInfo {
5-
#[prost(string, tag = "1")]
6-
pub git_commit: ::prost::alloc::string::String,
7-
#[prost(string, tag = "2")]
8-
pub git_ref: ::prost::alloc::string::String,
9-
#[prost(string, tag = "3")]
10-
pub build_time_utc: ::prost::alloc::string::String,
11-
}
122
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
133
pub struct Empty {}
144
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
@@ -54,10 +44,41 @@ pub struct LandedBlockInfo {
5444
}
5545
#[derive(Clone, PartialEq, ::prost::Message)]
5646
pub struct LandedBlocksParams {
57-
/// Added field name
5847
#[prost(message, repeated, tag = "1")]
5948
pub landed_block_info: ::prost::alloc::vec::Vec<LandedBlockInfo>,
6049
}
50+
#[derive(Clone, PartialEq, ::prost::Message)]
51+
pub struct RelaySet {
52+
/// Ids of the relays (MevBoostRelayIDs)
53+
#[prost(string, repeated, tag = "2")]
54+
pub relay_ids: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
55+
}
56+
#[derive(Clone, PartialEq, ::prost::Message)]
57+
pub struct InitParams {
58+
#[prost(message, repeated, tag = "1")]
59+
pub landed_block_info: ::prost::alloc::vec::Vec<LandedBlockInfo>,
60+
/// All the relays we bid to.
61+
#[prost(message, optional, tag = "2")]
62+
pub all_relay_ids: ::core::option::Option<RelaySet>,
63+
}
64+
/// Mapping of build_info::Version
65+
#[derive(Clone, PartialEq, ::prost::Message)]
66+
pub struct BidderVersionInfo {
67+
#[prost(string, tag = "1")]
68+
pub git_commit: ::prost::alloc::string::String,
69+
#[prost(string, tag = "2")]
70+
pub git_ref: ::prost::alloc::string::String,
71+
#[prost(string, tag = "3")]
72+
pub build_time_utc: ::prost::alloc::string::String,
73+
}
74+
#[derive(Clone, PartialEq, ::prost::Message)]
75+
pub struct InitReturn {
76+
#[prost(message, optional, tag = "1")]
77+
pub bidder_version: ::core::option::Option<BidderVersionInfo>,
78+
/// RelaySets the bidder will use.
79+
#[prost(message, repeated, tag = "2")]
80+
pub relay_sets: ::prost::alloc::vec::Vec<RelaySet>,
81+
}
6182
/// Generated client implementations.
6283
pub mod bidding_service_client {
6384
#![allow(
@@ -163,11 +184,8 @@ pub mod bidding_service_client {
163184
/// Returns the version info for the server side.
164185
pub async fn initialize(
165186
&mut self,
166-
request: impl tonic::IntoRequest<super::LandedBlocksParams>,
167-
) -> std::result::Result<
168-
tonic::Response<super::BidderVersionInfo>,
169-
tonic::Status,
170-
> {
187+
request: impl tonic::IntoRequest<super::InitParams>,
188+
) -> std::result::Result<tonic::Response<super::InitReturn>, tonic::Status> {
171189
self.inner
172190
.ready()
173191
.await
@@ -329,11 +347,8 @@ pub mod bidding_service_server {
329347
/// Returns the version info for the server side.
330348
async fn initialize(
331349
&self,
332-
request: tonic::Request<super::LandedBlocksParams>,
333-
) -> std::result::Result<
334-
tonic::Response<super::BidderVersionInfo>,
335-
tonic::Status,
336-
>;
350+
request: tonic::Request<super::InitParams>,
351+
) -> std::result::Result<tonic::Response<super::InitReturn>, tonic::Status>;
337352
/// BiddingService
338353
async fn create_slot_bidder(
339354
&self,
@@ -447,16 +462,16 @@ pub mod bidding_service_server {
447462
struct InitializeSvc<T: BiddingService>(pub Arc<T>);
448463
impl<
449464
T: BiddingService,
450-
> tonic::server::UnaryService<super::LandedBlocksParams>
465+
> tonic::server::UnaryService<super::InitParams>
451466
for InitializeSvc<T> {
452-
type Response = super::BidderVersionInfo;
467+
type Response = super::InitReturn;
453468
type Future = BoxFuture<
454469
tonic::Response<Self::Response>,
455470
tonic::Status,
456471
>;
457472
fn call(
458473
&mut self,
459-
request: tonic::Request<super::LandedBlocksParams>,
474+
request: tonic::Request<super::InitParams>,
460475
) -> Self::Future {
461476
let inner = Arc::clone(&self.0);
462477
let fut = async move {

crates/rbuilder-operator/src/bidding_service_wrapper/client/bidding_service_client_adapter.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use parking_lot::Mutex;
44
use rbuilder::{
55
live_builder::block_output::bidding_service_interface::{
66
BiddingService, BlockSealInterfaceForSlotBidder, LandedBlockInfo as RealLandedBlockInfo,
7-
ScrapedRelayBlockBidWithStats, SlotBidder, SlotBlockId,
7+
RelaySet, ScrapedRelayBlockBidWithStats, SlotBidder, SlotBlockId,
88
},
99
utils::build_info::Version,
1010
};
@@ -21,17 +21,19 @@ use tokio::sync::mpsc;
2121
use tokio_util::sync::CancellationToken;
2222
use tonic::transport::{Channel, Endpoint, Uri};
2323
use tower::service_fn;
24-
use tracing::error;
24+
use tracing::{error, info};
2525

2626
use crate::{
2727
bidding_service_wrapper::{
2828
bidding_service_client::BiddingServiceClient,
29-
conversion::{real2rpc_block_hash, real2rpc_landed_block_info},
29+
conversion::{
30+
real2rpc_block_hash, real2rpc_landed_block_info, real2rpc_relay_set, rpc2real_relay_set,
31+
},
3032
fast_streams::helpers::{
3133
self, create_blocks_publisher, spawn_slot_bidder_seal_bid_command_subscriber,
3234
BlocksPublisher, ScrapedBidsPublisher,
3335
},
34-
CreateSlotBidderParams, DestroySlotBidderParams, Empty, LandedBlocksParams,
36+
CreateSlotBidderParams, DestroySlotBidderParams, Empty, InitParams, LandedBlocksParams,
3537
MustWinBlockParams,
3638
},
3739
metrics::set_bidding_service_version,
@@ -66,6 +68,7 @@ pub struct BiddingServiceClientAdapter {
6668
last_session_id: AtomicU64,
6769
scraped_bids_publisher: ScrapedBidsPublisher,
6870
blocks_publisher: Arc<BlocksPublisher>,
71+
relay_sets: Vec<RelaySet>,
6972
}
7073

7174
impl std::fmt::Debug for BiddingServiceClientAdapter {
@@ -93,6 +96,8 @@ pub enum Error {
9396
InitFailed(tonic::Status),
9497
#[error("ScrapedBidsPublisher error : {0}")]
9598
ScrapedBidsPublisher(#[from] helpers::Error),
99+
#[error("Bidder version not found")]
100+
BidderVersionNotFound,
96101
}
97102

98103
pub type Result<T> = core::result::Result<T, Error>;
@@ -102,17 +107,20 @@ impl BiddingServiceClientAdapter {
102107
pub async fn new(
103108
uds_path: &str,
104109
landed_blocks_history: &[RealLandedBlockInfo],
110+
all_relay_ids: RelaySet,
105111
cancellation_token: CancellationToken,
106112
) -> Result<Self> {
107113
let session_id_to_slot_bidder = Arc::new(Mutex::new(HashMap::new()));
108-
let commands_sender = Self::init_sender_task(
114+
let (commands_sender, relay_sets) = Self::init_sender_task(
109115
uds_path,
110116
landed_blocks_history,
117+
all_relay_ids,
111118
session_id_to_slot_bidder.clone(),
112119
)
113120
.await?;
114121
spawn_slot_bidder_seal_bid_command_subscriber(
115122
session_id_to_slot_bidder,
123+
relay_sets.clone(),
116124
cancellation_token.clone(),
117125
)?;
118126
let scraped_bids_publisher = ScrapedBidsPublisher::new()?;
@@ -122,20 +130,26 @@ impl BiddingServiceClientAdapter {
122130
last_session_id: AtomicU64::new(0),
123131
scraped_bids_publisher,
124132
blocks_publisher,
133+
relay_sets,
125134
})
126135
}
127136

128137
fn new_session_id(&self) -> u64 {
129138
self.last_session_id.fetch_add(1, Ordering::Relaxed)
130139
}
131140

141+
// returns the commands_sender to send commands to the bidding service and the relay_sets that it got on the initialize call.
132142
async fn init_sender_task(
133143
uds_path: &str,
134144
landed_blocks_history: &[RealLandedBlockInfo],
145+
all_relay_ids: RelaySet,
135146
session_id_to_slot_bidder: Arc<
136147
Mutex<HashMap<u64, Arc<dyn BlockSealInterfaceForSlotBidder + Send + Sync>>>,
137148
>,
138-
) -> Result<mpsc::UnboundedSender<BiddingServiceClientCommand>> {
149+
) -> Result<(
150+
mpsc::UnboundedSender<BiddingServiceClientCommand>,
151+
Vec<RelaySet>,
152+
)> {
139153
let uds_path = uds_path.to_string();
140154
// Url us dummy but needed to create the Endpoint.
141155
let channel = Endpoint::try_from("http://[::]:50051")
@@ -148,23 +162,42 @@ impl BiddingServiceClientAdapter {
148162
.await?;
149163
// Create a client
150164
let mut client = BiddingServiceClient::new(channel);
151-
let init_params = LandedBlocksParams {
165+
let init_params = InitParams {
152166
landed_block_info: landed_blocks_history
153167
.iter()
154168
.map(real2rpc_landed_block_info)
155169
.collect(),
170+
all_relay_ids: Some(real2rpc_relay_set(&all_relay_ids)),
156171
};
157-
let bidding_service_version = client
172+
let init_res = client
158173
.initialize(init_params)
159174
.await
160175
.map_err(Error::InitFailed)?;
161-
let bidding_service_version = bidding_service_version.into_inner();
176+
let init_res = init_res.into_inner();
177+
let bidding_service_version = init_res
178+
.bidder_version
179+
.ok_or(Error::BidderVersionNotFound)?;
180+
let relay_sets = init_res.relay_sets.iter().map(rpc2real_relay_set).collect();
181+
info!(?relay_sets, "relay sets received from bidding service");
162182
set_bidding_service_version(Version {
163183
git_commit: bidding_service_version.git_commit,
164184
git_ref: bidding_service_version.git_ref,
165185
build_time_utc: bidding_service_version.build_time_utc,
166186
});
167-
let (commands_sender, mut rx) = mpsc::unbounded_channel::<BiddingServiceClientCommand>();
187+
let (commands_sender, rx) = mpsc::unbounded_channel::<BiddingServiceClientCommand>();
188+
Self::spawn_sender_loop_task(rx, client, session_id_to_slot_bidder);
189+
Ok((commands_sender, relay_sets))
190+
}
191+
192+
/// Spawns a task to execute on client commands received via the channel.
193+
/// Sessions are kept in session_id_to_slot_bidder.
194+
fn spawn_sender_loop_task(
195+
mut rx: mpsc::UnboundedReceiver<BiddingServiceClientCommand>,
196+
mut client: BiddingServiceClient<Channel>,
197+
session_id_to_slot_bidder: Arc<
198+
Mutex<HashMap<u64, Arc<dyn BlockSealInterfaceForSlotBidder + Send + Sync>>>,
199+
>,
200+
) {
168201
// Spawn a task to execute received futures
169202
tokio::spawn(async move {
170203
while let Some(command) = rx.recv().await {
@@ -201,7 +234,6 @@ impl BiddingServiceClientAdapter {
201234
}
202235
}
203236
});
204-
Ok(commands_sender)
205237
}
206238

207239
/// Calls create_slot_bidder via RPC to init the bidder.
@@ -276,6 +308,10 @@ impl BiddingService for BiddingServiceClientAdapter {
276308
))
277309
}
278310

311+
fn relay_sets(&self) -> Vec<RelaySet> {
312+
self.relay_sets.clone()
313+
}
314+
279315
fn update_new_landed_blocks_detected(&self, landed_blocks: &[RealLandedBlockInfo]) {
280316
let param = LandedBlocksParams {
281317
landed_block_info: landed_blocks

crates/rbuilder-operator/src/bidding_service_wrapper/conversion.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
//! Conversion real data <-> gRPC data
2-
use crate::bidding_service_wrapper::LandedBlockInfo as RPCLandedBlockInfo;
2+
use crate::bidding_service_wrapper::{
3+
LandedBlockInfo as RPCLandedBlockInfo, RelaySet as RPCRelaySet,
4+
};
35

46
use alloy_primitives::{BlockHash, U256};
5-
use rbuilder::live_builder::block_output::bidding_service_interface::LandedBlockInfo as RealLandedBlockInfo;
7+
use rbuilder::live_builder::block_output::bidding_service_interface::{
8+
LandedBlockInfo as RealLandedBlockInfo, RelaySet as RealRelaySet,
9+
};
610
use time::OffsetDateTime;
711
use tonic::Status;
812

@@ -43,3 +47,13 @@ pub fn real2rpc_block_hash(v: BlockHash) -> Vec<u8> {
4347
pub fn rpc2real_block_hash(v: &Vec<u8>) -> Result<BlockHash, Status> {
4448
BlockHash::try_from(v.as_slice()).map_err(|_| Status::invalid_argument("rpc BlockHash error"))
4549
}
50+
51+
pub fn real2rpc_relay_set(l: &RealRelaySet) -> RPCRelaySet {
52+
RPCRelaySet {
53+
relay_ids: l.relays().to_vec(),
54+
}
55+
}
56+
57+
pub fn rpc2real_relay_set(l: &RPCRelaySet) -> RealRelaySet {
58+
RealRelaySet::new(l.relay_ids.clone())
59+
}

0 commit comments

Comments
 (0)