github.com/igggame/nebulas-go@v2.1.0+incompatible/net/config.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package net
    20  
    21  import (
    22  	"fmt"
    23  	"net"
    24  	"time"
    25  
    26  	"github.com/multiformats/go-multiaddr"
    27  	"github.com/nebulasio/go-nebulas/neblet/pb"
    28  )
    29  
    30  // const
    31  const ( // TODO delete redundant vars
    32  	DefaultBucketCapacity         = 64
    33  	DefaultRoutingTableMaxLatency = 10
    34  	DefaultPrivateKeyPath         = "conf/network.key"
    35  	DefaultMaxSyncNodes           = 64
    36  	DefaultChainID                = 1
    37  	DefaultRoutingTableDir        = ""
    38  	DefaultMaxStreamNum           = 200
    39  	DefaultReservedStreamNum      = 20
    40  )
    41  
    42  // Default Configuration in P2P network
    43  var (
    44  	DefaultListen = []string{"0.0.0.0:8680"}
    45  
    46  	RouteTableSyncLoopInterval     = 30 * time.Second
    47  	RouteTableSaveToDiskInterval   = 3 * 60 * time.Second
    48  	RouteTableCacheFileName        = "routetable.cache"
    49  	RouteTableInternalNodeFileName = "conf/internal_list.txt"
    50  
    51  	MaxPeersCountForSyncResp = 32
    52  )
    53  
    54  // Config TODO: move to proto config.
    55  type Config struct {
    56  	Bucketsize           int
    57  	Latency              time.Duration
    58  	BootNodes            []multiaddr.Multiaddr
    59  	PrivateKeyPath       string
    60  	Listen               []string
    61  	MaxSyncNodes         int
    62  	ChainID              uint32
    63  	RoutingTableDir      string
    64  	StreamLimits         int32
    65  	ReservedStreamLimits int32
    66  }
    67  
    68  // Neblet interface breaks cycle import dependency.
    69  type Neblet interface {
    70  	Config() *nebletpb.Config
    71  }
    72  
    73  // NewP2PConfig return new config object.
    74  func NewP2PConfig(n Neblet) *Config {
    75  	chainConf := n.Config().Chain
    76  	networkConf := n.Config().Network
    77  	config := NewConfigFromDefaults()
    78  
    79  	// listen.
    80  	if len(networkConf.Listen) == 0 {
    81  		panic("Missing network.listen config.")
    82  	}
    83  	if err := verifyListenAddress(networkConf.Listen); err != nil {
    84  		panic(fmt.Sprintf("Invalid network.listen config: err is %s, config value is %s.", err, networkConf.Listen))
    85  	}
    86  	config.Listen = networkConf.Listen
    87  
    88  	// private key path.
    89  	if checkPathConfig(networkConf.PrivateKey) == false {
    90  		panic(fmt.Sprintf("The network private key path %s is not exist.", networkConf.PrivateKey))
    91  	}
    92  	config.PrivateKeyPath = networkConf.PrivateKey
    93  
    94  	// Chain ID.
    95  	config.ChainID = chainConf.ChainId
    96  
    97  	// routing table dir.
    98  	// TODO: @robin using diff dir for temp files.
    99  	if checkPathConfig(chainConf.Datadir) == false {
   100  		panic(fmt.Sprintf("The chain data directory %s is not exist.", chainConf.Datadir))
   101  	}
   102  	config.RoutingTableDir = chainConf.Datadir
   103  
   104  	// seed server address.
   105  	seeds := networkConf.Seed
   106  	if len(seeds) > 0 {
   107  		config.BootNodes = make([]multiaddr.Multiaddr, len(seeds))
   108  		for i, v := range seeds {
   109  			addr, err := multiaddr.NewMultiaddr(v)
   110  			if err != nil {
   111  				panic(fmt.Sprintf("Invalid seed address config: err is %s, config value is %s.", err, v))
   112  			}
   113  			config.BootNodes[i] = addr
   114  		}
   115  	}
   116  
   117  	// max stream limits
   118  	if networkConf.GetStreamLimits() > 0 {
   119  		config.StreamLimits = networkConf.StreamLimits
   120  	}
   121  
   122  	if networkConf.GetReservedStreamLimits() > 0 {
   123  		config.ReservedStreamLimits = networkConf.ReservedStreamLimits
   124  	}
   125  
   126  	return config
   127  }
   128  
   129  func localHost() string {
   130  	addrs, err := net.InterfaceAddrs()
   131  	if err != nil {
   132  		return ""
   133  	}
   134  	for _, address := range addrs {
   135  		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
   136  			if ipnet.IP.To4() != nil {
   137  				return ipnet.IP.String()
   138  			}
   139  		}
   140  	}
   141  
   142  	return ""
   143  }
   144  
   145  // NewConfigFromDefaults return new config from defaults.
   146  func NewConfigFromDefaults() *Config {
   147  	return &Config{
   148  		DefaultBucketCapacity,
   149  		DefaultRoutingTableMaxLatency,
   150  		[]multiaddr.Multiaddr{},
   151  		DefaultPrivateKeyPath,
   152  		DefaultListen,
   153  		DefaultMaxSyncNodes,
   154  		DefaultChainID,
   155  		DefaultRoutingTableDir,
   156  		DefaultMaxStreamNum,
   157  		DefaultReservedStreamNum,
   158  	}
   159  }