/
crawler.go
315 lines (257 loc) 路 7.83 KB
/
crawler.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
package discv4
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p/core/peer"
log "github.com/sirupsen/logrus"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
"github.com/dennis-tra/nebula-crawler/config"
"github.com/dennis-tra/nebula-crawler/core"
"github.com/dennis-tra/nebula-crawler/db"
"github.com/dennis-tra/nebula-crawler/db/models"
"github.com/dennis-tra/nebula-crawler/devp2p"
"github.com/dennis-tra/nebula-crawler/discvx"
)
type CrawlerConfig struct {
DialTimeout time.Duration
AddrDialType config.AddrType
LogErrors bool
}
type Crawler struct {
id string
cfg *CrawlerConfig
listener *discvx.UDPv4
client *devp2p.Client
crawledPeers int
done chan struct{}
}
var _ core.Worker[PeerInfo, core.CrawlResult[PeerInfo]] = (*Crawler)(nil)
func (c *Crawler) Work(ctx context.Context, task PeerInfo) (core.CrawlResult[PeerInfo], error) {
logEntry := log.WithFields(log.Fields{
"crawlerID": c.id,
"remoteID": task.peerID.ShortString(),
"crawlCount": c.crawledPeers,
})
logEntry.Debugln("Crawling peer")
defer logEntry.Debugln("Crawled peer")
crawlStart := time.Now()
discv4ResultCh := c.crawlDiscV4(ctx, task)
devp2pResultCh := c.crawlDevp2p(ctx, task)
discv4Result := <-discv4ResultCh
devp2pResult := <-devp2pResultCh
properties := map[string]any{}
// keep track of all unknown connection errors
if devp2pResult.ConnectErrorStr == models.NetErrorUnknown && devp2pResult.ConnectError != nil {
properties["connect_error"] = devp2pResult.ConnectError.Error()
}
// keep track of all unknown crawl errors
if discv4Result.ErrorStr == models.NetErrorUnknown && discv4Result.Error != nil {
properties["crawl_error"] = discv4Result.Error.Error()
}
data, err := json.Marshal(properties)
if err != nil {
log.WithError(err).WithField("properties", properties).Warnln("Could not marshal peer properties")
}
cr := core.CrawlResult[PeerInfo]{
CrawlerID: c.id,
Info: task,
CrawlStartTime: crawlStart,
RoutingTableFromAPI: false,
RoutingTable: discv4Result.RoutingTable,
Agent: devp2pResult.Agent,
Protocols: devp2pResult.Protocols,
ConnectError: devp2pResult.ConnectError,
ConnectErrorStr: devp2pResult.ConnectErrorStr,
CrawlError: discv4Result.Error,
CrawlErrorStr: discv4Result.ErrorStr,
CrawlEndTime: time.Now(),
ConnectStartTime: devp2pResult.ConnectStartTime,
ConnectEndTime: devp2pResult.ConnectEndTime,
Properties: data,
LogErrors: c.cfg.LogErrors,
}
// We've now crawled this peer, so increment
c.crawledPeers++
return cr, nil
}
type DiscV4Result struct {
// The time we received the first successful response
RespondedAt *time.Time
// The updated ethereum node record
ENR *enode.Node
// The neighbors of the crawled peer
RoutingTable *core.RoutingTable[PeerInfo]
// The time the draining of bucket entries was finished
DoneAt time.Time
// The combined error of crawling the peer's buckets
Error error
// The above error mapped to a known string
ErrorStr string
}
func (c *Crawler) crawlDiscV4(ctx context.Context, pi PeerInfo) <-chan DiscV4Result {
resultCh := make(chan DiscV4Result)
go func() {
// mutex to guard access to result and allNeighbors
mu := sync.RWMutex{}
// the final result struct
result := DiscV4Result{}
// all neighbors of pi. We're using a map to deduplicate.
allNeighbors := map[string]PeerInfo{}
// errorBits tracks at which CPL errors have occurred.
// 0000 0000 0000 0000 - No error
// 0000 0000 0000 0001 - An error has occurred at CPL 0
// 1000 0000 0000 0001 - An error has occurred at CPL 0 and 15
errorBits := atomic.NewUint32(0)
enr, err := c.listener.RequestENR(pi.Node)
if err != nil {
result.ENR = pi.Node
} else {
result.ENR = enr
now := time.Now()
result.RespondedAt = &now
}
errg := errgroup.Group{}
for i := 0; i <= 15; i++ { // 15 is maximum
count := i // Copy value
errg.Go(func() error {
pubKey, err := discvx.GenRandomPublicKey(pi.Node.ID(), count)
if err != nil {
log.WithError(err).WithField("enr", pi.Node.String()).Warnln("Failed generating public key")
errorBits.Add(1 << count)
return fmt.Errorf("generating random public key with CPL %d: %w", count, err)
}
udpAddr := &net.UDPAddr{IP: pi.Node.IP(), Port: pi.Node.UDP()}
var neighbors []*enode.Node
for retry := 0; retry < 2; retry++ {
neighbors, err = c.listener.FindNode(pi.Node.ID(), udpAddr, pubKey)
if err == nil {
break
}
errorBits.Add(1 << count)
if errors.Is(err, discvx.ErrTimeout) {
sleepDur := time.Second * time.Duration(3*(retry+1))
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(sleepDur): // may add jitter here
continue
}
}
errorBits.Add(1 << count)
return fmt.Errorf("getting closest peer with CPL %d: %w", count, err)
}
mu.Lock()
defer mu.Unlock()
if result.RespondedAt == nil {
now := time.Now()
result.RespondedAt = &now
}
for _, n := range neighbors {
npi, err := NewPeerInfo(n)
if err != nil {
log.WithError(err).Warnln("Failed parsing ethereum node neighbor")
continue
}
allNeighbors[string(npi.peerID)] = npi
}
if err != nil {
errorBits.Add(1 << count)
return err
}
return nil
})
}
// wait for go routines to finish
err = errg.Wait()
// track done timestamp and error
result.DoneAt = time.Now()
result.Error = err
result.RoutingTable = &core.RoutingTable[PeerInfo]{
PeerID: pi.ID(),
Neighbors: []PeerInfo{},
ErrorBits: uint16(errorBits.Load()),
Error: err,
}
for _, n := range allNeighbors {
result.RoutingTable.Neighbors = append(result.RoutingTable.Neighbors, n)
}
// if there was a connection error, parse it to a known one
if result.Error != nil {
result.ErrorStr = db.NetError(result.Error)
}
// send the result back and close channel
select {
case resultCh <- result:
case <-ctx.Done():
}
close(resultCh)
}()
return resultCh
}
type Devp2pResult struct {
ConnectStartTime time.Time
ConnectEndTime time.Time
ConnectError error
ConnectErrorStr string
Agent string
Protocols []string
}
func (c *Crawler) crawlDevp2p(ctx context.Context, pi PeerInfo) <-chan Devp2pResult {
resultCh := make(chan Devp2pResult)
go func() {
// the final result struct
result := Devp2pResult{}
addrInfo := peer.AddrInfo{
ID: pi.ID(),
Addrs: pi.Addrs(),
}
result.ConnectStartTime = time.Now()
for retry := 0; retry < 3; retry++ {
result.ConnectError = c.client.Connect(ctx, addrInfo)
if result.ConnectError == nil {
break
}
if strings.Contains(result.ConnectError.Error(), "handshake failed: EOF") {
time.Sleep(time.Second)
continue
}
}
result.ConnectEndTime = time.Now()
if result.ConnectError == nil {
resp, err := c.client.Identify(pi.ID())
if err == nil {
result.Agent = resp.Name
protocols := make([]string, len(resp.Caps))
for i, c := range resp.Caps {
protocols[i] = "/" + c.String()
}
result.Protocols = protocols
} else {
log.WithError(err).Debugln("Could not identify peer")
}
}
// if there was a connection error, parse it to a known one
if result.ConnectError != nil {
result.ConnectErrorStr = db.NetError(result.ConnectError)
}
// Free connection resources
if err := c.client.CloseConn(pi.ID()); err != nil {
log.WithError(err).WithField("remoteID", pi.ID().ShortString()).Warnln("Could not close connection to peer")
}
// send the result back and close channel
select {
case resultCh <- result:
case <-ctx.Done():
}
close(resultCh)
}()
return resultCh
}