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: bug that validatorSet order was changed incorrectly while _felon… #9

Merged
merged 2 commits into from
Mar 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
bool isMaintaining;

// reserve for future use
uint256[40] slots;
uint256[20] slots;
}

/*********************** cross chain package **************************/
Expand Down Expand Up @@ -713,7 +713,11 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
bool isFelony;

// 1. validators exit maintenance
for (uint i = 0; i < currentValidatorSet.length; i++) {
uint256 i;
// caution: it must loop from the endIndex to startIndex in currentValidatorSet
// because the validators order in currentValidatorSet may be changed by _felony(validator)
for (uint index = currentValidatorSet.length; index > 0; index--) {
i = index - 1; // the actual index
if (!validatorExtraSet[i].isMaintaining) {
continue;
}
Expand All @@ -739,7 +743,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

// 2. get unjailed validators from validatorSet
unjailedValidatorSet = new Validator[](_validatorSet.length - numOfFelony);
uint256 i = 0;
i = 0;
for (uint index = 0; index < _validatorSet.length; index++) {
if (!_validatorSet[index].jailed) {
unjailedValidatorSet[i] = _validatorSet[index];
Expand All @@ -759,8 +763,10 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

function _exitMaintenance(address validator, uint index) private returns (bool isFelony){
uint256 workingValidatorCount = getValidators().length;
if (workingValidatorCount > numOfCabinets) {
workingValidatorCount = numOfCabinets;

uint256 _numOfCabinets = numOfCabinets > 0 ? numOfCabinets : INIT_NUM_OF_CABINETS;
if (workingValidatorCount > _numOfCabinets) {
workingValidatorCount = _numOfCabinets;
}
if (maintainSlashScale == 0 || workingValidatorCount == 0 || numOfMaintaining == 0) {
// should not happen, still protect
Expand Down
16 changes: 11 additions & 5 deletions contracts/BSCValidatorSet.template
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
bool isMaintaining;

// reserve for future use
uint256[40] slots;
uint256[20] slots;
}

/*********************** cross chain package **************************/
Expand Down Expand Up @@ -713,7 +713,11 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
bool isFelony;

// 1. validators exit maintenance
for (uint i = 0; i < currentValidatorSet.length; i++) {
uint256 i;
// caution: it must loop from the endIndex to startIndex in currentValidatorSet
// because the validators order in currentValidatorSet may be changed by _felony(validator)
for (uint index = currentValidatorSet.length; index > 0; index--) {
i = index - 1; // the actual index
if (!validatorExtraSet[i].isMaintaining) {
continue;
}
Expand All @@ -739,7 +743,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

// 2. get unjailed validators from validatorSet
unjailedValidatorSet = new Validator[](_validatorSet.length - numOfFelony);
uint256 i = 0;
i = 0;
for (uint index = 0; index < _validatorSet.length; index++) {
if (!_validatorSet[index].jailed) {
unjailedValidatorSet[i] = _validatorSet[index];
Expand All @@ -759,8 +763,10 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

function _exitMaintenance(address validator, uint index) private returns (bool isFelony){
uint256 workingValidatorCount = getValidators().length;
if (workingValidatorCount > numOfCabinets) {
workingValidatorCount = numOfCabinets;

uint256 _numOfCabinets = numOfCabinets > 0 ? numOfCabinets : INIT_NUM_OF_CABINETS;
if (workingValidatorCount > _numOfCabinets) {
workingValidatorCount = _numOfCabinets;
}
if (maintainSlashScale == 0 || workingValidatorCount == 0 || numOfMaintaining == 0) {
// should not happen, still protect
Expand Down
93 changes: 47 additions & 46 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,56 @@ import '@nomiclabs/hardhat-waffle';
* @type import('hardhat/config').HardhatUserConfig
*/
export default {
solidity: {
compilers: [
{
version: "0.6.4",
settings: {
optimizer: {
enabled: true,
runs: 200
}
solidity: {
compilers: [
{
version: "0.6.4",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
]
},
networks: {
hardhat: {
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
count: 100
},
allowUnlimitedContractSize: true,
},
development: {
url: "http://127.0.0.1:8545", // Localhost (default: none)
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
count: 100
}
},
'bsc': {
url: "https://bsc-dataseed1.ninicoin.io",
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
}
}
},
watcher: {
compilation: {
tasks: ["compile"],
files: ["./contracts"],
verbose: true,
}
},
]
},
networks: {
hardhat: {
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
count: 100
}
},
development: {
url: "http://127.0.0.1:8545", // Localhost (default: none)
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
count: 100
}
mocha: {
timeout: 2000000
},
'bsc': {
url: "https://bsc-dataseed1.ninicoin.io",
accounts: {
mnemonic: "clock radar mass judge dismiss just intact mind resemble fringe diary casino",
}
paths: {
sources: "./contracts",
tests: "./test/test-maintenance",
cache: "./cache",
artifacts: "./artifacts"
},
},
watcher: {
compilation: {
tasks: ["compile"],
files: ["./contracts"],
verbose: true,
}
},
mocha: {
timeout: 2000000
},
paths: {
sources: "./contracts",
tests: "./test/test-maintenance",
cache: "./cache",
artifacts: "./artifacts"
},
};