diff --git a/testscommon/evictionNotifierStub.go b/testscommon/evictionNotifierStub.go index 1a9c47fa..edaba8df 100644 --- a/testscommon/evictionNotifierStub.go +++ b/testscommon/evictionNotifierStub.go @@ -2,7 +2,8 @@ package testscommon // EvictionNotifierStub - type EvictionNotifierStub struct { - NotifyEvictionCalled func(txHash []byte) + NotifyEvictionCalled func(txHash []byte) + ShouldNotifyEvictionCalled func(txHash []byte) bool } // NotifyEviction - @@ -12,6 +13,14 @@ func (stub *EvictionNotifierStub) NotifyEviction(txHash []byte) { } } +// ShouldNotifyEviction - +func (stub *EvictionNotifierStub) ShouldNotifyEviction(txHash []byte) bool { + if stub.ShouldNotifyEvictionCalled != nil { + return stub.ShouldNotifyEvictionCalled(txHash) + } + return false +} + // IsInterfaceNil - func (stub *EvictionNotifierStub) IsInterfaceNil() bool { return stub == nil diff --git a/txcache/baseTxCache.go b/txcache/baseTxCache.go index bdcbf0e9..51199657 100644 --- a/txcache/baseTxCache.go +++ b/txcache/baseTxCache.go @@ -37,12 +37,20 @@ func (cache *baseTxCache) RegisterEvictionHandler(handler types.EvictionNotifier // enqueueEvictedHashesForNotification will enqueue the provided hashes on the workers pool func (cache *baseTxCache) enqueueEvictedHashesForNotification(txHashes [][]byte) { cache.mutEvictionHandlers.RLock() + if len(cache.evictionHandlers) == 0 { + cache.mutEvictionHandlers.RUnlock() + return + } handlers := make([]types.EvictionNotifier, len(cache.evictionHandlers)) copy(handlers, cache.evictionHandlers) cache.mutEvictionHandlers.RUnlock() for _, handler := range handlers { for _, txHash := range txHashes { + if !handler.ShouldNotifyEviction(txHash) { + continue + } + cache.evictionWorkerPool.Submit(func() { handler.NotifyEviction(txHash) }) diff --git a/txcache/crossTxCache_test.go b/txcache/crossTxCache_test.go index 61aa9423..a7556dc2 100644 --- a/txcache/crossTxCache_test.go +++ b/txcache/crossTxCache_test.go @@ -80,6 +80,9 @@ func TestCrossTxCache_RegisterEvictionHandler(t *testing.T) { require.True(t, bytes.Equal([]byte("hash-1"), hash)) ch <- struct{}{} }, + ShouldNotifyEvictionCalled: func(txHash []byte) bool { + return true + }, }) require.NoError(t, err) diff --git a/txcache/txCache_test.go b/txcache/txCache_test.go index 4af75c83..e22a5025 100644 --- a/txcache/txCache_test.go +++ b/txcache/txCache_test.go @@ -559,6 +559,9 @@ func TestTxCache_NoCriticalInconsistency_WhenConcurrentAdditionsAndRemovals(t *t atomic.AddUint32(&handlerCalls, 1) evictionHandlerWG.Done() }, + ShouldNotifyEvictionCalled: func(txHash []byte) bool { + return true + }, }) // A lot of routines concur to add & remove THE FIRST transaction of a sender @@ -668,6 +671,9 @@ func TestTxCache_RegisterEvictionHandler(t *testing.T) { require.True(t, bytes.Equal([]byte("hash-1"), hash) || bytes.Equal([]byte("hash-2"), hash)) ch <- atomic.LoadUint32(&cnt) }, + ShouldNotifyEvictionCalled: func(txHash []byte) bool { + return true + }, }) require.NoError(t, err) diff --git a/types/interface.go b/types/interface.go index 4a483f6a..ad841209 100644 --- a/types/interface.go +++ b/types/interface.go @@ -234,5 +234,6 @@ type PersisterCreator interface { // EvictionNotifier defines the behaviour of a component which is able to handle an evicted transaction type EvictionNotifier interface { NotifyEviction(txHash []byte) + ShouldNotifyEviction(txHash []byte) bool IsInterfaceNil() bool }