Files
prysm/beacon-chain/core/transition/skip_slot_cache.go
terence tsao ee5d75732d Add pkg crypto (#9603)
* Add pkg crypto

* Update go.yml

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-09-15 22:55:11 +00:00

33 lines
1.2 KiB
Go

package transition
import (
"context"
"errors"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
)
// SkipSlotCache exists for the unlikely scenario that is a large gap between the head state and
// the current slot. If the beacon chain were ever to be stalled for several epochs, it may be
// difficult or impossible to compute the appropriate beacon state for assignments within a
// reasonable amount of time.
var SkipSlotCache = cache.NewSkipSlotCache()
// The key for skip slot cache is mixed between state root and state slot.
// state root is in the mix to defend against different forks with same skip slots
// to hit the same cache. We don't want beacon states mixed up between different chains.
func cacheKey(ctx context.Context, state state.ReadOnlyBeaconState) ([32]byte, error) {
bh := state.LatestBlockHeader()
if bh == nil {
return [32]byte{}, errors.New("block head in state can't be nil")
}
r, err := bh.HashTreeRoot()
if err != nil {
return [32]byte{}, err
}
return hash.Hash(append(bytesutil.Bytes32(uint64(state.Slot())), r[:]...)), nil
}