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

Ignore staking queue #3861

Merged
merged 23 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f9d87f9
FEAT: Add staking v4 flags in staking.go
mariusmihaic Feb 28, 2022
36c8648
FEAT: Add flag to systemSCs.go
mariusmihaic Feb 28, 2022
44677a9
FIX: Broken tests
mariusmihaic Feb 28, 2022
afc619a
Merge branch 'EN-11662-filter-new-nodes' into EN-11662-ignore-staking…
mariusmihaic Feb 28, 2022
fb072e3
FEAT: Add first test
mariusmihaic Mar 1, 2022
97d1d7d
Merge branch 'EN-11662-filter-new-nodes' into EN-11662-ignore-staking…
mariusmihaic Mar 1, 2022
5265146
FIX: Review findings
mariusmihaic Mar 1, 2022
3a8e998
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 2, 2022
30d48ca
FIX: Staking v4 test
mariusmihaic Mar 2, 2022
f6b3a6e
FEAT: Add flagStakingV4 tests in staking.go
mariusmihaic Mar 2, 2022
b10b28e
FEAT: Add extra safety flag check
mariusmihaic Mar 2, 2022
3a59057
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 3, 2022
30c635d
FIX: Review findings
mariusmihaic Mar 3, 2022
e8c552c
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 7, 2022
c6b85c7
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 9, 2022
d2c6e9c
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 9, 2022
4fcc03f
FIX: Broken test
mariusmihaic Mar 9, 2022
6c2ae11
Merge branch 'EN-11662-filter-new-nodes' into EN-116622-ignore-stakin…
mariusmihaic Mar 9, 2022
56b33f5
FIX: Broken tests
mariusmihaic Mar 9, 2022
e25a573
Merge branch 'feat/liquid-staking' into EN-116622-ignore-staking-queu…
mariusmihaic Mar 11, 2022
4d27010
FIX: Merge conflict
mariusmihaic Mar 11, 2022
779733d
FIX: Finding
mariusmihaic Mar 16, 2022
5fdc264
Merge branch 'feat/liquid-staking' into EN-116622-ignore-staking-queu…
mariusmihaic Mar 17, 2022
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
2 changes: 1 addition & 1 deletion epochStart/metachain/systemSCs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ func (s *systemSCProcessor) IsInterfaceNil() bool {

// EpochConfirmed is called whenever a new epoch is confirmed
func (s *systemSCProcessor) EpochConfirmed(epoch uint32, _ uint64) {
s.flagSwitchJailedWaiting.SetValue(epoch >= s.switchEnableEpoch)
s.flagSwitchJailedWaiting.SetValue(epoch >= s.switchEnableEpoch && epoch < s.stakingV4InitEnableEpoch)
log.Debug("systemSCProcessor: switch jail with waiting", "enabled", s.flagSwitchJailedWaiting.IsSet())

// only toggle on exact epoch. In future epochs the config should have already been synchronized from peers
Expand Down
3 changes: 3 additions & 0 deletions vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,6 @@ var ErrInvalidNodeLimitPercentage = errors.New("invalid node limit percentage")

// ErrNilNodesCoordinator signals that nil nodes coordinator was provided
var ErrNilNodesCoordinator = errors.New("nil nodes coordinator")

// ErrWaitingListDisabled signals that waiting list has been disabled, since staking v4 is active
var ErrWaitingListDisabled = errors.New("waiting list is disabled since staking v4 activation")
80 changes: 69 additions & 11 deletions vm/systemSmartContracts/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ type stakingSC struct {
flagCorrectLastUnjailed atomic.Flag
flagCorrectFirstQueued atomic.Flag
flagCorrectJailedNotUnstakedEmptyQueue atomic.Flag
flagStakingV4 atomic.Flag
correctJailedNotUnstakedEmptyQueueEpoch uint32
correctFirstQueuedEpoch uint32
correctLastUnjailedEpoch uint32
stakingV2Epoch uint32
stakingV4Epoch uint32
walletAddressLen int
mutExecution sync.RWMutex
minNodePrice *big.Int
Expand Down Expand Up @@ -138,13 +140,15 @@ func NewStakingSmartContract(
validatorToDelegationEnableEpoch: args.EpochConfig.EnableEpochs.ValidatorToDelegationEnableEpoch,
correctFirstQueuedEpoch: args.EpochConfig.EnableEpochs.CorrectFirstQueuedEpoch,
correctJailedNotUnstakedEmptyQueueEpoch: args.EpochConfig.EnableEpochs.CorrectJailedNotUnstakedEmptyQueueEpoch,
stakingV4Epoch: args.EpochConfig.EnableEpochs.StakingV4EnableEpoch,
}
log.Debug("staking: enable epoch for stake", "epoch", reg.enableStakingEpoch)
log.Debug("staking: enable epoch for staking v2", "epoch", reg.stakingV2Epoch)
log.Debug("staking: enable epoch for correct last unjailed", "epoch", reg.correctLastUnjailedEpoch)
log.Debug("staking: enable epoch for validator to delegation", "epoch", reg.validatorToDelegationEnableEpoch)
log.Debug("staking: enable epoch for correct first queued", "epoch", reg.correctFirstQueuedEpoch)
log.Debug("staking: enable epoch for correct jailed not unstaked with empty queue", "epoch", reg.correctJailedNotUnstakedEmptyQueueEpoch)
log.Debug("staking: enable epoch for staking v4", "epoch", reg.stakingV4Epoch)

var conversionOk bool
reg.stakeValue, conversionOk = big.NewInt(0).SetString(args.StakingSCConfig.GenesisNodePrice, conversionBase)
Expand Down Expand Up @@ -258,6 +262,10 @@ func (s *stakingSC) numSpareNodes() int64 {
}

func (s *stakingSC) canStake() bool {
if s.flagStakingV4.IsSet() {
return true
}

stakeConfig := s.getConfig()
return stakeConfig.StakedNodes < stakeConfig.MaxNumNodes
}
Expand Down Expand Up @@ -536,10 +544,12 @@ func (s *stakingSC) processStake(blsKey []byte, registrationData *StakedDataV2_0
return nil
}

err := s.removeFromWaitingList(blsKey)
if err != nil {
s.eei.AddReturnMessage("error while removing from waiting")
return err
if !s.flagStakingV4.IsSet() {
err := s.removeFromWaitingList(blsKey)
if err != nil {
s.eei.AddReturnMessage("error while removing from waiting")
return err
}
}
s.addToStakedNodes(1)
s.activeStakingFor(registrationData)
Expand Down Expand Up @@ -588,6 +598,7 @@ func (s *stakingSC) unStakeAtEndOfEpoch(args *vmcommon.ContractCallInput) vmcomm
if registrationData.Staked {
s.removeFromStakedNodes()
}

if registrationData.Waiting {
err = s.removeFromWaitingList(args.Arguments[0])
if err != nil {
Expand Down Expand Up @@ -644,6 +655,11 @@ func (s *stakingSC) unStake(args *vmcommon.ContractCallInput) vmcommon.ReturnCod
}

if !registrationData.Staked {
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.ExecutionFailed
}

registrationData.Waiting = false
err = s.removeFromWaitingList(args.Arguments[0])
if err != nil {
Expand All @@ -659,12 +675,14 @@ func (s *stakingSC) unStake(args *vmcommon.ContractCallInput) vmcommon.ReturnCod
return vmcommon.Ok
}

addOneFromQueue := !s.flagCorrectLastUnjailed.IsSet() || s.canStakeIfOneRemoved()
if addOneFromQueue {
_, err = s.moveFirstFromWaitingToStaked()
if err != nil {
s.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
if !s.flagStakingV4.IsSet() {
addOneFromQueue := !s.flagCorrectLastUnjailed.IsSet() || s.canStakeIfOneRemoved()
Copy link
Contributor

Choose a reason for hiding this comment

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

this file will need one big cleanup - like stakingV2 separate file and stakingV4 separate. Add a task in JIRA and we will see later.

if addOneFromQueue {
_, err = s.moveFirstFromWaitingToStaked()
if err != nil {
s.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
}
}
}

Expand Down Expand Up @@ -1147,6 +1165,10 @@ func createWaitingListKey(blsKey []byte) []byte {
}

func (s *stakingSC) switchJailedWithWaiting(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}
if !bytes.Equal(args.CallerAddr, s.endOfEpochAccessAddr) {
s.eei.AddReturnMessage("switchJailedWithWaiting function not allowed to be called by address " + string(args.CallerAddr))
return vmcommon.UserError
Expand Down Expand Up @@ -1289,6 +1311,12 @@ func (s *stakingSC) isNodeJailedOrWithBadRating(registrationData *StakedDataV2_0
}

func (s *stakingSC) getWaitingListIndex(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
s.eei.Finish([]byte{0})

return vmcommon.Ok
Copy link
Contributor

Choose a reason for hiding this comment

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

if you have return message than return vmcommon.UserError, and do not add to Finish anything. as it is ignored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for heads up, refactored it

}
if !bytes.Equal(args.CallerAddr, s.stakeAccessAddr) {
s.eei.AddReturnMessage("this is only a view function")
return vmcommon.UserError
Expand Down Expand Up @@ -1353,6 +1381,13 @@ func (s *stakingSC) getWaitingListIndex(args *vmcommon.ContractCallInput) vmcomm
}

func (s *stakingSC) getWaitingListSize(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
s.eei.Finish([]byte{0})
Copy link
Contributor

Choose a reason for hiding this comment

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

if you have return message than return vmcommon.UserError, and do not add to Finish anything. as it is ignored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for heads up, refactored it


return vmcommon.Ok
}

if args.CallValue.Cmp(zero) != 0 {
s.eei.AddReturnMessage(vm.TransactionValueMustBeZero)
return vmcommon.UserError
Expand Down Expand Up @@ -1581,13 +1616,13 @@ func (s *stakingSC) getTotalNumberOfRegisteredNodes(args *vmcommon.ContractCallI
return vmcommon.UserError
}

stakeConfig := s.getConfig()
waitingListHead, err := s.getWaitingListHead()
if err != nil {
s.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
}

stakeConfig := s.getConfig()
totalRegistered := stakeConfig.StakedNodes + stakeConfig.JailedNodes + int64(waitingListHead.Length)
s.eei.Finish(big.NewInt(totalRegistered).Bytes())
return vmcommon.Ok
Expand All @@ -1598,6 +1633,10 @@ func (s *stakingSC) resetLastUnJailedFromQueue(args *vmcommon.ContractCallInput)
// backward compatibility
return vmcommon.UserError
}
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}
if !bytes.Equal(args.CallerAddr, s.endOfEpochAccessAddr) {
s.eei.AddReturnMessage("stake nodes from waiting list can be called by endOfEpochAccess address only")
return vmcommon.UserError
Expand Down Expand Up @@ -1682,6 +1721,10 @@ func (s *stakingSC) stakeNodesFromQueue(args *vmcommon.ContractCallInput) vmcomm
s.eei.AddReturnMessage("invalid method to call")
return vmcommon.UserError
}
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}
if !bytes.Equal(args.CallerAddr, s.endOfEpochAccessAddr) {
s.eei.AddReturnMessage("stake nodes from waiting list can be called by endOfEpochAccess address only")
return vmcommon.UserError
Expand Down Expand Up @@ -1754,6 +1797,10 @@ func (s *stakingSC) cleanAdditionalQueue(args *vmcommon.ContractCallInput) vmcom
s.eei.AddReturnMessage("invalid method to call")
return vmcommon.UserError
}
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}
if !bytes.Equal(args.CallerAddr, s.endOfEpochAccessAddr) {
s.eei.AddReturnMessage("stake nodes from waiting list can be called by endOfEpochAccess address only")
return vmcommon.UserError
Expand Down Expand Up @@ -1964,6 +2011,10 @@ func (s *stakingSC) fixWaitingListQueueSize(args *vmcommon.ContractCallInput) vm
s.eei.AddReturnMessage("invalid method to call")
return vmcommon.UserError
}
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}

if args.CallValue.Cmp(zero) != 0 {
s.eei.AddReturnMessage(vm.TransactionValueMustBeZero)
Expand Down Expand Up @@ -2035,6 +2086,10 @@ func (s *stakingSC) addMissingNodeToQueue(args *vmcommon.ContractCallInput) vmco
s.eei.AddReturnMessage("invalid method to call")
return vmcommon.UserError
}
if s.flagStakingV4.IsSet() {
s.eei.AddReturnMessage(vm.ErrWaitingListDisabled.Error())
return vmcommon.UserError
}
if args.CallValue.Cmp(zero) != 0 {
s.eei.AddReturnMessage(vm.TransactionValueMustBeZero)
return vmcommon.UserError
Expand Down Expand Up @@ -2114,6 +2169,9 @@ func (s *stakingSC) EpochConfirmed(epoch uint32, _ uint64) {

s.flagCorrectJailedNotUnstakedEmptyQueue.SetValue(epoch >= s.correctJailedNotUnstakedEmptyQueueEpoch)
log.Debug("stakingSC: correct jailed not unstaked with empty queue", "enabled", s.flagCorrectJailedNotUnstakedEmptyQueue.IsSet())

s.flagStakingV4.SetValue(epoch >= s.stakingV4Epoch)
log.Debug("stakingSC: staking v4", "enabled", s.flagStakingV4.IsSet())
}

// CanUseContract returns true if contract can be used
Expand Down