2020-08-21 15:01:59 +00:00
|
|
|
package main
|
|
|
|
|
2020-08-22 11:03:45 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2020-08-22 14:17:03 +00:00
|
|
|
"crypto/ecdsa"
|
2020-08-24 09:40:32 +00:00
|
|
|
"net"
|
2020-09-22 11:02:32 +00:00
|
|
|
"strconv"
|
2020-09-16 07:45:08 +00:00
|
|
|
"strings"
|
2020-08-22 11:03:45 +00:00
|
|
|
"sync"
|
2020-08-22 14:17:03 +00:00
|
|
|
|
2020-08-22 15:20:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-08-22 14:17:03 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-09-16 07:45:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/misc"
|
2020-08-22 15:20:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-08-24 15:51:42 +00:00
|
|
|
tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
2020-09-16 07:45:08 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-08-22 15:20:47 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-08-22 11:03:45 +00:00
|
|
|
)
|
|
|
|
|
2020-09-16 07:45:08 +00:00
|
|
|
const (
|
|
|
|
// config keys for cfgNodeInfo
|
2020-09-22 11:02:32 +00:00
|
|
|
cfgNodeKey = "node.key"
|
|
|
|
cfgBootstrapAddress = "node.address"
|
|
|
|
cfgNodeAttributePrefix = "node.attribute"
|
2020-09-16 07:45:08 +00:00
|
|
|
|
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2020-08-21 15:01:59 +00:00
|
|
|
type cfg struct {
|
2020-08-22 11:03:45 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
2020-09-16 07:45:08 +00:00
|
|
|
viper *viper.Viper
|
|
|
|
|
2020-08-22 11:03:45 +00:00
|
|
|
wg *sync.WaitGroup
|
|
|
|
|
2020-08-22 14:17:03 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
cfgGRPC cfgGRPC
|
|
|
|
|
|
|
|
cfgMorph cfgMorph
|
|
|
|
|
|
|
|
cfgAccounting cfgAccounting
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
cfgContainer cfgContainer
|
2020-08-24 15:51:42 +00:00
|
|
|
|
2020-08-31 15:19:21 +00:00
|
|
|
cfgNetmap cfgNetmap
|
|
|
|
|
2020-08-24 15:51:42 +00:00
|
|
|
privateTokenStore *tokenStorage.TokenStore
|
2020-08-31 15:19:21 +00:00
|
|
|
|
|
|
|
cfgNodeInfo cfgNodeInfo
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cfgGRPC struct {
|
|
|
|
listener net.Listener
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
server *grpc.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
type cfgMorph struct {
|
|
|
|
client *client.Client
|
|
|
|
}
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
type cfgAccounting struct {
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash util.Uint160
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
fee util.Fixed8
|
2020-08-21 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
type cfgContainer struct {
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash util.Uint160
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
fee util.Fixed8
|
|
|
|
}
|
|
|
|
|
2020-08-31 15:19:21 +00:00
|
|
|
type cfgNetmap struct {
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash util.Uint160
|
2020-08-31 15:19:21 +00:00
|
|
|
|
|
|
|
fee util.Fixed8
|
|
|
|
}
|
|
|
|
|
|
|
|
type BootstrapType uint32
|
|
|
|
|
|
|
|
type cfgNodeInfo struct {
|
2020-09-22 11:02:32 +00:00
|
|
|
bootType BootstrapType
|
|
|
|
attributes []string
|
2020-09-22 12:31:13 +00:00
|
|
|
capacity uint64 // default: 0
|
|
|
|
price uint64 // default: 0
|
2020-08-31 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ BootstrapType = iota
|
|
|
|
StorageNode
|
|
|
|
RelayNode
|
|
|
|
)
|
|
|
|
|
2020-09-16 07:45:08 +00:00
|
|
|
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))
|
2020-08-22 14:17:03 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2020-08-21 15:01:59 +00:00
|
|
|
return &cfg{
|
2020-09-16 07:45:08 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
viper: viperCfg,
|
|
|
|
wg: new(sync.WaitGroup),
|
|
|
|
key: key,
|
2020-08-24 09:40:32 +00:00
|
|
|
cfgAccounting: cfgAccounting{
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash: u160Accounting,
|
|
|
|
fee: util.Fixed8(viperCfg.GetInt(cfgAccountingFee)),
|
2020-08-22 15:20:47 +00:00
|
|
|
},
|
2020-08-24 14:07:08 +00:00
|
|
|
cfgContainer: cfgContainer{
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash: u160Container,
|
|
|
|
fee: util.Fixed8(viperCfg.GetInt(cfgContainerFee)),
|
2020-08-24 14:07:08 +00:00
|
|
|
},
|
2020-08-31 15:19:21 +00:00
|
|
|
cfgNetmap: cfgNetmap{
|
2020-09-16 07:45:08 +00:00
|
|
|
scriptHash: u160Netmap,
|
|
|
|
fee: util.Fixed8(viperCfg.GetInt(cfgNetmapFee)),
|
2020-08-31 15:19:21 +00:00
|
|
|
},
|
|
|
|
cfgNodeInfo: cfgNodeInfo{
|
2020-09-22 11:02:32 +00:00
|
|
|
bootType: StorageNode,
|
|
|
|
attributes: readAttributes(viperCfg),
|
2020-08-31 15:19:21 +00:00
|
|
|
},
|
2020-08-21 15:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 07:45:08 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2020-09-22 11:02:32 +00:00
|
|
|
|
|
|
|
func readAttributes(v *viper.Viper) (attrs []string) {
|
|
|
|
const maxAttributes = 100
|
|
|
|
|
|
|
|
for i := 0; i < maxAttributes; i++ {
|
|
|
|
attr := v.GetString(cfgNodeAttributePrefix + "_" + strconv.Itoa(i))
|
|
|
|
if attr == "" {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|