Skip to content

Commit

Permalink
further fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Sep 18, 2023
1 parent b844733 commit ac3c529
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
18 changes: 18 additions & 0 deletions testscommon/evictionNotifierStub.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 8 additions & 5 deletions txcache/baseTxCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion txcache/crossTxCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions txcache/crossTxCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions txcache/txCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ac3c529

Please sign in to comment.