Skip to content

Commit

Permalink
p2p/server: add DNS resolved candidates to distributed hash table
Browse files Browse the repository at this point in the history
Currently, DNS resolved candidates of eth protocol are added as candidates for
dialing but not added to DHT. The DHT's seed nodes are still configured via
hardcoded nodes in bootnodes flag. As we want to replace the hardcoded bootnodes
entirely with DNS resolved nodes, we resolve some nodes from the DNS discovery
URL and put them into DHT when setting up the p2p discovery.
  • Loading branch information
minh-bq committed Oct 19, 2023
1 parent 800ec03 commit 9ee4e4a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/ronin/les_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func startGethWithIpc(t *testing.T, name string, args ...string) *gethrpc {
// We can't know exactly how long geth will take to start, so we try 10
// times over a 5 second period.
var err error
for i := 0; i < 10; i++ {
for i := 0; i < 30; i++ {
time.Sleep(500 * time.Millisecond)
if g.rpc, err = rpc.Dial(ipcpath); err == nil {
return g
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {

// Setup DNS discovery iterators.
dnsclient := dnsdisc.NewClient(dnsdisc.Config{})
log.Info("DNS url", "eth", eth.config.EthDiscoveryURLs, "snap", eth.config.SnapDiscoveryURLs)
eth.ethDialCandidates, err = dnsclient.NewIterator(eth.config.EthDiscoveryURLs...)
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (

// Maximum amount of time allowed for writing a complete message.
frameWriteTimeout = 20 * time.Second

// The number of DHT seed nodes got from DNS resolve
numSeedNodesFromDns = 5

// Name of eth protocol
ethProtocolName = "eth"
)

var errServerStopped = errors.New("server stopped")
Expand Down Expand Up @@ -539,6 +545,21 @@ func (srv *Server) setupDiscovery() error {
added := make(map[string]bool)
for _, proto := range srv.Protocols {
if proto.DialCandidates != nil && !added[proto.Name] {
// If the protocol is eth, the dial candidates are retrieved from
// DNS URL. We want to use the DNS discovery for bootstrap nodes
// too, so resolve some node from DNS and add to DHT.
if proto.Name == ethProtocolName {
for i := 0; i < numSeedNodesFromDns; i++ {
if !proto.DialCandidates.Next() {
break
}

node := proto.DialCandidates.Node()
srv.BootstrapNodes = append(srv.BootstrapNodes, node)
srv.BootstrapNodesV5 = append(srv.BootstrapNodes, node)
}
}

srv.discmix.AddSource(proto.DialCandidates)
added[proto.Name] = true
}
Expand Down

0 comments on commit 9ee4e4a

Please sign in to comment.