2020-04-29 21:32:39 +00:00
|
|
|
// Package p2putils contains useful helpers for eth2 fork-related functionality.
|
2020-04-19 02:32:53 +00:00
|
|
|
package p2putils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-04-19 02:32:53 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-05-05 05:15:32 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-04-19 02:32:53 +00:00
|
|
|
"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)
|
|
|
|
|
2020-05-27 15:07:40 +00:00
|
|
|
forkData, err := Fork(currentEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return [4]byte{}, err
|
2020-04-19 02:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 15:07:40 +00:00
|
|
|
digest, err := helpers.ComputeForkDigest(forkData.CurrentVersion, genesisValidatorsRoot)
|
2020-04-19 02:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return [4]byte{}, err
|
|
|
|
}
|
|
|
|
return digest, nil
|
|
|
|
}
|
2020-05-05 05:15:32 +00:00
|
|
|
|
|
|
|
// Fork given a target epoch,
|
|
|
|
// returns the active fork version during this epoch.
|
|
|
|
func Fork(
|
2021-02-09 10:05:22 +00:00
|
|
|
targetEpoch types.Epoch,
|
2020-05-05 05:15:32 +00:00
|
|
|
) (*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
|
2021-02-09 10:05:22 +00:00
|
|
|
forkEpoch := types.Epoch(0)
|
2020-05-05 05:15:32 +00:00
|
|
|
for epoch, forkVersion := range scheduledForks {
|
|
|
|
if epoch <= targetEpoch {
|
|
|
|
previousForkVersion = retrievedForkVersion
|
|
|
|
retrievedForkVersion = forkVersion
|
|
|
|
forkEpoch = epoch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &pb.Fork{
|
|
|
|
PreviousVersion: previousForkVersion,
|
|
|
|
CurrentVersion: retrievedForkVersion,
|
|
|
|
Epoch: forkEpoch,
|
|
|
|
}, nil
|
|
|
|
}
|