-
Notifications
You must be signed in to change notification settings - Fork 31
/
gov_db.go
515 lines (447 loc) · 14.3 KB
/
gov_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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2021 The Alaya Network Authors
// This file is part of the Alaya-Go library.
//
// The Alaya-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 Alaya-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 Alaya-Go library. If not, see <http://www.gnu.org/licenses/>.
package gov
import (
"encoding/json"
"fmt"
"github.com/AlayaNetwork/Alaya-Go/params"
"github.com/AlayaNetwork/Alaya-Go/rlp"
"github.com/AlayaNetwork/Alaya-Go/core/snapshotdb"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/common/vm"
"github.com/AlayaNetwork/Alaya-Go/log"
"github.com/AlayaNetwork/Alaya-Go/p2p/discover"
"github.com/AlayaNetwork/Alaya-Go/x/xcom"
)
var (
ValueDelimiter = []byte(":")
)
func SetProposal(proposal Proposal, state xcom.StateDB) error {
bytes, e := json.Marshal(proposal)
if e != nil {
return e
}
value := append(bytes, byte(proposal.GetProposalType()))
state.SetState(vm.GovContractAddr, KeyProposal(proposal.GetProposalID()), value)
return nil
//return AddPIPID(proposal.GetPIPID(), state)
}
func GetProposal(proposalID common.Hash, state xcom.StateDB) (Proposal, error) {
value := state.GetState(vm.GovContractAddr, KeyProposal(proposalID))
if len(value) == 0 {
return nil, nil
}
pData := value[0 : len(value)-1]
pType := value[len(value)-1]
if pType == byte(Text) {
var proposal TextProposal
if e := json.Unmarshal(pData, &proposal); e != nil {
log.Error("cannot parse data to text proposal")
return nil, e
}
return &proposal, nil
} else if pType == byte(Version) {
var proposal VersionProposal
if e := json.Unmarshal(pData, &proposal); e != nil {
log.Error("cannot parse data to version proposal")
return nil, e
}
return &proposal, nil
} else if pType == byte(Cancel) {
var proposal CancelProposal
if e := json.Unmarshal(pData, &proposal); e != nil {
log.Error("cannot parse data to cancel proposal")
return nil, e
}
return &proposal, nil
} else if pType == byte(Param) {
var proposal ParamProposal
if e := json.Unmarshal(pData, &proposal); e != nil {
log.Error("cannot parse data to param proposal")
return nil, e
}
return &proposal, nil
} else {
return nil, common.InternalError.Wrap("Incorrect proposal type.")
}
}
// Select proposal id list from snapshot database ,then get proposal detail from statedb one by one
func GetProposalList(blockHash common.Hash, state xcom.StateDB) ([]Proposal, error) {
proposalIds, err := getAllProposalIDList(blockHash)
if err != nil {
return nil, err
}
var proposls []Proposal
for _, proposalId := range proposalIds {
proposal, err := GetExistProposal(proposalId, state)
if err != nil {
return nil, err
} else {
proposls = append(proposls, proposal)
}
}
return proposls, nil
}
//Add the Vote detail
func AddVoteValue(proposalID common.Hash, voter discover.NodeID, option VoteOption, blockHash common.Hash) error {
voteValueList, err := ListVoteValue(proposalID, blockHash)
if err != nil {
return err
}
voteValueList = append(voteValueList, VoteValue{voter, option})
return UpdateVoteValue(proposalID, voteValueList, blockHash)
}
//list vote detail
func ListVoteValue(proposalID common.Hash, blockHash common.Hash) ([]VoteValue, error) {
voteListBytes, err := get(blockHash, KeyVote(proposalID))
if err != nil && err != snapshotdb.ErrNotFound {
return nil, err
}
var voteList []VoteValue
if len(voteListBytes) > 0 {
if err = rlp.DecodeBytes(voteListBytes, &voteList); err != nil {
return nil, err
}
}
return voteList, nil
}
func UpdateVoteValue(proposalID common.Hash, voteValueList []VoteValue, blockHash common.Hash) error {
//state.SetState(vm.GovContractAddr, KeyVote(proposalID), voteListBytes)
if err := put(blockHash, KeyVote(proposalID), voteValueList); err != nil {
return err
}
return nil
}
// TallyVoteValue statistics vote option for a proposal
func TallyVoteValue(proposalID common.Hash, blockHash common.Hash) (yeas, nays, abstentions uint64, e error) {
yes := uint64(0)
no := uint64(0)
abst := uint64(0)
voteList, err := ListVoteValue(proposalID, blockHash)
if err == nil {
for _, v := range voteList {
if v.VoteOption == Yes {
yes++
}
if v.VoteOption == No {
no++
}
if v.VoteOption == Abstention {
abst++
}
}
}
return yes, no, abst, err
}
func ClearVoteValue(proposalID common.Hash, blockHash common.Hash) error {
if err := del(blockHash, KeyVote(proposalID)); err != nil {
log.Error("clear vote value in snapshot db failed", "proposalID", proposalID, "blockHash", blockHash.Hex(), "error", err)
return err
}
return nil
}
/*
func ListVotedVerifier(proposalID common.Hash, state xcom.StateDB) ([]discover.NodeID, error) {
var voterList []discover.NodeID
valueList, err := ListVoteValue(proposalID, state)
if err != nil {
return nil, err
}
for _, value := range valueList {
voterList = append(voterList, value.VoteNodeID)
}
return voterList, nil
}
*/
func GetVotedVerifierMap(proposalID common.Hash, blockHash common.Hash) (map[discover.NodeID]struct{}, error) {
valueList, err := ListVoteValue(proposalID, blockHash)
if err != nil {
return nil, err
}
votedMap := make(map[discover.NodeID]struct{}, len(valueList))
for _, value := range valueList {
votedMap[value.VoteNodeID] = struct{}{}
}
return votedMap, nil
}
func SetTallyResult(tallyResult TallyResult, state xcom.StateDB) error {
value, err := json.Marshal(tallyResult)
if err != nil {
return err
}
state.SetState(vm.GovContractAddr, KeyTallyResult(tallyResult.ProposalID), value)
return nil
}
func GetTallyResult(proposalID common.Hash, state xcom.StateDB) (*TallyResult, error) {
proposal, err := GetProposal(proposalID, state)
if err != nil {
return nil, err
} else if proposal == nil {
return nil, ProposalNotFound
}
value := state.GetState(vm.GovContractAddr, KeyTallyResult(proposalID))
if len(value) == 0 {
return nil, nil
}
var tallyResult TallyResult
if err := json.Unmarshal(value, &tallyResult); err != nil {
return nil, err
}
return &tallyResult, nil
}
// Set pre-active version
func SetPreActiveVersion(blockHash common.Hash, preActiveVersion uint32) error {
return setPreActiveVersion(blockHash, preActiveVersion)
}
// Get pre-active version
func GetPreActiveVersion(blockHash common.Hash) uint32 {
return getPreActiveVersion(blockHash)
}
// Set active version record
func AddActiveVersion(activeVersion uint32, activeBlock uint64, state xcom.StateDB) error {
avList, err := ListActiveVersion(state)
if err != nil {
return err
}
curAv := ActiveVersionValue{ActiveVersion: activeVersion, ActiveBlock: activeBlock}
//Insert the object into the head of the list
avList = append([]ActiveVersionValue{curAv}, avList...)
avListBytes, _ := json.Marshal(avList)
state.SetState(vm.GovContractAddr, KeyActiveVersions(), avListBytes)
return nil
}
func Set0140Param(hash common.Hash, curVersion uint32, db snapshotdb.DB) error {
if curVersion == params.FORKVERSION_0_14_0 {
list, err := db.Get(hash, KeyParamItems())
if err != nil {
return err
}
var paramItemList []*ParamItem
if err := rlp.DecodeBytes(list, ¶mItemList); err != nil {
return err
}
for _, param := range init0140VersionParam() {
paramItemList = append(paramItemList, param.ParamItem)
value := common.MustRlpEncode(param.ParamValue)
if err := db.Put(hash, KeyParamValue(param.ParamItem.Module, param.ParamItem.Name), value); err != nil {
return fmt.Errorf("failed to Store govern 0140 parameter. error:%s", err.Error())
}
RegGovernParamVerifier(param.ParamItem.Module, param.ParamItem.Name, param.ParamVerifier)
}
value := common.MustRlpEncode(paramItemList)
if err := db.Put(hash, KeyParamItems(), value); err != nil {
return fmt.Errorf("failed to Store govern 0140 parameter list. error:%s", err.Error())
}
}
return nil
}
// Get voting proposal
func ListVotingProposal(blockHash common.Hash) ([]common.Hash, error) {
value, err := getVotingIDList(blockHash)
if err != nil {
log.Error("List voting proposal ID error")
return nil, err
}
return value, nil
}
func ListEndProposalID(blockHash common.Hash) ([]common.Hash, error) {
value, err := getEndIDList(blockHash)
if err != nil {
return nil, err
}
return value, nil
}
func GetPreActiveProposalID(blockHash common.Hash) (common.Hash, error) {
value, err := getPreActiveProposalID(blockHash)
if err != nil {
//log.Error("Get pre-active proposal ID error")
return common.ZeroHash, err
}
return value, nil
}
func AddVotingProposalID(blockHash common.Hash, proposalID common.Hash) error {
if err := addProposalByKey(blockHash, KeyVotingProposals(), proposalID); err != nil {
//log.Error("add voting proposal to snapshot db error:%s", err)
return err
}
return nil
}
func MoveVotingProposalIDToPreActive(blockHash common.Hash, proposalID common.Hash, preactiveVersion uint32) error {
voting, err := getVotingIDList(blockHash)
if err != nil {
return err
}
voting = remove(voting, proposalID)
err = put(blockHash, KeyVotingProposals(), voting)
if err != nil {
return err
}
err = put(blockHash, KeyPreActiveProposal(), proposalID)
if err != nil {
return err
}
if err := SetPreActiveVersion(blockHash, preactiveVersion); err != nil {
return err
}
return nil
}
func remove(list []common.Hash, item common.Hash) []common.Hash {
if len(list) == 0 {
return list
}
for i, id := range list {
if id == item {
if len(list) > 1 {
list = append(list[:i], list[i+1:]...)
} else {
list = []common.Hash{}
}
}
}
return list
}
func MoveVotingProposalIDToEnd(proposalID common.Hash, blockHash common.Hash) error {
voting, err := getVotingIDList(blockHash)
if err != nil {
return err
}
voting = remove(voting, proposalID)
err = put(blockHash, KeyVotingProposals(), voting)
if err != nil {
return err
}
err = addProposalByKey(blockHash, KeyEndProposals(), proposalID)
if err != nil {
return err
}
return nil
}
func MovePreActiveProposalIDToEnd(blockHash common.Hash, proposalID common.Hash) error {
//only one proposalID in PreActiveProposalIDList, so, just set it empty.
err := del(blockHash, KeyPreActiveProposal())
if err != nil {
return err
}
// add this proposal ID to End list
err = addProposalByKey(blockHash, KeyEndProposals(), proposalID)
if err != nil {
return err
}
// remove the pre-active version
err = delPreActiveVersion(blockHash)
if err != nil {
return err
}
return nil
}
// Add the node that has made a new version declare or vote during voting period
func AddActiveNode(blockHash common.Hash, proposalID common.Hash, nodeID discover.NodeID) error {
if err := addActiveNode(blockHash, nodeID, proposalID); err != nil {
log.Error("add active node to snapshot db failed", "blockHash", blockHash.Hex(), "proposalID", proposalID, "error", err)
return err
}
return nil
}
// Get the node list that have made a new version declare or vote during voting period
func GetActiveNodeList(blockHash common.Hash, proposalID common.Hash) ([]discover.NodeID, error) {
nodes, err := getActiveNodeList(blockHash, proposalID)
if err != nil {
log.Error("get active nodes from snapshot db failed", "blockHash", blockHash.Hex(), "proposalID", proposalID, "error", err)
return nil, err
}
return nodes, nil
}
// Clear the version declaration records after upgrade
func ClearActiveNodes(blockHash common.Hash, proposalID common.Hash) error {
err := deleteActiveNodeList(blockHash, proposalID)
if err != nil {
log.Error("clear active nodes in snapshot db failed", "blockHash", blockHash.Hex(), "proposalID", proposalID, "error", err)
return err
}
return nil
}
// AccuVerifiers accumulates all distinct verifiers those can vote this proposal ID
func AccuVerifiers(blockHash common.Hash, proposalID common.Hash, verifierList []discover.NodeID) error {
if err := addAccuVerifiers(blockHash, proposalID, verifierList); err != nil {
log.Error("accumulates verifiers to snapshot db failed", "blockHash", blockHash.Hex(), "proposalID", proposalID, "error", err)
return err
}
return nil
}
// Get the total number of all voting verifiers
func ListAccuVerifier(blockHash common.Hash, proposalID common.Hash) ([]discover.NodeID, error) {
if l, err := getAccuVerifiers(blockHash, proposalID); err != nil {
log.Error("list accumulated verifiers failed", "blockHash", blockHash.Hex(), "proposalID", proposalID, "error", err)
return nil, err
} else {
return l, nil
}
}
func ClearAccuVerifiers(blockHash common.Hash, proposalID common.Hash) error {
if err := delAccuVerifiers(blockHash, proposalID); err != nil {
log.Error("clear voted verifiers in snapshot db failed", "proposalID", proposalID, "blockHash", blockHash.Hex(), "error", err)
return err
}
return nil
}
func AddPIPID(pipID string, state xcom.StateDB) error {
pipIDList, err := ListPIPID(state)
if err != nil {
return err
}
if pipIDList == nil || len(pipIDList) == 0 {
pipIDList = []string{pipID}
} else {
pipIDList = append(pipIDList, pipID)
}
pipIDListBytes, _ := json.Marshal(pipIDList)
state.SetState(vm.GovContractAddr, KeyPIPIDs(), pipIDListBytes)
return nil
}
func ListPIPID(state xcom.StateDB) ([]string, error) {
pipIDListBytes := state.GetState(vm.GovContractAddr, KeyPIPIDs())
if len(pipIDListBytes) > 0 {
var pipIDList []string
if err := json.Unmarshal(pipIDListBytes, &pipIDList); err != nil {
return nil, err
}
return pipIDList, nil
} else {
return nil, nil
}
}
func GetExistProposal(proposalID common.Hash, state xcom.StateDB) (Proposal, error) {
p, err := GetProposal(proposalID, state)
if err != nil {
return nil, err
} else if p == nil {
//log.Error("Cannot find proposal.", "proposalID", proposalID)
return nil, ProposalNotFound
} else {
return p, nil
}
}
func ListActiveVersion(state xcom.StateDB) ([]ActiveVersionValue, error) {
avListBytes := state.GetState(vm.GovContractAddr, KeyActiveVersions())
if len(avListBytes) == 0 {
return nil, nil
}
var avList []ActiveVersionValue
if err := json.Unmarshal(avListBytes, &avList); err != nil {
return nil, err
}
return avList, nil
}