mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
f8b4d8c57a
* Deprecate store WIP * fix spectests build * mock right interface * sync tests build * more tests builds * blockchain tests - TestFinalizedCheckpt_GenesisRootOk - TestCurrentJustifiedCheckpt_CanRetrieve - TestJustifiedCheckpt_GenesisRootOk - TestHeadRoot_CanRetrieve - TestHeadRoot_UseDB - TestService_ProcessAttestationsAndUpdateHead - TestService_VerifyWeakSubjectivityRoot - TestVerifyFinalizedConsistency_InconsistentRoot_ProtoArray - TestVerifyFinalizedConsistency_InconsistentRoot_DoublyLinkedTree - TestVerifyFinalizedConsistency_Ok - TestStore_OnBlock_ProtoArray - TestStore_OnBlock_DoublyLinkedTree - TestStore_OnBlockBatch_ProtoArray - TestStore_OnBlockBatch_DoublyLinkedTree - TestStore_OnBlockBatch_NotifyNewPayload - TestCachedPreState_CanGetFromStateSummary_ProtoArray - TestCachedPreState_CanGetFromStateSummary_DoublyLinkedTree * more blockchain tests - TestStore_OnBlockBatch_PruneOK_Protoarray - TestFillForkChoiceMissingBlocks_CanSave_ProtoArray - TestFillForkChoiceMissingBlocks_CanSave_DoublyLinkedTree - TestFillForkChoiceMissingBlocks_RootsMatch_Protoarray - TestFillForkChoiceMissingBlocks_RootsMatch_DoublyLinkedTree - TestFillForkChoiceMissingBlocks_FilterFinalized_ProtoArray - TestFillForkChoiceMissingBlocks_FilterFinalized_DoublyLinkedTree - TestVerifyBlkDescendant - Test_verifyBlkFinalizedSlot_invalidBlock - TestChainService_SaveHeadNoDB * update best justified from genesis * deal with nil head on saveHead * initialize prev justified checkpoint * update finalization correctly * update finalization logic * update finalization logic * track the wall clock on forkchoice spectests * export copies of checkpoints from blockchain package * do not use forkchoice's head on HeadRoot * Remove debug remain Co-authored-by: terencechain <terence@prysmaticlabs.com> * terence's review * add forkchoice types deps * wtf * debugging * init-sync: update justified and finalized checkpoints on db * call updateFinalized instead of only DB * remove debug symbols * safe copy headroot Co-authored-by: terencechain <terence@prysmaticlabs.com>
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/consensus-types/interfaces"
|
|
"github.com/prysmaticlabs/prysm/consensus-types/wrapper"
|
|
)
|
|
|
|
// This saves a beacon block to the initial sync blocks cache. It rate limits how many blocks
|
|
// the cache keeps in memory (2 epochs worth of blocks) and saves them to DB when it hits this limit.
|
|
func (s *Service) saveInitSyncBlock(ctx context.Context, r [32]byte, b interfaces.SignedBeaconBlock) error {
|
|
s.initSyncBlocksLock.Lock()
|
|
s.initSyncBlocks[r] = b
|
|
numBlocks := len(s.initSyncBlocks)
|
|
s.initSyncBlocksLock.Unlock()
|
|
if uint64(numBlocks) > initialSyncBlockCacheSize {
|
|
if err := s.cfg.BeaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
|
return err
|
|
}
|
|
s.clearInitSyncBlocks()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// This checks if a beacon block exists in the initial sync blocks cache using the root
|
|
// of the block.
|
|
func (s *Service) hasInitSyncBlock(r [32]byte) bool {
|
|
s.initSyncBlocksLock.RLock()
|
|
defer s.initSyncBlocksLock.RUnlock()
|
|
_, ok := s.initSyncBlocks[r]
|
|
return ok
|
|
}
|
|
|
|
// Returns true if a block for root `r` exists in the initial sync blocks cache or the DB.
|
|
func (s *Service) hasBlockInInitSyncOrDB(ctx context.Context, r [32]byte) bool {
|
|
if s.hasInitSyncBlock(r) {
|
|
return true
|
|
}
|
|
return s.cfg.BeaconDB.HasBlock(ctx, r)
|
|
}
|
|
|
|
// Returns block for a given root `r` from either the initial sync blocks cache or the DB.
|
|
// Error is returned if the block is not found in either cache or DB.
|
|
func (s *Service) getBlock(ctx context.Context, r [32]byte) (interfaces.SignedBeaconBlock, error) {
|
|
s.initSyncBlocksLock.RLock()
|
|
|
|
// Check cache first because it's faster.
|
|
b, ok := s.initSyncBlocks[r]
|
|
s.initSyncBlocksLock.RUnlock()
|
|
var err error
|
|
if !ok {
|
|
b, err = s.cfg.BeaconDB.Block(ctx, r)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not retrieve block from db")
|
|
}
|
|
}
|
|
if err := wrapper.BeaconBlockIsNil(b); err != nil {
|
|
return nil, errBlockNotFoundInCacheOrDB
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// This retrieves all the beacon blocks from the initial sync blocks cache, the returned
|
|
// blocks are unordered.
|
|
func (s *Service) getInitSyncBlocks() []interfaces.SignedBeaconBlock {
|
|
s.initSyncBlocksLock.RLock()
|
|
defer s.initSyncBlocksLock.RUnlock()
|
|
|
|
blks := make([]interfaces.SignedBeaconBlock, 0, len(s.initSyncBlocks))
|
|
for _, b := range s.initSyncBlocks {
|
|
blks = append(blks, b)
|
|
}
|
|
return blks
|
|
}
|
|
|
|
// This clears out the initial sync blocks cache.
|
|
func (s *Service) clearInitSyncBlocks() {
|
|
s.initSyncBlocksLock.Lock()
|
|
defer s.initSyncBlocksLock.Unlock()
|
|
s.initSyncBlocks = make(map[[32]byte]interfaces.SignedBeaconBlock)
|
|
}
|