-
Notifications
You must be signed in to change notification settings - Fork 22
/
vote_spam_policy.go
274 lines (236 loc) · 9.05 KB
/
vote_spam_policy.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package spam
import (
"encoding/hex"
"errors"
"sort"
"strings"
"sync"
"code.vegaprotocol.io/vega/core/blockchain/abci"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
protoapi "code.vegaprotocol.io/vega/protos/vega/api/v1"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
var (
// ErrInsufficientTokensForVoting is returned when the party has insufficient tokens for voting.
ErrInsufficientTokensForVoting = errors.New("party has insufficient associated governance tokens in their staking account to submit votes")
// ErrTooManyVotes is returned when the party has voted already the maximum allowed votes per proposal per epoch.
ErrTooManyVotes = errors.New("party has already voted the maximum number of times per proposal per epoch")
)
type VoteSpamPolicy struct {
log *logging.Logger
numVotes uint64
minVotingTokens *num.Uint
accounts StakingAccounts
minTokensParamName string
maxAllowedParamName string
partyToVote map[string]map[string]uint64 // those are votes that are already on blockchain
blockPartyToVote map[string]map[string]uint64 // votes in the current block
currentEpochSeq uint64 // the sequence id of the current epoch
lock sync.RWMutex // global lock to sync calls from multiple tendermint threads
}
// NewVoteSpamPolicy instantiates vote spam policy.
func NewVoteSpamPolicy(minTokensParamName string, maxAllowedParamName string, log *logging.Logger, accounts StakingAccounts) *VoteSpamPolicy {
return &VoteSpamPolicy{
log: log,
accounts: accounts,
minVotingTokens: num.NewUint(1),
partyToVote: map[string]map[string]uint64{},
blockPartyToVote: map[string]map[string]uint64{},
lock: sync.RWMutex{},
minTokensParamName: minTokensParamName,
maxAllowedParamName: maxAllowedParamName,
}
}
func (vsp *VoteSpamPolicy) Serialise() ([]byte, error) {
partyProposalVoteCount := []*types.PartyProposalVoteCount{}
for party, proposalToCount := range vsp.partyToVote {
for proposal, count := range proposalToCount {
partyProposalVoteCount = append(partyProposalVoteCount, &types.PartyProposalVoteCount{
Party: party,
Proposal: proposal,
Count: count,
})
}
}
sort.SliceStable(partyProposalVoteCount, func(i, j int) bool {
switch strings.Compare(partyProposalVoteCount[i].Party, partyProposalVoteCount[j].Party) {
case -1:
return true
case 1:
return false
}
return partyProposalVoteCount[i].Proposal < partyProposalVoteCount[j].Proposal
})
payload := types.Payload{
Data: &types.PayloadVoteSpamPolicy{
VoteSpamPolicy: &types.VoteSpamPolicy{
PartyProposalVoteCount: partyProposalVoteCount,
CurrentEpochSeq: vsp.currentEpochSeq,
},
},
}
return proto.Marshal(payload.IntoProto())
}
func (vsp *VoteSpamPolicy) Deserialise(p *types.Payload) error {
pl := p.Data.(*types.PayloadVoteSpamPolicy).VoteSpamPolicy
vsp.partyToVote = map[string]map[string]uint64{}
for _, ptv := range pl.PartyProposalVoteCount {
if _, ok := vsp.partyToVote[ptv.Party]; !ok {
vsp.partyToVote[ptv.Party] = map[string]uint64{}
}
vsp.partyToVote[ptv.Party][ptv.Proposal] = ptv.Count
}
vsp.currentEpochSeq = pl.CurrentEpochSeq
return nil
}
// UpdateUintParam is called to update Uint net params for the policy
// Specifically the min tokens required for voting.
func (vsp *VoteSpamPolicy) UpdateUintParam(name string, value *num.Uint) error {
if name == vsp.minTokensParamName {
vsp.minVotingTokens = value.Clone()
} else {
return errors.New("unknown parameter for vote spam policy")
}
return nil
}
// UpdateIntParam is called to update iint net params for the policy
// Specifically the number of votes to a proposal a party can submit in an epoch.
func (vsp *VoteSpamPolicy) UpdateIntParam(name string, value int64) error {
if name == vsp.maxAllowedParamName {
vsp.numVotes = uint64(value)
} else {
return errors.New("unknown parameter for vote spam policy")
}
return nil
}
// Reset is called at the beginning of an epoch to reset the settings for the epoch.
func (vsp *VoteSpamPolicy) Reset(epoch types.Epoch) {
vsp.lock.Lock()
defer vsp.lock.Unlock()
// reset the token count factor to 1
vsp.currentEpochSeq = epoch.Seq
// reset vote counts
vsp.partyToVote = map[string]map[string]uint64{}
// reset current block vote counts
vsp.blockPartyToVote = map[string]map[string]uint64{}
}
func (vsp *VoteSpamPolicy) UpdateTx(tx abci.Tx) {
vsp.lock.Lock()
defer vsp.lock.Unlock()
if _, ok := vsp.partyToVote[tx.Party()]; !ok {
vsp.partyToVote[tx.Party()] = map[string]uint64{}
}
vote := &commandspb.VoteSubmission{}
tx.Unmarshal(vote)
if _, ok := vsp.partyToVote[tx.Party()][vote.ProposalId]; !ok {
vsp.partyToVote[tx.Party()][vote.ProposalId] = 0
}
vsp.partyToVote[tx.Party()][vote.ProposalId] = vsp.partyToVote[tx.Party()][vote.ProposalId] + 1
}
func (vsp *VoteSpamPolicy) RollbackProposal() {
vsp.blockPartyToVote = map[string]map[string]uint64{}
}
func (vsp *VoteSpamPolicy) CheckBlockTx(tx abci.Tx) error {
party := tx.Party()
vsp.lock.Lock()
defer vsp.lock.Unlock()
vote := &commandspb.VoteSubmission{}
if err := tx.Unmarshal(vote); err != nil {
return err
}
// get number of votes preceding the block in this epoch
var epochVotes uint64
if partyVotes, ok := vsp.partyToVote[party]; ok {
if voteCount, ok := partyVotes[vote.ProposalId]; ok {
epochVotes = voteCount
}
}
// get number of votes so far in current block
var blockVotes uint64
if proposals, ok := vsp.blockPartyToVote[party]; ok {
if votes, ok := proposals[vote.ProposalId]; ok {
blockVotes += votes
}
}
// if too many votes in total - reject and update counters
if epochVotes+blockVotes >= vsp.numVotes {
return ErrTooManyVotes
}
// update vote counters for party/proposal votes
if _, ok := vsp.blockPartyToVote[party]; !ok {
vsp.blockPartyToVote[party] = map[string]uint64{}
}
if votes, ok := vsp.blockPartyToVote[party][vote.ProposalId]; !ok {
vsp.blockPartyToVote[party][vote.ProposalId] = 1
} else {
vsp.blockPartyToVote[party][vote.ProposalId] = votes + 1
}
return nil
}
// PreBlockAccept checks if the vote should be rejected as spam or not based on the number of votes in current epoch's preceding blocks and the number of tokens
// held by the party.
// NB: this is done at mempool before adding to block.
func (vsp *VoteSpamPolicy) PreBlockAccept(tx abci.Tx) error {
party := tx.Party()
vsp.lock.RLock()
defer vsp.lock.RUnlock()
// check if the party has enough balance to submit votes
balance, err := vsp.accounts.GetAvailableBalance(party)
if err != nil || balance.LT(vsp.minVotingTokens) {
if vsp.log.GetLevel() <= logging.DebugLevel {
vsp.log.Debug("Spam pre: party has insufficient balance for voting", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("party", party), logging.String("balance", num.UintToString(balance)))
}
return ErrInsufficientTokensForVoting
}
vote := &commandspb.VoteSubmission{}
if err := tx.Unmarshal(vote); err != nil {
return err
}
// Check we have not exceeded our vote limit for this given proposal in this epoch
if partyVotes, ok := vsp.partyToVote[party]; ok {
if voteCount, ok := partyVotes[vote.ProposalId]; ok && voteCount >= vsp.numVotes {
if vsp.log.GetLevel() <= logging.DebugLevel {
vsp.log.Debug("Spam pre: party has already voted for proposal the max amount of votes", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("party", party), logging.String("proposal", vote.ProposalId), logging.Uint64("voteCount", voteCount), logging.Uint64("maxAllowed", vsp.numVotes))
}
return ErrTooManyVotes
}
}
return nil
}
func (vsp *VoteSpamPolicy) GetSpamStats(_ string) *protoapi.SpamStatistic {
return nil
}
func (vsp *VoteSpamPolicy) GetVoteSpamStats(partyID string) *protoapi.VoteSpamStatistics {
vsp.lock.RLock()
defer vsp.lock.RUnlock()
partyStats := vsp.partyToVote[partyID]
stats := make([]*protoapi.VoteSpamStatistic, 0, len(partyStats))
for proposal, votes := range partyStats {
stats = append(stats, &protoapi.VoteSpamStatistic{
Proposal: proposal,
CountForEpoch: votes,
MinTokensRequired: vsp.minVotingTokens.String(),
})
}
return &protoapi.VoteSpamStatistics{
Statistics: stats,
MaxForEpoch: vsp.numVotes,
}
}