swarm/network: add Hive.Peer method to safely retrieve a bzz peer

This commit is contained in:
Janos Guljas 2019-04-11 15:21:08 +02:00
parent bc652aebd9
commit 2b2314e6af
2 changed files with 10 additions and 3 deletions

View file

@ -160,9 +160,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) {
// block until control peer is found among hive peers
found := false
for attempts := 0; attempts < 20; attempts++ {
hive.lock.Lock()
_, found = hive.peers[peerID]
hive.lock.Unlock()
found = hive.Peer(peerID) != nil
if found {
break
}

View file

@ -209,6 +209,15 @@ func (h *Hive) PeerInfo(id enode.ID) interface{} {
}
}
// Peer returns a bzz peer from the Hive. If there is no peer
// with the provided enode id, a nil value is returned.
func (h *Hive) Peer(id enode.ID) *BzzPeer {
h.lock.Lock()
defer h.lock.Unlock()
return h.peers[id]
}
// loadPeers, savePeer implement persistence callback/
func (h *Hive) loadPeers() error {
var as []*BzzAddr