mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 20:50:05 +00:00
9ea3f58a5e
* first cherry pick * second * new endpoint * fork digest by epoch * fork and sig verify * fix test * fix comment * gaz * remove unused param * remove space * moved to shared attestation_utils.go * raul comment * gaz * goimports * fix import cycle * fix test * shorter helper method * add another helper function * fix comment * param fix * gaz * fix error * Update beacon-chain/core/blocks/block_operations.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * update pseudocode * remove teardown Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
// Package p2putils contains useful helpers for eth2 fork-related functionality.
|
|
package p2putils
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
// CreateForkDigest creates a fork digest from a genesis time and genesis
|
|
// validators root, utilizing the current slot to determine
|
|
// the active fork version in the node.
|
|
func CreateForkDigest(
|
|
genesisTime time.Time,
|
|
genesisValidatorsRoot []byte,
|
|
) ([4]byte, error) {
|
|
if genesisTime.IsZero() {
|
|
return [4]byte{}, errors.New("genesis time is not set")
|
|
}
|
|
if len(genesisValidatorsRoot) == 0 {
|
|
return [4]byte{}, errors.New("genesis validators root is not set")
|
|
}
|
|
currentSlot := helpers.SlotsSince(genesisTime)
|
|
currentEpoch := helpers.SlotToEpoch(currentSlot)
|
|
|
|
// We retrieve a list of scheduled forks by epoch.
|
|
// We loop through the keys in this map to determine the current
|
|
// fork version based on the current, time-based epoch number
|
|
// since the genesis time.
|
|
currentForkVersion := params.BeaconConfig().GenesisForkVersion
|
|
scheduledForks := params.BeaconConfig().ForkVersionSchedule
|
|
for epoch, forkVersion := range scheduledForks {
|
|
if epoch <= currentEpoch {
|
|
currentForkVersion = forkVersion
|
|
}
|
|
}
|
|
|
|
digest, err := helpers.ComputeForkDigest(currentForkVersion, genesisValidatorsRoot)
|
|
if err != nil {
|
|
return [4]byte{}, err
|
|
}
|
|
return digest, nil
|
|
}
|
|
|
|
// Fork given a target epoch,
|
|
// returns the active fork version during this epoch.
|
|
func Fork(
|
|
targetEpoch uint64,
|
|
) (*pb.Fork, error) {
|
|
// We retrieve a list of scheduled forks by epoch.
|
|
// We loop through the keys in this map to determine the current
|
|
// fork version based on the requested epoch.
|
|
retrievedForkVersion := params.BeaconConfig().GenesisForkVersion
|
|
previousForkVersion := params.BeaconConfig().GenesisForkVersion
|
|
scheduledForks := params.BeaconConfig().ForkVersionSchedule
|
|
forkEpoch := uint64(0)
|
|
for epoch, forkVersion := range scheduledForks {
|
|
if epoch <= targetEpoch {
|
|
previousForkVersion = retrievedForkVersion
|
|
retrievedForkVersion = forkVersion
|
|
forkEpoch = epoch
|
|
|
|
}
|
|
}
|
|
|
|
return &pb.Fork{
|
|
PreviousVersion: previousForkVersion,
|
|
CurrentVersion: retrievedForkVersion,
|
|
Epoch: forkEpoch,
|
|
}, nil
|
|
}
|