forked from ethereum/node-crawler
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.go
164 lines (150 loc) · 3.12 KB
/
db.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
package main
import (
"bytes"
"database/sql"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enr"
beacon "github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/codec"
"github.com/oschwald/geoip2-golang"
)
func updateNodes(db *sql.DB, geoipDB *geoip2.Reader, nodes []nodeJSON) error {
log.Info("Writing nodes to db", "nodes", len(nodes))
now := time.Now()
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(
`insert into nodes(ID,
Now,
ClientType,
PK,
SoftwareVersion,
Capabilities,
NetworkID,
ForkID,
Blockheight,
TotalDifficulty,
HeadHash,
IP,
Country,
City,
Coordinates,
FirstSeen,
LastSeen,
Seq,
Score,
ConnType)
values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
if err != nil {
return err
}
defer stmt.Close()
for _, n := range nodes {
info := &clientInfo{}
if n.Info != nil {
info = n.Info
}
if info.ClientType == "" && n.TooManyPeers {
info.ClientType = "tmp"
}
connType := ""
var portUDP enr.UDP
if n.N.Load(&portUDP) == nil {
connType = "UDP"
}
var portTCP enr.TCP
if n.N.Load(&portTCP) == nil {
connType = "TCP"
}
fid := fmt.Sprintf("Hash: %v, Next %v", info.ForkID.Hash, info.ForkID.Next)
var eth2 ETH2
if n.N.Load(ð2) == nil {
info.ClientType = "eth2"
var dat beacon.Eth2Data
if err := dat.Deserialize(codec.NewDecodingReader(bytes.NewReader(eth2), uint64(len(eth2)))); err == nil {
fid = fmt.Sprintf("Hash: %v, Next %v", dat.ForkDigest, dat.NextForkEpoch)
}
}
var caps string
for _, c := range info.Capabilities {
caps = fmt.Sprintf("%v, %v", caps, c.String())
}
var pk string
if n.N.Pubkey() != nil {
pk = fmt.Sprintf("X: %v, Y: %v", n.N.Pubkey().X.String(), n.N.Pubkey().Y.String())
}
var country, city, loc string
if geoipDB != nil {
// parse GeoIp info
ipRecord, err := geoipDB.City(n.N.IP())
if err != nil {
return err
}
country, city, loc =
ipRecord.Country.Names["en"],
ipRecord.City.Names["en"],
fmt.Sprintf("%v,%v", ipRecord.Location.Latitude, ipRecord.Location.Longitude)
}
_, err = stmt.Exec(
n.N.ID().String(),
now.String(),
info.ClientType,
pk,
info.SoftwareVersion,
caps,
info.NetworkID,
fid,
info.Blockheight,
info.TotalDifficulty.String(),
info.HeadHash.String(),
n.N.IP().String(),
country,
city,
loc,
n.FirstResponse.String(),
n.LastResponse.String(),
n.Seq,
n.Score,
connType,
)
if err != nil {
return err
}
}
return tx.Commit()
}
func createDB(db *sql.DB) error {
sqlStmt := `
CREATE TABLE nodes (
ID text not null,
Now text not null,
ClientType text,
PK text,
SoftwareVersion text,
Capabilities text,
NetworkID number,
ForkID text,
Blockheight text,
TotalDifficulty text,
HeadHash text,
IP text,
Country text,
City text,
Coordinates text,
FirstSeen text,
LastSeen text,
Seq number,
Score number,
ConnType text,
PRIMARY KEY (ID, Now)
);
delete from nodes;
`
_, err := db.Exec(sqlStmt)
return err
}