-
Notifications
You must be signed in to change notification settings - Fork 14
/
votes.go
55 lines (47 loc) · 1.11 KB
/
votes.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
package consensus
import (
"sync"
"github.com/qlcchain/go-qlc/common/types"
"github.com/qlcchain/go-qlc/p2p/protos"
)
type tallyResult byte
const (
vote tallyResult = iota
changed
confirm
)
type Votes struct {
id types.Hash //Previous block of fork
repVotes *sync.Map // All votes received by account
}
func NewVotes(blk *types.StateBlock) *Votes {
return &Votes{
id: blk.Parent(),
repVotes: new(sync.Map),
}
}
func (vs *Votes) voteExit(address types.Address) (bool, *protos.ConfirmAckBlock) {
if v, ok := vs.repVotes.Load(address); !ok {
return false, nil
} else {
return true, v.(*protos.ConfirmAckBlock)
}
}
func (vs *Votes) voteStatus(va *protos.ConfirmAckBlock) tallyResult {
var result tallyResult
if v, ok := vs.repVotes.Load(va.Account); !ok {
result = vote
vs.repVotes.Store(va.Account, va)
} else {
if v.(*protos.ConfirmAckBlock).Blk.GetHash() != va.Blk.GetHash() {
//Rep changed their vote
result = changed
vs.repVotes.Delete(va.Account)
vs.repVotes.Store(va.Account, va)
} else {
// Rep vote remained the same
result = confirm
}
}
return result
}