Skip to content

Commit

Permalink
ensure burnRatio plus systemRewardRatio not larger than 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Dec 15, 2023
1 parent 1ed3964 commit a061441
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
mapping(address =>uint256) public currentValidatorSetMap;
uint256 public numOfJailed;

uint256 public constant BURN_RATIO_SCALE = 10000;
uint256 public constant BLOCK_FEES_RATIO_SCALE = 10000;
address public constant BURN_ADDRESS = 0x000000000000000000000000000000000000dEaD;
uint256 public constant INIT_BURN_RATIO = 1000;
uint256 public burnRatio;
Expand All @@ -82,7 +82,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

// BEP-126 Fast Finality
uint256 public constant INIT_SYSTEM_REWARD_RATIO = 625; // 625/10000 is 1/16
uint256 public constant SYSTEM_REWARD_RATIO_SCALE = 10000;
uint256 public constant MAX_SYSTEM_REWARD_BALANCE = 100 ether;

uint256 public systemRewardRatio;
Expand Down Expand Up @@ -322,14 +321,18 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
isSystemRewardIncluded = true;
}

uint256 toSystemReward = value.mul(systemRewardRatio).div(SYSTEM_REWARD_RATIO_SCALE);
if (toSystemReward > 0) {
address(uint160(SYSTEM_REWARD_ADDR)).transfer(toSystemReward);
emit systemTransfer(toSystemReward);
if (value > 0 && systemRewardRatio > 0) {
uint256 toSystemReward = msg.value.mul(systemRewardRatio).div(BLOCK_FEES_RATIO_SCALE);
if (toSystemReward > 0) {
address(uint160(SYSTEM_REWARD_ADDR)).transfer(toSystemReward);
emit systemTransfer(toSystemReward);

value = value.sub(toSystemReward);
}
}

if (value > 0 && burnRatio > 0) {
uint256 toBurn = value.mul(burnRatio).div(BURN_RATIO_SCALE);
uint256 toBurn = msg.value.mul(burnRatio).div(BLOCK_FEES_RATIO_SCALE);
if (toBurn > 0) {
address(uint160(BURN_ADDRESS)).transfer(toBurn);
emit feeBurned(toBurn);
Expand All @@ -338,7 +341,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}
}

value = value.sub(toSystemReward);
if (index>0) {
Validator storage validator = currentValidatorSet[index-1];
if (validator.jailed) {
Expand Down Expand Up @@ -833,7 +835,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
} else if (Memory.compareStrings(key, "burnRatio")) {
require(value.length == 32, "length of burnRatio mismatch");
uint256 newBurnRatio = BytesToTypes.bytesToUint256(32, value);
require(newBurnRatio <= BURN_RATIO_SCALE, "the burnRatio must be no greater than 10000");
require(newBurnRatio + systemRewardRatio <= BLOCK_FEES_RATIO_SCALE, "the burnRatio plus systemRewardRatio must be no greater than 10000");
burnRatio = newBurnRatio;
} else if (Memory.compareStrings(key, "maxNumOfMaintaining")) {
require(value.length == 32, "length of maxNumOfMaintaining mismatch");
Expand Down Expand Up @@ -870,7 +872,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
} else if (Memory.compareStrings(key, "systemRewardRatio")) {
require(value.length == 32, "length of systemRewardRatio mismatch");
uint256 newSystemRewardRatio = BytesToTypes.bytesToUint256(32, value);
require(newSystemRewardRatio >= 1 && newSystemRewardRatio <= SYSTEM_REWARD_RATIO_SCALE, "the systemRewardRatio must be no greater than 10000");
require(newSystemRewardRatio + burnRatio <= BLOCK_FEES_RATIO_SCALE, "the systemRewardRatio plus burnRatio must be no greater than 10000");
systemRewardRatio = newSystemRewardRatio;
} else {
require(false, "unknown param");
Expand Down

0 comments on commit a061441

Please sign in to comment.