Commit 45f7bdfa authored by Janos Guljas's avatar Janos Guljas

add p2p ErrPeerNotFound error and improve overaly peer id locking

parent 87b9f1a5
......@@ -234,9 +234,7 @@ func (s *Service) AddProtocol(p p2p.ProtocolSpec) (err error) {
s.host.SetStreamHandlerMatch(id, matcher, func(stream network.Stream) {
peerID := stream.Conn().RemotePeer()
s.overlayPeerIDMu.Lock()
overlay, ok := s.peerIDToOverlay[peerID]
s.overlayPeerIDMu.Unlock()
overlay, ok := s.overlayForPeerID(peerID)
if !ok {
// todo: handle better
fmt.Printf("Could not fetch handshake for peerID %s\n", stream)
......@@ -293,12 +291,9 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (err error) {
return nil
}
func (s *Service) NewStream(ctx context.Context, overlay, protocolName, streamName, version string) (p2p.Stream, error) {
s.overlayPeerIDMu.Lock()
peerID, ok := s.overlayToPeerID[overlay]
s.overlayPeerIDMu.Unlock()
peerID, ok := s.peerIDForOverlay(overlay)
if !ok {
fmt.Printf("Could not fetch peerID for handshake %s\n", overlay)
return nil, nil
return nil, p2p.ErrPeerNotFound
}
return s.newStreamForPeerID(ctx, peerID, protocolName, streamName, version)
......@@ -317,6 +312,20 @@ func (s *Service) newStreamForPeerID(ctx context.Context, peerID libp2ppeer.ID,
return st, nil
}
func (s *Service) peerIDForOverlay(overlay string) (peerID libp2ppeer.ID, ok bool) {
s.overlayPeerIDMu.RLock()
peerID, ok = s.overlayToPeerID[overlay]
s.overlayPeerIDMu.RUnlock()
return peerID, ok
}
func (s *Service) overlayForPeerID(peerID libp2ppeer.ID) (overlay string, ok bool) {
s.overlayPeerIDMu.RLock()
overlay, ok = s.peerIDToOverlay[peerID]
s.overlayPeerIDMu.RUnlock()
return overlay, ok
}
func (s *Service) addAddresses(overlay string, peerID libp2ppeer.ID) {
s.overlayPeerIDMu.Lock()
s.overlayToPeerID[overlay] = peerID
......
......@@ -4,6 +4,10 @@
package p2p
import "errors"
type Peer struct {
Address string
}
var ErrPeerNotFound = errors.New("peer not found")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment