Skip to content

Commit

Permalink
chore: add view function and update annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Dec 20, 2023
1 parent e077181 commit 2c9f7c6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
12 changes: 6 additions & 6 deletions abi/bscvalidatorset.abi
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
},
{
"type": "function",
"name": "BURN_ADDRESS",
"name": "BLOCK_FEES_RATIO_SCALE",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address",
"internalType": "address"
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "BLOCK_FEES_RATIO_SCALE",
"name": "BURN_ADDRESS",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
Expand Down
34 changes: 34 additions & 0 deletions abi/stakehub.abi
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,40 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getValidators",
"inputs": [
{
"name": "offset",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "limit",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "operatorAddrs",
"type": "address[]",
"internalType": "address[]"
},
{
"name": "creditAddrs",
"type": "address[]",
"internalType": "address[]"
},
{
"name": "totalLength",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "initialize",
Expand Down
46 changes: 39 additions & 7 deletions contracts/BC_fusion/StakeHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ contract StakeHub is System, Initializable {
function initialize() external initializer onlyCoinbase onlyZeroGasPrice {
transferGasLimit = 5000;
minSelfDelegationBNB = 2_000 ether;
minDelegationBNBChange = 1 ether;
minDelegationBNBChange = 1 ether - 1; // minus 1 to be more user-friendly when precision loss happens
maxElectedValidators = 29;
unbondPeriod = 7 days;
redelegateFeeRate = 2;
Expand Down Expand Up @@ -314,6 +314,7 @@ contract StakeHub is System, Initializable {
voteToOperator[voteAddress] = operatorAddress;

emit ValidatorCreated(consensusAddress, operatorAddress, creditContract, voteAddress);
emit Delegated(operatorAddress, operatorAddress, delegation, delegation);

IGovToken(GOV_TOKEN_ADDR).sync(creditContract, operatorAddress);
}
Expand Down Expand Up @@ -780,7 +781,7 @@ contract StakeHub is System, Initializable {

/*----------------- view functions -----------------*/
/**
* @return is the system paused
* @return whether the system is paused
*/
function isPaused() external view returns (bool) {
return _paused;
Expand All @@ -802,9 +803,39 @@ contract StakeHub is System, Initializable {
return IStakeCredit(_validators[operatorAddress].creditContract).totalPooledBNBRecord(index);
}

/**
* @notice pagination query all validators' operator address and credit contract address
* @return operatorAddrs operator addresses
* @return creditAddrs credit contract addresses
* @return totalLength total number of validators
*/
function getValidators(
uint256 offset,
uint256 limit
) external view returns (address[] memory operatorAddrs, address[] memory creditAddrs, uint256 totalLength) {
totalLength = _validatorSet.length();
if (offset >= totalLength) {
return (operatorAddrs, creditAddrs, totalLength);
}

limit = limit == 0 ? totalLength : limit;
uint256 count = (totalLength - offset) > limit ? limit : (totalLength - offset);
operatorAddrs = new address[](count);
creditAddrs = new address[](count);
for (uint256 i; i < count; ++i) {
operatorAddrs[i] = _validatorSet.at(offset + i);
creditAddrs[i] = _validators[operatorAddrs[i]].creditContract;
}
}

/**
* @notice get the basic info of a validator
* including consensus address, credit contract, created time, vote address, jailed and jailUntil
* @return consensusAddress the consensus address of the validator
* @return creditContract the credit contract address of the validator
* @return createdTime the creation time of the validator
* @return voteAddress the vote address of the validator
* @return jailed whether the validator is jailed
* @return jailUntil the jail time of the validator
*/
function getValidatorBasicInfo(address operatorAddress)
external
Expand Down Expand Up @@ -853,11 +884,12 @@ contract StakeHub is System, Initializable {
}

/**
* @dev this function will be invoked by Parlia consensus engine.
* @dev this function will be used by Parlia consensus engine.
* @notice get the election info of a validator
* including consensus address, voting power and vote address.
* The voting power will be 0 if the validator is jailed.
* This function is for the consensus engine.
* @return consensusAddrs the consensus addresses of the validators
* @return votingPowers the voting powers of the validators. The voting power will be 0 if the validator is jailed.
* @return voteAddrs the vote addresses of the validators
* @return totalLength the total number of validators
*/
function getValidatorElectionInfo(
uint256 offset,
Expand Down
2 changes: 2 additions & 0 deletions test/StakeHub.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ contract StakeHubTest is Deployer {
stakeHub.delegate{ value: bnbAmount }(validator, false);
uint256 shares = IStakeCredit(credit).balanceOf(delegator);
assertEq(shares, bnbAmount);
uint256 pooledBNB = IStakeCredit(credit).getPooledBNBByShares(shares);
assertEq(pooledBNB, bnbAmount);

vm.stopPrank();
}
Expand Down
2 changes: 1 addition & 1 deletion test/utils/interface/IBSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ interface BSCValidatorSet {
receive() external payable;

function BIND_CHANNELID() external view returns (uint8);
function BURN_ADDRESS() external view returns (address);
function BLOCK_FEES_RATIO_SCALE() external view returns (uint256);
function BURN_ADDRESS() external view returns (address);
function CODE_OK() external view returns (uint32);
function CROSS_CHAIN_CONTRACT_ADDR() external view returns (address);
function CROSS_STAKE_CHANNELID() external view returns (uint8);
Expand Down
4 changes: 4 additions & 0 deletions test/utils/interface/IStakeHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ interface StakeHub {
);
function getValidatorRewardRecord(address operatorAddress, uint256 index) external view returns (uint256);
function getValidatorTotalPooledBNBRecord(address operatorAddress, uint256 index) external view returns (uint256);
function getValidators(uint256 offset, uint256 limit)
external
view
returns (address[] memory operatorAddrs, address[] memory creditAddrs, uint256 totalLength);
function initialize() external;
function isPaused() external view returns (bool);
function maliciousVoteSlash(bytes memory voteAddress) external;
Expand Down

0 comments on commit 2c9f7c6

Please sign in to comment.