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

Power in decentralization #3447

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Changes from 1 commit
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
Next Next commit
limit total stake value
sasurobert committed Sep 15, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 035479fc065ba85e1922086a1f0aa36fd7ab9c13
3 changes: 3 additions & 0 deletions cmd/node/config/enableEpochs.toml
Original file line number Diff line number Diff line change
@@ -108,6 +108,9 @@
# BuiltInFunctionOnMetaEnableEpoch represents the epoch when built in function processing on metachain is enabled
BuiltInFunctionOnMetaEnableEpoch = 5

# StakeLimitsEnableEpoch represents the epoch when stake limits on validators are enabled
StakeLimitsEnableEpoch = 5

# MaxNodesChangeEnableEpoch holds configuration for changing the maximum number of nodes and the enabling epoch
MaxNodesChangeEnableEpoch = [
{ EpochEnable = 0, MaxNumNodes = 36, NodesToShufflePerShard = 4 },
1 change: 1 addition & 0 deletions cmd/node/config/systemSmartContractsConfig.toml
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
MaxNumberOfNodesForStake = 36
UnJailValue = "2500000000000000000" #0.1% of genesis node price
ActivateBLSPubKeyMessageVerification = false
LimitPercentage = 1.0

[ESDTSystemSCConfig]
BaseIssuingCost = "5000000000000000000" #5 eGLD
1 change: 1 addition & 0 deletions config/epochConfig.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ type EnableEpochs struct {
GlobalMintBurnDisableEpoch uint32
ESDTTransferRoleEnableEpoch uint32
BuiltInFunctionOnMetaEnableEpoch uint32
StakeLimitsEnableEpoch uint32
}

// GasScheduleByEpochs represents a gas schedule toml entry that will be applied from the provided epoch
1 change: 1 addition & 0 deletions config/systemSmartContractsConfig.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ type StakingSystemSCConfig struct {
BleedPercentagePerRound float64
MaxNumberOfNodesForStake uint64
ActivateBLSPubKeyMessageVerification bool
LimitPercentage float64
}

// ESDTSystemSCConfig defines a set of constant to initialize the esdt system smart contract
1 change: 1 addition & 0 deletions node/nodeRunner.go
Original file line number Diff line number Diff line change
@@ -146,6 +146,7 @@ func printEnableEpochs(configs *config.Configs) {
log.Debug(readEpochFor("contract global mint and burn"), "epoch", enableEpochs.GlobalMintBurnDisableEpoch)
log.Debug(readEpochFor("contract transfer role"), "epoch", enableEpochs.ESDTTransferRoleEnableEpoch)
log.Debug(readEpochFor("built in functions on metachain"), "epoch", enableEpochs.BuiltInFunctionOnMetaEnableEpoch)
log.Debug(readEpochFor("limit validators"), "epoch", enableEpochs.StakeLimitsEnableEpoch)

gasSchedule := configs.EpochConfig.GasSchedule

34 changes: 34 additions & 0 deletions vm/systemSmartContracts/validator.go
Original file line number Diff line number Diff line change
@@ -61,7 +61,12 @@ type validatorSC struct {
flagValidatorToDelegation atomic.Flag
enableUnbondTokensV2Epoch uint32
flagUnbondTokensV2 atomic.Flag
stakeLimitsEnableEpoch uint32
flagStakeLimits atomic.Flag
shardCoordinator sharding.Coordinator
limitPercentage float64
totalStakeLimit *big.Int
totalNodeLimit uint32
}

// ArgsValidatorSmartContract is the arguments structure to create a new ValidatorSmartContract
@@ -175,12 +180,17 @@ func NewValidatorSmartContract(
enableUnbondTokensV2Epoch: args.EpochConfig.EnableEpochs.UnbondTokensV2EnableEpoch,
validatorToDelegationEnableEpoch: args.EpochConfig.EnableEpochs.ValidatorToDelegationEnableEpoch,
shardCoordinator: args.ShardCoordinator,
stakeLimitsEnableEpoch: args.EpochConfig.EnableEpochs.StakeLimitsEnableEpoch,
limitPercentage: args.StakingSCConfig.LimitPercentage,
}
log.Debug("validator: enable epoch for staking v2", "epoch", reg.stakingV2Epoch)
log.Debug("validator: enable epoch for stake", "epoch", reg.enableStakingEpoch)
log.Debug("validator: enable epoch for double key protection", "epoch", reg.enableDoubleKeyEpoch)
log.Debug("validator: enable epoch for unbond tokens v2", "epoch", reg.enableUnbondTokensV2Epoch)
log.Debug("validator: enable epoch for validator to delegation", "epoch", reg.validatorToDelegationEnableEpoch)
log.Debug("validator: enable epoch for stake limits", "epoch", reg.stakeLimitsEnableEpoch)

reg.totalStakeLimit = core.GetIntTrimmedPercentageOfValue(args.GenesisTotalSupply, reg.limitPercentage)

args.EpochNotifier.RegisterNotifyHandler(reg)

@@ -909,6 +919,22 @@ func (v *validatorSC) checkAllGivenKeysAreUnStaked(registrationData *ValidatorDa
return mapBlsKeys, nil
}

func (v *validatorSC) isStakeTooHigh(registrationData *ValidatorDataV2) bool {
if !v.flagStakeLimits.IsSet() {
return false
}

return registrationData.TotalStakeValue.Cmp(v.totalStakeLimit) > 0
}

func (v *validatorSC) isStakedNodesNumberTooHigh(registrationData *ValidatorDataV2) bool {
if !v.flagStakeLimits.IsSet() {
return false
}

return false
}

func (v *validatorSC) stake(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
err := v.eei.UseGas(v.gasCost.MetaChainSystemSCsCost.Stake)
if err != nil {
@@ -942,6 +968,11 @@ func (v *validatorSC) stake(args *vmcommon.ContractCallInput) vmcommon.ReturnCod
return vmcommon.UserError
}

if v.isStakeTooHigh(registrationData) {
v.eei.AddReturnMessage("total stake limit reached")
return vmcommon.UserError
}

lenArgs := len(args.Arguments)
if lenArgs == 0 {
return v.updateStakeValue(registrationData, args.CallerAddr)
@@ -2136,6 +2167,9 @@ func (v *validatorSC) EpochConfirmed(epoch uint32, _ uint64) {

v.flagUnbondTokensV2.Toggle(epoch >= v.enableUnbondTokensV2Epoch)
log.Debug("validatorSC: unbond tokens v2", "enabled", v.flagUnbondTokensV2.IsSet())

v.flagStakeLimits.Toggle(epoch >= v.stakeLimitsEnableEpoch)
log.Debug("validatorSC: stake limits", "enabled", v.flagStakeLimits.IsSet())
}

// CanUseContract returns true if contract can be used