From 64df57a3b576ec2c696de1e6d9bce8dc0992910c Mon Sep 17 00:00:00 2001 From: WillQ Date: Thu, 17 Oct 2024 09:00:24 +0800 Subject: [PATCH] fix: timestamp include genesis delay --- crates/api/src/builder/api.rs | 4 +++- crates/api/src/lib.rs | 9 +++++++++ crates/api/src/proposer/api.rs | 7 +++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/api/src/builder/api.rs b/crates/api/src/builder/api.rs index 9c3cb7f8..1466d4d3 100644 --- a/crates/api/src/builder/api.rs +++ b/crates/api/src/builder/api.rs @@ -49,6 +49,7 @@ use uuid::Uuid; use crate::{ builder::{error::BuilderApiError, traits::BlockSimulator, BlockSimRequest, DbInfo, OptimisticVersion}, + get_genesis_time, gossiper::{ traits::GossipClientTrait, types::{BroadcastHeaderParams, BroadcastPayloadParams, GossipedMessage}, @@ -1646,7 +1647,8 @@ fn sanity_check_block_submission( payload_attributes: &PayloadAttributesUpdate, chain_info: &ChainInfo, ) -> Result<(), BuilderApiError> { - let expected_timestamp = chain_info.genesis_time_in_secs + (bid_trace.slot * chain_info.seconds_per_slot); + let genesis_time = get_genesis_time(chain_info); + let expected_timestamp = genesis_time + (bid_trace.slot * chain_info.seconds_per_slot); if payload.timestamp() != expected_timestamp { return Err(BuilderApiError::IncorrectTimestamp { got: payload.timestamp(), expected: expected_timestamp }); } diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 6b72b979..f1036986 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -1,5 +1,7 @@ #![allow(clippy::too_many_arguments)] +use helix_common::chain_info::ChainInfo; + pub mod builder; pub mod constraints; pub mod gossiper; @@ -16,3 +18,10 @@ pub mod test_utils; mod grpc { include!(concat!(env!("OUT_DIR"), "/gossip.rs")); } + +pub fn get_genesis_time(chain_info: &ChainInfo) -> u64 { + match chain_info.context.genesis_time() { + Ok(genesis_time) => genesis_time, + Err(_) => chain_info.context.min_genesis_time + chain_info.context.genesis_delay, + } +} diff --git a/crates/api/src/proposer/api.rs b/crates/api/src/proposer/api.rs index e8aeebb3..ebb5c1c3 100644 --- a/crates/api/src/proposer/api.rs +++ b/crates/api/src/proposer/api.rs @@ -54,6 +54,7 @@ use crate::{ api::{MAX_GATEWAY_ELECTION_SIZE, MAX_SET_CONSTRAINTS_SIZE}, SET_CONSTRAINTS_CUTOFF_NS, }, + get_genesis_time, gossiper::{ traits::GossipClientTrait, types::{BroadcastGetPayloadParams, GossipedMessage}, @@ -849,7 +850,8 @@ where } // Constraints cannot be set more than `SET_CONSTRAINTS_CUTOFF_NS` into the requested slot. - let slot_start_timestamp = self.chain_info.genesis_time_in_secs + (constraints.slot() * self.chain_info.seconds_per_slot); + let genesis_time = get_genesis_time(&self.chain_info); + let slot_start_timestamp = genesis_time + (constraints.slot() * self.chain_info.seconds_per_slot); let ns_into_slot = (receive_ns as i64).saturating_sub((slot_start_timestamp * 1_000_000_000) as i64); if ns_into_slot > SET_CONSTRAINTS_CUTOFF_NS { return Err(ProposerApiError::SetConstraintsTooLate { ns_into_slot: ns_into_slot as u64, cutoff: SET_CONSTRAINTS_CUTOFF_NS as u64 }); @@ -1319,7 +1321,8 @@ where /// Calculates the time information for a given slot. fn calculate_slot_time_info(chain_info: &ChainInfo, slot: u64, request_time: u64) -> (i64, Duration) { - let slot_start_timestamp_in_secs = chain_info.genesis_time_in_secs + (slot * chain_info.seconds_per_slot); + let genesis_time = get_genesis_time(chain_info); + let slot_start_timestamp_in_secs = genesis_time + (slot * chain_info.seconds_per_slot); let ms_into_slot = (request_time / 1_000_000) as i64 - (slot_start_timestamp_in_secs * 1000) as i64; let duration_until_slot_start = chain_info.clock.duration_until_slot(slot);