-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcrossTxCache.go
More file actions
137 lines (115 loc) · 3.8 KB
/
Copy pathcrossTxCache.go
File metadata and controls
137 lines (115 loc) · 3.8 KB
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
package txcache
import (
"github.com/gammazero/workerpool"
"github.com/multiversx/mx-chain-storage-go/immunitycache"
"github.com/multiversx/mx-chain-storage-go/types"
)
var _ types.Cacher = (*CrossTxCache)(nil)
// CrossTxCache holds cross-shard transactions (where destination == me)
type CrossTxCache struct {
*immunitycache.ImmunityCache
*baseTxCache
config ConfigDestinationMe
}
// NewCrossTxCache creates a new transactions cache
func NewCrossTxCache(config ConfigDestinationMe) (*CrossTxCache, error) {
log.Debug("NewCrossTxCache", "config", config.String())
err := config.verify()
if err != nil {
return nil, err
}
immunityCacheConfig := immunitycache.CacheConfig{
Name: config.Name,
NumChunks: config.NumChunks,
MaxNumBytes: config.MaxNumBytes,
MaxNumItems: config.MaxNumItems,
NumItemsToPreemptivelyEvict: config.NumItemsToPreemptivelyEvict,
}
immunityCache, err := immunitycache.NewImmunityCache(immunityCacheConfig)
if err != nil {
return nil, err
}
cache := CrossTxCache{
ImmunityCache: immunityCache,
baseTxCache: &baseTxCache{
evictionHandlers: make([]types.EvictionNotifier, 0),
evictionWorkerPool: workerpool.New(maxNumOfEvictionWorkers),
},
config: config,
}
return &cache, nil
}
// ImmunizeTxsAgainstEviction marks items as non-evictable
func (cache *CrossTxCache) ImmunizeTxsAgainstEviction(keys [][]byte) {
numNow, numFuture := cache.ImmunityCache.ImmunizeKeys(keys)
log.Trace("CrossTxCache.ImmunizeTxsAgainstEviction()",
"name", cache.config.Name,
"len(keys)", len(keys),
"numNow", numNow,
"numFuture", numFuture,
)
cache.Diagnose(false)
}
// AddTx adds a transaction in the cache
func (cache *CrossTxCache) AddTx(tx *WrappedTransaction) (has, added bool) {
return cache.HasOrAdd(tx.TxHash, tx, int(tx.Size))
}
// GetByTxHash gets the transaction by hash
func (cache *CrossTxCache) GetByTxHash(txHash []byte) (*WrappedTransaction, bool) {
item, ok := cache.ImmunityCache.Get(txHash)
if !ok {
return nil, false
}
tx, ok := item.(*WrappedTransaction)
if !ok {
return nil, false
}
return tx, true
}
// Get returns the unwrapped payload of a TransactionWrapper
// Implemented for compatibility reasons (see txPoolsCleaner.go).
func (cache *CrossTxCache) Get(key []byte) (value interface{}, ok bool) {
wrapped, ok := cache.GetByTxHash(key)
if !ok {
return nil, false
}
return wrapped.Tx, true
}
// Peek returns the unwrapped payload of a TransactionWrapper
// Implemented for compatibility reasons (see transactions.go, common.go).
func (cache *CrossTxCache) Peek(key []byte) (value interface{}, ok bool) {
return cache.Get(key)
}
// RemoveTxByHash removes tx by hash
func (cache *CrossTxCache) RemoveTxByHash(txHash []byte) bool {
ok := cache.RemoveWithResult(txHash)
if ok {
cache.enqueueEvictedHashesForNotification([][]byte{txHash})
}
return ok
}
// ForEachTransaction iterates over the transactions in the cache
func (cache *CrossTxCache) ForEachTransaction(function ForEachTransaction) {
cache.ForEachItem(func(key []byte, item interface{}) {
tx, ok := item.(*WrappedTransaction)
if !ok {
return
}
function(key, tx)
})
}
// GetTransactionsPoolForSender returns an empty slice, only to respect the interface
// CrossTxCache does not support transaction selection (not applicable, since transactions are already half-executed),
// thus does not handle nonces, nonce gaps etc.
func (cache *CrossTxCache) GetTransactionsPoolForSender(_ string) []*WrappedTransaction {
return make([]*WrappedTransaction, 0)
}
// Close closes the eviction worker pool
func (cache *CrossTxCache) Close() error {
cache.evictionWorkerPool.Stop()
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (cache *CrossTxCache) IsInterfaceNil() bool {
return cache == nil
}