github.com/deso-protocol/core@v1.2.9/cmd/config.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/deso-protocol/core/lib"
     5  	"github.com/golang/glog"
     6  	"github.com/spf13/viper"
     7  	"os"
     8  )
     9  
    10  type Config struct {
    11  	// Core
    12  	Params               *lib.DeSoParams
    13  	ProtocolPort         uint16
    14  	DataDirectory        string
    15  	MempoolDumpDirectory string
    16  	TXIndex              bool
    17  	Regtest              bool
    18  	PostgresURI          string
    19  
    20  	// Peers
    21  	ConnectIPs          []string
    22  	AddIPs              []string
    23  	AddSeeds            []string
    24  	TargetOutboundPeers uint32
    25  	StallTimeoutSeconds uint64
    26  
    27  	// Peer Restrictions
    28  	PrivateMode       bool
    29  	ReadOnlyMode      bool
    30  	DisableNetworking bool
    31  	IgnoreInboundInvs bool
    32  	MaxInboundPeers   uint32
    33  	OneInboundPerIp   bool
    34  
    35  	// Mining
    36  	MinerPublicKeys  []string
    37  	NumMiningThreads uint64
    38  
    39  	// Fees
    40  	RateLimitFeerate uint64
    41  	MinFeerate       uint64
    42  
    43  	// BlockProducer
    44  	MaxBlockTemplatesCache          uint64
    45  	MinBlockUpdateInterval          uint64
    46  	BlockCypherAPIKey               string
    47  	BlockProducerSeed               string
    48  	TrustedBlockProducerPublicKeys  []string
    49  	TrustedBlockProducerStartHeight uint64
    50  
    51  	// Logging
    52  	LogDirectory          string
    53  	GlogV                 uint64
    54  	GlogVmodule           string
    55  	LogDBSummarySnapshots bool
    56  	DatadogProfiler       bool
    57  }
    58  
    59  func LoadConfig() *Config {
    60  	config := Config{}
    61  
    62  	// Core
    63  	testnet := viper.GetBool("testnet")
    64  	if testnet {
    65  		config.Params = &lib.DeSoTestnetParams
    66  	} else {
    67  		config.Params = &lib.DeSoMainnetParams
    68  	}
    69  
    70  	config.ProtocolPort = uint16(viper.GetUint64("protocol-port"))
    71  	if config.ProtocolPort <= 0 {
    72  		config.ProtocolPort = config.Params.DefaultSocketPort
    73  	}
    74  
    75  	config.DataDirectory = viper.GetString("data-dir")
    76  	if config.DataDirectory == "" {
    77  		config.DataDirectory = lib.GetDataDir(config.Params)
    78  	}
    79  	if err := os.MkdirAll(config.DataDirectory, os.ModePerm); err != nil {
    80  		glog.Fatalf("Could not create data directories (%s): %v", config.DataDirectory, err)
    81  	}
    82  
    83  	config.MempoolDumpDirectory = viper.GetString("mempool-dump-dir")
    84  	config.TXIndex = viper.GetBool("txindex")
    85  	config.Regtest = viper.GetBool("regtest")
    86  	config.PostgresURI = viper.GetString("postgres-uri")
    87  
    88  	// Peers
    89  	config.ConnectIPs = viper.GetStringSlice("connect-ips")
    90  	config.AddIPs = viper.GetStringSlice("add-ips")
    91  	config.AddSeeds = viper.GetStringSlice("add-seeds")
    92  	config.TargetOutboundPeers = viper.GetUint32("target-outbound-peers")
    93  	config.StallTimeoutSeconds = viper.GetUint64("stall-timeout-seconds")
    94  
    95  	// Peer Restrictions
    96  	config.PrivateMode = viper.GetBool("private-mode")
    97  	config.ReadOnlyMode = viper.GetBool("read-only-mode")
    98  	config.DisableNetworking = viper.GetBool("disable-networking")
    99  	config.IgnoreInboundInvs = viper.GetBool("ignore-inbound-invs")
   100  	config.MaxInboundPeers = viper.GetUint32("max-inbound-peers")
   101  	config.OneInboundPerIp = viper.GetBool("one-inbound-per-ip")
   102  
   103  	// Mining + Admin
   104  	config.MinerPublicKeys = viper.GetStringSlice("miner-public-keys")
   105  	config.NumMiningThreads = viper.GetUint64("num-mining-threads")
   106  
   107  	// Fees
   108  	config.RateLimitFeerate = viper.GetUint64("rate-limit-feerate")
   109  	config.MinFeerate = viper.GetUint64("min-feerate")
   110  
   111  	// BlockProducer
   112  	config.MaxBlockTemplatesCache = viper.GetUint64("max-block-templates-cache")
   113  	config.MinBlockUpdateInterval = viper.GetUint64("min-block-update-interval")
   114  	config.BlockCypherAPIKey = viper.GetString("block-cypher-api-key")
   115  	config.BlockProducerSeed = viper.GetString("block-producer-seed")
   116  	config.TrustedBlockProducerStartHeight = viper.GetUint64("trusted-block-producer-start-height")
   117  	config.TrustedBlockProducerPublicKeys = viper.GetStringSlice("trusted-block-producer-public-keys")
   118  
   119  	// Logging
   120  	config.LogDirectory = viper.GetString("log-dir")
   121  	if config.LogDirectory == "" {
   122  		config.LogDirectory = config.DataDirectory
   123  	}
   124  	config.GlogV = viper.GetUint64("glog-v")
   125  	config.GlogVmodule = viper.GetString("glog-vmodule")
   126  	config.LogDBSummarySnapshots = viper.GetBool("log-db-summary-snapshots")
   127  	config.DatadogProfiler = viper.GetBool("datadog-profiler")
   128  
   129  	return &config
   130  }
   131  
   132  func (config *Config) Print() {
   133  	glog.Infof("Logging to directory %s", config.LogDirectory)
   134  	glog.Infof("Running node in %s mode", config.Params.NetworkType)
   135  	glog.Infof("Data Directory: %s", config.DataDirectory)
   136  
   137  	if config.MempoolDumpDirectory != "" {
   138  		glog.Infof("Mempool Dump Directory: %s", config.MempoolDumpDirectory)
   139  	}
   140  
   141  	if config.PostgresURI != "" {
   142  		glog.Infof("Postgres URI: %s", config.PostgresURI)
   143  	}
   144  
   145  	if len(config.ConnectIPs) > 0 {
   146  		glog.Infof("Connect IPs: %s", config.ConnectIPs)
   147  	}
   148  
   149  	if len(config.AddIPs) > 0 {
   150  		glog.Infof("Add IPs: %s", config.ConnectIPs)
   151  	}
   152  
   153  	if config.PrivateMode {
   154  		glog.Infof("PRIVATE MODE")
   155  	}
   156  
   157  	if config.ReadOnlyMode {
   158  		glog.Infof("READ ONLY MODE")
   159  	}
   160  
   161  	if config.DisableNetworking {
   162  		glog.Infof("NETWORKING DISABLED")
   163  	}
   164  
   165  	if config.IgnoreInboundInvs {
   166  		glog.Infof("IGNORING INBOUND INVS")
   167  	}
   168  
   169  	glog.Infof("Max Inbound Peers: %d", config.MaxInboundPeers)
   170  	glog.Infof("Protocol listening on port %d", config.ProtocolPort)
   171  
   172  	if len(config.MinerPublicKeys) > 0 {
   173  		glog.Infof("Mining with public keys: %s", config.MinerPublicKeys)
   174  	}
   175  
   176  	glog.Infof("Rate Limit Feerate: %d", config.RateLimitFeerate)
   177  	glog.Infof("Min Feerate: %d", config.MinFeerate)
   178  }