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"
|
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
|
|
|
|
// 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
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|