Skip to content

Commit

Permalink
fix: standardize protocol ID across all protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
kant committed Jul 17, 2024
1 parent 65753f6 commit d9a2704
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
7 changes: 0 additions & 7 deletions p2p/pkg/p2p/libp2p/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"context"
"crypto/ecdh"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p/core"
"github.com/libp2p/go-libp2p/core/protocol"
handshakepb "github.com/primev/mev-commit/p2p/gen/go/handshake/v1"
p2pcrypto "github.com/primev/mev-commit/p2p/pkg/crypto"
"github.com/primev/mev-commit/p2p/pkg/p2p"
Expand Down Expand Up @@ -73,10 +70,6 @@ func New(
return s, nil
}

func ProtocolID() protocol.ID {
return protocol.ID(fmt.Sprintf("/%s/%s", ProtocolName, ProtocolVersion))
}

func (h *Service) verifyReq(
req *handshakepb.HandshakeReq,
peerID core.PeerID,
Expand Down
10 changes: 7 additions & 3 deletions p2p/pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func New(opts *Options) (*Service, error) {

host.Network().Notify(s.peers)

s.host.SetStreamHandler(handshake.ProtocolID(), s.handleConnectReq)
s.host.SetStreamHandler(ConstructProtocolID(handshake.ProtocolName, handshake.ProtocolVersion), s.handleConnectReq)

if len(opts.BootstrapAddrs) > 0 {
go s.startBootstrapper(opts.BootstrapAddrs)
Expand Down Expand Up @@ -276,6 +276,10 @@ func (s *Service) Self() map[string]interface{} {
}
}

func ConstructProtocolID(protocolName, protocolVersion string) protocol.ID {
return protocol.ID(fmt.Sprintf("/%s/%s", protocolName, protocolVersion))
}

func matchProtocolIDWithSemver(
incomingProto string,
protoID string,
Expand Down Expand Up @@ -311,7 +315,7 @@ func (s *Service) AddStreamHandlers(streams ...p2p.StreamDesc) {
ss := stream

s.host.SetStreamHandlerMatch(
protocol.ID(ss.Name),
ConstructProtocolID(ss.Name, ss.Version),
func(p protocol.ID) bool {
matched, err := matchProtocolIDWithSemver(string(p), ss.Name, ss.Version)
if err != nil {
Expand Down Expand Up @@ -422,7 +426,7 @@ func (s *Service) Connect(ctx context.Context, info []byte) (p2p.Peer, error) {
return p2p.Peer{}, err
}

streamlibp2p, err := s.host.NewStream(ctx, addrInfo.ID, handshake.ProtocolID())
streamlibp2p, err := s.host.NewStream(ctx, addrInfo.ID, ConstructProtocolID(handshake.ProtocolName, handshake.ProtocolVersion))
if err != nil {
return p2p.Peer{}, err
}
Expand Down

0 comments on commit d9a2704

Please sign in to comment.