Skip to content

Commit 1ddd335

Browse files
committed
get state first and query by most recent block root
1 parent 5fda708 commit 1ddd335

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

beacon_node/beacon_chain/src/builder.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,22 @@ where
430430
weak_subj_state
431431
.build_all_caches(&self.spec)
432432
.map_err(|e| format!("Error building caches on checkpoint state: {e:?}"))?;
433-
434-
let computed_state_root = weak_subj_state
433+
weak_subj_state
435434
.update_tree_hash_cache()
436435
.map_err(|e| format!("Error computing checkpoint state root: {:?}", e))?;
437436

438-
if weak_subj_state_root != computed_state_root {
437+
let state_slot_block_root = weak_subj_state
438+
.get_block_root(weak_subj_state.slot())
439+
.map_err(|e| {
440+
format!(
441+
"Unable to get block root for slot {}: {e:?}",
442+
weak_subj_state.slot()
443+
)
444+
})?;
445+
if weak_subj_block_root != *state_slot_block_root {
439446
return Err(format!(
440-
"Snapshot state root does not match block, expected: {:?}, got: {:?}",
441-
weak_subj_state_root, computed_state_root
447+
"Snapshot state's most recent block root does not match block, expected: {:?}, got: {:?}",
448+
weak_subj_block_root, state_slot_block_root
442449
));
443450
}
444451

beacon_node/client/src/builder.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,25 @@ where
338338
None
339339
};
340340

341-
debug!(context.log(), "Downloading finalized block");
342-
// Find a suitable finalized block.
341+
debug!(
342+
context.log(),
343+
"Downloading finalized state";
344+
);
345+
let mut state = remote
346+
.get_debug_beacon_states_ssz::<TEthSpec>(StateId::Finalized, &spec)
347+
.await
348+
.map_err(|e| format!("Error loading checkpoint state from remote: {:?}", e))?
349+
.ok_or_else(|| "Checkpoint state missing from remote".to_string())?;
350+
351+
debug!(context.log(), "Downloaded finalized state"; "slot" => ?state.slot());
352+
353+
let block_root = state.get_block_root(state.slot()).map_err(|e| {
354+
format!("Unable to get block root for slot {}: {e:?}", state.slot())
355+
})?;
356+
357+
debug!(context.log(), "Downloading finalized block"; "block_root" => ?block_root);
343358
let block = remote
344-
.get_beacon_blocks_ssz::<TEthSpec>(BlockId::Finalized, &spec)
359+
.get_beacon_blocks_ssz::<TEthSpec>(BlockId::Root(*block_root), &spec)
345360
.await
346361
.map_err(|e| match e {
347362
ApiError::InvalidSsz(e) => format!(
@@ -355,29 +370,13 @@ where
355370

356371
debug!(context.log(), "Downloaded finalized block");
357372

358-
let state_root = block.state_root();
359-
debug!(
360-
context.log(),
361-
"Downloading finalized state";
362-
"state_root" => ?state_root
363-
);
364-
let mut state = remote
365-
.get_debug_beacon_states_ssz::<TEthSpec>(StateId::Slot(block.slot()), &spec)
366-
.await
367-
.map_err(|e| {
368-
format!(
369-
"Error loading checkpoint state from remote {:?}: {:?}",
370-
state_root, e
371-
)
372-
})?
373-
.ok_or_else(|| {
374-
format!("Checkpoint state missing from remote: {:?}", state_root)
375-
})?;
376-
377-
debug!(context.log(), "Downloaded finalized state");
373+
let epoch_boundary_slot = state.slot() % slots_per_epoch;
374+
if epoch_boundary_slot != 0 {
375+
debug!(context.log(), "Advancing state to epoch boundary"; "state_slot" => state.slot(), "epoch_boundary_slot" => epoch_boundary_slot);
376+
}
378377

379378
while state.slot() % slots_per_epoch != 0 {
380-
per_slot_processing(&mut state, Some(state_root), &spec)
379+
per_slot_processing(&mut state, None, &spec)
381380
.map_err(|e| format!("Error advancing state: {:?}", e))?;
382381
}
383382

@@ -390,7 +389,6 @@ where
390389
"block_slot" => block.slot(),
391390
"state_slot" => state.slot(),
392391
"block_root" => ?block.canonical_root(),
393-
"state_root" => ?state_root,
394392
);
395393

396394
let service =

consensus/proto_array/src/proto_array_fork_choice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ mod test_compute_deltas {
984984
};
985985

986986
let mut fc = ProtoArrayForkChoice::new::<MainnetEthSpec>(
987+
genesis_slot,
987988
genesis_slot,
988989
state_root,
989990
genesis_checkpoint,

consensus/state_processing/src/per_slot_processing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl From<ArithError> for Error {
2121
///
2222
/// If the root of the supplied `state` is known, then it can be passed as `state_root`. If
2323
/// `state_root` is `None`, the root of `state` will be computed using a cached tree hash.
24-
/// Providing the `state_root` makes this function several orders of magniude faster.
24+
/// Providing the `state_root` makes this function several orders of magnitude faster.
2525
pub fn per_slot_processing<T: EthSpec>(
2626
state: &mut BeaconState<T>,
2727
state_root: Option<Hash256>,

0 commit comments

Comments
 (0)