Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion testscommon/evictionNotifierStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package testscommon

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

// NotifyEviction -
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions txcache/baseTxCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
3 changes: 3 additions & 0 deletions txcache/crossTxCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions txcache/txCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}