|
| 1 | +// Copyright 2026 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package p2p_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/coreos/go-semver/semver" |
| 12 | + "github.com/ethersphere/bee/v2/pkg/p2p" |
| 13 | + "github.com/ethersphere/bee/v2/pkg/p2p/protobuf" |
| 14 | + "github.com/ethersphere/bee/v2/pkg/swarm" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + exampleProtocolName = "versionedping" |
| 19 | + exampleProtocolVersion = "1.2.0" |
| 20 | + exampleStreamName = "ping" |
| 21 | +) |
| 22 | + |
| 23 | +// ExampleService represents a full Bee protocol service (structured like pkg/pingpong) |
| 24 | +// supporting 3 version levels: |
| 25 | +// - v1.2.0: Current version |
| 26 | +// - v1.1.0: Legacy version 1.1 |
| 27 | +// - v1.0.0: Legacy version 1.0 |
| 28 | +type ExampleService struct { |
| 29 | + streamer p2p.Streamer |
| 30 | +} |
| 31 | + |
| 32 | +func NewExampleService(streamer p2p.Streamer) *ExampleService { |
| 33 | + return &ExampleService{ |
| 34 | + streamer: streamer, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (s *ExampleService) Protocol() p2p.ProtocolSpec { |
| 39 | + return p2p.ProtocolSpec{ |
| 40 | + Name: exampleProtocolName, |
| 41 | + Version: exampleProtocolVersion, |
| 42 | + StreamSpecs: []p2p.StreamSpec{ |
| 43 | + { |
| 44 | + Name: exampleStreamName, |
| 45 | + Handler: p2p.NewVersionedHandlersFunc( |
| 46 | + p2p.VersionedHandler{ |
| 47 | + Version: semver.New("1.2.0"), // Server handler for >= 1.2.0 |
| 48 | + Handler: func(ctx context.Context, p p2p.Peer, stream p2p.Stream) error { |
| 49 | + w, r := protobuf.NewWriterAndReader(stream) |
| 50 | + _, _ = w, r |
| 51 | + fmt.Println("Server received ping on v1.2.0 handler") |
| 52 | + return stream.FullClose() |
| 53 | + }, |
| 54 | + }, |
| 55 | + p2p.VersionedHandler{ |
| 56 | + Version: semver.New("1.1.0"), // Server handler for legacy 1.1.0 |
| 57 | + Handler: func(ctx context.Context, p p2p.Peer, stream p2p.Stream) error { |
| 58 | + w, r := protobuf.NewWriterAndReader(stream) |
| 59 | + _, _ = w, r |
| 60 | + fmt.Println("Server received ping on v1.1.0 legacy handler") |
| 61 | + return stream.FullClose() |
| 62 | + }, |
| 63 | + }, |
| 64 | + p2p.VersionedHandler{ |
| 65 | + Version: semver.New("1.0.0"), // Server handler for legacy 1.0.0 |
| 66 | + Handler: func(ctx context.Context, p p2p.Peer, stream p2p.Stream) error { |
| 67 | + w, r := protobuf.NewWriterAndReader(stream) |
| 68 | + _, _ = w, r |
| 69 | + fmt.Println("Server received ping on v1.0.0 legacy handler") |
| 70 | + return stream.FullClose() |
| 71 | + }, |
| 72 | + }, |
| 73 | + ), |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (s *ExampleService) Ping(ctx context.Context, peer swarm.Address) error { |
| 80 | + stream, err := s.streamer.NewStream(ctx, peer, nil, exampleProtocolName, "1.1.0", exampleStreamName) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + defer stream.Close() |
| 85 | + |
| 86 | + pingClient := p2p.NewVersionedHandlersFunc( |
| 87 | + p2p.VersionedHandler{ |
| 88 | + Version: semver.New("1.2.0"), // Current version client (>= 1.2.0) |
| 89 | + Handler: func(ctx context.Context, _ p2p.Peer, stream p2p.Stream) error { |
| 90 | + w, r := protobuf.NewWriterAndReader(stream) |
| 91 | + _, _ = w, r |
| 92 | + fmt.Println("Client sent ping using v1.2.0 format") |
| 93 | + return nil |
| 94 | + }, |
| 95 | + }, |
| 96 | + p2p.VersionedHandler{ |
| 97 | + Version: semver.New("1.1.0"), // Legacy v1.1.0 client (1.1.0 <= v < 1.2.0) |
| 98 | + Handler: func(ctx context.Context, _ p2p.Peer, stream p2p.Stream) error { |
| 99 | + w, r := protobuf.NewWriterAndReader(stream) |
| 100 | + _, _ = w, r |
| 101 | + fmt.Println("Client sent ping using v1.1.0 legacy format") |
| 102 | + return nil |
| 103 | + }, |
| 104 | + }, |
| 105 | + p2p.VersionedHandler{ |
| 106 | + Version: semver.New("1.0.0"), // Legacy v1.0.0 client (1.0.0 <= v < 1.1.0) |
| 107 | + Handler: func(ctx context.Context, _ p2p.Peer, stream p2p.Stream) error { |
| 108 | + w, r := protobuf.NewWriterAndReader(stream) |
| 109 | + _, _ = w, r |
| 110 | + fmt.Println("Client sent ping using v1.0.0 legacy format") |
| 111 | + return nil |
| 112 | + }, |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + return pingClient(ctx, p2p.Peer{Address: peer}, stream) |
| 117 | +} |
| 118 | + |
| 119 | +// Example_versionedProtocol demonstrates constructing a versioned P2P protocol service |
| 120 | +// and executing versioned message exchange between client and server nodes. |
| 121 | +func Example_versionedProtocol() { |
| 122 | + ctx := context.Background() |
| 123 | + |
| 124 | + serverSvc := NewExampleService(nil) |
| 125 | + serverSpec := serverSvc.Protocol() |
| 126 | + serverHandler := serverSpec.StreamSpecs[0].Handler |
| 127 | + |
| 128 | + // Client streamer connects to server where version 1.1.0 is negotiated: |
| 129 | + clientStreamer := &mockStreamer{ |
| 130 | + supportedVersions: map[string]bool{ |
| 131 | + "1.1.0": true, |
| 132 | + }, |
| 133 | + } |
| 134 | + clientSvc := NewExampleService(clientStreamer) |
| 135 | + |
| 136 | + // Client sends ping request (client-side dispatcher automatically selects v1.1.0 format) |
| 137 | + _ = clientSvc.Ping(ctx, swarm.ZeroAddress) |
| 138 | + |
| 139 | + // Server receives incoming stream (server-side dispatcher automatically selects v1.1.0 handler) |
| 140 | + incomingStream := mockStream{ |
| 141 | + version: "1.1.0", |
| 142 | + } |
| 143 | + _ = serverHandler(ctx, p2p.Peer{Address: swarm.ZeroAddress}, incomingStream) |
| 144 | + |
| 145 | + // Output: |
| 146 | + // Client sent ping using v1.1.0 legacy format |
| 147 | + // Server received ping on v1.1.0 legacy handler |
| 148 | +} |
0 commit comments