diff --git a/common/constants.go b/common/constants.go index 0e7f6774ce5..affbbe2cce3 100644 --- a/common/constants.go +++ b/common/constants.go @@ -596,6 +596,8 @@ const ( const ( // StorerOrder defines the order of storers to be notified of a start of epoch event StorerOrder = iota + // ChainParametersOrder defines the order in which ChainParameters is notified of a start of epoch event + ChainParametersOrder // NodesCoordinatorOrder defines the order in which NodesCoordinator is notified of a start of epoch event NodesCoordinatorOrder // ConsensusOrder defines the order in which Consensus is notified of a start of epoch event diff --git a/factory/core/coreComponents.go b/factory/core/coreComponents.go index 726a965f4c8..f32352e9504 100644 --- a/factory/core/coreComponents.go +++ b/factory/core/coreComponents.go @@ -179,10 +179,11 @@ func (ccf *coreComponentsFactory) Create() (*coreComponents, error) { log.Debug("NTP average clock offset", "value", syncer.ClockOffset()) epochNotifier := forking.NewGenericEpochNotifier() + epochStartHandlerWithConfirm := notifier.NewEpochStartSubscriptionHandler() argsChainParametersHandler := sharding.ArgsChainParametersHolder{ - EpochNotifier: epochNotifier, - ChainParameters: ccf.config.GeneralSettings.ChainParametersByEpoch, + EpochStartEventNotifier: epochStartHandlerWithConfirm, + ChainParameters: ccf.config.GeneralSettings.ChainParametersByEpoch, } chainParametersHandler, err := sharding.NewChainParametersHolder(argsChainParametersHandler) if err != nil { @@ -305,13 +306,10 @@ func (ccf *coreComponentsFactory) Create() (*coreComponents, error) { } argsNodesShuffler := &nodesCoordinator.NodesShufflerArgs{ - NodesShard: genesisNodesConfig.MinNumberOfShardNodes(), - NodesMeta: genesisNodesConfig.MinNumberOfMetaNodes(), - Hysteresis: genesisNodesConfig.GetHysteresis(), - Adaptivity: genesisNodesConfig.GetAdaptivity(), - ShuffleBetweenShards: true, - MaxNodesEnableConfig: ccf.epochConfig.EnableEpochs.MaxNodesChangeEnableEpoch, - EnableEpochsHandler: enableEpochsHandler, + ChainParametersHandler: chainParametersHandler, + ShuffleBetweenShards: true, + MaxNodesEnableConfig: ccf.epochConfig.EnableEpochs.MaxNodesChangeEnableEpoch, + EnableEpochsHandler: enableEpochsHandler, } nodesShuffler, err := nodesCoordinator.NewHashValidatorsShuffler(argsNodesShuffler) diff --git a/integrationTests/nodesCoordinatorFactory.go b/integrationTests/nodesCoordinatorFactory.go index 0a3cc6193e3..bd82764a5da 100644 --- a/integrationTests/nodesCoordinatorFactory.go +++ b/integrationTests/nodesCoordinatorFactory.go @@ -5,12 +5,14 @@ import ( "github.com/ElrondNetwork/elrond-go-core/data/endProcess" "github.com/ElrondNetwork/elrond-go-core/hashing" + "github.com/ElrondNetwork/elrond-go/config" "github.com/ElrondNetwork/elrond-go/integrationTests/mock" "github.com/ElrondNetwork/elrond-go/sharding" "github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator" "github.com/ElrondNetwork/elrond-go/storage" "github.com/ElrondNetwork/elrond-go/testscommon" "github.com/ElrondNetwork/elrond-go/testscommon/nodeTypeProviderMock" + "github.com/ElrondNetwork/elrond-go/testscommon/shardingmock" vic "github.com/ElrondNetwork/elrond-go/testscommon/validatorInfoCacher" ) @@ -43,10 +45,16 @@ func (tpn *IndexHashedNodesCoordinatorFactory) CreateNodesCoordinator(arg ArgInd pubKeyBytes, _ := keys.Pk.ToByteArray() nodeShufflerArgs := &nodesCoordinator.NodesShufflerArgs{ - NodesShard: uint32(arg.nodesPerShard), - NodesMeta: uint32(arg.nbMetaNodes), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: &shardingmock.ChainParametersHandlerStub{ + CurrentChainParametersCalled: func() config.ChainParametersByEpochConfig { + return config.ChainParametersByEpochConfig{ + ShardMinNumNodes: uint32(arg.nodesPerShard), + MetachainMinNumNodes: uint32(arg.nbMetaNodes), + Hysteresis: hysteresis, + Adaptivity: adaptivity, + } + }, + }, ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, @@ -97,10 +105,16 @@ func (ihncrf *IndexHashedNodesCoordinatorWithRaterFactory) CreateNodesCoordinato pubKeyBytes, _ := keys.Pk.ToByteArray() shufflerArgs := &nodesCoordinator.NodesShufflerArgs{ - NodesShard: uint32(arg.nodesPerShard), - NodesMeta: uint32(arg.nbMetaNodes), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: &shardingmock.ChainParametersHandlerStub{ + CurrentChainParametersCalled: func() config.ChainParametersByEpochConfig { + return config.ChainParametersByEpochConfig{ + ShardMinNumNodes: uint32(arg.nodesPerShard), + MetachainMinNumNodes: uint32(arg.nbMetaNodes), + Hysteresis: hysteresis, + Adaptivity: adaptivity, + } + }, + }, ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{ diff --git a/integrationTests/testProcessorNodeWithMultisigner.go b/integrationTests/testProcessorNodeWithMultisigner.go index 162687db599..e2a6fe323ea 100644 --- a/integrationTests/testProcessorNodeWithMultisigner.go +++ b/integrationTests/testProcessorNodeWithMultisigner.go @@ -32,6 +32,7 @@ import ( "github.com/ElrondNetwork/elrond-go/testscommon/cryptoMocks" "github.com/ElrondNetwork/elrond-go/testscommon/nodeTypeProviderMock" "github.com/ElrondNetwork/elrond-go/testscommon/shardingMocks" + "github.com/ElrondNetwork/elrond-go/testscommon/shardingmock" vic "github.com/ElrondNetwork/elrond-go/testscommon/validatorInfoCacher" ) @@ -392,10 +393,16 @@ func CreateNodesWithNodesCoordinatorAndHeaderSigVerifier( nodesMap := make(map[uint32][]*TestProcessorNode) shufflerArgs := &nodesCoordinator.NodesShufflerArgs{ - NodesShard: uint32(nodesPerShard), - NodesMeta: uint32(nbMetaNodes), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: &shardingmock.ChainParametersHandlerStub{ + CurrentChainParametersCalled: func() config.ChainParametersByEpochConfig { + return config.ChainParametersByEpochConfig{ + ShardMinNumNodes: uint32(nodesPerShard), + MetachainMinNumNodes: uint32(nbMetaNodes), + Hysteresis: hysteresis, + Adaptivity: adaptivity, + } + }, + }, ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, diff --git a/sharding/chainParametersHolder.go b/sharding/chainParametersHolder.go index 01cccbb5eee..a5cb3ad3d13 100644 --- a/sharding/chainParametersHolder.go +++ b/sharding/chainParametersHolder.go @@ -6,6 +6,8 @@ import ( "sync" "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go-core/data" + "github.com/ElrondNetwork/elrond-go/common" "github.com/ElrondNetwork/elrond-go/config" ) @@ -17,8 +19,8 @@ type chainParametersHolder struct { // ArgsChainParametersHolder holds the arguments needed for creating a new chainParametersHolder type ArgsChainParametersHolder struct { - EpochNotifier EpochNotifier - ChainParameters []config.ChainParametersByEpochConfig + EpochStartEventNotifier EpochStartEventNotifier + ChainParameters []config.ChainParametersByEpochConfig } // NewChainParametersHolder returns a new instance of chainParametersHolder @@ -39,24 +41,19 @@ func NewChainParametersHolder(args ArgsChainParametersHolder) (*chainParametersH return nil, ErrMissingConfigurationForEpochZero } - currentParams, err := getMatchingChainParametersUnprotected(args.EpochNotifier.CurrentEpoch(), args.ChainParameters) - if err != nil { - return nil, err - } - paramsHolder := &chainParametersHolder{ - currentChainParameters: currentParams, + currentChainParameters: earliestChainParams, // will be updated on the epoch notifier handlers chainParameters: args.ChainParameters, } - args.EpochNotifier.RegisterNotifyHandler(paramsHolder) + args.EpochStartEventNotifier.RegisterHandler(paramsHolder) return paramsHolder, nil } func validateArgs(args ArgsChainParametersHolder) error { - if check.IfNil(args.EpochNotifier) { - return ErrNilEpochNotifier + if check.IfNil(args.EpochStartEventNotifier) { + return ErrNilEpochStartEventNotifier } if len(args.ChainParameters) == 0 { return ErrMissingChainParameters @@ -83,8 +80,21 @@ func validateChainParameters(chainParametersConfig []config.ChainParametersByEpo return nil } -// EpochConfirmed is called at each epoch change event -func (c *chainParametersHolder) EpochConfirmed(epoch uint32, _ uint64) { +// EpochStartAction is called when a new epoch is confirmed +func (c *chainParametersHolder) EpochStartAction(header data.HeaderHandler) { + c.handleEpochChange(header.GetEpoch()) +} + +// EpochStartPrepare is called when a new epoch is observed, but not yet confirmed. No action is required on this component +func (c *chainParametersHolder) EpochStartPrepare(_ data.HeaderHandler, _ data.BodyHandler) { +} + +// NotifyOrder returns the notification order for a start of epoch event +func (c *chainParametersHolder) NotifyOrder() uint32 { + return common.ChainParametersOrder +} + +func (c *chainParametersHolder) handleEpochChange(epoch uint32) { c.mutOperations.Lock() defer c.mutOperations.Unlock() diff --git a/sharding/chainParametersHolder_test.go b/sharding/chainParametersHolder_test.go index 15b6521f962..58285fec09f 100644 --- a/sharding/chainParametersHolder_test.go +++ b/sharding/chainParametersHolder_test.go @@ -6,8 +6,9 @@ import ( "testing" "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go-core/data/block" "github.com/ElrondNetwork/elrond-go/config" - "github.com/ElrondNetwork/elrond-go/testscommon/epochNotifier" + "github.com/ElrondNetwork/elrond-go/testscommon/epochstartmock" "github.com/stretchr/testify/require" ) @@ -16,7 +17,7 @@ func TestNewChainParametersHolder(t *testing.T) { getDummyArgs := func() ArgsChainParametersHolder { return ArgsChainParametersHolder{ - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, ChainParameters: []config.ChainParametersByEpochConfig{ { EnableEpoch: 0, @@ -32,15 +33,15 @@ func TestNewChainParametersHolder(t *testing.T) { } } - t.Run("nil epoch notifier", func(t *testing.T) { + t.Run("nil epoch start event notifier", func(t *testing.T) { t.Parallel() args := getDummyArgs() - args.EpochNotifier = nil + args.EpochStartEventNotifier = nil paramsHolder, err := NewChainParametersHolder(args) require.True(t, check.IfNil(paramsHolder)) - require.Equal(t, ErrNilEpochNotifier, err) + require.Equal(t, ErrNilEpochStartEventNotifier, err) }) t.Run("empty chain parameters", func(t *testing.T) { @@ -165,8 +166,8 @@ func TestChainParametersHolder_ChainParametersForEpoch(t *testing.T) { } paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{ - ChainParameters: params, - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + ChainParameters: params, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, }) res, _ := paramsHolder.ChainParametersForEpoch(0) @@ -210,8 +211,8 @@ func TestChainParametersHolder_ChainParametersForEpoch(t *testing.T) { } paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{ - ChainParameters: params, - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + ChainParameters: params, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, }) for i := 0; i < 200; i++ { @@ -251,20 +252,20 @@ func TestChainParametersHolder_CurrentChainParameters(t *testing.T) { } paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{ - ChainParameters: params, - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + ChainParameters: params, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, }) - paramsHolder.EpochConfirmed(0, 0) + paramsHolder.EpochStartAction(&block.MetaBlock{Epoch: 0}) require.Equal(t, uint32(5), paramsHolder.CurrentChainParameters().ShardConsensusGroupSize) - paramsHolder.EpochConfirmed(3, 0) + paramsHolder.EpochStartAction(&block.MetaBlock{Epoch: 3}) require.Equal(t, uint32(5), paramsHolder.CurrentChainParameters().ShardConsensusGroupSize) - paramsHolder.EpochConfirmed(10, 0) + paramsHolder.EpochStartAction(&block.MetaBlock{Epoch: 10}) require.Equal(t, uint32(50), paramsHolder.CurrentChainParameters().ShardConsensusGroupSize) - paramsHolder.EpochConfirmed(999, 0) + paramsHolder.EpochStartAction(&block.MetaBlock{Epoch: 999}) require.Equal(t, uint32(50), paramsHolder.CurrentChainParameters().ShardConsensusGroupSize) } @@ -289,8 +290,8 @@ func TestChainParametersHolder_AllChainParameters(t *testing.T) { } paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{ - ChainParameters: params, - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + ChainParameters: params, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, }) returnedAllChainsParameters := paramsHolder.AllChainParameters() @@ -314,8 +315,8 @@ func TestChainParametersHolder_ConcurrentOperations(t *testing.T) { } paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{ - ChainParameters: chainParams, - EpochNotifier: &epochNotifier.EpochNotifierStub{}, + ChainParameters: chainParams, + EpochStartEventNotifier: &epochstartmock.EpochStartNotifierStub{}, }) numOperations := 500 @@ -325,7 +326,7 @@ func TestChainParametersHolder_ConcurrentOperations(t *testing.T) { go func(idx int) { switch idx { case 0: - paramsHolder.EpochConfirmed(uint32(idx), 0) + paramsHolder.EpochStartAction(&block.MetaBlock{Epoch: uint32(idx)}) case 1: _ = paramsHolder.CurrentChainParameters() case 2: diff --git a/sharding/errors.go b/sharding/errors.go index 792b57b5d10..e4c0ba98d3c 100644 --- a/sharding/errors.go +++ b/sharding/errors.go @@ -43,8 +43,8 @@ var ErrNilEndOfProcessingHandler = errors.New("nil end of processing handler") // ErrNilChainParametersProvider signals that a nil chain parameters provider has been given var ErrNilChainParametersProvider = errors.New("nil chain parameters provider") -// ErrNilEpochNotifier signals that a nil epoch notifier has been provided -var ErrNilEpochNotifier = errors.New("nil epoch notifier") +// ErrNilEpochStartEventNotifier signals that a nil epoch start event notifier has been provided +var ErrNilEpochStartEventNotifier = errors.New("nil epoch start event notifier") // ErrMissingChainParameters signals that a nil chain parameters array has been provided var ErrMissingChainParameters = errors.New("empty chain parameters array") diff --git a/sharding/interface.go b/sharding/interface.go index 0dc84c6f0d5..1bb753700c5 100644 --- a/sharding/interface.go +++ b/sharding/interface.go @@ -1,10 +1,9 @@ package sharding import ( - "github.com/ElrondNetwork/elrond-go-core/data" "github.com/ElrondNetwork/elrond-go/config" + "github.com/ElrondNetwork/elrond-go/epochStart" "github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator" - vmcommon "github.com/ElrondNetwork/elrond-vm-common" ) // Coordinator defines what a shard state coordinator should hold @@ -68,11 +67,10 @@ type GenesisNodesSetupHandler interface { IsInterfaceNil() bool } -// EpochNotifier can notify upon an epoch change and provide the current epoch -type EpochNotifier interface { - RegisterNotifyHandler(handler vmcommon.EpochSubscriberHandler) - CurrentEpoch() uint32 - CheckEpoch(header data.HeaderHandler) +// EpochStartEventNotifier provides Register and Unregister functionality for the end of epoch events +type EpochStartEventNotifier interface { + RegisterHandler(handler epochStart.ActionHandler) + UnregisterHandler(handler epochStart.ActionHandler) IsInterfaceNil() bool } diff --git a/sharding/mock/epochHandlerMock.go b/sharding/mock/epochHandlerMock.go deleted file mode 100644 index 9b78066bd3e..00000000000 --- a/sharding/mock/epochHandlerMock.go +++ /dev/null @@ -1,16 +0,0 @@ -package mock - -// EpochHandlerMock - -type EpochHandlerMock struct { - EpochValue uint32 -} - -// Epoch - -func (ehm *EpochHandlerMock) Epoch() uint32 { - return ehm.EpochValue -} - -// IsInterfaceNil - -func (ehm *EpochHandlerMock) IsInterfaceNil() bool { - return ehm == nil -} diff --git a/sharding/mock/epochHandlerStub.go b/sharding/mock/epochHandlerStub.go deleted file mode 100644 index 4470eaca56c..00000000000 --- a/sharding/mock/epochHandlerStub.go +++ /dev/null @@ -1,20 +0,0 @@ -package mock - -// EpochHandlerStub - -type EpochHandlerStub struct { - EpochCalled func() uint32 -} - -// Epoch - -func (ehs *EpochHandlerStub) Epoch() uint32 { - if ehs.EpochCalled != nil { - return ehs.EpochCalled() - } - - return uint32(0) -} - -// IsInterfaceNil - -func (ehs *EpochHandlerStub) IsInterfaceNil() bool { - return ehs == nil -} diff --git a/sharding/mock/hasherStub.go b/sharding/mock/hasherStub.go deleted file mode 100644 index f05c2fd2cc8..00000000000 --- a/sharding/mock/hasherStub.go +++ /dev/null @@ -1,28 +0,0 @@ -package mock - -// HasherStub - -type HasherStub struct { - ComputeCalled func(s string) []byte - EmptyHashCalled func() []byte - SizeCalled func() int -} - -// Compute will output the SHA's equivalent of the input string -func (hs *HasherStub) Compute(s string) []byte { - return hs.ComputeCalled(s) -} - -// EmptyHash will return the equivalent of empty string SHA's -func (hs *HasherStub) EmptyHash() []byte { - return hs.EmptyHashCalled() -} - -// Size returns the required size in bytes -func (hs *HasherStub) Size() int { - return hs.SizeCalled() -} - -// IsInterfaceNil returns true if there is no value under the interface -func (hs *HasherStub) IsInterfaceNil() bool { - return hs == nil -} diff --git a/sharding/mock/listIndexUpdaterStub.go b/sharding/mock/listIndexUpdaterStub.go deleted file mode 100644 index 31c5ae19b76..00000000000 --- a/sharding/mock/listIndexUpdaterStub.go +++ /dev/null @@ -1,20 +0,0 @@ -package mock - -// ListIndexUpdaterStub - -type ListIndexUpdaterStub struct { - UpdateListAndIndexCalled func(pubKey string, shardID uint32, list string, index uint32) error -} - -// UpdateListAndIndex - -func (lius *ListIndexUpdaterStub) UpdateListAndIndex(pubKey string, shardID uint32, list string, index uint32) error { - if lius.UpdateListAndIndexCalled != nil { - return lius.UpdateListAndIndexCalled(pubKey, shardID, list, index) - } - - return nil -} - -// IsInterfaceNil returns true if there is no value under the interface -func (lius *ListIndexUpdaterStub) IsInterfaceNil() bool { - return lius == nil -} diff --git a/sharding/mock/multipleShardsCoordinatorFake.go b/sharding/mock/multipleShardsCoordinatorFake.go deleted file mode 100644 index 89a145beca2..00000000000 --- a/sharding/mock/multipleShardsCoordinatorFake.go +++ /dev/null @@ -1,94 +0,0 @@ -package mock - -import ( - "fmt" - "math" -) - -type multipleShardsCoordinatorFake struct { - numOfShards uint32 - CurrentShard uint32 - maskHigh uint32 - maskLow uint32 -} - -// NewMultipleShardsCoordinatorFake - -func NewMultipleShardsCoordinatorFake(numOfShards uint32, currentShard uint32) *multipleShardsCoordinatorFake { - mscf := &multipleShardsCoordinatorFake{ - numOfShards: numOfShards, - CurrentShard: currentShard, - } - mscf.maskHigh, mscf.maskLow = mscf.calculateMasks() - return mscf -} - -func (mscf *multipleShardsCoordinatorFake) calculateMasks() (uint32, uint32) { - n := math.Ceil(math.Log2(float64(mscf.numOfShards))) - return (1 << uint(n)) - 1, (1 << uint(n-1)) - 1 -} - -// NumberOfShards - -func (mscf *multipleShardsCoordinatorFake) NumberOfShards() uint32 { - return mscf.numOfShards -} - -// ComputeId - -func (mscf *multipleShardsCoordinatorFake) ComputeId(address []byte) uint32 { - bytesNeed := int(mscf.numOfShards/256) + 1 - startingIndex := 0 - if len(address) > bytesNeed { - startingIndex = len(address) - bytesNeed - } - - buffNeeded := address[startingIndex:] - - addr := uint32(0) - for i := 0; i < len(buffNeeded); i++ { - addr = addr<<8 + uint32(buffNeeded[i]) - } - - shard := addr & mscf.maskHigh - if shard > mscf.numOfShards-1 { - shard = addr & mscf.maskLow - } - return shard -} - -// SelfId - -func (mscf *multipleShardsCoordinatorFake) SelfId() uint32 { - return mscf.CurrentShard -} - -// SetSelfId - -func (mscf *multipleShardsCoordinatorFake) SetSelfId(_ uint32) error { - return nil -} - -// SameShard - -func (mscf *multipleShardsCoordinatorFake) SameShard(_, _ []byte) bool { - return true -} - -// SetNoShards - -func (mscf *multipleShardsCoordinatorFake) SetNoShards(numOfShards uint32) { - mscf.numOfShards = numOfShards -} - -// CommunicationIdentifier returns the identifier between current shard ID and destination shard ID -// identifier is generated such as the first shard from identifier is always smaller than the last -func (mscf *multipleShardsCoordinatorFake) CommunicationIdentifier(destShardID uint32) string { - if destShardID == mscf.CurrentShard { - return fmt.Sprintf("_%d", mscf.CurrentShard) - } - - if destShardID < mscf.CurrentShard { - return fmt.Sprintf("_%d_%d", destShardID, mscf.CurrentShard) - } - - return fmt.Sprintf("_%d_%d", mscf.CurrentShard, destShardID) -} - -// IsInterfaceNil returns true if there is no value under the interface -func (mscf *multipleShardsCoordinatorFake) IsInterfaceNil() bool { - return mscf == nil -} diff --git a/sharding/nodesCoordinator/errors.go b/sharding/nodesCoordinator/errors.go index 7c2bf75f933..1dc3decc4ac 100644 --- a/sharding/nodesCoordinator/errors.go +++ b/sharding/nodesCoordinator/errors.go @@ -108,3 +108,6 @@ var ErrNilEnableEpochsHandler = errors.New("nil enable epochs handler") // ErrNilValidatorInfoCacher signals that a nil value for the validator info cacher has been provided var ErrNilValidatorInfoCacher = errors.New("validator info cacher is nil") + +// ErrNilChainParametersHandler signals that a nil chain parameters handler has been provided +var ErrNilChainParametersHandler = errors.New("nil chain parameters handler") diff --git a/sharding/nodesCoordinator/hashValidatorShuffler.go b/sharding/nodesCoordinator/hashValidatorShuffler.go index 033d0e10013..e48670828b7 100644 --- a/sharding/nodesCoordinator/hashValidatorShuffler.go +++ b/sharding/nodesCoordinator/hashValidatorShuffler.go @@ -18,13 +18,10 @@ var _ NodesShuffler = (*randHashShuffler)(nil) // NodesShufflerArgs defines the arguments required to create a nodes shuffler type NodesShufflerArgs struct { - NodesShard uint32 - NodesMeta uint32 - Hysteresis float32 - Adaptivity bool - ShuffleBetweenShards bool - MaxNodesEnableConfig []config.MaxNodesChangeConfig - EnableEpochsHandler common.EnableEpochsHandler + ShuffleBetweenShards bool + MaxNodesEnableConfig []config.MaxNodesChangeConfig + EnableEpochsHandler common.EnableEpochsHandler + ChainParametersHandler ChainParametersHandler } type shuffleNodesArg struct { @@ -49,11 +46,7 @@ type randHashShuffler struct { // when reinitialization of node in new shard is implemented shuffleBetweenShards bool - adaptivity bool - nodesShard uint32 - nodesMeta uint32 - shardHysteresis uint32 - metaHysteresis uint32 + currentChainParameters config.ChainParametersByEpochConfig activeNodesConfig config.MaxNodesChangeConfig availableNodesConfigs []config.MaxNodesChangeConfig mutShufflerParams sync.RWMutex @@ -72,7 +65,9 @@ func NewHashValidatorsShuffler(args *NodesShufflerArgs) (*randHashShuffler, erro if check.IfNil(args.EnableEpochsHandler) { return nil, ErrNilEnableEpochsHandler } - + if check.IfNil(args.ChainParametersHandler) { + return nil, ErrNilChainParametersHandler + } var configs []config.MaxNodesChangeConfig log.Debug("hashValidatorShuffler: enable epoch for max nodes change", "epoch", args.MaxNodesEnableConfig) @@ -81,15 +76,15 @@ func NewHashValidatorsShuffler(args *NodesShufflerArgs) (*randHashShuffler, erro copy(configs, args.MaxNodesEnableConfig) } + currentChainParameters := args.ChainParametersHandler.CurrentChainParameters() log.Debug("Shuffler created", "shuffleBetweenShards", args.ShuffleBetweenShards) rxs := &randHashShuffler{ - shuffleBetweenShards: args.ShuffleBetweenShards, - availableNodesConfigs: configs, - enableEpochsHandler: args.EnableEpochsHandler, + currentChainParameters: currentChainParameters, + shuffleBetweenShards: args.ShuffleBetweenShards, + availableNodesConfigs: configs, + enableEpochsHandler: args.EnableEpochsHandler, } - rxs.UpdateParams(args.NodesShard, args.NodesMeta, args.Hysteresis, args.Adaptivity) - if rxs.shuffleBetweenShards { rxs.validatorDistributor = &CrossShardValidatorDistributor{} } else { @@ -101,27 +96,6 @@ func NewHashValidatorsShuffler(args *NodesShufflerArgs) (*randHashShuffler, erro return rxs, nil } -// UpdateParams updates the shuffler parameters -// Should be called when new params are agreed through governance -func (rhs *randHashShuffler) UpdateParams( - nodesShard uint32, - nodesMeta uint32, - hysteresis float32, - adaptivity bool, -) { - // TODO: are there constraints we want to enforce? e.g min/max hysteresis - shardHysteresis := uint32(float32(nodesShard) * hysteresis) - metaHysteresis := uint32(float32(nodesMeta) * hysteresis) - - rhs.mutShufflerParams.Lock() - rhs.shardHysteresis = shardHysteresis - rhs.metaHysteresis = metaHysteresis - rhs.nodesShard = nodesShard - rhs.nodesMeta = nodesMeta - rhs.adaptivity = adaptivity - rhs.mutShufflerParams.Unlock() -} - // UpdateNodeLists shuffles the nodes and returns the lists with the new nodes configuration // The function needs to ensure that: // 1. Old eligible nodes list will have up to shuffleOutThreshold percent nodes shuffled out from each shard @@ -143,7 +117,7 @@ func (rhs *randHashShuffler) UpdateNodeLists(args ArgsUpdateNodes) (*ResUpdateNo eligibleAfterReshard := copyValidatorMap(args.Eligible) waitingAfterReshard := copyValidatorMap(args.Waiting) - args.AdditionalLeaving = removeDupplicates(args.UnStakeLeaving, args.AdditionalLeaving) + args.AdditionalLeaving = removeDuplicates(args.UnStakeLeaving, args.AdditionalLeaving) totalLeavingNum := len(args.AdditionalLeaving) + len(args.UnStakeLeaving) newNbShards := rhs.computeNewShards( @@ -155,10 +129,10 @@ func (rhs *randHashShuffler) UpdateNodeLists(args ArgsUpdateNodes) (*ResUpdateNo ) rhs.mutShufflerParams.RLock() - canSplit := rhs.adaptivity && newNbShards > args.NbShards - canMerge := rhs.adaptivity && newNbShards < args.NbShards - nodesPerShard := rhs.nodesShard - nodesMeta := rhs.nodesMeta + canSplit := rhs.currentChainParameters.Adaptivity && newNbShards > args.NbShards + canMerge := rhs.currentChainParameters.Adaptivity && newNbShards < args.NbShards + nodesPerShard := rhs.currentChainParameters.ShardMinNumNodes + nodesMeta := rhs.currentChainParameters.MetachainMinNumNodes rhs.mutShufflerParams.RUnlock() if canSplit { @@ -185,7 +159,7 @@ func (rhs *randHashShuffler) UpdateNodeLists(args ArgsUpdateNodes) (*ResUpdateNo }) } -func removeDupplicates(unstake []Validator, additionalLeaving []Validator) []Validator { +func removeDuplicates(unstake []Validator, additionalLeaving []Validator) []Validator { additionalCopy := make([]Validator, 0, len(additionalLeaving)) additionalCopy = append(additionalCopy, additionalLeaving...) @@ -452,10 +426,10 @@ func (rhs *randHashShuffler) computeNewShards( nodesNewEpoch := uint32(nbEligible + nbWaiting + numNewNodes - numLeavingNodes) rhs.mutShufflerParams.RLock() - maxNodesMeta := rhs.nodesMeta + rhs.metaHysteresis - maxNodesShard := rhs.nodesShard + rhs.shardHysteresis + maxNodesMeta := rhs.currentChainParameters.MetachainMinNumNodes + rhs.metaHysteresis() + maxNodesShard := rhs.currentChainParameters.ShardMinNumNodes + rhs.shardHysteresis() nodesForSplit := (nbShards+1)*maxNodesShard + maxNodesMeta - nodesForMerge := nbShards*rhs.nodesShard + rhs.nodesMeta + nodesForMerge := nbShards*rhs.currentChainParameters.ShardMinNumNodes + rhs.currentChainParameters.MetachainMinNumNodes rhs.mutShufflerParams.RUnlock() nbShardsNew := nbShards @@ -473,6 +447,14 @@ func (rhs *randHashShuffler) computeNewShards( return nbShardsNew } +func (rhs *randHashShuffler) metaHysteresis() uint32 { + return uint32(rhs.currentChainParameters.Hysteresis * float32(rhs.currentChainParameters.MetachainMinNumNodes)) +} + +func (rhs *randHashShuffler) shardHysteresis() uint32 { + return uint32(rhs.currentChainParameters.Hysteresis * float32(rhs.currentChainParameters.ShardMinNumNodes)) +} + // shuffleOutNodes shuffles the list of eligible validators in each shard and returns the map of shuffled out // validators func shuffleOutNodes( @@ -761,7 +743,7 @@ func sortKeys(nodes map[uint32][]Validator) []uint32 { func (rhs *randHashShuffler) UpdateShufflerConfig(epoch uint32) { rhs.mutShufflerParams.Lock() defer rhs.mutShufflerParams.Unlock() - rhs.activeNodesConfig.NodesToShufflePerShard = rhs.nodesShard + rhs.activeNodesConfig.NodesToShufflePerShard = rhs.currentChainParameters.ShardMinNumNodes for _, maxNodesConfig := range rhs.availableNodesConfigs { if epoch >= maxNodesConfig.EpochEnable { rhs.activeNodesConfig = maxNodesConfig diff --git a/sharding/nodesCoordinator/hashValidatorShuffler_test.go b/sharding/nodesCoordinator/hashValidatorShuffler_test.go index 4fb761ae452..f08c309c849 100644 --- a/sharding/nodesCoordinator/hashValidatorShuffler_test.go +++ b/sharding/nodesCoordinator/hashValidatorShuffler_test.go @@ -15,6 +15,7 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core" "github.com/ElrondNetwork/elrond-go/config" "github.com/ElrondNetwork/elrond-go/sharding/mock" + "github.com/ElrondNetwork/elrond-go/testscommon/shardingmock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -27,6 +28,27 @@ const ( waitingPerShard = 30 ) +type testChainParametersCreator struct { + numNodesShards uint32 + numNodesMeta uint32 + hysteresis float32 + adaptivity bool +} + +func (t testChainParametersCreator) build() ChainParametersHandler { + return &shardingmock.ChainParametersHandlerStub{ + CurrentChainParametersCalled: func() config.ChainParametersByEpochConfig { + return config.ChainParametersByEpochConfig{ + EnableEpoch: 0, + Hysteresis: t.hysteresis, + ShardMinNumNodes: t.numNodesShards, + MetachainMinNumNodes: t.numNodesMeta, + Adaptivity: t.adaptivity, + } + }, + } +} + func generateRandomByteArray(size int) []byte { r := make([]byte, size) _, _ = rand.Read(r) @@ -187,10 +209,12 @@ func testShuffledOut( func createHashShufflerInter() (*randHashShuffler, error) { shufflerArgs := &NodesShufflerArgs{ - NodesShard: eligiblePerShard, - NodesMeta: eligiblePerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: true, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -203,10 +227,12 @@ func createHashShufflerInter() (*randHashShuffler, error) { func createHashShufflerIntraShards() (*randHashShuffler, error) { shufflerArgs := &NodesShufflerArgs{ - NodesShard: eligiblePerShard, - NodesMeta: eligiblePerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -582,8 +608,8 @@ func Test_removeValidatorsFromListRemoveFromLastMaxGreater(t *testing.T) { func Test_removeValidatorsFromListRandomValidatorsMaxSmaller(t *testing.T) { t.Parallel() - nbValidatotrsToRemove := 10 - maxToRemove := nbValidatotrsToRemove - 3 + nbValidatorsToRemove := 10 + maxToRemove := nbValidatorsToRemove - 3 validators := generateValidatorList(30) validatorsCopy := make([]Validator, len(validators)) validatorsToRemove := make([]Validator, 0) @@ -592,7 +618,7 @@ func Test_removeValidatorsFromListRandomValidatorsMaxSmaller(t *testing.T) { sort.Sort(validatorList(validators)) - validatorsToRemove = append(validatorsToRemove, validators[:nbValidatotrsToRemove]...) + validatorsToRemove = append(validatorsToRemove, validators[:nbValidatorsToRemove]...) v, removed := removeValidatorsFromList(validators, validatorsToRemove, maxToRemove) testRemoveValidators(t, validatorsCopy, validatorsToRemove, v, removed, maxToRemove) @@ -601,8 +627,8 @@ func Test_removeValidatorsFromListRandomValidatorsMaxSmaller(t *testing.T) { func Test_removeValidatorsFromListRandomValidatorsMaxGreater(t *testing.T) { t.Parallel() - nbValidatotrsToRemove := 10 - maxToRemove := nbValidatotrsToRemove + 3 + nbValidatorsToRemove := 10 + maxToRemove := nbValidatorsToRemove + 3 validators := generateValidatorList(30) validatorsCopy := make([]Validator, len(validators)) validatorsToRemove := make([]Validator, 0) @@ -611,13 +637,13 @@ func Test_removeValidatorsFromListRandomValidatorsMaxGreater(t *testing.T) { sort.Sort(validatorList(validators)) - validatorsToRemove = append(validatorsToRemove, validators[:nbValidatotrsToRemove]...) + validatorsToRemove = append(validatorsToRemove, validators[:nbValidatorsToRemove]...) v, removed := removeValidatorsFromList(validators, validatorsToRemove, maxToRemove) testRemoveValidators(t, validatorsCopy, validatorsToRemove, v, removed, maxToRemove) } -func Test_removeDupplicates_NoDupplicates(t *testing.T) { +func Test_removeDuplicates_NoDuplicates(t *testing.T) { t.Parallel() firstList := generateValidatorList(30) @@ -629,13 +655,13 @@ func Test_removeDupplicates_NoDupplicates(t *testing.T) { secondListCopy := make([]Validator, len(secondList)) copy(secondListCopy, secondList) - secondListAfterRemove := removeDupplicates(firstList, secondList) + secondListAfterRemove := removeDuplicates(firstList, secondList) assert.Equal(t, firstListCopy, firstList) assert.Equal(t, secondListCopy, secondListAfterRemove) } -func Test_removeDupplicates_SomeDupplicates(t *testing.T) { +func Test_removeDuplicates_SomeDuplicates(t *testing.T) { t.Parallel() firstList := generateValidatorList(30) @@ -649,14 +675,14 @@ func Test_removeDupplicates_SomeDupplicates(t *testing.T) { secondListCopy := make([]Validator, len(secondList)) copy(secondListCopy, secondList) - secondListAfterRemove := removeDupplicates(firstList, secondList) + secondListAfterRemove := removeDuplicates(firstList, secondList) assert.Equal(t, firstListCopy, firstList) assert.Equal(t, len(secondListCopy)-len(validatorsFromFirstList), len(secondListAfterRemove)) assert.Equal(t, secondListCopy[:20], secondListAfterRemove) } -func Test_removeDupplicates_AllDupplicates(t *testing.T) { +func Test_removeDuplicates_AllDuplicates(t *testing.T) { t.Parallel() firstList := generateValidatorList(30) @@ -668,7 +694,7 @@ func Test_removeDupplicates_AllDupplicates(t *testing.T) { secondListCopy := make([]Validator, len(secondList)) copy(secondListCopy, secondList) - secondListAfterRemove := removeDupplicates(firstList, secondList) + secondListAfterRemove := removeDuplicates(firstList, secondList) assert.Equal(t, firstListCopy, firstList) assert.Equal(t, len(secondListCopy)-len(firstListCopy), len(secondListAfterRemove)) @@ -1099,10 +1125,12 @@ func TestNewHashValidatorsShuffler(t *testing.T) { t.Parallel() shufflerArgs := &NodesShufflerArgs{ - NodesShard: eligiblePerShard, - NodesMeta: eligiblePerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1119,10 +1147,10 @@ func TestRandHashShuffler_computeNewShardsNotChanging(t *testing.T) { shuffler, err := createHashShufflerInter() require.Nil(t, err) - eligible := generateValidatorMap(int(shuffler.nodesShard), currentNbShards) + eligible := generateValidatorMap(int(shuffler.currentChainParameters.ShardMinNumNodes), currentNbShards) nbShards := currentNbShards + 1 // account for meta - maxNodesNoSplit := (nbShards + 1) * (shuffler.nodesShard + shuffler.shardHysteresis) - nbWaitingPerShard := int(maxNodesNoSplit/nbShards - shuffler.nodesShard) + maxNodesNoSplit := (nbShards + 1) * (shuffler.currentChainParameters.ShardMinNumNodes + shuffler.shardHysteresis()) + nbWaitingPerShard := int(maxNodesNoSplit/nbShards - shuffler.currentChainParameters.ShardMinNumNodes) waiting := generateValidatorMap(nbWaitingPerShard, currentNbShards) newNodes := generateValidatorList(0) leavingUnstake := generateValidatorList(0) @@ -1142,10 +1170,10 @@ func TestRandHashShuffler_computeNewShardsWithSplit(t *testing.T) { shuffler, err := createHashShufflerInter() require.Nil(t, err) - eligible := generateValidatorMap(int(shuffler.nodesShard), currentNbShards) + eligible := generateValidatorMap(int(shuffler.currentChainParameters.ShardMinNumNodes), currentNbShards) nbShards := currentNbShards + 1 // account for meta - maxNodesNoSplit := (nbShards + 1) * (shuffler.nodesShard + shuffler.shardHysteresis) - nbWaitingPerShard := int(maxNodesNoSplit/nbShards-shuffler.nodesShard) + 1 + maxNodesNoSplit := (nbShards + 1) * (shuffler.currentChainParameters.ShardMinNumNodes + shuffler.shardHysteresis()) + nbWaitingPerShard := int(maxNodesNoSplit/nbShards-shuffler.currentChainParameters.ShardMinNumNodes) + 1 waiting := generateValidatorMap(nbWaitingPerShard, currentNbShards) newNodes := generateValidatorList(0) leavingUnstake := generateValidatorList(0) @@ -1165,7 +1193,7 @@ func TestRandHashShuffler_computeNewShardsWithMerge(t *testing.T) { shuffler, err := createHashShufflerInter() require.Nil(t, err) - eligible := generateValidatorMap(int(shuffler.nodesShard), currentNbShards) + eligible := generateValidatorMap(int(shuffler.currentChainParameters.ShardMinNumNodes), currentNbShards) nbWaitingPerShard := 0 waiting := generateValidatorMap(nbWaitingPerShard, currentNbShards) newNodes := generateValidatorList(0) @@ -1179,41 +1207,13 @@ func TestRandHashShuffler_computeNewShardsWithMerge(t *testing.T) { assert.Equal(t, currentNbShards-1, newNbShards) } -func TestRandHashShuffler_UpdateParams(t *testing.T) { - t.Parallel() - - shuffler, err := createHashShufflerInter() - require.Nil(t, err) - - shuffler2 := &randHashShuffler{ - nodesShard: 200, - nodesMeta: 200, - shardHysteresis: 0, - metaHysteresis: 0, - adaptivity: true, - shuffleBetweenShards: true, - validatorDistributor: &CrossShardValidatorDistributor{}, - availableNodesConfigs: nil, - enableEpochsHandler: &mock.EnableEpochsHandlerMock{}, - } - - shuffler.UpdateParams( - shuffler2.nodesShard, - shuffler2.nodesMeta, - 0, - shuffler2.adaptivity, - ) - - assert.Equal(t, shuffler2, shuffler) -} - func TestRandHashShuffler_UpdateNodeListsNoReSharding(t *testing.T) { t.Parallel() shuffler, err := createHashShufflerInter() require.Nil(t, err) - eligiblePerShard := int(shuffler.nodesShard) + eligiblePerShard := int(shuffler.currentChainParameters.ShardMinNumNodes) waitingPerShard := 30 nbShards := uint32(3) randomness := generateRandomByteArray(32) @@ -1253,10 +1253,12 @@ func TestRandHashShuffler_UpdateNodeListsWithUnstakeLeavingRemovesFromEligible(t eligibleMeta := 10 shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligibleMeta), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligibleMeta), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1321,10 +1323,12 @@ func testUpdateNodesAndCheckNumLeaving(t *testing.T, beforeFix bool) { } shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligibleMeta), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligibleMeta), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: []config.MaxNodesChangeConfig{ { @@ -1393,10 +1397,12 @@ func testUpdateNodeListsAndCheckWaitingList(t *testing.T, beforeFix bool) { } shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligibleMeta), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligibleMeta), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: []config.MaxNodesChangeConfig{ { @@ -1455,10 +1461,12 @@ func TestRandHashShuffler_UpdateNodeListsWithUnstakeLeavingRemovesFromWaiting(t eligibleMeta := 10 shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligibleMeta), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligibleMeta), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1498,10 +1506,12 @@ func TestRandHashShuffler_UpdateNodeListsWithNonExistentUnstakeLeavingDoesNotRem t.Parallel() shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(10), + numNodesMeta: uint32(10), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1509,7 +1519,7 @@ func TestRandHashShuffler_UpdateNodeListsWithNonExistentUnstakeLeavingDoesNotRem shuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - eligiblePerShard := int(shuffler.nodesShard) + eligiblePerShard := int(shuffler.currentChainParameters.ShardMinNumNodes) waitingPerShard := 2 nbShards := uint32(0) @@ -1550,10 +1560,12 @@ func TestRandHashShuffler_UpdateNodeListsWithRangeOnMaps(t *testing.T) { for _, shuffle := range shuffleBetweenShards { shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligiblePerShard), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffle, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2180,7 +2192,7 @@ func TestRandHashShuffler_UpdateNodeLists_WithAdditionalLeaving(t *testing.T) { } } -func TestRandHashShuffler_UpdateNodeLists_WithUnstakeAndAdditionalLeaving_NoDupplicates(t *testing.T) { +func TestRandHashShuffler_UpdateNodeLists_WithUnstakeAndAdditionalLeaving_NoDuplicates(t *testing.T) { t.Parallel() eligiblePerShard := 100 @@ -2255,7 +2267,7 @@ func TestRandHashShuffler_UpdateNodeLists_WithUnstakeAndAdditionalLeaving_NoDupp ) } } -func TestRandHashShuffler_UpdateNodeLists_WithAdditionalLeaving_WithDupplicates(t *testing.T) { +func TestRandHashShuffler_UpdateNodeLists_WithAdditionalLeaving_WithDuplicates(t *testing.T) { t.Parallel() eligiblePerShard := 100 @@ -2386,10 +2398,12 @@ func TestRandHashShuffler_UpdateNodeLists_All(t *testing.T) { unstakeLeavingList, additionalLeavingList := prepareListsFromMaps(unstakeLeaving, additionalLeaving) shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligiblePerShard), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligiblePerShard), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2490,10 +2504,12 @@ func TestRandHashShuffler_UpdateNodeLists_WithNewNodes_NoWaiting(t *testing.T) { } shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligiblePerShard), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligiblePerShard), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2551,10 +2567,12 @@ func TestRandHashShuffler_UpdateNodeLists_WithNewNodes_NilOrEmptyWaiting(t *test } shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(eligiblePerShard), - NodesMeta: uint32(eligiblePerShard), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(eligiblePerShard), + numNodesMeta: uint32(eligiblePerShard), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2682,10 +2700,12 @@ func TestRandHashShuffler_UpdateNodeLists_WithNewNodes_WithWaiting_WithLeaving(t } shufflerArgs := &NodesShufflerArgs{ - NodesShard: uint32(numEligiblePerShard), - NodesMeta: uint32(numEligiblePerShard), - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: uint32(numEligiblePerShard), + numNodesMeta: uint32(numEligiblePerShard), + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2734,7 +2754,7 @@ func verifyResultsIntraShardShuffling( initialNumWaiting := len(waiting) numToRemove := initialNumWaiting - additionalLeaving = removeDupplicates(unstakeLeaving, additionalLeaving) + additionalLeaving = removeDuplicates(unstakeLeaving, additionalLeaving) computedNewWaiting, removedFromWaiting := removeValidatorsFromList(waiting, unstakeLeaving, numToRemove) removedNodes = append(removedNodes, removedFromWaiting...) @@ -2910,10 +2930,12 @@ func TestRandHashShuffler_sortConfigs(t *testing.T) { require.NotEqual(t, orderedConfigs, shuffledConfigs) shufflerArgs := &NodesShufflerArgs{ - NodesShard: eligiblePerShard, - NodesMeta: eligiblePerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: shuffledConfigs, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -2929,10 +2951,12 @@ func TestRandHashShuffler_UpdateShufflerConfig(t *testing.T) { orderedConfigs := getDummyShufflerConfigs() shufflerArgs := &NodesShufflerArgs{ - NodesShard: eligiblePerShard, - NodesMeta: eligiblePerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: eligiblePerShard, + numNodesMeta: eligiblePerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: orderedConfigs, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, diff --git a/sharding/nodesCoordinator/indexHashedNodesCoordinatorLite_test.go b/sharding/nodesCoordinator/indexHashedNodesCoordinatorLite_test.go index 39d92d908da..d0cecde3c31 100644 --- a/sharding/nodesCoordinator/indexHashedNodesCoordinatorLite_test.go +++ b/sharding/nodesCoordinator/indexHashedNodesCoordinatorLite_test.go @@ -86,8 +86,12 @@ func TestIndexHashedNodesCoordinator_SetNodesConfigFromValidatorsInfo(t *testing arguments := createArguments() shufflerArgs := &NodesShufflerArgs{ - NodesShard: 3, - NodesMeta: 3, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 3, + numNodesMeta: 3, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, } nodeShuffler, _ := NewHashValidatorsShuffler(shufflerArgs) @@ -110,8 +114,10 @@ func TestIndexHashedNodesCoordinator_SetNodesConfigFromValidatorsInfoMultipleEpo arguments := createArguments() shufflerArgs := &NodesShufflerArgs{ - NodesShard: 3, - NodesMeta: 3, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 3, + numNodesMeta: 3, + }.build(), EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, } nodeShuffler, _ := NewHashValidatorsShuffler(shufflerArgs) diff --git a/sharding/nodesCoordinator/indexHashedNodesCoordinatorWithRater_test.go b/sharding/nodesCoordinator/indexHashedNodesCoordinatorWithRater_test.go index b86400d2369..03c906c429f 100644 --- a/sharding/nodesCoordinator/indexHashedNodesCoordinatorWithRater_test.go +++ b/sharding/nodesCoordinator/indexHashedNodesCoordinatorWithRater_test.go @@ -16,6 +16,7 @@ import ( "github.com/ElrondNetwork/elrond-go/common" "github.com/ElrondNetwork/elrond-go/sharding/mock" "github.com/ElrondNetwork/elrond-go/state" + "github.com/ElrondNetwork/elrond-go/testscommon/epochstartmock" "github.com/ElrondNetwork/elrond-go/testscommon/genericMocks" "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/ElrondNetwork/elrond-go/testscommon/nodeTypeProviderMock" @@ -64,10 +65,12 @@ func TestIndexHashedGroupSelectorWithRater_OkValShouldWork(t *testing.T) { eligibleMap := createDummyNodesMap(3, 1, "waiting") waitingMap := make(map[uint32][]Validator) shufflerArgs := &NodesShufflerArgs{ - NodesShard: 3, - NodesMeta: 3, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 3, + numNodesMeta: 3, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -75,7 +78,7 @@ func TestIndexHashedGroupSelectorWithRater_OkValShouldWork(t *testing.T) { nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -162,17 +165,19 @@ func BenchmarkIndexHashedGroupSelectorWithRater_ComputeValidatorsGroup63of400(b eligibleMap[core.MetachainShardId] = listMeta shufflerArgs := &NodesShufflerArgs{ - NodesShard: 400, - NodesMeta: 1, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 400, + numNodesMeta: 1, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, } nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(b, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -237,10 +242,12 @@ func Test_ComputeValidatorsGroup63of400(t *testing.T) { eligibleMap[0] = list eligibleMap[core.MetachainShardId] = listMeta shufflerArgs := &NodesShufflerArgs{ - NodesShard: shardSize, - NodesMeta: 1, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: shardSize, + numNodesMeta: 1, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -248,7 +255,7 @@ func Test_ComputeValidatorsGroup63of400(t *testing.T) { nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -310,10 +317,12 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldReturn eligibleMap[0] = list eligibleMap[core.MetachainShardId] = list sufflerArgs := &NodesShufflerArgs{ - NodesShard: 1, - NodesMeta: 1, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 1, + numNodesMeta: 1, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -321,7 +330,7 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldReturn nodeShuffler, err := NewHashValidatorsShuffler(sufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -364,10 +373,12 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldReturn eligibleMap[core.MetachainShardId] = list shufflerArgs := &NodesShufflerArgs{ - NodesShard: 1, - NodesMeta: 1, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 1, + numNodesMeta: 1, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -375,7 +386,7 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldReturn nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -428,10 +439,12 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldWork(t waitingMap := make(map[uint32][]Validator) shufflerArgs := &NodesShufflerArgs{ - NodesShard: 3, - NodesMeta: 3, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 3, + numNodesMeta: 3, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -439,7 +452,7 @@ func TestIndexHashedGroupSelectorWithRater_GetValidatorWithPublicKeyShouldWork(t nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() eligibleMap[core.MetachainShardId] = listMeta @@ -513,17 +526,19 @@ func TestIndexHashedGroupSelectorWithRater_GetAllEligibleValidatorsPublicKeys(t waitingMap := make(map[uint32][]Validator) shufflerArgs := &NodesShufflerArgs{ - NodesShard: 3, - NodesMeta: 3, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 3, + numNodesMeta: 3, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, } nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() eligibleMap[core.MetachainShardId] = listMeta @@ -823,10 +838,12 @@ func BenchmarkIndexHashedWithRaterGroupSelector_ComputeValidatorsGroup21of400(b eligibleMap[0] = list eligibleMap[core.MetachainShardId] = listMeta shufflerArgs := &NodesShufflerArgs{ - NodesShard: 400, - NodesMeta: 1, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 400, + numNodesMeta: 1, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -834,7 +851,7 @@ func BenchmarkIndexHashedWithRaterGroupSelector_ComputeValidatorsGroup21of400(b nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(b, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ diff --git a/sharding/nodesCoordinator/indexHashedNodesCoordinator_test.go b/sharding/nodesCoordinator/indexHashedNodesCoordinator_test.go index cb0483b51a4..5fd24a1b1dc 100644 --- a/sharding/nodesCoordinator/indexHashedNodesCoordinator_test.go +++ b/sharding/nodesCoordinator/indexHashedNodesCoordinator_test.go @@ -26,6 +26,7 @@ import ( "github.com/ElrondNetwork/elrond-go/sharding/mock" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/storage/cache" + "github.com/ElrondNetwork/elrond-go/testscommon/epochstartmock" "github.com/ElrondNetwork/elrond-go/testscommon/genericMocks" "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/ElrondNetwork/elrond-go/testscommon/nodeTypeProviderMock" @@ -86,17 +87,19 @@ func createArguments() ArgNodesCoordinator { eligibleMap := createDummyNodesMap(10, nbShards, "eligible") waitingMap := createDummyNodesMap(3, nbShards, "waiting") shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, } nodeShuffler, _ := NewHashValidatorsShuffler(shufflerArgs) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -247,10 +250,12 @@ func TestIndexHashedNodesCoordinator_OkValShouldWork(t *testing.T) { waitingMap := createDummyNodesMap(3, 3, "waiting") shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -258,7 +263,7 @@ func TestIndexHashedNodesCoordinator_OkValShouldWork(t *testing.T) { nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -307,10 +312,12 @@ func TestIndexHashedNodesCoordinator_NewCoordinatorTooFewNodesShouldErr(t *testi eligibleMap := createDummyNodesMap(5, 3, "eligible") waitingMap := createDummyNodesMap(3, 3, "waiting") shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -318,7 +325,7 @@ func TestIndexHashedNodesCoordinator_NewCoordinatorTooFewNodesShouldErr(t *testi nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -381,10 +388,12 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup1ValidatorShouldRetur nodesMap[0] = list nodesMap[core.MetachainShardId] = tmp[core.MetachainShardId] shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -392,7 +401,7 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup1ValidatorShouldRetur nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -427,10 +436,12 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup400of400For10locksNoM waitingMap := make(map[uint32][]Validator) eligibleMap := createDummyNodesMap(nodesPerShard, 1, "eligible") shufflerArgs := &NodesShufflerArgs{ - NodesShard: nodesPerShard, - NodesMeta: nodesPerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: nodesPerShard, + numNodesMeta: nodesPerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -438,7 +449,7 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup400of400For10locksNoM nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() getCounter := int32(0) @@ -502,10 +513,12 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup400of400For10BlocksMe waitingMap := make(map[uint32][]Validator) eligibleMap := createDummyNodesMap(nodesPerShard, 1, "eligible") shufflerArgs := &NodesShufflerArgs{ - NodesShard: nodesPerShard, - NodesMeta: nodesPerShard, - Hysteresis: 0, - Adaptivity: false, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: nodesPerShard, + numNodesMeta: nodesPerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: false, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -513,7 +526,7 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup400of400For10BlocksMe nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() getCounter := 0 @@ -601,10 +614,12 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup63of400TestEqualSameP eligibleMap := createDummyNodesMap(nodesPerShard, 1, "eligible") shufflerArgs := &NodesShufflerArgs{ - NodesShard: nodesPerShard, - NodesMeta: nodesPerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: nodesPerShard, + numNodesMeta: nodesPerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -612,7 +627,7 @@ func TestIndexHashedNodesCoordinator_ComputeValidatorsGroup63of400TestEqualSameP nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -664,10 +679,12 @@ func BenchmarkIndexHashedGroupSelector_ComputeValidatorsGroup21of400(b *testing. waitingMap := make(map[uint32][]Validator) eligibleMap := createDummyNodesMap(nodesPerShard, 1, "eligible") shufflerArgs := &NodesShufflerArgs{ - NodesShard: nodesPerShard, - NodesMeta: nodesPerShard, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: nodesPerShard, + numNodesMeta: nodesPerShard, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -675,7 +692,7 @@ func BenchmarkIndexHashedGroupSelector_ComputeValidatorsGroup21of400(b *testing. nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(b, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -737,10 +754,12 @@ func BenchmarkIndexHashedNodesCoordinator_CopyMaps(b *testing.B) { func runBenchmark(consensusGroupCache Cacher, consensusGroupSize int, nodesMap map[uint32][]Validator, b *testing.B) { waitingMap := make(map[uint32][]Validator) shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -748,7 +767,7 @@ func runBenchmark(consensusGroupCache Cacher, consensusGroupSize int, nodesMap m nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(b, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -787,10 +806,12 @@ func runBenchmark(consensusGroupCache Cacher, consensusGroupSize int, nodesMap m func computeMemoryRequirements(consensusGroupCache Cacher, consensusGroupSize int, nodesMap map[uint32][]Validator, b *testing.B) { waitingMap := make(map[uint32][]Validator) shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -798,7 +819,7 @@ func computeMemoryRequirements(consensusGroupCache Cacher, consensusGroupSize in nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(b, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -927,10 +948,12 @@ func TestIndexHashedNodesCoordinator_GetValidatorWithPublicKeyShouldWork(t *test eligibleMap[0] = listShard0 eligibleMap[1] = listShard1 shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -938,7 +961,7 @@ func TestIndexHashedNodesCoordinator_GetValidatorWithPublicKeyShouldWork(t *test nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -1010,10 +1033,12 @@ func TestIndexHashedGroupSelector_GetAllEligibleValidatorsPublicKeys(t *testing. eligibleMap[shardZeroId] = listShard0 eligibleMap[shardOneId] = listShard1 shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1021,7 +1046,7 @@ func TestIndexHashedGroupSelector_GetAllEligibleValidatorsPublicKeys(t *testing. nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ @@ -1085,10 +1110,12 @@ func TestIndexHashedGroupSelector_GetAllWaitingValidatorsPublicKeys(t *testing.T waitingMap[shardOneId] = listShard1 shufflerArgs := &NodesShufflerArgs{ - NodesShard: 10, - NodesMeta: 10, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 10, + numNodesMeta: 10, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1096,7 +1123,7 @@ func TestIndexHashedGroupSelector_GetAllWaitingValidatorsPublicKeys(t *testing.T nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() eligibleMap := make(map[uint32][]Validator) @@ -1449,10 +1476,12 @@ func TestIndexHashedNodesCoordinator_EpochStart_EligibleSortedAscendingByIndex(t eligibleMap[core.MetachainShardId] = list shufflerArgs := &NodesShufflerArgs{ - NodesShard: 2, - NodesMeta: 2, - Hysteresis: hysteresis, - Adaptivity: adaptivity, + ChainParametersHandler: testChainParametersCreator{ + numNodesShards: 2, + numNodesMeta: 2, + hysteresis: hysteresis, + adaptivity: adaptivity, + }.build(), ShuffleBetweenShards: shuffleBetweenShards, MaxNodesEnableConfig: nil, EnableEpochsHandler: &mock.EnableEpochsHandlerMock{}, @@ -1460,7 +1489,7 @@ func TestIndexHashedNodesCoordinator_EpochStart_EligibleSortedAscendingByIndex(t nodeShuffler, err := NewHashValidatorsShuffler(shufflerArgs) require.Nil(t, err) - epochStartSubscriber := &mock.EpochStartNotifierStub{} + epochStartSubscriber := &epochstartmock.EpochStartNotifierStub{} bootStorer := genericMocks.NewStorerMock() arguments := ArgNodesCoordinator{ diff --git a/sharding/nodesCoordinator/interface.go b/sharding/nodesCoordinator/interface.go index cbe5241bc84..61829320cbc 100644 --- a/sharding/nodesCoordinator/interface.go +++ b/sharding/nodesCoordinator/interface.go @@ -3,6 +3,7 @@ package nodesCoordinator import ( "github.com/ElrondNetwork/elrond-go-core/core" "github.com/ElrondNetwork/elrond-go-core/data" + "github.com/ElrondNetwork/elrond-go/config" "github.com/ElrondNetwork/elrond-go/epochStart" "github.com/ElrondNetwork/elrond-go/state" ) @@ -51,7 +52,6 @@ type PublicKeysSelector interface { // NodesShuffler provides shuffling functionality for nodes type NodesShuffler interface { - UpdateParams(numNodesShard uint32, numNodesMeta uint32, hysteresis float32, adaptivity bool) UpdateNodeLists(args ArgsUpdateNodes) (*ResUpdateNodes, error) IsInterfaceNil() bool } @@ -130,3 +130,11 @@ type EpochsConfigUpdateHandler interface { SetNodesConfigFromValidatorsInfo(epoch uint32, randomness []byte, validatorsInfo []*state.ShardValidatorInfo) error IsEpochInConfig(epoch uint32) bool } + +// ChainParametersHandler defines the actions that need to be done by a component that can handle chain parameters +type ChainParametersHandler interface { + CurrentChainParameters() config.ChainParametersByEpochConfig + AllChainParameters() []config.ChainParametersByEpochConfig + ChainParametersForEpoch(epoch uint32) (config.ChainParametersByEpochConfig, error) + IsInterfaceNil() bool +} diff --git a/sharding/mock/epochStartNotifierStub.go b/testscommon/epochstartmock/epochStartNotifierStub.go similarity index 98% rename from sharding/mock/epochStartNotifierStub.go rename to testscommon/epochstartmock/epochStartNotifierStub.go index 2ddd42f2bbb..9c9789447a1 100644 --- a/sharding/mock/epochStartNotifierStub.go +++ b/testscommon/epochstartmock/epochStartNotifierStub.go @@ -1,4 +1,4 @@ -package mock +package epochstartmock import ( "github.com/ElrondNetwork/elrond-go-core/data" diff --git a/testscommon/shardingMocks/shufflerMock.go b/testscommon/shardingMocks/shufflerMock.go index 30f581ae920..a5016a551da 100644 --- a/testscommon/shardingMocks/shufflerMock.go +++ b/testscommon/shardingMocks/shufflerMock.go @@ -8,16 +8,6 @@ import ( type NodeShufflerMock struct { } -// UpdateParams - -func (nsm *NodeShufflerMock) UpdateParams( - _ uint32, - _ uint32, - _ float32, - _ bool, -) { - -} - // UpdateNodeLists - func (nsm *NodeShufflerMock) UpdateNodeLists(args nodesCoordinator.ArgsUpdateNodes) (*nodesCoordinator.ResUpdateNodes, error) { return &nodesCoordinator.ResUpdateNodes{