2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-12-07 18:29:40 +00:00
|
|
|
"time"
|
2020-03-25 15:30:21 +00:00
|
|
|
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-09-25 10:09:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
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
|
2022-06-15 18:23:29 +00:00
|
|
|
// DefaultMaxIteratorResultItems is the default upper bound of traversed
|
|
|
|
// iterator items per JSON-RPC response.
|
|
|
|
DefaultMaxIteratorResultItems = 100
|
2022-04-26 09:32:06 +00:00
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
2022-12-06 13:34:38 +00:00
|
|
|
// Blockchain generates a Blockchain configuration based on Protocol and
|
|
|
|
// Application settings.
|
|
|
|
func (c Config) Blockchain() Blockchain {
|
|
|
|
return Blockchain{
|
|
|
|
ProtocolConfiguration: c.ProtocolConfiguration,
|
|
|
|
Ledger: c.ApplicationConfiguration.Ledger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:11:18 +00:00
|
|
|
// LoadFile loads config from the provided path. It also applies backwards compatibility
|
|
|
|
// fixups if necessary.
|
2020-07-03 08:40:40 +00:00
|
|
|
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{
|
2022-12-06 19:35:37 +00:00
|
|
|
P2P: P2P{
|
2022-12-07 18:29:40 +00:00
|
|
|
PingInterval: 30 * time.Second,
|
|
|
|
PingTimeout: 90 * time.Second,
|
2022-12-06 19:35:37 +00:00
|
|
|
},
|
2022-07-22 16:17:48 +00:00
|
|
|
RPC: RPC{
|
2022-06-15 18:23:29 +00:00
|
|
|
MaxIteratorResultItems: DefaultMaxIteratorResultItems,
|
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-12-05 15:11:18 +00:00
|
|
|
if len(config.ApplicationConfiguration.UnlockWallet.Path) > 0 && len(config.ApplicationConfiguration.Consensus.UnlockWallet.Path) == 0 {
|
|
|
|
config.ApplicationConfiguration.Consensus.UnlockWallet = config.ApplicationConfiguration.UnlockWallet
|
|
|
|
config.ApplicationConfiguration.Consensus.Enabled = true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|