2021-06-01 17:42:57 +00:00
|
|
|
package nodeconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-28 14:01:31 +00:00
|
|
|
"os"
|
2021-06-01 17:42:57 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-05-31 08:55:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-06-01 17:42:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-05-31 08:55:38 +00:00
|
|
|
utilConfig "github.com/nspcc-dev/neofs-node/pkg/util/config"
|
2021-06-01 17:42:57 +00:00
|
|
|
)
|
|
|
|
|
2021-09-06 13:03:43 +00:00
|
|
|
// PersistentStateConfig is a wrapper over "persistent_state" config section
|
|
|
|
// which provides access to persistent state storage configuration of node.
|
|
|
|
type PersistentStateConfig struct {
|
|
|
|
cfg *config.Config
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:42:57 +00:00
|
|
|
const (
|
2021-09-06 13:03:43 +00:00
|
|
|
subsection = "node"
|
|
|
|
persistentStateSubsection = "persistent_state"
|
2021-06-01 17:42:57 +00:00
|
|
|
|
|
|
|
attributePrefix = "attribute"
|
2021-09-06 13:03:43 +00:00
|
|
|
|
|
|
|
// PersistentStatePathDefault is a default path for persistent state file.
|
|
|
|
PersistentStatePathDefault = ".neofs-storage-state"
|
2021-06-01 17:42:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Key returns value of "key" config parameter
|
|
|
|
// from "node" section.
|
|
|
|
//
|
2021-07-13 08:43:51 +00:00
|
|
|
// If value is not set, fallbacks to Wallet section.
|
|
|
|
//
|
|
|
|
// Panics if value is incorrect filename of binary encoded private key.
|
2021-06-03 15:24:09 +00:00
|
|
|
func Key(c *config.Config) *keys.PrivateKey {
|
2021-06-01 17:42:57 +00:00
|
|
|
v := config.StringSafe(c.Sub(subsection), "key")
|
|
|
|
if v == "" {
|
2021-07-13 08:43:51 +00:00
|
|
|
return Wallet(c)
|
2021-06-01 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 15:24:09 +00:00
|
|
|
var (
|
|
|
|
key *keys.PrivateKey
|
|
|
|
err error
|
|
|
|
data []byte
|
|
|
|
)
|
2021-06-28 14:01:31 +00:00
|
|
|
if data, err = os.ReadFile(v); err == nil {
|
2021-06-03 15:24:09 +00:00
|
|
|
key, err = keys.NewPrivateKeyFromBytes(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-13 08:43:51 +00:00
|
|
|
panic(fmt.Errorf("invalid private key in node section: %w", err))
|
2021-06-03 15:24:09 +00:00
|
|
|
}
|
2021-06-01 17:42:57 +00:00
|
|
|
|
2021-06-03 15:24:09 +00:00
|
|
|
return key
|
2021-06-01 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 08:55:38 +00:00
|
|
|
// Wallet returns value of node private key from "node" section.
|
2021-07-13 08:43:51 +00:00
|
|
|
//
|
|
|
|
// Panics if section contains invalid values.
|
2021-05-31 08:55:38 +00:00
|
|
|
func Wallet(c *config.Config) *keys.PrivateKey {
|
|
|
|
v := c.Sub(subsection).Sub("wallet")
|
|
|
|
acc, err := utilConfig.LoadAccount(
|
|
|
|
config.String(v, "path"),
|
|
|
|
config.String(v, "address"),
|
|
|
|
config.String(v, "password"))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("invalid wallet config: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc.PrivateKey()
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:35:56 +00:00
|
|
|
type stringAddressGroup []string
|
|
|
|
|
|
|
|
func (x stringAddressGroup) IterateAddresses(f func(string) bool) {
|
|
|
|
for i := range x {
|
|
|
|
if f(x[i]) {
|
|
|
|
break
|
|
|
|
}
|
2021-06-01 17:42:57 +00:00
|
|
|
}
|
2021-06-22 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x stringAddressGroup) NumberOfAddresses() int {
|
|
|
|
return len(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BootstrapAddresses returns value of "addresses" config parameter
|
|
|
|
// from "node" section as network.AddressGroup.
|
|
|
|
//
|
|
|
|
// Panics if value is not a string list of valid NeoFS network addresses.
|
|
|
|
func BootstrapAddresses(c *config.Config) (addr network.AddressGroup) {
|
|
|
|
v := config.StringSlice(c.Sub(subsection), "addresses")
|
2021-06-01 17:42:57 +00:00
|
|
|
|
2021-06-22 17:35:56 +00:00
|
|
|
err := addr.FromIterator(stringAddressGroup(v))
|
2021-06-01 17:42:57 +00:00
|
|
|
if err != nil {
|
2021-06-22 17:35:56 +00:00
|
|
|
panic(fmt.Errorf("could not parse bootstrap addresses: %w", err))
|
2021-06-01 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns list of config parameters
|
|
|
|
// from "node" section that are set in "attribute_i" format,
|
|
|
|
// where i in range [0,100).
|
|
|
|
func Attributes(c *config.Config) (attrs []string) {
|
|
|
|
const maxAttributes = 100
|
|
|
|
|
|
|
|
for i := 0; i < maxAttributes; i++ {
|
|
|
|
attr := config.StringSafe(c.Sub(subsection), attributePrefix+"_"+strconv.Itoa(i))
|
|
|
|
if attr == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relay returns value of "relay" config parameter
|
|
|
|
// from "node" section.
|
|
|
|
//
|
|
|
|
// Returns false if value is not set.
|
|
|
|
func Relay(c *config.Config) bool {
|
|
|
|
return config.BoolSafe(c.Sub(subsection), "relay")
|
|
|
|
}
|
2021-09-06 13:03:43 +00:00
|
|
|
|
|
|
|
// PersistentState returns structure that provides access to "persistent_state"
|
|
|
|
// subsection of "node" section.
|
|
|
|
func PersistentState(c *config.Config) PersistentStateConfig {
|
|
|
|
return PersistentStateConfig{
|
|
|
|
c.Sub(subsection).Sub(persistentStateSubsection),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns value of "path" config parameter.
|
|
|
|
//
|
|
|
|
// Returns PersistentStatePathDefault if value is not a non-empty string.
|
|
|
|
func (p PersistentStateConfig) Path() string {
|
|
|
|
v := config.String(p.cfg, "path")
|
|
|
|
if v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return PersistentStatePathDefault
|
|
|
|
}
|