-
Notifications
You must be signed in to change notification settings - Fork 22
/
engine.go
367 lines (319 loc) · 16.1 KB
/
engine.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
// 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 (
"context"
"encoding/hex"
"sync"
"code.vegaprotocol.io/vega/core/blockchain/abci"
"code.vegaprotocol.io/vega/core/netparams"
"code.vegaprotocol.io/vega/core/txn"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
protoapi "code.vegaprotocol.io/vega/protos/vega/api/v1"
)
type StakingAccounts interface {
GetAvailableBalance(party string) (*num.Uint, error)
}
type EpochEngine interface {
NotifyOnEpoch(f func(context.Context, types.Epoch), r func(context.Context, types.Epoch))
}
type Engine struct {
log *logging.Logger
config Config
accounting StakingAccounts
cfgMu sync.Mutex
transactionTypeToPolicy map[txn.Command]Policy
currentEpoch *types.Epoch
policyNameToPolicy map[string]Policy
hashKeys []string
noSpamProtection bool // flag that disables chesk for the spam policies, that is useful for the nullchain
}
type Policy interface {
Reset(epoch types.Epoch)
UpdateTx(tx abci.Tx)
RollbackProposal()
CheckBlockTx(abci.Tx) error
PreBlockAccept(tx abci.Tx) error
UpdateUintParam(name string, value *num.Uint) error
UpdateIntParam(name string, value int64) error
Serialise() ([]byte, error)
Deserialise(payload *types.Payload) error
GetSpamStats(partyID string) *protoapi.SpamStatistic
GetVoteSpamStats(partyID string) *protoapi.VoteSpamStatistics
}
// ReloadConf updates the internal configuration of the spam engine.
func (e *Engine) ReloadConf(cfg Config) {
e.log.Info("reloading configuration")
if e.log.GetLevel() != cfg.Level.Get() {
e.log.Info("updating log level",
logging.String("old", e.log.GetLevel().String()),
logging.String("new", cfg.Level.String()),
)
e.log.SetLevel(cfg.Level.Get())
}
e.cfgMu.Lock()
e.config = cfg
e.cfgMu.Unlock()
}
// New instantiates a new spam engine.
func New(log *logging.Logger, config Config, epochEngine EpochEngine, accounting StakingAccounts) *Engine {
log = log.Named(namedLogger)
log.SetLevel(config.Level.Get())
e := &Engine{
config: config,
log: log,
accounting: accounting,
transactionTypeToPolicy: map[txn.Command]Policy{},
}
// simple policies
proposalPolicy := NewSimpleSpamPolicy("proposal", netparams.SpamProtectionMinTokensForProposal, netparams.SpamProtectionMaxProposals, log, accounting)
valJoinPolicy := NewSimpleSpamPolicy("validatorJoin", netparams.StakingAndDelegationRewardMinimumValidatorStake, "", log, accounting)
delegationPolicy := NewSimpleSpamPolicy("delegation", netparams.SpamProtectionMinTokensForDelegation, netparams.SpamProtectionMaxDelegations, log, accounting)
transferPolicy := NewSimpleSpamPolicy("transfer", "", netparams.TransferMaxCommandsPerEpoch, log, accounting)
issuesSignaturesPolicy := NewSimpleSpamPolicy("issueSignature", netparams.SpamProtectionMinMultisigUpdates, netparams.SpamProtectionMaxMultisigUpdates, log, accounting)
createReferralSetPolicy := NewSimpleSpamPolicy("createReferralSet", netparams.ReferralProgramMinStakedVegaTokens, netparams.SpamProtectionMaxCreateReferralSet, log, accounting)
updateReferralSetPolicy := NewSimpleSpamPolicy("updateReferralSet", netparams.ReferralProgramMinStakedVegaTokens, netparams.SpamProtectionMaxUpdateReferralSet, log, accounting)
applyReferralCodePolicy := NewSimpleSpamPolicy("applyReferralCode", "", netparams.SpamProtectionMaxApplyReferralCode, log, accounting)
updatePartyProfilePolicy := NewSimpleSpamPolicy("updatePartyProfile", "", netparams.SpamProtectionMaxUpdatePartyProfile, log, accounting)
// complex policies
votePolicy := NewVoteSpamPolicy(netparams.SpamProtectionMinTokensForVoting, netparams.SpamProtectionMaxVotes, log, accounting)
voteKey := (&types.PayloadVoteSpamPolicy{}).Key()
e.policyNameToPolicy = map[string]Policy{
proposalPolicy.policyName: proposalPolicy,
valJoinPolicy.policyName: valJoinPolicy,
delegationPolicy.policyName: delegationPolicy,
transferPolicy.policyName: transferPolicy,
issuesSignaturesPolicy.policyName: issuesSignaturesPolicy,
voteKey: votePolicy,
createReferralSetPolicy.policyName: createReferralSetPolicy,
updateReferralSetPolicy.policyName: updateReferralSetPolicy,
applyReferralCodePolicy.policyName: applyReferralCodePolicy,
updatePartyProfilePolicy.policyName: updatePartyProfilePolicy,
}
e.hashKeys = []string{
proposalPolicy.policyName,
valJoinPolicy.policyName,
delegationPolicy.policyName,
transferPolicy.policyName,
issuesSignaturesPolicy.policyName,
createReferralSetPolicy.policyName,
updateReferralSetPolicy.policyName,
applyReferralCodePolicy.policyName,
updatePartyProfilePolicy.policyName,
voteKey,
}
e.transactionTypeToPolicy[txn.ProposeCommand] = proposalPolicy
e.transactionTypeToPolicy[txn.BatchProposeCommand] = proposalPolicy
e.transactionTypeToPolicy[txn.AnnounceNodeCommand] = valJoinPolicy
e.transactionTypeToPolicy[txn.DelegateCommand] = delegationPolicy
e.transactionTypeToPolicy[txn.UndelegateCommand] = delegationPolicy
e.transactionTypeToPolicy[txn.TransferFundsCommand] = transferPolicy
e.transactionTypeToPolicy[txn.CancelTransferFundsCommand] = transferPolicy
e.transactionTypeToPolicy[txn.IssueSignatures] = issuesSignaturesPolicy
e.transactionTypeToPolicy[txn.VoteCommand] = votePolicy
e.transactionTypeToPolicy[txn.CreateReferralSetCommand] = createReferralSetPolicy
e.transactionTypeToPolicy[txn.UpdateReferralSetCommand] = updateReferralSetPolicy
e.transactionTypeToPolicy[txn.ApplyReferralCodeCommand] = applyReferralCodePolicy
e.transactionTypeToPolicy[txn.UpdatePartyProfileCommand] = updatePartyProfilePolicy
// register for epoch end notifications
epochEngine.NotifyOnEpoch(e.OnEpochEvent, e.OnEpochRestore)
e.log.Info("Spam protection started")
return e
}
func (e *Engine) DisableSpamProtection() {
e.log.Infof("Disabling spam protection for the Spam Engine")
e.noSpamProtection = true
}
// OnCreateReferralSet is called when the net param for max create referral set per epoch has changed.
func (e *Engine) OnMaxCreateReferralSet(ctx context.Context, max int64) error {
return e.transactionTypeToPolicy[txn.CreateReferralSetCommand].UpdateIntParam(netparams.SpamProtectionMaxCreateReferralSet, max)
}
// OnMaxPartyProfileUpdate is called when the net param for max update party profile per epoch has changed.
func (e *Engine) OnMaxPartyProfile(ctx context.Context, max int64) error {
return e.transactionTypeToPolicy[txn.UpdatePartyProfileCommand].UpdateIntParam(netparams.SpamProtectionMaxUpdatePartyProfile, max)
}
// OnMaxUpdateReferralSet is called when the net param for max update referral set per epoch has changed.
func (e *Engine) OnMaxUpdateReferralSet(ctx context.Context, max int64) error {
return e.transactionTypeToPolicy[txn.UpdateReferralSetCommand].UpdateIntParam(netparams.SpamProtectionMaxUpdateReferralSet, max)
}
// OnMaxApplyReferralCode is called when the net param for max update referral set per epoch has changed.
func (e *Engine) OnMaxApplyReferralCode(ctx context.Context, max int64) error {
return e.transactionTypeToPolicy[txn.ApplyReferralCodeCommand].UpdateIntParam(netparams.SpamProtectionMaxApplyReferralCode, max)
}
// OnMinTokensForReferral is called when the net param for min staked tokens requirement for referral set create/update has changed.
func (e *Engine) OnMinTokensForReferral(ctx context.Context, minTokens *num.Uint) error {
err := e.transactionTypeToPolicy[txn.CreateReferralSetCommand].UpdateUintParam(netparams.ReferralProgramMinStakedVegaTokens, minTokens)
if err != nil {
return err
}
return e.transactionTypeToPolicy[txn.UpdateReferralSetCommand].UpdateUintParam(netparams.ReferralProgramMinStakedVegaTokens, minTokens)
}
// OnMaxDelegationsChanged is called when the net param for max delegations per epoch has changed.
func (e *Engine) OnMaxDelegationsChanged(ctx context.Context, maxDelegations int64) error {
return e.transactionTypeToPolicy[txn.DelegateCommand].UpdateIntParam(netparams.SpamProtectionMaxDelegations, maxDelegations)
}
// OnMinTokensForDelegationChanged is called when the net param for min tokens requirement for voting has changed.
func (e *Engine) OnMinTokensForDelegationChanged(ctx context.Context, minTokens num.Decimal) error {
minTokensFoDelegation, _ := num.UintFromDecimal(minTokens)
return e.transactionTypeToPolicy[txn.DelegateCommand].UpdateUintParam(netparams.SpamProtectionMinTokensForDelegation, minTokensFoDelegation)
}
// OnMaxVotesChanged is called when the net param for max votes per epoch has changed.
func (e *Engine) OnMaxVotesChanged(ctx context.Context, maxVotes int64) error {
return e.transactionTypeToPolicy[txn.VoteCommand].UpdateIntParam(netparams.SpamProtectionMaxVotes, maxVotes)
}
// OnMinTokensForVotingChanged is called when the net param for min tokens requirement for voting has changed.
func (e *Engine) OnMinTokensForVotingChanged(ctx context.Context, minTokens num.Decimal) error {
minTokensForVoting, _ := num.UintFromDecimal(minTokens)
return e.transactionTypeToPolicy[txn.VoteCommand].UpdateUintParam(netparams.SpamProtectionMinTokensForVoting, minTokensForVoting)
}
// OnMaxProposalsChanged is called when the net param for max proposals per epoch has changed.
func (e *Engine) OnMaxProposalsChanged(ctx context.Context, maxProposals int64) error {
return e.transactionTypeToPolicy[txn.ProposeCommand].UpdateIntParam(netparams.SpamProtectionMaxProposals, maxProposals)
}
// OnMinTokensForProposalChanged is called when the net param for min tokens requirement for submitting a proposal has changed.
func (e *Engine) OnMinTokensForProposalChanged(ctx context.Context, minTokens num.Decimal) error {
minTokensForProposal, _ := num.UintFromDecimal(minTokens)
return e.transactionTypeToPolicy[txn.ProposeCommand].UpdateUintParam(netparams.SpamProtectionMinTokensForProposal, minTokensForProposal)
}
// OnMaxTransfersChanged is called when the net param for max transfers per epoch changes.
func (e *Engine) OnMaxTransfersChanged(_ context.Context, maxTransfers int64) error {
return e.transactionTypeToPolicy[txn.TransferFundsCommand].UpdateIntParam(netparams.TransferMaxCommandsPerEpoch, maxTransfers)
}
// OnMinValidatorTokensChanged is called when the net param for min tokens for joining validator changes.
func (e *Engine) OnMinValidatorTokensChanged(_ context.Context, minTokens num.Decimal) error {
minTokensForJoiningValidator, _ := num.UintFromDecimal(minTokens)
return e.transactionTypeToPolicy[txn.AnnounceNodeCommand].UpdateUintParam(netparams.StakingAndDelegationRewardMinimumValidatorStake, minTokensForJoiningValidator)
}
// OnMinTokensForProposalChanged is called when the net param for min tokens requirement for submitting a proposal has changed.
func (e *Engine) OnMinTokensForMultisigUpdatesChanged(_ context.Context, minTokens num.Decimal) error {
minTokensForMultisigUpdates, _ := num.UintFromDecimal(minTokens)
return e.transactionTypeToPolicy[txn.IssueSignatures].UpdateUintParam(netparams.SpamProtectionMinMultisigUpdates, minTokensForMultisigUpdates)
}
// OnMinTokensForProposalChanged is called when the net param for min tokens requirement for submitting a proposal has changed.
func (e *Engine) OnMaxMultisigUpdatesChanged(_ context.Context, maxUpdates int64) error {
return e.transactionTypeToPolicy[txn.IssueSignatures].UpdateIntParam(netparams.SpamProtectionMaxMultisigUpdates, maxUpdates)
}
// OnEpochEvent is a callback for epoch events.
func (e *Engine) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
e.log.Info("Spam protection OnEpochEvent called", logging.Uint64("epoch", epoch.Seq))
if e.noSpamProtection {
e.log.Info("Spam protection OnEpochEvent disabled", logging.Uint64("epoch", epoch.Seq))
return
}
if e.currentEpoch == nil || e.currentEpoch.Seq != epoch.Seq {
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("Spam protection new epoch started", logging.Uint64("epochSeq", epoch.Seq))
}
e.currentEpoch = &epoch
for _, policy := range e.transactionTypeToPolicy {
policy.Reset(epoch)
}
}
}
func (e *Engine) BeginBlock(txs []abci.Tx) {
for _, tx := range txs {
if _, ok := e.transactionTypeToPolicy[tx.Command()]; !ok {
continue
}
e.transactionTypeToPolicy[tx.Command()].UpdateTx(tx)
}
}
func (e *Engine) EndPrepareProposal() {
for _, policy := range e.transactionTypeToPolicy {
policy.RollbackProposal()
}
}
// PreBlockAccept is called from onCheckTx before a tx is added to mempool
// returns false is rejected by spam engine with a corresponding error.
func (e *Engine) PreBlockAccept(tx abci.Tx) error {
command := tx.Command()
if _, ok := e.transactionTypeToPolicy[command]; !ok {
return nil
}
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("Spam protection PreBlockAccept called for policy", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("command", command.String()))
}
if e.noSpamProtection {
e.log.Debug("Spam protection PreBlockAccept disabled for policy", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("command", command.String()))
return nil
}
return e.transactionTypeToPolicy[command].PreBlockAccept(tx)
}
func (e *Engine) ProcessProposal(txs []abci.Tx) bool {
success := true
for _, tx := range txs {
command := tx.Command()
if _, ok := e.transactionTypeToPolicy[command]; !ok {
continue
}
if e.noSpamProtection {
e.log.Debug("Spam protection PreBlockAccept disabled for policy", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("command", command.String()))
continue
}
if err := e.transactionTypeToPolicy[command].CheckBlockTx(tx); err != nil {
success = false
}
}
for _, p := range e.transactionTypeToPolicy {
p.RollbackProposal()
}
return success
}
// PostBlockAccept is called from onDeliverTx before the block is processed
// returns false is rejected by spam engine with a corresponding error.
func (e *Engine) CheckBlockTx(tx abci.Tx) error {
command := tx.Command()
if _, ok := e.transactionTypeToPolicy[command]; !ok {
return nil
}
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("Spam protection PostBlockAccept called for policy", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("command", command.String()))
}
if e.noSpamProtection {
e.log.Debug("Spam protection PreBlockAccept disabled for policy", logging.String("txHash", hex.EncodeToString(tx.Hash())), logging.String("command", command.String()))
return nil
}
return e.transactionTypeToPolicy[command].CheckBlockTx(tx)
}
func (e *Engine) GetSpamStatistics(partyID string) *protoapi.SpamStatistics {
stats := &protoapi.SpamStatistics{}
for txType, policy := range e.transactionTypeToPolicy {
switch txType {
case txn.ProposeCommand, txn.BatchProposeCommand:
stats.Proposals = policy.GetSpamStats(partyID)
case txn.DelegateCommand:
stats.Delegations = policy.GetSpamStats(partyID)
case txn.TransferFundsCommand:
stats.Transfers = policy.GetSpamStats(partyID)
case txn.AnnounceNodeCommand:
stats.NodeAnnouncements = policy.GetSpamStats(partyID)
case txn.IssueSignatures:
stats.IssueSignatures = policy.GetSpamStats(partyID)
case txn.VoteCommand:
stats.Votes = policy.GetVoteSpamStats(partyID)
case txn.CreateReferralSetCommand:
stats.CreateReferralSet = policy.GetSpamStats(partyID)
case txn.UpdateReferralSetCommand:
stats.UpdateReferralSet = policy.GetSpamStats(partyID)
case txn.ApplyReferralCodeCommand:
stats.ApplyReferralCode = policy.GetSpamStats(partyID)
default:
continue
}
}
return stats
}