Empty list [] to return all validators balances (#7474)

The endpoint `/eth/v1/beacon/states/head/validator_balances` returns an empty data when the data field is `[]`. According to the beacon API spec, it should return the balances of all validators:

Reference: https://ethereum.github.io/beacon-APIs/#/Beacon/postStateValidatorBalances
`If the supplied list is empty (i.e. the body is []) or no body is supplied then balances will be returned for all validators.`


  This PR changes so that: `curl -X 'POST' 'http://localhost:5052/eth/v1/beacon/states/head/validator_balances' -d '[]' | jq` returns balances of all validators.
This commit is contained in:
chonghe
2025-05-20 15:18:29 +08:00
committed by GitHub
parent 805c2dc831
commit 7e2df6b602
5 changed files with 56 additions and 13 deletions

View File

@@ -31,3 +31,27 @@ pub fn json<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error =
.map_err(|err| reject::custom_deserialize_error(format!("{:?}", err)))
})
}
// Add a json_no_body function to handle the case when no body is provided in the HTTP request
pub fn json_no_body<T: DeserializeOwned + Default + Send>(
) -> impl Filter<Extract = (T,), Error = Rejection> + Copy {
warp::header::optional::<String>(CONTENT_TYPE_HEADER)
.and(warp::body::bytes())
.and_then(|header: Option<String>, bytes: Bytes| async move {
if let Some(header) = header {
if header == SSZ_CONTENT_TYPE_HEADER {
return Err(reject::unsupported_media_type(
"The request's content-type is not supported".to_string(),
));
}
}
// Handle the case when the HTTP request has no body, i.e., without the -d header
if bytes.is_empty() {
return Ok(T::default());
}
Json::decode(bytes)
.map_err(|err| reject::custom_deserialize_error(format!("{:?}", err)))
})
}