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
Nodes coordinator with staking v4 #3883
Nodes coordinator with staking v4 #3883
Conversation
…rdinator-staking-v4
…rdinator-staking-v4
40715e9
to
4fddf7b
Compare
…rdinator-staking-v4 # Conflicts: # epochStart/bootstrap/baseStorageHandler.go # epochStart/bootstrap/fromLocalStorage.go # epochStart/bootstrap/interface.go # epochStart/bootstrap/process.go # epochStart/bootstrap/syncValidatorStatus.go # factory/bootstrapParameters.go # factory/interface.go # factory/shardingFactory.go # sharding/interface.go # sharding/nodesCoordinator/indexHashedNodesCoordinator.go # sharding/nodesCoordinator/indexHashedNodesCoordinatorRegistry.go # testscommon/bootstrapMocks/bootstrapParamsStub.go # testscommon/shardingMocks/nodesCoordinatorStub.go
sharding/nodesCoordinator/common.go
Outdated
// NodesCoordinatorRegistry with a json marshaller; while the new version(from staking v4) uses NodesCoordinatorRegistryWithAuction | ||
// with proto marshaller | ||
func CreateNodesCoordinatorRegistry(marshaller marshal.Marshalizer, buff []byte) (NodesCoordinatorRegistryHandler, error) { | ||
registry, err := createOldRegistry(buff) |
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.
do not unmarshall 2 times - push the epoch here - make a component which is NodesCoordinatorRegistryFactory - which knows depending on the epoch which registry has to be created
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.
Very good suggestion. I've done this similar to how CreateHeader
is done, which creates either Header
/HeaderV2
, but your suggestion is way better.
Therefore, I've done a pretty big refactored and created a NodesCoordinatorRegistryFactory
component
if err != nil { | ||
log.Warn("distributeValidators shuffledOut failed", "error", err) | ||
if arg.flagStakingV4 { | ||
err = distributeValidators(newWaiting, arg.auction, arg.randomness, false) |
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.
it is hard to understand the difference between the 2 versions. Rename this function to distributeValidatorsUsingAuction - or something more clear.
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.
I do not think that creating an extra function here would bring any benefit, since the only thing it would do is to call distributeValidators
-> which already does its job.
I think the way to go here is to add an explanatory comment
if err != nil { | ||
log.Warn("distributeValidators auction list failed", "error", err) | ||
} | ||
} else { |
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.
I do not see why there is log.Warn everywhere - this errors are critical - so they should return error. Add a task to refactor this.
Plus I do not see the point of shuffleNodes - why is it like a function which gets a lot of arguments as input - I think it would be better that this is a component, with defined interface. Add a refactor task for this as well.
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.
RIght, I was also thinking the same when I saw this, but decided to be consistent with the current implementation.
Added 2 tasks for this refactor !
chanStopNode: arguments.ChanStopNode, | ||
nodeTypeProvider: arguments.NodeTypeProvider, | ||
isFullArchive: arguments.IsFullArchive, | ||
} | ||
log.Debug("indexHashedNodesCoordinator: enable epoch for waiting waiting list", "epoch", ihnc.waitingListFixEnableEpoch) | ||
log.Debug("indexHashedNodesCoordinator: staking v4", "epoch", ihnc.stakingV4EnableEpoch) |
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.
enable epoch for
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.
Right, refactored it
@@ -739,10 +751,15 @@ func (ihnc *indexHashedNodesCoordinator) computeNodesConfigFromList( | |||
log.Debug("inactive validator", "pk", validatorInfo.PublicKey) | |||
case string(common.JailedList): | |||
log.Debug("jailed validator", "pk", validatorInfo.PublicKey) | |||
case string(common.SelectedFromAuctionList): | |||
if ihnc.flagStakingV4.IsSet() { |
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.
add a log.Error - in case of "else"
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.
later after like refactoring - returning error here should be good as well.
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.
Decided to return error right here, actually.
@@ -160,27 +161,37 @@ func (bcf *bootstrapComponentsFactory) Create() (*bootstrapComponents, error) { | |||
|
|||
dataSyncerFactory := bootstrap.NewScheduledDataSyncerFactory() | |||
|
|||
nodesCoordinatorRegistryFactory, err := nodesCoordinator.NewNodesCoordinatorRegistryFactory( |
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.
can you add the epoch notifier as an argument to the constructor and have the registration of the nodes coordinator registry factory to the epoch notifier done on the constructor as well?
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.
Refactored the code such that NodesCoordinatorRegistryFactory
takes EpochNotifier
as input param in constructor.
Good suggestion, thanks !
factory/bootstrapComponents.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
bcf.coreComponents.EpochNotifier().RegisterNotifyHandler(nodesCoordinatorRegistryFactory) |
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.
this should be removed if the nodes coordinator registry factory registers itself to the epoch notifier.
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.
Yes, this registration has been removed after refactor 👍
factory/shardingFactory.go
Outdated
@@ -173,27 +174,33 @@ func CreateNodesCoordinator( | |||
return nil, err | |||
} | |||
|
|||
nodesCoordinatorRegistryFactory, err := nodesCoordinator.NewNodesCoordinatorRegistryFactory(marshalizer, stakingV4EnableEpoch) |
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.
this is not registered to the epoch notifier so it will not be notified on epoch changes.
Also there is no need to keep creating different instances of this factory, you should instead create it once and have it passed to the other components requiring it.
Please check all instantiations and pass it instead as an argument.
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.
You're right, good suggestion !
Refactored all code, such that BootStrapCompHolder
factory will create only one instance and provide it for other subcomps that need it
@@ -520,24 +520,26 @@ func createNodes( | |||
bootStorer := integrationTests.CreateMemUnit() | |||
consensusCache, _ := lrucache.NewCache(10000) | |||
|
|||
ncf, _ := nodesCoordinator.NewNodesCoordinatorRegistryFactory(&testscommon.MarshalizerMock{}, integrationTests.StakingV4Epoch) |
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.
would not work properly as it is not registered to the epoch notifier.
Will be fixed if epoch notifier is passed instead to the constructor, see my previous comments.
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.
Right, however this is used for tests
sharding/nodesCoordinator/errors.go
Outdated
var ErrNilNodesCoordinatorRegistryFactory = errors.New("nil nodes coordinator registry factory has been given") | ||
|
||
// ErrReceivedAuctionValidatorsBeforeStakingV4 signals that auction nodes have been received from peer mini blocks before enabling staking v4 | ||
var ErrReceivedAuctionValidatorsBeforeStakingV4 = errors.New("should no have received selected nodes from auction in peer mini blocks, since staking v4 is not enabled yet") |
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.
typo should no have received -> should not have received
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.
Right, fixed it
ihnc.flagStakingV4.SetValue(epoch >= ihnc.stakingV4EnableEpoch) | ||
log.Debug("indexHashedNodesCoordinator: staking v4", "enabled", ihnc.flagStakingV4.IsSet()) | ||
|
||
ihnc.nodesCoordinatorRegistryFactory.EpochConfirmed(epoch, 0) |
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.
we should not need to manually call this here.
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.
Right, this was just a "hack", but was not supposed to stay there. Removed it
require.Equal(t, nc.shardIDAsObserver, computedShardId) | ||
require.False(t, isValidator) | ||
|
||
nc.flagStakingV4.SetReturningPrevious() |
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.
why not SetValue(true) as you are not checking the returned value anyway?
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.
SetReturningPrevious
was doing the same thing, but I guess it's better to use SetValue
. Refactored as suggested
@@ -49,6 +50,12 @@ func TestProcessComponents_Close_ShouldWork(t *testing.T) { | |||
require.Nil(t, err) | |||
nodesShufflerOut, err := mainFactory.CreateNodesShuffleOut(managedCoreComponents.GenesisNodesSetup(), configs.GeneralConfig.EpochStartConfig, managedCoreComponents.ChanStopNodeProcess()) | |||
require.Nil(t, err) | |||
nodesCoordinatorRegistryFactory, err := nodesCoord.NewNodesCoordinatorRegistryFactory( |
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.
could you use the nodesCoordinatorRegistryFactory from managedBootstrapComponents instead?
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.
Very good suggestion, applied it !
@@ -49,6 +50,12 @@ func TestStatusComponents_Create_Close_ShouldWork(t *testing.T) { | |||
require.Nil(t, err) | |||
nodesShufflerOut, err := mainFactory.CreateNodesShuffleOut(managedCoreComponents.GenesisNodesSetup(), configs.GeneralConfig.EpochStartConfig, managedCoreComponents.ChanStopNodeProcess()) | |||
require.Nil(t, err) | |||
nodesCoordinatorRegistryFactory, err := nodesCoord.NewNodesCoordinatorRegistryFactory( |
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.
please use the factory from managedBootstrapComponents
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.
Very good suggestion, applied it !
@@ -48,6 +49,12 @@ func TestConsensusComponents_Close_ShouldWork(t *testing.T) { | |||
require.Nil(t, err) | |||
nodesShufflerOut, err := mainFactory.CreateNodesShuffleOut(managedCoreComponents.GenesisNodesSetup(), configs.GeneralConfig.EpochStartConfig, managedCoreComponents.ChanStopNodeProcess()) | |||
require.Nil(t, err) | |||
nodesCoordinatorRegistryFactory, err := nodesCoord.NewNodesCoordinatorRegistryFactory( |
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.
please use the factory from managedBootstrapComponents instead
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.
Very good suggestion, applied it !
Refactor
NodesCoordinatorRegistryHandler
&EpochValidatorsHandler
interfaces to be used instead of pointers to structsEpochValidatorsWithAuction
&NodesCoordinatorRegistryWithAuction
proto structs which implement the interfaces above and will be used starting staking v4. To be noted that old code used simple structs and json marshallerNew code
NodeShuffler
, starting with staking v4: