Skip to content

Commit

Permalink
use unified config, give a default config and parsed from cmd better.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-vite committed Aug 17, 2018
1 parent 950cc37 commit 6de9535
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 121 deletions.
57 changes: 20 additions & 37 deletions cmd/gvite/main.go
Expand Up @@ -11,20 +11,24 @@ import (
_ "net/http/pprof"
)

var (
nameFlag = flag.String("name", "", "boot name")
maxPeersFlag = flag.Uint("maxpeers", 0, "max number of connections will be connected")
addrFlag = flag.String("addr", "0.0.0.0:8483", "will be listen by vite")
privateKeyFlag = flag.String("priv", "", "use for sign message")
dataDirFlag = flag.String("dir", "", "use for store all files")
netIdFlag = flag.Uint("netid", 0, "the network vite will connect")
//minerFlag = flag.Bool("miner", false, "boot miner")
//minerInterval = flag.Int("minerInterval", 6, "miner interval(unit sec).")
//coinbaseFlag = flag.String("coinbaseAddress", "", "boot coinbaseAddress")
)
func parseConfig() *config.Config {
var globalConfig = config.GlobalConfig

func main() {
flag.StringVar(&globalConfig.Name, "name", globalConfig.Name, "boot name")
flag.UintVar(&globalConfig.MaxPeers, "peers", globalConfig.MaxPeers, "max number of connections will be connected")
flag.StringVar(&globalConfig.Addr, "addr", globalConfig.Addr, "will be listen by vite")
flag.StringVar(&globalConfig.PrivateKey, "priv", globalConfig.PrivateKey, "hex encode of ed25519 privateKey, use for sign message")
flag.StringVar(&globalConfig.DataDir, "dir", globalConfig.DataDir, "use for store all files")
flag.UintVar(&globalConfig.NetID, "netid", globalConfig.NetID, "the network vite will connect")

flag.Parse()

globalConfig.P2P.Datadir = globalConfig.DataDir

return globalConfig
}

func main() {
govite.PrintBuildVersion()

mainLog := log15.New("module", "gvite/main")
Expand All @@ -36,40 +40,19 @@ func main() {
}
}()

flag.Parse()

globalConfig := config.GlobalConfig

if *dataDirFlag != "" {
globalConfig.DataDir = *dataDirFlag
}

globalConfig.P2P = config.MergeP2PConfig(&config.P2P{
Name: *nameFlag,
MaxPeers: uint32(*maxPeersFlag),
Addr: *addrFlag,
PrivateKey: *privateKeyFlag,
NetID: *netIdFlag,
})
globalConfig.P2P.Datadir = globalConfig.DataDir

//globalConfig.Miner = config.MergeMinerConfig(&config.Miner{
// Miner: *minerFlag,
// Coinbase: *coinbaseFlag,
// MinerInterval: *minerInterval,
//})
parsedConfig := parseConfig()

if s, e := config.GlobalConfig.RunLogDirFile(); e == nil {
if s, e := parsedConfig.RunLogDirFile(); e == nil {
log15.Root().SetHandler(
log15.LvlFilterHandler(log15.LvlInfo, log15.Must.FileHandler(s, log15.TerminalFormat())),
)
}

vnode, err := vite.New(globalConfig)
vnode, err := vite.New(parsedConfig)

if err != nil {
mainLog.Crit("Start vite failed.", "err", err)
}

rpc_vite.StartIpcRpc(vnode, globalConfig.DataDir)
rpc_vite.StartIpcRpc(vnode, parsedConfig.DataDir)
}
2 changes: 0 additions & 2 deletions cmd/rpc/main.go
Expand Up @@ -21,8 +21,6 @@ func main() {
}
}()

config.RecoverConfig()

if s, e := config.GlobalConfig.RunLogDirFile(); e == nil {
log15.Root().SetHandler(
log15.LvlFilterHandler(log15.LvlInfo, log15.Must.FileHandler(s, log15.TerminalFormat())),
Expand Down
12 changes: 6 additions & 6 deletions config/config.go
Expand Up @@ -11,8 +11,8 @@ import (
)

type Config struct {
P2P `json:"P2P"`
Miner `json:"Miner"`
*P2P `json:"P2P"`
*Miner `json:"Miner"`

// global keys
DataDir string `json:"DataDir"`
Expand All @@ -37,9 +37,9 @@ const configFileName = "vite.config.json"

var GlobalConfig *Config

func RecoverConfig() {
func defaultConfig() {
GlobalConfig = &Config{
P2P: P2P{
P2P: &P2P{
Name: "vite-server",
PrivateKey: "",
MaxPeers: 100,
Expand All @@ -50,7 +50,7 @@ func RecoverConfig() {
Datadir: common.DefaultDataDir(),
NetID: 4,
},
Miner: Miner{
Miner: &Miner{
Miner: false,
Coinbase: "",
MinerInterval: 6,
Expand All @@ -60,7 +60,7 @@ func RecoverConfig() {
}

func init() {
GlobalConfig = new(Config)
defaultConfig()

if text, err := ioutil.ReadFile(configFileName); err == nil {
err = json.Unmarshal(text, GlobalConfig)
Expand Down
42 changes: 21 additions & 21 deletions config/miner.go
Expand Up @@ -6,24 +6,24 @@ type Miner struct {
MinerInterval int `json:"MinerInterval"`
}

func MergeMinerConfig(cfg *Miner) Miner {
m := GlobalConfig.Miner

if cfg == nil {
return m
}

if cfg.Miner {
m.Miner = cfg.Miner
}

if cfg.Coinbase != "" {
m.Coinbase = cfg.Coinbase
}

if cfg.MinerInterval != 0 {
m.MinerInterval = cfg.MinerInterval
}

return m
}
//func MergeMinerConfig(cfg *Miner) *Miner {
// m := GlobalConfig.Miner
//
// if cfg == nil {
// return m
// }
//
// if cfg.Miner {
// m.Miner = cfg.Miner
// }
//
// if cfg.Coinbase != "" {
// m.Coinbase = cfg.Coinbase
// }
//
// if cfg.MinerInterval != 0 {
// m.MinerInterval = cfg.MinerInterval
// }
//
// return m
//}
94 changes: 47 additions & 47 deletions config/p2p.go
Expand Up @@ -7,11 +7,11 @@ type P2P struct {
PrivateKey string `json:"PrivateKey"`

// `MaxPeers` is the maximum number of peers that can be connected.
MaxPeers uint32 `json:"MaxPeers"`
MaxPeers uint `json:"MaxPeers"`

// `MaxPassivePeersRatio` is the ratio of MaxPeers that initiate an active connection to this node.
// the actual value is `MaxPeers / MaxPassivePeersRatio`
MaxPassivePeersRatio uint32 `json:"MaxPassivePeersRatio"`
MaxPassivePeersRatio uint `json:"MaxPassivePeersRatio"`

// `MaxPendingPeers` is the maximum number of peers that wait to connect.
MaxPendingPeers uint32 `json:"MaxPendingPeers"`
Expand All @@ -25,48 +25,48 @@ type P2P struct {
NetID uint `json:"NetID"`
}

func MergeP2PConfig(cfg *P2P) P2P {
p2p := GlobalConfig.P2P

if cfg == nil {
return p2p
}

if cfg.Name != "" {
p2p.Name = cfg.Name
}

if cfg.PrivateKey != "" {
p2p.PrivateKey = cfg.PrivateKey
}

if cfg.MaxPeers != 0 {
p2p.MaxPeers = cfg.MaxPeers
}

if cfg.MaxPassivePeersRatio != 0 {
p2p.MaxPassivePeersRatio = cfg.MaxPassivePeersRatio
}

if cfg.MaxPendingPeers != 0 {
p2p.MaxPendingPeers = cfg.MaxPendingPeers
}

if cfg.BootNodes != nil {
p2p.BootNodes = cfg.BootNodes
}

if cfg.Addr != "" {
p2p.Addr = cfg.Addr
}

if cfg.Datadir != "" {
p2p.Datadir = cfg.Datadir
}

if cfg.NetID != 0 {
p2p.NetID = cfg.NetID
}

return p2p
}
//func MergeP2PConfig(cfg *P2P) *P2P {
// p2p := GlobalConfig.P2P
//
// if cfg == nil {
// return p2p
// }
//
// if cfg.Name != "" {
// p2p.Name = cfg.Name
// }
//
// if cfg.PrivateKey != "" {
// p2p.PrivateKey = cfg.PrivateKey
// }
//
// if cfg.MaxPeers != 0 {
// p2p.MaxPeers = cfg.MaxPeers
// }
//
// if cfg.MaxPassivePeersRatio != 0 {
// p2p.MaxPassivePeersRatio = cfg.MaxPassivePeersRatio
// }
//
// if cfg.MaxPendingPeers != 0 {
// p2p.MaxPendingPeers = cfg.MaxPendingPeers
// }
//
// if cfg.BootNodes != nil {
// p2p.BootNodes = cfg.BootNodes
// }
//
// if cfg.Addr != "" {
// p2p.Addr = cfg.Addr
// }
//
// if cfg.Datadir != "" {
// p2p.Datadir = cfg.Datadir
// }
//
// if cfg.NetID != 0 {
// p2p.NetID = cfg.NetID
// }
//
// return p2p
//}
14 changes: 7 additions & 7 deletions p2p/server.go
Expand Up @@ -262,11 +262,11 @@ func (svr *Server) Available() bool {
return count > 0
}

func (svr *Server) MaxActivePeers() uint32 {
func (svr *Server) MaxActivePeers() uint {
return svr.MaxPeers - svr.MaxPassivePeers()
}

func (svr *Server) MaxPassivePeers() uint32 {
func (svr *Server) MaxPassivePeers() uint {
return svr.MaxPeers / svr.MaxPassivePeersRatio
}

Expand Down Expand Up @@ -424,8 +424,8 @@ func (svr *Server) SetupConn(conn net.Conn, flag connFlag) error {
return nil
}

func (svr *Server) CheckConn(peers map[NodeID]*Peer, c *TSConn, passivePeersCount uint32) error {
if uint32(len(peers)) >= svr.MaxPeers {
func (svr *Server) CheckConn(peers map[NodeID]*Peer, c *TSConn, passivePeersCount uint) error {
if uint(len(peers)) >= svr.MaxPeers {
return DiscTooManyPeers
}
if passivePeersCount >= svr.MaxPassivePeers() {
Expand Down Expand Up @@ -454,7 +454,7 @@ func (svr *Server) ScheduleTask() {
peers := make(map[NodeID]*Peer)
taskHasDone := make(chan Task, defaultMaxActiveDail)

var passivePeersCount uint32 = 0
var passivePeersCount uint = 0
var activeTasks []Task
var taskQueue []Task

Expand Down Expand Up @@ -654,7 +654,7 @@ func (t *waitTask) Perform(svr *Server) {

// @section DialManager
type DialManager struct {
maxDials uint32
maxDials uint
dialing map[NodeID]connFlag
start time.Time
bootNodes []*Node
Expand Down Expand Up @@ -776,7 +776,7 @@ func (dm *DialManager) checkDial(n *Node, peers map[NodeID]*Peer) error {
return nil
}

func NewDialManager(ntab *table, maxDials uint32, bootNodes []*Node) *DialManager {
func NewDialManager(ntab *table, maxDials uint, bootNodes []*Node) *DialManager {
return &DialManager{
maxDials: maxDials,
bootNodes: copyNodes(bootNodes), // dm will modify bootNodes
Expand Down
2 changes: 1 addition & 1 deletion vite/backend.go
Expand Up @@ -63,7 +63,7 @@ func New(cfg *config.Config) (*Vite, error) {
vite.pm = protocols.NewProtocolManager(vite)

var initP2pErr error
vite.p2p, initP2pErr = p2p.NewServer(&cfg.P2P, vite.pm.HandlePeer)
vite.p2p, initP2pErr = p2p.NewServer(cfg.P2P, vite.pm.HandlePeer)
if initP2pErr != nil {
log.Crit(initP2pErr.Error())
}
Expand Down

0 comments on commit 6de9535

Please sign in to comment.