cmd/utils: randomly enable light server by default

This commit is contained in:
Zsolt Felfoldi 2019-03-01 21:39:53 +01:00
parent 729bf365b5
commit 2e8626797d
5 changed files with 41 additions and 27 deletions

View file

@ -298,7 +298,7 @@ func accountCreate(ctx *cli.Context) error {
utils.Fatalf("%v", err) utils.Fatalf("%v", err)
} }
} }
utils.SetNodeConfig(ctx, &cfg.Node) utils.SetNodeConfig(ctx, &cfg.Node, nil)
scryptN, scryptP, keydir, err := cfg.Node.AccountConfig() scryptN, scryptP, keydir, err := cfg.Node.AccountConfig()
if err != nil { if err != nil {

View file

@ -125,8 +125,8 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
} }
// Apply flags. // Apply flags.
utils.SetULC(ctx, &cfg.Eth) utils.SetLesConfig(ctx, &cfg.Eth)
utils.SetNodeConfig(ctx, &cfg.Node) utils.SetNodeConfig(ctx, &cfg.Node, &cfg.Eth)
stack, err := node.New(&cfg.Node) stack, err := node.New(&cfg.Node)
if err != nil { if err != nil {
utils.Fatalf("Failed to create the protocol stack: %v", err) utils.Fatalf("Failed to create the protocol stack: %v", err)

View file

@ -149,7 +149,7 @@ func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
func getIPCEndpoint(ctx *cli.Context) string { func getIPCEndpoint(ctx *cli.Context) string {
cfg := defaultNodeConfig cfg := defaultNodeConfig
utils.SetNodeConfig(ctx, &cfg) utils.SetNodeConfig(ctx, &cfg, nil)
endpoint := cfg.IPCEndpoint() endpoint := cfg.IPCEndpoint()

View file

@ -285,7 +285,7 @@ func bzzd(ctx *cli.Context) error {
//optionally set the bootnodes before configuring the node //optionally set the bootnodes before configuring the node
setSwarmBootstrapNodes(ctx, &cfg) setSwarmBootstrapNodes(ctx, &cfg)
//setup the ethereum node //setup the ethereum node
utils.SetNodeConfig(ctx, &cfg) utils.SetNodeConfig(ctx, &cfg, nil)
//disable dynamic dialing from p2p/discovery //disable dynamic dialing from p2p/discovery
cfg.P2P.NoDial = true cfg.P2P.NoDial = true
@ -377,7 +377,7 @@ func getPrivKey(ctx *cli.Context) *ecdsa.PrivateKey {
if _, err := os.Stat(bzzconfig.Path); err == nil { if _, err := os.Stat(bzzconfig.Path); err == nil {
cfg.DataDir = bzzconfig.Path cfg.DataDir = bzzconfig.Path
} }
utils.SetNodeConfig(ctx, &cfg) utils.SetNodeConfig(ctx, &cfg, nil)
stack, err := node.New(&cfg) stack, err := node.New(&cfg)
if err != nil { if err != nil {
utils.Fatalf("can't create node: %v", err) utils.Fatalf("can't create node: %v", err)

View file

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -919,8 +920,32 @@ func setIPC(ctx *cli.Context, cfg *node.Config) {
} }
} }
// SetULC setup ULC config from file if given. // SetLesConfig processes LES server/client/ultralight client parameters
func SetULC(ctx *cli.Context, cfg *eth.Config) { func SetLesConfig(ctx *cli.Context, cfg *eth.Config) {
if ctx.GlobalIsSet(SyncModeFlag.Name) {
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
}
if cfg.SyncMode != downloader.LightSync {
if ctx.GlobalIsSet(LightServFlag.Name) {
cfg.LightServ = ctx.GlobalInt(LightServFlag.Name)
} else if !ctx.GlobalBool(MiningEnabledFlag.Name) {
// enable light server with a given chance
rand.Seed(time.Now().UnixNano())
if rand.Intn(100) < 20 {
log.Warn("LES server mode enabled (use --lightserv option to explicitly enable or disable)")
cfg.LightServ = 50
}
}
}
cfg.LightBandwidthIn = ctx.GlobalInt(LightBandwidthInFlag.Name)
cfg.LightBandwidthOut = ctx.GlobalInt(LightBandwidthOutFlag.Name)
if ctx.GlobalIsSet(LightPeersFlag.Name) {
cfg.LightPeers = ctx.GlobalInt(LightPeersFlag.Name)
}
if ctx.GlobalIsSet(OnlyAnnounceModeFlag.Name) {
cfg.OnlyAnnounce = ctx.GlobalBool(OnlyAnnounceModeFlag.Name)
}
// ULC config isn't loaded from global config and ULC config and ULC trusted nodes are not defined. // ULC config isn't loaded from global config and ULC config and ULC trusted nodes are not defined.
if cfg.ULC == nil && !(ctx.GlobalIsSet(ULCModeConfigFlag.Name) || ctx.GlobalIsSet(ULCTrustedNodesFlag.Name)) { if cfg.ULC == nil && !(ctx.GlobalIsSet(ULCModeConfigFlag.Name) || ctx.GlobalIsSet(ULCTrustedNodesFlag.Name)) {
return return
@ -1035,15 +1060,18 @@ func MakePasswordList(ctx *cli.Context) []string {
return lines return lines
} }
func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config, ethCfg *eth.Config) {
setNodeKey(ctx, cfg) setNodeKey(ctx, cfg)
setNAT(ctx, cfg) setNAT(ctx, cfg)
setListenAddress(ctx, cfg) setListenAddress(ctx, cfg)
setBootstrapNodes(ctx, cfg) setBootstrapNodes(ctx, cfg)
setBootstrapNodesV5(ctx, cfg) setBootstrapNodesV5(ctx, cfg)
lightClient := ctx.GlobalString(SyncModeFlag.Name) == "light" var lightClient, lightServer bool
lightServer := ctx.GlobalInt(LightServFlag.Name) != 0 if ethCfg != nil {
lightClient = ethCfg.SyncMode == downloader.LightSync
lightServer = ethCfg.LightServ != 0
}
lightPeers := ctx.GlobalInt(LightPeersFlag.Name) lightPeers := ctx.GlobalInt(LightPeersFlag.Name)
if ctx.GlobalIsSet(MaxPeersFlag.Name) { if ctx.GlobalIsSet(MaxPeersFlag.Name) {
@ -1103,8 +1131,8 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
} }
// SetNodeConfig applies node-related command line flags to the config. // SetNodeConfig applies node-related command line flags to the config.
func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { func SetNodeConfig(ctx *cli.Context, cfg *node.Config, ethCfg *eth.Config) {
SetP2PConfig(ctx, &cfg.P2P) SetP2PConfig(ctx, &cfg.P2P, ethCfg)
setIPC(ctx, cfg) setIPC(ctx, cfg)
setHTTP(ctx, cfg) setHTTP(ctx, cfg)
setGraphQL(ctx, cfg) setGraphQL(ctx, cfg)
@ -1309,20 +1337,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
setEthash(ctx, cfg) setEthash(ctx, cfg)
setWhitelist(ctx, cfg) setWhitelist(ctx, cfg)
if ctx.GlobalIsSet(SyncModeFlag.Name) {
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
}
if ctx.GlobalIsSet(LightServFlag.Name) {
cfg.LightServ = ctx.GlobalInt(LightServFlag.Name)
}
cfg.LightBandwidthIn = ctx.GlobalInt(LightBandwidthInFlag.Name)
cfg.LightBandwidthOut = ctx.GlobalInt(LightBandwidthOutFlag.Name)
if ctx.GlobalIsSet(LightPeersFlag.Name) {
cfg.LightPeers = ctx.GlobalInt(LightPeersFlag.Name)
}
if ctx.GlobalIsSet(OnlyAnnounceModeFlag.Name) {
cfg.OnlyAnnounce = ctx.GlobalBool(OnlyAnnounceModeFlag.Name)
}
if ctx.GlobalIsSet(NetworkIdFlag.Name) { if ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name) cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name)
} }