-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossTxCache.go
121 lines (100 loc) · 3.4 KB
/
crossTxCache.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
package txcache
import (
"github.com/ThotaGopichandThota/gn-storage/immunitycache"
"github.com/ThotaGopichandThota/gn-storage/types"
)
var _ types.Cacher = (*CrossTxCache)(nil)
// CrossTxCache holds cross-shard transactions (where destination == me)
type CrossTxCache struct {
*immunitycache.ImmunityCache
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,
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 {
return cache.RemoveWithResult(txHash)
}
// 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)
}
// IsInterfaceNil returns true if there is no value under the interface
func (cache *CrossTxCache) IsInterfaceNil() bool {
return cache == nil
}