Skip to content

Commit 309c301

Browse files
authored
Allow /validator apis to work pre-genesis (#7729)
N/A Lighthouse BN http endpoint would return a server error pre-genesis on the `validator/duties/attester` and `validator/prepare_beacon_proposer` because `slot_clock.now()` would return a `None` pre-genesis. The prysm VC depends on the endpoints pre-genesis and was having issues interoping with the lighthouse bn because of this reason. The proposer duties endpoint explicitly handles the pre-genesis case here https://github.com/sigp/lighthouse/blob/538067f1ff9840d44e3c2ea60581e18aba8c4143/beacon_node/http_api/src/proposer_duties.rs#L23-L28 I see no reason why we can't make the other endpoints more flexible to work pre-genesis. This PR handles the pre-genesis case on the attester and prepare_beacon_proposer endpoints as well. Thanks for raising @james-prysm.
1 parent 6409a32 commit 309c301

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

beacon_node/http_api/src/attester_duties.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,30 @@ pub fn attester_duties<T: BeaconChainTypes>(
1616
request_indices: &[u64],
1717
chain: &BeaconChain<T>,
1818
) -> Result<ApiDuties, warp::reject::Rejection> {
19-
let current_epoch = chain.epoch().map_err(warp_utils::reject::unhandled_error)?;
19+
let current_epoch = chain
20+
.slot_clock
21+
.now_or_genesis()
22+
.map(|slot| slot.epoch(T::EthSpec::slots_per_epoch()))
23+
.ok_or(BeaconChainError::UnableToReadSlot)
24+
.map_err(warp_utils::reject::unhandled_error)?;
2025

2126
// Determine what the current epoch would be if we fast-forward our system clock by
2227
// `MAXIMUM_GOSSIP_CLOCK_DISPARITY`.
2328
//
2429
// Most of the time, `tolerant_current_epoch` will be equal to `current_epoch`. However, during
2530
// the first `MAXIMUM_GOSSIP_CLOCK_DISPARITY` duration of the epoch `tolerant_current_epoch`
2631
// will equal `current_epoch + 1`
27-
let tolerant_current_epoch = chain
28-
.slot_clock
29-
.now_with_future_tolerance(chain.spec.maximum_gossip_clock_disparity())
30-
.ok_or_else(|| warp_utils::reject::custom_server_error("unable to read slot clock".into()))?
31-
.epoch(T::EthSpec::slots_per_epoch());
32+
let tolerant_current_epoch = if chain.slot_clock.is_prior_to_genesis().unwrap_or(true) {
33+
current_epoch
34+
} else {
35+
chain
36+
.slot_clock
37+
.now_with_future_tolerance(chain.spec.maximum_gossip_clock_disparity())
38+
.ok_or_else(|| {
39+
warp_utils::reject::custom_server_error("unable to read slot clock".into())
40+
})?
41+
.epoch(T::EthSpec::slots_per_epoch())
42+
};
3243

3344
if request_epoch == current_epoch
3445
|| request_epoch == current_epoch + 1

beacon_node/http_api/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3790,7 +3790,11 @@ pub fn serve<T: BeaconChainTypes>(
37903790
.ok_or(BeaconChainError::ExecutionLayerMissing)
37913791
.map_err(warp_utils::reject::unhandled_error)?;
37923792

3793-
let current_slot = chain.slot().map_err(warp_utils::reject::unhandled_error)?;
3793+
let current_slot = chain
3794+
.slot_clock
3795+
.now_or_genesis()
3796+
.ok_or(BeaconChainError::UnableToReadSlot)
3797+
.map_err(warp_utils::reject::unhandled_error)?;
37943798
let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch());
37953799

37963800
debug!(

0 commit comments

Comments
 (0)