2022-06-25 03:57:52 +00:00
|
|
|
package doublylinkedtree
|
2022-01-25 22:07:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-29 16:32:01 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2022-01-25 22:07:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewSlot mimics the implementation of `on_tick` in fork choice consensus spec.
|
|
|
|
// It resets the proposer boost root in fork choice, and it updates store's justified checkpoint
|
|
|
|
// if a better checkpoint on the store's finalized checkpoint chain.
|
|
|
|
// This should only be called at the start of every slot interval.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
//
|
2022-11-18 19:12:19 +00:00
|
|
|
// # Reset store.proposer_boost_root if this is a new slot
|
|
|
|
// if current_slot > previous_slot:
|
|
|
|
// store.proposer_boost_root = Root()
|
2022-01-25 22:07:57 +00:00
|
|
|
//
|
2022-11-18 19:12:19 +00:00
|
|
|
// # Not a new epoch, return
|
|
|
|
// if not (current_slot > previous_slot and compute_slots_since_epoch_start(current_slot) == 0):
|
|
|
|
// return
|
|
|
|
//
|
|
|
|
// # Update store.justified_checkpoint if a better checkpoint on the store.finalized_checkpoint chain
|
|
|
|
// if store.best_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
|
|
|
|
// finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
|
|
|
|
// ancestor_at_finalized_slot = get_ancestor(store, store.best_justified_checkpoint.root, finalized_slot)
|
|
|
|
// if ancestor_at_finalized_slot == store.finalized_checkpoint.root:
|
|
|
|
// store.justified_checkpoint = store.best_justified_checkpoint
|
2023-01-26 14:40:12 +00:00
|
|
|
func (f *ForkChoice) NewSlot(ctx context.Context, slot primitives.Slot) error {
|
2022-06-25 03:57:52 +00:00
|
|
|
// Reset proposer boost root
|
2023-05-22 15:59:16 +00:00
|
|
|
f.store.proposerBoostRoot = [32]byte{}
|
2022-01-25 22:07:57 +00:00
|
|
|
|
|
|
|
// Return if it's not a new epoch.
|
|
|
|
if !slots.IsEpochStart(slot) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update store.justified_checkpoint if a better checkpoint on the store.finalized_checkpoint chain
|
2023-03-16 17:27:30 +00:00
|
|
|
if err := f.updateUnrealizedCheckpoints(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not update unrealized checkpoints")
|
2022-06-29 23:37:21 +00:00
|
|
|
}
|
2022-09-08 14:41:10 +00:00
|
|
|
return f.store.prune(ctx)
|
2022-01-25 22:07:57 +00:00
|
|
|
}
|