-
Notifications
You must be signed in to change notification settings - Fork 34
/
service.go
97 lines (85 loc) · 2.6 KB
/
service.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
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
// Package crawl holds the eth2 node discovery utilities
package crawl
import (
"context"
"crypto/ecdsa"
"eth2-crawler/store/peerstore"
"eth2-crawler/store/record"
"fmt"
"net"
"github.com/robfig/cron/v3"
"eth2-crawler/crawler/p2p"
ipResolver "eth2-crawler/resolver"
"github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p"
ic "github.com/libp2p/go-libp2p-core/crypto"
noise "github.com/libp2p/go-libp2p-noise"
"github.com/libp2p/go-tcp-transport"
ma "github.com/multiformats/go-multiaddr"
)
// listenConfig holds configuration for running v5discovry node
type listenConfig struct {
bootNodeAddrs []string
listenAddress net.IP
listenPORT int
dbPath string
privateKey *ecdsa.PrivateKey
}
// Initialize initializes the core crawler component
func Initialize(peerStore peerstore.Provider, historyStore record.Provider, ipResolver ipResolver.Provider, bootNodeAddrs []string) error {
ctx := context.Background()
pkey, _ := crypto.GenerateKey()
listenCfg := &listenConfig{
bootNodeAddrs: bootNodeAddrs,
listenAddress: net.IPv4zero,
listenPORT: 30304,
dbPath: "",
privateKey: pkey,
}
disc, err := startV5(listenCfg)
if err != nil {
return err
}
listenAddrs, err := multiAddressBuilder(listenCfg.listenAddress, listenCfg.listenPORT)
if err != nil {
return err
}
host, err := p2p.NewHost(
libp2p.Identity(convertToInterfacePrivkey(listenCfg.privateKey)),
libp2p.ListenAddrs(listenAddrs),
libp2p.UserAgent("Eth2-Crawler"),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Security(noise.ID, noise.New),
libp2p.NATPortMap(),
)
if err != nil {
return err
}
c := newCrawler(disc, peerStore, historyStore, ipResolver, listenCfg.privateKey, disc.RandomNodes(), host, 200)
go c.start(ctx)
// scheduler for updating peer
go c.updatePeer(ctx)
// add scheduler for updating history store
scheduler := cron.New()
_, err = scheduler.AddFunc("@daily", c.insertToHistory)
if err != nil {
return err
}
scheduler.Start()
return nil
}
func convertToInterfacePrivkey(privkey *ecdsa.PrivateKey) ic.PrivKey {
typeAssertedKey := ic.PrivKey((*ic.Secp256k1PrivateKey)(privkey))
return typeAssertedKey
}
func multiAddressBuilder(ipAddr net.IP, port int) (ma.Multiaddr, error) {
if ipAddr.To4() == nil && ipAddr.To16() == nil {
return nil, fmt.Errorf("invalid ip address provided: %s", ipAddr)
}
if ipAddr.To4() != nil {
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr.String(), port))
}
return ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/%d", ipAddr.String(), port))
}