From cc771bbea6f0f8d3ca55c34f97ce8f69017a9cbd Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 13 Jun 2026 13:57:56 +0200 Subject: [PATCH] introduce packet protocol v1 --- node/node.go | 18 +++++++++++------- p2p/node.go | 17 ++++++++++++++--- rpc/server.go | 3 ++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/node/node.go b/node/node.go index 46534f0..9375f67 100644 --- a/node/node.go +++ b/node/node.go @@ -345,12 +345,16 @@ func (node *Node) streamHandler(stream network.Stream) { } var streamLock = new(sync.Mutex) - inserted := node.insertActiveStream(remotePeerID, SharedStream{ - Stream: &stream, - Lock: streamLock, - }) - if inserted { - defer node.expireActiveStream(remotePeerID) + + // Version 0 nodes don't read from this stream, so we can't reuse it. + if stream.Protocol() != p2p.ProtocolV0 { + inserted := node.insertActiveStream(remotePeerID, SharedStream{ + Stream: &stream, + Lock: streamLock, + }) + if inserted { + defer node.expireActiveStream(remotePeerID) + } } var packet = make([]byte, 1420) @@ -419,7 +423,7 @@ func (node *Node) sendPacket(dst peer.ID, packet []byte, plen int) { } } - stream, err := node.p2p.NewStream(node.ctx, dst, p2p.Protocol) + stream, err := node.p2p.NewStream(node.ctx, dst, p2p.Protocols...) if err != nil { logger.With(zap.String("destination", dst.String()), zap.Error(err)).Error("Failed to open stream") go p2p.Rediscover() diff --git a/p2p/node.go b/p2p/node.go index bff19c3..de8e2c0 100644 --- a/p2p/node.go +++ b/p2p/node.go @@ -21,6 +21,7 @@ import ( "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/pnet" + "github.com/libp2p/go-libp2p/core/protocol" "github.com/libp2p/go-libp2p/core/routing" "github.com/libp2p/go-libp2p/p2p/discovery/backoff" "github.com/libp2p/go-libp2p/p2p/host/autorelay" @@ -48,8 +49,16 @@ func (c *httpRoutingWrapper) Bootstrap(ctx context.Context) error { return nil } -// Protocol is a descriptor for the Hyprspace P2P Protocol. -const Protocol = "/hyprspace/0.0.1" +// Version 0 +const ProtocolV0 = "/hyprspace/0.0.1" + +// Version 1: Bidirectional streams +const ProtocolV1 = "/hyprspace/1" + +var Protocols = []protocol.ID{ + ProtocolV1, + ProtocolV0, +} func getExtraPeers(addr ma.Multiaddr) (nodesList []string) { nodesList = []string{} @@ -281,7 +290,9 @@ func CreateNode(ctx context.Context, privateKey crypto.PrivKey, listenAddreses [ node = routedhost.Wrap(basicHost, pr) // Setup Hyprspace Stream Handler - node.SetStreamHandler(Protocol, handler) + for _, proto := range Protocols { + node.SetStreamHandler(proto, handler) + } if err != nil { return node, nil, err diff --git a/rpc/server.go b/rpc/server.go index 2531985..5fb2e7c 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -7,6 +7,7 @@ import ( "net" "net/rpc" "os" + "slices" "sync" "syscall" @@ -82,7 +83,7 @@ func (hsr *HyprspaceRPC) Route(args *RouteArgs, reply *RouteReply) error { ConnLoop: for _, c := range hsr.host.Network().ConnsToPeer(rte.Target.ID) { for _, s := range c.GetStreams() { - if s.Protocol() == p2p.Protocol { + if slices.Contains(p2p.Protocols, s.Protocol()) { if _, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_CIRCUIT); err == nil { relay = true if ra, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_P2P); err == nil {