2022-10-08 14:15:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-29 22:20:09 +00:00
|
|
|
package fork
|
|
|
|
|
|
|
|
import (
|
2023-01-07 11:25:28 +00:00
|
|
|
"encoding/binary"
|
2022-09-29 22:20:09 +00:00
|
|
|
"errors"
|
2023-01-24 16:36:02 +00:00
|
|
|
"fmt"
|
2022-09-29 22:20:09 +00:00
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-05-11 11:54:20 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/types/ssz"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
2023-01-30 13:39:56 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2022-10-29 19:51:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
2022-09-29 22:20:09 +00:00
|
|
|
)
|
|
|
|
|
2023-06-01 23:21:08 +00:00
|
|
|
var NO_GENESIS_TIME_ERR error = errors.New("genesis time is not set")
|
|
|
|
var NO_VALIDATOR_ROOT_HASH error = errors.New("genesis validators root is not set")
|
|
|
|
|
2023-01-24 16:36:02 +00:00
|
|
|
func ForkDigestVersion(digest [4]byte, b *clparams.BeaconChainConfig, genesisValidatorRoot libcommon.Hash) (clparams.StateVersion, error) {
|
|
|
|
var (
|
2023-05-15 18:07:27 +00:00
|
|
|
phase0ForkDigest, altairForkDigest, bellatrixForkDigest, capellaForkDigest, denebForkDigest [4]byte
|
|
|
|
err error
|
2023-01-24 16:36:02 +00:00
|
|
|
)
|
|
|
|
phase0ForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.GenesisForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
altairForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.AltairForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bellatrixForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.BellatrixForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
capellaForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.CapellaForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-15 18:07:27 +00:00
|
|
|
|
|
|
|
denebForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.DenebForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-24 16:36:02 +00:00
|
|
|
switch digest {
|
|
|
|
case phase0ForkDigest:
|
|
|
|
return clparams.Phase0Version, nil
|
|
|
|
case altairForkDigest:
|
|
|
|
return clparams.AltairVersion, nil
|
|
|
|
case bellatrixForkDigest:
|
|
|
|
return clparams.BellatrixVersion, nil
|
|
|
|
case capellaForkDigest:
|
|
|
|
return clparams.CapellaVersion, nil
|
2023-05-15 18:07:27 +00:00
|
|
|
case denebForkDigest:
|
|
|
|
return clparams.DenebVersion, nil
|
2023-01-24 16:36:02 +00:00
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("invalid state version")
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:20:09 +00:00
|
|
|
func ComputeForkDigest(
|
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
|
|
|
) ([4]byte, error) {
|
|
|
|
if genesisConfig.GenesisTime == 0 {
|
2023-06-01 23:21:08 +00:00
|
|
|
return [4]byte{}, NO_GENESIS_TIME_ERR
|
2022-09-29 22:20:09 +00:00
|
|
|
}
|
2023-01-13 18:12:18 +00:00
|
|
|
if genesisConfig.GenesisValidatorRoot == (libcommon.Hash{}) {
|
2023-06-01 23:21:08 +00:00
|
|
|
return [4]byte{}, NO_VALIDATOR_ROOT_HASH
|
2022-09-29 22:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
// Retrieve current fork version.
|
2023-01-23 23:03:48 +00:00
|
|
|
currentForkVersion := utils.Uint32ToBytes4(beaconConfig.GenesisForkVersion)
|
2022-09-29 22:20:09 +00:00
|
|
|
for _, fork := range forkList(beaconConfig.ForkVersionSchedule) {
|
|
|
|
if currentEpoch >= fork.epoch {
|
|
|
|
currentForkVersion = fork.version
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-12-23 17:39:44 +00:00
|
|
|
return ComputeForkDigestForVersion(currentForkVersion, genesisConfig.GenesisValidatorRoot)
|
2022-09-29 22:20:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:51:19 +00:00
|
|
|
func ComputeNextForkDigest(
|
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
|
|
|
) ([4]byte, error) {
|
|
|
|
if genesisConfig.GenesisTime == 0 {
|
2023-06-01 23:21:08 +00:00
|
|
|
return [4]byte{}, NO_GENESIS_TIME_ERR
|
2023-03-17 21:51:19 +00:00
|
|
|
}
|
|
|
|
if genesisConfig.GenesisValidatorRoot == (libcommon.Hash{}) {
|
2023-06-01 23:21:08 +00:00
|
|
|
return [4]byte{}, NO_VALIDATOR_ROOT_HASH
|
2023-03-17 21:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
// Retrieve next fork version.
|
|
|
|
nextForkIndex := 0
|
|
|
|
forkList := forkList(beaconConfig.ForkVersionSchedule)
|
|
|
|
for _, fork := range forkList {
|
|
|
|
if currentEpoch >= fork.epoch {
|
|
|
|
nextForkIndex++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2023-03-19 20:13:53 +00:00
|
|
|
nextForkIndex--
|
|
|
|
if nextForkIndex == len(forkList)-1 {
|
|
|
|
return [4]byte{}, nil
|
2023-03-17 21:51:19 +00:00
|
|
|
}
|
2023-03-19 20:13:53 +00:00
|
|
|
nextForkIndex++
|
2023-03-17 21:51:19 +00:00
|
|
|
return ComputeForkDigestForVersion(forkList[nextForkIndex].version, genesisConfig.GenesisValidatorRoot)
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:20:09 +00:00
|
|
|
type fork struct {
|
|
|
|
epoch uint64
|
|
|
|
version [4]byte
|
|
|
|
}
|
|
|
|
|
2023-09-05 20:37:18 +00:00
|
|
|
func forkList(schedule map[libcommon.Bytes4]uint64) (f []fork) {
|
2022-09-29 22:20:09 +00:00
|
|
|
for version, epoch := range schedule {
|
|
|
|
f = append(f, fork{epoch: epoch, version: version})
|
|
|
|
}
|
|
|
|
sort.Slice(f, func(i, j int) bool {
|
|
|
|
return f[i].epoch < f[j].epoch
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 17:39:44 +00:00
|
|
|
func ComputeForkDigestForVersion(currentVersion [4]byte, genesisValidatorsRoot [32]byte) (digest [4]byte, err error) {
|
2023-01-13 18:12:18 +00:00
|
|
|
var currentVersion32 libcommon.Hash
|
2023-01-07 11:25:28 +00:00
|
|
|
copy(currentVersion32[:], currentVersion[:])
|
2023-11-22 12:24:35 +00:00
|
|
|
dataRoot := utils.Sha256(currentVersion32[:], genesisValidatorsRoot[:])
|
2022-09-29 22:20:09 +00:00
|
|
|
// copy first four bytes to output
|
|
|
|
copy(digest[:], dataRoot[:4])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComputeForkId(
|
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
|
|
|
) ([]byte, error) {
|
|
|
|
digest, err := ComputeForkDigest(beaconConfig, genesisConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
|
|
|
|
if time.Now().Unix() < int64(genesisConfig.GenesisTime) {
|
|
|
|
currentEpoch = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextForkVersion [4]byte
|
|
|
|
nextForkEpoch := uint64(math.MaxUint64)
|
|
|
|
for _, fork := range forkList(beaconConfig.ForkVersionSchedule) {
|
|
|
|
if currentEpoch < fork.epoch {
|
|
|
|
nextForkVersion = fork.version
|
|
|
|
nextForkEpoch = fork.epoch
|
|
|
|
break
|
|
|
|
}
|
|
|
|
nextForkVersion = fork.version
|
|
|
|
}
|
|
|
|
|
2023-01-07 11:25:28 +00:00
|
|
|
enrForkId := make([]byte, 16)
|
|
|
|
copy(enrForkId, digest[:])
|
|
|
|
copy(enrForkId[4:], nextForkVersion[:])
|
|
|
|
binary.BigEndian.PutUint64(enrForkId[8:], nextForkEpoch)
|
|
|
|
|
|
|
|
return enrForkId, nil
|
2022-09-29 22:20:09 +00:00
|
|
|
}
|
2022-09-30 21:53:54 +00:00
|
|
|
|
2022-10-14 13:24:44 +00:00
|
|
|
func GetLastFork(
|
2022-09-30 21:53:54 +00:00
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
2022-10-14 13:24:44 +00:00
|
|
|
) [4]byte {
|
2022-09-30 21:53:54 +00:00
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
// Retrieve current fork version.
|
2023-01-23 23:03:48 +00:00
|
|
|
currentFork := utils.Uint32ToBytes4(beaconConfig.GenesisForkVersion)
|
2022-09-30 21:53:54 +00:00
|
|
|
for _, fork := range forkList(beaconConfig.ForkVersionSchedule) {
|
|
|
|
if currentEpoch >= fork.epoch {
|
2022-10-14 13:24:44 +00:00
|
|
|
currentFork = fork.version
|
2022-09-30 21:53:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-10-14 13:24:44 +00:00
|
|
|
return currentFork
|
2022-09-30 21:53:54 +00:00
|
|
|
}
|
2022-10-08 14:15:44 +00:00
|
|
|
|
2022-10-14 13:24:44 +00:00
|
|
|
func ComputeDomain(
|
|
|
|
domainType []byte,
|
|
|
|
currentVersion [4]byte,
|
2022-12-24 18:39:39 +00:00
|
|
|
genesisValidatorsRoot [32]byte,
|
2022-10-14 13:24:44 +00:00
|
|
|
) ([]byte, error) {
|
2023-01-13 18:12:18 +00:00
|
|
|
var currentVersion32 libcommon.Hash
|
2023-01-07 11:25:28 +00:00
|
|
|
copy(currentVersion32[:], currentVersion[:])
|
2023-11-22 12:24:35 +00:00
|
|
|
forkDataRoot := utils.Sha256(currentVersion32[:], genesisValidatorsRoot[:])
|
2022-10-14 13:24:44 +00:00
|
|
|
return append(domainType, forkDataRoot[:28]...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComputeSigningRoot(
|
2023-03-12 14:41:53 +00:00
|
|
|
obj ssz.HashableSSZ,
|
2022-10-14 13:24:44 +00:00
|
|
|
domain []byte,
|
|
|
|
) ([32]byte, error) {
|
2023-01-21 21:33:50 +00:00
|
|
|
objRoot, err := obj.HashSSZ()
|
2022-10-14 13:24:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
2023-11-22 12:24:35 +00:00
|
|
|
return utils.Sha256(objRoot[:], domain), nil
|
2022-10-14 13:24:44 +00:00
|
|
|
}
|
2023-01-30 13:39:56 +00:00
|
|
|
|
|
|
|
func Domain(fork *cltypes.Fork, epoch uint64, domainType [4]byte, genesisRoot libcommon.Hash) ([]byte, error) {
|
|
|
|
if fork == nil {
|
|
|
|
return []byte{}, errors.New("nil fork or domain type")
|
|
|
|
}
|
|
|
|
var forkVersion []byte
|
|
|
|
if epoch < fork.Epoch {
|
|
|
|
forkVersion = fork.PreviousVersion[:]
|
|
|
|
} else {
|
|
|
|
forkVersion = fork.CurrentVersion[:]
|
|
|
|
}
|
|
|
|
if len(forkVersion) != 4 {
|
|
|
|
return []byte{}, errors.New("fork version length is not 4 byte")
|
|
|
|
}
|
|
|
|
var forkVersionArray [4]byte
|
|
|
|
copy(forkVersionArray[:], forkVersion[:4])
|
|
|
|
return ComputeDomain(domainType[:], forkVersionArray, genesisRoot)
|
|
|
|
}
|