Skip to content

Commit 8ef297b

Browse files
committed
refactor get_light_client_update logic
1 parent 8c1f980 commit 8ef297b

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

beacon_node/beacon_chain/src/light_client_server_cache.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,13 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
200200

201201
// Spec: Full nodes SHOULD provide the best derivable LightClientUpdate (according to is_better_update)
202202
// for each sync committee period
203-
let prev_light_client_update = match &self.latest_light_client_update.read().clone() {
204-
Some(prev_light_client_update) => Some(prev_light_client_update.clone()),
205-
None => self.get_light_client_update(&store, sync_period, chain_spec)?,
206-
};
203+
let prev_light_client_update =
204+
self.get_light_client_update(&store, sync_period, chain_spec)?;
207205

208206
let should_persist_light_client_update =
209207
if let Some(prev_light_client_update) = prev_light_client_update {
210-
let prev_sync_period = prev_light_client_update
211-
.signature_slot()
212-
.epoch(T::EthSpec::slots_per_epoch())
213-
.sync_committee_period(chain_spec)?;
214-
215-
if sync_period != prev_sync_period {
216-
true
217-
} else {
218-
prev_light_client_update
219-
.is_better_light_client_update(&new_light_client_update, chain_spec)?
220-
}
208+
prev_light_client_update
209+
.is_better_light_client_update(&new_light_client_update, chain_spec)?
221210
} else {
222211
true
223212
};
@@ -293,9 +282,11 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
293282
Ok(())
294283
}
295284

296-
// Used to fetch the most recently persisted "best" light client update.
297-
// Should not be used outside the light client server, as it also caches the fetched
298-
// light client update.
285+
/// Used to fetch the most recently persisted light client update for the given `sync_committee_period`.
286+
/// It first checks the `latest_light_client_update` cache before querying the db.
287+
///
288+
/// Note: Should not be used outside the light client server, as it also caches the fetched
289+
/// light client update.
299290
fn get_light_client_update(
300291
&self,
301292
store: &BeaconStore<T>,
@@ -457,9 +448,9 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
457448
chain_spec: &ChainSpec,
458449
) -> Result<Option<(LightClientBootstrap<T::EthSpec>, ForkName)>, BeaconChainError> {
459450
let Some(block) = store.get_blinded_block(block_root)? else {
460-
return Err(BeaconChainError::LightClientBootstrapError(
461-
"Block not found".to_string(),
462-
));
451+
return Err(BeaconChainError::LightClientBootstrapError(format!(
452+
"Block root {block_root} not found"
453+
)));
463454
};
464455

465456
let (_, slot) = (block.state_root(), block.slot());
@@ -474,16 +465,22 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
474465
let Some(current_sync_committee_branch) =
475466
self.get_sync_committee_branch(store, block_root)?
476467
else {
477-
return Ok(None);
468+
return Err(BeaconChainError::LightClientBootstrapError(format!(
469+
"Sync committee branch for block root {block_root} not found"
470+
)));
478471
};
479472

480473
if sync_committee_period > finalized_period {
481-
return Ok(None);
474+
return Err(BeaconChainError::LightClientBootstrapError(
475+
format!("The blocks sync committee period {sync_committee_period} is greater than the current finalized period {finalized_period}"),
476+
));
482477
}
483478

484479
let Some(current_sync_committee) = self.get_sync_committee(store, sync_committee_period)?
485480
else {
486-
return Ok(None);
481+
return Err(BeaconChainError::LightClientBootstrapError(format!(
482+
"Sync committee for block root {block_root} not found"
483+
)));
487484
};
488485

489486
let light_client_bootstrap = LightClientBootstrap::new(

0 commit comments

Comments
 (0)