-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_node.go
229 lines (206 loc) · 5.48 KB
/
sql_node.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
package main
import (
"fmt"
"time"
s "github.com/ValidatorCenter/prs3r/strc"
"github.com/jmoiron/sqlx"
_ "github.com/kshvakov/clickhouse"
)
// Добавить ноду в SQL
func addNodeSql(db *sqlx.DB, dt *s.NodeExt) bool {
var err error
tx := db.MustBegin()
dt.UpdYCH = time.Now()
qPg := `
INSERT INTO nodes (
pub_key,
pub_key_min,
reward_address,
owner_address,
created,
commission,
created_at_block,
updated_date
) VALUES (
:pub_key,
:pub_key_min,
:reward_address,
:owner_address,
:created,
:commission,
:created_at_block,
:updated_date
);
`
_, err = tx.NamedExec(qPg, &dt)
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] addNodeSql(NamedExec) - ", err), "")
panic(err)
return false
}
log("INF", "INSERT", fmt.Sprint("node ", dt.PubKey))
err = tx.Commit()
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] addNodeSql(Commit) -", err), "")
panic(err)
return false
}
return true
}
// Добавить о ноде историю блоков в SQL
func addNodeBlockstorySql(db *sqlx.DB, dt *s.NodeExt) bool {
chNAr := srchNodeBlockstory(db, dt.PubKey) // Для проверки на задвоенность блока для ноды
for _, bs1 := range dt.Blocks {
//+Проверка
fndBS := false
for _, tst1 := range chNAr {
if tst1.ID == bs1.ID && tst1.Type == bs1.Type {
fndBS = true
}
}
if fndBS {
log("ERR", fmt.Sprint("[sql_node.go] addNodeBlockstorySql(Find!) - [", dt.PubKey, " ", bs1.ID, " ", bs1.Type, "]"), "")
//panic("!!!")
continue
}
//-
tx := db.MustBegin()
qPg := `
INSERT INTO node_blockstory (
pub_key,
block_id,
block_type
) VALUES (
:pub_key,
:block_id,
:block_type
)`
m2 := map[string]interface{}{
"pub_key": dt.PubKey,
"block_id": bs1.ID,
"block_type": bs1.Type,
}
_, err := tx.NamedExec(qPg, m2)
if err != nil {
//Возможно добавили раньше, поэтому не сильно надо обращать на эту ошибку, но за неё может не пройти другие транзакции
log("WRN", fmt.Sprint("[sql_node.go] addNodeBlockstorySql(NamedExec) - [", dt.PubKey, " ", bs1.ID, " ", bs1.Type, "] ", err), "")
continue
}
err = tx.Commit()
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] addNodeBlockstorySql(Commit) -", err), "")
return false
}
log("INF", "INSERT", fmt.Sprint("node_blockstory ", dt.PubKey))
}
return true
}
// Добавить о ноде стэк в SQL
func addNodeStakeSql(db *sqlx.DB, dt *s.NodeExt) bool {
for _, st1 := range dt.Stakes {
tx := db.MustBegin()
// Через tx.MustExec и "?" не сработало, выдаёт ошибку
qPg := `
INSERT INTO node_stakes (
pub_key,
owner_address,
coin,
value_f32,
bip_value_f32,
updated_date
) VALUES (
:pub_key,
:owner_address,
:coin,
:value_f32,
:bip_value_f32,
:updated_date
)`
m2 := map[string]interface{}{
"pub_key": dt.PubKey,
"owner_address": st1.Owner,
"coin": st1.Coin,
"value_f32": st1.Value,
"bip_value_f32": st1.BipValue,
"updated_date": time.Now(),
}
_, err := tx.NamedExec(qPg, m2)
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] addNodeStakeSql(NamedExec) - [", dt.PubKey, "] ", err), "")
panic(err) // dbg
return false
}
err = tx.Commit()
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] addNodeStakeSql(Commit) -", err), "")
panic(err) // dbg
return false //continue
}
}
log("INF", "INSERT", fmt.Sprint("node_stakes ", dt.PubKey))
return true
}
// Поиск ноды по публичному ключу и основному адресу кошелька в 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("[sql_node.go] srchNodeSql_oa(Select) - ", err), "")
} else {
log("ERR", fmt.Sprint("[sql_node.go] srchNodeSql_oa(Select) - ", err), "")
}
return s.NodeExt{}
}
return p
}
// Поиск ноды по публичному ключу в SQL
func srchNodeSql_pk(db *sqlx.DB, pub_key string) s.NodeExt {
p := s.NodeExt{}
err := db.Get(&p, fmt.Sprintf(`
SELECT *
FROM nodes FINAL
WHERE pub_key = '%s'
LIMIT 1
`, pub_key))
if err != nil {
if err.Error() == "sql: no rows in result set" {
log("WRN", fmt.Sprint("[sql_node.go] srchNodeSql_pk(Select) - ", err), "")
} else {
log("ERR", fmt.Sprint("[sql_node.go] srchNodeSql_pk(Select) - ", err), "")
panic(err)
}
return s.NodeExt{}
}
return p
}
// Берем все ноды из SQL
func srchNodeSql_all(db *sqlx.DB) []s.NodeExt {
pp := []s.NodeExt{}
err := db.Select(&pp, "SELECT * FROM nodes")
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] srchCoinSql_all(Select) -", err), "")
return []s.NodeExt{}
}
return pp
}
//Поиск блоков-истории ноды по паблику
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'
`, pub_key))
if err != nil {
log("ERR", fmt.Sprint("[sql_node.go] srchNodeBlockstory(Select) - [pub_key ", pub_key, "] ERR:", err), "")
panic(err) //dbg
return v
}
return v
}