Implement liveness BeaconAPI (#4343)

## Issue Addressed

#4243

## Proposed Changes

- create a new endpoint for liveness/{endpoint}

## Additional Info
This is my first PR.
This commit is contained in:
Aoi Kurokawa
2023-07-31 01:53:03 +00:00
parent 071dd4cd9c
commit 85a3340d0e
4 changed files with 136 additions and 0 deletions

View File

@@ -3301,6 +3301,45 @@ pub fn serve<T: BeaconChainTypes>(
},
);
// POST vaidator/liveness/{epoch}
let post_validator_liveness_epoch = eth_v1
.and(warp::path("validator"))
.and(warp::path("liveness"))
.and(warp::path::param::<Epoch>())
.and(warp::path::end())
.and(warp::body::json())
.and(chain_filter.clone())
.and_then(
|epoch: Epoch, indices: Vec<u64>, chain: Arc<BeaconChain<T>>| {
blocking_json_task(move || {
// Ensure the request is for either the current, previous or next epoch.
let current_epoch = chain
.epoch()
.map_err(warp_utils::reject::beacon_chain_error)?;
let prev_epoch = current_epoch.saturating_sub(Epoch::new(1));
let next_epoch = current_epoch.saturating_add(Epoch::new(1));
if epoch < prev_epoch || epoch > next_epoch {
return Err(warp_utils::reject::custom_bad_request(format!(
"request epoch {} is more than one epoch from the current epoch {}",
epoch, current_epoch
)));
}
let liveness: Vec<api_types::StandardLivenessResponseData> = indices
.iter()
.cloned()
.map(|index| {
let is_live = chain.validator_seen_at_epoch(index as usize, epoch);
api_types::StandardLivenessResponseData { index, is_live }
})
.collect();
Ok(api_types::GenericResponse::from(liveness))
})
},
);
// POST lighthouse/liveness
let post_lighthouse_liveness = warp::path("lighthouse")
.and(warp::path("liveness"))
@@ -3963,6 +4002,7 @@ pub fn serve<T: BeaconChainTypes>(
.uor(post_validator_sync_committee_subscriptions)
.uor(post_validator_prepare_beacon_proposer)
.uor(post_validator_register_validator)
.uor(post_validator_liveness_epoch)
.uor(post_lighthouse_liveness)
.uor(post_lighthouse_database_reconstruct)
.uor(post_lighthouse_database_historical_blocks)