2021-06-25 18:13:40 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-04 13:23:00 +00:00
|
|
|
// Package aura implements the proof-of-authority consensus engine.
|
2021-06-25 18:13:40 +00:00
|
|
|
package aura
|
|
|
|
|
|
|
|
import (
|
2022-10-13 12:46:03 +00:00
|
|
|
"errors"
|
2021-06-25 18:13:40 +00:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/holiman/uint256"
|
2023-04-13 11:19:02 +00:00
|
|
|
|
2023-04-14 07:51:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
|
2021-06-25 18:13:40 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/u256"
|
2021-07-21 11:13:26 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
2021-06-25 18:13:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Draws an validator nonce modulo number of validators.
|
2023-01-13 18:12:18 +00:00
|
|
|
func GetFromValidatorSet(set ValidatorSet, parent libcommon.Hash, nonce uint, call consensus.Call) (libcommon.Address, error) {
|
2021-07-21 11:13:26 +00:00
|
|
|
//d, err := set.defaultCaller(parent)
|
|
|
|
//if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
// return libcommon.Address{}, err
|
2021-07-21 11:13:26 +00:00
|
|
|
//}
|
|
|
|
return set.getWithCaller(parent, nonce, call)
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:51:25 +00:00
|
|
|
func newValidatorSetFromJson(j *chain.ValidatorSetJson, posdaoTransition *uint64) ValidatorSet {
|
2021-06-25 18:13:40 +00:00
|
|
|
if j.List != nil {
|
|
|
|
return &SimpleList{validators: j.List}
|
|
|
|
}
|
|
|
|
if j.SafeContract != nil {
|
2021-07-21 11:13:26 +00:00
|
|
|
return NewValidatorSafeContract(*j.SafeContract, posdaoTransition, nil)
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
if j.Contract != nil {
|
2021-07-08 12:40:43 +00:00
|
|
|
return &ValidatorContract{
|
2022-10-06 16:06:18 +00:00
|
|
|
contractAddress: *j.Contract,
|
Use NewValidatorSafeContract in ValidatorContract (#5717)
This fixes the following panic for Gnosis Chain on the validator switch
at block 9186425:
```
panic: method 'getValidators' not found
goroutine 90 [running]:
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorSafeContract).getListSyscall(0x14000ed9358, 0xd40004bf620)
github.com/ledgerwatch/erigon/consensus/aura/validators.go:634 +0x258
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorSafeContract).epochSet(0x16?, 0x20?, 0x8c2c79, {0xd4002d77180, 0x25f, 0x25f}, 0x11400fac7ee8?)
github.com/ledgerwatch/erigon/consensus/aura/validators.go:453 +0xdc
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorContract).epochSet(0x140006ae980?, 0x38?, 0x6f9d00000000c28e?, {0xd4002d77180?, 0x108acc108?, 0x40?}, 0x14000618000?)
```
2022-10-12 14:12:17 +00:00
|
|
|
validators: NewValidatorSafeContract(*j.Contract, posdaoTransition, nil),
|
2021-07-08 12:40:43 +00:00
|
|
|
posdaoTransition: posdaoTransition,
|
|
|
|
}
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
if j.Multi != nil {
|
|
|
|
l := map[uint64]ValidatorSet{}
|
|
|
|
for block, set := range j.Multi {
|
|
|
|
l[block] = newValidatorSetFromJson(set, posdaoTransition)
|
|
|
|
}
|
|
|
|
return NewMulti(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Code struct {
|
|
|
|
Code []byte
|
2023-01-13 18:12:18 +00:00
|
|
|
CodeHash libcommon.Hash
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BlockRewardContract struct {
|
2021-07-02 08:00:41 +00:00
|
|
|
blockNum uint64
|
2023-01-13 18:12:18 +00:00
|
|
|
address libcommon.Address // On-chain address.
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:00:41 +00:00
|
|
|
type BlockRewardContractList []BlockRewardContract
|
2021-06-25 18:13:40 +00:00
|
|
|
|
2021-07-02 08:00:41 +00:00
|
|
|
func (r BlockRewardContractList) Less(i, j int) bool { return r[i].blockNum < r[j].blockNum }
|
2021-06-25 18:13:40 +00:00
|
|
|
func (r BlockRewardContractList) Len() int { return len(r) }
|
|
|
|
func (r BlockRewardContractList) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
2021-07-02 08:00:41 +00:00
|
|
|
|
|
|
|
type BlockReward struct {
|
|
|
|
blockNum uint64
|
|
|
|
amount *uint256.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockRewardList []BlockReward
|
|
|
|
|
|
|
|
func (r BlockRewardList) Less(i, j int) bool { return r[i].blockNum < r[j].blockNum }
|
|
|
|
func (r BlockRewardList) Len() int { return len(r) }
|
|
|
|
func (r BlockRewardList) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func NewBlockRewardContract(address libcommon.Address) *BlockRewardContract {
|
2021-07-02 08:00:41 +00:00
|
|
|
return &BlockRewardContract{address: address}
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorityRoundParams struct {
|
|
|
|
// A map defining intervals of blocks with the given times (in seconds) to wait before next
|
|
|
|
// block or authority switching. The keys in the map are steps of starting blocks of those
|
|
|
|
// periods. The entry at `0` should be defined.
|
|
|
|
//
|
|
|
|
// Wait times (durations) are additionally required to be less than 65535 since larger values
|
|
|
|
// lead to slow block issuance.
|
|
|
|
StepDurations map[uint64]uint64
|
|
|
|
// Starting step,
|
|
|
|
StartStep *uint64
|
|
|
|
// Valid validators.
|
|
|
|
Validators ValidatorSet
|
|
|
|
// Chain score validation transition block.
|
|
|
|
ValidateScoreTransition uint64
|
|
|
|
// Monotonic step validation transition block.
|
|
|
|
ValidateStepTransition uint64
|
|
|
|
// Immediate transitions.
|
|
|
|
ImmediateTransitions bool
|
|
|
|
// Block reward in base units.
|
2021-07-02 08:00:41 +00:00
|
|
|
BlockReward BlockRewardList
|
2021-06-25 18:13:40 +00:00
|
|
|
// Block reward contract addresses with their associated starting block numbers.
|
|
|
|
BlockRewardContractTransitions BlockRewardContractList
|
|
|
|
// Number of accepted uncles transition block.
|
|
|
|
MaximumUncleCountTransition uint64
|
|
|
|
// Number of accepted uncles.
|
|
|
|
MaximumUncleCount uint
|
|
|
|
// Transition block to strict empty steps validation.
|
|
|
|
StrictEmptyStepsTransition uint64
|
|
|
|
// If set, enables random number contract integration. It maps the transition block to the contract address.
|
2023-01-13 18:12:18 +00:00
|
|
|
RandomnessContractAddress map[uint64]libcommon.Address
|
2021-06-25 18:13:40 +00:00
|
|
|
// The addresses of contracts that determine the block gas limit with their associated block
|
|
|
|
// numbers.
|
2023-01-13 18:12:18 +00:00
|
|
|
BlockGasLimitContractTransitions map[uint64]libcommon.Address
|
2021-06-25 18:13:40 +00:00
|
|
|
// If set, this is the block number at which the consensus engine switches from AuRa to AuRa
|
|
|
|
// with POSDAO modifications.
|
|
|
|
PosdaoTransition *uint64
|
2022-10-26 11:03:47 +00:00
|
|
|
// Stores human-readable keys associated with addresses, like DNS information.
|
|
|
|
// This contract is primarily required to store the address of the Certifier contract.
|
2023-01-13 18:12:18 +00:00
|
|
|
Registrar *libcommon.Address
|
2022-11-08 12:03:06 +00:00
|
|
|
|
2023-02-24 11:43:29 +00:00
|
|
|
// See https://github.com/gnosischain/specs/blob/master/execution/withdrawals.md
|
|
|
|
WithdrawalContractAddress *libcommon.Address
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
RewriteBytecode map[uint64]map[libcommon.Address][]byte
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:51:25 +00:00
|
|
|
func FromJson(jsonParams *chain.AuRaConfig) (AuthorityRoundParams, error) {
|
2021-06-25 18:13:40 +00:00
|
|
|
params := AuthorityRoundParams{
|
|
|
|
Validators: newValidatorSetFromJson(jsonParams.Validators, jsonParams.PosdaoTransition),
|
|
|
|
StartStep: jsonParams.StartStep,
|
|
|
|
RandomnessContractAddress: jsonParams.RandomnessContractAddress,
|
|
|
|
BlockGasLimitContractTransitions: jsonParams.BlockGasLimitContractTransitions,
|
|
|
|
PosdaoTransition: jsonParams.PosdaoTransition,
|
2022-10-26 11:03:47 +00:00
|
|
|
Registrar: jsonParams.Registrar,
|
2023-02-24 11:43:29 +00:00
|
|
|
WithdrawalContractAddress: jsonParams.WithdrawalContractAddress,
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
params.StepDurations = map[uint64]uint64{}
|
|
|
|
if jsonParams.StepDuration != nil {
|
|
|
|
params.StepDurations[0] = *jsonParams.StepDuration
|
|
|
|
}
|
|
|
|
|
2022-10-13 12:46:03 +00:00
|
|
|
for blockNum, address := range jsonParams.BlockRewardContractTransitions {
|
|
|
|
params.BlockRewardContractTransitions = append(params.BlockRewardContractTransitions, BlockRewardContract{blockNum: uint64(blockNum), address: address})
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
2021-07-02 08:00:41 +00:00
|
|
|
sort.Sort(params.BlockRewardContractTransitions)
|
2022-10-13 12:46:03 +00:00
|
|
|
if jsonParams.BlockRewardContractAddress != nil {
|
|
|
|
transitionBlockNum := uint64(0)
|
|
|
|
if jsonParams.BlockRewardContractTransition != nil {
|
|
|
|
transitionBlockNum = *jsonParams.BlockRewardContractTransition
|
|
|
|
}
|
|
|
|
if len(params.BlockRewardContractTransitions) > 0 && transitionBlockNum >= params.BlockRewardContractTransitions[0].blockNum {
|
|
|
|
return params, errors.New("blockRewardContractTransition should be less than any of the keys in BlockRewardContractTransitions")
|
|
|
|
}
|
|
|
|
contract := BlockRewardContract{blockNum: transitionBlockNum, address: *jsonParams.BlockRewardContractAddress}
|
|
|
|
params.BlockRewardContractTransitions = append(BlockRewardContractList{contract}, params.BlockRewardContractTransitions...)
|
|
|
|
}
|
2021-06-25 18:13:40 +00:00
|
|
|
|
|
|
|
if jsonParams.ValidateScoreTransition != nil {
|
|
|
|
params.ValidateScoreTransition = *jsonParams.ValidateScoreTransition
|
|
|
|
}
|
|
|
|
if jsonParams.ValidateStepTransition != nil {
|
|
|
|
params.ValidateStepTransition = *jsonParams.ValidateStepTransition
|
|
|
|
}
|
|
|
|
if jsonParams.ImmediateTransitions != nil {
|
|
|
|
params.ImmediateTransitions = *jsonParams.ImmediateTransitions
|
|
|
|
}
|
|
|
|
if jsonParams.MaximumUncleCount != nil {
|
|
|
|
params.MaximumUncleCount = *jsonParams.MaximumUncleCount
|
|
|
|
}
|
|
|
|
if jsonParams.MaximumUncleCountTransition != nil {
|
|
|
|
params.MaximumUncleCountTransition = *jsonParams.MaximumUncleCountTransition
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonParams.BlockReward == nil {
|
2021-07-02 08:00:41 +00:00
|
|
|
params.BlockReward = append(params.BlockReward, BlockReward{blockNum: 0, amount: u256.Num0})
|
2021-06-25 18:13:40 +00:00
|
|
|
} else {
|
|
|
|
if jsonParams.BlockReward != nil {
|
2023-04-14 07:51:25 +00:00
|
|
|
params.BlockReward = append(params.BlockReward, BlockReward{blockNum: 0, amount: uint256.NewInt(*jsonParams.BlockReward)})
|
2021-06-25 18:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 08:00:41 +00:00
|
|
|
sort.Sort(params.BlockReward)
|
2021-06-25 18:13:40 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
params.RewriteBytecode = make(map[uint64]map[libcommon.Address][]byte, len(jsonParams.RewriteBytecode))
|
2022-11-08 12:03:06 +00:00
|
|
|
for block, overrides := range jsonParams.RewriteBytecode {
|
2023-01-13 18:12:18 +00:00
|
|
|
params.RewriteBytecode[block] = make(map[libcommon.Address][]byte, len(overrides))
|
2022-11-08 12:03:06 +00:00
|
|
|
for address, code := range overrides {
|
|
|
|
params.RewriteBytecode[block][address] = []byte(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:13:40 +00:00
|
|
|
return params, nil
|
|
|
|
}
|