-
Notifications
You must be signed in to change notification settings - Fork 199
/
txsPoolsCleaner.go
366 lines (305 loc) · 9.42 KB
/
txsPoolsCleaner.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
package poolsCleaner
import (
"bytes"
"context"
"fmt"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/closing"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/txcache"
)
var _ closing.Closer = (*txsPoolsCleaner)(nil)
// sleepTime defines the time between each iteration made in clean...Pools methods
const sleepTime = time.Minute
const (
blockTx = iota
rewardTx
unsignedTx
)
type txInfo struct {
round int64
senderShardID uint32
receiverShardID uint32
txType int8
txStore storage.Cacher
}
// txsPoolsCleaner represents a pools cleaner that checks and cleans txs which should not be in pool anymore
type txsPoolsCleaner struct {
addressPubkeyConverter core.PubkeyConverter
blockTransactionsPool dataRetriever.ShardedDataCacherNotifier
rewardTransactionsPool dataRetriever.ShardedDataCacherNotifier
unsignedTransactionsPool dataRetriever.ShardedDataCacherNotifier
roundHandler process.RoundHandler
shardCoordinator sharding.Coordinator
mutMapTxsRounds sync.RWMutex
mapTxsRounds map[string]*txInfo
emptyAddress []byte
cancelFunc func()
}
// NewTxsPoolsCleaner will return a new txs pools cleaner
func NewTxsPoolsCleaner(
addressPubkeyConverter core.PubkeyConverter,
dataPool dataRetriever.PoolsHolder,
roundHandler process.RoundHandler,
shardCoordinator sharding.Coordinator,
) (*txsPoolsCleaner, error) {
if check.IfNil(addressPubkeyConverter) {
return nil, process.ErrNilPubkeyConverter
}
if check.IfNil(dataPool) {
return nil, process.ErrNilPoolsHolder
}
if check.IfNil(dataPool.Transactions()) {
return nil, process.ErrNilTransactionPool
}
if check.IfNil(dataPool.RewardTransactions()) {
return nil, process.ErrNilRewardTxDataPool
}
if check.IfNil(dataPool.UnsignedTransactions()) {
return nil, process.ErrNilUnsignedTxDataPool
}
if check.IfNil(roundHandler) {
return nil, process.ErrNilRoundHandler
}
if check.IfNil(shardCoordinator) {
return nil, process.ErrNilShardCoordinator
}
tpc := txsPoolsCleaner{
addressPubkeyConverter: addressPubkeyConverter,
blockTransactionsPool: dataPool.Transactions(),
rewardTransactionsPool: dataPool.RewardTransactions(),
unsignedTransactionsPool: dataPool.UnsignedTransactions(),
roundHandler: roundHandler,
shardCoordinator: shardCoordinator,
}
tpc.mapTxsRounds = make(map[string]*txInfo)
tpc.blockTransactionsPool.RegisterOnAdded(tpc.receivedBlockTx)
tpc.rewardTransactionsPool.RegisterOnAdded(tpc.receivedRewardTx)
tpc.unsignedTransactionsPool.RegisterOnAdded(tpc.receivedUnsignedTx)
tpc.emptyAddress = make([]byte, tpc.addressPubkeyConverter.Len())
return &tpc, nil
}
// StartCleaning actually starts the pools cleaning mechanism
func (tpc *txsPoolsCleaner) StartCleaning() {
var ctx context.Context
ctx, tpc.cancelFunc = context.WithCancel(context.Background())
go tpc.cleanTxsPools(ctx)
}
func (tpc *txsPoolsCleaner) cleanTxsPools(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Debug("txsPoolsCleaner's go routine is stopping...")
return
case <-time.After(sleepTime):
}
startTime := time.Now()
numTxsInMap := tpc.cleanTxsPoolsIfNeeded()
elapsedTime := time.Since(startTime)
log.Debug("txsPoolsCleaner.cleanTxsPools",
"num txs in map", numTxsInMap,
"elapsed time", elapsedTime)
}
}
func (tpc *txsPoolsCleaner) receivedBlockTx(key []byte, value interface{}) {
if key == nil {
return
}
log.Trace("txsPoolsCleaner.receivedBlockTx", "hash", key)
wrappedTx, ok := value.(*txcache.WrappedTransaction)
if !ok {
log.Warn("txsPoolsCleaner.receivedBlockTx",
"error", process.ErrWrongTypeAssertion,
"found type", fmt.Sprintf("%T", value),
)
return
}
tpc.processReceivedTx(key, wrappedTx.SenderShardID, wrappedTx.ReceiverShardID, blockTx)
}
func (tpc *txsPoolsCleaner) receivedRewardTx(key []byte, value interface{}) {
if key == nil {
return
}
log.Trace("txsPoolsCleaner.receivedRewardTx", "hash", key)
tx, ok := value.(data.TransactionHandler)
if !ok {
log.Warn("txsPoolsCleaner.receivedRewardTx",
"error", process.ErrWrongTypeAssertion,
"found type", fmt.Sprintf("%T", value),
)
return
}
senderShardID := core.MetachainShardId
receiverShardID, err := tpc.getShardFromAddress(tx.GetRcvAddr())
if err != nil {
log.Debug("txsPoolsCleaner.receivedRewardTx", "error", err.Error())
return
}
tpc.processReceivedTx(key, senderShardID, receiverShardID, rewardTx)
}
func (tpc *txsPoolsCleaner) receivedUnsignedTx(key []byte, value interface{}) {
if key == nil {
return
}
log.Trace("txsPoolsCleaner.receivedUnsignedTx", "hash", key)
tx, ok := value.(data.TransactionHandler)
if !ok {
log.Warn("txsPoolsCleaner.receivedUnsignedTx",
"error", process.ErrWrongTypeAssertion,
"found type", fmt.Sprintf("%T", value),
)
return
}
senderShardID, receiverShardID, err := tpc.computeSenderAndReceiverShards(tx)
if err != nil {
log.Debug("txsPoolsCleaner.receivedUnsignedTx", "error", err.Error())
return
}
tpc.processReceivedTx(key, senderShardID, receiverShardID, unsignedTx)
}
func (tpc *txsPoolsCleaner) processReceivedTx(
key []byte,
senderShardID uint32,
receiverShardID uint32,
txType int8,
) {
tpc.mutMapTxsRounds.RLock()
_, ok := tpc.mapTxsRounds[string(key)]
tpc.mutMapTxsRounds.RUnlock()
if !ok {
transactionPool := tpc.getTransactionPool(txType)
if check.IfNil(transactionPool) {
return
}
strCache := process.ShardCacherIdentifier(senderShardID, receiverShardID)
txStore := transactionPool.ShardDataStore(strCache)
if check.IfNil(txStore) {
return
}
currTxInfo := &txInfo{
round: tpc.roundHandler.Index(),
senderShardID: senderShardID,
receiverShardID: receiverShardID,
txType: txType,
txStore: txStore,
}
tpc.mutMapTxsRounds.Lock()
tpc.mapTxsRounds[string(key)] = currTxInfo
tpc.mutMapTxsRounds.Unlock()
log.Trace("transaction has been added",
"hash", key,
"round", currTxInfo.round,
"sender", currTxInfo.senderShardID,
"receiver", currTxInfo.receiverShardID,
"type", getTxTypeName(currTxInfo.txType))
}
}
func (tpc *txsPoolsCleaner) cleanTxsPoolsIfNeeded() int {
numTxsCleaned := 0
hashesToRemove := make(map[string]storage.Cacher)
tpc.mutMapTxsRounds.Lock()
for hash, currTxInfo := range tpc.mapTxsRounds {
_, ok := currTxInfo.txStore.Get([]byte(hash))
if !ok {
log.Trace("transaction not found in pool",
"hash", []byte(hash),
"round", currTxInfo.round,
"sender", currTxInfo.senderShardID,
"receiver", currTxInfo.receiverShardID,
"type", getTxTypeName(currTxInfo.txType))
delete(tpc.mapTxsRounds, hash)
continue
}
roundDif := tpc.roundHandler.Index() - currTxInfo.round
if roundDif <= process.MaxRoundsToKeepUnprocessedTransactions {
log.Trace("cleaning transaction not yet allowed",
"hash", []byte(hash),
"round", currTxInfo.round,
"sender", currTxInfo.senderShardID,
"receiver", currTxInfo.receiverShardID,
"type", getTxTypeName(currTxInfo.txType),
"round dif", roundDif)
continue
}
hashesToRemove[hash] = currTxInfo.txStore
delete(tpc.mapTxsRounds, hash)
numTxsCleaned++
log.Trace("transaction has been cleaned",
"hash", []byte(hash),
"round", currTxInfo.round,
"sender", currTxInfo.senderShardID,
"receiver", currTxInfo.receiverShardID,
"type", getTxTypeName(currTxInfo.txType))
}
numTxsRounds := len(tpc.mapTxsRounds)
tpc.mutMapTxsRounds.Unlock()
startTime := time.Now()
for hash, txStore := range hashesToRemove {
txStore.Remove([]byte(hash))
}
elapsedTime := time.Since(startTime)
if numTxsCleaned > 0 {
log.Debug("txsPoolsCleaner.cleanTxsPoolsIfNeeded",
"num txs cleaned", numTxsCleaned,
"elapsed time to remove txs from cacher", elapsedTime)
}
return numTxsRounds
}
func (tpc *txsPoolsCleaner) getTransactionPool(txType int8) dataRetriever.ShardedDataCacherNotifier {
switch txType {
case blockTx:
return tpc.blockTransactionsPool
case rewardTx:
return tpc.rewardTransactionsPool
case unsignedTx:
return tpc.unsignedTransactionsPool
}
return nil
}
func getTxTypeName(txType int8) string {
switch txType {
case blockTx:
return "blockTx"
case rewardTx:
return "rewardTx"
case unsignedTx:
return "unsignedTx"
}
return "unknownTx"
}
func (tpc *txsPoolsCleaner) computeSenderAndReceiverShards(tx data.TransactionHandler) (uint32, uint32, error) {
senderShardID, err := tpc.getShardFromAddress(tx.GetSndAddr())
if err != nil {
return 0, 0, err
}
receiverShardID, err := tpc.getShardFromAddress(tx.GetRcvAddr())
if err != nil {
return 0, 0, err
}
return senderShardID, receiverShardID, nil
}
func (tpc *txsPoolsCleaner) getShardFromAddress(address []byte) (uint32, error) {
isEmptyAddress := bytes.Equal(address, tpc.emptyAddress)
if isEmptyAddress {
return tpc.shardCoordinator.SelfId(), nil
}
return tpc.shardCoordinator.ComputeId(address), nil
}
// Close will close the endless running go routine
func (tpc *txsPoolsCleaner) Close() error {
if tpc.cancelFunc != nil {
tpc.cancelFunc()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (tpc *txsPoolsCleaner) IsInterfaceNil() bool {
return tpc == nil
}