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 1f49860d7..f5e29c900 100644 Binary files a/go.sum and b/go.sum differ