Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions factory/core/coreComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions integrationTests/nodesCoordinatorFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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{
Expand Down
15 changes: 11 additions & 4 deletions integrationTests/testProcessorNodeWithMultisigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{},
Expand Down
36 changes: 23 additions & 13 deletions sharding/chainParametersHolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

yup, that is the behavior right now. I do not see a good reason why we should change this in the future.

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
Expand All @@ -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()

Expand Down
41 changes: 21 additions & 20 deletions sharding/chainParametersHolder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/sharding/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +17,7 @@ func TestNewChainParametersHolder(t *testing.T) {

getDummyArgs := func() ArgsChainParametersHolder {
return ArgsChainParametersHolder{
EpochNotifier: &epochNotifier.EpochNotifierStub{},
EpochStartEventNotifier: &mock.EpochStartNotifierStub{},
ChainParameters: []config.ChainParametersByEpochConfig{
{
EnableEpoch: 0,
Expand All @@ -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) {
Expand Down Expand Up @@ -165,8 +166,8 @@ func TestChainParametersHolder_ChainParametersForEpoch(t *testing.T) {
}

paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{
ChainParameters: params,
EpochNotifier: &epochNotifier.EpochNotifierStub{},
ChainParameters: params,
EpochStartEventNotifier: &mock.EpochStartNotifierStub{},
})

res, _ := paramsHolder.ChainParametersForEpoch(0)
Expand Down Expand Up @@ -210,8 +211,8 @@ func TestChainParametersHolder_ChainParametersForEpoch(t *testing.T) {
}

paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{
ChainParameters: params,
EpochNotifier: &epochNotifier.EpochNotifierStub{},
ChainParameters: params,
EpochStartEventNotifier: &mock.EpochStartNotifierStub{},
})

for i := 0; i < 200; i++ {
Expand Down Expand Up @@ -251,20 +252,20 @@ func TestChainParametersHolder_CurrentChainParameters(t *testing.T) {
}

paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{
ChainParameters: params,
EpochNotifier: &epochNotifier.EpochNotifierStub{},
ChainParameters: params,
EpochStartEventNotifier: &mock.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)
}

Expand All @@ -289,8 +290,8 @@ func TestChainParametersHolder_AllChainParameters(t *testing.T) {
}

paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{
ChainParameters: params,
EpochNotifier: &epochNotifier.EpochNotifierStub{},
ChainParameters: params,
EpochStartEventNotifier: &mock.EpochStartNotifierStub{},
})

returnedAllChainsParameters := paramsHolder.AllChainParameters()
Expand All @@ -314,8 +315,8 @@ func TestChainParametersHolder_ConcurrentOperations(t *testing.T) {
}

paramsHolder, _ := NewChainParametersHolder(ArgsChainParametersHolder{
ChainParameters: chainParams,
EpochNotifier: &epochNotifier.EpochNotifierStub{},
ChainParameters: chainParams,
EpochStartEventNotifier: &mock.EpochStartNotifierStub{},
})

numOperations := 500
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions sharding/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 5 additions & 7 deletions sharding/interface.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions sharding/nodesCoordinator/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading