Skip to content

Commit 38d1b0c

Browse files
authored
cmd/geth: enable DNS discovery by default (#20660)
* node: expose config in service context * eth: integrate p2p/dnsdisc * cmd/geth: add some DNS flags * eth: remove DNS URLs * cmd/utils: configure DNS names for testnets * params: update DNS URLs * cmd/geth: configure mainnet DNS * cmd/utils: rename DNS flag and fix flag processing * cmd/utils: remove debug print * node: fix test
1 parent eddcecc commit 38d1b0c

File tree

11 files changed

+104
-19
lines changed

11 files changed

+104
-19
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var (
131131
utils.NetrestrictFlag,
132132
utils.NodeKeyFileFlag,
133133
utils.NodeKeyHexFlag,
134+
utils.DNSDiscoveryFlag,
134135
utils.DeveloperFlag,
135136
utils.DeveloperPeriodFlag,
136137
utils.TestnetFlag,

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ var AppHelpFlagGroups = []flagGroup{
182182
utils.BootnodesFlag,
183183
utils.BootnodesV4Flag,
184184
utils.BootnodesV5Flag,
185+
utils.DNSDiscoveryFlag,
185186
utils.ListenPortFlag,
186187
utils.MaxPeersFlag,
187188
utils.MaxPendingPeersFlag,

cmd/utils/flags.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ var (
658658
Name: "netrestrict",
659659
Usage: "Restricts network communication to the given IP networks (CIDR masks)",
660660
}
661+
DNSDiscoveryFlag = cli.StringFlag{
662+
Name: "discovery.dns",
663+
Usage: "Sets DNS discovery entry points (use \"\" to disable DNS)",
664+
}
661665

662666
// ATM the url is left to the user and deployment to
663667
JSpathFlag = cli.StringFlag{
@@ -811,9 +815,9 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
811815
switch {
812816
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(BootnodesV4Flag.Name):
813817
if ctx.GlobalIsSet(BootnodesV4Flag.Name) {
814-
urls = strings.Split(ctx.GlobalString(BootnodesV4Flag.Name), ",")
818+
urls = splitAndTrim(ctx.GlobalString(BootnodesV4Flag.Name))
815819
} else {
816-
urls = strings.Split(ctx.GlobalString(BootnodesFlag.Name), ",")
820+
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
817821
}
818822
case ctx.GlobalBool(TestnetFlag.Name):
819823
urls = params.TestnetBootnodes
@@ -845,9 +849,9 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
845849
switch {
846850
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(BootnodesV5Flag.Name):
847851
if ctx.GlobalIsSet(BootnodesV5Flag.Name) {
848-
urls = strings.Split(ctx.GlobalString(BootnodesV5Flag.Name), ",")
852+
urls = splitAndTrim(ctx.GlobalString(BootnodesV5Flag.Name))
849853
} else {
850-
urls = strings.Split(ctx.GlobalString(BootnodesFlag.Name), ",")
854+
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
851855
}
852856
case ctx.GlobalBool(RinkebyFlag.Name):
853857
urls = params.RinkebyBootnodes
@@ -1477,6 +1481,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14771481
if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
14781482
cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
14791483
}
1484+
if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
1485+
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
1486+
if urls == "" {
1487+
cfg.DiscoveryURLs = []string{}
1488+
} else {
1489+
cfg.DiscoveryURLs = splitAndTrim(urls)
1490+
}
1491+
}
14801492

14811493
// Override any default configs for hard coded networks.
14821494
switch {
@@ -1485,16 +1497,19 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14851497
cfg.NetworkId = 3
14861498
}
14871499
cfg.Genesis = core.DefaultTestnetGenesisBlock()
1500+
setDNSDiscoveryDefaults(cfg, params.KnownDNSNetworks[params.TestnetGenesisHash])
14881501
case ctx.GlobalBool(RinkebyFlag.Name):
14891502
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
14901503
cfg.NetworkId = 4
14911504
}
14921505
cfg.Genesis = core.DefaultRinkebyGenesisBlock()
1506+
setDNSDiscoveryDefaults(cfg, params.KnownDNSNetworks[params.RinkebyGenesisHash])
14931507
case ctx.GlobalBool(GoerliFlag.Name):
14941508
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
14951509
cfg.NetworkId = 5
14961510
}
14971511
cfg.Genesis = core.DefaultGoerliGenesisBlock()
1512+
setDNSDiscoveryDefaults(cfg, params.KnownDNSNetworks[params.GoerliGenesisHash])
14981513
case ctx.GlobalBool(DeveloperFlag.Name):
14991514
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
15001515
cfg.NetworkId = 1337
@@ -1521,7 +1536,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15211536
if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
15221537
cfg.Miner.GasPrice = big.NewInt(1)
15231538
}
1539+
default:
1540+
if cfg.NetworkId == 1 {
1541+
setDNSDiscoveryDefaults(cfg, params.KnownDNSNetworks[params.MainnetGenesisHash])
1542+
}
1543+
}
1544+
}
1545+
1546+
// setDNSDiscoveryDefaults configures DNS discovery with the given URL if
1547+
// no URLs are set.
1548+
func setDNSDiscoveryDefaults(cfg *eth.Config, url string) {
1549+
if cfg.DiscoveryURLs != nil {
1550+
return
15241551
}
1552+
cfg.DiscoveryURLs = []string{url}
15251553
}
15261554

15271555
// RegisterEthService adds an Ethereum client to the stack.

eth/backend.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
"github.com/ethereum/go-ethereum/miner"
4848
"github.com/ethereum/go-ethereum/node"
4949
"github.com/ethereum/go-ethereum/p2p"
50+
"github.com/ethereum/go-ethereum/p2p/enode"
5051
"github.com/ethereum/go-ethereum/p2p/enr"
5152
"github.com/ethereum/go-ethereum/params"
5253
"github.com/ethereum/go-ethereum/rlp"
@@ -74,6 +75,7 @@ type Ethereum struct {
7475
blockchain *core.BlockChain
7576
protocolManager *ProtocolManager
7677
lesServer LesServer
78+
dialCandiates enode.Iterator
7779

7880
// DB interfaces
7981
chainDb ethdb.Database // Block chain database
@@ -220,6 +222,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
220222
}
221223
eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
222224

225+
eth.dialCandiates, err = eth.setupDiscovery(&ctx.Config.P2P)
226+
if err != nil {
227+
return nil, err
228+
}
229+
223230
return eth, nil
224231
}
225232

@@ -510,6 +517,7 @@ func (s *Ethereum) Protocols() []p2p.Protocol {
510517
for i, vsn := range ProtocolVersions {
511518
protos[i] = s.protocolManager.makeProtocol(vsn)
512519
protos[i].Attributes = []enr.Entry{s.currentEthEntry()}
520+
protos[i].DialCandidates = s.dialCandiates
513521
}
514522
if s.lesServer != nil {
515523
protos = append(protos, s.lesServer.Protocols()...)

eth/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ type Config struct {
9595
NetworkId uint64 // Network ID to use for selecting peers to connect to
9696
SyncMode downloader.SyncMode
9797

98+
// This can be set to list of enrtree:// URLs which will be queried for
99+
// for nodes to connect to.
100+
DiscoveryURLs []string
101+
98102
NoPruning bool // Whether to disable pruning and flush everything to disk
99103
NoPrefetch bool // Whether to disable prefetching and only load state on demand
100104

@@ -156,8 +160,8 @@ type Config struct {
156160
CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
157161

158162
// Istanbul block override (TODO: remove after the fork)
159-
OverrideIstanbul *big.Int
163+
OverrideIstanbul *big.Int `toml:",omitempty"`
160164

161165
// MuirGlacier block override (TODO: remove after the fork)
162-
OverrideMuirGlacier *big.Int
166+
OverrideMuirGlacier *big.Int `toml:",omitempty"`
163167
}

eth/enr_entry.go renamed to eth/discovery.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package eth
1919
import (
2020
"github.com/ethereum/go-ethereum/core"
2121
"github.com/ethereum/go-ethereum/core/forkid"
22+
"github.com/ethereum/go-ethereum/p2p"
23+
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
2224
"github.com/ethereum/go-ethereum/p2p/enode"
2325
"github.com/ethereum/go-ethereum/rlp"
2426
)
@@ -37,6 +39,7 @@ func (e ethEntry) ENRKey() string {
3739
return "eth"
3840
}
3941

42+
// startEthEntryUpdate starts the ENR updater loop.
4043
func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) {
4144
var newHead = make(chan core.ChainHeadEvent, 10)
4245
sub := eth.blockchain.SubscribeChainHeadEvent(newHead)
@@ -59,3 +62,12 @@ func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) {
5962
func (eth *Ethereum) currentEthEntry() *ethEntry {
6063
return &ethEntry{ForkID: forkid.NewID(eth.blockchain)}
6164
}
65+
66+
// setupDiscovery creates the node discovery source for the eth protocol.
67+
func (eth *Ethereum) setupDiscovery(cfg *p2p.Config) (enode.Iterator, error) {
68+
if cfg.NoDiscovery || len(eth.config.DiscoveryURLs) == 0 {
69+
return nil, nil
70+
}
71+
client := dnsdisc.NewClient(dnsdisc.Config{})
72+
return client.NewIterator(eth.config.DiscoveryURLs...)
73+
}

eth/gen_config.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (n *Node) Start() error {
194194
for _, constructor := range n.serviceFuncs {
195195
// Create a new context for the particular service
196196
ctx := &ServiceContext{
197-
config: n.config,
197+
Config: *n.config,
198198
services: make(map[reflect.Type]Service),
199199
EventMux: n.eventmux,
200200
AccountManager: n.accman,

node/service.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ import (
3232
// the protocol stack, that is passed to all constructors to be optionally used;
3333
// as well as utility methods to operate on the service environment.
3434
type ServiceContext struct {
35-
config *Config
3635
services map[reflect.Type]Service // Index of the already constructed services
37-
EventMux *event.TypeMux // Event multiplexer used for decoupled notifications
38-
AccountManager *accounts.Manager // Account manager created by the node.
36+
Config Config
37+
EventMux *event.TypeMux // Event multiplexer used for decoupled notifications
38+
AccountManager *accounts.Manager // Account manager created by the node.
3939
}
4040

4141
// OpenDatabase opens an existing database with the given name (or creates one
4242
// if no previous can be found) from within the node's data directory. If the
4343
// node is an ephemeral one, a memory database is returned.
4444
func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, namespace string) (ethdb.Database, error) {
45-
if ctx.config.DataDir == "" {
45+
if ctx.Config.DataDir == "" {
4646
return rawdb.NewMemoryDatabase(), nil
4747
}
48-
return rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace)
48+
return rawdb.NewLevelDBDatabase(ctx.Config.ResolvePath(name), cache, handles, namespace)
4949
}
5050

5151
// OpenDatabaseWithFreezer opens an existing database with the given name (or
@@ -54,16 +54,16 @@ func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, nam
5454
// database to immutable append-only files. If the node is an ephemeral one, a
5555
// memory database is returned.
5656
func (ctx *ServiceContext) OpenDatabaseWithFreezer(name string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
57-
if ctx.config.DataDir == "" {
57+
if ctx.Config.DataDir == "" {
5858
return rawdb.NewMemoryDatabase(), nil
5959
}
60-
root := ctx.config.ResolvePath(name)
60+
root := ctx.Config.ResolvePath(name)
6161

6262
switch {
6363
case freezer == "":
6464
freezer = filepath.Join(root, "ancient")
6565
case !filepath.IsAbs(freezer):
66-
freezer = ctx.config.ResolvePath(freezer)
66+
freezer = ctx.Config.ResolvePath(freezer)
6767
}
6868
return rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace)
6969
}
@@ -72,7 +72,7 @@ func (ctx *ServiceContext) OpenDatabaseWithFreezer(name string, cache int, handl
7272
// and if the user actually uses persistent storage. It will return an empty string
7373
// for emphemeral storage and the user's own input for absolute paths.
7474
func (ctx *ServiceContext) ResolvePath(path string) string {
75-
return ctx.config.ResolvePath(path)
75+
return ctx.Config.ResolvePath(path)
7676
}
7777

7878
// Service retrieves a currently running service registered of a specific type.
@@ -88,7 +88,7 @@ func (ctx *ServiceContext) Service(service interface{}) error {
8888
// ExtRPCEnabled returns the indicator whether node enables the external
8989
// RPC(http, ws or graphql).
9090
func (ctx *ServiceContext) ExtRPCEnabled() bool {
91-
return ctx.config.ExtRPCEnabled()
91+
return ctx.Config.ExtRPCEnabled()
9292
}
9393

9494
// ServiceConstructor is the function signature of the constructors needed to be

node/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestContextDatabases(t *testing.T) {
3838
t.Fatalf("non-created database already exists")
3939
}
4040
// Request the opening/creation of a database and ensure it persists to disk
41-
ctx := &ServiceContext{config: &Config{Name: "unit-test", DataDir: dir}}
41+
ctx := &ServiceContext{Config: Config{Name: "unit-test", DataDir: dir}}
4242
db, err := ctx.OpenDatabase("persistent", 0, 0, "")
4343
if err != nil {
4444
t.Fatalf("failed to open persistent database: %v", err)
@@ -49,7 +49,7 @@ func TestContextDatabases(t *testing.T) {
4949
t.Fatalf("persistent database doesn't exists: %v", err)
5050
}
5151
// Request th opening/creation of an ephemeral database and ensure it's not persisted
52-
ctx = &ServiceContext{config: &Config{DataDir: ""}}
52+
ctx = &ServiceContext{Config: Config{DataDir: ""}}
5353
db, err = ctx.OpenDatabase("ephemeral", 0, 0, "")
5454
if err != nil {
5555
t.Fatalf("failed to open ephemeral database: %v", err)

0 commit comments

Comments
 (0)