github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/state/skip_slot_cache.go (about)

     1  package state
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/prysmaticlabs/prysm/beacon-chain/cache"
     8  	iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
     9  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    10  	"github.com/prysmaticlabs/prysm/shared/hashutil"
    11  )
    12  
    13  // SkipSlotCache exists for the unlikely scenario that is a large gap between the head state and
    14  // the current slot. If the beacon chain were ever to be stalled for several epochs, it may be
    15  // difficult or impossible to compute the appropriate beacon state for assignments within a
    16  // reasonable amount of time.
    17  var SkipSlotCache = cache.NewSkipSlotCache()
    18  
    19  // The key for skip slot cache is mixed between state root and state slot.
    20  // state root is in the mix to defend against different forks with same skip slots
    21  // to hit the same cache. We don't want beacon states mixed up between different chains.
    22  func cacheKey(ctx context.Context, state iface.ReadOnlyBeaconState) ([32]byte, error) {
    23  	bh := state.LatestBlockHeader()
    24  	if bh == nil {
    25  		return [32]byte{}, errors.New("block head in state can't be nil")
    26  	}
    27  	r, err := bh.HashTreeRoot()
    28  	if err != nil {
    29  		return [32]byte{}, err
    30  	}
    31  	return hashutil.Hash(append(bytesutil.Bytes32(uint64(state.Slot())), r[:]...)), nil
    32  }