Skip to content

Commit 7c2b5b6

Browse files
committed
Move get_light_client_bootstrap logic to BeaconChain. LightClientBootstrap API to return ForkVersionedResponse.
1 parent bfd3fb7 commit 7c2b5b6

File tree

4 files changed

+67
-94
lines changed

4 files changed

+67
-94
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6446,6 +6446,33 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
64466446
pub fn data_availability_boundary(&self) -> Option<Epoch> {
64476447
self.data_availability_checker.data_availability_boundary()
64486448
}
6449+
6450+
/// Gets the `LightClientBootstrap` object for a requested block root.
6451+
///
6452+
/// Returns `None` when the state or block is not found in the database.
6453+
pub fn get_light_client_bootstrap(
6454+
&self,
6455+
block_root: &Hash256,
6456+
) -> Result<Option<(LightClientBootstrap<T::EthSpec>, ForkName)>, Error> {
6457+
let Some(state_root) = self
6458+
.get_blinded_block(block_root)?
6459+
.map(|block| block.state_root())
6460+
else {
6461+
return Ok(None);
6462+
};
6463+
6464+
let Some(mut state) = self.get_state(&state_root, None)? else {
6465+
return Ok(None);
6466+
};
6467+
6468+
let fork_name = state
6469+
.fork_name(&self.spec)
6470+
.map_err(Error::InconsistentFork)?;
6471+
6472+
LightClientBootstrap::from_beacon_state(&mut state)
6473+
.map(|bootstrap| Some((bootstrap, fork_name)))
6474+
.map_err(Error::LightClientError)
6475+
}
64496476
}
64506477

64516478
impl<T: BeaconChainTypes> Drop for BeaconChain<T> {

beacon_node/beacon_chain/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub enum BeaconChainError {
221221
ProposerHeadForkChoiceError(fork_choice::Error<proto_array::Error>),
222222
UnableToPublish,
223223
AvailabilityCheckError(AvailabilityCheckError),
224+
LightClientError(LightClientError),
224225
}
225226

226227
easy_from_to!(SlotProcessingError, BeaconChainError);

beacon_node/http_api/src/lib.rs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ use tokio_stream::{
7676
};
7777
use types::{
7878
Attestation, AttestationData, AttestationShufflingId, AttesterSlashing, BeaconStateError,
79-
BlindedPayload, CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName, Hash256,
80-
LightClientBootstrap, ProposerPreparationData, ProposerSlashing, RelativeEpoch,
79+
BlindedPayload, CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName,
80+
ForkVersionedResponse, Hash256, ProposerPreparationData, ProposerSlashing, RelativeEpoch,
8181
SignedAggregateAndProof, SignedBlsToExecutionChange, SignedContributionAndProof,
8282
SignedValidatorRegistrationData, SignedVoluntaryExit, Slot, SyncCommitteeMessage,
8383
SyncContributionData,
@@ -2423,41 +2423,21 @@ pub fn serve<T: BeaconChainTypes>(
24232423
block_root: Hash256,
24242424
accept_header: Option<api_types::Accept>| {
24252425
task_spawner.blocking_response_task(Priority::P1, move || {
2426-
let state_root = chain
2427-
.get_blinded_block(&block_root)
2428-
.map_err(|_| {
2429-
warp_utils::reject::custom_server_error(
2430-
"Error retrieving block".to_string(),
2431-
)
2432-
})?
2433-
.map(|signed_block| signed_block.state_root())
2434-
.ok_or_else(|| {
2435-
warp_utils::reject::custom_not_found(
2426+
let (bootstrap, fork_name) = match chain.get_light_client_bootstrap(&block_root)
2427+
{
2428+
Ok(Some(res)) => res,
2429+
Ok(None) => {
2430+
return Err(warp_utils::reject::custom_not_found(
24362431
"Light client bootstrap unavailable".to_string(),
2437-
)
2438-
})?;
2432+
));
2433+
}
2434+
Err(e) => {
2435+
return Err(warp_utils::reject::custom_server_error(format!(
2436+
"Unable to obtain LightClientBootstrap instance: {e:?}"
2437+
)));
2438+
}
2439+
};
24392440

2440-
let mut state = chain
2441-
.get_state(&state_root, None)
2442-
.map_err(|_| {
2443-
warp_utils::reject::custom_server_error(
2444-
"Error retrieving state".to_string(),
2445-
)
2446-
})?
2447-
.ok_or_else(|| {
2448-
warp_utils::reject::custom_not_found(
2449-
"Light client bootstrap unavailable".to_string(),
2450-
)
2451-
})?;
2452-
let fork_name = state
2453-
.fork_name(&chain.spec)
2454-
.map_err(inconsistent_fork_rejection)?;
2455-
let bootstrap =
2456-
LightClientBootstrap::from_beacon_state(&mut state).map_err(|_| {
2457-
warp_utils::reject::custom_server_error(
2458-
"Failed to create light client bootstrap".to_string(),
2459-
)
2460-
})?;
24612441
match accept_header {
24622442
Some(api_types::Accept::Ssz) => Response::builder()
24632443
.status(200)
@@ -2469,10 +2449,11 @@ pub fn serve<T: BeaconChainTypes>(
24692449
e
24702450
))
24712451
}),
2472-
_ => Ok(
2473-
warp::reply::json(&api_types::GenericResponse::from(bootstrap))
2474-
.into_response(),
2475-
),
2452+
_ => Ok(warp::reply::json(&ForkVersionedResponse {
2453+
version: Some(fork_name),
2454+
data: bootstrap,
2455+
})
2456+
.into_response()),
24762457
}
24772458
.map(|resp| add_consensus_version_header(resp, fork_name))
24782459
})

beacon_node/network/src/network_beacon_processor/rpc_methods.rs

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use std::sync::Arc;
1818
use task_executor::TaskExecutor;
1919
use tokio_stream::StreamExt;
2020
use types::blob_sidecar::BlobIdentifier;
21-
use types::{
22-
light_client_bootstrap::LightClientBootstrap, Epoch, EthSpec, ForkName, Hash256, Slot,
23-
};
21+
use types::{Epoch, EthSpec, ForkName, Hash256, Slot};
2422

2523
impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
2624
/* Auxiliary functions */
@@ -304,66 +302,32 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
304302
request: LightClientBootstrapRequest,
305303
) {
306304
let block_root = request.root;
307-
let state_root = match self.chain.get_blinded_block(&block_root) {
308-
Ok(signed_block) => match signed_block {
309-
Some(signed_block) => signed_block.state_root(),
310-
None => {
311-
self.send_error_response(
312-
peer_id,
313-
RPCResponseErrorCode::ResourceUnavailable,
314-
"Bootstrap not available".into(),
315-
request_id,
316-
);
317-
return;
318-
}
319-
},
320-
Err(_) => {
321-
self.send_error_response(
322-
peer_id,
323-
RPCResponseErrorCode::ResourceUnavailable,
324-
"Bootstrap not available".into(),
325-
request_id,
326-
);
327-
return;
328-
}
329-
};
330-
let mut beacon_state = match self.chain.get_state(&state_root, None) {
331-
Ok(beacon_state) => match beacon_state {
332-
Some(state) => state,
333-
None => {
334-
self.send_error_response(
335-
peer_id,
336-
RPCResponseErrorCode::ResourceUnavailable,
337-
"Bootstrap not available".into(),
338-
request_id,
339-
);
340-
return;
341-
}
342-
},
343-
Err(_) => {
305+
match self.chain.get_light_client_bootstrap(&block_root) {
306+
Ok(Some((bootstrap, _))) => self.send_response(
307+
peer_id,
308+
Response::LightClientBootstrap(bootstrap),
309+
request_id,
310+
),
311+
Ok(None) => self.send_error_response(
312+
peer_id,
313+
RPCResponseErrorCode::ResourceUnavailable,
314+
"Bootstrap not available".into(),
315+
request_id,
316+
),
317+
Err(e) => {
344318
self.send_error_response(
345319
peer_id,
346320
RPCResponseErrorCode::ResourceUnavailable,
347321
"Bootstrap not available".into(),
348322
request_id,
349323
);
350-
return;
324+
error!(self.log, "Error getting LightClientBootstrap instance";
325+
"block_root" => ?block_root,
326+
"peer" => %peer_id,
327+
"error" => ?e
328+
)
351329
}
352330
};
353-
let Ok(bootstrap) = LightClientBootstrap::from_beacon_state(&mut beacon_state) else {
354-
self.send_error_response(
355-
peer_id,
356-
RPCResponseErrorCode::ResourceUnavailable,
357-
"Bootstrap not available".into(),
358-
request_id,
359-
);
360-
return;
361-
};
362-
self.send_response(
363-
peer_id,
364-
Response::LightClientBootstrap(bootstrap),
365-
request_id,
366-
)
367331
}
368332

369333
/// Handle a `BlocksByRange` request from the peer.

0 commit comments

Comments
 (0)