-
Notifications
You must be signed in to change notification settings - Fork 5
/
request_blobs.go
192 lines (178 loc) · 5.01 KB
/
request_blobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package p2p
import (
"context"
"fmt"
"strings"
"time"
libp2pcore "github.com/libp2p/go-libp2p/core"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/theQRL/qrysm/v4/beacon-chain/p2p"
p2ptypes "github.com/theQRL/qrysm/v4/beacon-chain/p2p/types"
"github.com/theQRL/qrysm/v4/beacon-chain/sync"
"github.com/theQRL/qrysm/v4/cmd"
"github.com/theQRL/qrysm/v4/config/params"
"github.com/theQRL/qrysm/v4/consensus-types/primitives"
pb "github.com/theQRL/qrysm/v4/proto/prysm/v1alpha1"
"github.com/theQRL/qrysm/v4/time/slots"
"github.com/urfave/cli/v2"
"google.golang.org/protobuf/types/known/emptypb"
)
var requestBlobsFlags = struct {
Peers string
ClientPort uint
APIEndpoints string
StartSlot uint64
Count uint64
}{}
var requestBlobsCmd = &cli.Command{
Name: "blobs-by-range",
Usage: "Request a range of blobs from a beacon node via a p2p connection",
Action: func(cliCtx *cli.Context) error {
if err := cliActionRequestBlobs(cliCtx); err != nil {
log.WithError(err).Fatal("Could not request blobs by range")
}
return nil
},
Flags: []cli.Flag{
cmd.ChainConfigFileFlag,
&cli.StringFlag{
Name: "peer-multiaddrs",
Usage: "comma-separated, peer multiaddr(s) to connect to for p2p requests",
Destination: &requestBlobsFlags.Peers,
Value: "",
},
&cli.UintFlag{
Name: "client-port",
Usage: "port to use for the client as a libp2p host",
Destination: &requestBlobsFlags.ClientPort,
Value: 13001,
},
&cli.StringFlag{
Name: "prysm-api-endpoints",
Usage: "comma-separated, gRPC API endpoint(s) for Prysm beacon node(s)",
Destination: &requestBlobsFlags.APIEndpoints,
Value: "localhost:4000",
},
&cli.Uint64Flag{
Name: "start-slot",
Usage: "start slot for blocks by range request. If unset, will use start_slot(current_epoch-1)",
Destination: &requestBlobsFlags.StartSlot,
Value: 0,
},
&cli.Uint64Flag{
Name: "count",
Usage: "number of blocks to request, (default 32)",
Destination: &requestBlobsFlags.Count,
Value: 32,
},
},
}
func cliActionRequestBlobs(cliCtx *cli.Context) error {
if cliCtx.IsSet(cmd.ChainConfigFileFlag.Name) {
chainConfigFileName := cliCtx.String(cmd.ChainConfigFileFlag.Name)
if err := params.LoadChainConfigFile(chainConfigFileName, nil); err != nil {
return err
}
}
p2ptypes.InitializeDataMaps()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
allAPIEndpoints := make([]string, 0)
if requestBlobsFlags.APIEndpoints != "" {
allAPIEndpoints = strings.Split(requestBlobsFlags.APIEndpoints, ",")
}
var err error
c, err := newClient(allAPIEndpoints, requestBlobsFlags.ClientPort)
if err != nil {
return err
}
defer c.Close()
allPeers := make([]string, 0)
if requestBlobsFlags.Peers != "" {
allPeers = strings.Split(requestBlobsFlags.Peers, ",")
}
if len(allPeers) == 0 {
allPeers, err = c.retrievePeerAddressesViaRPC(ctx, allAPIEndpoints)
if err != nil {
return err
}
}
if len(allPeers) == 0 {
return errors.New("no peers found")
}
log.WithField("peers", allPeers).Info("List of peers")
chain, err := c.initializeMockChainService(ctx)
if err != nil {
return err
}
c.registerHandshakeHandlers()
c.registerRPCHandler(p2p.RPCBlobSidecarsByRangeTopicV1, func(
ctx context.Context, i interface{}, stream libp2pcore.Stream,
) error {
return nil
})
if err := c.connectToPeers(ctx, allPeers...); err != nil {
return errors.Wrap(err, "could not connect to peers")
}
startSlot := primitives.Slot(requestBlobsFlags.StartSlot)
var headSlot *primitives.Slot
if startSlot == 0 {
headResp, err := c.beaconClient.GetChainHead(ctx, &emptypb.Empty{})
if err != nil {
return err
}
startSlot, err = slots.EpochStart(headResp.HeadEpoch.Sub(1))
if err != nil {
return err
}
headSlot = &headResp.HeadSlot
}
// Submit requests.
for _, pr := range c.host.Peerstore().Peers() {
if pr.String() == c.host.ID().String() {
continue
}
req := &pb.BlobSidecarsByRangeRequest{
StartSlot: startSlot,
Count: requestBlobsFlags.Count,
}
fields := logrus.Fields{
"startSlot": startSlot,
"count": requestBlobsFlags.Count,
"peer": pr.String(),
}
if headSlot != nil {
fields["headSlot"] = *headSlot
}
ctxByte, err := sync.ContextByteVersionsForValRoot(chain.genesisValsRoot)
if err != nil {
return err
}
log.WithFields(fields).Info("Blobs by range p2p request to peer")
blobs, err := sync.SendBlobsByRangeRequest(
ctx,
chain,
c,
pr,
ctxByte,
req,
)
if err != nil {
return err
}
for _, b := range blobs {
log.WithFields(logrus.Fields{
"slot": b.Slot,
"index": b.Index,
"commitment": fmt.Sprintf("%#x", b.KzgCommitment),
"kzgProof": fmt.Sprintf("%#x", b.KzgProof),
}).Info("Received blob sidecar")
}
log.WithFields(logrus.Fields{
"numBlobs": len(blobs),
"peer": pr.String(),
}).Info("Received blob sidecars from peer")
}
return nil
}