2020-02-18 21:10:54 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2022-04-29 07:56:31 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
|
2020-02-18 21:10:54 +00:00
|
|
|
)
|
|
|
|
|
2022-06-25 03:57:52 +00:00
|
|
|
// 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.
|
2023-02-09 09:23:32 +00:00
|
|
|
func (s *Service) saveInitSyncBlock(ctx context.Context, r [32]byte, b interfaces.ReadOnlySignedBeaconBlock) error {
|
2020-03-30 18:04:10 +00:00
|
|
|
s.initSyncBlocksLock.Lock()
|
|
|
|
s.initSyncBlocks[r] = b
|
2022-06-25 03:57:52 +00:00
|
|
|
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
|
2020-03-30 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-04-29 07:56:31 +00:00
|
|
|
// 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.
|
2023-02-09 09:23:32 +00:00
|
|
|
func (s *Service) getBlock(ctx context.Context, r [32]byte) (interfaces.ReadOnlySignedBeaconBlock, error) {
|
2020-03-30 18:04:10 +00:00
|
|
|
s.initSyncBlocksLock.RLock()
|
2022-04-29 07:56:31 +00:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
if err := blocks.BeaconBlockIsNil(b); err != nil {
|
2022-04-29 07:56:31 +00:00
|
|
|
return nil, errBlockNotFoundInCacheOrDB
|
|
|
|
}
|
|
|
|
return b, nil
|
2020-03-30 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This retrieves all the beacon blocks from the initial sync blocks cache, the returned
|
|
|
|
// blocks are unordered.
|
2023-02-09 09:23:32 +00:00
|
|
|
func (s *Service) getInitSyncBlocks() []interfaces.ReadOnlySignedBeaconBlock {
|
2020-03-30 18:04:10 +00:00
|
|
|
s.initSyncBlocksLock.RLock()
|
|
|
|
defer s.initSyncBlocksLock.RUnlock()
|
|
|
|
|
2023-02-09 09:23:32 +00:00
|
|
|
blks := make([]interfaces.ReadOnlySignedBeaconBlock, 0, len(s.initSyncBlocks))
|
2020-03-30 18:04:10 +00:00
|
|
|
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()
|
2023-02-09 09:23:32 +00:00
|
|
|
s.initSyncBlocks = make(map[[32]byte]interfaces.ReadOnlySignedBeaconBlock)
|
2020-03-30 18:04:10 +00:00
|
|
|
}
|