-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathget_header.rs
More file actions
292 lines (250 loc) · 9.8 KB
/
Copy pathget_header.rs
File metadata and controls
292 lines (250 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::{Duration, Instant},
};
use axum::{Extension, extract::Path, http::HeaderMap, response::IntoResponse};
use helix_common::{
GET_HEADER_REQUEST_CUTOFF_MS, GetHeaderTrace, RequestTimings,
api::proposer_api::GetHeaderParams,
api_provider::{ApiProvider, TimingResult},
chain_info::ChainInfo,
decoder::{Encoding, HEADER_SSZ},
metrics::{BID_SIGNING_LATENCY, HEADER_TIMEOUT_FETCH, HEADER_TIMEOUT_SLEEP},
signing::RelaySigningContext,
spawn_tracked,
utils::{extract_request_id, utcnow_ms, utcnow_ns},
};
use helix_types::{BuilderBid, ForkName, GetHeaderResponse, SignedBuilderBid};
use http::{HeaderValue, header::CONTENT_TYPE};
use ssz::Encode;
use tracing::{Instrument, debug, error, info, trace, warn};
use super::ProposerApi;
use crate::api::{
Api,
proposer::{CONSENSUS_VERSION_HEADER, error::ProposerApiError},
router::Terminating,
};
pub(super) struct ValidatedHeaderRequest {
pub ms_into_slot: u64,
pub validation_complete_ns: u64,
pub user_agent: Option<String>,
pub is_mev_boost: bool,
pub sleep_time: Option<Duration>,
pub timeout_ms: Option<u64>,
}
impl<A: Api> ProposerApi<A> {
pub(super) fn validate_header_request(
&self,
params: &GetHeaderParams,
headers: &HeaderMap,
terminating: &AtomicBool,
) -> Result<ValidatedHeaderRequest, ProposerApiError> {
if terminating.load(Ordering::Relaxed) || self.local_cache.kill_switch_enabled() {
return Err(ProposerApiError::ServiceUnavailableError);
}
let (head_slot, duty) = self.curr_slot_info.slot_info();
let bid_slot = head_slot.as_u64() + 1;
if params.slot != bid_slot {
debug!("request for past slot");
return Err(ProposerApiError::RequestWrongSlot { request_slot: params.slot, bid_slot });
}
// Only return a bid if there is a proposer connected to this slot.
let Some(duty) = duty else {
debug!("proposer duty not found");
return Err(ProposerApiError::ProposerNotRegistered);
};
let ms_into_slot = validate_bid_request_time(&self.chain_info, params)?;
let validation_complete_ns = utcnow_ns();
trace!(ms_into_slot, "completed validation");
let user_agent = self.api_provider.get_metadata(headers);
let TimingResult { is_mev_boost, sleep_time, timeout_ms } = self
.api_provider
.get_timing(params, headers, &duty.entry.preferences, ms_into_slot)
.map_err(ProposerApiError::InvalidGetHeader)?;
Ok(ValidatedHeaderRequest {
ms_into_slot,
validation_complete_ns,
user_agent,
is_mev_boost,
sleep_time,
timeout_ms,
})
}
/// Retrieves the best bid header for the specified slot, parent hash, and public key.
///
/// This function accepts a slot number, parent hash and public_key.
/// 1. Validates that the request's slot is not older than the head slot.
/// 2. Validates the request timestamp to ensure it's not too late.
/// 3. Fetches the best bid for the given parameters from the auctioneer.
///
/// The function returns a JSON response containing the best bid if found.
///
/// Implements this API: <https://ethereum.github.io/builder-specs/#/Builder/getHeader>
#[tracing::instrument(skip_all, err(level = tracing::Level::TRACE), fields(id =% extract_request_id(&headers), slot = params.slot, parent_hash =? params.parent_hash))]
pub async fn get_header(
Extension(proposer_api): Extension<Arc<ProposerApi<A>>>,
Extension(timings): Extension<RequestTimings>,
Extension(Terminating(terminating)): Extension<Terminating>,
headers: HeaderMap,
Path(params): Path<GetHeaderParams>,
) -> Result<impl IntoResponse, ProposerApiError> {
trace!("starting call");
let ValidatedHeaderRequest {
ms_into_slot,
validation_complete_ns,
user_agent,
is_mev_boost,
sleep_time,
..
} = proposer_api.validate_header_request(¶ms, &headers, &terminating)?;
let mut trace = GetHeaderTrace {
receive: timings.on_receive_ns,
validation_complete: validation_complete_ns,
..Default::default()
};
let mut timing_guard = TimeoutGuard::default();
if let Some(sleep_time) = sleep_time {
debug!(
?sleep_time,
ms_into_slot,
slot = params.slot,
pubkey = ?params.pubkey,
"timing game sleep");
tokio::time::sleep(sleep_time).await;
timing_guard.done_sleep = true;
} else {
timing_guard.done_sleep = true;
};
trace!("done sleep");
let Ok(rx) = proposer_api.auctioneer_handle.get_header(params, is_mev_boost) else {
error!("failed to send get_header to auctioneer");
return Err(ProposerApiError::InternalServerError);
};
let bid = match rx.await {
Ok(res) => res.inspect_err(|_| timing_guard.done_fetch = true)?,
Err(err) => {
warn!(%err, "failed to get header from auctioneer");
return Err(ProposerApiError::InternalServerError);
}
};
let now_ns = utcnow_ns();
trace.best_bid_fetched = now_ns;
debug!(trace = ?trace, "best bid fetched");
let bid_block_hash = *bid.block_hash();
let value = *bid.value();
let ep = bid.execution_payload();
let builder_pubkey = *bid.bid_data_ref().builder_pubkey;
let proposer_fee_recipient = ep.fee_recipient;
let block_number = ep.block_number;
let extra_data = ep.extra_data.to_vec();
proposer_api.db.save_get_header_call(
params,
bid_block_hash,
value,
trace,
is_mev_boost,
user_agent,
builder_pubkey,
proposer_fee_recipient,
block_number,
extra_data,
);
let fork = proposer_api.chain_info.fork_at_slot(params.slot.into());
let payload_and_blobs = bid.payload_and_blobs();
let bid_data = bid.bid_data_ref().to_owned();
let bid = bid.into_builder_bid_slow();
let signed_bid = resign_builder_bid(bid, &proposer_api.signing_context, fork);
if proposer_api.relay_config.gossip_payload_on_header && is_mev_boost {
spawn_tracked!(
async move {
info!("gossiping payload");
proposer_api
.gossip_payload(
params.slot.into(),
¶ms.pubkey,
&payload_and_blobs,
fork,
&bid_data,
)
.await;
}
.in_current_span()
);
}
info!(block_hash =% bid_block_hash, ?value, "delivering bid");
timing_guard.done_fetch = true;
let response_encoding = Encoding::from_accept(&headers);
match response_encoding {
Encoding::Json => Ok(axum::Json(serde_json::to_value(signed_bid)?).into_response()),
Encoding::Ssz => {
let mut response = signed_bid.data.as_ssz_bytes().into_response();
let headers = response.headers_mut();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(HEADER_SSZ).unwrap());
headers.insert(
CONSENSUS_VERSION_HEADER,
HeaderValue::from_str(&fork.to_string()).unwrap(),
);
Ok(response)
}
}
}
}
#[derive(Default)]
struct TimeoutGuard {
done_sleep: bool,
done_fetch: bool,
}
impl Drop for TimeoutGuard {
fn drop(&mut self) {
if !self.done_sleep {
HEADER_TIMEOUT_SLEEP.inc();
warn!("didn't complete sleep")
} else if !self.done_fetch {
HEADER_TIMEOUT_FETCH.inc();
warn!("didn't complete fetch")
}
}
}
/// Validates that the bid request is not sent too late within the current slot.
///
/// - Only allows requests for the current slot until a certain cutoff time.
///
/// Returns how many ms we are into the slot if ok.
fn validate_bid_request_time(
chain_info: &ChainInfo,
bid_request: &GetHeaderParams,
) -> Result<u64, ProposerApiError> {
let curr_timestamp_ms = utcnow_ms() as i64;
let slot_start_timestamp =
chain_info.genesis_time_in_secs + (bid_request.slot * chain_info.seconds_per_slot());
let ms_into_slot = curr_timestamp_ms.saturating_sub((slot_start_timestamp * 1000) as i64);
if ms_into_slot > GET_HEADER_REQUEST_CUTOFF_MS {
return Err(ProposerApiError::GetHeaderRequestTooLate {
ms_into_slot: ms_into_slot as u64,
cutoff: GET_HEADER_REQUEST_CUTOFF_MS as u64,
});
}
Ok(ms_into_slot.max(0) as u64)
}
/// Signs the builder bid with the relay key. This is necessary because the relay is the "builder"
/// from the proposer point of view
pub fn resign_builder_bid(
mut message: BuilderBid,
signing_ctx: &RelaySigningContext,
fork: ForkName,
) -> GetHeaderResponse {
let start = Instant::now();
message.pubkey = *signing_ctx.pubkey();
let sig = signing_ctx.sign_builder_message(&message).serialize().into();
let bid = GetHeaderResponse {
version: fork,
metadata: Default::default(),
data: SignedBuilderBid { message, signature: sig },
};
BID_SIGNING_LATENCY.observe(start.elapsed().as_micros() as f64);
debug!("signing builder bid took {:?}", start.elapsed());
bid
}