Skip to content

Commit 1fb5ec5

Browse files
authored
refactor(generator): behaviour fixes for new_payload_job and best_payload (#480)
- `best_payload`: was `unimplemented!()` and would crash the builder if ever called. Now returns the latest payload published to the internal `watch` channel, or `PayloadBuilderError::MissingPayload` when nothing has been built yet. - FCU arrival-delay metric: use `checked_sub` to avoid an underflow panic when `block_time` exceeds the payload timestamp (possible in tests with tiny timestamps), and switch to `Duration::as_secs_f64() * 1000.0` instead of casting through `u128 -> i64 -> f64`. - `new_payload_job`: collapse the cancel-old / store-new sequence into a single critical section. Closes a small window where a concurrent observer could see the old cancellation fired but the new one not yet stored. Also runs the parent-header lookup before cancelling the previous job, so a malformed FCU does not abort in-progress work. - `resolve_kind`: drop the `payload_kind` field from the info log, since only one kind is in use.
1 parent 667a564 commit 1fb5ec5

1 file changed

Lines changed: 26 additions & 29 deletions

File tree

crates/op-rbuilder/src/builder/generator.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,15 @@ where
149149
// Expected: FCU should arrive at (payload_timestamp - block_time)
150150
// Positive delay = FCU arrived late, Negative = FCU arrived early
151151
let timestamp = rpc_attributes.timestamp();
152-
let now = SystemTime::now();
153-
let expected_fcu_arrival =
154-
SystemTime::UNIX_EPOCH + Duration::from_secs(timestamp) - self.block_time;
155-
let fcu_arrival_delay_ms = now
156-
.duration_since(expected_fcu_arrival)
157-
.map(|d| d.as_millis() as i64)
158-
.unwrap_or_else(|e| -(e.duration().as_millis() as i64));
159-
self.metrics
160-
.fcu_arrival_delay
161-
.record(fcu_arrival_delay_ms as f64);
162-
163-
let cancellation = {
164-
// Cancel existing payload via new_fcu
165-
{
166-
let last_cancel = self.last_payload_cancel.lock().unwrap();
167-
last_cancel.cancel_new_fcu();
168-
}
169-
170-
// Create new PayloadJobCancellation and store it
171-
let cancellation = PayloadJobCancellation::new();
172-
{
173-
let mut last_cancel = self.last_payload_cancel.lock().unwrap();
174-
*last_cancel = cancellation.clone();
175-
}
176-
cancellation
152+
let payload_time = SystemTime::UNIX_EPOCH + Duration::from_secs(timestamp);
153+
let expected_fcu_arrival = payload_time
154+
.checked_sub(self.block_time)
155+
.unwrap_or(SystemTime::UNIX_EPOCH);
156+
let fcu_arrival_delay_ms = match SystemTime::now().duration_since(expected_fcu_arrival) {
157+
Ok(d) => d.as_secs_f64() * 1000.0,
158+
Err(e) => -(e.duration().as_secs_f64() * 1000.0),
177159
};
160+
self.metrics.fcu_arrival_delay.record(fcu_arrival_delay_ms);
178161

179162
let parent_header = if parent_hash.is_zero() {
180163
// use latest block if parent is zero: genesis block
@@ -187,6 +170,17 @@ where
187170
.ok_or_else(|| PayloadBuilderError::MissingParentBlock(parent_hash))?
188171
};
189172

173+
let cancel = {
174+
// Cancel existing payload via new_fcu
175+
let mut last_cancel = self.last_payload_cancel.lock().unwrap();
176+
last_cancel.cancel_new_fcu();
177+
178+
// Create new PayloadJobCancellation and store it
179+
let new = PayloadJobCancellation::new();
180+
*last_cancel = new.clone();
181+
new
182+
};
183+
190184
info!(
191185
target: "payload_builder",
192186
id = %id,
@@ -204,7 +198,7 @@ where
204198
config,
205199
rpc_attributes,
206200
payload_rx: None,
207-
cancel: cancellation,
201+
cancel,
208202
deadline,
209203
cached_reads: self
210204
.maybe_pre_cached(parent_header.hash())
@@ -278,7 +272,10 @@ where
278272
type BuiltPayload = Builder::BuiltPayload;
279273

280274
fn best_payload(&self) -> Result<Self::BuiltPayload, PayloadBuilderError> {
281-
unimplemented!()
275+
self.payload_rx
276+
.as_ref()
277+
.and_then(|rx| rx.borrow().clone())
278+
.ok_or(PayloadBuilderError::MissingPayload)
282279
}
283280

284281
fn payload_attributes(&self) -> Result<Self::PayloadAttributes, PayloadBuilderError> {
@@ -287,9 +284,9 @@ where
287284

288285
fn resolve_kind(
289286
&mut self,
290-
kind: PayloadKind,
287+
_kind: PayloadKind,
291288
) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive) {
292-
info!(target: "payload_builder", payload_kind = ?kind, "Resolve payload job");
289+
info!(target: "payload_builder", "Resolve payload job");
293290

294291
let rx = self.payload_rx.take();
295292
let cancellation = self.cancel.clone();

0 commit comments

Comments
 (0)