Skip to content

Commit aaf6235

Browse files
committed
Revert "Rebuild from finalized state if get_head fails"
This reverts commit 17cf507.
1 parent ffb83c8 commit aaf6235

File tree

4 files changed

+30
-120
lines changed

4 files changed

+30
-120
lines changed

beacon_node/beacon_chain/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ where
355355
let fc_store = BeaconForkChoiceStore::get_forkchoice_store(store, &genesis);
356356
let current_slot = None;
357357

358-
let fork_choice = ForkChoice::from_anchor_state(
358+
let fork_choice = ForkChoice::from_anchor(
359359
fc_store,
360360
genesis.beacon_block_root,
361361
&genesis.beacon_block,
@@ -466,7 +466,7 @@ where
466466
let fc_store = BeaconForkChoiceStore::get_forkchoice_store(store, &snapshot);
467467

468468
let current_slot = Some(snapshot.beacon_block.slot());
469-
let fork_choice = ForkChoice::from_anchor_state(
469+
let fork_choice = ForkChoice::from_anchor(
470470
fc_store,
471471
snapshot.beacon_block_root,
472472
&snapshot.beacon_block,

beacon_node/beacon_chain/src/fork_revert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn reset_fork_choice_to_finalization<E: EthSpec, Hot: ItemStore<E>, Cold: It
145145

146146
let fc_store = BeaconForkChoiceStore::get_forkchoice_store(store.clone(), &finalized_snapshot);
147147

148-
let mut fork_choice = ForkChoice::from_anchor_state(
148+
let mut fork_choice = ForkChoice::from_anchor(
149149
fc_store,
150150
finalized_block_root,
151151
&finalized_snapshot.beacon_block,

beacon_node/beacon_chain/src/schema_change/migration_schema_v7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn update_with_reinitialized_fork_choice<T: BeaconChainTypes>(
4444
beacon_state: anchor_state,
4545
};
4646
let store = BeaconForkChoiceStore::get_forkchoice_store(db, &snapshot);
47-
let fork_choice = ForkChoice::from_anchor_state(
47+
let fork_choice = ForkChoice::from_anchor(
4848
store,
4949
anchor_block_root,
5050
&snapshot.beacon_block,

consensus/fork_choice/src/fork_choice.rs

Lines changed: 26 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub enum Error<T> {
5151
MissingFinalizedBlock {
5252
finalized_checkpoint: Checkpoint,
5353
},
54-
CannotCreateAnchorFromState(BeaconStateError),
5554
}
5655

5756
impl<T> From<InvalidAttestation> for Error<T> {
@@ -60,59 +59,6 @@ impl<T> From<InvalidAttestation> for Error<T> {
6059
}
6160
}
6261

63-
#[derive(Debug)]
64-
pub struct Anchor {
65-
finalized_block_slot: Slot,
66-
finalized_block_state_root: Hash256,
67-
finalized_block_execution_status: ExecutionStatus,
68-
current_epoch_shuffling_id: AttestationShufflingId,
69-
next_epoch_shuffling_id: AttestationShufflingId,
70-
}
71-
72-
impl Anchor {
73-
pub fn from_state<E: EthSpec>(
74-
anchor_state: &BeaconState<E>,
75-
anchor_state_root: Hash256,
76-
) -> Result<Self, BeaconStateError> {
77-
let finalized_block_slot = anchor_state.latest_block_header().slot;
78-
let finalized_block_root = anchor_state.latest_block_header().canonical_root();
79-
let finalized_block_state_root = if finalized_block_slot == anchor_state.slot() {
80-
anchor_state_root
81-
} else {
82-
anchor_state.latest_block_header().state_root
83-
};
84-
let finalized_block_execution_status =
85-
anchor_state.latest_execution_payload_header().map_or_else(
86-
|_| ExecutionStatus::irrelevant(),
87-
|payload_header| {
88-
if payload_header == &<_>::default() {
89-
// A default payload does not have execution enabled.
90-
ExecutionStatus::irrelevant()
91-
} else {
92-
// Assume that this payload is valid, since the anchor should be a trusted block and
93-
// state.
94-
ExecutionStatus::Valid(payload_header.block_hash)
95-
}
96-
},
97-
);
98-
let current_epoch_shuffling_id = AttestationShufflingId::new(
99-
finalized_block_root,
100-
anchor_state,
101-
RelativeEpoch::Current,
102-
)?;
103-
let next_epoch_shuffling_id =
104-
AttestationShufflingId::new(finalized_block_root, anchor_state, RelativeEpoch::Next)?;
105-
106-
Ok(Anchor {
107-
finalized_block_slot,
108-
finalized_block_state_root,
109-
finalized_block_execution_status,
110-
current_epoch_shuffling_id,
111-
next_epoch_shuffling_id,
112-
})
113-
}
114-
}
115-
11662
#[derive(Debug)]
11763
pub enum InvalidBlock {
11864
UnknownParent(Hash256),
@@ -365,8 +311,8 @@ where
365311
T: ForkChoiceStore<E>,
366312
E: EthSpec,
367313
{
368-
/// Instantiates `Self` from an anchor state (genesis or another finalized checkpoint).
369-
pub fn from_anchor_state(
314+
/// Instantiates `Self` from an anchor (genesis or another finalized checkpoint).
315+
pub fn from_anchor(
370316
fc_store: T,
371317
anchor_block_root: Hash256,
372318
anchor_block: &SignedBeaconBlock<E>,
@@ -407,32 +353,6 @@ where
407353
},
408354
);
409355

410-
let anchor = Anchor {
411-
finalized_block_slot,
412-
finalized_block_state_root,
413-
finalized_block_execution_status: execution_status,
414-
current_epoch_shuffling_id,
415-
next_epoch_shuffling_id,
416-
};
417-
418-
Self::from_anchor(fc_store, anchor, current_slot, spec)
419-
}
420-
421-
/// Instantiates `Self` from an `Anchor`.
422-
pub fn from_anchor(
423-
fc_store: T,
424-
anchor: Anchor,
425-
current_slot: Option<Slot>,
426-
spec: &ChainSpec,
427-
) -> Result<Self, Error<T::Error>> {
428-
let Anchor {
429-
finalized_block_slot,
430-
finalized_block_state_root,
431-
finalized_block_execution_status,
432-
current_epoch_shuffling_id,
433-
next_epoch_shuffling_id,
434-
} = anchor;
435-
436356
// If the current slot is not provided, use the value that was last provided to the store.
437357
let current_slot = current_slot.unwrap_or_else(|| fc_store.get_current_slot());
438358

@@ -443,7 +363,7 @@ where
443363
*fc_store.finalized_checkpoint(),
444364
current_epoch_shuffling_id,
445365
next_epoch_shuffling_id,
446-
finalized_block_execution_status,
366+
execution_status,
447367
)?;
448368

449369
let mut fork_choice = Self {
@@ -463,16 +383,31 @@ where
463383
};
464384

465385
// Ensure that `fork_choice.head_block_root` is updated.
466-
//
467-
// It's possible that this function will return an error if the only valid head with regard
468-
// to FFG checkpoints is invalid. When starting from an anchor state, this would mean that
469-
// we've started from an invalid anchor which should be impossible and is fundamentally
470-
// flawed.
471386
fork_choice.get_head(current_slot, spec)?;
472387

473388
Ok(fork_choice)
474389
}
475390

391+
/*
392+
/// Instantiates `Self` from some existing components.
393+
///
394+
/// This is useful if the existing components have been loaded from disk after a process
395+
/// restart.
396+
pub fn from_components(
397+
fc_store: T,
398+
proto_array: ProtoArrayForkChoice,
399+
queued_attestations: Vec<QueuedAttestation>,
400+
) -> Self {
401+
Self {
402+
fc_store,
403+
proto_array,
404+
queued_attestations,
405+
forkchoice_update_parameters: None,
406+
_phantom: PhantomData,
407+
}
408+
}
409+
*/
410+
476411
/// Returns cached information that can be used to issue a `forkchoiceUpdated` message to an
477412
/// execution engine.
478413
///
@@ -1292,34 +1227,9 @@ where
12921227
_phantom: PhantomData,
12931228
};
12941229

1295-
if fork_choice.get_head(current_slot, spec).is_ok() {
1296-
// We were successful in finding a head block, use the `fork_choice` we have already
1297-
// constructed.
1298-
Ok(fork_choice)
1299-
} else {
1300-
// Fork choice returned an error whilst getting the head, therefore there are no
1301-
// valid candidates for the head. Rebuild fork choice from the current finalized
1302-
// checkpoint, potentially losing some history for the sake of having a functioning
1303-
// client.
1304-
let fc_store = fork_choice.fc_store;
1305-
let proto_array = fork_choice.proto_array;
1306-
1307-
let finalized_checkpoint = *fc_store.finalized_checkpoint();
1308-
let finalized_block = proto_array.get_block(&finalized_checkpoint.root).ok_or(
1309-
Error::MissingFinalizedBlock {
1310-
finalized_checkpoint,
1311-
},
1312-
)?;
1313-
1314-
let anchor = Anchor {
1315-
finalized_block_slot: finalized_block.slot,
1316-
finalized_block_state_root: finalized_block.state_root,
1317-
finalized_block_execution_status: finalized_block.execution_status,
1318-
current_epoch_shuffling_id: finalized_block.current_epoch_shuffling_id,
1319-
next_epoch_shuffling_id: finalized_block.next_epoch_shuffling_id,
1320-
};
1321-
Self::from_anchor(fc_store, anchor, None, spec)
1322-
}
1230+
fork_choice.get_head(current_slot, spec)?;
1231+
1232+
Ok(fork_choice)
13231233
}
13241234

13251235
/// Takes a snapshot of `Self` and stores it in `PersistedForkChoice`, allowing this struct to

0 commit comments

Comments
 (0)