Fix IPV6 binding for Beacon Node (#8326)

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Nishant Das 2021-01-26 04:10:41 +08:00 committed by GitHub
parent d21365b882
commit b2d6012371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -104,12 +104,14 @@ func (s *Service) createListener(
// on which we will bind our listener on
// by default we will listen to all interfaces.
var bindIP net.IP
if ipAddr.To4() != nil {
networkVersion = "udp4"
networkVersion = udpVersionFromIP(ipAddr)
switch networkVersion {
case "udp4":
bindIP = net.IPv4zero
} else {
networkVersion = "udp6"
case "udp6":
bindIP = net.IPv6zero
default:
return nil, errors.New("invalid ip provided")
}
// If local ip is specified then use that instead.
@ -119,6 +121,7 @@ func (s *Service) createListener(
return nil, errors.New("invalid local ip provided")
}
bindIP = ipAddr
networkVersion = udpVersionFromIP(ipAddr)
}
udpAddr := &net.UDPAddr{
IP: bindIP,
@ -421,3 +424,10 @@ func multiAddrFromString(address string) (ma.Multiaddr, error) {
}
return addr.Multiaddr(), nil
}
func udpVersionFromIP(ipAddr net.IP) string {
if ipAddr.To4() != nil {
return "udp4"
}
return "udp6"
}

View File

@ -312,6 +312,15 @@ func TestMultipleDiscoveryAddresses(t *testing.T) {
assert.Equal(t, true, ipv6Found, "IPv6 discovery address not found")
}
func TestCorrectUDPVersion(t *testing.T) {
assert.Equal(t, "udp4", udpVersionFromIP(net.IPv4zero), "incorrect network version")
assert.Equal(t, "udp6", udpVersionFromIP(net.IPv6zero), "incorrect network version")
assert.Equal(t, "udp4", udpVersionFromIP(net.IP{200, 20, 12, 255}), "incorrect network version")
assert.Equal(t, "udp6", udpVersionFromIP(net.IP{22, 23, 24, 251, 17, 18, 0, 0, 0, 0, 12, 14, 212, 213, 16, 22}), "incorrect network version")
// v4 in v6
assert.Equal(t, "udp4", udpVersionFromIP(net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 212, 213, 16, 22}), "incorrect network version")
}
// addPeer is a helper to add a peer with a given connection state)
func addPeer(t *testing.T, p *peers.Status, state peerdata.PeerConnectionState) peer.ID {
// Set up some peers with different states