Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message validation optimization #6462

Merged
merged 47 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
10afdba
initial commit.
cristure Sep 12, 2024
aaa9d4b
added test for processing message.
cristure Sep 13, 2024
603bd69
added message validation cache.
cristure Sep 16, 2024
57f009e
fix some tests.
cristure Sep 16, 2024
f5a6f2e
fixed missing topic.
cristure Sep 16, 2024
8eec330
fix some more tests.
cristure Sep 17, 2024
a733097
fix nil pointer dereferences in tests.
cristure Sep 17, 2024
bab3b05
more tests fixed.
cristure Sep 17, 2024
c3f7120
add map for cacher in more unit tests.
cristure Sep 17, 2024
b62e37b
fix nil map for more tests.
cristure Sep 17, 2024
c00f8ee
fix process tests.
cristure Sep 17, 2024
94a64c1
Merge branch 'feat/equivalent-messages' into message_validation_optim…
cristure Sep 17, 2024
d77e367
Merge remote-tracking branch 'origin/feat/equivalent-messages' into m…
cristure Sep 18, 2024
d59c5bd
fix conflicts with target branch.
cristure Sep 18, 2024
396cd8e
refactored message validation to a different component.
cristure Sep 19, 2024
a44129f
cosmetic changes.
cristure Sep 20, 2024
afdceb1
added intercepted data verifier stub in multi data tests.
cristure Sep 20, 2024
d8d8113
fix single data interceptor tests.
cristure Sep 20, 2024
fe2082c
commit debug strings for CI.
cristure Sep 20, 2024
c6b7a0f
moved map of cachers in node processor.
cristure Sep 20, 2024
f59fc85
cosmetic changes.
cristure Sep 20, 2024
d00c7ab
fix integration tests.
cristure Sep 20, 2024
f21e24f
fix some more tests.
cristure Sep 20, 2024
f40d222
fix some unit tests.
cristure Sep 23, 2024
3a327c4
fix nil map in tests.
cristure Sep 23, 2024
a756a1f
Merge branch 'feat/equivalent-messages' into message_validation_optim…
cristure Sep 23, 2024
ff8eaa9
Merge remote-tracking branch 'origin/feat/equivalent-messages' into m…
cristure Sep 25, 2024
cde6c80
cosmetic changes.
cristure Sep 25, 2024
e0cf24b
Merge remote-tracking branch 'origin/message_validation_optimization'…
cristure Sep 25, 2024
9504263
refactored map of caches for intercepted data into a new component.
cristure Sep 25, 2024
b622085
added mock for interceptedDataVerifierFactory and injected into tests.
cristure Sep 26, 2024
f0b34ef
more test fixes.
cristure Sep 26, 2024
b05c650
increase wait time for CI run.
cristure Sep 26, 2024
5dc6bba
bring back rw mutex on interceptedDataVerifier.
cristure Sep 26, 2024
d214027
fixes after review.
cristure Sep 30, 2024
899b04d
split test and improved lock mechanism.
cristure Oct 1, 2024
959e037
Update node/nodeRunner.go
cristure Oct 2, 2024
7762f62
addressed some comments.
cristure Oct 2, 2024
23dbb5a
more fixes.
cristure Oct 3, 2024
b9eb2a5
fix unit tests.
cristure Oct 3, 2024
0deeb37
Merge branch 'feat/equivalent-messages' into message_validation_optim…
sstanculeanu Oct 3, 2024
8451f62
added close to intercepted data cacher and a few unit tests on nil ch…
cristure Oct 3, 2024
130e115
Merge remote-tracking branch 'origin/message_validation_optimization'…
cristure Oct 3, 2024
754fae3
Merge branch 'feat/equivalent-messages' into message_validation_optim…
sstanculeanu Oct 3, 2024
8dd18b8
cosmetic changes.
cristure Oct 3, 2024
1b11cb8
Merge remote-tracking branch 'origin/message_validation_optimization'…
cristure Oct 3, 2024
111fdb1
Merge branch 'feat/equivalent-messages' into message_validation_optim…
AdoAdoAdo Oct 4, 2024
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move the last const outside to have clearer the "enum" values for the status

)

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 {
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
hash := string(interceptedData.Hash())
sstanculeanu marked this conversation as resolved.
Show resolved Hide resolved

if len(interceptedData.Hash()) == 0 {
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
return interceptedData.CheckValidity()
}

if val, ok := idv.cache.Get(interceptedData.Hash()); ok {
idv.km.RLock(hash)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
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()
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
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
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
},
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
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
},
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())
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved

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)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading