Skip to content

Commit

Permalink
fixes after review.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristure committed Sep 30, 2024
1 parent 5dc6bba commit d214027
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/multiversx/mx-chain-go/integrationTests/factory"
"github.com/multiversx/mx-chain-go/node"
//"github.com/multiversx/mx-chain-go/process"
//factory2 "github.com/multiversx/mx-chain-go/process/interceptors/factory"
"github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/testscommon/goroutines"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,17 +958,3 @@ func (bicf *baseInterceptorsContainerFactory) addInterceptorsToContainers(keys [

return bicf.fullArchiveContainer.AddMultiple(keys, interceptors)
}

//func (bicf *baseInterceptorsContainerFactory) createCacheForInterceptor(topic string) (process.InterceptedDataVerifier, error) {
// internalCache, err := cache.NewTimeCacher(cache.ArgTimeCacher{
// DefaultSpan: cacheDefaultSpan,
// CacheExpiry: cacheDefaultExpiry,
// })
// if err != nil {
// return nil, err
// }
//
// bicf.processedMessagesCacheMap[topic] = internalCache
// verifier := interceptors.NewInterceptedDataVerifier(internalCache)
// return verifier, nil
//}
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ func (idvf *InterceptedDataVerifierFactory) Create(topic string) (process.Interc
verifier := interceptors.NewInterceptedDataVerifier(internalCache)
return verifier, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (idvf *InterceptedDataVerifierFactory) IsInterfaceNil() bool {
return idvf == nil
}
30 changes: 16 additions & 14 deletions process/interceptors/interceptedDataVerifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/multiversx/mx-chain-go/storage"
)

type interceptedDataStatus int
type interceptedDataStatus int8

const (
ValidInterceptedData interceptedDataStatus = iota
InvalidInterceptedData

interceptedDataStatusBytesSize = 8
)

var (
Expand All @@ -39,38 +41,38 @@ func NewInterceptedDataVerifier(cache storage.Cacher) *interceptedDataVerifier {
// It will retrieve the status in the cache if it exists, otherwise it will validate it and store the status of the
// validation in the cache. Note that the entries are stored for a set period of time
func (idv *interceptedDataVerifier) Verify(interceptedData process.InterceptedData) error {
hash := string(interceptedData.Hash())

if len(interceptedData.Hash()) == 0 {
return interceptedData.CheckValidity()
}

if val, ok := idv.cache.Get(interceptedData.Hash()); ok {
idv.km.RLock(hash)
val, ok := idv.cache.Get(interceptedData.Hash())
idv.km.RUnlock(hash)

if ok {
if val == ValidInterceptedData {
return nil
}

return ErrInvalidInterceptedData
}

err := idv.checkValidity(interceptedData)
err := interceptedData.CheckValidity()
if err != nil {
idv.cache.Put(interceptedData.Hash(), InvalidInterceptedData, 8)
idv.km.Lock(hash)
idv.cache.Put(interceptedData.Hash(), InvalidInterceptedData, interceptedDataStatusBytesSize)
idv.km.Unlock(hash)

return ErrInvalidInterceptedData
}

idv.cache.Put(interceptedData.Hash(), ValidInterceptedData, 100)
idv.cache.Put(interceptedData.Hash(), ValidInterceptedData, interceptedDataStatusBytesSize)
return nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (idv *interceptedDataVerifier) IsInterfaceNil() bool {
return idv == nil
}

func (idv *interceptedDataVerifier) checkValidity(interceptedData process.InterceptedData) error {
hash := string(interceptedData.Hash())

idv.km.Lock(hash)
defer idv.km.Unlock(hash)

return interceptedData.CheckValidity()
}
12 changes: 11 additions & 1 deletion process/interceptors/interceptedDataVerifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ func defaultInterceptedDataVerifier(span time.Duration) process.InterceptedDataV
func TestInterceptedDataVerifier_CheckValidityShouldWork(t *testing.T) {
t.Parallel()

checkValidityCounter := atomic.Counter{}

interceptedData := &testscommon.InterceptedDataStub{
CheckValidityCalled: func() error {
checkValidityCounter.Add(1)
return nil
},
IsForCurrentShardCalled: func() bool {
Expand Down Expand Up @@ -60,13 +63,17 @@ func TestInterceptedDataVerifier_CheckValidityShouldWork(t *testing.T) {
wg.Wait()

require.Equal(t, int64(0), errCount.Get())
require.Equal(t, int64(1), checkValidityCounter.Get())
}

func TestInterceptedDataVerifier_CheckValidityShouldNotWork(t *testing.T) {
t.Parallel()

checkValidityCounter := atomic.Counter{}

interceptedData := &testscommon.InterceptedDataStub{
CheckValidityCalled: func() error {
checkValidityCounter.Add(1)
return nil
},
IsForCurrentShardCalled: func() bool {
Expand All @@ -93,13 +100,16 @@ func TestInterceptedDataVerifier_CheckValidityShouldNotWork(t *testing.T) {

err := verifier.Verify(interceptedDataWithErr)
require.Equal(t, ErrInvalidInterceptedData, err)
require.Equal(t, int64(0), checkValidityCounter.Get())

err = verifier.Verify(interceptedData)
// It is still invalid because it has the same hash.
require.Equal(t, ErrInvalidInterceptedData, err)
require.Equal(t, int64(0), checkValidityCounter.Get())

<-time.After(defaultSpan + 100*time.Millisecond)

err = verifier.Verify(interceptedData)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, int64(1), checkValidityCounter.Get())
}
1 change: 1 addition & 0 deletions process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,4 +1409,5 @@ type InterceptedDataVerifier interface {

type InterceptedDataVerifierFactory interface {
Create(topic string) (InterceptedDataVerifier, error)
IsInterfaceNil() bool
}
5 changes: 5 additions & 0 deletions process/mock/interceptedDataVerifierFactoryStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ func (idvfs *InterceptedDataVerifierFactoryStub) Create(topic string) (process.I

return nil, nil
}

// IsInterfaceNil -
func (idvfs *InterceptedDataVerifierFactoryStub) IsInterfaceNil() bool {
return idvfs == nil
}

0 comments on commit d214027

Please sign in to comment.