-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
419 lines (353 loc) · 9.99 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"os"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/rodaine/table"
cli "github.com/urfave/cli/v2"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/store"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Options struct {
NodeKey *ecdsa.PrivateKey
ClusterID uint
PubSubTopic string
ContentTopics cli.StringSlice
StartTime int64
EndTime int64
Hashes cli.StringSlice
AdvanceCursor bool
PageSize uint64
Forward bool
StoreNode *multiaddr.Multiaddr
UseLegacy bool
QueryTimeout time.Duration
LogLevel string
LogEncoding string
LogOutput string
}
var options Options
func main() {
// Defaults
options.LogLevel = "INFO"
app := &cli.App{
Name: "query",
Version: "0.0.1",
Flags: []cli.Flag{
NodeKey,
ClusterID,
PubsubTopic,
ContentTopic,
StartTime,
EndTime,
Hashes,
Pagesize,
Forward,
Storenode,
UseLegacy,
Timeout,
LogLevel,
LogEncoding,
LogOutput,
},
Action: func(c *cli.Context) error {
if len(options.Hashes.Value()) == 1 {
err := FetchMessage(c.Context, options)
if err != nil {
return cli.Exit(err.Error(), 1)
}
} else {
err := QueryMessages(c.Context, options)
if err != nil {
return cli.Exit(err.Error(), 1)
}
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func initializeWaku(opts Options) (*node.WakuNode, error) {
utils.InitLogger(opts.LogEncoding, opts.LogOutput, "query")
var prvKey *ecdsa.PrivateKey
var err error
if opts.NodeKey != nil {
prvKey = opts.NodeKey
} else {
if prvKey, err = crypto.GenerateKey(); err != nil {
return nil, fmt.Errorf("error generating key: %w", err)
}
}
p2pPrvKey := utils.EcdsaPrivKeyToSecp256k1PrivKey(prvKey)
id, err := peer.IDFromPublicKey(p2pPrvKey.GetPublic())
if err != nil {
return nil, err
}
logger := utils.Logger().With(logging.HostID("node", id))
lvl, err := zapcore.ParseLevel(opts.LogLevel)
if err != nil {
return nil, err
}
libp2pOpts := append(node.DefaultLibP2POptions, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.)
wakuNode, err := node.New(
node.WithLogger(logger),
node.WithLogLevel(lvl),
node.WithPrivateKey(prvKey),
node.WithClusterID(uint16(options.ClusterID)),
node.WithNTP(),
node.WithLibP2POptions(libp2pOpts...),
)
if err != nil {
return nil, fmt.Errorf("could not instantiate waku: %w", err)
}
return wakuNode, nil
}
func FetchMessage(ctx context.Context, opts Options) error {
wakuNode, err := initializeWaku(opts)
if err != nil {
return err
}
if err = wakuNode.Start(ctx); err != nil {
return err
}
defer wakuNode.Stop()
h, err := hexutil.Decode(opts.Hashes.Value()[0])
if err != nil {
return fmt.Errorf("invalid message hash: %s", opts.Hashes.Value()[0])
}
ctx, cancel := context.WithTimeout(context.Background(), options.QueryTimeout)
result, err := wakuNode.Store().Request(ctx, store.MessageHashCriteria{MessageHashes: []pb.MessageHash{pb.ToMessageHash(h)}},
store.WithPeerAddr(*options.StoreNode),
store.WithPaging(options.Forward, options.PageSize),
)
cancel()
if err != nil {
return err
}
if len(result.Messages()) == 0 {
fmt.Println("Message not found")
return nil
}
fmt.Println()
msg := result.Messages()[0]
fmt.Println("PubsubTopic:", msg.GetPubsubTopic())
fmt.Println("MessageHash:", msg.WakuMessageHash())
fmt.Println("ContentTopic:", msg.Message.ContentTopic)
if msg.Message.Timestamp == nil {
fmt.Println("Timestamp: <nil>")
fmt.Println("Timestamp (unix nano): <nil>")
} else {
fmt.Println("Timestamp:", time.Unix(0, msg.Message.GetTimestamp()).UTC())
fmt.Println("Timestamp (unix nano):", msg.Message.GetTimestamp())
}
if msg.Message.Version == nil {
fmt.Println("Version: <nil>")
} else {
fmt.Println("Version:", *msg.Message.Version)
}
if len(msg.Message.Payload) != 0 {
fmt.Printf("Payload: (%d bytes)\n", len(msg.Message.Payload))
fmt.Print(hex.Dump(msg.Message.Payload))
} else {
fmt.Println("Payload: <nil>")
}
if len(msg.Message.Meta) != 0 {
fmt.Println("Meta:")
fmt.Print(hex.Dump(msg.Message.Meta))
} else {
fmt.Println("Meta: <nil>")
}
if len(msg.Message.RateLimitProof) != 0 {
fmt.Println("RateLimitProof:")
fmt.Print(hex.Dump(msg.Message.RateLimitProof))
} else {
fmt.Println("RateLimitProof: <nil>")
}
return nil
}
func QueryMessages(ctx context.Context, opts Options) error {
wakuNode, err := initializeWaku(opts)
if err != nil {
return err
}
if err = wakuNode.Start(ctx); err != nil {
return err
}
defer wakuNode.Stop()
var hashes []pb.MessageHash
if len(options.Hashes.Value()) != 0 {
if options.PubSubTopic != "" || len(options.ContentTopics.Value()) != 0 || options.StartTime != 0 || options.EndTime != 0 {
return errors.New("cannot specify pubsub topic / content topics / start time / end time if using the --hash flag")
}
if options.UseLegacy {
return errors.New("cannot use legacy store with the --hash flag")
}
for _, hash := range options.Hashes.Value() {
h, err := hexutil.Decode(hash)
if err != nil {
return fmt.Errorf("invalid message hash: %s", hash)
}
hashes = append(hashes, pb.ToMessageHash(h))
}
}
var StartTime *int64
if options.StartTime > 0 {
StartTime = &options.StartTime
}
var EndTime *int64
if options.EndTime > 0 {
EndTime = &options.EndTime
}
cnt := 0
if !options.UseLegacy {
var criteria store.Criteria
if len(hashes) == 0 {
criteria = store.FilterCriteria{
ContentFilter: protocol.NewContentFilter(options.PubSubTopic, options.ContentTopics.Value()...),
TimeStart: StartTime,
TimeEnd: EndTime,
}
} else {
criteria = store.MessageHashCriteria{
MessageHashes: hashes,
}
}
now := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), options.QueryTimeout)
result, err := wakuNode.Store().Request(ctx, criteria,
store.WithPeerAddr(*options.StoreNode),
store.WithPaging(false, options.PageSize),
)
ellapsed := time.Since(now)
cancel()
if err != nil {
return err
}
pageCount := 0
if len(result.Messages()) == 0 {
fmt.Printf("No messages found (%v)\n", ellapsed)
return nil
}
fmt.Println()
for !result.IsComplete() {
if len(result.Messages()) == 0 {
break
}
pageCount++
cnt += len(result.Messages())
headers := []interface{}{"MessageHash"}
if len(hashes) != 0 {
headers = append(headers, "PubsubTopic")
}
headers = append(headers, "Content Topic", "Timestamp", "")
tbl := table.New(headers...)
for _, msg := range result.Messages() {
unixTime := "<nil>"
readableTime := "<nil>"
if msg.Message.Timestamp != nil {
unixTime = fmt.Sprintf("%d", msg.Message.GetTimestamp())
readableTime = time.Unix(0, msg.Message.GetTimestamp()).UTC().String()
}
var cols []interface{} = []interface{}{msg.WakuMessageHash()}
if len(hashes) != 0 {
cols = append(cols, msg.GetPubsubTopic())
}
cols = append(cols, msg.Message.ContentTopic, unixTime, readableTime)
tbl.AddRow(cols...)
}
fmt.Printf("Page: %d, Record from %d to %d (%v)\n", pageCount, cnt-len(result.Messages())+1, cnt, ellapsed)
tbl.Print()
fmt.Println()
if result.Cursor() != nil {
fmt.Printf("Cursor: %s\n", hex.EncodeToString(result.Cursor()))
}
fmt.Println()
now = time.Now()
ctx, cancel := context.WithTimeout(context.Background(), options.QueryTimeout)
err := result.Next(ctx)
ellapsed = time.Since(now)
cancel()
if err != nil {
return err
}
}
} else {
query := legacy_store.Query{
PubsubTopic: options.PubSubTopic,
ContentTopics: options.ContentTopics.Value(),
StartTime: StartTime,
EndTime: EndTime,
}
now := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), options.QueryTimeout)
result, err := wakuNode.LegacyStore().Query(ctx, query,
legacy_store.WithPeerAddr(*options.StoreNode),
legacy_store.WithPaging(false, options.PageSize),
)
ellapsed := time.Since(now)
cancel()
if err != nil {
return err
}
if len(result.Messages) == 0 {
fmt.Printf("No messages found (%v)\n", ellapsed)
return nil
}
fmt.Println()
pageCount := 0
for {
pageCount++
now = time.Now()
ctx, cancel := context.WithTimeout(context.Background(), options.QueryTimeout)
hasNext, err := result.Next(ctx)
ellapsed = time.Since(now)
cancel()
if err != nil {
return err
}
if !hasNext { // No more messages available
break
}
cnt += len(result.GetMessages())
tbl := table.New("MessageHash", "PubsubTopic", "Content Topic", "Timestamp", "")
for _, msg := range result.GetMessages() {
env := protocol.NewEnvelope(msg, msg.GetTimestamp(), query.PubsubTopic)
unixTime := "<nil>"
readableTime := "<nil>"
if msg.Timestamp != nil {
unixTime = fmt.Sprintf("%d", msg.GetTimestamp())
readableTime = time.Unix(0, msg.GetTimestamp()).UTC().String()
}
tbl.AddRow(env.Hash(), env.PubsubTopic(), env.Message().ContentTopic, unixTime, readableTime)
}
fmt.Printf("Page: %d, Record from %d to %d (%v)\n", pageCount, cnt-len(result.GetMessages())+1, cnt, ellapsed)
tbl.Print()
fmt.Println()
if result.Cursor() != nil {
fmt.Printf("Cursor: Digest(%s); ReceiverTime:%d, SenderTime: %d, PubsubTopic: %s\n", hex.EncodeToString(result.Cursor().Digest), result.Cursor().ReceiverTime, result.Cursor().SenderTime, result.Cursor().PubsubTopic)
}
fmt.Println()
}
}
utils.Logger().Info("Total messages retrieved", zap.Int("num", cnt))
return nil
}