Merge pull request #1488 from Crypt-iQ/server_race_1108

server: add addressesMtx to fix race condition
This commit is contained in:
Olaoluwa Osuntokun 2019-11-08 18:21:10 -08:00 committed by GitHub
commit 79943b2331
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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