Skip to content

Commit

Permalink
Merge pull request #22 from lu-bann/fix-timestamp
Browse files Browse the repository at this point in the history
fix: fix genesis timestamp problem
  • Loading branch information
zsluedem authored Nov 1, 2024
2 parents 538e8be + 9278f49 commit da1d87c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/api/src/builder/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use helix_common::{
},
bid_submission::{v2::header_submission::SignedHeaderSubmission, BidSubmission, BidTrace, SignedBidSubmission},
chain_info::ChainInfo,
get_genesis_time_with_delay,
signing::RelaySigningContext,
simulator::BlockSimError,
versioned_payload::PayloadAndBlobs,
Expand All @@ -49,7 +50,6 @@ use uuid::Uuid;

use crate::{
builder::{error::BuilderApiError, traits::BlockSimulator, BlockSimRequest, DbInfo, OptimisticVersion},
get_genesis_time,
gossiper::{
traits::GossipClientTrait,
types::{BroadcastHeaderParams, BroadcastPayloadParams, GossipedMessage},
Expand Down Expand Up @@ -1647,7 +1647,7 @@ fn sanity_check_block_submission(
payload_attributes: &PayloadAttributesUpdate,
chain_info: &ChainInfo,
) -> Result<(), BuilderApiError> {
let genesis_time = get_genesis_time(chain_info);
let genesis_time = get_genesis_time_with_delay(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 });
Expand Down
9 changes: 0 additions & 9 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![allow(clippy::too_many_arguments)]

use helix_common::chain_info::ChainInfo;

pub mod builder;
pub mod constraints;
pub mod gossiper;
Expand All @@ -18,10 +16,3 @@ 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,
}
}
13 changes: 7 additions & 6 deletions crates/api/src/proposer/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use helix_common::{
beacon_api::PublishBlobsRequest,
chain_info::{ChainInfo, Network},
deneb::{BlobSidecars, BuildBlobSidecarError},
get_genesis_time_with_delay,
signed_proposal::VersionedSignedProposal,
traces::constraints_api::{ElectGatewayTrace, SetConstraintsTrace},
try_execution_header_from_payload,
Expand All @@ -54,7 +55,6 @@ use crate::{
api::{MAX_GATEWAY_ELECTION_SIZE, MAX_SET_CONSTRAINTS_SIZE},
SET_CONSTRAINTS_CUTOFF_NS,
},
get_genesis_time,
gossiper::{
traits::GossipClientTrait,
types::{BroadcastGetPayloadParams, GossipedMessage},
Expand Down Expand Up @@ -850,7 +850,7 @@ where
}

// Constraints cannot be set more than `SET_CONSTRAINTS_CUTOFF_NS` into the requested slot.
let genesis_time = get_genesis_time(&self.chain_info);
let genesis_time = get_genesis_time_with_delay(&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 {
Expand Down Expand Up @@ -967,7 +967,8 @@ where
/// - Only allows requests for the current slot until a certain cutoff time.
fn validate_bid_request_time(&self, bid_request: &BidRequest) -> Result<(), ProposerApiError> {
let curr_timestamp_ms = get_millis_timestamp()? as i64;
let slot_start_timestamp = self.chain_info.genesis_time_in_secs + (bid_request.slot * self.chain_info.seconds_per_slot);
let genesis_time = get_genesis_time_with_delay(&self.chain_info);
let slot_start_timestamp = genesis_time + (bid_request.slot * self.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 {
Expand Down Expand Up @@ -1161,8 +1162,8 @@ where
request_id: &Uuid,
) -> Result<PayloadAndBlobs, ProposerApiError> {
const RETRY_DELAY: Duration = Duration::from_millis(20);

let slot_time = self.chain_info.genesis_time_in_secs + (slot * self.chain_info.seconds_per_slot);
let genesis_time = get_genesis_time_with_delay(&self.chain_info);
let slot_time = genesis_time + (slot * self.chain_info.seconds_per_slot);
let slot_cutoff_millis = (slot_time * 1000) + GET_PAYLOAD_REQUEST_CUTOFF_MS as u64;

let mut last_error: Option<ProposerApiError> = None;
Expand Down Expand Up @@ -1321,7 +1322,7 @@ 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 genesis_time = get_genesis_time(chain_info);
let genesis_time = get_genesis_time_with_delay(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);
Expand Down
8 changes: 8 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ pub mod validator_preferences;

pub use api::*;
pub use builder_info::*;
use chain_info::ChainInfo;
pub use config::*;
pub use eth::*;
pub use proposer::*;
pub use traces::*;
pub use validator::*;
pub use validator_preferences::*;

pub fn get_genesis_time_with_delay(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,
}
}
4 changes: 3 additions & 1 deletion crates/housekeeper/src/chain_event_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use helix_common::{
api::builder_api::BuilderGetValidatorsResponseEntry,
bellatrix::{List, Node},
chain_info::ChainInfo,
get_genesis_time_with_delay,
};
use helix_database::DatabaseService;
use helix_utils::{get_payload_attributes_key, has_reached_fork};
Expand Down Expand Up @@ -127,7 +128,8 @@ impl<D: DatabaseService> ChainEventUpdater<D> {

// Validate this isn't a faulty head slot
if let Ok(current_timestamp) = SystemTime::now().duration_since(UNIX_EPOCH) {
let slot_timestamp = self.chain_info.genesis_time_in_secs + (event.slot * self.chain_info.seconds_per_slot);
let genesis_time = get_genesis_time_with_delay(&self.chain_info);
let slot_timestamp = genesis_time + (event.slot * self.chain_info.seconds_per_slot);
if slot_timestamp > current_timestamp.as_secs() + MAX_DISTANCE_FOR_FUTURE_SLOT {
warn!(head_slot = event.slot, "head event slot is too far in the future",);
return;
Expand Down

0 comments on commit da1d87c

Please sign in to comment.