2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
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
|
|
|
)
|
|
|
|
|
2022-04-26 09:32:06 +00:00
|
|
|
const (
|
|
|
|
// UserAgentWrapper is a string that user agent string should be wrapped into.
|
|
|
|
UserAgentWrapper = "/"
|
|
|
|
// UserAgentPrefix is a prefix used to generate user agent string.
|
|
|
|
UserAgentPrefix = "NEO-GO:"
|
|
|
|
// UserAgentFormat is a formatted string used to generate user agent string.
|
|
|
|
UserAgentFormat = UserAgentWrapper + UserAgentPrefix + "%s" + UserAgentWrapper
|
|
|
|
)
|
2020-03-25 15:30:21 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Version is the version of the node, set at the build time.
|
2020-03-25 15:30:21 +00:00
|
|
|
var Version string
|
|
|
|
|
|
|
|
// Config top level struct representing the config
|
|
|
|
// for the node.
|
|
|
|
type Config struct {
|
|
|
|
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
|
|
|
|
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GenerateUserAgent creates a user agent string based on the build time environment.
|
2020-03-25 15:30:21 +00:00
|
|
|
func (c Config) GenerateUserAgent() string {
|
2022-04-26 09:32:06 +00:00
|
|
|
return fmt.Sprintf(UserAgentFormat, Version)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-02-22 16:27:32 +00:00
|
|
|
configData, err := os.ReadFile(configPath)
|
2020-03-25 15:30:21 +00:00
|
|
|
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,
|
2021-10-07 13:56:27 +00:00
|
|
|
MaxFindResultItems: 100,
|
2021-11-17 20:04:50 +00:00
|
|
|
MaxNEP11Tokens: 100,
|
2021-04-28 14:46:34 +00:00
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2022-01-19 23:59:40 +00:00
|
|
|
err = config.ProtocolConfiguration.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, err
|
2021-03-11 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
return config, nil
|
|
|
|
}
|