2022-10-08 16:15:44 +02: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-30 00:20:09 +02:00
|
|
|
package fork
|
|
|
|
|
|
|
|
import (
|
2023-01-07 12:25:28 +01:00
|
|
|
"encoding/binary"
|
2022-09-30 00:20:09 +02:00
|
|
|
"errors"
|
2023-01-24 17:36:02 +01:00
|
|
|
"fmt"
|
2022-09-30 00:20:09 +02:00
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-05-11 04:54:20 -07:00
|
|
|
"github.com/ledgerwatch/erigon-lib/types/ssz"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2022-10-29 21:51:32 +02:00
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
2023-01-30 14:39:56 +01:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2022-10-29 21:51:32 +02:00
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
2022-09-30 00:20:09 +02:00
|
|
|
)
|
|
|
|
|
2023-06-02 00:21:08 +01: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 17:36:02 +01:00
|
|
|
func ForkDigestVersion(digest [4]byte, b *clparams.BeaconChainConfig, genesisValidatorRoot libcommon.Hash) (clparams.StateVersion, error) {
|
|
|
|
var (
|
2023-05-15 20:07:27 +02:00
|
|
|
phase0ForkDigest, altairForkDigest, bellatrixForkDigest, capellaForkDigest, denebForkDigest [4]byte
|
|
|
|
err error
|
2023-01-24 17:36:02 +01: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 20:07:27 +02:00
|
|
|
|
|
|
|
denebForkDigest, err = ComputeForkDigestForVersion(
|
|
|
|
utils.Uint32ToBytes4(b.DenebForkVersion),
|
|
|
|
genesisValidatorRoot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-24 17:36:02 +01: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 20:07:27 +02:00
|
|
|
case denebForkDigest:
|
|
|
|
return clparams.DenebVersion, nil
|
2023-01-24 17:36:02 +01:00
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("invalid state version")
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:20:09 +02:00
|
|
|
func ComputeForkDigest(
|
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
|
|
|
) ([4]byte, error) {
|
|
|
|
if genesisConfig.GenesisTime == 0 {
|
2023-06-02 00:21:08 +01:00
|
|
|
return [4]byte{}, NO_GENESIS_TIME_ERR
|
2022-09-30 00:20:09 +02:00
|
|
|
}
|
2023-01-13 18:12:18 +00:00
|
|
|
if genesisConfig.GenesisValidatorRoot == (libcommon.Hash{}) {
|
2023-06-02 00:21:08 +01:00
|
|
|
return [4]byte{}, NO_VALIDATOR_ROOT_HASH
|
2022-09-30 00:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
// Retrieve current fork version.
|
2023-01-24 00:03:48 +01:00
|
|
|
currentForkVersion := utils.Uint32ToBytes4(beaconConfig.GenesisForkVersion)
|
2022-09-30 00:20:09 +02:00
|
|
|
for _, fork := range forkList(beaconConfig.ForkVersionSchedule) {
|
|
|
|
if currentEpoch >= fork.epoch {
|
|
|
|
currentForkVersion = fork.version
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-12-23 18:39:44 +01:00
|
|
|
return ComputeForkDigestForVersion(currentForkVersion, genesisConfig.GenesisValidatorRoot)
|
2022-09-30 00:20:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-17 22:51:19 +01:00
|
|
|
func ComputeNextForkDigest(
|
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
|
|
|
) ([4]byte, error) {
|
|
|
|
if genesisConfig.GenesisTime == 0 {
|
2023-06-02 00:21:08 +01:00
|
|
|
return [4]byte{}, NO_GENESIS_TIME_ERR
|
2023-03-17 22:51:19 +01:00
|
|
|
}
|
|
|
|
if genesisConfig.GenesisValidatorRoot == (libcommon.Hash{}) {
|
2023-06-02 00:21:08 +01:00
|
|
|
return [4]byte{}, NO_VALIDATOR_ROOT_HASH
|
2023-03-17 22:51:19 +01: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 21:13:53 +01:00
|
|
|
nextForkIndex--
|
|
|
|
if nextForkIndex == len(forkList)-1 {
|
|
|
|
return [4]byte{}, nil
|
2023-03-17 22:51:19 +01:00
|
|
|
}
|
2023-03-19 21:13:53 +01:00
|
|
|
nextForkIndex++
|
2023-03-17 22:51:19 +01:00
|
|
|
return ComputeForkDigestForVersion(forkList[nextForkIndex].version, genesisConfig.GenesisValidatorRoot)
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:20:09 +02:00
|
|
|
type fork struct {
|
|
|
|
epoch uint64
|
|
|
|
version [4]byte
|
|
|
|
}
|
|
|
|
|
2023-09-05 22:37:18 +02:00
|
|
|
func forkList(schedule map[libcommon.Bytes4]uint64) (f []fork) {
|
2022-09-30 00:20:09 +02: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 18:39:44 +01: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 12:25:28 +01:00
|
|
|
copy(currentVersion32[:], currentVersion[:])
|
2023-11-22 13:24:35 +01:00
|
|
|
dataRoot := utils.Sha256(currentVersion32[:], genesisValidatorsRoot[:])
|
2022-09-30 00:20:09 +02: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 12:25:28 +01:00
|
|
|
enrForkId := make([]byte, 16)
|
|
|
|
copy(enrForkId, digest[:])
|
|
|
|
copy(enrForkId[4:], nextForkVersion[:])
|
|
|
|
binary.BigEndian.PutUint64(enrForkId[8:], nextForkEpoch)
|
|
|
|
|
|
|
|
return enrForkId, nil
|
2022-09-30 00:20:09 +02:00
|
|
|
}
|
2022-09-30 23:53:54 +02:00
|
|
|
|
2022-10-14 15:24:44 +02:00
|
|
|
func GetLastFork(
|
2022-09-30 23:53:54 +02:00
|
|
|
beaconConfig *clparams.BeaconChainConfig,
|
|
|
|
genesisConfig *clparams.GenesisConfig,
|
2022-10-14 15:24:44 +02:00
|
|
|
) [4]byte {
|
2022-09-30 23:53:54 +02:00
|
|
|
currentEpoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
// Retrieve current fork version.
|
2023-01-24 00:03:48 +01:00
|
|
|
currentFork := utils.Uint32ToBytes4(beaconConfig.GenesisForkVersion)
|
2022-09-30 23:53:54 +02:00
|
|
|
for _, fork := range forkList(beaconConfig.ForkVersionSchedule) {
|
|
|
|
if currentEpoch >= fork.epoch {
|
2022-10-14 15:24:44 +02:00
|
|
|
currentFork = fork.version
|
2022-09-30 23:53:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-10-14 15:24:44 +02:00
|
|
|
return currentFork
|
2022-09-30 23:53:54 +02:00
|
|
|
}
|
2022-10-08 16:15:44 +02:00
|
|
|
|
2022-10-14 15:24:44 +02:00
|
|
|
func ComputeDomain(
|
|
|
|
domainType []byte,
|
|
|
|
currentVersion [4]byte,
|
2022-12-24 13:39:39 -05:00
|
|
|
genesisValidatorsRoot [32]byte,
|
2022-10-14 15:24:44 +02:00
|
|
|
) ([]byte, error) {
|
2023-01-13 18:12:18 +00:00
|
|
|
var currentVersion32 libcommon.Hash
|
2023-01-07 12:25:28 +01:00
|
|
|
copy(currentVersion32[:], currentVersion[:])
|
2023-11-22 13:24:35 +01:00
|
|
|
forkDataRoot := utils.Sha256(currentVersion32[:], genesisValidatorsRoot[:])
|
2022-10-14 15:24:44 +02:00
|
|
|
return append(domainType, forkDataRoot[:28]...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComputeSigningRoot(
|
2023-03-12 15:41:53 +01:00
|
|
|
obj ssz.HashableSSZ,
|
2022-10-14 15:24:44 +02:00
|
|
|
domain []byte,
|
|
|
|
) ([32]byte, error) {
|
2023-01-21 22:33:50 +01:00
|
|
|
objRoot, err := obj.HashSSZ()
|
2022-10-14 15:24:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
2023-11-22 13:24:35 +01:00
|
|
|
return utils.Sha256(objRoot[:], domain), nil
|
2022-10-14 15:24:44 +02:00
|
|
|
}
|
2023-01-30 14:39:56 +01: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)
|
|
|
|
}
|