This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
/
spam_throttler.go
304 lines (251 loc) · 8.79 KB
/
spam_throttler.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
// Copyright 2021 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package blockchain
import (
"errors"
"sync"
"time"
"github.com/rcrowley/go-metrics"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
)
// TODO-Klaytn: move these variables into TxPool when BlockChain struct contains a TxPool interface
// spamThrottler need to be accessed by both of TxPool and BlockChain.
var (
spamThrottler *throttler = nil
spamThrottlerMu = new(sync.RWMutex)
)
var (
thresholdGauge = metrics.NewRegisteredGauge("txpool/throttler/threshold", nil)
candidateSizeGauge = metrics.NewRegisteredGauge("txpool/throttler/candidate/size", nil)
throttledSizeGauge = metrics.NewRegisteredGauge("txpool/throttler/throttled/size", nil)
allowedSizeGauge = metrics.NewRegisteredGauge("txpool/throttler/allowed/size", nil)
throttlerUpdateTimeGauge = metrics.NewRegisteredGauge("txpool/throttler/update/time", nil)
throttlerDropCount = metrics.NewRegisteredCounter("txpool/throttler/dropped/count", nil)
)
type throttler struct {
config *ThrottlerConfig
candidates map[common.Address]int // throttle candidates with spam weight. Not for concurrent use
throttled map[common.Address]int // throttled addresses with throttle time. Requires mu.lock for concurrent use
allowed map[common.Address]bool // white listed addresses. Requires mu.lock for concurrent use
mu *sync.RWMutex // mutex for throttled and allowed
threshold int
throttleCh chan *types.Transaction
quitCh chan struct{}
}
type ThrottlerConfig struct {
ActivateTxPoolSize uint `json:"activate_tx_pool_size"`
TargetFailRatio uint `json:"target_fail_ratio"`
ThrottleTPS uint `json:"throttle_tps"`
MaxCandidates uint `json:"max_candidates"`
IncreaseWeight int `json:"increase_weight"`
DecreaseWeight int `json:"decrease_weight"`
InitialThreshold int `json:"initial_threshold"`
MinimumThreshold int `json:"minimum_threshold"`
ThresholdAdjustment int `json:"threshold_adjustment"`
ThrottleSeconds int `json:"throttle_seconds"`
}
var DefaultSpamThrottlerConfig = &ThrottlerConfig{
ActivateTxPoolSize: 1000,
TargetFailRatio: 20,
ThrottleTPS: 100, // len(throttleCh) = ThrottleTPS * 5. 32KB * 100 * 5 = 16MB
MaxCandidates: 10000, // (20 + 4)B * 10000 = 240KB
IncreaseWeight: 5,
DecreaseWeight: 1,
InitialThreshold: 500,
MinimumThreshold: 100,
ThresholdAdjustment: 5,
ThrottleSeconds: 300,
}
func GetSpamThrottler() *throttler {
spamThrottlerMu.RLock()
t := spamThrottler
spamThrottlerMu.RUnlock()
return t
}
func validateConfig(conf *ThrottlerConfig) error {
if conf == nil {
return errors.New("nil ThrottlerConfig")
}
if conf.TargetFailRatio > 100 {
return errors.New("invalid ThrottlerConfig. 0 <= TargetFailRatio <= 100")
}
if conf.InitialThreshold < conf.MinimumThreshold {
return errors.New("invalid ThrottlerConfig. MinimumThreshold <= InitialThreshold")
}
return nil
}
// adjustThreshold adjusts the spam weight threshold of throttler in an adaptive way.
func (t *throttler) adjustThreshold(ratio uint) {
var newThreshold int
// Decrease threshold if a fail ratio is bigger than target value to put more addresses in throttled map
if ratio > t.config.TargetFailRatio {
if t.threshold-t.config.ThresholdAdjustment > t.config.MinimumThreshold {
newThreshold = t.threshold - t.config.ThresholdAdjustment
} else {
// Set minimum threshold
newThreshold = t.config.MinimumThreshold
}
// Increase threshold if a fail ratio is smaller than target ratio until it exceeds InitialThreshold
} else {
if t.threshold+t.config.ThresholdAdjustment < t.config.InitialThreshold {
newThreshold = t.threshold + t.config.ThresholdAdjustment
} else {
// Set maximum threshold
newThreshold = t.config.InitialThreshold
}
}
t.threshold = newThreshold
// Update metrics
thresholdGauge.Update(int64(newThreshold))
}
// newAllowed generates a new allowed list of throttler.
func (t *throttler) newAllowed(allowed []common.Address) {
t.mu.Lock()
defer t.mu.Unlock()
a := make(map[common.Address]bool, len(allowed))
for _, addr := range allowed {
a[addr] = true
}
t.allowed = a
}
// updateThrottled removes outdated addresses from the throttle list and adds new addresses to the list.
func (t *throttler) updateThrottled(newThrottled []common.Address) {
var removeThrottled []common.Address
t.mu.Lock()
defer t.mu.Unlock()
// Decrease throttling remained time for all throttled addresses.
for addr, remained := range t.throttled {
t.throttled[addr] = remained - 1
if t.throttled[addr] < 0 {
removeThrottled = append(removeThrottled, addr)
}
}
// Remove throttled addresses from throttled map.
for _, addr := range removeThrottled {
delete(t.throttled, addr)
}
for _, addr := range newThrottled {
t.throttled[addr] = t.config.ThrottleSeconds
}
// Update metrics
throttledSizeGauge.Update(int64(len(t.throttled)))
allowedSizeGauge.Update(int64(len(t.allowed)))
}
// updateThrottlerState updates the throttle list by calculating spam weight of candidates.
func (t *throttler) updateThrottlerState(txs types.Transactions, receipts types.Receipts) {
var removeCandidate []common.Address
var newThrottled []common.Address
startTime := time.Now()
numFailed := 0
failRatio := uint(0)
mapSize := uint(len(t.candidates))
// Increase spam weight of throttle candidates who generate failed txs.
for i, receipt := range receipts {
if receipt.Status != types.ReceiptStatusSuccessful {
numFailed++
toAddr := txs[i].To()
if toAddr == nil {
continue
}
weight := t.candidates[*toAddr]
if weight == 0 {
if mapSize >= t.config.MaxCandidates {
continue
}
mapSize++
}
t.candidates[*toAddr] = weight + t.config.IncreaseWeight
}
}
// Decrease spam weight for all candidates and update throttle lists in throttled.
for addr, weight := range t.candidates {
newWeight := weight - t.config.DecreaseWeight
switch {
case newWeight <= 0:
removeCandidate = append(removeCandidate, addr)
case newWeight > t.threshold:
removeCandidate = append(removeCandidate, addr)
newThrottled = append(newThrottled, addr)
default:
t.candidates[addr] = newWeight
}
}
// Remove throttle candidates from candidates map.
for _, addr := range removeCandidate {
delete(t.candidates, addr)
}
if len(receipts) != 0 {
failRatio = uint(100 * numFailed / len(receipts))
}
// Update throttled and threshold
t.updateThrottled(newThrottled)
t.adjustThreshold(failRatio)
// Update metrics
candidateSizeGauge.Update(int64(len(t.candidates)))
throttlerUpdateTimeGauge.Update(int64(time.Since(startTime)))
}
// classifyTxs classifies given txs into allowTxs and throttleTxs.
// If to-address of tx is listed in the throttle list, it is classified as throttleTx.
func (t *throttler) classifyTxs(txs types.Transactions) (types.Transactions, types.Transactions) {
allowTxs := txs[:0]
throttleTxs := txs[:0]
t.mu.RLock()
for _, tx := range txs {
if tx.To() != nil && t.throttled[*tx.To()] > 0 && t.allowed[*tx.To()] == false {
throttleTxs = append(throttleTxs, tx)
} else {
allowTxs = append(allowTxs, tx)
}
}
t.mu.RUnlock()
return allowTxs, throttleTxs
}
// SetAllowed resets the allowed list of throttler. The previous list will be abandoned.
func (t *throttler) SetAllowed(list []common.Address) {
t.mu.Lock()
defer t.mu.Unlock()
t.allowed = make(map[common.Address]bool)
for _, addr := range list {
t.allowed[addr] = true
}
}
func (t *throttler) GetAllowed() []common.Address {
t.mu.RLock()
defer t.mu.RUnlock()
allowList := make([]common.Address, 0)
for addr := range t.allowed {
allowList = append(allowList, addr)
}
return allowList
}
func (t *throttler) GetThrottled() []common.Address {
t.mu.RLock()
defer t.mu.RUnlock()
throttledList := make([]common.Address, 0)
for addr := range t.throttled {
throttledList = append(throttledList, addr)
}
return throttledList
}
func (t *throttler) GetCandidates() map[common.Address]int {
t.mu.RLock()
defer t.mu.RUnlock()
return t.candidates
}
func (t *throttler) GetConfig() *ThrottlerConfig {
return t.config
}