Skip to content

Commit

Permalink
network: fix publicKeyIdentTracker data race in hybrid mode (#6110)
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy authored Aug 26, 2024
1 parent 8e2e2e4 commit 4990077
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions network/netidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-deadlock"
)

// netidentity.go implements functionality to participate in an "Identity Challenge Exchange"
Expand Down Expand Up @@ -461,12 +462,14 @@ func (noopIdentityTracker) removeIdentity(p *wsPeer) {}
// mapping from PublicKeys exchanged in identity challenges to a peer
// this structure is not thread-safe; it is protected by wn.peersLock or p2p.wsPeersLock
type publicKeyIdentTracker struct {
mu deadlock.Mutex
peersByID map[crypto.PublicKey]*wsPeer
}

// NewIdentityTracker returns a new publicKeyIdentTracker
func NewIdentityTracker() *publicKeyIdentTracker {
return &publicKeyIdentTracker{
mu: deadlock.Mutex{},
peersByID: make(map[crypto.PublicKey]*wsPeer),
}
}
Expand All @@ -475,6 +478,8 @@ func NewIdentityTracker() *publicKeyIdentTracker {
// returns false if it was unable to load the peer into the given identity
// or true otherwise (if the peer was already there, or if it was added)
func (t *publicKeyIdentTracker) setIdentity(p *wsPeer) bool {
t.mu.Lock()
defer t.mu.Unlock()
existingPeer, exists := t.peersByID[p.identity]
if !exists {
// the identity is not occupied, so set it and return true
Expand All @@ -489,6 +494,8 @@ func (t *publicKeyIdentTracker) setIdentity(p *wsPeer) bool {
// removeIdentity removes the entry in the peersByID map if it exists
// and is occupied by the given peer
func (t *publicKeyIdentTracker) removeIdentity(p *wsPeer) {
t.mu.Lock()
defer t.mu.Unlock()
if t.peersByID[p.identity] == p {
delete(t.peersByID, p.identity)
}
Expand Down

0 comments on commit 4990077

Please sign in to comment.