-
Notifications
You must be signed in to change notification settings - Fork 215
chain parameters notifier #4927
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
Merged
bogdan-rosianu
merged 16 commits into
feat/consensus-size-changes
from
chain-parameters-notifier
Apr 11, 2023
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8238e5b
chain parameters notifier
bogdan-rosianu be4806f
fix linter + stubs
bogdan-rosianu e071567
fix stub
bogdan-rosianu 6b6e478
added test for the notification of the new params
bogdan-rosianu 053fe06
remove unused interface
bogdan-rosianu 617da1e
fix subscriber instance
bogdan-rosianu abc1890
fixes after review
bogdan-rosianu 1b49b5a
Merge branch 'feat/consensus-size-changes' into chain-parameters-noti…
bogdan-rosianu a885724
test renaming
bogdan-rosianu b210638
fix compilation issue
bogdan-rosianu 56bcfb7
add changes from the consensus-group-size-further-updates branch
bogdan-rosianu 19b2db2
remove option
bogdan-rosianu fbe6f81
Merge branch 'feat/consensus-size-changes' into chain-parameters-noti…
bogdan-rosianu 55b9939
fix init in consensus components
bogdan-rosianu 5601282
extended unit test
bogdan-rosianu 961bb62
fixes after review
bogdan-rosianu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package chainparametersnotifier | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/multiversx/mx-chain-core-go/core/check" | ||
| "github.com/multiversx/mx-chain-go/common" | ||
| "github.com/multiversx/mx-chain-go/config" | ||
| logger "github.com/multiversx/mx-chain-logger-go" | ||
| ) | ||
|
|
||
| var log = logger.GetOrCreate("common/chainparameters") | ||
|
|
||
| type chainParametersNotifier struct { | ||
| mutData sync.RWMutex | ||
| wasInitialized bool | ||
| currentChainParameters config.ChainParametersByEpochConfig | ||
| mutHandler sync.RWMutex | ||
| handlers []common.ChainParametersSubscriptionHandler | ||
| } | ||
|
|
||
| // NewChainParametersNotifier creates a new instance of a chainParametersNotifier component | ||
| func NewChainParametersNotifier() *chainParametersNotifier { | ||
| return &chainParametersNotifier{ | ||
| wasInitialized: false, | ||
| handlers: make([]common.ChainParametersSubscriptionHandler, 0), | ||
| } | ||
| } | ||
|
|
||
| // UpdateCurrentChainParameters should be called whenever new chain parameters become active on the network | ||
| func (cpn *chainParametersNotifier) UpdateCurrentChainParameters(params config.ChainParametersByEpochConfig) { | ||
| cpn.mutData.Lock() | ||
| shouldSkipParams := cpn.wasInitialized && cpn.currentChainParameters.EnableEpoch == params.EnableEpoch | ||
| if shouldSkipParams { | ||
| cpn.mutData.Unlock() | ||
|
|
||
| return | ||
| } | ||
| cpn.wasInitialized = true | ||
| cpn.currentChainParameters = params | ||
| cpn.mutData.Unlock() | ||
|
|
||
| cpn.mutHandler.RLock() | ||
| handlersCopy := make([]common.ChainParametersSubscriptionHandler, len(cpn.handlers)) | ||
| copy(handlersCopy, cpn.handlers) | ||
| cpn.mutHandler.RUnlock() | ||
|
|
||
| log.Debug("chainParametersNotifier.UpdateCurrentChainParameters", | ||
| "enable epoch", params.EnableEpoch, | ||
| "shard consensus group size", params.ShardConsensusGroupSize, | ||
| "shard min number of nodes", params.ShardMinNumNodes, | ||
| "meta consensus group size", params.MetachainConsensusGroupSize, | ||
| "meta min number of nodes", params.MetachainMinNumNodes, | ||
| "round duration", params.RoundDuration, | ||
| "hysteresis", params.Hysteresis, | ||
| "adaptivity", params.Adaptivity, | ||
| ) | ||
|
|
||
| for _, handler := range handlersCopy { | ||
| handler.ChainParametersChanged(params) | ||
| } | ||
| } | ||
|
|
||
| // RegisterNotifyHandler will register the provided handler to be called whenever chain parameters have changed | ||
| func (cpn *chainParametersNotifier) RegisterNotifyHandler(handler common.ChainParametersSubscriptionHandler) { | ||
| if check.IfNil(handler) { | ||
| return | ||
| } | ||
|
|
||
| cpn.mutHandler.Lock() | ||
| cpn.handlers = append(cpn.handlers, handler) | ||
| cpn.mutHandler.Unlock() | ||
|
|
||
| cpn.mutData.RLock() | ||
| handler.ChainParametersChanged(cpn.currentChainParameters) | ||
| cpn.mutData.RUnlock() | ||
| } | ||
|
|
||
| // CurrentChainParameters returns the current chain parameters | ||
| func (cpn *chainParametersNotifier) CurrentChainParameters() config.ChainParametersByEpochConfig { | ||
| cpn.mutData.RLock() | ||
| defer cpn.mutData.RUnlock() | ||
|
|
||
| return cpn.currentChainParameters | ||
| } | ||
|
|
||
| // UnRegisterAll removes all registered handlers queue | ||
| func (cpn *chainParametersNotifier) UnRegisterAll() { | ||
| cpn.mutHandler.Lock() | ||
| cpn.handlers = make([]common.ChainParametersSubscriptionHandler, 0) | ||
| cpn.mutHandler.Unlock() | ||
| } | ||
|
|
||
| // IsInterfaceNil returns true if there is no value under the interface | ||
| func (cpn *chainParametersNotifier) IsInterfaceNil() bool { | ||
| return cpn == nil | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
common/chainparametersnotifier/chainParametersNotifier_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package chainparametersnotifier | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/multiversx/mx-chain-core-go/core/check" | ||
| "github.com/multiversx/mx-chain-go/config" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewChainParametersNotifier(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| notifier := NewChainParametersNotifier() | ||
| require.False(t, check.IfNil(notifier)) | ||
| } | ||
|
|
||
| func TestChainParametersNotifier_UpdateCurrentChainParameters(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| notifier := NewChainParametersNotifier() | ||
| require.False(t, check.IfNil(notifier)) | ||
|
|
||
| chainParams := config.ChainParametersByEpochConfig{ | ||
| EnableEpoch: 7, | ||
| Adaptivity: true, | ||
| Hysteresis: 0.7, | ||
| } | ||
| notifier.UpdateCurrentChainParameters(chainParams) | ||
|
|
||
| resultedChainParams := notifier.CurrentChainParameters() | ||
| require.NotNil(t, resultedChainParams) | ||
|
|
||
| // update with same epoch but other params - should not change (impossible scenario in production, but easier for tests) | ||
| chainParams.Hysteresis = 0.8 | ||
| notifier.UpdateCurrentChainParameters(chainParams) | ||
| require.Equal(t, float32(0.7), notifier.CurrentChainParameters().Hysteresis) | ||
|
|
||
| chainParams.Hysteresis = 0.8 | ||
| chainParams.EnableEpoch = 8 | ||
| notifier.UpdateCurrentChainParameters(chainParams) | ||
| require.Equal(t, float32(0.8), notifier.CurrentChainParameters().Hysteresis) | ||
| } | ||
|
|
||
| func TestChainParametersNotifier_RegisterNotifyHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| notifier := NewChainParametersNotifier() | ||
| require.False(t, check.IfNil(notifier)) | ||
|
|
||
| // register a nil handler - should not panic | ||
| notifier.RegisterNotifyHandler(nil) | ||
|
|
||
| testNotifee := &dummyNotifee{} | ||
| notifier.RegisterNotifyHandler(testNotifee) | ||
|
|
||
| chainParams := config.ChainParametersByEpochConfig{ | ||
| ShardMinNumNodes: 37, | ||
| } | ||
| notifier.UpdateCurrentChainParameters(chainParams) | ||
|
|
||
| require.Equal(t, chainParams, testNotifee.receivedChainParameters) | ||
| } | ||
|
|
||
| func TestChainParametersNotifier_UnRegisterAll(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| notifier := NewChainParametersNotifier() | ||
| require.False(t, check.IfNil(notifier)) | ||
|
|
||
| testNotifee := &dummyNotifee{} | ||
| notifier.RegisterNotifyHandler(testNotifee) | ||
| notifier.UnRegisterAll() | ||
|
|
||
| chainParams := config.ChainParametersByEpochConfig{ | ||
| ShardMinNumNodes: 37, | ||
| } | ||
| notifier.UpdateCurrentChainParameters(chainParams) | ||
|
|
||
| require.Empty(t, testNotifee.receivedChainParameters) | ||
| } | ||
|
|
||
| func TestChainParametersNotifier_ConcurrentOperations(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| notifier := NewChainParametersNotifier() | ||
|
|
||
| numOperations := 500 | ||
| wg := sync.WaitGroup{} | ||
| wg.Add(numOperations) | ||
| for i := 0; i < numOperations; i++ { | ||
| go func(idx int) { | ||
| switch idx { | ||
| case 0: | ||
| notifier.RegisterNotifyHandler(&dummyNotifee{}) | ||
| case 1: | ||
| _ = notifier.CurrentChainParameters() | ||
| case 2: | ||
| notifier.UpdateCurrentChainParameters(config.ChainParametersByEpochConfig{}) | ||
| case 3: | ||
| notifier.UnRegisterAll() | ||
| case 4: | ||
| _ = notifier.IsInterfaceNil() | ||
| } | ||
|
|
||
| wg.Done() | ||
| }(i % 5) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| } | ||
|
|
||
| type dummyNotifee struct { | ||
| receivedChainParameters config.ChainParametersByEpochConfig | ||
| } | ||
|
|
||
| // ChainParametersChanged - | ||
| func (dn *dummyNotifee) ChainParametersChanged(chainParameters config.ChainParametersByEpochConfig) { | ||
| dn.receivedChainParameters = chainParameters | ||
| } | ||
|
|
||
| // IsInterfaceNil - | ||
| func (dn *dummyNotifee) IsInterfaceNil() bool { | ||
| return dn == nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L32-L46 ensures that the handlers are called only once for each epoch change regardless of the number of
UpdateCurrentChainParameterscalls. However, if the calls for different epochs are called in extremely fast manner in parallel, some calls on the handlers can be made in a different order. I do not think this will be an issue because the processing is done in sync.