forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.go
108 lines (89 loc) · 2.59 KB
/
vote.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
package types
import (
"fmt"
"math/big"
)
const (
AergoSystem = "aergo.system"
AergoName = "aergo.name"
AergoEnterprise = "aergo.enterprise"
AergoVault = "aergo.vault" // For community reward program (i.e. voting reward)
MaxCandidates = 30
)
type VotingIssue interface {
ID() string
Key() []byte
}
//go:generate stringer -type=OpSysTx
// OpSysTx represents a kind of a system transaction.
type OpSysTx int
const (
// For compatibility with the old version, in which the first character of
// each voting type is lower, the constant name does not follow go naming
// convertion.
// OpvoteBP corresponds to a voting transaction for a BP election.
OpvoteBP OpSysTx = iota
// OpvoteDAO corresponds to a proposal transaction for a system parameter change.
OpvoteDAO
// Opstake represents a staking tranaction.
Opstake
// Opunstake represents a unstaking tranaction.
Opunstake
// OpSysTxMax is the maximum of system tx OP numbers.
OpSysTxMax
version = 1
)
var cmdToOp map[string]OpSysTx
func initSysCmd() {
cmdToOp = make(map[string]OpSysTx, OpSysTxMax)
for i := OpvoteBP; i < OpSysTxMax; i++ {
cmdToOp[i.Cmd()] = i
}
}
func init() {
initSysCmd()
}
// GetVotingIssues returns all the VotingIssues in this package.
func GetVotingIssues() []VotingIssue {
return []VotingIssue{OpvoteBP}
}
// GetOpSysTx returns a OpSysTx value corresponding to vName.
func GetOpSysTx(vName string) OpSysTx {
return cmdToOp[vName]
}
// Name returns a unprefixed name corresponding to op.
func (op OpSysTx) ID() string {
const prefixLen = 2 // prefix = "Op"
if op < OpSysTxMax && op >= 0 {
return op.String()[prefixLen:]
}
return ""
}
// Cmd returns a string representation for op.
func (op OpSysTx) Cmd() string {
name := op.ID()
if len(name) == 0 {
return name
}
return fmt.Sprintf("v%d%s", version, name)
}
func (op OpSysTx) Key() []byte {
return []byte(op.ID())
}
func (vl VoteList) Len() int { return len(vl.Votes) }
func (vl VoteList) Less(i, j int) bool {
result := new(big.Int).SetBytes(vl.Votes[i].Amount).Cmp(new(big.Int).SetBytes(vl.Votes[j].Amount))
if result == -1 {
return true
} else if result == 0 {
if len(vl.Votes[i].Candidate) == 39 /*peer id length*/ {
return new(big.Int).SetBytes(vl.Votes[i].Candidate[7:]).Cmp(new(big.Int).SetBytes(vl.Votes[j].Candidate[7:])) > 0
}
return new(big.Int).SetBytes(vl.Votes[i].Candidate).Cmp(new(big.Int).SetBytes(vl.Votes[j].Candidate)) > 0
}
return false
}
func (vl VoteList) Swap(i, j int) { vl.Votes[i], vl.Votes[j] = vl.Votes[j], vl.Votes[i] }
func (v *Vote) GetAmountBigInt() *big.Int {
return new(big.Int).SetBytes(v.Amount)
}