From ac3c529bd596fdfa624be82036713f5e19e9a30c Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Mon, 18 Sep 2023 11:21:22 +0300 Subject: [PATCH] further fixes after review --- testscommon/evictionNotifierStub.go | 18 ++++++++++++++++++ txcache/baseTxCache.go | 13 ++++++++----- txcache/crossTxCache.go | 2 +- txcache/crossTxCache_test.go | 9 ++++++--- txcache/txCache.go | 2 +- txcache/txCache_test.go | 19 ++++++++++++------- types/interface.go | 8 +++++++- 7 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 testscommon/evictionNotifierStub.go diff --git a/testscommon/evictionNotifierStub.go b/testscommon/evictionNotifierStub.go new file mode 100644 index 00000000..1a9c47fa --- /dev/null +++ b/testscommon/evictionNotifierStub.go @@ -0,0 +1,18 @@ +package testscommon + +// EvictionNotifierStub - +type EvictionNotifierStub struct { + NotifyEvictionCalled func(txHash []byte) +} + +// NotifyEviction - +func (stub *EvictionNotifierStub) NotifyEviction(txHash []byte) { + if stub.NotifyEvictionCalled != nil { + stub.NotifyEvictionCalled(txHash) + } +} + +// IsInterfaceNil - +func (stub *EvictionNotifierStub) IsInterfaceNil() bool { + return stub == nil +} diff --git a/txcache/baseTxCache.go b/txcache/baseTxCache.go index 67321686..e835770c 100644 --- a/txcache/baseTxCache.go +++ b/txcache/baseTxCache.go @@ -3,7 +3,9 @@ package txcache import ( "sync" + "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-storage-go/common" + "github.com/multiversx/mx-chain-storage-go/types" ) const maxNumOfEvictionWorkers = 5 @@ -15,13 +17,13 @@ type evictionWorkerPool interface { type baseTxCache struct { mutEvictionHandlers sync.RWMutex - evictionHandlers []func(txHash []byte) + evictionHandlers []types.EvictionNotifier evictionWorkerPool evictionWorkerPool } // RegisterEvictionHandler registers a handler which will be called when a tx is evicted from cache -func (cache *baseTxCache) RegisterEvictionHandler(handler func(hash []byte)) error { - if handler == nil { +func (cache *baseTxCache) RegisterEvictionHandler(handler types.EvictionNotifier) error { + if check.IfNil(handler) { return common.ErrNilEvictionHandler } @@ -35,12 +37,13 @@ func (cache *baseTxCache) RegisterEvictionHandler(handler func(hash []byte)) err // notifyEvictionHandlers will be called on a separate go routine func (cache *baseTxCache) notifyEvictionHandlers(txHashes [][]byte) { cache.mutEvictionHandlers.RLock() - handlers := cache.evictionHandlers + handlers := make([]types.EvictionNotifier, len(cache.evictionHandlers)) + copy(handlers, cache.evictionHandlers) cache.mutEvictionHandlers.RUnlock() for _, handler := range handlers { for _, txHash := range txHashes { - handler(txHash) + handler.NotifyEviction(txHash) } } } diff --git a/txcache/crossTxCache.go b/txcache/crossTxCache.go index d0d19233..22ac51e7 100644 --- a/txcache/crossTxCache.go +++ b/txcache/crossTxCache.go @@ -40,7 +40,7 @@ func NewCrossTxCache(config ConfigDestinationMe) (*CrossTxCache, error) { cache := CrossTxCache{ ImmunityCache: immunityCache, baseTxCache: &baseTxCache{ - evictionHandlers: make([]func(txHash []byte), 0), + evictionHandlers: make([]types.EvictionNotifier, 0), evictionWorkerPool: workerpool.New(maxNumOfEvictionWorkers), }, config: config, diff --git a/txcache/crossTxCache_test.go b/txcache/crossTxCache_test.go index 4f6a2133..61aa9423 100644 --- a/txcache/crossTxCache_test.go +++ b/txcache/crossTxCache_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/multiversx/mx-chain-storage-go/common" + "github.com/multiversx/mx-chain-storage-go/testscommon" "github.com/stretchr/testify/require" ) @@ -74,9 +75,11 @@ func TestCrossTxCache_RegisterEvictionHandler(t *testing.T) { require.Equal(t, common.ErrNilEvictionHandler, err) ch := make(chan struct{}) - err = cache.RegisterEvictionHandler(func(hash []byte) { - require.True(t, bytes.Equal([]byte("hash-1"), hash)) - ch <- struct{}{} + err = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{ + NotifyEvictionCalled: func(hash []byte) { + require.True(t, bytes.Equal([]byte("hash-1"), hash)) + ch <- struct{}{} + }, }) require.NoError(t, err) diff --git a/txcache/txCache.go b/txcache/txCache.go index f139ae91..18d1ccfc 100644 --- a/txcache/txCache.go +++ b/txcache/txCache.go @@ -53,7 +53,7 @@ func NewTxCache(config ConfigSourceMe, txGasHandler TxGasHandler) (*TxCache, err scoreComputerObj := newDefaultScoreComputer(txFeeHelper) txCache := &TxCache{ baseTxCache: &baseTxCache{ - evictionHandlers: make([]func(txHash []byte), 0), + evictionHandlers: make([]types.EvictionNotifier, 0), evictionWorkerPool: workerpool.New(maxNumOfEvictionWorkers), }, name: config.Name, diff --git a/txcache/txCache_test.go b/txcache/txCache_test.go index 5a043d4b..4af75c83 100644 --- a/txcache/txCache_test.go +++ b/txcache/txCache_test.go @@ -14,6 +14,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-storage-go/common" + "github.com/multiversx/mx-chain-storage-go/testscommon" "github.com/multiversx/mx-chain-storage-go/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -553,9 +554,11 @@ func TestTxCache_NoCriticalInconsistency_WhenConcurrentAdditionsAndRemovals(t *t handlerCalls := uint32(0) evictionHandlerWG := sync.WaitGroup{} - _ = cache.RegisterEvictionHandler(func(hash []byte) { - atomic.AddUint32(&handlerCalls, 1) - evictionHandlerWG.Done() + _ = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{ + NotifyEvictionCalled: func(hash []byte) { + atomic.AddUint32(&handlerCalls, 1) + evictionHandlerWG.Done() + }, }) // A lot of routines concur to add & remove THE FIRST transaction of a sender @@ -659,10 +662,12 @@ func TestTxCache_RegisterEvictionHandler(t *testing.T) { ch := make(chan uint32) cnt := uint32(0) - err = cache.RegisterEvictionHandler(func(hash []byte) { - atomic.AddUint32(&cnt, 1) - require.True(t, bytes.Equal([]byte("hash-1"), hash) || bytes.Equal([]byte("hash-2"), hash)) - ch <- atomic.LoadUint32(&cnt) + err = cache.RegisterEvictionHandler(&testscommon.EvictionNotifierStub{ + NotifyEvictionCalled: func(hash []byte) { + atomic.AddUint32(&cnt, 1) + require.True(t, bytes.Equal([]byte("hash-1"), hash) || bytes.Equal([]byte("hash-2"), hash)) + ch <- atomic.LoadUint32(&cnt) + }, }) require.NoError(t, err) diff --git a/types/interface.go b/types/interface.go index 99dca5f4..4a483f6a 100644 --- a/types/interface.go +++ b/types/interface.go @@ -225,8 +225,14 @@ type ShardIDProvider interface { IsInterfaceNil() bool } -// PersisterCreator defines the behavour of a component which is able to create a persister +// PersisterCreator defines the behaviour of a component which is able to create a persister type PersisterCreator interface { CreateBasePersister(path string) (Persister, error) IsInterfaceNil() bool } + +// EvictionNotifier defines the behaviour of a component which is able to handle an evicted transaction +type EvictionNotifier interface { + NotifyEviction(txHash []byte) + IsInterfaceNil() bool +}