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

FIX: Low waiting list edge case in StakingV4Step2 #5111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 137 additions & 0 deletions integrationTests/vm/staking/stakingV4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,140 @@ func TestStakingV4_DifferentEdgeCasesWithNotEnoughNodesInWaitingShouldSendShuffl
epoch++
}
}

func TestStakingV4_NewlyStakedNodesInStakingV4Step2ShouldBeSentToWaitingIfListIsTooLow(t *testing.T) {
pubKeys := generateAddresses(0, 20)

owner1 := "owner1"
owner1Stats := &OwnerStats{
EligibleBlsKeys: map[uint32][][]byte{
core.MetachainShardId: pubKeys[:4],
0: pubKeys[4:8],
},
WaitingBlsKeys: map[uint32][][]byte{
core.MetachainShardId: pubKeys[8:9],
0: pubKeys[9:10],
},
TotalStake: big.NewInt(20 * nodePrice),
}

cfg := &InitialNodesConfig{
MetaConsensusGroupSize: 2,
ShardConsensusGroupSize: 2,
MinNumberOfEligibleShardNodes: 4,
MinNumberOfEligibleMetaNodes: 4,
NumOfShards: 1,
Owners: map[string]*OwnerStats{
owner1: owner1Stats,
},
MaxNodesChangeConfig: []config.MaxNodesChangeConfig{
{
EpochEnable: 0,
MaxNumNodes: 20,
NodesToShufflePerShard: 1,
},
{
EpochEnable: stakingV4Step3EnableEpoch,
MaxNumNodes: 18,
NodesToShufflePerShard: 1,
},
},
}
node := NewTestMetaProcessorWithCustomNodes(cfg)
node.EpochStartTrigger.SetRoundsPerEpoch(4)

// 1. Check initial config is correct
expectedNodesNum := &configNum{
eligible: map[uint32]int{
core.MetachainShardId: 4,
0: 4,
},
waiting: map[uint32]int{
core.MetachainShardId: 1,
0: 1,
},
}
currNodesConfig := node.NodesConfig
checkConfig(t, expectedNodesNum, currNodesConfig)

// Epoch = 0, before staking v4, owner2 stakes 2 nodes
// - maxNumNodes = 20
// - activeNumNodes = 10
// Newly staked nodes should be sent to new list
owner2Nodes := pubKeys[12:14]
node.ProcessStake(t, map[string]*NodesRegisterData{
"owner2": {
BLSKeys: owner2Nodes,
TotalStake: big.NewInt(2 * nodePrice),
},
})
currNodesConfig = node.NodesConfig
expectedNodesNum.new = 2
checkConfig(t, expectedNodesNum, currNodesConfig)
requireSameSliceDifferentOrder(t, currNodesConfig.new, owner2Nodes)

// Epoch = 1, staking v4 step 1
// - maxNumNodes = 20
// - activeNumNodes = 12
// Owner2's new nodes should have been sent to waiting
node.Process(t, 5)
currNodesConfig = node.NodesConfig
expectedNodesNum.new = 0
expectedNodesNum.waiting[0]++
expectedNodesNum.waiting[core.MetachainShardId]++
checkConfig(t, expectedNodesNum, currNodesConfig)
requireSliceContainsNumOfElements(t, getAllPubKeys(currNodesConfig.waiting), owner2Nodes, 2)

// Epoch = 1, before staking v4, owner3 stakes 2 nodes
// - maxNumNodes = 20
// - activeNumNodes = 12
// Newly staked nodes should be sent to auction list
owner3Nodes := pubKeys[15:17]
node.ProcessStake(t, map[string]*NodesRegisterData{
"owner3": {
BLSKeys: owner3Nodes,
TotalStake: big.NewInt(2 * nodePrice),
},
})
currNodesConfig = node.NodesConfig
expectedNodesNum.auction = 2
checkConfig(t, expectedNodesNum, currNodesConfig)
requireSameSliceDifferentOrder(t, currNodesConfig.auction, owner3Nodes)

// Epoch = 2, staking v4 step 2
// - maxNumNodes = 20
// - activeNumNodes = 14
// Owner3's auction nodes should have been sent to waiting
node.Process(t, 5)
currNodesConfig = node.NodesConfig
expectedNodesNum.auction = 0
expectedNodesNum.waiting[0]++
expectedNodesNum.waiting[core.MetachainShardId]++
checkConfig(t, expectedNodesNum, currNodesConfig)
requireSliceContainsNumOfElements(t, getAllPubKeys(currNodesConfig.waiting), owner3Nodes, 2)

// During epochs 2-6, we will have:
// - activeNodes = 14
// - maxNumNodes = 18-20
// Since activeNodes < maxNumNodes, shuffled out nodes will always be sent directly to waiting list, instead of auction
epoch := uint32(2)
require.Equal(t, epoch, node.EpochStartTrigger.Epoch())

numOfShuffledOut := 2
numRemainingEligible := 6
numOfUnselectedNodesFromAuction := 0
numOfSelectedNodesFromAuction := 0

prevNodesConfig := currNodesConfig
for epoch < 6 {
node.Process(t, 5)

currNodesConfig = node.NodesConfig
checkConfig(t, expectedNodesNum, currNodesConfig)
checkShuffledOutNodes(t, currNodesConfig, prevNodesConfig, numOfShuffledOut, numRemainingEligible)
checkStakingV4EpochChangeFlow(t, currNodesConfig, prevNodesConfig, numOfShuffledOut, numOfUnselectedNodesFromAuction, numOfSelectedNodesFromAuction)

prevNodesConfig = currNodesConfig
epoch++
}
}
9 changes: 5 additions & 4 deletions sharding/nodesCoordinator/hashValidatorShuffler.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ func shuffleNodes(arg shuffleNodesArg) (*ResUpdateNodes, error) {
flagStakingV4Step2: arg.flagStakingV4Step2,
}

if arg.flagStakingV4Step3 {
lowWaitingList := shouldDistributeShuffledToWaitingInStakingV4(shuffledNodesCfg)
if arg.flagStakingV4Step3 || lowWaitingList {
log.Debug("distributing selected nodes from auction to waiting",
"num auction nodes", len(arg.auction), "num waiting nodes", shuffledNodesCfg.numNewWaiting)

Expand All @@ -325,7 +326,7 @@ func shuffleNodes(arg shuffleNodesArg) (*ResUpdateNodes, error) {
}
}

if shouldDistributeShuffledToWaiting(shuffledNodesCfg) {
if !arg.flagStakingV4Step2 || lowWaitingList {
Copy link
Contributor

Choose a reason for hiding this comment

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

if lowWaitingList is true, it will distributeValidators 2 times? Once on line 318 and once on line 329?

log.Debug("distributing shuffled out nodes to waiting",
"num shuffled nodes", shuffledNodesCfg.numShuffled, "num waiting nodes", shuffledNodesCfg.numNewWaiting)

Expand Down Expand Up @@ -599,9 +600,9 @@ func checkAndDistributeNewNodes(
return nil
}

func shouldDistributeShuffledToWaiting(shuffledNodesCfg *shuffledNodesConfig) bool {
func shouldDistributeShuffledToWaitingInStakingV4(shuffledNodesCfg *shuffledNodesConfig) bool {
if !shuffledNodesCfg.flagStakingV4Step2 {
return true
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

some unitTest should have failed here. Can you please add an unit test?

}

totalNewWaiting := shuffledNodesCfg.numNewWaiting + shuffledNodesCfg.numSelectedAuction
Expand Down