@@ -16,6 +16,13 @@ Exchange Server, Exchange Client and GossipSub Subscriber for header exchange.
16
16
On start the client connects to trusted peers (bootstrappers) as well as kicks off tracking and garbage collection routines for peer connections.
17
17
The getters it defines are several and are used to get headers by height, range, and hash.
18
18
The exchange client uses bi-directional read-write libp2p streams to request and receive headers from peers.
19
+ The peers that the client interacts with are tracked by the client's peerTracker, and they are of two types:
20
+
21
+ - Trusted Peers: These are bootstrapper peers that are trusted by the client and are used to request headers.
22
+
23
+ - Connected Peers: Peers that were not supplied as TrustedPeers but got connected to the node down the line.
24
+ The peer manager provides the most available peers to the client's session for requesting headers by using an availability queue
25
+ with negative sorting. The availability queue is updated by the client's peerTracker on every peer connection/disconnection.
19
26
20
27
- Subscriber:
21
28
The subscriber is an abstraction over GossipSub subscriptions that tracks the pubsub object + topic.
@@ -29,103 +36,83 @@ For more information, see the documentation for each component.
29
36
30
37
# Usage Examples
31
38
32
- - Exchange Server Usage
33
- To use the exchange server, first create a new instance of the server and start it:
34
-
35
- ```go
36
- s, err:= p2p.NewExchangeServer[H](
37
-
38
- libp2pHost,
39
- store,
40
- WithNetworkID[ServerParameters](networkID),
41
-
42
- )
43
- err = s.Start()
44
- // ...
45
- err = s.Stop()
46
- ```
47
-
48
- - Exchange Client Usage
39
+ Exchange Server Usage:
40
+ To use the exchange server, first create a new instance of the server and start it:
49
41
50
- To use the exchange client, first create a new instance of the client and start it:
42
+ s, err:= p2p.NewExchangeServer[H](
43
+ libp2pHost,
44
+ store,
45
+ WithNetworkID[ServerParameters](networkID),
46
+ )
47
+ err = s.Start()
48
+ // ...
49
+ err = s.Stop()
51
50
52
- ```go
53
- c, err := p2p.NewExchange[H](
51
+ Exchange Client Usage: To use the exchange client, first create a new instance of the client and start it:
54
52
55
- libp2pHost,
56
- trustedPeers,
57
- libp2pConnGater,
58
- WithChainId(chainID),
53
+ c, err := p2p.NewExchange[H](
54
+ libp2pHost,
55
+ trustedPeers,
56
+ libp2pConnGater,
57
+ WithChainId(chainID),
58
+ )
59
+ err = c.Start()
59
60
60
- )
61
- err = c.Start()
62
- ```
61
+ Then, you can use the various getters to get headers:
63
62
64
- Then, you can use the various getters to get headers:
63
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
64
+ // Get the best head from trusted peers
65
+ head, err := c.Head(ctx)
65
66
66
- ```go
67
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
68
- // Get the best head from trusted peers
69
- head, err := c.Head(ctx)
67
+ // Get a header by height
68
+ header, err := c.GetByHeight(ctx, height)
70
69
71
- // Get a header by height
72
- header , err := c.GetByHeight (ctx, height )
70
+ // Get a range of headers by height
71
+ headers , err := c.GetRangeByHeight (ctx, start, end )
73
72
74
- // Get a range of headers by height
75
- headers, err := c.GetRangeByHeight (ctx, start, end )
73
+ // Get a range of verified headers by height
74
+ headers, err := c.GetVerifiedRange (ctx, header, amount )
76
75
77
- // Get a range of verified headers by height
78
- headers , err := c.GetVerifiedRange (ctx, header, amount )
76
+ // Get a header by hash
77
+ header , err := c.Get (ctx, header.hash )
79
78
80
- // Get a header by hash
81
- header, err := c.Get(ctx, header.hash)
82
- ```
79
+ Subscriber Usage: To use the subscriber, first create a new instance of the subscriber and start it:
83
80
84
- - Subscriber Usage
85
- To use the subscriber, first create a new instance of the subscriber and start it:
81
+ sub := p2p.NewSubscriber[H](
82
+ pubsub, // pubsub.PubSub from libp2p
83
+ msdIDFn, // message ID signing function
84
+ networkID, // network ID
85
+ )
86
+ err := sub.Start()
86
87
87
- ```go
88
- sub := p2p.NewSubscriber[H](
89
-
90
- pubsub, // pubsub.PubSub from libp2p
91
- msdIDFn, // message ID signing function
92
- networkID, // network ID
93
-
94
- )
95
- err := sub.Start()
96
- ```
97
-
98
- Then, you can add validators and subscribe to headers:
99
-
100
- ```go
101
- // Add a validator
88
+ Then, you can add validators and subscribe to headers:
102
89
90
+ // Add a validator
103
91
err := sub.AddValidator(func(ctx context.Context, header H) pubsub.ValidationResult {
104
92
if msg.ValidatorData != nil {
105
93
return true
106
94
}
107
95
return false
108
96
})
109
97
110
- // Subscribe to headers
111
- subscription, err := sub.Subscribe(ctx)
98
+ // Subscribe to headers
99
+ subscription, err := sub.Subscribe(ctx)
112
100
113
- // Keep listening for new headers
101
+ // Keep listening for new headers
114
102
115
- go func() {
116
- for {
117
- select {
118
- case <-ctx.Done():
119
- subscription.Cancel()
120
- return
121
- case header := subscription.NextHeader(ctx):
122
- // Do something with the header
103
+ go func() {
104
+ for {
105
+ select {
106
+ case <-ctx.Done():
107
+ subscription.Cancel()
108
+ return
109
+ case header := subscription.NextHeader(ctx):
110
+ // Do something with the header
111
+ }
123
112
}
124
- }
125
- }()
113
+ }()
126
114
127
- // Broadcast a header
128
- err := sub.Broadcast(header)
129
- ```
115
+ // Broadcast a header
116
+ err := sub.Broadcast(header)
130
117
*/
131
118
package p2p
0 commit comments