Skip to content

Commit

Permalink
filter block tree changes
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed Jun 15, 2022
1 parent dc51a24 commit da9047c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions consensus/fork_choice/src/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ where
self.fc_store.set_proposer_boost_root(block_root);
}

// Update store with checkpoints if necessary
self.update_checkpoints(
state.current_justified_checkpoint(),
state.finalized_checkpoint(),
Expand All @@ -602,7 +603,7 @@ where

// Update unrealized justified/finalized checkpoints.
let (unrealized_justified_checkpoint, unrealized_finalized_checkpoint) = {
if !matches!(block, BeaconBlock::Base(_)) {
if !matches!(block, BeaconBlock::Merge(_)) {
let (justifiable_beacon_state, _) =
state_processing::per_epoch_processing::altair::process_justifiable(
state, spec,
Expand Down Expand Up @@ -717,7 +718,7 @@ where
execution_status,
unrealized_justified_checkpoint,
unrealized_finalized_checkpoint,
})?;
}, current_slot)?;

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion consensus/proto_array/src/fork_choice_test_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl ForkChoiceTestDefinition {
finalized_checkpoint,
&justified_state_balances,
Hash256::zero(),
Slot::new(0),
&spec,
)
.map_err(|e| e)
Expand Down Expand Up @@ -130,6 +131,7 @@ impl ForkChoiceTestDefinition {
finalized_checkpoint,
&justified_state_balances,
proposer_boost_root,
Slot::new(0),
&spec,
)
.map_err(|e| e)
Expand All @@ -154,6 +156,7 @@ impl ForkChoiceTestDefinition {
finalized_checkpoint,
&justified_state_balances,
Hash256::zero(),
Slot::new(0),
&spec,
);

Expand Down Expand Up @@ -195,7 +198,7 @@ impl ForkChoiceTestDefinition {
unrealized_justified_checkpoint: None,
unrealized_finalized_checkpoint: None,
};
fork_choice.process_block(block).unwrap_or_else(|e| {
fork_choice.process_block(block, slot).unwrap_or_else(|e| {
panic!(
"process_block op at index {} returned error: {:?}",
op_index, e
Expand Down
13 changes: 3 additions & 10 deletions consensus/proto_array/src/proto_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl ProtoArray {
/// Register a block with the fork choice.
///
/// It is only sane to supply a `None` parent for the genesis block.
pub fn on_block(&mut self, block: Block) -> Result<(), Error> {
pub fn on_block(&mut self, block: Block, current_slot:Slot) -> Result<(), Error> {
// If the block is already known, simply ignore it.
if self.indices.contains_key(&block.root) {
return Ok(());
Expand Down Expand Up @@ -338,7 +338,7 @@ impl ProtoArray {
self.nodes.push(node.clone());

if let Some(parent_index) = node.parent {
self.maybe_update_best_child_and_descendant(parent_index, node_index)?;
self.maybe_update_best_child_and_descendant(parent_index, node_index, current_slot)?;

if matches!(block.execution_status, ExecutionStatus::Valid(_)) {
self.propagate_execution_payload_validation_by_index(parent_index)?;
Expand Down Expand Up @@ -857,8 +857,6 @@ impl ProtoArray {
return false;
}

let node_epoch = node.slot.epoch(MainnetEthSpec::slots_per_epoch());

let checkpoint_match_predicate =
|node_justified_checkpoint: Checkpoint, node_finalized_checkpoint: Checkpoint| {
let correct_justified = node_justified_checkpoint == self.justified_checkpoint
Expand All @@ -879,17 +877,12 @@ impl ProtoArray {
node.justified_checkpoint,
node.finalized_checkpoint,
) {
if justified_checkpoint.epoch < self.justified_checkpoint.epoch
&& finalized_checkpoint.epoch < self.finalized_checkpoint.epoch
if node.slot.epoch(MainnetEthSpec::slots_per_epoch()) < current_slot.epoch(MainnetEthSpec::slots_per_epoch())
{
checkpoint_match_predicate(
unrealized_justified_checkpoint,
unrealized_finalized_checkpoint,
)
} else if justified_checkpoint.epoch < self.justified_checkpoint.epoch {
checkpoint_match_predicate(unrealized_justified_checkpoint, finalized_checkpoint)
} else if finalized_checkpoint.epoch < self.finalized_checkpoint.epoch {
checkpoint_match_predicate(justified_checkpoint, unrealized_finalized_checkpoint)
} else {
checkpoint_match_predicate(justified_checkpoint, finalized_checkpoint)
}
Expand Down
8 changes: 4 additions & 4 deletions consensus/proto_array/src/proto_array_fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl ProtoArrayForkChoice {
};

proto_array
.on_block(block)
.on_block(block, finalized_block_slot)
.map_err(|e| format!("Failed to add finalized block to proto_array: {:?}", e))?;

Ok(Self {
Expand Down Expand Up @@ -246,13 +246,13 @@ impl ProtoArrayForkChoice {
Ok(())
}

pub fn process_block(&mut self, block: Block) -> Result<(), String> {
pub fn process_block(&mut self, block: Block, current_slot:Slot) -> Result<(), String> {
if block.parent_root.is_none() {
return Err("Missing parent root".to_string());
}

self.proto_array
.on_block(block)
.on_block(block, current_slot)
.map_err(|e| format!("process_block_error: {:?}", e))
}

Expand Down Expand Up @@ -292,7 +292,7 @@ impl ProtoArrayForkChoice {
*old_balances = new_balances.to_vec();

self.proto_array
.find_head(&justified_checkpoint.root)
.find_head(&justified_checkpoint.root, current_slot)
.map_err(|e| format!("find_head failed: {:?}", e))
}

Expand Down
32 changes: 19 additions & 13 deletions testing/ef_tests/src/cases/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,27 @@ impl<E: EthSpec> Tester<E> {
}

pub fn set_tick(&self, tick: u64) {
self.harness
.chain
.slot_clock
.set_current_time(Duration::from_secs(tick));

// Compute the slot time manually to ensure the slot clock is correct.
let slot = self.tick_to_slot(tick).unwrap();
assert_eq!(slot, self.harness.chain.slot().unwrap());
// get current slot, get difference, call update_time on every slot

self.harness
.chain
.fork_choice
.write()
.update_time(slot)
.unwrap();
let slot = self.harness.chain.slot().unwrap();
let new_slots = tick.checked_div(self.spec.seconds_per_slot).unwrap();

for i in slot.as_u64()..new_slots {
let new_slot = i + 1;

self.harness
.chain
.slot_clock
.set_slot(new_slot);

self.harness
.chain
.fork_choice
.write()
.update_time(Slot::new(new_slot))
.unwrap();
}
}

pub fn process_block(&self, block: SignedBeaconBlock<E>, valid: bool) -> Result<(), Error> {
Expand Down
1 change: 1 addition & 0 deletions testing/ef_tests/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub trait Handler {
.filter_map(as_directory)
.map(|test_case_dir| {
let path = test_case_dir.path();
dbg!(&path);
let case = Self::Case::load_from_dir(&path, fork_name).expect("test should load");
(path, case)
})
Expand Down

0 comments on commit da9047c

Please sign in to comment.