2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-01-19 23:59:40 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-24 19:03:10 +00:00
|
|
|
"maps"
|
|
|
|
"slices"
|
2022-01-21 02:33:06 +00:00
|
|
|
"sort"
|
2022-12-02 16:10:45 +00:00
|
|
|
"time"
|
2022-01-19 23:59:40 +00:00
|
|
|
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-07-20 12:54:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-03-25 15:30:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProtocolConfiguration represents the protocol config.
|
|
|
|
type (
|
|
|
|
ProtocolConfiguration struct {
|
2022-01-21 02:33:06 +00:00
|
|
|
// CommitteeHistory stores committee size change history (height: size).
|
2023-04-13 09:43:21 +00:00
|
|
|
CommitteeHistory map[uint32]uint32 `yaml:"CommitteeHistory"`
|
2023-10-18 21:52:29 +00:00
|
|
|
// Genesis stores genesis-related settings including a set of NeoGo
|
|
|
|
// extensions that should be included into genesis block or be enabled
|
|
|
|
// at the moment of native contracts initialization.
|
|
|
|
Genesis Genesis `yaml:"Genesis"`
|
2022-01-29 08:28:29 +00:00
|
|
|
|
|
|
|
Magic netmode.Magic `yaml:"Magic"`
|
|
|
|
MemPoolSize int `yaml:"MemPoolSize"`
|
2021-07-20 12:54:56 +00:00
|
|
|
|
2022-05-06 12:19:17 +00:00
|
|
|
// Hardforks is a map of hardfork names that enables version-specific application
|
|
|
|
// logic dependent on the specified height.
|
|
|
|
Hardforks map[string]uint32 `yaml:"Hardforks"`
|
2021-07-20 12:54:56 +00:00
|
|
|
// InitialGASSupply is the amount of GAS generated in the genesis block.
|
|
|
|
InitialGASSupply fixedn.Fixed8 `yaml:"InitialGASSupply"`
|
2020-11-27 10:55:48 +00:00
|
|
|
// P2PNotaryRequestPayloadPoolSize specifies the memory pool size for P2PNotaryRequestPayloads.
|
|
|
|
// It is valid only if P2PSigExtensions are enabled.
|
|
|
|
P2PNotaryRequestPayloadPoolSize int `yaml:"P2PNotaryRequestPayloadPoolSize"`
|
2021-03-15 10:00:04 +00:00
|
|
|
// MaxBlockSize is the maximum block size in bytes.
|
|
|
|
MaxBlockSize uint32 `yaml:"MaxBlockSize"`
|
2021-03-15 10:51:07 +00:00
|
|
|
// MaxBlockSystemFee is the maximum overall system fee per block.
|
|
|
|
MaxBlockSystemFee int64 `yaml:"MaxBlockSystemFee"`
|
2020-11-05 19:01:12 +00:00
|
|
|
// MaxTraceableBlocks is the length of the chain accessible to smart contracts.
|
|
|
|
MaxTraceableBlocks uint32 `yaml:"MaxTraceableBlocks"`
|
2021-02-17 15:22:57 +00:00
|
|
|
// MaxTransactionsPerBlock is the maximum amount of transactions per block.
|
|
|
|
MaxTransactionsPerBlock uint16 `yaml:"MaxTransactionsPerBlock"`
|
2021-05-17 08:07:08 +00:00
|
|
|
// MaxValidUntilBlockIncrement is the upper increment size of blockchain height in blocks
|
|
|
|
// exceeding that a transaction should fail validation. It is set to estimated daily number
|
|
|
|
// of blocks with 15s interval.
|
|
|
|
MaxValidUntilBlockIncrement uint32 `yaml:"MaxValidUntilBlockIncrement"`
|
2020-11-27 10:55:48 +00:00
|
|
|
// P2PSigExtensions enables additional signature-related logic.
|
2020-10-14 16:07:16 +00:00
|
|
|
P2PSigExtensions bool `yaml:"P2PSigExtensions"`
|
2021-06-15 08:09:35 +00:00
|
|
|
// P2PStateExchangeExtensions enables additional P2P MPT state data exchange logic.
|
|
|
|
P2PStateExchangeExtensions bool `yaml:"P2PStateExchangeExtensions"`
|
2020-10-15 10:06:22 +00:00
|
|
|
// ReservedAttributes allows to have reserved attributes range for experimental or private purposes.
|
|
|
|
ReservedAttributes bool `yaml:"ReservedAttributes"`
|
2023-10-09 20:32:46 +00:00
|
|
|
|
2020-08-03 14:07:24 +00:00
|
|
|
SeedList []string `yaml:"SeedList"`
|
|
|
|
StandbyCommittee []string `yaml:"StandbyCommittee"`
|
2020-11-17 12:57:50 +00:00
|
|
|
// StateRooInHeader enables storing state root in block header.
|
|
|
|
StateRootInHeader bool `yaml:"StateRootInHeader"`
|
2021-06-15 08:09:35 +00:00
|
|
|
// StateSyncInterval is the number of blocks between state heights available for MPT state data synchronization.
|
|
|
|
// It is valid only if P2PStateExchangeExtensions are enabled.
|
|
|
|
StateSyncInterval int `yaml:"StateSyncInterval"`
|
2022-12-02 16:10:45 +00:00
|
|
|
// TimePerBlock is the time interval between blocks that consensus nodes work with.
|
|
|
|
// It must be an integer number of milliseconds.
|
|
|
|
TimePerBlock time.Duration `yaml:"TimePerBlock"`
|
2023-04-13 10:10:01 +00:00
|
|
|
ValidatorsCount uint32 `yaml:"ValidatorsCount"`
|
2022-01-21 02:33:06 +00:00
|
|
|
// Validators stores history of changes to consensus node number (height: number).
|
2023-04-13 09:43:21 +00:00
|
|
|
ValidatorsHistory map[uint32]uint32 `yaml:"ValidatorsHistory"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// Whether to verify transactions in the received blocks.
|
2020-03-25 15:30:21 +00:00
|
|
|
VerifyTransactions bool `yaml:"VerifyTransactions"`
|
|
|
|
}
|
|
|
|
)
|
2022-01-19 23:59:40 +00:00
|
|
|
|
2022-01-21 02:33:06 +00:00
|
|
|
// heightNumber is an auxiliary structure for configuration checks.
|
|
|
|
type heightNumber struct {
|
|
|
|
h uint32
|
2023-04-13 09:43:21 +00:00
|
|
|
n uint32
|
2022-01-21 02:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:59:40 +00:00
|
|
|
// Validate checks ProtocolConfiguration for internal consistency and returns
|
2022-04-20 18:30:09 +00:00
|
|
|
// an error if anything inappropriate found. Other methods can rely on protocol
|
2022-01-19 23:59:40 +00:00
|
|
|
// validity after this.
|
|
|
|
func (p *ProtocolConfiguration) Validate() error {
|
2022-01-21 02:33:06 +00:00
|
|
|
var err error
|
|
|
|
|
2022-12-02 16:10:45 +00:00
|
|
|
if p.TimePerBlock%time.Millisecond != 0 {
|
|
|
|
return errors.New("TimePerBlock must be an integer number of milliseconds")
|
|
|
|
}
|
2022-05-06 12:19:17 +00:00
|
|
|
for name := range p.Hardforks {
|
|
|
|
if !IsHardforkValid(name) {
|
|
|
|
return fmt.Errorf("Hardforks configuration section contains unexpected hardfork: %s", name)
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 16:24:34 +00:00
|
|
|
var (
|
|
|
|
prev uint32
|
|
|
|
shouldBeDisabled bool
|
|
|
|
)
|
|
|
|
for _, cfgHf := range Hardforks {
|
|
|
|
h := p.Hardforks[cfgHf.String()]
|
|
|
|
if h != 0 && shouldBeDisabled {
|
|
|
|
return fmt.Errorf("missing previous hardfork configuration with %s present", cfgHf.String())
|
|
|
|
}
|
|
|
|
if h != 0 && h < prev {
|
|
|
|
return fmt.Errorf("hardfork %s has inconsistent enabling height %d (lower than the previouse one)", cfgHf.String(), h)
|
|
|
|
}
|
|
|
|
if h != 0 {
|
|
|
|
prev = h
|
|
|
|
} else if prev != 0 {
|
|
|
|
shouldBeDisabled = true
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 22:21:02 +00:00
|
|
|
if p.ValidatorsCount != 0 && len(p.ValidatorsHistory) != 0 || p.ValidatorsCount == 0 && len(p.ValidatorsHistory) == 0 {
|
|
|
|
return errors.New("configuration should either have one of ValidatorsCount or ValidatorsHistory, not both")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.StandbyCommittee) == 0 {
|
|
|
|
return errors.New("configuration should include StandbyCommittee")
|
2022-01-21 02:33:06 +00:00
|
|
|
}
|
2023-04-13 10:10:01 +00:00
|
|
|
if len(p.StandbyCommittee) < int(p.ValidatorsCount) {
|
2022-01-21 02:33:06 +00:00
|
|
|
return errors.New("validators count can't exceed the size of StandbyCommittee")
|
|
|
|
}
|
|
|
|
var arr = make([]heightNumber, 0, len(p.CommitteeHistory))
|
|
|
|
for h, n := range p.CommitteeHistory {
|
2023-04-13 10:02:36 +00:00
|
|
|
if n == 0 {
|
|
|
|
return fmt.Errorf("invalid CommitteeHistory: bad members count (%d) for height %d", n, h)
|
|
|
|
}
|
2022-01-21 02:33:06 +00:00
|
|
|
if int(n) > len(p.StandbyCommittee) {
|
|
|
|
return fmt.Errorf("too small StandbyCommittee for required number of committee members at %d", h)
|
|
|
|
}
|
|
|
|
arr = append(arr, heightNumber{h, n})
|
|
|
|
}
|
|
|
|
if len(arr) != 0 {
|
|
|
|
err = sortCheckZero(arr, "CommitteeHistory")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, hn := range arr[1:] {
|
|
|
|
if int64(hn.h)%int64(hn.n) != 0 || int64(hn.h)%int64(arr[i].n) != 0 {
|
|
|
|
return fmt.Errorf("invalid CommitteeHistory: bad %d height for %d and %d committee", hn.h, hn.n, arr[i].n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arr = arr[:0]
|
|
|
|
for h, n := range p.ValidatorsHistory {
|
2023-04-13 10:02:36 +00:00
|
|
|
if n == 0 {
|
|
|
|
return fmt.Errorf("invalid ValidatorsHistory: bad members count (%d) for height %d", n, h)
|
|
|
|
}
|
2022-01-21 02:33:06 +00:00
|
|
|
if int(n) > len(p.StandbyCommittee) {
|
|
|
|
return fmt.Errorf("too small StandbyCommittee for required number of validators at %d", h)
|
|
|
|
}
|
|
|
|
arr = append(arr, heightNumber{h, n})
|
|
|
|
}
|
|
|
|
if len(arr) != 0 {
|
|
|
|
err = sortCheckZero(arr, "ValidatorsHistory")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, hn := range arr {
|
|
|
|
if int64(hn.n) > int64(p.GetCommitteeSize(hn.h)) {
|
|
|
|
return fmt.Errorf("requested number of validators is too big: %d at %d", hn.n, hn.h)
|
|
|
|
}
|
|
|
|
if int64(hn.h)%int64(p.GetCommitteeSize(hn.h)) != 0 {
|
|
|
|
return fmt.Errorf("validators number change is not aligned with committee change at %d", hn.h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 23:59:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-21 02:33:06 +00:00
|
|
|
|
|
|
|
// sortCheckZero sorts heightNumber array and checks for zero height presence.
|
|
|
|
func sortCheckZero(arr []heightNumber, field string) error {
|
|
|
|
sort.Slice(arr, func(i, j int) bool {
|
|
|
|
return arr[i].h < arr[j].h
|
|
|
|
})
|
|
|
|
if arr[0].h != 0 {
|
|
|
|
return fmt.Errorf("invalid %s: no height 0 specified", field)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommitteeSize returns the committee size for the given height. It implies
|
|
|
|
// valid configuration file.
|
|
|
|
func (p *ProtocolConfiguration) GetCommitteeSize(height uint32) int {
|
|
|
|
if len(p.CommitteeHistory) == 0 {
|
|
|
|
return len(p.StandbyCommittee)
|
|
|
|
}
|
2023-04-13 09:43:21 +00:00
|
|
|
return int(getBestFromMap(p.CommitteeHistory, height))
|
2022-01-21 02:33:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 09:43:21 +00:00
|
|
|
func getBestFromMap(dict map[uint32]uint32, height uint32) uint32 {
|
|
|
|
var res uint32
|
2022-01-21 02:33:06 +00:00
|
|
|
var bestH = uint32(0)
|
|
|
|
for h, n := range dict {
|
|
|
|
if h >= bestH && h <= height {
|
|
|
|
res = n
|
|
|
|
bestH = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNumOfCNs returns the number of validators for the given height.
|
|
|
|
// It implies valid configuration file.
|
|
|
|
func (p *ProtocolConfiguration) GetNumOfCNs(height uint32) int {
|
|
|
|
if len(p.ValidatorsHistory) == 0 {
|
2023-04-13 10:10:01 +00:00
|
|
|
return int(p.ValidatorsCount)
|
2022-01-21 02:33:06 +00:00
|
|
|
}
|
2023-04-13 09:43:21 +00:00
|
|
|
return int(getBestFromMap(p.ValidatorsHistory, height))
|
2022-01-21 02:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldUpdateCommitteeAt answers the question of whether the committee
|
|
|
|
// should be updated at the given height.
|
|
|
|
func (p *ProtocolConfiguration) ShouldUpdateCommitteeAt(height uint32) bool {
|
|
|
|
return height%uint32(p.GetCommitteeSize(height)) == 0
|
|
|
|
}
|
2022-07-26 12:18:30 +00:00
|
|
|
|
|
|
|
// Equals allows to compare two ProtocolConfiguration instances, returns true if
|
|
|
|
// they're equal.
|
|
|
|
func (p *ProtocolConfiguration) Equals(o *ProtocolConfiguration) bool {
|
2023-10-09 20:32:46 +00:00
|
|
|
if p.InitialGASSupply != o.InitialGASSupply ||
|
2022-07-26 12:18:30 +00:00
|
|
|
p.Magic != o.Magic ||
|
|
|
|
p.MaxBlockSize != o.MaxBlockSize ||
|
|
|
|
p.MaxBlockSystemFee != o.MaxBlockSystemFee ||
|
|
|
|
p.MaxTraceableBlocks != o.MaxTraceableBlocks ||
|
|
|
|
p.MaxTransactionsPerBlock != o.MaxTransactionsPerBlock ||
|
|
|
|
p.MaxValidUntilBlockIncrement != o.MaxValidUntilBlockIncrement ||
|
|
|
|
p.MemPoolSize != o.MemPoolSize ||
|
|
|
|
p.P2PNotaryRequestPayloadPoolSize != o.P2PNotaryRequestPayloadPoolSize ||
|
|
|
|
p.P2PSigExtensions != o.P2PSigExtensions ||
|
|
|
|
p.P2PStateExchangeExtensions != o.P2PStateExchangeExtensions ||
|
|
|
|
p.ReservedAttributes != o.ReservedAttributes ||
|
|
|
|
p.StateRootInHeader != o.StateRootInHeader ||
|
|
|
|
p.StateSyncInterval != o.StateSyncInterval ||
|
2022-12-02 16:10:45 +00:00
|
|
|
p.TimePerBlock != o.TimePerBlock ||
|
2022-07-26 12:18:30 +00:00
|
|
|
p.ValidatorsCount != o.ValidatorsCount ||
|
|
|
|
p.VerifyTransactions != o.VerifyTransactions ||
|
2024-08-24 19:03:10 +00:00
|
|
|
!maps.Equal(p.CommitteeHistory, o.CommitteeHistory) ||
|
|
|
|
!maps.Equal(p.Hardforks, o.Hardforks) ||
|
|
|
|
!slices.Equal(p.SeedList, o.SeedList) ||
|
|
|
|
!slices.Equal(p.StandbyCommittee, o.StandbyCommittee) ||
|
|
|
|
!maps.Equal(p.ValidatorsHistory, o.ValidatorsHistory) {
|
2022-07-26 12:18:30 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|