forked from PlatONnetwork/PlatON-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gov_snapdb.go
335 lines (294 loc) · 9.08 KB
/
gov_snapdb.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2018-2019 The PlatON Network Authors
// This file is part of the PlatON-Go library.
//
// The PlatON-Go library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The PlatON-Go library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.
package gov
import (
"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/core/snapshotdb"
"github.com/PlatONnetwork/PlatON-Go/log"
"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
"github.com/PlatONnetwork/PlatON-Go/rlp"
"github.com/PlatONnetwork/PlatON-Go/x/xutil"
)
func get(blockHash common.Hash, key []byte) ([]byte, error) {
return snapshotdb.Instance().Get(blockHash, key)
}
func put(blockHash common.Hash, key []byte, value interface{}) error {
bytes, err := rlp.EncodeToBytes(value)
if err != nil {
return err
}
return snapshotdb.Instance().Put(blockHash, key, bytes)
}
func del(blockHash common.Hash, key []byte) error {
return snapshotdb.Instance().Del(blockHash, key)
}
func addProposalByKey(blockHash common.Hash, key []byte, proposalId common.Hash) error {
proposalIDList, err := getProposalIDListByKey(blockHash, key)
if err != nil {
return err
}
for _, pID := range proposalIDList {
if pID == proposalId {
return nil
}
}
proposalIDList = append(proposalIDList, proposalId)
return put(blockHash, key, proposalIDList)
}
func getVotingIDList(blockHash common.Hash) ([]common.Hash, error) {
return getProposalIDListByKey(blockHash, KeyVotingProposals())
}
// Set pre-active version
func setPreActiveVersion(blockHash common.Hash, preActiveVersion uint32) error {
return put(blockHash, KeyPreActiveVersion(), preActiveVersion)
}
// Get pre-active version
func getPreActiveVersion(blockHash common.Hash) uint32 {
bytes, err := get(blockHash, KeyPreActiveVersion())
if snapshotdb.NonDbNotFoundErr(err) {
return uint32(0)
}
var activeVersion uint32
if len(bytes) > 0 {
if err = rlp.DecodeBytes(bytes, &activeVersion); err != nil {
return uint32(0)
}
}
return activeVersion
}
func delPreActiveVersion(blockHash common.Hash) error {
return del(blockHash, KeyPreActiveVersion())
}
func getPreActiveProposalID(blockHash common.Hash) (common.Hash, error) {
//return self.getProposalIDListByKey(blockHash, KeyPreActiveProposals())
bytes, err := get(blockHash, KeyPreActiveProposal())
if snapshotdb.NonDbNotFoundErr(err) {
return common.Hash{}, err
}
var proposalID common.Hash
if len(bytes) > 0 {
if err = rlp.DecodeBytes(bytes, &proposalID); err != nil {
return common.Hash{}, err
}
}
return proposalID, nil
}
func getEndIDList(blockHash common.Hash) ([]common.Hash, error) {
return getProposalIDListByKey(blockHash, KeyEndProposals())
}
func getProposalIDListByKey(blockHash common.Hash, key []byte) ([]common.Hash, error) {
bytes, err := get(blockHash, key)
if snapshotdb.NonDbNotFoundErr(err) {
return nil, err
}
var idList []common.Hash
if len(bytes) > 0 {
if err = rlp.DecodeBytes(bytes, &idList); err != nil {
return nil, err
}
}
return idList, nil
}
func getAllProposalIDList(blockHash common.Hash) ([]common.Hash, error) {
var total []common.Hash
proposalIDList, err := getVotingIDList(blockHash)
if err != nil {
log.Error("list voting proposal IDs failed", "blockHash", blockHash)
return nil, err
} else if len(proposalIDList) > 0 {
total = append(total, proposalIDList...)
}
proposalID, err := getPreActiveProposalID(blockHash)
if err != nil {
log.Error("list pre-active proposal IDs failed", "blockHash", blockHash)
return nil, err
} else if proposalID != common.ZeroHash {
total = append(total, proposalID)
}
proposalIDList, err = getEndIDList(blockHash)
if err != nil {
log.Error("list end proposal IDs failed", "blockHash", blockHash)
return nil, err
} else if len(proposalIDList) > 0 {
total = append(total, proposalIDList...)
}
return total, nil
}
func addActiveNode(blockHash common.Hash, node discover.NodeID, proposalId common.Hash) error {
nodes, err := getActiveNodeList(blockHash, proposalId)
if snapshotdb.NonDbNotFoundErr(err) {
return err
}
//distinct the nodeID
if xutil.InNodeIDList(node, nodes) {
return nil
} else {
nodes = append(nodes, node)
return put(blockHash, KeyActiveNodes(proposalId), nodes)
}
}
func getActiveNodeList(blockHash common.Hash, proposalId common.Hash) ([]discover.NodeID, error) {
value, err := get(blockHash, KeyActiveNodes(proposalId))
if snapshotdb.NonDbNotFoundErr(err) {
return nil, err
}
var nodes []discover.NodeID
if len(value) > 0 {
if err := rlp.DecodeBytes(value, &nodes); err != nil {
return nil, err
}
}
return nodes, nil
}
func deleteActiveNodeList(blockHash common.Hash, proposalId common.Hash) error {
return del(blockHash, KeyActiveNodes(proposalId))
}
func addAccuVerifiers(blockHash common.Hash, proposalId common.Hash, nodes []discover.NodeID) error {
value, err := get(blockHash, KeyAccuVerifier(proposalId))
if snapshotdb.NonDbNotFoundErr(err) {
return err
}
var accuVerifiers []discover.NodeID
if value != nil {
if err := rlp.DecodeBytes(value, &accuVerifiers); err != nil {
return err
}
}
existMap := make(map[discover.NodeID]struct{}, len(accuVerifiers))
for _, nodeID := range accuVerifiers {
existMap[nodeID] = struct{}{}
}
for _, nodeID := range nodes {
if _, ok := existMap[nodeID]; !ok {
accuVerifiers = append(accuVerifiers, nodeID)
}
/*
if !xutil.InNodeIDList(nodeID, accuVerifiers) {
accuVerifiers = append(accuVerifiers, nodeID)
}
*/
}
log.Debug("accumulated verifiers", "proposalID", proposalId, "total", len(accuVerifiers))
return put(blockHash, KeyAccuVerifier(proposalId), accuVerifiers)
}
func getAccuVerifiers(blockHash common.Hash, proposalId common.Hash) ([]discover.NodeID, error) {
value, err := get(blockHash, KeyAccuVerifier(proposalId))
if snapshotdb.NonDbNotFoundErr(err) {
return nil, err
}
if len(value) > 0 {
var verifiers []discover.NodeID
if err := rlp.DecodeBytes(value, &verifiers); err != nil {
return nil, err
} else {
return verifiers, nil
}
}
return nil, nil
}
func delAccuVerifiers(blockHash common.Hash, proposalId common.Hash) error {
return del(blockHash, KeyAccuVerifier(proposalId))
}
func addGovernParam(module, name, desc string, paramValue *ParamValue, blockHash common.Hash) error {
itemList, err := listGovernParamItem("", blockHash)
if err != nil {
return nil
}
itemList = append(itemList, &ParamItem{module, name, desc})
if err := put(blockHash, keyPrefixParamItems, itemList); err != nil {
return err
}
if err := put(blockHash, KeyParamValue(module, name), paramValue); err != nil {
return err
}
return nil
}
func findGovernParamValue(module, name string, blockHash common.Hash) (*ParamValue, error) {
value, err := get(blockHash, KeyParamValue(module, name))
if snapshotdb.NonDbNotFoundErr(err) {
return nil, err
}
if len(value) > 0 {
var paramValue ParamValue
if err := rlp.DecodeBytes(value, ¶mValue); err != nil {
return nil, err
} else {
return ¶mValue, nil
}
}
return nil, nil
}
func updateGovernParamValue(module, name, newValue string, activeBlock uint64, blockHash common.Hash) error {
value, err := get(blockHash, KeyParamValue(module, name))
if snapshotdb.NonDbNotFoundErr(err) {
return err
}
if len(value) > 0 {
var paramValue ParamValue
if err := rlp.DecodeBytes(value, ¶mValue); err != nil {
return err
}
paramValue.StaleValue = paramValue.Value
paramValue.Value = newValue
paramValue.ActiveBlock = activeBlock
if err := put(blockHash, KeyParamValue(module, name), paramValue); err != nil {
return err
}
return nil
}
return UnsupportedGovernParam
}
func listGovernParam(module string, blockHash common.Hash) ([]*GovernParam, error) {
itemList, err := listGovernParamItem(module, blockHash)
if err != nil {
return nil, err
}
var paraList []*GovernParam
for _, item := range itemList {
if value, err := findGovernParamValue(item.Module, item.Name, blockHash); err != nil {
return nil, err
} else {
param := &GovernParam{item, value, nil}
paraList = append(paraList, param)
}
}
return paraList, nil
}
func listGovernParamItem(module string, blockHash common.Hash) ([]*ParamItem, error) {
itemBytes, err := get(blockHash, KeyParamItems())
if snapshotdb.NonDbNotFoundErr(err) {
return nil, err
}
if len(itemBytes) > 0 {
var itemList []*ParamItem
if err := rlp.DecodeBytes(itemBytes, &itemList); err != nil {
return nil, err
}
if len(module) == 0 {
return itemList, nil
} else {
idx := 0
for _, item := range itemList {
if item.Module == module {
itemList[idx] = item
idx++
}
}
return itemList[:idx], nil
}
}
return nil, nil
}