Skip to content

Commit

Permalink
feat: deprecate transferOut in TokenHub (#466)
Browse files Browse the repository at this point in the history
* feat: deprecate `transferOut` in `TokenHub`

* fix review comments
  • Loading branch information
pythonberg1997 authored Jan 15, 2024
1 parent 8e5e491 commit 0c7a24f
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 249 deletions.
2 changes: 1 addition & 1 deletion abi/bscvalidatorset.abi
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@
"name": "getCurrentValidatorIndex",
"inputs": [
{
"name": "_validator",
"name": "validator",
"type": "address",
"internalType": "address"
}
Expand Down
22 changes: 22 additions & 0 deletions contracts/BC_fusion/StakeHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,9 @@ contract StakeHub is System, Initializable {
}

/**
* @param operatorAddress the operator address of the validator
* @param index the index of the day to query(timestamp / 1 days)
*
* @return the validator's reward of the day
*/
function getValidatorRewardRecord(address operatorAddress, uint256 index) external view returns (uint256) {
Expand All @@ -868,6 +871,9 @@ contract StakeHub is System, Initializable {
}

/**
* @param operatorAddress the operator address of the validator
* @param index the index of the day to query(timestamp / 1 days)
*
* @return the validator's total pooled BNB of the day
*/
function getValidatorTotalPooledBNBRecord(address operatorAddress, uint256 index) external view returns (uint256) {
Expand All @@ -877,6 +883,10 @@ contract StakeHub is System, Initializable {

/**
* @notice pagination query all validators' operator address and credit contract address
*
* @param offset the offset of the query
* @param limit the limit of the query
*
* @return operatorAddrs operator addresses
* @return creditAddrs credit contract addresses
* @return totalLength total number of validators
Expand All @@ -902,6 +912,9 @@ contract StakeHub is System, Initializable {

/**
* @notice get the basic info of a validator
*
* @param operatorAddress the operator address of the validator
*
* @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
Expand Down Expand Up @@ -932,6 +945,8 @@ contract StakeHub is System, Initializable {
}

/**
* @param operatorAddress the operator address of the validator
*
* @return the description of a validator
*/
function getValidatorDescription(address operatorAddress)
Expand All @@ -944,6 +959,8 @@ contract StakeHub is System, Initializable {
}

/**
* @param operatorAddress the operator address of the validator
*
* @return the commission of a validator
*/
function getValidatorCommission(address operatorAddress)
Expand All @@ -957,7 +974,12 @@ contract StakeHub is System, Initializable {

/**
* @dev this function will be used by Parlia consensus engine.
*
* @notice get the election info of a validator
*
* @param offset the offset of the query
* @param limit the limit of the query
*
* @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
Expand Down
41 changes: 29 additions & 12 deletions contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

/**
* @dev With each epoch, there will be a partial rotation between cabinets and candidates. Rotation is determined by this function
*
*/
function shuffle(address[] memory validators, bytes[] memory voteAddrs, uint256 epochNumber, uint startIdx, uint offset, uint limit, uint modNumber) internal pure {
for (uint i; i<limit; ++i) {
Expand All @@ -548,6 +547,9 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}
}

/**
* @notice Return the vote address and consensus address of the validators in `currentValidatorSet` that are not jailed
*/
function getLivingValidators() external view override returns (address[] memory, bytes[] memory) {
uint n = currentValidatorSet.length;
uint living;
Expand Down Expand Up @@ -579,14 +581,14 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}

/**
* @dev Get mining validators that are block producers in the current epoch, including most of the cabinets and a few of the candidates
* @notice Return the vote address and consensus address of mining validators
*
* Mining validators are block producers in the current epoch
* including most of the cabinets and a few of the candidates
*/
function getMiningValidators() external view override returns(address[] memory, bytes[] memory) {
uint256 _maxNumOfWorkingCandidates = maxNumOfWorkingCandidates;
uint256 _numOfCabinets = numOfCabinets;
if (_numOfCabinets == 0 ){
_numOfCabinets = INIT_NUM_OF_CABINETS;
}
uint256 _numOfCabinets = numOfCabinets > 0 ? numOfCabinets : INIT_NUM_OF_CABINETS;

address[] memory validators = getValidators();
bytes[] memory voteAddrs = getVoteAddresses(validators);
Expand All @@ -613,8 +615,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}

/**
* @dev Get all validators, including all of the cabinets and all of the candidates
*
* @notice Return the consensus address of the validators in `currentValidatorSet` that are not jailed and not maintaining
*/
function getValidators() public view returns(address[] memory) {
uint n = currentValidatorSet.length;
Expand All @@ -635,6 +636,11 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
return consensusAddrs;
}

/**
* @notice Return whether the validator is a working validator(not jailed or maintaining) by index
*
* @param index The index of the validator in `currentValidatorSet`(from 0 to `currentValidatorSet.length-1`)
*/
function isWorkingValidator(uint index) public view returns (bool) {
if (index >= currentValidatorSet.length) {
return false;
Expand All @@ -648,6 +654,9 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
return !currentValidatorSet[index].jailed && !validatorExtraSet[index].isMaintaining;
}

/**
* @notice Return the current incoming of the validator
*/
function getIncoming(address validator)external view returns(uint256) {
uint256 index = currentValidatorSetMap[validator];
if (index<=0) {
Expand All @@ -656,6 +665,10 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
return currentValidatorSet[index-1].incoming;
}

/**
* @notice Return whether the validator is a working validator(not jailed or maintaining) by consensus address
* Will return false if the validator is not in `currentValidatorSet`
*/
function isCurrentValidator(address validator) external view override returns (bool) {
uint256 index = currentValidatorSetMap[validator];
if (index <= 0) {
Expand Down Expand Up @@ -761,14 +774,20 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}

/*********************** For Temporary Maintenance **************************/
function getCurrentValidatorIndex(address _validator) public view returns (uint256) {
uint256 index = currentValidatorSetMap[_validator];
/**
* @notice Return the index of the validator in `currentValidatorSet`(from 0 to `currentValidatorSet.length-1`)
*/
function getCurrentValidatorIndex(address validator) public view returns (uint256) {
uint256 index = currentValidatorSetMap[validator];
require(index > 0, "only current validators");

// the actual index
return index - 1;
}

/**
* @notice Return whether the validator at index could enter maintenance
*/
function canEnterMaintenance(uint256 index) public view returns (bool) {
if (index >= currentValidatorSet.length) {
return false;
Expand All @@ -791,7 +810,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

/**
* @dev Enter maintenance for current validators. refer to https://github.com/bnb-chain/BEPs/blob/master/BEP127.md
*
*/
function enterMaintenance() external initValidatorExtraSet {
// check maintain config
Expand All @@ -809,7 +827,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

/**
* @dev Exit maintenance for current validators. refer to https://github.com/bnb-chain/BEPs/blob/master/BEP127.md
*
*/
function exitMaintenance() external {
uint256 index = getCurrentValidatorIndex(msg.sender);
Expand Down
42 changes: 2 additions & 40 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -593,53 +593,15 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR

/**
* @dev request a cross-chain transfer from BSC to BC
* @notice this function is deprecated after Feynman upgrade
*
* @param contractAddr The token contract which is transferred
* @param recipient The destination address of the cross-chain transfer on BC.
* @param amount The amount to transfer
* @param expireTime The expire time for the cross-chain transfer
*/
function transferOut(address contractAddr, address recipient, uint256 amount, uint64 expireTime) external override onlyInit payable returns (bool) {
require(expireTime>=block.timestamp + 120, "expireTime must be two minutes later");
require(msg.value%TEN_DECIMALS==0, "invalid received BNB amount: precision loss in amount conversion");
bytes32 bep2TokenSymbol;
uint256 convertedAmount;
uint256 rewardForRelayer;
if (contractAddr==address(0x0)) {
require(msg.value>=amount.add(relayFee), "received BNB amount should be no less than the sum of transferOut BNB amount and minimum relayFee");
require(amount%TEN_DECIMALS==0, "invalid transfer amount: precision loss in amount conversion");
rewardForRelayer=msg.value.sub(amount);
convertedAmount = amount.div(TEN_DECIMALS); // native bnb decimals is 8 on BBC, while the native bnb decimals on BSC is 18
bep2TokenSymbol=BEP2_TOKEN_SYMBOL_FOR_BNB;
} else {
bep2TokenSymbol = contractAddrToBEP2Symbol[contractAddr];
require(bep2TokenSymbol!=bytes32(0x00), "the contract has not been bound to any bep2 token");
require(msg.value>=relayFee, "received BNB amount should be no less than the minimum relayFee");
rewardForRelayer=msg.value;
uint256 bep20TokenDecimals=bep20ContractDecimals[contractAddr];
require(bep20TokenDecimals<=BEP2_TOKEN_DECIMALS || (bep20TokenDecimals>BEP2_TOKEN_DECIMALS && amount.mod(10**(bep20TokenDecimals-BEP2_TOKEN_DECIMALS))==0), "invalid transfer amount: precision loss in amount conversion");
convertedAmount = convertToBep2Amount(amount, bep20TokenDecimals);// convert to bep2 amount
if (isMiniBEP2Token(bep2TokenSymbol)) {
require(convertedAmount >= 1e8 , "For miniToken, the transfer amount must not be less than 1");
}
require(bep20TokenDecimals>=BEP2_TOKEN_DECIMALS || (bep20TokenDecimals<BEP2_TOKEN_DECIMALS && convertedAmount>amount), "amount is too large, uint256 overflow");
require(convertedAmount<=MAX_BEP2_TOTAL_SUPPLY, "amount is too large, exceed maximum bep2 token amount");
require(IBEP20(contractAddr).transferFrom(msg.sender, address(this), amount));
}
TransferOutSynPackage memory transOutSynPkg = TransferOutSynPackage({
bep2TokenSymbol: bep2TokenSymbol,
contractAddr: contractAddr,
amounts: new uint256[](1),
recipients: new address[](1),
refundAddrs: new address[](1),
expireTime: expireTime
});
transOutSynPkg.amounts[0]=convertedAmount;
transOutSynPkg.recipients[0]=recipient;
transOutSynPkg.refundAddrs[0]=msg.sender;
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendSynPackage(TRANSFER_OUT_CHANNELID, encodeTransferOutSynPackage(transOutSynPkg), rewardForRelayer.div(TEN_DECIMALS));
emit transferOutSuccess(contractAddr, msg.sender, amount, rewardForRelayer);
return true;
revert("deprecated");
}

/**
Expand Down
Loading

0 comments on commit 0c7a24f

Please sign in to comment.