diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index 18c4841e02..2f1797d1e2 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -268,7 +268,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value { } for _, id := range gen([]enode.ID{}, rand).([]enode.ID) { r := new(enr.Record) - r.Set(enr.IP(genIP(rand))) + r.Set(enr.IPv4Addr(netutil.RandomAddr(rand, true))) n := enode.SignNull(r, id) t.All = append(t.All, n) } @@ -483,12 +483,6 @@ func gen(typ interface{}, rand *rand.Rand) interface{} { return v.Interface() } -func genIP(rand *rand.Rand) net.IP { - ip := make(net.IP, 4) - rand.Read(ip) - return ip -} - func quickcfg() *quick.Config { return &quick.Config{ MaxCount: 5000, diff --git a/p2p/enode/localnode_test.go b/p2p/enode/localnode_test.go index 7508dc6a08..86b962a74e 100644 --- a/p2p/enode/localnode_test.go +++ b/p2p/enode/localnode_test.go @@ -119,7 +119,7 @@ func TestLocalNodeEndpoint(t *testing.T) { assert.Equal(t, fallback.Port, ln.Node().UDP()) assert.Equal(t, initialSeq+1, ln.Node().Seq()) - from := netip.AddrPortFrom(netutil.RandomAddr(rng), 9000) + from := netip.AddrPortFrom(netutil.RandomAddr(rng, true), 9000) endpoint := netip.AddrPortFrom(netutil.IPToAddr(predicted.IP), uint16(predicted.Port)) ln.UDPEndpointStatement(from, endpoint) } diff --git a/p2p/netutil/net.go b/p2p/netutil/net.go index 37dd0424da..01fda7dc7d 100644 --- a/p2p/netutil/net.go +++ b/p2p/netutil/net.go @@ -354,9 +354,9 @@ func IPToAddr(ip net.IP) netip.Addr { } // RandomAddr creates a random IP address. -func RandomAddr(rng *rand.Rand) netip.Addr { +func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr { var bytes []byte - if rng.Intn(2) == 0 { + if ipv4 || rng.Intn(2) == 0 { bytes = make([]byte, 4) } else { bytes = make([]byte, 16) diff --git a/p2p/netutil/net_test.go b/p2p/netutil/net_test.go index ededdcf7f1..d9fcac8caf 100644 --- a/p2p/netutil/net_test.go +++ b/p2p/netutil/net_test.go @@ -252,7 +252,7 @@ func TestDistinctNetSetAddRemove(t *testing.T) { Values: func(s []reflect.Value, rng *rand.Rand) { slice := make([]netip.Addr, rng.Intn(20)+1) for i := range slice { - slice[i] = RandomAddr(rng) + slice[i] = RandomAddr(rng, false) } s[0] = reflect.ValueOf(slice) },