Skip to content

Commit

Permalink
Merge pull request #41 from bladehan1/network-flags
Browse files Browse the repository at this point in the history
feat(cmd): add startup flags and embed genesis file
  • Loading branch information
yuekun0707 authored Mar 22, 2023
2 parents 4c4fb1f + bd56691 commit 9afd64a
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 6 deletions.
65 changes: 65 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"
"os"
"reflect"
"time"
"unicode"

"gopkg.in/urfave/cli.v1"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/usbwallet"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/eth/catalyst"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -133,6 +135,14 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
}
}

if ctx.GlobalIsSet(utils.BttcDonauFlag.Name) {
setDefaultBttcDonauGethConfig(ctx, &cfg)
}

if ctx.GlobalIsSet(utils.BttcMainnetFlag.Name) {
setDefaultBttcMainnetGethConfig(ctx, &cfg)
}

// Apply flags.
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := node.New(&cfg.Node)
Expand Down Expand Up @@ -328,3 +338,58 @@ func setAccountManagerBackends(stack *node.Node) error {

return nil
}

func setDefaultBttcDonauGethConfig(ctx *cli.Context, config *gethConfig) {
config.Node.P2P.ListenAddr = fmt.Sprintf(":%d", 30303)
config.Node.HTTPHost = "0.0.0.0"
config.Node.HTTPVirtualHosts = []string{"*"}
config.Node.HTTPCors = []string{"*"}
config.Node.HTTPPort = 8545
config.Node.IPCPath = utils.MakeDataDir(ctx) + "/bttc.ipc"
config.Node.HTTPModules = []string{"eth", "net", "web3", "txpool", "bor"}
config.Eth.SyncMode = downloader.FullSync
config.Eth.NetworkId = 1029
config.Node.AllowUnprotectedTxs = true
config.Eth.RPCTxFeeCap = 0
config.Eth.Miner.GasCeil = 20000000
config.Eth.Miner.GasPrice = big.NewInt(9000000000000000)
//--miner.gastarget is depreceated, No longed used
config.Eth.GPO.MaxPrice = big.NewInt(15000000000000000)
config.Eth.TxPool.PriceLimit = 9000000000000000
config.Eth.TxPool.NoLocals = true
config.Eth.TxPool.AccountSlots = 16
config.Eth.TxPool.GlobalSlots = 131072
config.Eth.TxPool.AccountQueue = 64
config.Eth.TxPool.GlobalQueue = 131072
config.Eth.TxPool.Lifetime = 90 * time.Minute
config.Node.P2P.MaxPeers = 200
config.Metrics.Enabled = true
// --pprof is enabled in 'internal/debug/flags.go'
}

func setDefaultBttcMainnetGethConfig(ctx *cli.Context, config *gethConfig) {
config.Node.P2P.ListenAddr = fmt.Sprintf(":%d", 30303)
config.Node.HTTPHost = "0.0.0.0"
config.Node.HTTPVirtualHosts = []string{"*"}
config.Node.HTTPCors = []string{"*"}
config.Node.HTTPPort = 8545
config.Node.IPCPath = utils.MakeDataDir(ctx) + "/bttc.ipc"
config.Node.HTTPModules = []string{"eth", "net", "web3", "txpool", "bor"}
config.Eth.SyncMode = downloader.FullSync
config.Eth.NetworkId = 199
config.Node.AllowUnprotectedTxs = true
config.Eth.RPCTxFeeCap = 0
config.Eth.Miner.GasCeil = 20000000
config.Eth.Miner.GasPrice = big.NewInt(300000000000000)
//--miner.gastarget is depreceated, No longed used
config.Eth.GPO.MaxPrice = big.NewInt(500000000000000)
config.Eth.TxPool.NoLocals = true
config.Eth.TxPool.AccountSlots = 16
config.Eth.TxPool.GlobalSlots = 131072
config.Eth.TxPool.AccountQueue = 64
config.Eth.TxPool.GlobalQueue = 131072
config.Eth.TxPool.Lifetime = 90 * time.Minute
config.Node.P2P.MaxPeers = 200
config.Metrics.Enabled = true
// --pprof is enabled in 'internal/debug/flags.go'
}
8 changes: 8 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ var (
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
utils.BttcDonauFlag,
utils.BttcMainnetFlag,
utils.VMEnableDebugFlag,
utils.NetworkIdFlag,
utils.EthStatsURLFlag,
Expand Down Expand Up @@ -282,6 +284,12 @@ func prepare(ctx *cli.Context) {
case ctx.GlobalIsSet(utils.GoerliFlag.Name):
log.Info("Starting Geth on Görli testnet...")

case ctx.GlobalIsSet(utils.BttcDonauFlag.Name):
log.Info("Starting bttc on bttc donau testnet...")

case ctx.GlobalIsSet(utils.BttcMainnetFlag.Name):
log.Info("Starting bttc on bttc mainnet...")

case ctx.GlobalIsSet(utils.DeveloperFlag.Name):
log.Info("Starting Geth in ephemeral dev mode...")

Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.NetworkIdFlag,
utils.MainnetFlag,
utils.GoerliFlag,
utils.BttcDonauFlag,
utils.BttcMainnetFlag,
utils.RinkebyFlag,
utils.RopstenFlag,
utils.SyncModeFlag,
Expand Down
44 changes: 42 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ var (
Name: "ropsten",
Usage: "Ropsten network: pre-configured proof-of-work test network",
}
BttcDonauFlag = cli.BoolFlag{
Name: "bttc-donau",
Usage: "Bttc Donau network: pre-configured proof-of-stake test network",
}
BttcMainnetFlag = cli.BoolFlag{
Name: "bttc-mainnet",
Usage: "Bttc mainnet",
}
DeveloperFlag = cli.BoolFlag{
Name: "dev",
Usage: "Ephemeral proof-of-authority network with a pre-funded developer account, mining enabled",
Expand Down Expand Up @@ -797,6 +805,12 @@ func MakeDataDir(ctx *cli.Context) string {
if ctx.GlobalBool(GoerliFlag.Name) {
return filepath.Join(path, "goerli")
}
if (ctx.GlobalBool(BttcDonauFlag.Name) || ctx.GlobalBool(BttcMainnetFlag.Name)) &&
path == node.DefaultDataDir() {
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, "/.bttc/data")
}

return path
}
Fatalf("Cannot determine default data directory, please set manually (--datadir)")
Expand Down Expand Up @@ -849,6 +863,10 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls = params.RinkebyBootnodes
case ctx.GlobalBool(GoerliFlag.Name):
urls = params.GoerliBootnodes
case ctx.GlobalBool(BttcDonauFlag.Name):
urls = params.BttcDonauBootnodes
case ctx.GlobalBool(BttcMainnetFlag.Name):
urls = params.BttcMainnetBootnodes
case cfg.BootstrapNodes != nil:
return // already set, don't apply defaults.
}
Expand Down Expand Up @@ -1292,6 +1310,12 @@ func setDataDir(ctx *cli.Context, cfg *node.Config) {
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "rinkeby")
case ctx.GlobalBool(GoerliFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "goerli")
case ctx.GlobalBool(BttcDonauFlag.Name) && cfg.DataDir == node.DefaultDataDir():
homeDir, _ := os.UserHomeDir()
cfg.DataDir = filepath.Join(homeDir, "/.bttc/data")
case ctx.GlobalBool(BttcMainnetFlag.Name) && cfg.DataDir == node.DefaultDataDir():
homeDir, _ := os.UserHomeDir()
cfg.DataDir = filepath.Join(homeDir, "/.bttc/data")
}
}

Expand Down Expand Up @@ -1477,7 +1501,7 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
// SetEthConfig applies eth-related command line flags to the config.
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// Avoid conflicting network flags
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, RopstenFlag, RinkebyFlag, GoerliFlag)
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, RopstenFlag, RinkebyFlag, GoerliFlag, BttcDonauFlag, BttcMainnetFlag)
CheckExclusive(ctx, LightServeFlag, SyncModeFlag, "light")
CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
if ctx.GlobalString(GCModeFlag.Name) == "archive" && ctx.GlobalUint64(TxLookupLimitFlag.Name) != 0 {
Expand All @@ -1503,7 +1527,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.BorLogs = ctx.GlobalBool(BorLogsFlag.Name)
}

// Cap the cache allowance and tune the garbage collector
// Cap the cache allowance and tune the garbage collector
mem, err := gopsutil.VirtualMemory()
if err == nil {
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
Expand Down Expand Up @@ -1634,6 +1658,18 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
cfg.Genesis = core.DefaultGoerliGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.GoerliGenesisHash)
case ctx.GlobalBool(BttcDonauFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1029
}
cfg.Genesis = core.DefaultBttcDonauGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.BttcDonauGenesisHash)
case ctx.GlobalBool(BttcMainnetFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 199
}
cfg.Genesis = core.DefaultBttcMainnetGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.BttcMainnetGenesisHash)
case ctx.GlobalBool(DeveloperFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1337
Expand Down Expand Up @@ -1854,6 +1890,10 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
genesis = core.DefaultRinkebyGenesisBlock()
case ctx.GlobalBool(GoerliFlag.Name):
genesis = core.DefaultGoerliGenesisBlock()
case ctx.GlobalBool(BttcDonauFlag.Name):
genesis = core.DefaultBttcDonauGenesisBlock()
case ctx.GlobalBool(BttcMainnetFlag.Name):
genesis = core.DefaultBttcMainnetGenesisBlock()
case ctx.GlobalBool(DeveloperFlag.Name):
Fatalf("Developer chains are ephemeral")
}
Expand Down
35 changes: 35 additions & 0 deletions core/allocs/bttc-donau.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions core/allocs/bttc-mainnet.json

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"bytes"
"embed"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -42,6 +43,9 @@ import (
//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
//go:generate gencodec -type GenesisAccount -field-override genesisAccountMarshaling -out gen_genesis_account.go

//go:embed allocs
var allocs embed.FS

var errGenesisNoConfig = errors.New("genesis has no chain configuration")

// Genesis specifies the header fields, state of a genesis block. It also defines hard
Expand Down Expand Up @@ -248,6 +252,10 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.RinkebyChainConfig
case ghash == params.GoerliGenesisHash:
return params.GoerliChainConfig
case ghash == params.BttcDonauGenesisHash:
return params.BttcDonauChainConfig
case ghash == params.BttcMainnetGenesisHash:
return params.BttcMainnetChainConfig
default:
return params.AllEthashProtocolChanges
}
Expand Down Expand Up @@ -397,6 +405,40 @@ func DefaultGoerliGenesisBlock() *Genesis {
}
}

// DefaultBttcDonauGenesisBlock returns the Bttc Donau network genesis block.
func DefaultBttcDonauGenesisBlock() *Genesis {
return &Genesis{
Config: params.BttcDonauChainConfig,
Nonce: 0x0,
Timestamp: 0x5ce28211,
GasLimit: 0x989680,
Difficulty: big.NewInt(1),
Mixhash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Alloc: readPrealloc("allocs/bttc-donau.json"),
Number: 0x0,
GasUsed: 0x0,
ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
}
}

//DefaultBttcMainnet returns the Bor Mainnet network gensis block.
func DefaultBttcMainnetGenesisBlock() *Genesis {
return &Genesis{
Config: params.BttcMainnetChainConfig,
Nonce: 0x0,
Timestamp: 0x5ce28211,
GasLimit: 0x989680,
Difficulty: big.NewInt(1),
Mixhash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Alloc: readPrealloc("allocs/bttc-mainnet.json"),
Number: 0x0,
GasUsed: 0x0,
ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
}
}

// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
// Override the default period to the user requested one
Expand Down Expand Up @@ -439,3 +481,18 @@ func decodePrealloc(data string) GenesisAlloc {
}
return ga
}

func readPrealloc(filename string) GenesisAlloc {
f, err := allocs.Open(filename)
if err != nil {
panic(fmt.Sprintf("Could not open genesis preallocation for %s: %v", filename, err))
}
defer f.Close()
decoder := json.NewDecoder(f)
ga := make(GenesisAlloc)
err = decoder.Decode(&ga)
if err != nil {
panic(fmt.Sprintf("Could not parse genesis preallocation for %s: %v", filename, err))
}
return ga
}
6 changes: 6 additions & 0 deletions internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ func Setup(ctx *cli.Context) error {
// This context value ("metrics.addr") represents the utils.MetricsHTTPFlag.Name.
// It cannot be imported because it will cause a cyclical dependency.
StartPProf(address, !ctx.GlobalIsSet("metrics.addr"))
} else if ctx.GlobalIsSet("bttc-donau") {
address := fmt.Sprintf("%s:%d", "0.0.0.0", 7071)
StartPProf(address, !ctx.GlobalIsSet("metrics.addr"))
} else if ctx.GlobalIsSet("bttc-mainnet") {
address := fmt.Sprintf("%s:%d", "0.0.0.0", 7071)
StartPProf(address, !ctx.GlobalIsSet("metrics.addr"))
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions params/bootnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ var GoerliBootnodes = []string{
"enode://a59e33ccd2b3e52d578f1fbd70c6f9babda2650f0760d6ff3b37742fdcdfdb3defba5d56d315b40c46b70198c7621e63ffa3f987389c7118634b0fefbbdfa7fd@51.15.119.157:40303",
}

// BttcDonauBootnodes are the enode URLs of the P2P bootstrap nodes running on the
// Bttc Donau test network.
var BttcDonauBootnodes = []string{
"enode://2e6a732ba9d0fcf102a4f4bda7d76f28095c9f03ee56bc89dc5c2235cd527c914b6063b0c76598cc37287f0594ae4022df550c592b3ba2a56a9f02810edbeee1@52.53.72.234:30303",
"enode://3d7da6d583072fbbe733135047010698e8b6a24c9315ce953b09dddbfabb2476c8b720b2ff2beb2ec73ef111b691c7dcd87f5e42bcba4a7bc385b7f728b2ab44@54.176.105.93:30303",
}

// BttcMainnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the
// Bttc main network.
var BttcMainnetBootnodes = []string{
"enode://8ef920be1d44ad7c41a517a6420e43511f2e30d1c35a4bb05954c9f413b1712dae6e9e05f56595966470506891ff05d203e233c2e8f6df8c72621537a3d783e9@54.157.35.210:30303",
"enode://f3a2534ac30db7387f84c1262bce9a0737c46a8b5627f8193d412a4bde415c191191bbf984f51e04e5d974e62b70fab148f38522c5e2917ca1f1860361f14cc9@107.20.250.182:30303",
"enode://268cc5c4062b4c30f7ae972322ec119465655d9b3f7220df4614f2890b5cef6ac350d65890f8ecebfe6c5ce0af635f7ae420db84de7677c54b35ed1ce6bb4fbd@54.219.27.155:30303",
"enode://a9aa7a7ec5b34485c73436d311d86c55f900db4008058231a2fd2fb8ee7ad1b68d7d5a64acbf1f62b8d5f25388b492d16befb686d6146b374a85a6ea7d5a95c9@54.241.235.101:30303",
}

var V5Bootnodes = []string{
// Teku team's bootnode
"enr:-KG4QOtcP9X1FbIMOe17QNMKqDxCpm14jcX5tiOE4_TyMrFqbmhPZHK_ZPG2Gxb1GE2xdtodOfx9-cgvNtxnRyHEmC0ghGV0aDKQ9aX9QgAAAAD__________4JpZIJ2NIJpcIQDE8KdiXNlY3AyNTZrMaEDhpehBDbZjM_L9ek699Y7vhUJ-eAdMyQW_Fil522Y0fODdGNwgiMog3VkcIIjKA",
Expand Down
Loading

0 comments on commit 9afd64a

Please sign in to comment.