swarm/pss: mutex lifecycle fixed (#19045)

(cherry picked from commit b30109df3c)
pull/19029/head
gluk256 6 years ago committed by Rafael Matias
parent 3b02b0ba4b
commit 799fe99537
No known key found for this signature in database
GPG Key ID: 1BC39532FB4A2DBD
  1. 11
      swarm/pss/protocol.go

@ -228,6 +228,7 @@ func ToP2pMsg(msg []byte) (p2p.Msg, error) {
// to link the peer to. // to link the peer to.
// The key must exist in the pss store prior to adding the peer. // The key must exist in the pss store prior to adding the peer.
func (p *Protocol) AddPeer(peer *p2p.Peer, topic Topic, asymmetric bool, key string) (p2p.MsgReadWriter, error) { func (p *Protocol) AddPeer(peer *p2p.Peer, topic Topic, asymmetric bool, key string) (p2p.MsgReadWriter, error) {
var ok bool
rw := &PssReadWriter{ rw := &PssReadWriter{
Pss: p.Pss, Pss: p.Pss,
rw: make(chan p2p.Msg), rw: make(chan p2p.Msg),
@ -242,19 +243,21 @@ func (p *Protocol) AddPeer(peer *p2p.Peer, topic Topic, asymmetric bool, key str
} }
if asymmetric { if asymmetric {
p.Pss.pubKeyPoolMu.Lock() p.Pss.pubKeyPoolMu.Lock()
if _, ok := p.Pss.pubKeyPool[key]; !ok { _, ok = p.Pss.pubKeyPool[key]
p.Pss.pubKeyPoolMu.Unlock()
if !ok {
return nil, fmt.Errorf("asym key does not exist: %s", key) return nil, fmt.Errorf("asym key does not exist: %s", key)
} }
p.Pss.pubKeyPoolMu.Unlock()
p.RWPoolMu.Lock() p.RWPoolMu.Lock()
p.pubKeyRWPool[key] = rw p.pubKeyRWPool[key] = rw
p.RWPoolMu.Unlock() p.RWPoolMu.Unlock()
} else { } else {
p.Pss.symKeyPoolMu.Lock() p.Pss.symKeyPoolMu.Lock()
if _, ok := p.Pss.symKeyPool[key]; !ok { _, ok = p.Pss.symKeyPool[key]
p.Pss.symKeyPoolMu.Unlock()
if !ok {
return nil, fmt.Errorf("symkey does not exist: %s", key) return nil, fmt.Errorf("symkey does not exist: %s", key)
} }
p.Pss.symKeyPoolMu.Unlock()
p.RWPoolMu.Lock() p.RWPoolMu.Lock()
p.symKeyRWPool[key] = rw p.symKeyRWPool[key] = rw
p.RWPoolMu.Unlock() p.RWPoolMu.Unlock()

Loading…
Cancel
Save