Skip to content

Commit

Permalink
cmd/geth,cmd/utils,params: Add random list setting for Bootnodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-wjhan committed Dec 21, 2023
1 parent da3cbaa commit 5a47b5b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ var (
utils.BlockTrailTime,
utils.PublicRequestsCacheLocation,
utils.MaxPublicRequests,
utils.BootnodeCount,
}
)

Expand Down
1 change: 1 addition & 0 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.BlockTrailTime,
utils.PublicRequestsCacheLocation,
utils.MaxPublicRequests,
utils.BootnodeCount,
},
},
{
Expand Down
35 changes: 33 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"math"
"math/big"
"math/rand"
"os"
"path/filepath"
godebug "runtime/debug"
Expand Down Expand Up @@ -926,6 +927,11 @@ var (
Usage: "Max # of concurrent public requests",
Value: params.MaxPublicRequests,
}
BootnodeCount = cli.IntFlag{
Name: "wemix.bootnodecount",
Usage: "Default bootnode peer count",
Value: params.BootnodeCount,
}
)

var (
Expand Down Expand Up @@ -1021,15 +1027,40 @@ func setNodeUserIdent(ctx *cli.Context, cfg *node.Config) {
}
}

// setRandomBootstrapNodes setting a random list of bootstrap nodes using the command line
func setRandomBootstrapNodes(ctx *cli.Context, bootnodes []string) []string {
rand.Seed(time.Now().UnixNano())
bootnodeslen := len(bootnodes)

// check command line
if ctx.GlobalIsSet(BootnodeCount.Name) {
setcount := ctx.GlobalInt(BootnodeCount.Name)
if setcount > 0 && setcount <= bootnodeslen {
params.BootnodeCount = setcount
}
}
// select random bootnodes
selectcount := params.BootnodeCount
urls := make([]string, selectcount)
tempnode := make([]string, bootnodeslen)
copy(tempnode, bootnodes)
for i := 0; i < selectcount; i++ {
index := rand.Intn(len(tempnode))
urls = append(urls, tempnode[index])
tempnode = append(tempnode[:index], tempnode[index+1:]...)
}
return urls
}

// setBootstrapNodes creates a list of bootstrap nodes from the command line
// flags, reverting to pre-configured ones if none have been specified.
func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls := params.WemixMainnetBootnodes
urls := setRandomBootstrapNodes(ctx, params.WemixMainnetBootnodes)
switch {
case ctx.GlobalIsSet(BootnodesFlag.Name):
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
case ctx.GlobalBool(WemixTestnetFlag.Name):
urls = params.WemixTestnetBootnodes
urls = setRandomBootstrapNodes(ctx, params.WemixTestnetBootnodes)
case ctx.GlobalBool(RopstenFlag.Name):
urls = params.RopstenBootnodes
case ctx.GlobalBool(SepoliaFlag.Name):
Expand Down
1 change: 1 addition & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,5 @@ var (

PublicRequestsCacheLocation string = "" // Cache DB location
MaxPublicRequests int64 = 100 // Max # of concurrent public requests
BootnodeCount int = 3 // Default bootnode peer count.
)

0 comments on commit 5a47b5b

Please sign in to comment.