From 3acd7a12de1fb460435699b8d6263cc5b1535061 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Mon, 4 Oct 2021 18:19:31 +0200 Subject: [PATCH] Add more flags --- command/server/chains/chain.go | 24 ++++++ command/server/chains/mainnet.go | 85 ++++++++++---------- command/server/chains/mumbai.go | 64 ++++++++------- command/server/command.go | 39 ++++++++- command/server/config.go | 133 ++++++++++++++++++++++--------- 5 files changed, 230 insertions(+), 115 deletions(-) create mode 100644 command/server/chains/chain.go diff --git a/command/server/chains/chain.go b/command/server/chains/chain.go new file mode 100644 index 0000000000..05c12ef0b0 --- /dev/null +++ b/command/server/chains/chain.go @@ -0,0 +1,24 @@ +package chains + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" +) + +type Chain struct { + Hash common.Hash + Genesis *core.Genesis + Bootnodes []string + NetworkId uint64 + DNS []string +} + +var chains = map[string]*Chain{ + "mainnet": mainnetBor, + "mumbai": mumbaiTestnet, +} + +func GetChain(name string) (*Chain, bool) { + chain, ok := chains[name] + return chain, ok +} diff --git a/command/server/chains/mainnet.go b/command/server/chains/mainnet.go index ad92979ff7..8d6f163256 100644 --- a/command/server/chains/mainnet.go +++ b/command/server/chains/mainnet.go @@ -8,48 +8,45 @@ import ( "github.com/ethereum/go-ethereum/params" ) -var BorMainnetGenesisHash = common.HexToHash("0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b") - -var BorMainnetChainConfig = ¶ms.ChainConfig{ - ChainID: big.NewInt(137), - HomesteadBlock: big.NewInt(0), - DAOForkBlock: nil, - DAOForkSupport: true, - EIP150Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(3395000), - MuirGlacierBlock: big.NewInt(3395000), - BerlinBlock: big.NewInt(14750000), - Bor: ¶ms.BorConfig{ - Period: 2, - ProducerDelay: 6, - Sprint: 64, - BackupMultiplier: 2, - ValidatorContract: "0x0000000000000000000000000000000000001000", - StateReceiverContract: "0x0000000000000000000000000000000000001001", - OverrideStateSyncRecords: map[string]int{ - "14949120": 8, - "14949184": 0, - "14953472": 0, - "14953536": 5, - "14953600": 0, - "14953664": 0, - "14953728": 0, - "14953792": 0, - "14953856": 0, +var mainnetBor = &Chain{ + Hash: common.HexToHash("0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b"), + NetworkId: 137, + Genesis: &core.Genesis{ + Config: ¶ms.ChainConfig{ + ChainID: big.NewInt(137), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: nil, + DAOForkSupport: true, + EIP150Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(3395000), + MuirGlacierBlock: big.NewInt(3395000), + BerlinBlock: big.NewInt(14750000), + Bor: ¶ms.BorConfig{ + Period: 2, + ProducerDelay: 6, + Sprint: 64, + BackupMultiplier: 2, + ValidatorContract: "0x0000000000000000000000000000000000001000", + StateReceiverContract: "0x0000000000000000000000000000000000001001", + OverrideStateSyncRecords: map[string]int{ + "14949120": 8, + "14949184": 0, + "14953472": 0, + "14953536": 5, + "14953600": 0, + "14953664": 0, + "14953728": 0, + "14953792": 0, + "14953856": 0, + }, + }, }, - }, -} - -//DefaultBorMainnet returns the Bor Mainnet network gensis block. -func DefaultBorMainnetGenesisBlock() *core.Genesis { - return &core.Genesis{ - Config: BorMainnetChainConfig, Nonce: 0, Timestamp: 1590824836, GasLimit: 10000000, @@ -57,5 +54,9 @@ func DefaultBorMainnetGenesisBlock() *core.Genesis { Mixhash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"), Alloc: readPrealloc("allocs/bor_mainnet.json"), - } + }, + Bootnodes: []string{ + "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303", + "enode://88116f4295f5a31538ae409e4d44ad40d22e44ee9342869e7d68bdec55b0f83c1530355ce8b41fbec0928a7d75a5745d528450d30aec92066ab6ba1ee351d710@159.203.9.164:30303", + }, } diff --git a/command/server/chains/mumbai.go b/command/server/chains/mumbai.go index 8a0af6dfdc..1003380d10 100644 --- a/command/server/chains/mumbai.go +++ b/command/server/chains/mumbai.go @@ -8,36 +8,34 @@ import ( "github.com/ethereum/go-ethereum/params" ) -var MumbaiGenesisHash = common.HexToHash("0x7b66506a9ebdbf30d32b43c5f15a3b1216269a1ec3a75aa3182b86176a2b1ca7") - -var MumbaiChainConfig = ¶ms.ChainConfig{ - ChainID: big.NewInt(80001), - HomesteadBlock: big.NewInt(0), - DAOForkBlock: nil, - DAOForkSupport: true, - EIP150Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(2722000), - MuirGlacierBlock: big.NewInt(2722000), - BerlinBlock: big.NewInt(13996000), - Bor: ¶ms.BorConfig{ - Period: 2, - ProducerDelay: 6, - Sprint: 64, - BackupMultiplier: 2, - ValidatorContract: "0x0000000000000000000000000000000000001000", - StateReceiverContract: "0x0000000000000000000000000000000000001001", - }, -} - -func DefaultMumbaiGenesisBlock() *core.Genesis { - return &core.Genesis{ - Config: MumbaiChainConfig, +var mumbaiTestnet = &Chain{ + Hash: common.HexToHash("0x7b66506a9ebdbf30d32b43c5f15a3b1216269a1ec3a75aa3182b86176a2b1ca7"), + NetworkId: 80001, + Genesis: &core.Genesis{ + Config: ¶ms.ChainConfig{ + ChainID: big.NewInt(80001), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: nil, + DAOForkSupport: true, + EIP150Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(2722000), + MuirGlacierBlock: big.NewInt(2722000), + BerlinBlock: big.NewInt(13996000), + Bor: ¶ms.BorConfig{ + Period: 2, + ProducerDelay: 6, + Sprint: 64, + BackupMultiplier: 2, + ValidatorContract: "0x0000000000000000000000000000000000001000", + StateReceiverContract: "0x0000000000000000000000000000000000001001", + }, + }, Nonce: 0, Timestamp: 1558348305, GasLimit: 10000000, @@ -45,5 +43,9 @@ func DefaultMumbaiGenesisBlock() *core.Genesis { Mixhash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"), Alloc: readPrealloc("allocs/mumbai.json"), - } + }, + Bootnodes: []string{ + "enode://320553cda00dfc003f499a3ce9598029f364fbb3ed1222fdc20a94d97dcc4d8ba0cd0bfa996579dcc6d17a534741fb0a5da303a90579431259150de66b597251@54.147.31.250:30303", + "enode://f0f48a8781629f95ff02606081e6e43e4aebd503f3d07fc931fad7dd5ca1ba52bd849a6f6c3be0e375cf13c9ae04d859c4a9ae3546dc8ed4f10aa5dbb47d4998@34.226.134.117:30303", + }, } diff --git a/command/server/command.go b/command/server/command.go index 985971e510..cd26e469ee 100644 --- a/command/server/command.go +++ b/command/server/command.go @@ -10,8 +10,9 @@ import ( "time" "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/eth/tracers" + "github.com/ethereum/go-ethereum/ethstats" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/node" "github.com/mattn/go-colorable" "github.com/mattn/go-isatty" @@ -66,7 +67,12 @@ func (c *Command) Run(args []string) int { // start the logger setupLogger(*config.LogLevel) - // create the node + // load the chain genesis + if err := config.loadChain(); err != nil { + panic(err) + } + + // create the node/stack nodeCfg, err := config.buildNode() if err != nil { panic(err) @@ -77,6 +83,14 @@ func (c *Command) Run(args []string) int { } c.node = stack + // TODO: MakeChain? + // TODO: Metrics + // TODO: Node key, pre generate one + // TODO: apis + // TODO: account management + // TODO: Graphql + // TODO: embed + // register the ethereum backend ethCfg, err := config.buildEth() if err != nil { @@ -86,7 +100,16 @@ func (c *Command) Run(args []string) int { if err != nil { panic(err) } - c.node.RegisterAPIs(tracers.APIs(backend.APIBackend)) + + // This is the tracers api, not sure if this should be here, i do not think so. + // c.node.RegisterAPIs(tracers.APIs(backend.APIBackend)) + + // register ethash service + if config.EthStats != nil { + if err := ethstats.New(stack, backend.APIBackend, backend.Engine(), *config.EthStats); err != nil { + panic(err) + } + } // start the node if err := c.node.Start(); err != nil { @@ -95,6 +118,16 @@ func (c *Command) Run(args []string) int { return c.handleSignals() } +func (c *Command) setupMetrics() { + if !metrics.Enabled { + // metrics are globally disabled + return + } + + // Start system runtime metrics collection + go metrics.CollectProcessMetrics(3 * time.Second) +} + func (c *Command) handleSignals() int { signalCh := make(chan os.Signal, 4) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) diff --git a/command/server/config.go b/command/server/config.go index 8977c6372b..45734d7316 100644 --- a/command/server/config.go +++ b/command/server/config.go @@ -5,11 +5,13 @@ import ( "strconv" "time" + "github.com/ethereum/go-ethereum/command/server/chains" "github.com/ethereum/go-ethereum/common/fdlimit" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/enode" "github.com/imdario/mergo" ) @@ -30,6 +32,8 @@ func durPtr(d time.Duration) *time.Duration { } type Config struct { + chain *chains.Chain + Chain *string Debug *bool LogLevel *string @@ -42,14 +46,21 @@ type Config struct { } type P2PConfig struct { - MaxPeers *uint64 - Bind *string - Port *uint64 - NoDiscover *bool - V5Disc *bool - Bootnodes []string - BootnodesV4 []string - BootnodesV5 []string + MaxPeers *uint64 + Bind *string + Port *uint64 + NoDiscover *bool + V5Disc *bool + Discovery *P2PDiscovery +} + +type P2PDiscovery struct { + Bootnodes []string + BootnodesV4 []string + BootnodesV5 []string + StaticNodes []string + TrustedNodes []string + DNS []string } type TxPoolConfig struct { @@ -79,14 +90,19 @@ func DefaultConfig() *Config { LogLevel: stringPtr("INFO"), DataDir: stringPtr(""), P2P: &P2PConfig{ - MaxPeers: uint64Ptr(30), - Bind: stringPtr("0.0.0.0"), - Port: uint64Ptr(30303), - NoDiscover: boolPtr(false), - V5Disc: boolPtr(false), - Bootnodes: []string{}, - BootnodesV4: []string{}, - BootnodesV5: []string{}, + MaxPeers: uint64Ptr(30), + Bind: stringPtr("0.0.0.0"), + Port: uint64Ptr(30303), + NoDiscover: boolPtr(false), + V5Disc: boolPtr(false), + Discovery: &P2PDiscovery{ + Bootnodes: []string{}, + BootnodesV4: []string{}, + BootnodesV5: []string{}, + StaticNodes: []string{}, + TrustedNodes: []string{}, + DNS: []string{}, + }, }, SyncMode: stringPtr("fast"), EthStats: stringPtr(""), @@ -113,14 +129,31 @@ func readConfigFile(path string) (*Config, error) { return nil, nil } +func (c *Config) loadChain() error { + chain, ok := chains.GetChain(*c.Chain) + if !ok { + return fmt.Errorf("chain '%s' not found", *c.Chain) + } + c.chain = chain + + // preload some default values that are on the chain file + if c.P2P.Discovery.Bootnodes == nil { + c.P2P.Discovery.Bootnodes = c.chain.Bootnodes + } + if c.P2P.Discovery.DNS == nil { + c.P2P.Discovery.DNS = c.chain.DNS + } + return nil +} + func (c *Config) buildEth() (*ethconfig.Config, error) { dbHandles, err := makeDatabaseHandles() if err != nil { return nil, err } n := ethconfig.Defaults - //n.NetworkId = c.genesis.NetworkId - //n.Genesis = c.genesis.Genesis + n.NetworkId = c.chain.NetworkId + n.Genesis = c.chain.Genesis // txpool options { @@ -143,6 +176,12 @@ func (c *Config) buildEth() (*ethconfig.Config, error) { fmt.Println(cfg) } + // discovery (this params should be in node.Config) + { + n.EthDiscoveryURLs = c.P2P.Discovery.DNS + n.SnapDiscoveryURLs = c.P2P.Discovery.DNS + } + var syncMode downloader.SyncMode switch *c.SyncMode { case "fast": @@ -152,22 +191,12 @@ func (c *Config) buildEth() (*ethconfig.Config, error) { } n.SyncMode = syncMode n.DatabaseHandles = dbHandles + return &n, nil } func (c *Config) buildNode() (*node.Config, error) { - /* - bootstrap, err := parseBootnodes(c.genesis.Bootstrap) - if err != nil { - return nil, err - } - static, err := parseBootnodes(c.genesis.Static) - if err != nil { - return nil, err - } - */ - - n := &node.Config{ + cfg := &node.Config{ Name: "reader", DataDir: *c.DataDir, P2P: p2p.Config{ @@ -182,16 +211,28 @@ func (c *Config) buildNode() (*node.Config, error) { WSPort: int(*c.Ports.Websocket), */ } - /* - if *c.NoDiscovery { - // avoid incoming connections - n.P2P.MaxPeers = 0 - // avoid outgoing connections - n.P2P.NoDiscovery = true - } - */ - return n, nil + // Discovery + var err error + if cfg.P2P.BootstrapNodes, err = parseBootnodes(c.P2P.Discovery.Bootnodes); err != nil { + return nil, err + } + if cfg.P2P.BootstrapNodesV5, err = parseBootnodes(c.P2P.Discovery.BootnodesV5); err != nil { + return nil, err + } + if cfg.P2P.StaticNodes, err = parseBootnodes(c.P2P.Discovery.StaticNodes); err != nil { + return nil, err + } + if cfg.P2P.TrustedNodes, err = parseBootnodes(c.P2P.Discovery.TrustedNodes); err != nil { + return nil, err + } + + if *c.P2P.NoDiscover { + // Disable networking, for now, we will not even allow incomming connections + cfg.P2P.MaxPeers = 0 + cfg.P2P.NoDiscovery = true + } + return cfg, nil } func (c *Config) Merge(cc ...*Config) error { @@ -214,3 +255,17 @@ func makeDatabaseHandles() (int, error) { } return int(raised / 2), nil } + +func parseBootnodes(urls []string) ([]*enode.Node, error) { + dst := []*enode.Node{} + for _, url := range urls { + if url != "" { + node, err := enode.Parse(enode.ValidSchemes, url) + if err != nil { + return nil, fmt.Errorf("invalid bootstrap url '%s': %v", url, err) + } + dst = append(dst, node) + } + } + return dst, nil +}