mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 01:43:47 +00:00
p2p: add test for dialing with DNS hostname
This commit is contained in:
parent
7ffb97c414
commit
3a983d3b3a
3 changed files with 45 additions and 15 deletions
|
|
@ -490,6 +490,7 @@ func (d *dialScheduler) dnsResolveHostname(n *enode.Node) (*enode.Node, error) {
|
|||
if foundIP6.IsValid() {
|
||||
rec.Set(enr.IPv6Addr(foundIP6))
|
||||
}
|
||||
rec.SetSeq(n.Seq()) // ensure seq not bumped by update
|
||||
newNode := enode.SignNull(rec, n.ID()).WithHostname(n.Hostname())
|
||||
d.log.Trace("Node updated from DNS lookup", "id", n.ID(), "name", n.Hostname(), "ip", newNode.IP())
|
||||
return newNode, nil
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
|
@ -394,6 +395,34 @@ func TestDialSchedResolve(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestDialSchedDNSHostname(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
config := dialConfig{
|
||||
maxActiveDials: 1,
|
||||
maxDialPeers: 1,
|
||||
}
|
||||
node := newNode(uintID(0x01), ":30303").WithHostname("node-hostname")
|
||||
resolved := newNode(uintID(0x01), "1.2.3.4:30303").WithHostname("node-hostname")
|
||||
runDialTest(t, config, []dialTestRound{
|
||||
{
|
||||
update: func(d *dialScheduler) {
|
||||
d.dnsLookupFunc = func(ctx context.Context, network string, name string) ([]netip.Addr, error) {
|
||||
if name != "node-hostname" {
|
||||
t.Error("wrong hostname in DNS lookup:", name)
|
||||
}
|
||||
result := []netip.Addr{netip.MustParseAddr("1.2.3.4")}
|
||||
return result, nil
|
||||
}
|
||||
d.addStatic(node)
|
||||
},
|
||||
wantNewDials: []*enode.Node{
|
||||
resolved,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// -------
|
||||
// Code below here is the framework for the tests above.
|
||||
|
||||
|
|
|
|||
|
|
@ -60,22 +60,22 @@ func uintID(i uint16) enode.ID {
|
|||
// newNode creates a node record with the given address.
|
||||
func newNode(id enode.ID, addr string) *enode.Node {
|
||||
var r enr.Record
|
||||
if addr != "" {
|
||||
// Set the port if present.
|
||||
if strings.Contains(addr, ":") {
|
||||
hs, ps, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("invalid address %q", addr))
|
||||
}
|
||||
port, err := strconv.Atoi(ps)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("invalid port in %q", addr))
|
||||
}
|
||||
r.Set(enr.TCP(port))
|
||||
r.Set(enr.UDP(port))
|
||||
addr = hs
|
||||
// Set the port if present.
|
||||
if strings.Contains(addr, ":") {
|
||||
hs, ps, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("invalid address %q", addr))
|
||||
}
|
||||
// Set the IP.
|
||||
port, err := strconv.Atoi(ps)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("invalid port in %q", addr))
|
||||
}
|
||||
r.Set(enr.TCP(port))
|
||||
r.Set(enr.UDP(port))
|
||||
addr = hs
|
||||
}
|
||||
// Set the IP.
|
||||
if addr != "" {
|
||||
ip := net.ParseIP(addr)
|
||||
if ip == nil {
|
||||
panic(fmt.Errorf("invalid IP %q", addr))
|
||||
|
|
|
|||
Loading…
Reference in a new issue