-
Notifications
You must be signed in to change notification settings - Fork 3
/
vote.go
472 lines (449 loc) · 12.8 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
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
package cryptonym
import (
"bytes"
"encoding/json"
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/dialog"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
fioassets "github.com/blockpane/cryptonym/assets"
errs "github.com/blockpane/cryptonym/errLog"
"github.com/fioprotocol/fio-go"
"github.com/fioprotocol/fio-go/eos"
"golang.org/x/text/language"
"golang.org/x/text/message"
"gopkg.in/yaml.v2"
"image"
"io/ioutil"
"log"
"math"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type existVotes struct {
Producers []string `json:"producers"`
}
type prodRow struct {
FioAddress string `json:"fio_address"`
}
func GetCurrentVotes(actor string, api *fio.API) (votes string) {
getVote, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "voters",
Index: "3",
LowerBound: actor,
UpperBound: actor,
Limit: 1,
KeyType: "name",
JSON: true,
})
if err != nil {
return
}
v := make([]*existVotes, 0)
err = json.Unmarshal(getVote.Rows, &v)
if err != nil {
return
}
if len(v) == 0 {
return
}
votedFor := make([]string, 0)
for _, row := range v[0].Producers {
if row == "" {
continue
}
gtr, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "producers",
LowerBound: row,
UpperBound: row,
KeyType: "name",
Index: "4",
JSON: true,
})
if err != nil {
continue
}
p := make([]*prodRow, 0)
err = json.Unmarshal(gtr.Rows, &p)
if err != nil {
continue
}
if len(p) == 1 && p[0].FioAddress != "" {
votedFor = append(votedFor, p[0].FioAddress)
}
}
if len(votedFor) == 0 {
return
}
return strings.Join(votedFor, ", ")
}
type bpInfo struct {
CurrentVotes float64
FioAddress string
Actor string
BpJson *fio.BpJson
VoteFor bool
OrigVoteFor bool
Url string
Img *canvas.Image
Top21 bool
Tied bool
}
var bpInfoCache = make(map[string]*bpInfo)
func getBpInfo(actor string, api *fio.API) ([]bpInfo, error) {
bpi := make([]bpInfo, 0)
p, err := api.GetFioProducers()
if err != nil {
return bpi, err
}
prods := &fio.Producers{
Producers: make([]fio.Producer, 0),
}
// little hack for testnet to cleanup crap from various test scripts:
for i := range p.Producers {
if strings.Contains(p.Producers[i].Url, "dapix.io") {
continue
}
prods.Producers = append(prods.Producers, p.Producers[i])
}
curVotes := strings.Split(GetCurrentVotes(actor, api), ",")
for i := range curVotes {
curVotes[i] = strings.TrimSpace(curVotes[i])
}
hasVoted := func(s string) bool {
for _, v := range curVotes {
if s == v {
return true
}
}
return false
}
isTopProd := make(map[string]bool)
sched, _ := api.GetProducerSchedule()
for _, tp := range sched.Active.Producers {
isTopProd[string(tp.AccountName)] = true
}
voteTies := make(map[string]int)
for _, bp := range prods.Producers {
voteTies[bp.TotalVotes] += 1
}
bpiMux := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(len(prods.Producers))
// temporarily set a very aggressive timeout, otherwise this can take up to a minute.
// sorry BPs, if your server is slow no icon and bp info will show up.
oldTimout := api.HttpClient.Timeout
api.HttpClient.Timeout = 2 * time.Second
for _, bp := range prods.Producers {
go func(bp fio.Producer) {
defer wg.Done()
if bp.IsActive == 0 {
return
}
votes, _ := strconv.ParseFloat(bp.TotalVotes, 64)
bpiMux.Lock()
var tied bool
if voteTies[bp.TotalVotes] > 1 {
tied = true
}
if bpInfoCache[string(bp.FioAddress)] != nil {
bpInfoCache[string(bp.FioAddress)].VoteFor = hasVoted(string(bp.FioAddress))
bpInfoCache[string(bp.FioAddress)].OrigVoteFor = hasVoted(string(bp.FioAddress))
bpInfoCache[string(bp.FioAddress)].Tied = tied
bpInfoCache[string(bp.FioAddress)].CurrentVotes = votes
bpInfoCache[string(bp.FioAddress)].Top21 = isTopProd[string(bp.Owner)]
bpi = append(bpi, *bpInfoCache[string(bp.FioAddress)])
bpiMux.Unlock()
return
}
bpiMux.Unlock()
bpj, err := api.GetBpJson(bp.Owner)
if err != nil {
log.Printf("could not get bp.json for %s, %s\n", bp.Owner, err.Error())
}
img := canvas.NewImageFromResource(theme.QuestionIcon())
if bpj != nil && bpj.Org.Branding.Logo256 != "" {
resp, err := api.HttpClient.Get(bpj.Org.Branding.Logo256)
if err == nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
decoded, _, err := image.Decode(bytes.NewReader(body))
if err == nil {
img = canvas.NewImageFromImage(decoded)
}
}
}
}
info := bpInfo{
CurrentVotes: votes,
FioAddress: string(bp.FioAddress),
Actor: string(bp.Owner),
Url: bp.Url,
BpJson: bpj,
VoteFor: hasVoted(string(bp.FioAddress)),
OrigVoteFor: hasVoted(string(bp.FioAddress)),
Img: img,
Top21: isTopProd[string(bp.Owner)],
Tied: tied,
}
bpiMux.Lock()
bpi = append(bpi, info)
bpInfoCache[string(bp.FioAddress)] = &info
bpiMux.Unlock()
}(bp)
}
wg.Wait()
api.HttpClient.Timeout = oldTimout
sort.Slice(bpi, func(i, j int) bool {
if bpi[i].CurrentVotes == bpi[j].CurrentVotes {
iName, _ := eos.StringToName(bpi[i].Actor)
jName, _ := eos.StringToName(bpi[j].Actor)
return iName < jName
}
return bpi[i].CurrentVotes > bpi[j].CurrentVotes
})
return bpi, nil
}
var RefreshVotesChan = make(chan bool)
func VoteContent(content chan fyne.CanvasObject, refresh chan bool) {
pp := message.NewPrinter(language.AmericanEnglish)
r := regexp.MustCompile("(?m)^-")
table := func() fyne.CanvasObject {
bpi, err := getBpInfo(string(Account.Actor), Api)
if err != nil {
return widget.NewLabel(err.Error())
}
voteRowsBox := widget.NewVBox()
origVotes := make(map[string]bool)
curVotes := make(map[string]bool)
voteButton := &widget.Button{}
countLabel := widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
myAddrs := func() []string {
names := make([]string, 0)
_, _, _ = Account.GetNames(Api)
for _, n := range Account.Addresses {
names = append(names, n.FioAddress)
}
return names
}()
addrsSelect := widget.NewSelect(myAddrs, func(s string) {
fee := fio.GetMaxFee(`vote_producer`)
if err == nil {
voteButton.SetText(pp.Sprintf("Vote! %s %g", fio.FioSymbol, fee))
}
})
voteButton = widget.NewButtonWithIcon("Vote!", fioassets.NewFioLogoResource(), func() {
go func() {
prods := make([]string, 0)
for k, v := range curVotes {
if v {
prods = append(prods, k)
}
}
vp := fio.NewVoteProducer(prods, Account.Actor, addrsSelect.Selected)
var result string
resp, err := Api.SignPushTransaction(fio.NewTransaction(
[]*fio.Action{vp},
Opts,
),
Opts.ChainID,
fio.CompressionNone,
)
if err != nil {
result = err.Error()
errs.ErrChan <- err.Error()
} else {
j, err := json.MarshalIndent(resp, "", " ")
if err != nil {
errs.ErrChan <- err.Error()
return
}
result = string(j)
}
content := fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(RWidth()/2, PctHeight()-250/2)),
widget.NewScrollContainer(
widget.NewLabelWithStyle(result, fyne.TextAlignLeading, fyne.TextStyle{Monospace: true}),
))
dialog.ShowCustom("voteproducer result", "OK", content, Win)
go func() {
time.Sleep(100 * time.Millisecond)
RefreshVotesChan <- true
}()
}()
})
refreshButton := widget.NewButtonWithIcon("Refresh", theme.ViewRefreshIcon(), func() {
RefreshVotesChan <- true
})
if len(myAddrs) > 0 {
addrsSelect.SetSelected(myAddrs[0])
}
performVoteBox := widget.NewHBox(
layout.NewSpacer(),
countLabel,
widget.NewLabel("vote as:"),
addrsSelect,
voteButton,
refreshButton,
layout.NewSpacer(),
)
refreshTopChan := make(chan map[string]bool)
go func() {
for _, gvc := range bpi {
if gvc.VoteFor {
origVotes[gvc.FioAddress] = true
curVotes[gvc.FioAddress] = true
}
}
for {
select {
case votee := <-refreshTopChan:
for k, v := range votee {
curVotes[k] = v
}
var votes, changed int
for k, v := range curVotes {
if origVotes[k] != v {
changed = changed + 1
}
if v {
votes = votes + 1
}
}
countLabel.SetText(fmt.Sprintf("%d of 30 votes selected", votes))
if changed != 0 && votes <= 30 && addrsSelect.Selected != "" && addrsSelect.Selected != "(Select one)" {
voteButton.Enable()
continue
}
voteButton.Disable()
}
}
}()
moreInfoSize := widget.NewButtonWithIcon("more info", theme.InfoIcon(), func() {}).MinSize()
for rank, bp := range bpi {
func(rank int, bp bpInfo) {
voteContent := fyne.NewContainerWithLayout(layout.NewGridLayoutWithColumns(2))
var isTop string
if bp.Top21 {
isTop = "Top 21 "
}
boldName := widget.NewLabelWithStyle(bp.FioAddress, fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
boldName.Hide()
softName := widget.NewLabel(bp.FioAddress)
//selected := canvas.NewImageFromImage(votedImg())
selected := canvas.NewImageFromResource(theme.NavigateNextIcon())
selected.Hide()
voteCheck := widget.NewCheck("Vote", func(b bool) {
bp.VoteFor = b
refreshTopChan <- map[string]bool{bp.FioAddress: b}
switch {
case bp.OrigVoteFor && bp.VoteFor:
selected.Resource = theme.NavigateNextIcon()
selected.Show()
case !bp.OrigVoteFor && bp.VoteFor:
selected.Resource = theme.ContentAddIcon()
selected.Show()
case bp.OrigVoteFor && !bp.VoteFor:
selected.Resource = theme.DeleteIcon()
selected.Show()
default:
selected.Hide()
}
selected.Refresh()
if b {
boldName.Show()
softName.Hide()
return
}
boldName.Hide()
softName.Show()
})
voteCheck.SetChecked(bp.VoteFor)
tied := widget.NewLabelWithStyle(" ", fyne.TextAlignLeading, fyne.TextStyle{Monospace: true})
if bp.Tied && bp.CurrentVotes > 0 {
tied.SetText(" * ")
}
voteContent.AddObject(widget.NewHBox(
fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(40, 40)), selected),
fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(60, 40)), widget.NewLabel(isTop)),
fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(40, 40)), bp.Img),
voteCheck, boldName, softName, layout.NewSpacer(),
widget.NewLabelWithStyle(
pp.Sprintf("%s %d voted - rank #%3d", fio.FioSymbol, int64(math.Round(bp.CurrentVotes))/1_000_000_000, rank+1),
fyne.TextAlignTrailing,
fyne.TextStyle{}),
tied,
))
moreInfo := widget.NewButtonWithIcon("more info", theme.InfoIcon(), func() {
var yContent string
y, err := yaml.Marshal(bp.BpJson)
if err != nil {
yContent = err.Error()
} else {
yContent = string(y)
}
mi := widget.NewMultiLineEntry() //yContent, fyne.TextAlignLeading, fyne.TextStyle{Monospace: true})
txt := r.ReplaceAllString(yContent, "\n-")
func(txt string) {
mi.OnChanged = func(string) {
mi.SetText(txt)
}
}(txt)
mi.SetText(txt)
content := fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(RWidth()/2, PctHeight()-250-performVoteBox.MinSize().Height/2)),
widget.NewScrollContainer(
mi,
))
dialog.ShowCustom(bp.FioAddress, "OK", content, Win)
})
if bp.BpJson == nil {
moreInfo.Hide()
bp.Img.Hide()
}
nameBox := widget.NewHBox()
if !strings.HasPrefix(bp.Url, "http") && bp.Url != "" {
bp.Url = "http://" + bp.Url
}
u, err := url.Parse(bp.Url)
nameBox.Append(fyne.NewContainerWithLayout(layout.NewFixedGridLayout(moreInfoSize), moreInfo))
if err == nil {
nameBox.Append(widget.NewHyperlinkWithStyle(u.Host, u, fyne.TextAlignLeading, fyne.TextStyle{Bold: true}))
}
nameBox.Append(layout.NewSpacer())
//nameBox.Append(widget.NewLabelWithStyle(" ", fyne.TextAlignTrailing, fyne.TextStyle{Monospace: true}))
voteContent.AddObject(nameBox)
voteRowsBox.Append(voteContent)
}(rank, bp)
}
voteRowsBox.Append(fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.Size{
Width: 20,
Height: 50,
})))
return widget.NewVBox(
performVoteBox,
fyne.NewContainerWithLayout(layout.NewFixedGridLayout(fyne.NewSize(RWidth(), PctHeight()-250-performVoteBox.MinSize().Height)),
widget.NewGroupWithScroller("Producer Candidates", voteRowsBox),
))
}
content <- widget.NewLabel("Please wait, updating producer information ....")
content <- table()
for {
select {
case <-refresh:
content <- widget.NewLabel("Please wait, updating producer information ....")
content <- table()
}
}
}