Merge pull request #2131 from kcalvinalvin/2024-03-05-fix-intermittent-addrmgr-bug

addrmgr: fix intermittent addrmanager_internal_test bug
This commit is contained in:
Olaoluwa Osuntokun 2024-03-05 12:07:57 -06:00 committed by GitHub
commit a4f447006e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,7 +12,7 @@ import (
) )
// randAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6 // randAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6
// address. // address. Some of the returned addresses may not be routable.
func randAddr(t *testing.T) *wire.NetAddressV2 { func randAddr(t *testing.T) *wire.NetAddressV2 {
t.Helper() t.Helper()
@ -40,6 +40,23 @@ func randAddr(t *testing.T) *wire.NetAddressV2 {
) )
} }
// routableRandAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6
// address that is always routable.
func routableRandAddr(t *testing.T) *wire.NetAddressV2 {
t.Helper()
var addr *wire.NetAddressV2
// If the address is not routable, try again.
routable := false
for !routable {
addr = randAddr(t)
routable = IsRoutable(addr)
}
return addr
}
// assertAddr ensures that the two addresses match. The timestamp is not // assertAddr ensures that the two addresses match. The timestamp is not
// checked as it does not affect uniquely identifying a specific address. // checked as it does not affect uniquely identifying a specific address.
func assertAddr(t *testing.T, got, expected *wire.NetAddressV2) { func assertAddr(t *testing.T, got, expected *wire.NetAddressV2) {
@ -104,9 +121,9 @@ func TestAddrManagerSerialization(t *testing.T) {
expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs) expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
for i := 0; i < numAddrs; i++ { for i := 0; i < numAddrs; i++ {
addr := randAddr(t) addr := routableRandAddr(t)
expectedAddrs[NetAddressKey(addr)] = addr expectedAddrs[NetAddressKey(addr)] = addr
addrMgr.AddAddress(addr, randAddr(t)) addrMgr.AddAddress(addr, routableRandAddr(t))
} }
// Now that the addresses have been added, we should be able to retrieve // Now that the addresses have been added, we should be able to retrieve
@ -149,9 +166,9 @@ func TestAddrManagerV1ToV2(t *testing.T) {
expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs) expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
for i := 0; i < numAddrs; i++ { for i := 0; i < numAddrs; i++ {
addr := randAddr(t) addr := routableRandAddr(t)
expectedAddrs[NetAddressKey(addr)] = addr expectedAddrs[NetAddressKey(addr)] = addr
addrMgr.AddAddress(addr, randAddr(t)) addrMgr.AddAddress(addr, routableRandAddr(t))
} }
// Then, we'll persist these addresses to disk and restart the address // Then, we'll persist these addresses to disk and restart the address