Skip to content

Commit

Permalink
fix: add check in _forceMaintainingValidatorsExit (#500)
Browse files Browse the repository at this point in the history
* fix: add check in `_forceMaintainingValidatorsExit`

* add `latestConsensusAddress`
  • Loading branch information
pythonberg1997 authored Feb 18, 2024
1 parent 44e886b commit 5b4125b
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 148 deletions.
72 changes: 57 additions & 15 deletions abi/stakehub.abi
Original file line number Diff line number Diff line change
Expand Up @@ -458,26 +458,11 @@
}
],
"outputs": [
{
"name": "consensusAddress",
"type": "address",
"internalType": "address"
},
{
"name": "creditContract",
"type": "address",
"internalType": "address"
},
{
"name": "createdTime",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "voteAddress",
"type": "bytes",
"internalType": "bytes"
},
{
"name": "jailed",
"type": "bool",
Expand Down Expand Up @@ -527,6 +512,44 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidatorConsensusAddress",
"inputs": [
{
"name": "operatorAddress",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "consensusAddress",
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidatorCreditContract",
"inputs": [
{
"name": "operatorAddress",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "creditContract",
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidatorDescription",
Expand Down Expand Up @@ -655,6 +678,25 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidatorVoteAddress",
"inputs": [
{
"name": "operatorAddress",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "voteAddress",
"type": "bytes",
"internalType": "bytes"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidators",
Expand Down
50 changes: 36 additions & 14 deletions contracts/BC_fusion/StakeHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -872,35 +872,57 @@ contract StakeHub is System, Initializable, Protectable {
}

/**
* @notice get the basic info of a validator
* @notice get the consensus address of a validator
*
* @param operatorAddress the operator address of the validator
*
* @return consensusAddress the consensus address of the validator
*/
function getValidatorConsensusAddress(address operatorAddress) external view returns (address consensusAddress) {
Validator memory valInfo = _validators[operatorAddress];
consensusAddress = valInfo.consensusAddress;
}

/**
* @notice get the credit contract address of a validator
*
* @param operatorAddress the operator address of the validator
*
* @return creditContract the credit contract address of the validator
* @return createdTime the creation time of the validator
*/
function getValidatorCreditContract(address operatorAddress) external view returns (address creditContract) {
Validator memory valInfo = _validators[operatorAddress];
creditContract = valInfo.creditContract;
}

/**
* @notice get the vote address of a validator
*
* @param operatorAddress the operator address of the validator
*
* @return voteAddress the vote address of the validator
*/
function getValidatorVoteAddress(address operatorAddress) external view returns (bytes memory voteAddress) {
Validator memory valInfo = _validators[operatorAddress];
voteAddress = valInfo.voteAddress;
}

/**
* @notice get the basic info of a validator
*
* @param operatorAddress the operator address of the validator
*
* @return createdTime the creation time of the validator
* @return jailed whether the validator is jailed
* @return jailUntil the jail time of the validator
*/
function getValidatorBasicInfo(address operatorAddress)
external
view
validatorExist(operatorAddress)
returns (
address consensusAddress,
address creditContract,
uint256 createdTime,
bytes memory voteAddress,
bool jailed,
uint256 jailUntil
)
returns (uint256 createdTime, bool jailed, uint256 jailUntil)
{
Validator memory valInfo = _validators[operatorAddress];
consensusAddress = valInfo.consensusAddress;
creditContract = valInfo.creditContract;
createdTime = valInfo.createdTime;
voteAddress = valInfo.voteAddress;
jailed = valInfo.jailed;
jailUntil = valInfo.jailUntil;
}
Expand Down
9 changes: 8 additions & 1 deletion contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1144,9 +1144,16 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
continue;
}

// get the latest consensus address
address latestConsensusAddress;
address operatorAddress = IStakeHub(STAKE_HUB_ADDR).consensusToOperator(validator);
if (operatorAddress != address(0)) {
latestConsensusAddress = IStakeHub(STAKE_HUB_ADDR).getValidatorConsensusAddress(operatorAddress);
}

// record the jailed validator in validatorSet
for (uint k; k<_validatorSet.length; ++k) {
if (_validatorSet[k].consensusAddress == validator) {
if (_validatorSet[k].consensusAddress == validator || _validatorSet[k].consensusAddress == latestConsensusAddress) {
_validatorSet[k].jailed = true;
break;
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/interface/IStakeHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ interface IStakeHub {
function doubleSignSlash(address validator) external;
function voteToOperator(bytes calldata voteAddress) external view returns (address);
function consensusToOperator(address validator) external view returns (address);
function getValidatorConsensusAddress(address validator) external view returns (address);
function getValidatorCreditContract(address validator) external view returns (address);
function getValidatorVoteAddress(address validator) external view returns (bytes memory);
function maxElectedValidators() external view returns (uint256);
function distributeReward(address validator) external payable;
}
2 changes: 1 addition & 1 deletion test/Governor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ contract GovernorTest is Deployer {
consensusAddress, blsPubKey, blsProof, commission, description
);

(, credit,,,,) = stakeHub.getValidatorBasicInfo(operatorAddress);
credit = stakeHub.getValidatorCreditContract(operatorAddress);
}

function _encodeValidatorSetUpdatePack(
Expand Down
28 changes: 16 additions & 12 deletions test/StakeHub.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ contract StakeHubTest is Deployer {
function testCreateValidator() public {
// create validator success
(address validator,) = _createValidator(2000 ether);
(address consensusAddress,,, bytes memory voteAddress,,) = stakeHub.getValidatorBasicInfo(validator);
address consensusAddress = stakeHub.getValidatorConsensusAddress(validator);
bytes memory voteAddress = stakeHub.getValidatorVoteAddress(validator);

address operatorAddress = _getNextUserAddress();
vm.startPrank(operatorAddress);
Expand Down Expand Up @@ -104,7 +105,7 @@ contract StakeHubTest is Deployer {
vm.expectEmit(true, true, false, true, address(stakeHub));
emit ConsensusAddressEdited(validator, newConsensusAddress);
stakeHub.editConsensusAddress(newConsensusAddress);
(address realConsensusAddr,,,,,) = stakeHub.getValidatorBasicInfo(validator);
address realConsensusAddr = stakeHub.getValidatorConsensusAddress(validator);
assertEq(realConsensusAddr, newConsensusAddress);

// edit commission rate
Expand Down Expand Up @@ -139,7 +140,7 @@ contract StakeHubTest is Deployer {
vm.expectEmit(true, false, false, true, address(stakeHub));
emit VoteAddressEdited(validator, newVoteAddress);
stakeHub.editVoteAddress(newVoteAddress, blsProof);
(,,, bytes memory realVoteAddr,,) = stakeHub.getValidatorBasicInfo(validator);
bytes memory realVoteAddr = stakeHub.getValidatorVoteAddress(validator);
assertEq(realVoteAddr, newVoteAddress);

vm.stopPrank();
Expand Down Expand Up @@ -297,7 +298,7 @@ contract StakeHubTest is Deployer {

// 2. distribute reward
uint256 reward = 100 ether;
(address consensusAddress,,,,,) = stakeHub.getValidatorBasicInfo(validator);
address consensusAddress = stakeHub.getValidatorConsensusAddress(validator);
vm.expectEmit(true, true, false, true, address(stakeHub));
emit RewardDistributed(validator, reward);
vm.deal(VALIDATOR_CONTRACT_ADDR, VALIDATOR_CONTRACT_ADDR.balance + reward);
Expand Down Expand Up @@ -347,7 +348,7 @@ contract StakeHubTest is Deployer {
vm.prank(delegator);
stakeHub.delegate{ value: 100 ether }(validator, false);

(address consensusAddress,,,,,) = stakeHub.getValidatorBasicInfo(validator);
address consensusAddress = stakeHub.getValidatorConsensusAddress(validator);
vm.deal(VALIDATOR_CONTRACT_ADDR, VALIDATOR_CONTRACT_ADDR.balance + reward);
vm.prank(VALIDATOR_CONTRACT_ADDR);
stakeHub.distributeReward{ value: reward }(consensusAddress);
Expand Down Expand Up @@ -375,15 +376,15 @@ contract StakeHubTest is Deployer {
assertApproxEqAbs(preDelegatorBnbAmount, curDelegatorBnbAmount, 1); // there may be 1 delta due to the precision

// unjail
(,,,, bool jailed,) = stakeHub.getValidatorBasicInfo(validator);
(, bool jailed,) = stakeHub.getValidatorBasicInfo(validator);
assertEq(jailed, true);
vm.expectRevert();
stakeHub.unjail(validator);
vm.warp(block.timestamp + slashTime + 1);
vm.expectEmit(true, false, false, true, address(stakeHub));
emit ValidatorUnjailed(validator);
stakeHub.unjail(validator);
(,,,, jailed,) = stakeHub.getValidatorBasicInfo(validator);
(, jailed,) = stakeHub.getValidatorBasicInfo(validator);
assertEq(jailed, false);

vm.stopPrank();
Expand All @@ -400,7 +401,7 @@ contract StakeHubTest is Deployer {
vm.prank(delegator);
stakeHub.delegate{ value: 100 ether }(validator, false);

(address consensusAddress,,,,,) = stakeHub.getValidatorBasicInfo(validator);
address consensusAddress = stakeHub.getValidatorConsensusAddress(validator);
vm.deal(VALIDATOR_CONTRACT_ADDR, VALIDATOR_CONTRACT_ADDR.balance + reward);
vm.prank(VALIDATOR_CONTRACT_ADDR);
stakeHub.distributeReward{ value: reward }(consensusAddress);
Expand Down Expand Up @@ -432,7 +433,8 @@ contract StakeHubTest is Deployer {
vm.prank(delegator);
stakeHub.delegate{ value: 100 ether }(validator, false);

(address consensusAddress,,, bytes memory voteAddr,,) = stakeHub.getValidatorBasicInfo(validator);
address consensusAddress = stakeHub.getValidatorConsensusAddress(validator);
bytes memory voteAddr = stakeHub.getValidatorVoteAddress(validator);
vm.deal(VALIDATOR_CONTRACT_ADDR, VALIDATOR_CONTRACT_ADDR.balance + reward);
vm.prank(VALIDATOR_CONTRACT_ADDR);
stakeHub.distributeReward{ value: reward }(consensusAddress);
Expand Down Expand Up @@ -473,7 +475,8 @@ contract StakeHubTest is Deployer {
for (uint256 i; i < length; ++i) {
votingPower = (2000 + uint64(i) * 2 + 1) * 1e8;
(operatorAddress,) = _createValidator(uint256(votingPower) * 1e10);
(consensusAddress,,, voteAddress,,) = stakeHub.getValidatorBasicInfo(operatorAddress);
consensusAddress = stakeHub.getValidatorConsensusAddress(operatorAddress);
voteAddress = stakeHub.getValidatorVoteAddress(operatorAddress);
newConsensusAddrs[length - i - 1] = consensusAddress;
newVotingPower[length - i - 1] = votingPower;
newVoteAddrs[length - i - 1] = voteAddress;
Expand Down Expand Up @@ -522,7 +525,8 @@ contract StakeHubTest is Deployer {
for (uint256 i; i < length; ++i) {
votingPower = (2000 + uint64(i) * 2 + 1) * 1e8;
(operatorAddress,) = _createValidator(uint256(votingPower) * 1e10);
(consensusAddress,,, voteAddress,,) = stakeHub.getValidatorBasicInfo(operatorAddress);
consensusAddress = stakeHub.getValidatorConsensusAddress(operatorAddress);
voteAddress = stakeHub.getValidatorVoteAddress(operatorAddress);
newConsensusAddrs[length - i - 1] = consensusAddress;
newVotingPower[length - i - 1] = votingPower;
newVoteAddrs[length - i - 1] = voteAddress;
Expand Down Expand Up @@ -611,7 +615,7 @@ contract StakeHubTest is Deployer {
consensusAddress, blsPubKey, blsProof, commission, description
);

(, credit,,,,) = stakeHub.getValidatorBasicInfo(operatorAddress);
credit = stakeHub.getValidatorCreditContract(operatorAddress);
}

function _encodeValidatorSetUpdatePack(
Expand Down
Loading

0 comments on commit 5b4125b

Please sign in to comment.