2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-11-20 17:04:09 +00:00
|
|
|
"bytes"
|
2020-03-25 15:30:21 +00:00
|
|
|
"fmt"
|
2023-11-23 10:39:24 +00:00
|
|
|
"net/http"
|
2020-03-25 15:30:21 +00:00
|
|
|
"os"
|
2023-11-23 17:41:50 +00:00
|
|
|
"path/filepath"
|
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
|
2023-08-29 09:32:00 +00:00
|
|
|
// iterator items per JSON-RPC response. It covers both session-based and
|
|
|
|
// naive iterators.
|
2022-06-15 18:23:29 +00:00
|
|
|
DefaultMaxIteratorResultItems = 100
|
2023-08-29 09:34:53 +00:00
|
|
|
// DefaultMaxFindResultItems is the default maximum number of resulting
|
|
|
|
// contract states items that can be retrieved by `findstates` JSON-RPC handler.
|
|
|
|
DefaultMaxFindResultItems = 100
|
2023-08-22 16:00:43 +00:00
|
|
|
// DefaultMaxFindStorageResultItems is the default maximum number of resulting
|
|
|
|
// contract storage items that can be retrieved by `findstorge` JSON-RPC handler.
|
|
|
|
DefaultMaxFindStorageResultItems = 50
|
2023-08-29 10:03:52 +00:00
|
|
|
// DefaultMaxNEP11Tokens is the default maximum number of resulting NEP11 tokens
|
|
|
|
// that can be traversed by `getnep11balances` JSON-RPC handler.
|
|
|
|
DefaultMaxNEP11Tokens = 100
|
2023-11-23 10:23:22 +00:00
|
|
|
// DefaultMaxRequestBodyBytes is the default maximum allowed size of HTTP
|
|
|
|
// request body in bytes.
|
|
|
|
DefaultMaxRequestBodyBytes = 5 * 1024 * 1024
|
2023-11-23 10:39:24 +00:00
|
|
|
// DefaultMaxRequestHeaderBytes is the maximum permitted size of the headers
|
|
|
|
// in an HTTP request.
|
|
|
|
DefaultMaxRequestHeaderBytes = http.DefaultMaxHeaderBytes
|
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
|
2023-11-23 17:41:50 +00:00
|
|
|
// path for the given netMode. If relativePath is not empty, relative paths in the
|
|
|
|
// config will be updated based on the provided relative path.
|
|
|
|
func Load(path string, netMode netmode.Magic, relativePath ...string) (Config, error) {
|
2020-03-25 15:30:21 +00:00
|
|
|
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
|
2023-11-23 17:41:50 +00:00
|
|
|
return LoadFile(configPath, relativePath...)
|
2020-07-03 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 15:11:18 +00:00
|
|
|
// LoadFile loads config from the provided path. It also applies backwards compatibility
|
2023-11-23 17:41:50 +00:00
|
|
|
// fixups if necessary. If relativePath is not empty, relative paths in the config will
|
|
|
|
// be updated based on the provided relative path.
|
|
|
|
func LoadFile(configPath string, relativePath ...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
|
|
|
},
|
2020-03-25 15:30:21 +00:00
|
|
|
},
|
|
|
|
}
|
2023-11-20 17:04:09 +00:00
|
|
|
decoder := yaml.NewDecoder(bytes.NewReader(configData))
|
|
|
|
decoder.KnownFields(true)
|
|
|
|
err = decoder.Decode(&config)
|
2020-03-25 15:30:21 +00:00
|
|
|
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
|
|
|
}
|
2023-11-23 17:41:50 +00:00
|
|
|
if len(relativePath) == 1 && relativePath[0] != "" {
|
|
|
|
updateRelativePaths(relativePath[0], &config)
|
|
|
|
}
|
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
|
|
|
|
}
|
2023-11-23 17:41:50 +00:00
|
|
|
|
|
|
|
// updateRelativePaths updates relative paths in the config structure based on the provided relative path.
|
|
|
|
func updateRelativePaths(relativePath string, config *Config) {
|
|
|
|
updatePath := func(path *string) {
|
|
|
|
if *path != "" && !filepath.IsAbs(*path) {
|
|
|
|
*path = filepath.Join(relativePath, *path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePath(&config.ApplicationConfiguration.LogPath)
|
|
|
|
updatePath(&config.ApplicationConfiguration.DBConfiguration.BoltDBOptions.FilePath)
|
|
|
|
updatePath(&config.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath)
|
|
|
|
updatePath(&config.ApplicationConfiguration.Consensus.UnlockWallet.Path)
|
|
|
|
updatePath(&config.ApplicationConfiguration.P2PNotary.UnlockWallet.Path)
|
|
|
|
updatePath(&config.ApplicationConfiguration.Oracle.UnlockWallet.Path)
|
|
|
|
updatePath(&config.ApplicationConfiguration.StateRoot.UnlockWallet.Path)
|
|
|
|
}
|