[#28] Make storage node configurable
To run storage node at dev-env environment it should have configurable parameters. To keep `cfg` structures we can read configuration from env and yml config file with viper and parse values such as script hashes, fees, keys into `cfg` structures. Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
001a23eb31
commit
86b9aefcae
8 changed files with 123 additions and 149 deletions
|
@ -4,18 +4,47 @@ import (
|
|||
"context"
|
||||
"crypto/ecdsa"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
crypto "github.com/nspcc-dev/neofs-crypto"
|
||||
"github.com/nspcc-dev/neofs-node/misc"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
||||
tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
// config keys for cfgNodeInfo
|
||||
cfgNodeKey = "node.key"
|
||||
cfgBootstrapAddress = "node.address"
|
||||
|
||||
// config keys for cfgGRPC
|
||||
cfgListenAddress = "grpc.endpoint"
|
||||
|
||||
// config keys for cfgMorph
|
||||
cfgMorphRPCAddress = "morph.endpoint"
|
||||
|
||||
// config keys for cfgAccounting
|
||||
cfgAccountingContract = "accounting.scripthash"
|
||||
cfgAccountingFee = "accounting.fee"
|
||||
|
||||
// config keys for cfgNetmap
|
||||
cfgNetmapContract = "netmap.scripthash"
|
||||
cfgNetmapFee = "netmap.fee"
|
||||
|
||||
// config keys for cfgContainer
|
||||
cfgContainerContract = "container.scripthash"
|
||||
cfgContainerFee = "container.fee"
|
||||
)
|
||||
|
||||
type cfg struct {
|
||||
ctx context.Context
|
||||
|
||||
viper *viper.Viper
|
||||
|
||||
wg *sync.WaitGroup
|
||||
|
||||
key *ecdsa.PrivateKey
|
||||
|
@ -36,33 +65,29 @@ type cfg struct {
|
|||
}
|
||||
|
||||
type cfgGRPC struct {
|
||||
endpoint string
|
||||
|
||||
listener net.Listener
|
||||
|
||||
server *grpc.Server
|
||||
}
|
||||
|
||||
type cfgMorph struct {
|
||||
endpoint string
|
||||
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
type cfgAccounting struct {
|
||||
scriptHash string
|
||||
scriptHash util.Uint160
|
||||
|
||||
fee util.Fixed8
|
||||
}
|
||||
|
||||
type cfgContainer struct {
|
||||
scriptHash string
|
||||
scriptHash util.Uint160
|
||||
|
||||
fee util.Fixed8
|
||||
}
|
||||
|
||||
type cfgNetmap struct {
|
||||
scriptHash string
|
||||
scriptHash util.Uint160
|
||||
|
||||
fee util.Fixed8
|
||||
}
|
||||
|
@ -71,8 +96,6 @@ type BootstrapType uint32
|
|||
|
||||
type cfgNodeInfo struct {
|
||||
bootType BootstrapType
|
||||
|
||||
address string
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -81,34 +104,82 @@ const (
|
|||
RelayNode
|
||||
)
|
||||
|
||||
func defaultCfg() *cfg {
|
||||
key, err := crypto.LoadPrivateKey("Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s")
|
||||
func initCfg(path string) *cfg {
|
||||
viperCfg := initViper(path)
|
||||
|
||||
key, err := crypto.LoadPrivateKey(viperCfg.GetString(cfgNodeKey))
|
||||
fatalOnErr(err)
|
||||
|
||||
u160Accounting, err := util.Uint160DecodeStringLE(
|
||||
viperCfg.GetString(cfgAccountingContract))
|
||||
fatalOnErr(err)
|
||||
|
||||
u160Netmap, err := util.Uint160DecodeStringLE(
|
||||
viperCfg.GetString(cfgNetmapContract))
|
||||
fatalOnErr(err)
|
||||
|
||||
u160Container, err := util.Uint160DecodeStringLE(
|
||||
viperCfg.GetString(cfgContainerContract))
|
||||
fatalOnErr(err)
|
||||
|
||||
return &cfg{
|
||||
ctx: context.Background(),
|
||||
wg: new(sync.WaitGroup),
|
||||
key: key,
|
||||
cfgGRPC: cfgGRPC{
|
||||
endpoint: "127.0.0.1:50501",
|
||||
},
|
||||
cfgMorph: cfgMorph{
|
||||
endpoint: "http://morph_chain.localtest.nspcc.ru:30333/",
|
||||
},
|
||||
ctx: context.Background(),
|
||||
viper: viperCfg,
|
||||
wg: new(sync.WaitGroup),
|
||||
key: key,
|
||||
cfgAccounting: cfgAccounting{
|
||||
scriptHash: "1aeefe1d0dfade49740fff779c02cd4a0538ffb1",
|
||||
fee: util.Fixed8(1),
|
||||
scriptHash: u160Accounting,
|
||||
fee: util.Fixed8(viperCfg.GetInt(cfgAccountingFee)),
|
||||
},
|
||||
cfgContainer: cfgContainer{
|
||||
scriptHash: "9d2ca84d7fb88213c4baced5a6ed4dc402309039",
|
||||
fee: util.Fixed8(1),
|
||||
scriptHash: u160Container,
|
||||
fee: util.Fixed8(viperCfg.GetInt(cfgContainerFee)),
|
||||
},
|
||||
cfgNetmap: cfgNetmap{
|
||||
scriptHash: "75194459637323ea8837d2afe8225ec74a5658c3",
|
||||
fee: util.Fixed8(1),
|
||||
scriptHash: u160Netmap,
|
||||
fee: util.Fixed8(viperCfg.GetInt(cfgNetmapFee)),
|
||||
},
|
||||
cfgNodeInfo: cfgNodeInfo{
|
||||
bootType: StorageNode,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func initViper(path string) *viper.Viper {
|
||||
v := viper.New()
|
||||
|
||||
v.SetEnvPrefix(misc.Prefix)
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
v.SetDefault("app.name", misc.NodeName)
|
||||
v.SetDefault("app.version", misc.Version)
|
||||
|
||||
defaultConfiguration(v)
|
||||
|
||||
if path != "" {
|
||||
v.SetConfigFile(path)
|
||||
v.SetConfigType("yml")
|
||||
fatalOnErr(v.ReadInConfig())
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func defaultConfiguration(v *viper.Viper) {
|
||||
// fixme: all hardcoded private keys must be removed
|
||||
v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s")
|
||||
v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with
|
||||
|
||||
v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/")
|
||||
v.SetDefault(cfgListenAddress, "127.0.0.1:50501") // listen address
|
||||
|
||||
v.SetDefault(cfgAccountingContract, "1aeefe1d0dfade49740fff779c02cd4a0538ffb1")
|
||||
v.SetDefault(cfgAccountingFee, "1")
|
||||
|
||||
v.SetDefault(cfgContainerContract, "9d2ca84d7fb88213c4baced5a6ed4dc402309039")
|
||||
v.SetDefault(cfgContainerFee, "1")
|
||||
|
||||
v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3")
|
||||
v.SetDefault(cfgNetmapFee, "1")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue