server: add addressesMtx to fix race condition

This commit is contained in:
nsa 2019-11-08 21:11:24 -05:00
parent a41498d578
commit c2ca0a408e
No known key found for this signature in database
GPG Key ID: 118759E83439A9B1

View File

@ -273,6 +273,7 @@ type serverPeer struct {
sentAddrs bool
isWhitelisted bool
filter *bloom.Filter
addressesMtx sync.RWMutex
knownAddresses map[string]struct{}
banScore connmgr.DynamicBanScore
quit chan struct{}
@ -305,14 +306,18 @@ func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
// addKnownAddresses adds the given addresses to the set of known addresses to
// the peer to prevent sending duplicate addresses.
func (sp *serverPeer) addKnownAddresses(addresses []*wire.NetAddress) {
sp.addressesMtx.Lock()
for _, na := range addresses {
sp.knownAddresses[addrmgr.NetAddressKey(na)] = struct{}{}
}
sp.addressesMtx.Unlock()
}
// addressKnown true if the given address is already known to the peer.
func (sp *serverPeer) addressKnown(na *wire.NetAddress) bool {
sp.addressesMtx.RLock()
_, exists := sp.knownAddresses[addrmgr.NetAddressKey(na)]
sp.addressesMtx.RUnlock()
return exists
}