-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_sql.go
214 lines (196 loc) · 5.96 KB
/
node_sql.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
package main
import (
"fmt"
s "github.com/ValidatorCenter/prs3r/strc"
// база данных на SQL
"github.com/jmoiron/sqlx"
_ "github.com/kshvakov/clickhouse"
)
// Количество нод в базе
func srchNodeAmnt(db *sqlx.DB) int {
iAmnt := 0
err := db.Get(&iAmnt, "SELECT count() FROM nodes")
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeAmnt(Get) - ERR:", err), "")
panic(err) //dbg
}
return iAmnt
}
// Поиск всех нод пользователя
func srchNodeAddress(db *sqlx.DB, addrs string) []s.NodeExt {
v := []s.NodeExt{}
// Некоторая информация переехала в Redis! --> Sort("-status", "-total_stake_f32")
err := db.Select(&v, fmt.Sprintf(`
SELECT *
FROM nodes
WHERE owner_address = '%s' OR reward_address = '%s'
`, addrs, addrs))
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeAddress(Select) - [addrs ", addrs, "] ERR:", err), "")
panic(err) //dbg
return v
}
return v
}
//Поиск ноды по паблику
func srchNodePubkey(db *sqlx.DB, pub_key string) s.NodeExt {
v := s.NodeExt{}
// Некоторая информация переехала в Redis!
err := db.Get(&v, fmt.Sprintf(`
SELECT *
FROM nodes
WHERE pub_key = '%s'
`, pub_key))
if err != nil {
if err.Error() == "sql: no rows in result set" {
log("WRN", fmt.Sprint("[node_sql.go] srchNodePubkey(Get) - [pub_key ", pub_key, "] ERR:", err), "")
} else {
log("ERR", fmt.Sprint("[node_sql.go] srchNodePubkey(Get) - [pub_key ", pub_key, "] ERR:", err), "")
panic(err) //dbg
}
return v
}
return v
}
// Поиск ноды по публичному ключу и основному адресу кошелька в SQL
func srchNodeSql_oa(db *sqlx.DB, pub_key string, owner_address string) s.NodeExt {
p := s.NodeExt{}
err := db.Get(&p, fmt.Sprintf(`
SELECT *
FROM nodes FINAL
WHERE pub_key = '%s' AND owner_address = '%s'
LIMIT 1
`, pub_key, owner_address))
if err != nil {
if err.Error() == "sql: no rows in result set" {
log("WRN", fmt.Sprint("[node_sql.go] srchNodeSql_oa(Select) - ", err), "")
} else {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeSql_oa(Select) - ", err), "")
}
return s.NodeExt{}
}
return p
}
//Поиск стэк-ноды по паблику
func srchNodeStakeSql(db *sqlx.DB, pub_key string) []s.StakesInfo {
v := []s.StakesInfo{}
err := db.Select(&v, fmt.Sprintf(`
SELECT owner_address as owner, coin, value_f32, bip_value_f32
FROM node_stakes
FINAL
WHERE pub_key = '%s'
`, pub_key))
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeStake(Select) - [pub_key ", pub_key, "] ERR:", err), "")
panic(err) //dbg
return v
}
return v
}
//Поиск блоков-истории ноды по паблику
func srchNodeBlockstory(db *sqlx.DB, pub_key string) []s.BlocksStory {
v := []s.BlocksStory{}
err := db.Select(&v, fmt.Sprintf(`
SELECT block_id, block_type
FROM node_blockstory
WHERE pub_key = '%s'
ORDER BY block_id
`, pub_key))
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeBlockstory(Select) - [pub_key ", pub_key, "] ERR:", err), "")
panic(err) //dbg
return v
}
return v
}
// Список всех нод
func srchNodeList(db *sqlx.DB) []s.NodeExt { //, page int) []s.NodeExt {
nodeM := []s.NodeExt{}
// Некоторая информация переехала в Redis! status и total_stake_f32 в REDIS
// поэтому берем все данные, а не только по страницам
/*
SELECT *
FROM nodes b
FINAL
ORDER BY status, total_stake_f32 DESC
LIMIT %d, 50
*/
//skipVldt := page * 50
/*
SELECT n.*, count() AS amnt_slots
FROM nodes n FINAL
LEFT JOIN node_stakes ns FINAL
ON n.pub_key = ns.pub_key
GROUP BY n.*
*/
err := db.Select(&nodeM, `
SELECT *
FROM nodes FINAL
`)
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeList(Select) - ERR:", err), "")
panic(err) //dbg
}
return nodeM
}
type Delegate struct {
Validator string `json:"pub_key" db:"pub_key"`
ValidatorMin string `json:"pub_key_min" db:"pub_key_min"`
Coin string `json:"coin" db:"coin"`
Value float32 `json:"value_f32" db:"value_f32"`
ValueBip float32 `json:"bip_value_f32" db:"bip_value_f32"`
}
//...!тут STAKES.OWNER
// Поиск всех делегирований пользователя в ноды!
func srchNodeAddrsSql(db *sqlx.DB, addrs string) []Delegate {
//..nodesCollection := sessMDB.DB("mvc_db").C("tabl_node")
//nodesCollection.Find(bson.M{"stakes.owner": nmbrAddrs}).Sort("-$natural").All(&nodesDeleg)
nodesDeleg := []Delegate{}
err := db.Select(&nodesDeleg, fmt.Sprintf(`
SELECT n.pub_key,n.pub_key_min,ns.coin,ns.value_f32,ns.bip_value_f32
FROM nodes n FINAL
LEFT JOIN node_stakes ns FINAL
ON n.pub_key = ns.pub_key
WHERE ns.owner_address = '%s'
`, addrs))
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeList(Select) - ERR:", err), "")
panic(err) //dbg
}
return nodesDeleg
}
type NodeAmntInf struct {
PubKey string `json:"pub_key" db:"pub_key"`
Amnt int `json:"amnt" db:"amnt"`
}
//Поиск блоков-истории всех нод по типу
func srchNodeBlockstoryTypeAll(db *sqlx.DB, typeBlock string) []NodeAmntInf {
v := []NodeAmntInf{}
err := db.Select(&v, fmt.Sprintf(`
SELECT pub_key, count() AS amnt
FROM node_blockstory
WHERE block_type='%s'
GROUP BY pub_key
`, typeBlock)) //AbsentBlock
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeBlockstoryTypeAll(Select) - [typeBlock ", typeBlock, "] ERR:", err), "")
panic(err) //dbg
return v
}
return v
}
//Поиск занятых слотов всех нод
func srchNodeStakesAmntAll(db *sqlx.DB) []NodeAmntInf {
v := []NodeAmntInf{}
err := db.Select(&v, `
SELECT pub_key, count() AS amnt
FROM node_stakes FINAL
GROUP BY pub_key
`)
if err != nil {
log("ERR", fmt.Sprint("[node_sql.go] srchNodeStakesAmntAll(Select) - ERR:", err), "")
panic(err) //dbg
return v
}
return v
}