}
hash := &tx.ID
p.knownTxs.Add(hash.String())
+ if p.swPeer == nil {
+ return errPeerDropped
+ }
p.swPeer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg})
}
return nil
}
// Peer retrieves the registered peer with the given id.
-func (ps *peerSet) Peer(id string) *peer {
+func (ps *peerSet) Peer(id string) (*peer, bool) {
ps.lock.RLock()
defer ps.lock.RUnlock()
-
- return ps.peers[id]
+ p, ok := ps.peers[id]
+ return p, ok
}
// Len returns if the current number of peers in the set.
}
func (ps *peerSet) requestBlockByHash(peerID string, hash *bc.Hash) error {
- ps.lock.Lock()
- defer ps.lock.Unlock()
-
- peer, ok := ps.peers[peerID]
+ peer, ok := ps.Peer(peerID)
if !ok {
return errors.New("Can't find peer. ")
}
}
func (ps *peerSet) requestBlockByHeight(peerID string, height uint64) error {
- ps.lock.Lock()
- defer ps.lock.Unlock()
-
- peer, ok := ps.peers[peerID]
+ peer, ok := ps.Peer(peerID)
if !ok {
return errors.New("Can't find peer. ")
}
peers := ps.PeersWithoutBlock(&hash)
abnormalPeers := make([]*peer, 0)
for _, peer := range peers {
- if ok := peer.swPeer.Send(BlockchainChannel, struct{ BlockchainMessage }{msg}); !ok {
+ if ok := peer.swPeer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg}); !ok {
abnormalPeers = append(abnormalPeers, peer)
continue
}
- ps.MarkBlock(peer.swPeer.Key, &hash)
+ if p, ok := ps.Peer(peer.id); ok {
+ p.MarkBlock(&hash)
+ }
}
return abnormalPeers, nil
}
peers := ps.PeersWithoutTx(&tx.ID)
abnormalPeers := make([]*peer, 0)
for _, peer := range peers {
- if ok := peer.swPeer.Send(BlockchainChannel, struct{ BlockchainMessage }{msg}); !ok {
+ if ok := peer.swPeer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg}); !ok {
abnormalPeers = append(abnormalPeers, peer)
continue
}
- ps.peers[peer.swPeer.Key].MarkTransaction(&tx.ID)
+ if p, ok := ps.Peer(peer.id); ok {
+ p.MarkTransaction(&tx.ID)
+ }
}
return abnormalPeers, nil
}