config: consolidate ProtocolConfiguration consistency checks

This commit is contained in:
Roman Khimov 2022-01-20 02:59:40 +03:00
parent 42769d11ff
commit 6e9d725a29
4 changed files with 42 additions and 9 deletions

View file

@ -6,7 +6,6 @@ import (
"os"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/rpc"
"gopkg.in/yaml.v2"
)
@ -63,10 +62,9 @@ func LoadFile(configPath string) (Config, error) {
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
}
for name := range config.ProtocolConfiguration.NativeUpdateHistories {
if !nativenames.IsValid(name) {
return Config{}, fmt.Errorf("NativeActivations configuration section contains unexpected native contract name: %s", name)
}
err = config.ProtocolConfiguration.Validate()
if err != nil {
return Config{}, err
}
return config, nil

View file

@ -1,7 +1,11 @@
package config
import (
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
)
@ -59,3 +63,18 @@ type (
VerifyTransactions bool `yaml:"VerifyTransactions"`
}
)
// Validate checks ProtocolConfiguration for internal consistency and returns
// error if anything inappropriate found. Other methods can rely on protocol
// validity after this.
func (p *ProtocolConfiguration) Validate() error {
if len(p.StandbyCommittee) < p.ValidatorsCount {
return errors.New("validators count can't exceed the size of StandbyCommittee")
}
for name := range p.NativeUpdateHistories {
if !nativenames.IsValid(name) {
return fmt.Errorf("NativeActivations configuration section contains unexpected native contract name: %s", name)
}
}
return nil
}

View file

@ -0,0 +1,20 @@
package config
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestProtocolConfigurationValidation(t *testing.T) {
p := &ProtocolConfiguration{
ValidatorsCount: 1,
}
require.Error(t, p.Validate())
p = &ProtocolConfiguration{
NativeUpdateHistories: map[string][]uint32{
"someContract": []uint32{0, 10},
},
}
require.Error(t, p.Validate())
}

View file

@ -1,7 +1,6 @@
package core
import (
"errors"
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -58,9 +57,6 @@ func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey,
}
func committeeFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
if len(cfg.StandbyCommittee) < cfg.ValidatorsCount {
return nil, errors.New("validators count can be less than the size of StandbyCommittee")
}
validators := make([]*keys.PublicKey, len(cfg.StandbyCommittee))
for i := range validators {
pubKey, err := keys.NewPublicKeyFromString(cfg.StandbyCommittee[i])