-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
driver_crawler.go
204 lines (172 loc) 路 5.32 KB
/
driver_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
package libp2p
import (
"fmt"
"runtime"
"time"
"github.com/libp2p/go-libp2p"
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
ma "github.com/multiformats/go-multiaddr"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"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/kubo"
"github.com/dennis-tra/nebula-crawler/utils"
)
// Host is the interface that's required for crawling libp2p peers. Actually
// the *basichost.Host is required but to allow testing we define this interface
// here. That allows us to inject a mock host.
type Host interface {
host.Host
IDService() identify.IDService
}
type PeerInfo struct {
peer.AddrInfo
}
var _ core.PeerInfo[PeerInfo] = (*PeerInfo)(nil)
func (p PeerInfo) ID() peer.ID {
return p.AddrInfo.ID
}
func (p PeerInfo) Addrs() []ma.Multiaddr {
return p.AddrInfo.Addrs
}
func (p PeerInfo) Merge(other PeerInfo) PeerInfo {
if p.AddrInfo.ID != other.AddrInfo.ID {
panic("merge peer ID mismatch")
}
return PeerInfo{
AddrInfo: peer.AddrInfo{
ID: p.AddrInfo.ID,
Addrs: utils.MergeMaddrs(p.AddrInfo.Addrs, other.AddrInfo.Addrs),
},
}
}
type CrawlDriverConfig struct {
Version string
Network config.Network
Protocols []string
DialTimeout time.Duration
TrackNeighbors bool
CheckExposed bool
BootstrapPeers []peer.AddrInfo
AddrTrackType config.AddrType
AddrDialType config.AddrType
MeterProvider metric.MeterProvider
TracerProvider trace.TracerProvider
LogErrors bool
}
func (cfg *CrawlDriverConfig) CrawlerConfig() *CrawlerConfig {
crawlerCfg := DefaultCrawlerConfig()
crawlerCfg.TrackNeighbors = cfg.TrackNeighbors
crawlerCfg.DialTimeout = cfg.DialTimeout
crawlerCfg.CheckExposed = cfg.CheckExposed
crawlerCfg.AddrDialType = cfg.AddrDialType
crawlerCfg.LogErrors = cfg.LogErrors
return crawlerCfg
}
func (cfg *CrawlDriverConfig) WriterConfig() *core.CrawlWriterConfig {
return &core.CrawlWriterConfig{
AddrTrackType: cfg.AddrTrackType,
}
}
type CrawlDriver struct {
cfg *CrawlDriverConfig
hosts []Host
dbc db.Client
dbCrawl *models.Crawl
tasksChan chan PeerInfo
crawlerCount int
writerCount int
}
var _ core.Driver[PeerInfo, core.CrawlResult[PeerInfo]] = (*CrawlDriver)(nil)
func NewCrawlDriver(dbc db.Client, dbCrawl *models.Crawl, cfg *CrawlDriverConfig) (*CrawlDriver, error) {
// The Avail light clients verify the agent version:
// https://github.com/availproject/avail-light/blob/0ddc5d50d6f3d7217c448d6d008846c6b8c4fec3/src/network/p2p/event_loop.rs#L296
// Spoof it
userAgent := "nebula/" + cfg.Version
if cfg.Network == config.NetworkGoldbergLC {
userAgent = "avail-light-client/rust-client"
}
hosts := make([]Host, 0, runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
h, err := newLibp2pHost(userAgent)
if err != nil {
return nil, fmt.Errorf("new libp2p host: %w", err)
}
hosts = append(hosts, h)
}
tasksChan := make(chan PeerInfo, len(cfg.BootstrapPeers))
for _, addrInfo := range cfg.BootstrapPeers {
addrInfo := addrInfo
tasksChan <- PeerInfo{AddrInfo: addrInfo}
}
close(tasksChan)
return &CrawlDriver{
cfg: cfg,
hosts: hosts,
dbc: dbc,
dbCrawl: dbCrawl,
tasksChan: tasksChan,
crawlerCount: 0,
writerCount: 0,
}, nil
}
func (d *CrawlDriver) NewWorker() (core.Worker[PeerInfo, core.CrawlResult[PeerInfo]], error) {
h := d.hosts[d.crawlerCount%len(d.hosts)]
ms := &msgSender{
h: h,
protocols: protocol.ConvertFromStrings(d.cfg.Protocols),
timeout: d.cfg.DialTimeout,
}
pm, err := pb.NewProtocolMessenger(ms)
if err != nil {
return nil, err
}
c := &Crawler{
id: fmt.Sprintf("crawler-%02d", d.crawlerCount),
host: h,
pm: pm,
cfg: d.cfg.CrawlerConfig(),
client: kubo.NewClient(),
}
d.crawlerCount += 1
return c, nil
}
func (d *CrawlDriver) NewWriter() (core.Worker[core.CrawlResult[PeerInfo], core.WriteResult], error) {
w := core.NewCrawlWriter[PeerInfo](fmt.Sprintf("writer-%02d", d.writerCount), d.dbc, d.dbCrawl.ID, d.cfg.WriterConfig())
d.writerCount += 1
return w, nil
}
func (d *CrawlDriver) Tasks() <-chan PeerInfo {
return d.tasksChan
}
func (d *CrawlDriver) Close() {}
func newLibp2pHost(userAgent string) (Host, error) {
// Configure the resource manager to not limit anything
limiter := rcmgr.NewFixedLimiter(rcmgr.InfiniteLimits)
rm, err := rcmgr.NewResourceManager(limiter)
if err != nil {
return nil, fmt.Errorf("new resource manager: %w", err)
}
// Don't use a connection manager that could potentially
// prune any connections. We _theoretically_ clean up after
// // ourselves.
cm := connmgr.NullConnMgr{}
// Initialize a single libp2p node that's shared between all crawlers.
h, err := libp2p.New(
libp2p.UserAgent(userAgent),
libp2p.ResourceManager(rm),
libp2p.ConnectionManager(cm),
libp2p.DisableMetrics(),
libp2p.EnableRelay(), // enable the relay transport
)
return h.(Host), err
}