forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.go
148 lines (131 loc) · 3.17 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
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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"context"
"encoding/binary"
"encoding/json"
"io/ioutil"
"os"
"github.com/aergoio/aergo/types"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/mr-tron/base58/base58"
"github.com/spf13/cobra"
)
var revert bool
func init() {
rootCmd.AddCommand(voteStatCmd)
voteStatCmd.Flags().StringVar(&address, "address", "", "address of account")
voteStatCmd.Flags().Uint64Var(&number, "count", 1, "the number of elected")
}
var voteCmd = &cobra.Command{
Use: "vote",
Short: "vote to BPs",
Run: execVote,
}
const PeerIDLength = 39
func execVote(cmd *cobra.Command, args []string) {
account, err := types.DecodeAddress(address)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
_, err = os.Stat(to)
if err == nil {
b, readerr := ioutil.ReadFile(to)
if readerr != nil {
cmd.Printf("Failed: %s\n", readerr.Error())
return
}
to = string(b)
}
var candidates []string
err = json.Unmarshal([]byte(to), &candidates)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
payload := make([]byte, (len(candidates)*PeerIDLength)+1)
payload[0] = 'v'
for i, v := range candidates {
candidate, err := base58.Decode(v)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
_, err = peer.IDFromBytes(candidate)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
copy(payload[1+(i*PeerIDLength):], candidate)
}
txs := make([]*types.Tx, 1)
state, err := client.GetState(context.Background(),
&types.SingleBytes{Value: account})
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
txs[0] = &types.Tx{
Body: &types.TxBody{
Account: account,
Recipient: []byte(aergosystem),
Payload: payload,
Limit: 0,
Type: types.TxType_GOVERNANCE,
Nonce: state.GetNonce() + 1,
},
}
//TODO : support local
tx, err := client.SignTX(context.Background(), txs[0])
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
txs[0] = tx
msg, err := client.CommitTX(context.Background(), &types.TxList{Txs: txs})
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
for _, r := range msg.Results {
cmd.Println("voting hash :", base58.Encode(r.Hash), r.Error, r.Detail)
return
}
}
var voteStatCmd = &cobra.Command{
Use: "votestat",
Short: "show voting stat",
Run: execVoteStat,
}
func execVoteStat(cmd *cobra.Command, args []string) {
var rawAddr []byte
var err error
if address != "" {
rawAddr, err = types.DecodeAddress(address)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
}
var msg *types.VoteList
if rawAddr != nil {
msg, err = client.GetVotes(context.Background(), &types.SingleBytes{Value: rawAddr})
} else {
var voteQuery []byte
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(number))
voteQuery = b
msg, err = client.GetVotes(context.Background(), &types.SingleBytes{Value: voteQuery})
}
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
return
}
for i, r := range msg.GetVotes() {
cmd.Println(i+1, " : ", base58.Encode(r.Candidate), " : ", r.GetAmountBigInt().String())
}
}