diff --git a/p2p/addrbook.go b/p2p/addrbook.go index 8570dcf7a..0b3301060 100644 --- a/p2p/addrbook.go +++ b/p2p/addrbook.go @@ -191,7 +191,12 @@ func (a *AddrBook) size() int { return a.nNew + a.nOld } -// PickAddress picks an address to connect to with new/old bias. +// PickAddress picks an address to connect to. +// The address is picked randomly from an old or new bucket according +// to the newBias argument, which must be between [0, 100] (or else is truncated to that range) +// and determines how biased we are to pick an address from a new bucket. +// PickAddress returns nil if the AddrBook is empty or if we try to pick +// from an empty bucket. func (a *AddrBook) PickAddress(newBias int) *NetAddress { a.mtx.Lock() defer a.mtx.Unlock() diff --git a/p2p/addrbook_test.go b/p2p/addrbook_test.go index 419081d01..d84c008ed 100644 --- a/p2p/addrbook_test.go +++ b/p2p/addrbook_test.go @@ -112,6 +112,7 @@ func TestAddrBookLookup(t *testing.T) { } func TestAddrBookPromoteToOld(t *testing.T) { + assert := assert.New(t) fname := createTempFileName("addrbook_test") randAddrs := randNetAddressPairs(t, 100) @@ -143,9 +144,7 @@ func TestAddrBookPromoteToOld(t *testing.T) { t.Errorf("selection could not be bigger than the book") } - if book.Size() != 100 { - t.Errorf("Size is not 100. Got %v", book.Size()) - } + assert.Equal(book.Size(), 100, "expecting book size to be 100") } func TestAddrBookHandlesDuplicates(t *testing.T) { diff --git a/p2p/pex_reactor.go b/p2p/pex_reactor.go index da72bd535..fd70198f4 100644 --- a/p2p/pex_reactor.go +++ b/p2p/pex_reactor.go @@ -252,10 +252,13 @@ func (r *PEXReactor) ensurePeers() { if try == nil { continue } - _, alreadySelected := toDial[try.IP.String()] - alreadyDialing := r.Switch.IsDialing(try) - alreadyConnected := r.Switch.Peers().Has(try.IP.String()) - if alreadySelected || alreadyDialing || alreadyConnected { + if _, selected := toDial[try.IP.String()]; selected { + continue + } + if dialling := r.Switch.IsDialing(try); dialling { + continue + } + if connected := r.Switch.Peers().Has(try.IP.String()); connected { continue } r.Logger.Info("Will dial address", "addr", try)