Skip to content

Commit 412018d

Browse files
committed
feat: add more details + correct markdown for docs
1 parent 4c9f1fd commit 412018d

File tree

1 file changed

+61
-74
lines changed

1 file changed

+61
-74
lines changed

p2p/doc.go

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ Exchange Server, Exchange Client and GossipSub Subscriber for header exchange.
1616
On start the client connects to trusted peers (bootstrappers) as well as kicks off tracking and garbage collection routines for peer connections.
1717
The getters it defines are several and are used to get headers by height, range, and hash.
1818
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.
1926
2027
- Subscriber:
2128
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.
2936
3037
# Usage Examples
3138
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:
4941
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()
5150
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:
5452
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()
5960
60-
)
61-
err = c.Start()
62-
```
61+
Then, you can use the various getters to get headers:
6362
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)
6566
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)
7069
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)
7372
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)
7675
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)
7978
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:
8380
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()
8687
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:
10289
90+
// Add a validator
10391
err := sub.AddValidator(func(ctx context.Context, header H) pubsub.ValidationResult {
10492
if msg.ValidatorData != nil {
10593
return true
10694
}
10795
return false
10896
})
10997
110-
// Subscribe to headers
111-
subscription, err := sub.Subscribe(ctx)
98+
// Subscribe to headers
99+
subscription, err := sub.Subscribe(ctx)
112100
113-
// Keep listening for new headers
101+
// Keep listening for new headers
114102
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+
}
123112
}
124-
}
125-
}()
113+
}()
126114
127-
// Broadcast a header
128-
err := sub.Broadcast(header)
129-
```
115+
// Broadcast a header
116+
err := sub.Broadcast(header)
130117
*/
131118
package p2p

0 commit comments

Comments
 (0)