diff --git a/beacon_node/beacon_chain/src/builder.rs b/beacon_node/beacon_chain/src/builder.rs index 8600d3a7628..ff47c7a2b81 100644 --- a/beacon_node/beacon_chain/src/builder.rs +++ b/beacon_node/beacon_chain/src/builder.rs @@ -374,8 +374,13 @@ where let fc_store = BeaconForkChoiceStore::get_forkchoice_store(store, &genesis); - let fork_choice = ForkChoice::from_genesis(fc_store, &genesis.beacon_block.message) - .map_err(|e| format!("Unable to build initialize ForkChoice: {:?}", e))?; + let fork_choice = ForkChoice::from_genesis( + fc_store, + genesis.beacon_block_root, + &genesis.beacon_block.message, + &genesis.beacon_state, + ) + .map_err(|e| format!("Unable to build initialize ForkChoice: {:?}", e))?; self.fork_choice = Some(fork_choice); self.genesis_time = Some(genesis.beacon_state.genesis_time); diff --git a/consensus/fork_choice/src/fork_choice.rs b/consensus/fork_choice/src/fork_choice.rs index 8be0b328d1f..f6c43ae429a 100644 --- a/consensus/fork_choice/src/fork_choice.rs +++ b/consensus/fork_choice/src/fork_choice.rs @@ -240,10 +240,18 @@ where /// Instantiates `Self` from the genesis parameters. pub fn from_genesis( fc_store: T, + genesis_block_root: Hash256, genesis_block: &BeaconBlock, + genesis_state: &BeaconState, ) -> Result> { let finalized_block_slot = genesis_block.slot; let finalized_block_state_root = genesis_block.state_root; + let current_epoch_shuffling_id = + ShufflingId::new(genesis_block_root, genesis_state, RelativeEpoch::Current) + .map_err(Error::BeaconStateError)?; + let next_epoch_shuffling_id = + ShufflingId::new(genesis_block_root, genesis_state, RelativeEpoch::Next) + .map_err(Error::BeaconStateError)?; let proto_array = ProtoArrayForkChoice::new( finalized_block_slot, @@ -251,6 +259,8 @@ where fc_store.justified_checkpoint().epoch, fc_store.finalized_checkpoint().epoch, fc_store.finalized_checkpoint().root, + current_epoch_shuffling_id, + next_epoch_shuffling_id, )?; Ok(Self { diff --git a/consensus/proto_array/src/fork_choice_test_definition.rs b/consensus/proto_array/src/fork_choice_test_definition.rs index cd38d017c90..9cac0bafb10 100644 --- a/consensus/proto_array/src/fork_choice_test_definition.rs +++ b/consensus/proto_array/src/fork_choice_test_definition.rs @@ -55,12 +55,15 @@ pub struct ForkChoiceTestDefinition { impl ForkChoiceTestDefinition { pub fn run(self) { + let junk_shuffling_id = ShufflingId::from_components(Epoch::new(0), Hash256::zero()); let mut fork_choice = ProtoArrayForkChoice::new( self.finalized_block_slot, Hash256::zero(), self.justified_epoch, self.finalized_epoch, self.finalized_root, + junk_shuffling_id.clone(), + junk_shuffling_id, ) .expect("should create fork choice struct"); diff --git a/consensus/proto_array/src/proto_array_fork_choice.rs b/consensus/proto_array/src/proto_array_fork_choice.rs index a76cbb3fcbf..e4cf5bbc67b 100644 --- a/consensus/proto_array/src/proto_array_fork_choice.rs +++ b/consensus/proto_array/src/proto_array_fork_choice.rs @@ -72,6 +72,8 @@ impl ProtoArrayForkChoice { justified_epoch: Epoch, finalized_epoch: Epoch, finalized_root: Hash256, + current_epoch_shuffling_id: ShufflingId, + next_epoch_shuffling_id: ShufflingId, ) -> Result { let mut proto_array = ProtoArray { prune_threshold: DEFAULT_PRUNE_THRESHOLD, @@ -89,12 +91,8 @@ impl ProtoArrayForkChoice { // We are using the finalized_root as the target_root, since it always lies on an // epoch boundary. target_root: finalized_root, - // TODO: explain why this is safe. - current_epoch_shuffling_id: ShufflingId::from_components( - finalized_epoch, - finalized_root, - ), - next_epoch_shuffling_id: ShufflingId::from_components(finalized_epoch, finalized_root), + current_epoch_shuffling_id, + next_epoch_shuffling_id, justified_epoch, finalized_epoch, }; @@ -351,7 +349,7 @@ mod test_compute_deltas { let finalized_desc = Hash256::from_low_u64_be(2); let not_finalized_desc = Hash256::from_low_u64_be(3); let unknown = Hash256::from_low_u64_be(4); - let shuffling_id = ShufflingId::from_components(Epoch::new(0), Hash256::zero()); + let junk_shuffling_id = ShufflingId::from_components(Epoch::new(0), Hash256::zero()); let mut fc = ProtoArrayForkChoice::new( genesis_slot, @@ -359,6 +357,8 @@ mod test_compute_deltas { genesis_epoch, genesis_epoch, finalized_root, + junk_shuffling_id.clone(), + junk_shuffling_id.clone(), ) .unwrap(); @@ -370,8 +370,8 @@ mod test_compute_deltas { parent_root: Some(finalized_root), state_root, target_root: finalized_root, - current_epoch_shuffling_id: shuffling_id.clone(), - next_epoch_shuffling_id: shuffling_id.clone(), + current_epoch_shuffling_id: junk_shuffling_id.clone(), + next_epoch_shuffling_id: junk_shuffling_id.clone(), justified_epoch: genesis_epoch, finalized_epoch: genesis_epoch, }) @@ -385,8 +385,8 @@ mod test_compute_deltas { parent_root: None, state_root, target_root: finalized_root, - current_epoch_shuffling_id: shuffling_id.clone(), - next_epoch_shuffling_id: shuffling_id.clone(), + current_epoch_shuffling_id: junk_shuffling_id.clone(), + next_epoch_shuffling_id: junk_shuffling_id.clone(), justified_epoch: genesis_epoch, finalized_epoch: genesis_epoch, })