-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
237 lines (191 loc) · 5.77 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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/boreq/errors"
"github.com/gorilla/websocket"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/sirupsen/logrus"
"log"
"net"
"strings"
"sync"
"time"
)
var addr = flag.String("addr", "wss://rss.nos.social", "relay address starting with ws:// or wss://")
var author = flag.String("author", "", "author hex or npub")
const (
KindNote = 1
KindLongFormTextContent = 30023
)
func main() {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
t := NewTester(logger)
t.Run()
}
type Tester struct {
eventsReceivedFromSubscriptions map[string]*Subscription
eventsReceivedFromSubscriptionsLock sync.Mutex
subscriptionId int
logger logrus.FieldLogger
}
func NewTester(logger logrus.FieldLogger) *Tester {
return &Tester{
eventsReceivedFromSubscriptions: make(map[string]*Subscription),
logger: logger,
}
}
func (t *Tester) Run() {
flag.Parse()
log.SetFlags(0)
addr := *addr
author := *author
if author == "" {
t.logger.Error("please provide a feed id from this rsslay instance")
return
}
if strings.HasPrefix(author, "npub") {
_, value, err := nip19.Decode(author)
if err != nil {
t.logger.WithError(err).Error("error parsing author npub")
return
}
author = value.(string)
}
t.logger = t.logger.WithField("address", addr)
t.logger.Debug("Connecting to the relay")
c, _, err := websocket.DefaultDialer.Dial(addr, nil)
if err != nil {
t.logger.WithError(err).Error("Error connecting to the relay")
return
}
defer c.Close()
t.logger.Info("✔ Connected to the relay")
go func() {
for {
_, message, err := c.ReadMessage()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
t.logger.WithError(err).Error("read error")
return
}
envelope := nostr.ParseMessage(message)
if envelope == nil {
t.logger.WithField("message", string(message)).Error("unknown message")
continue
}
switch v := envelope.(type) {
case *nostr.EventEnvelope:
t.eventsReceivedFromSubscriptionsLock.Lock()
t.eventsReceivedFromSubscriptions[*v.SubscriptionID].AddEvent(v.Event)
t.eventsReceivedFromSubscriptionsLock.Unlock()
case *nostr.EOSEEnvelope:
t.eventsReceivedFromSubscriptionsLock.Lock()
t.eventsReceivedFromSubscriptions[string(*v)].ReceivedEOSE()
t.eventsReceivedFromSubscriptionsLock.Unlock()
default:
t.logger.WithField("message", envelope).Debug("received a message")
}
}
}()
for _, hexPublicKey := range []string{author} {
if err := t.grabStoredEvents(c, nostr.Filter{Authors: []string{hexPublicKey}}); err != nil {
t.logger.WithError(err).Error("Error getting events from the relay")
return
}
if err := t.grabStoredEvents(c, nostr.Filter{Authors: []string{hexPublicKey}, Kinds: []int{KindNote}}); err != nil {
t.logger.WithError(err).Error("Error getting events from the relay")
return
}
if err := t.grabStoredEvents(c, nostr.Filter{Authors: []string{hexPublicKey}, Kinds: []int{KindLongFormTextContent}}); err != nil {
t.logger.WithError(err).Error("Error getting events from the relay")
return
}
if err := t.grabStoredEvents(c, nostr.Filter{Authors: []string{hexPublicKey}, Kinds: []int{KindNote, KindLongFormTextContent}}); err != nil {
t.logger.WithError(err).Error("Error getting events from the relay")
return
}
}
waitForIters := 5
iter := 0
for {
if ok := t.printEvents(); ok {
break
}
<-time.After(1 * time.Second)
if iter > waitForIters {
t.logger.Error("opened subscriptions didn't receive EOSE on time")
return
}
iter++
}
}
func (t *Tester) printEvents() bool {
t.eventsReceivedFromSubscriptionsLock.Lock()
defer t.eventsReceivedFromSubscriptionsLock.Unlock()
for _, subscription := range t.eventsReceivedFromSubscriptions {
if !subscription.EOSE {
return false
}
}
for subscriptionId, subscription := range t.eventsReceivedFromSubscriptions {
subscriptionId := subscriptionId
subscription := subscription
logger := t.logger.
WithField("subscriptionId", subscriptionId).
WithField("filter", subscription.Filter.String())
if len(subscription.Events) == 0 {
logger.Warning("⚠️ ️Subscription received no events (this can be good or bad, please think about this result)")
}
eventKindToNumberOfEvents := make(map[int]int)
for _, event := range subscription.Events {
eventKindToNumberOfEvents[event.Kind]++
}
for kind, count := range eventKindToNumberOfEvents {
logger.
WithField("eventKind", kind).
WithField("count", count).
Info("✔ ️Subscription received stored events (this can be good or bad, please think about this result)")
}
}
return true
}
func (t *Tester) grabStoredEvents(c *websocket.Conn, filter nostr.Filter) error {
t.eventsReceivedFromSubscriptionsLock.Lock()
defer t.eventsReceivedFromSubscriptionsLock.Unlock()
t.subscriptionId++
subId := fmt.Sprintf("subscription-%d", t.subscriptionId)
filterJSON, err := json.Marshal(filter)
if err != nil {
return errors.Wrap(err, "error marshaling the filter")
}
request := fmt.Sprintf(`["REQ", "subscription-%d", %s]`, t.subscriptionId, string(filterJSON))
err = c.WriteMessage(websocket.TextMessage, []byte(request))
if err != nil {
return errors.Wrap(err, "error writing to socket")
}
t.eventsReceivedFromSubscriptions[subId] = NewSubscription(filter)
t.logger.WithField("request", request).Trace("sent request")
return nil
}
type Subscription struct {
EOSE bool
Events []nostr.Event
Filter nostr.Filter
}
func NewSubscription(filter nostr.Filter) *Subscription {
return &Subscription{
Filter: filter,
}
}
func (s *Subscription) AddEvent(event nostr.Event) {
s.Events = append(s.Events, event)
}
func (s *Subscription) ReceivedEOSE() {
s.EOSE = true
}