1
1
package main
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
- "crypto/rand"
6
6
"fmt"
7
- "sync"
8
7
"time"
9
8
10
9
"github.com/libp2p/go-libp2p/core/peer"
11
10
"github.com/multiformats/go-multiaddr"
11
+ "github.com/waku-org/go-waku/logging"
12
12
"github.com/waku-org/go-waku/waku/v2/node"
13
- "github.com/waku-org/go-waku/waku/v2/protocol/pb "
13
+ "github.com/waku-org/go-waku/waku/v2/peers "
14
14
"github.com/waku-org/go-waku/waku/v2/protocol/store"
15
15
"go.uber.org/zap"
16
16
)
17
17
18
- func connectToNodes (ctx context.Context , node * node.WakuNode ) {
19
- wg := sync.WaitGroup {}
18
+ func addNodes (ctx context.Context , node * node.WakuNode ) {
20
19
for _ , addr := range nodeList {
21
- wg .Add (1 )
22
- go func (addr string ) {
23
- wg .Done ()
24
- ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
25
- defer cancel ()
26
- err := node .DialPeer (ctx , addr )
27
- if err != nil {
28
- log .Error ("could not connect to peer" , zap .String ("addr" , addr ), zap .Error (err ))
29
- }
30
- }(addr )
31
- }
32
- wg .Wait ()
33
- }
34
-
35
- func sendMessages (ctx context.Context , node * node.WakuNode , numMsgToSend int , topic string , contentTopic string ) error {
36
- for i := 0 ; i < numMsgToSend ; i ++ {
37
- payload := make ([]byte , 128 )
38
- _ , err := rand .Read (payload )
20
+ ma , err := multiaddr .NewMultiaddr (addr )
39
21
if err != nil {
40
- panic (err )
22
+ log .Error ("invalid multiaddress" , zap .Error (err ), zap .String ("addr" , addr ))
23
+ continue
41
24
}
42
25
43
- msg := & pb.WakuMessage {
44
- Payload : payload ,
45
- Version : 0 ,
46
- ContentTopic : contentTopic ,
47
- Timestamp : node .Timesource ().Now ().UnixNano (),
48
- }
49
-
50
- _ , err = node .Relay ().Publish (ctx , msg )
26
+ _ , err = node .AddPeer (ma , peers .Static , store .StoreID_v20beta4 )
51
27
if err != nil {
52
- panic (err )
28
+ log .Error ("could not add peer" , zap .Error (err ), zap .Stringer ("addr" , ma ))
29
+ continue
53
30
}
54
- time .Sleep (10 * time .Millisecond )
55
31
}
56
- return nil
57
32
}
58
33
59
- func sendMessagesConcurrent (ctx context.Context , node * node.WakuNode , numMsgToSend int , topic string , contentTopic string ) error {
60
- wg := sync.WaitGroup {}
61
- for i := 0 ; i < numMsgToSend ; i ++ {
62
- wg .Add (1 )
63
- go func () {
64
- wg .Done ()
65
- payload := make ([]byte , 128 )
66
- _ , err := rand .Read (payload )
67
- if err != nil {
68
- panic (err )
69
- }
70
-
71
- msg := & pb.WakuMessage {
72
- Payload : payload ,
73
- Version : 0 ,
74
- ContentTopic : contentTopic ,
75
- Timestamp : node .Timesource ().Now ().UnixNano (),
76
- }
77
-
78
- _ , err = node .Relay ().Publish (ctx , msg )
79
- if err != nil {
80
- panic (err )
81
- }
82
- }()
83
- time .Sleep (10 * time .Millisecond )
84
- }
85
- wg .Wait ()
86
- return nil
87
- }
88
-
89
- func queryNode (ctx context.Context , node * node.WakuNode , addr string , pubsubTopic string , contentTopic string , startTime time.Time , endTime time.Time ) (int , error ) {
34
+ func queryNode (ctx context.Context , node * node.WakuNode , addr string , pubsubTopic string , contentTopics []string , startTime time.Time , endTime time.Time , envelopeHash []byte ) (int , error ) {
90
35
p , err := multiaddr .NewMultiaddr (addr )
91
36
if err != nil {
92
37
return - 1 , err
@@ -102,7 +47,7 @@ func queryNode(ctx context.Context, node *node.WakuNode, addr string, pubsubTopi
102
47
103
48
result , err := node .Store ().Query (ctx , store.Query {
104
49
Topic : pubsubTopic ,
105
- ContentTopics : [] string { contentTopic } ,
50
+ ContentTopics : contentTopics ,
106
51
StartTime : startTime .UnixNano (),
107
52
EndTime : endTime .UnixNano (),
108
53
}, store .WithPeer (info .ID ), store .WithPaging (false , 100 ), store .WithRequestId ([]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }))
@@ -121,6 +66,14 @@ func queryNode(ctx context.Context, node *node.WakuNode, addr string, pubsubTopi
121
66
}
122
67
cursorIterations += 1
123
68
69
+ // uncomment to find message by ID
70
+ for _ , m := range result .GetMessages () {
71
+ if len (envelopeHash ) != 0 && bytes .Equal (m .Hash (pubsubTopic ), envelopeHash ) {
72
+ log .Info ("MESSAGE FOUND!" , logging .HexString ("envelopeHash" , envelopeHash ), logging .HostID ("peerID" , info .ID ))
73
+ return 0 , nil
74
+ }
75
+ }
76
+
124
77
cnt += len (result .GetMessages ())
125
78
}
126
79
0 commit comments