Skip to content

Commit

Permalink
added cache id on NotifyEviction
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Oct 6, 2023
1 parent c78a414 commit 0c48618
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions testscommon/evictionNotifierStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package testscommon

// EvictionNotifierStub -
type EvictionNotifierStub struct {
NotifyEvictionCalled func(txHash []byte)
NotifyEvictionCalled func(txHash []byte, cacheId string)
ShouldNotifyEvictionCalled func(txHash []byte) bool
}

// NotifyEviction -
func (stub *EvictionNotifierStub) NotifyEviction(txHash []byte) {
func (stub *EvictionNotifierStub) NotifyEviction(txHash []byte, cacheId string) {
if stub.NotifyEvictionCalled != nil {
stub.NotifyEvictionCalled(txHash)
stub.NotifyEvictionCalled(txHash, cacheId)
}
}

Expand Down
3 changes: 2 additions & 1 deletion txcache/baseTxCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type baseTxCache struct {
mutEvictionHandlers sync.RWMutex
evictionHandlers []types.EvictionNotifier
evictionWorkerPool evictionWorkerPool
name string
}

// RegisterEvictionHandler registers a handler which will be called when a tx is evicted from cache
Expand Down Expand Up @@ -48,7 +49,7 @@ func (cache *baseTxCache) enqueueEvictedHashesForNotification(txHashes [][]byte)
}

cache.evictionWorkerPool.Submit(func() {
handler.NotifyEviction(txHash)
handler.NotifyEviction(txHash, cache.name)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions txcache/crossTxCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewCrossTxCache(config ConfigDestinationMe) (*CrossTxCache, error) {
baseTxCache: &baseTxCache{
evictionHandlers: make([]types.EvictionNotifier, 0),
evictionWorkerPool: workerpool.New(maxNumOfEvictionWorkers),
name: config.Name,
},
config: config,
}
Expand Down
2 changes: 1 addition & 1 deletion txcache/crossTxCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCrossTxCache_RegisterEvictionHandler(t *testing.T) {

ch := make(chan struct{})
err = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{
NotifyEvictionCalled: func(hash []byte) {
NotifyEvictionCalled: func(hash []byte, cacheId string) {
require.True(t, bytes.Equal([]byte("hash-1"), hash))
ch <- struct{}{}
},
Expand Down
3 changes: 1 addition & 2 deletions txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var _ types.Cacher = (*TxCache)(nil)
// TxCache represents a cache-like structure (it has a fixed capacity and implements an eviction mechanism) for holding transactions
type TxCache struct {
*baseTxCache
name string
txListBySender *txListBySenderMap
txByHash *txByHashMap
config ConfigSourceMe
Expand Down Expand Up @@ -55,8 +54,8 @@ func NewTxCache(config ConfigSourceMe, txGasHandler TxGasHandler) (*TxCache, err
baseTxCache: &baseTxCache{
evictionHandlers: make([]types.EvictionNotifier, 0),
evictionWorkerPool: workerpool.New(maxNumOfEvictionWorkers),
name: config.Name,
},
name: config.Name,
txListBySender: newTxListBySenderMap(numChunks, senderConstraintsObj, scoreComputerObj, txGasHandler, txFeeHelper),
txByHash: newTxByHashMap(numChunks),
config: config,
Expand Down
4 changes: 2 additions & 2 deletions txcache/txCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func TestTxCache_NoCriticalInconsistency_WhenConcurrentAdditionsAndRemovals(t *t
handlerCalls := uint32(0)
evictionHandlerWG := sync.WaitGroup{}
_ = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{
NotifyEvictionCalled: func(hash []byte) {
NotifyEvictionCalled: func(hash []byte, cacheId string) {
atomic.AddUint32(&handlerCalls, 1)
evictionHandlerWG.Done()
},
Expand Down Expand Up @@ -666,7 +666,7 @@ func TestTxCache_RegisterEvictionHandler(t *testing.T) {
ch := make(chan uint32)
cnt := uint32(0)
err = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{
NotifyEvictionCalled: func(hash []byte) {
NotifyEvictionCalled: func(hash []byte, cacheId string) {
atomic.AddUint32(&cnt, 1)
require.True(t, bytes.Equal([]byte("hash-1"), hash) || bytes.Equal([]byte("hash-2"), hash))
ch <- atomic.LoadUint32(&cnt)
Expand Down
2 changes: 1 addition & 1 deletion types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ type PersisterCreator interface {

// EvictionNotifier defines the behaviour of a component which is able to handle an evicted transaction
type EvictionNotifier interface {
NotifyEviction(txHash []byte)
NotifyEviction(txHash []byte, cacheId string)
ShouldNotifyEviction(txHash []byte) bool
IsInterfaceNil() bool
}

0 comments on commit 0c48618

Please sign in to comment.