forked from TrueCloudLab/neoneo-go
config: consolidate ProtocolConfiguration consistency checks
This commit is contained in:
parent
42769d11ff
commit
6e9d725a29
4 changed files with 42 additions and 9 deletions
|
@ -6,7 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
"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"
|
"github.com/nspcc-dev/neo-go/pkg/rpc"
|
||||||
"gopkg.in/yaml.v2"
|
"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)
|
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for name := range config.ProtocolConfiguration.NativeUpdateHistories {
|
err = config.ProtocolConfiguration.Validate()
|
||||||
if !nativenames.IsValid(name) {
|
if err != nil {
|
||||||
return Config{}, fmt.Errorf("NativeActivations configuration section contains unexpected native contract name: %s", name)
|
return Config{}, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
"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"
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -59,3 +63,18 @@ type (
|
||||||
VerifyTransactions bool `yaml:"VerifyTransactions"`
|
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
|
||||||
|
}
|
||||||
|
|
20
pkg/config/protocol_config_test.go
Normal file
20
pkg/config/protocol_config_test.go
Normal 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())
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/config"
|
"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) {
|
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))
|
validators := make([]*keys.PublicKey, len(cfg.StandbyCommittee))
|
||||||
for i := range validators {
|
for i := range validators {
|
||||||
pubKey, err := keys.NewPublicKeyFromString(cfg.StandbyCommittee[i])
|
pubKey, err := keys.NewPublicKeyFromString(cfg.StandbyCommittee[i])
|
||||||
|
|
Loading…
Reference in a new issue