-
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
Changes from 1 commit
8238e5b
be4806f
e071567
6b6e478
053fe06
617da1e
abc1890
1b49b5a
a885724
b210638
56bcfb7
19b2db2
fbe6f81
55b9939
5601282
961bb62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,8 @@ type p2pAntiflood struct { | |
| peerValidatorMapper process.PeerValidatorMapper | ||
| mapTopicsFromAll map[string]struct{} | ||
| mutTopicCheck sync.RWMutex | ||
| shardID core.OptionalUint32 | ||
| shardID uint32 | ||
| mutShardID sync.RWMutex | ||
| } | ||
|
|
||
| // NewP2PAntiflood creates a new p2p anti flood protection mechanism built on top of a flood preventer implementation. | ||
|
|
@@ -61,18 +62,21 @@ func NewP2PAntiflood( | |
|
|
||
| // SetConsensusSizeNotifier sets the consensus size notifier | ||
| func (af *p2pAntiflood) SetConsensusSizeNotifier(chainParametersNotifier process.ChainParametersSubscriber, shardID uint32) { | ||
| af.shardID = core.OptionalUint32{ | ||
| HasValue: true, | ||
| Value: shardID, | ||
| } | ||
| af.mutShardID.Lock() | ||
| af.shardID = shardID | ||
| af.mutShardID.Unlock() | ||
|
|
||
| chainParametersNotifier.RegisterNotifyHandler(af) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be called on the constructor
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the shard ID is unknown when creating the antiflood components, I cannot make this change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| } | ||
|
|
||
| // ChainParametersChanged will be called when new chain parameters are confirmed on the network | ||
| func (af *p2pAntiflood) ChainParametersChanged(chainParameters config.ChainParametersByEpochConfig) { | ||
| af.mutShardID.RLock() | ||
| shardID := af.shardID | ||
| af.mutShardID.RUnlock() | ||
|
|
||
| size := chainParameters.ShardConsensusGroupSize | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. concurrency issues on the usage of the af.shardID ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a mutex. the setter is needed for computing the consensus size in case of shard vs meta |
||
| if af.shardID.HasValue && af.shardID.Value == core.MetachainShardId { | ||
| if shardID == core.MetachainShardId { | ||
| size = chainParameters.MetachainConsensusGroupSize | ||
| } | ||
|
|
||
|
|
@@ -274,6 +278,9 @@ func (af *p2pAntiflood) BlacklistPeer(peer core.PeerID, reason string, duration | |
|
|
||
| // Close will call the close function on all sub components | ||
| func (af *p2pAntiflood) Close() error { | ||
| af.mutDebugger.Lock() | ||
|
||
| defer af.mutDebugger.Unlock() | ||
|
|
||
| return af.debugger.Close() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package antiflood_test | |
|
|
||
| import ( | ||
| "errors" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -15,6 +16,7 @@ import ( | |
| "github.com/multiversx/mx-chain-go/process/mock" | ||
| "github.com/multiversx/mx-chain-go/process/throttle/antiflood" | ||
| "github.com/multiversx/mx-chain-go/process/throttle/antiflood/disabled" | ||
| "github.com/multiversx/mx-chain-go/testscommon/commonmocks" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
|
|
@@ -325,7 +327,7 @@ func TestP2pAntiflood_SetConsensusSizeNotifier(t *testing.T) { | |
| }, | ||
| ) | ||
|
|
||
| chainParamsSubscriber := chainparametersnotifier.New() | ||
| chainParamsSubscriber := chainparametersnotifier.NewChainParametersNotifier() | ||
| afm.SetConsensusSizeNotifier(chainParamsSubscriber, 5) | ||
|
||
|
|
||
| chainParamsSubscriber.UpdateCurrentChainParameters(config.ChainParametersByEpochConfig{ | ||
|
|
@@ -473,3 +475,51 @@ func TestP2pAntiflood_IsOriginatorEligibleForTopic(t *testing.T) { | |
| err = afm.IsOriginatorEligibleForTopic(core.PeerID(validatorPID), "topic") | ||
| assert.Nil(t, err) | ||
| } | ||
|
|
||
| func TestP2pAntiflood_ConcurrentOperations(t *testing.T) { | ||
| afm, _ := antiflood.NewP2PAntiflood( | ||
| &mock.PeerBlackListHandlerStub{}, | ||
| &mock.TopicAntiFloodStub{}, | ||
| &mock.FloodPreventerStub{}, | ||
| ) | ||
|
|
||
| numOperations := 500 | ||
| wg := sync.WaitGroup{} | ||
| wg.Add(numOperations) | ||
| for i := 0; i < numOperations; i++ { | ||
| go func(idx int) { | ||
| switch idx { | ||
| case 0: | ||
| afm.SetConsensusSizeNotifier(&commonmocks.ChainParametersNotifierStub{}, 1) | ||
| case 1: | ||
| afm.ChainParametersChanged(config.ChainParametersByEpochConfig{}) | ||
| case 2: | ||
| _ = afm.Close() | ||
| case 3: | ||
| _ = afm.CanProcessMessage(&mock.P2PMessageMock{}, "peer") | ||
| case 4: | ||
| afm.BlacklistPeer("peer", "reason", time.Millisecond) | ||
| case 5: | ||
| _ = afm.CanProcessMessagesOnTopic("peer", "topic", 37, 39, []byte("sequence")) | ||
| case 6: | ||
| _ = afm.IsOriginatorEligibleForTopic("peer", "topic") | ||
| case 7: | ||
| afm.ResetForTopic("topic") | ||
| case 8: | ||
| _ = afm.SetDebugger(&disabled.AntifloodDebugger{}) | ||
| case 9: | ||
| afm.SetMaxMessagesForTopic("topic", 37) | ||
| case 10: | ||
| afm.SetTopicsForAll("topic", "topic1") | ||
| case 11: | ||
| _ = afm.Debugger() | ||
| case 12: | ||
| _ = afm.SetPeerValidatorMapper(&mock.PeerShardResolverStub{}) | ||
| } | ||
|
|
||
| wg.Done() | ||
| }(i % 13) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| } | ||
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.
TestNewChainParametersNotifier ?
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.
done