forked from thetatoken/theta-protocol-ledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elite_edge_node_vote_bookkeeper.go
146 lines (116 loc) · 3.08 KB
/
elite_edge_node_vote_bookkeeper.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
package consensus
import (
"container/list"
"encoding/hex"
"fmt"
"sync"
"time"
tcore "github.com/thetatoken/theta/core"
tcrypto "github.com/thetatoken/theta/crypto"
)
const DefaultMaxNumVotesCached = uint(5000000)
const maxVoteLife = 20 * time.Minute // more than one checkpoint interval
//
// EENVoteBookkeeper keeps tracks of recently seen elite edge node votes
//
type EENVoteBookkeeper struct {
mutex *sync.Mutex
voteMap map[string]*EENVoteRecord // map: vote hash -> record
voteList list.List // FIFO list of vote hashes
maxNumVotes uint
}
type EENVoteRecord struct {
Hash string
Count uint
CreatedAt time.Time
}
func (r *EENVoteRecord) IsOutdated() bool {
return time.Since(r.CreatedAt) > maxVoteLife
}
type TxStatus int
const (
TxStatusPending TxStatus = iota
TxStatusAbandoned
)
func CreateEENVoteBookkeeper(maxNumTxs uint) *EENVoteBookkeeper {
return &EENVoteBookkeeper{
mutex: &sync.Mutex{},
voteMap: make(map[string]*EENVoteRecord),
maxNumVotes: maxNumTxs,
}
}
func (vb *EENVoteBookkeeper) reset() {
vb.mutex.Lock()
defer vb.mutex.Unlock()
vb.voteMap = make(map[string]*EENVoteRecord)
vb.voteList.Init()
}
func (vb *EENVoteBookkeeper) ReceiveCount(vote *tcore.EENVote) uint {
vb.mutex.Lock()
defer vb.mutex.Unlock()
// Remove outdated Tx records
vb.removeOutdatedVotesUnsafe()
voteHash := getVoteHash(vote)
voteRecord, exists := vb.voteMap[voteHash]
if !exists || voteRecord == nil {
return 0
}
return voteRecord.Count
}
func (vb *EENVoteBookkeeper) HasSeen(vote *tcore.EENVote) bool {
vb.mutex.Lock()
defer vb.mutex.Unlock()
// Remove outdated Tx records
vb.removeOutdatedVotesUnsafe()
voteHash := getVoteHash(vote)
_, exists := vb.voteMap[voteHash]
return exists
}
func (vb *EENVoteBookkeeper) removeOutdatedVotesUnsafe() {
// Loop and remove all outdated Tx records
for {
el := vb.voteList.Front()
if el == nil {
return
}
voteRecord := el.Value.(*EENVoteRecord)
if !voteRecord.IsOutdated() {
return
}
if _, exists := vb.voteMap[voteRecord.Hash]; exists {
delete(vb.voteMap, voteRecord.Hash)
}
vb.voteList.Remove(el)
}
}
func (vb *EENVoteBookkeeper) Record(vote *tcore.EENVote) bool {
vb.mutex.Lock()
defer vb.mutex.Unlock()
voteHash := getVoteHash(vote)
// Remove outdated vote records
vb.removeOutdatedVotesUnsafe()
if existingVoteRecord, exists := vb.voteMap[voteHash]; exists {
existingVoteRecord.Count += 1
return true
}
if uint(vb.voteList.Len()) >= vb.maxNumVotes { // remove the oldest votes
popped := vb.voteList.Front()
poppedVoteHash := popped.Value.(*EENVoteRecord).Hash
delete(vb.voteMap, poppedVoteHash)
vb.voteList.Remove(popped)
}
record := &EENVoteRecord{
Hash: voteHash,
Count: 0,
CreatedAt: time.Now(),
}
vb.voteMap[voteHash] = record
vb.voteList.PushBack(record)
return true
}
func getVoteHash(vote *tcore.EENVote) string {
voteStr := fmt.Sprintf("%v:%v", vote.Address, vote.Block) // discard the height reported by the vote
txhash := tcrypto.Keccak256Hash([]byte(voteStr))
txhashStr := hex.EncodeToString(txhash[:])
return txhashStr
}