From 86b9aefcaee7be0995ee391e78850f1404943825 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 16 Sep 2020 10:45:08 +0300 Subject: [PATCH] [#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 --- cmd/neofs-node/accounting.go | 10 +-- cmd/neofs-node/config.go | 123 +++++++++++++++++++++++++++-------- cmd/neofs-node/container.go | 10 +-- cmd/neofs-node/grpc.go | 2 +- cmd/neofs-node/main.go | 6 +- cmd/neofs-node/morph.go | 14 ++-- go.mod | 2 +- go.sum | Bin 66973 -> 57836 bytes 8 files changed, 121 insertions(+), 46 deletions(-) diff --git a/cmd/neofs-node/accounting.go b/cmd/neofs-node/accounting.go index e99918905..5e99f2cc5 100644 --- a/cmd/neofs-node/accounting.go +++ b/cmd/neofs-node/accounting.go @@ -1,7 +1,6 @@ package main import ( - "github.com/nspcc-dev/neo-go/pkg/util" accountingGRPC "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc" "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-node/pkg/morph/client" @@ -17,10 +16,11 @@ func initAccountingService(c *cfg) { initMorphComponents(c) } - u160, err := util.Uint160DecodeStringLE(c.cfgAccounting.scriptHash) - fatalOnErr(err) - - staticClient, err := client.NewStatic(c.cfgMorph.client, u160, c.cfgAccounting.fee) + staticClient, err := client.NewStatic( + c.cfgMorph.client, + c.cfgAccounting.scriptHash, + c.cfgAccounting.fee, + ) fatalOnErr(err) balanceClient, err := balance.New(staticClient) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 8d2c2b771..52e95ddce 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -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") +} diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index 59b69c2cb..40618eaf3 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -1,7 +1,6 @@ package main import ( - "github.com/nspcc-dev/neo-go/pkg/util" containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-node/pkg/morph/client" @@ -12,10 +11,11 @@ import ( ) func initContainerService(c *cfg) { - u160, err := util.Uint160DecodeStringLE(c.cfgContainer.scriptHash) - fatalOnErr(err) - - staticClient, err := client.NewStatic(c.cfgMorph.client, u160, c.cfgContainer.fee) + staticClient, err := client.NewStatic( + c.cfgMorph.client, + c.cfgContainer.scriptHash, + c.cfgContainer.fee, + ) fatalOnErr(err) cnrClient, err := container.New(staticClient) diff --git a/cmd/neofs-node/grpc.go b/cmd/neofs-node/grpc.go index 4653e3912..be28a4303 100644 --- a/cmd/neofs-node/grpc.go +++ b/cmd/neofs-node/grpc.go @@ -10,7 +10,7 @@ import ( func initGRPC(c *cfg) { var err error - c.cfgGRPC.listener, err = net.Listen("tcp", c.cfgGRPC.endpoint) + c.cfgGRPC.listener, err = net.Listen("tcp", c.viper.GetString(cfgListenAddress)) fatalOnErr(err) c.cfgGRPC.server = grpc.NewServer() diff --git a/cmd/neofs-node/main.go b/cmd/neofs-node/main.go index e6fd51c37..74c8877ad 100644 --- a/cmd/neofs-node/main.go +++ b/cmd/neofs-node/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "log" @@ -14,7 +15,10 @@ func fatalOnErr(err error) { } func main() { - c := defaultCfg() + configFile := flag.String("config", "", "path to config") + flag.Parse() + + c := initCfg(*configFile) init_(c) diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 0c5063c0a..9ad22450d 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -1,7 +1,6 @@ package main import ( - "github.com/nspcc-dev/neo-go/pkg/util" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" @@ -11,16 +10,17 @@ import ( func initMorphComponents(c *cfg) { var err error - c.cfgMorph.client, err = client.New(c.key, c.cfgMorph.endpoint) + c.cfgMorph.client, err = client.New(c.key, c.viper.GetString(cfgMorphRPCAddress)) fatalOnErr(err) } func bootstrapNode(c *cfg) { if c.cfgNodeInfo.bootType == StorageNode { - u160, err := util.Uint160DecodeStringLE(c.cfgNetmap.scriptHash) - fatalOnErr(err) - - staticClient, err := client.NewStatic(c.cfgMorph.client, u160, c.cfgContainer.fee) + staticClient, err := client.NewStatic( + c.cfgMorph.client, + c.cfgNetmap.scriptHash, + c.cfgContainer.fee, + ) fatalOnErr(err) cli, err := netmap.New(staticClient) @@ -30,7 +30,7 @@ func bootstrapNode(c *cfg) { fatalOnErr(err) peerInfo := new(netmap.NodeInfo) - peerInfo.SetAddress(c.cfgNodeInfo.address) + peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress)) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) // todo: add attributes as opts diff --git a/go.mod b/go.mod index f6f94549e..20077c5f1 100644 --- a/go.mod +++ b/go.mod @@ -35,4 +35,4 @@ require ( ) // Used for debug reasons -replace github.com/nspcc-dev/neofs-api-go => ../neofs-api-go +// replace github.com/nspcc-dev/neofs-api-go => ../neofs-api-go diff --git a/go.sum b/go.sum index 1f49860d7dac02de404f73ab333ed0dfbe53057b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