-
Notifications
You must be signed in to change notification settings - Fork 199
/
adapterGenericCacheToSortedTransactionsProvider.go
139 lines (110 loc) · 4.03 KB
/
adapterGenericCacheToSortedTransactionsProvider.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
package preprocess
import (
"sort"
"github.com/ElrondNetwork/elrond-go/core/sliceUtil"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/txcache"
)
type adapterGenericCacheToSortedTransactionsProvider struct {
transactionsPreprocessor *transactions
cache storage.Cacher
cacheKey string
}
func newAdapterGenericCacheToSortedTransactionsProvider(transactionsPreprocessor *transactions, cache storage.Cacher, cacheKey string) *adapterGenericCacheToSortedTransactionsProvider {
adapter := &adapterGenericCacheToSortedTransactionsProvider{
transactionsPreprocessor: transactionsPreprocessor,
cache: cache,
cacheKey: cacheKey,
}
return adapter
}
// GetSortedTransactions gets the transactions from the cache
func (adapter *adapterGenericCacheToSortedTransactionsProvider) GetSortedTransactions() []*txcache.WrappedTransaction {
txs, txHashes := adapter.getOrderedTx()
senderShardID, receiverShardID, err := process.ParseShardCacherIdentifier(adapter.cacheKey)
if err != nil {
log.Error(err.Error())
return make([]*txcache.WrappedTransaction, 0)
}
result := make([]*txcache.WrappedTransaction, len(txs))
for i := 0; i < len(result); i++ {
result[i] = &txcache.WrappedTransaction{
Tx: txs[i],
TxHash: txHashes[i],
SenderShardID: senderShardID,
ReceiverShardID: receiverShardID,
}
}
return result
}
// getOrderedTx was moved here from the previous implementation
func (adapter *adapterGenericCacheToSortedTransactionsProvider) getOrderedTx() ([]data.TransactionHandler, [][]byte) {
txs := adapter.transactionsPreprocessor
strCache := adapter.cacheKey
txs.mutOrderedTxs.RLock()
orderedTxs := txs.orderedTxs[strCache]
orderedTxHashes := txs.orderedTxHashes[strCache]
txs.mutOrderedTxs.RUnlock()
alreadyOrdered := len(orderedTxs) > 0
if !alreadyOrdered {
orderedTxs, orderedTxHashes = sortTxByNonce(adapter.cache)
log.Debug("creating mini blocks has been started",
"have num txs", len(orderedTxs),
"strCache", strCache,
)
txs.mutOrderedTxs.Lock()
txs.orderedTxs[strCache] = orderedTxs
txs.orderedTxHashes[strCache] = orderedTxHashes
txs.mutOrderedTxs.Unlock()
}
return orderedTxs, orderedTxHashes
}
// sortTxByNonce was moved here from the previous implementation
func sortTxByNonce(cache storage.Cacher) ([]data.TransactionHandler, [][]byte) {
txShardPool := cache
keys := txShardPool.Keys()
txsSlice := make([]data.TransactionHandler, 0, len(keys))
txHashes := make([][]byte, 0, len(keys))
mTxHashes := make(map[uint64][][]byte, len(keys))
mTransactions := make(map[uint64][]data.TransactionHandler, len(keys))
nonces := make([]uint64, 0, len(keys))
for _, key := range keys {
val, _ := txShardPool.Peek(key)
if val == nil {
continue
}
tx, ok := val.(data.TransactionHandler)
if !ok {
continue
}
nonce := tx.GetNonce()
if mTxHashes[nonce] == nil {
nonces = append(nonces, nonce)
mTxHashes[nonce] = make([][]byte, 0)
mTransactions[nonce] = make([]data.TransactionHandler, 0)
}
mTxHashes[nonce] = append(mTxHashes[nonce], key)
mTransactions[nonce] = append(mTransactions[nonce], tx)
}
sort.Slice(nonces, func(i, j int) bool {
return nonces[i] < nonces[j]
})
for _, nonce := range nonces {
keysForNonce := mTxHashes[nonce]
for idx, key := range keysForNonce {
txHashes = append(txHashes, key)
txsSlice = append(txsSlice, mTransactions[nonce][idx])
}
}
return transaction.TrimSliceHandler(txsSlice), sliceUtil.TrimSliceSliceByte(txHashes)
}
// NotifyAccountNonce isn't implemented for the generic cache
func (adapter *adapterGenericCacheToSortedTransactionsProvider) NotifyAccountNonce(_ []byte, _ uint64) {
}
// IsInterfaceNil returns true if there is no value under the interface
func (adapter *adapterGenericCacheToSortedTransactionsProvider) IsInterfaceNil() bool {
return adapter == nil
}