2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-03-11 11:39:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-04-28 14:46:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc"
|
2020-08-12 13:31:06 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-03-25 15:30:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const userAgentFormat = "/NEO-GO:%s/"
|
|
|
|
|
|
|
|
// Version the version of the node, set at build time.
|
|
|
|
var Version string
|
|
|
|
|
|
|
|
// Config top level struct representing the config
|
|
|
|
// for the node.
|
|
|
|
type Config struct {
|
|
|
|
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
|
|
|
|
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateUserAgent creates user agent string based on build time environment.
|
|
|
|
func (c Config) GenerateUserAgent() string {
|
|
|
|
return fmt.Sprintf(userAgentFormat, Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load attempts to load the config from the given
|
|
|
|
// path for the given netMode.
|
2020-06-14 07:34:50 +00:00
|
|
|
func Load(path string, netMode netmode.Magic) (Config, error) {
|
2020-03-25 15:30:21 +00:00
|
|
|
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
|
2020-07-03 08:40:40 +00:00
|
|
|
return LoadFile(configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFile loads config from the provided path.
|
|
|
|
func LoadFile(configPath string) (Config, error) {
|
2020-03-25 15:30:21 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("config '%s' doesn't exist", configPath)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("unable to read config: %w", err)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
ApplicationConfiguration: ApplicationConfiguration{
|
|
|
|
PingInterval: 30,
|
|
|
|
PingTimeout: 90,
|
2021-04-28 14:46:34 +00:00
|
|
|
RPC: rpc.Config{
|
|
|
|
MaxIteratorResultItems: 100,
|
|
|
|
},
|
2020-03-25 15:30:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(configData, &config)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:39:51 +00:00
|
|
|
for name := range config.ProtocolConfiguration.NativeUpdateHistories {
|
|
|
|
if !nativenames.IsValid(name) {
|
|
|
|
return Config{}, fmt.Errorf("NativeActivations configuration section contains unexpected native contract name: %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
return config, nil
|
|
|
|
}
|