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) { ...@@ -234,9 +234,7 @@ func (s *Service) AddProtocol(p p2p.ProtocolSpec) (err error) {
s.host.SetStreamHandlerMatch(id, matcher, func(stream network.Stream) { s.host.SetStreamHandlerMatch(id, matcher, func(stream network.Stream) {
peerID := stream.Conn().RemotePeer() peerID := stream.Conn().RemotePeer()
s.overlayPeerIDMu.Lock() overlay, ok := s.overlayForPeerID(peerID)
overlay, ok := s.peerIDToOverlay[peerID]
s.overlayPeerIDMu.Unlock()
if !ok { if !ok {
// todo: handle better // todo: handle better
fmt.Printf("Could not fetch handshake for peerID %s\n", stream) 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) { ...@@ -293,12 +291,9 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (err error) {
return nil return nil
} }
func (s *Service) NewStream(ctx context.Context, overlay, protocolName, streamName, version string) (p2p.Stream, error) { func (s *Service) NewStream(ctx context.Context, overlay, protocolName, streamName, version string) (p2p.Stream, error) {
s.overlayPeerIDMu.Lock() peerID, ok := s.peerIDForOverlay(overlay)
peerID, ok := s.overlayToPeerID[overlay]
s.overlayPeerIDMu.Unlock()
if !ok { if !ok {
fmt.Printf("Could not fetch peerID for handshake %s\n", overlay) return nil, p2p.ErrPeerNotFound
return nil, nil
} }
return s.newStreamForPeerID(ctx, peerID, protocolName, streamName, version) return s.newStreamForPeerID(ctx, peerID, protocolName, streamName, version)
...@@ -317,6 +312,20 @@ func (s *Service) newStreamForPeerID(ctx context.Context, peerID libp2ppeer.ID, ...@@ -317,6 +312,20 @@ func (s *Service) newStreamForPeerID(ctx context.Context, peerID libp2ppeer.ID,
return st, nil 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) { func (s *Service) addAddresses(overlay string, peerID libp2ppeer.ID) {
s.overlayPeerIDMu.Lock() s.overlayPeerIDMu.Lock()
s.overlayToPeerID[overlay] = peerID s.overlayToPeerID[overlay] = peerID
......
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
package p2p package p2p
import "errors"
type Peer struct { type Peer struct {
Address string 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