From 1b71a23e73c6973a81a7a2632bc41a4dc16939c7 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 19 Aug 2020 23:26:48 +0400 Subject: [PATCH] Refactor configuration system of the node --- config/config.go | 97 +++++++++++++++++++--------------------------- node/config.go | 19 +++++++++ node/flags.go | 28 ------------- node/node.go | 36 +++++++++++------ p2p-oracle-node.go | 6 ++- 5 files changed, 88 insertions(+), 98 deletions(-) create mode 100644 node/config.go delete mode 100644 node/flags.go diff --git a/config/config.go b/config/config.go index a5b1a9e..82af425 100644 --- a/config/config.go +++ b/config/config.go @@ -1,74 +1,55 @@ package config import ( - "log" - "github.com/spf13/viper" ) type Config struct { - ListenPort string `toml:"listen_port"` - ListenAddr string `toml:"listen_addr"` - Bootstrap bool `toml:"is_bootstrap"` - BootstrapNodeMultiaddr string `toml:"bootstrap_node_multiaddr"` - Rendezvous string `toml:"rendezvous"` - ProtocolID string `toml:"protocol_id"` - SessionKey string `toml:"session_key"` - PrivateKey string `toml:"private_key"` + ListenPort string `mapstructure:"listen_port"` + ListenAddr string `mapstructure:"listen_addr"` + Bootstrap bool `mapstructure:"is_bootstrap"` + BootstrapNodeMultiaddr string `mapstructure:"bootstrap_node_multiaddr"` + Rendezvous string `mapstructure:"rendezvous"` + SessionKey string `mapstructure:"session_key"` + Etherium EtheriumConfig `mapstructure:"eth"` + PubSub PubSubConfig `mapstructure:"pubSub"` } -// viperEnvVariable loads config parameters from .env file -func viperEnvString(key string, default_value string) string { - viper.SetConfigFile(".env") - - err := viper.ReadInConfig() - - if err != nil { - log.Fatalf("Error while reading config file %s", err) - } - - value, ok := viper.Get(key).(string) - - if !ok { - return default_value - } - - return value +type EtheriumConfig struct { + PrivateKey string `mapstructure:"private_key"` } -func viperEnvBoolean(key string, default_value bool) bool { - viper.SetConfigFile(".env") - - err := viper.ReadInConfig() - - if err != nil { - log.Fatalf("Error while reading config file %s", err) - } - - value := viper.GetBool(key) - - return value +type PubSubConfig struct { + ProtocolID string `mapstructure:"protocolID"` } // NewConfig creates a new config based on default values or provided .env file -func NewConfig() *Config { - ListenPort := viperEnvString("LISTEN_PORT", ":8000") - ListenAddr := viperEnvString("LISTEN_ADDRESS", "debug") - Bootstrap := viperEnvBoolean("BOOTSTRAP_NODE", false) - BootstrapNodeMultiaddr := viperEnvString("BOOTSTRAP_NODE_MULTIADDRESS", "/ip4/127.0.0.1/tcp/0") - Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle") - ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle") - SessionKey := viperEnvString("SESSION_KEY", "go") - PrivateKey := viperEnvString("PRIVATE_KEY", "") - - return &Config{ - ListenPort: ListenPort, - ListenAddr: ListenAddr, - Bootstrap: Bootstrap, - BootstrapNodeMultiaddr: BootstrapNodeMultiaddr, - Rendezvous: Rendezvous, - ProtocolID: ProtocolID, - SessionKey: SessionKey, - PrivateKey: PrivateKey, +func NewConfig(configPath string) (*Config, error) { + cfg := &Config{ + ListenAddr: "localhost", + ListenPort: ":8000", + Bootstrap: false, + BootstrapNodeMultiaddr: "/ip4/127.0.0.1/tcp/0", + Rendezvous: "filecoin-p2p-oracle", + Etherium: EtheriumConfig{ + PrivateKey: "", + }, + SessionKey: "go", + PubSub: PubSubConfig{ + ProtocolID: "p2p-oracle", + }, } + + viper.SetConfigFile(configPath) + err := viper.ReadInConfig() + if err != nil { + return nil, err + } + + err = viper.Unmarshal(cfg) + if err != nil { + return nil, err + } + + return cfg, nil } diff --git a/node/config.go b/node/config.go new file mode 100644 index 0000000..ee5ba56 --- /dev/null +++ b/node/config.go @@ -0,0 +1,19 @@ +package node + +type Config struct { + EthPrivateKey string `toml:"ethPrivateKey"` + ListenAddress string `toml:"listenAddress"` + ListenPort string `toml:"listenPort"` + BootstrapAddress string `toml:"bootstrapAddress"` + RendezvousString string `toml:"rendezvousString"` + PubSub PubSubConfig `toml:"pubSub"` +} + +type PubSubConfig struct { + ProtocolID string `toml:"protocolID"` +} + +type EtheriumConfig struct { + PrivateKey string `toml:"privateKey"` + GatewayAddress string `toml:"gatewayAddress"` +} diff --git a/node/flags.go b/node/flags.go deleted file mode 100644 index 0f74703..0000000 --- a/node/flags.go +++ /dev/null @@ -1,28 +0,0 @@ -package node - -import ( - "flag" - - "github.com/Secured-Finance/p2p-oracle-node/config" -) - -func (node *Node) parseFlags() { - listenPort := flag.String("port", node.Config.ListenPort, "Listen port number") - listenAddr := flag.String("addr", node.Config.ListenAddr, "Listen address") - bootstrap := flag.Bool("bootstrap", node.Config.Bootstrap, "Start up bootstrap node") - bootstrapAddress := flag.String("baddr", node.Config.BootstrapNodeMultiaddr, "Address of bootstrap node") - rendezvousString := flag.String("rendezvous", node.Config.Rendezvous, "DHT rendezvous string") - protocolID := flag.String("protocol-id", node.Config.ProtocolID, "PubSub protocol ID") - - flag.Parse() - - new_config := &config.Config{ - ListenPort: *listenPort, - ListenAddr: *listenAddr, - Bootstrap: *bootstrap, - BootstrapNodeMultiaddr: *bootstrapAddress, - Rendezvous: *rendezvousString, - ProtocolID: *protocolID, - } - node.Config = new_config -} diff --git a/node/node.go b/node/node.go index 3c63b47..6675ab1 100644 --- a/node/node.go +++ b/node/node.go @@ -3,6 +3,7 @@ package node import ( "context" "crypto/rand" + "flag" "fmt" "github.com/Secured-Finance/p2p-oracle-node/config" @@ -34,16 +35,20 @@ type Node struct { Ethereum *rpcclient.EthereumClient } -func NewNode() *Node { +func NewNode(configPath string) (*Node, error) { + cfg, err := config.NewConfig(configPath) + if err != nil { + return nil, err + } node := &Node{ OracleTopic: "p2p_oracle", - Config: config.NewConfig(), - Logger: log.Logger("rendezvous"), + Config: cfg, + Logger: log.Logger("node"), networkTopics: mapset.NewSet(), } log.SetAllLoggers(log.LevelInfo) - return node + return node, nil } func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) { @@ -63,16 +68,24 @@ func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) { node.startPubSub(ctx, host) } -func Start() { - node := NewNode() - log.SetAllLoggers(log.LevelInfo) +func Start() error { + configPath := flag.String("config", "", "Path to config") + verbose := flag.Bool("verbose", false, "Verbose logging") + flag.Parse() - err := log.SetLogLevel("rendezvous", "info") - if err != nil { - node.Logger.Warn("Failed to set a rendezvous log level:", err) + if *configPath == "" { + return fmt.Errorf("no config path provided") } - node.parseFlags() + node, err := NewNode(*configPath) + if *verbose { + log.SetAllLoggers(log.LevelDebug) + } else { + log.SetAllLoggers(log.LevelInfo) + } + if err != nil { + log.Logger("node").Panic(err) + } r := rand.Reader @@ -87,4 +100,5 @@ func Start() { node.GlobalCtxCancel = ctxCancel node.setupNode(ctx, prvKey) + return nil } diff --git a/p2p-oracle-node.go b/p2p-oracle-node.go index 2b065b4..092087b 100644 --- a/p2p-oracle-node.go +++ b/p2p-oracle-node.go @@ -2,8 +2,12 @@ package main import ( "github.com/Secured-Finance/p2p-oracle-node/node" + "github.com/ipfs/go-log" ) func main() { - node.Start() + err := node.Start() + if err != nil { + log.Logger("node").Panic(err) + } }