mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-06 22:23:56 -05:00
137 lines
4.8 KiB
Go
137 lines
4.8 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/state"
|
|
consensus_blocks "github.com/OffchainLabs/prysm/v7/consensus-types/blocks"
|
|
"github.com/OffchainLabs/prysm/v7/consensus-types/forkchoice"
|
|
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
|
|
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
|
|
"github.com/OffchainLabs/prysm/v7/runtime/version"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// CachedHeadRoot returns the corresponding value from Forkchoice
|
|
func (s *Service) CachedHeadRoot() [32]byte {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.CachedHeadRoot()
|
|
}
|
|
|
|
// GetProposerHead returns the corresponding value from forkchoice
|
|
func (s *Service) GetProposerHead() [32]byte {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.GetProposerHead()
|
|
}
|
|
|
|
// SetForkChoiceGenesisTime sets the genesis time in Forkchoice
|
|
func (s *Service) SetForkChoiceGenesisTime(timestamp time.Time) {
|
|
s.cfg.ForkChoiceStore.Lock()
|
|
defer s.cfg.ForkChoiceStore.Unlock()
|
|
s.cfg.ForkChoiceStore.SetGenesisTime(timestamp)
|
|
}
|
|
|
|
// HighestReceivedBlockSlot returns the corresponding value from forkchoice
|
|
func (s *Service) HighestReceivedBlockSlot() primitives.Slot {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.HighestReceivedBlockSlot()
|
|
}
|
|
|
|
// ReceivedBlocksLastEpoch returns the corresponding value from forkchoice
|
|
func (s *Service) ReceivedBlocksLastEpoch() (uint64, error) {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.ReceivedBlocksLastEpoch()
|
|
}
|
|
|
|
// InsertNode is a wrapper for node insertion which is self locked
|
|
func (s *Service) InsertNode(ctx context.Context, st state.BeaconState, block consensus_blocks.ROBlock) error {
|
|
s.cfg.ForkChoiceStore.Lock()
|
|
defer s.cfg.ForkChoiceStore.Unlock()
|
|
return s.cfg.ForkChoiceStore.InsertNode(ctx, st, block)
|
|
}
|
|
|
|
// ForkChoiceDump returns the corresponding value from forkchoice
|
|
func (s *Service) ForkChoiceDump(ctx context.Context) (*forkchoice.Dump, error) {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.ForkChoiceDump(ctx)
|
|
}
|
|
|
|
// NewSlot returns the corresponding value from forkchoice
|
|
func (s *Service) NewSlot(ctx context.Context, slot primitives.Slot) error {
|
|
s.cfg.ForkChoiceStore.Lock()
|
|
defer s.cfg.ForkChoiceStore.Unlock()
|
|
return s.cfg.ForkChoiceStore.NewSlot(ctx, slot)
|
|
}
|
|
|
|
// ProposerBoost wraps the corresponding method from forkchoice
|
|
func (s *Service) ProposerBoost() [32]byte {
|
|
s.cfg.ForkChoiceStore.Lock()
|
|
defer s.cfg.ForkChoiceStore.Unlock()
|
|
return s.cfg.ForkChoiceStore.ProposerBoost()
|
|
}
|
|
|
|
// ChainHeads returns all possible chain heads (leaves of fork choice tree).
|
|
// Heads roots and heads slots are returned.
|
|
func (s *Service) ChainHeads() ([][32]byte, []primitives.Slot) {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.Tips()
|
|
}
|
|
|
|
// UnrealizedJustifiedPayloadBlockHash returns unrealized justified payload block hash from forkchoice.
|
|
func (s *Service) UnrealizedJustifiedPayloadBlockHash() [32]byte {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.UnrealizedJustifiedPayloadBlockHash()
|
|
}
|
|
|
|
// FinalizedBlockHash returns finalized payload block hash from forkchoice.
|
|
func (s *Service) FinalizedBlockHash() [32]byte {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.FinalizedPayloadBlockHash()
|
|
}
|
|
|
|
// ParentRoot wraps a call to the corresponding method in forkchoice
|
|
func (s *Service) ParentRoot(root [32]byte) ([32]byte, error) {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.ParentRoot(root)
|
|
}
|
|
|
|
// hashForGenesisBlock returns the right hash for the genesis block
|
|
func (s *Service) hashForGenesisBlock(ctx context.Context, root [32]byte) ([]byte, error) {
|
|
genRoot, err := s.cfg.BeaconDB.GenesisBlockRoot(ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not get genesis block root")
|
|
}
|
|
if root != genRoot {
|
|
return nil, errNotGenesisRoot
|
|
}
|
|
st, err := s.cfg.BeaconDB.GenesisState(ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not get genesis state")
|
|
}
|
|
if st.Version() < version.Bellatrix {
|
|
return nil, nil
|
|
}
|
|
header, err := st.LatestExecutionPayloadHeader()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not get latest execution payload header")
|
|
}
|
|
return bytesutil.SafeCopyBytes(header.BlockHash()), nil
|
|
}
|
|
|
|
// DependentRoot wraps the corresponding method in forkchoice
|
|
func (s *Service) DependentRoot(epoch primitives.Epoch) ([32]byte, error) {
|
|
s.cfg.ForkChoiceStore.RLock()
|
|
defer s.cfg.ForkChoiceStore.RUnlock()
|
|
return s.cfg.ForkChoiceStore.DependentRoot(epoch)
|
|
}
|