Skip to content
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

Merged

Conversation

mariusmihaic
Copy link
Contributor

@mariusmihaic mariusmihaic commented Mar 7, 2022

Refactor

  • Created NodesCoordinatorRegistryHandler & EpochValidatorsHandler interfaces to be used instead of pointers to structs
  • Created EpochValidatorsWithAuction & 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 marshaller

New code

  • Added staking v4 flag to save and load nodes registry using new proto structs
  • In NodeShuffler, starting with staking v4:
  1. All nodes which which have been selected from staking v4 (based on their top up), will be distributed in the waiting list
  2. All shuffled out nodes will not be moved into waiting list anymore, but saved in the nodes coordinator registry. Further PR will follow with how to handle them

@mariusmihaic mariusmihaic changed the title En 11664 nodes coordinator staking v4 Nodes coordinator with staking v4 Mar 7, 2022
@mariusmihaic mariusmihaic changed the base branch from EN-11662-ignore-staking-queue to move-waiting-list-from-staking March 7, 2022 10:40
@mariusmihaic mariusmihaic self-assigned this Mar 9, 2022
@mariusmihaic mariusmihaic marked this pull request as ready for review March 11, 2022 14:11
@mariusmihaic mariusmihaic force-pushed the EN-11664-nodes-coordinator-staking-v4 branch from 40715e9 to 4fddf7b Compare March 16, 2022 08:54
…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
Base automatically changed from move-waiting-list-from-staking to feat/liquid-staking March 21, 2022 13:22
// 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)
Copy link
Contributor

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

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

enable epoch for

Copy link
Contributor Author

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() {
Copy link
Contributor

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"

Copy link
Contributor

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.

Copy link
Contributor Author

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.

sasurobert
sasurobert previously approved these changes Mar 22, 2022
@@ -160,27 +161,37 @@ func (bcf *bootstrapComponentsFactory) Create() (*bootstrapComponents, error) {

dataSyncerFactory := bootstrap.NewScheduledDataSyncerFactory()

nodesCoordinatorRegistryFactory, err := nodesCoordinator.NewNodesCoordinatorRegistryFactory(
Copy link
Contributor

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?

Copy link
Contributor Author

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 !

if err != nil {
return nil, err
}
bcf.coreComponents.EpochNotifier().RegisterNotifyHandler(nodesCoordinatorRegistryFactory)
Copy link
Contributor

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.

Copy link
Contributor Author

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 👍

@@ -173,27 +174,33 @@ func CreateNodesCoordinator(
return nil, err
}

nodesCoordinatorRegistryFactory, err := nodesCoordinator.NewNodesCoordinatorRegistryFactory(marshalizer, stakingV4EnableEpoch)
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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

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")
Copy link
Contributor

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

Copy link
Contributor Author

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)
Copy link
Contributor

@AdoAdoAdo AdoAdoAdo Mar 23, 2022

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.

Copy link
Contributor Author

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()
Copy link
Contributor

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?

Copy link
Contributor Author

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

sasurobert
sasurobert previously approved these changes Mar 24, 2022
@@ -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(
Copy link
Contributor

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?

Copy link
Contributor Author

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(
Copy link
Contributor

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

Copy link
Contributor Author

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(
Copy link
Contributor

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

Copy link
Contributor Author

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 !

@mariusmihaic mariusmihaic merged commit 8a122e8 into feat/liquid-staking Mar 24, 2022
@mariusmihaic mariusmihaic deleted the EN-11664-nodes-coordinator-staking-v4 branch March 24, 2022 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants