feat: implement new beacon APIs(accessors for pending_deposits/pending_partial_withdrawals) (#7006)

Resolves #7003


  Added two endpoints as https://github.com/ethereum/beacon-APIs/pull/500 proposed:
- `/eth/v1/beacon/states/{state_id}/pending_deposits`
- `/eth/v1/beacon/states/{state_id}/pending_partial_withdrawals`
This commit is contained in:
Jun Song
2025-03-17 10:46:50 +09:00
committed by GitHub
parent 3a555f571f
commit 50b5a72c58
3 changed files with 177 additions and 0 deletions

View File

@@ -1119,6 +1119,72 @@ pub fn serve<T: BeaconChainTypes>(
},
);
// GET beacon/states/{state_id}/pending_deposits
let get_beacon_state_pending_deposits = beacon_states_path
.clone()
.and(warp::path("pending_deposits"))
.and(warp::path::end())
.then(
|state_id: StateId,
task_spawner: TaskSpawner<T::EthSpec>,
chain: Arc<BeaconChain<T>>| {
task_spawner.blocking_json_task(Priority::P1, move || {
let (data, execution_optimistic, finalized) = state_id
.map_state_and_execution_optimistic_and_finalized(
&chain,
|state, execution_optimistic, finalized| {
let Ok(deposits) = state.pending_deposits() else {
return Err(warp_utils::reject::custom_bad_request(
"Pending deposits not found".to_string(),
));
};
Ok((deposits.clone(), execution_optimistic, finalized))
},
)?;
Ok(api_types::ExecutionOptimisticFinalizedResponse {
data,
execution_optimistic: Some(execution_optimistic),
finalized: Some(finalized),
})
})
},
);
// GET beacon/states/{state_id}/pending_partial_withdrawals
let get_beacon_state_pending_partial_withdrawals = beacon_states_path
.clone()
.and(warp::path("pending_partial_withdrawals"))
.and(warp::path::end())
.then(
|state_id: StateId,
task_spawner: TaskSpawner<T::EthSpec>,
chain: Arc<BeaconChain<T>>| {
task_spawner.blocking_json_task(Priority::P1, move || {
let (data, execution_optimistic, finalized) = state_id
.map_state_and_execution_optimistic_and_finalized(
&chain,
|state, execution_optimistic, finalized| {
let Ok(withdrawals) = state.pending_partial_withdrawals() else {
return Err(warp_utils::reject::custom_bad_request(
"Pending withdrawals not found".to_string(),
));
};
Ok((withdrawals.clone(), execution_optimistic, finalized))
},
)?;
Ok(api_types::ExecutionOptimisticFinalizedResponse {
data,
execution_optimistic: Some(execution_optimistic),
finalized: Some(finalized),
})
})
},
);
// GET beacon/headers
//
// Note: this endpoint only returns information about blocks in the canonical chain. Given that
@@ -4667,6 +4733,8 @@ pub fn serve<T: BeaconChainTypes>(
.uor(get_beacon_state_committees)
.uor(get_beacon_state_sync_committees)
.uor(get_beacon_state_randao)
.uor(get_beacon_state_pending_deposits)
.uor(get_beacon_state_pending_partial_withdrawals)
.uor(get_beacon_headers)
.uor(get_beacon_headers_block_id)
.uor(get_beacon_block)

View File

@@ -1192,6 +1192,60 @@ impl ApiTester {
self
}
pub async fn test_beacon_states_pending_deposits(self) -> Self {
for state_id in self.interesting_state_ids() {
let mut state_opt = state_id
.state(&self.chain)
.ok()
.map(|(state, _execution_optimistic, _finalized)| state);
let result = self
.client
.get_beacon_states_pending_deposits(state_id.0)
.await
.unwrap()
.map(|res| res.data);
if result.is_none() && state_opt.is_none() {
continue;
}
let state = state_opt.as_mut().expect("result should be none");
let expected = state.pending_deposits().unwrap();
assert_eq!(result.unwrap(), expected.to_vec());
}
self
}
pub async fn test_beacon_states_pending_partial_withdrawals(self) -> Self {
for state_id in self.interesting_state_ids() {
let mut state_opt = state_id
.state(&self.chain)
.ok()
.map(|(state, _execution_optimistic, _finalized)| state);
let result = self
.client
.get_beacon_states_pending_partial_withdrawals(state_id.0)
.await
.unwrap()
.map(|res| res.data);
if result.is_none() && state_opt.is_none() {
continue;
}
let state = state_opt.as_mut().expect("result should be none");
let expected = state.pending_partial_withdrawals().unwrap();
assert_eq!(result.unwrap(), expected.to_vec());
}
self
}
pub async fn test_beacon_headers_all_slots(self) -> Self {
for slot in 0..CHAIN_LENGTH {
let slot = Slot::from(slot);
@@ -6316,6 +6370,22 @@ async fn beacon_get_state_info() {
.await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn beacon_get_state_info_electra() {
let mut config = ApiTesterConfig::default();
config.spec.altair_fork_epoch = Some(Epoch::new(0));
config.spec.bellatrix_fork_epoch = Some(Epoch::new(0));
config.spec.capella_fork_epoch = Some(Epoch::new(0));
config.spec.deneb_fork_epoch = Some(Epoch::new(0));
config.spec.electra_fork_epoch = Some(Epoch::new(0));
ApiTester::new_from_config(config)
.await
.test_beacon_states_pending_deposits()
.await
.test_beacon_states_pending_partial_withdrawals()
.await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn beacon_get_blocks() {
ApiTester::new()

View File

@@ -782,6 +782,45 @@ impl BeaconNodeHttpClient {
self.get_opt(path).await
}
/// `GET beacon/states/{state_id}/pending_deposits`
///
/// Returns `Ok(None)` on a 404 error.
pub async fn get_beacon_states_pending_deposits(
&self,
state_id: StateId,
) -> Result<Option<ExecutionOptimisticFinalizedResponse<Vec<PendingDeposit>>>, Error> {
let mut path = self.eth_path(V1)?;
path.path_segments_mut()
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
.push("beacon")
.push("states")
.push(&state_id.to_string())
.push("pending_deposits");
self.get_opt(path).await
}
/// `GET beacon/states/{state_id}/pending_partial_withdrawals`
///
/// Returns `Ok(None)` on a 404 error.
pub async fn get_beacon_states_pending_partial_withdrawals(
&self,
state_id: StateId,
) -> Result<Option<ExecutionOptimisticFinalizedResponse<Vec<PendingPartialWithdrawal>>>, Error>
{
let mut path = self.eth_path(V1)?;
path.path_segments_mut()
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
.push("beacon")
.push("states")
.push(&state_id.to_string())
.push("pending_partial_withdrawals");
self.get_opt(path).await
}
/// `GET beacon/light_client/updates`
///
/// Returns `Ok(None)` on a 404 error.