Refactor BaseFeeManager

This commit is contained in:
Gerardo Nardelli 2019-04-16 17:05:06 -03:00
parent 16be3f8229
commit cf81b767ba
6 changed files with 84 additions and 75 deletions

View File

@ -43,54 +43,13 @@ contract BaseFeeManager is EternalStorage, FeeTypes {
return uintStorage[keccak256(abi.encodePacked("foreignFee"))];
}
function distributeFeeFromAffirmation(uint256 _fee) external {
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_FOREIGN);
}
function distributeFeeFromAffirmation(uint256 _fee) external;
function distributeFeeFromSignatures(uint256 _fee) external {
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_HOME);
}
function distributeFeeFromSignatures(uint256 _fee) external;
function getFeeManagerMode() public pure returns(bytes4);
function random(uint256 _count) public view returns(uint256) {
return uint256(blockhash(block.number.sub(1))) % _count;
}
function distributeFeeProportionally(uint256 _fee, bytes32 _direction) internal {
IRewardableValidators validators = rewardableValidatorContract();
address[] memory validatorList = validators.validatorList();
uint256 feePerValidator = _fee.div(validatorList.length);
uint256 randomValidatorIndex;
uint256 diff = _fee.sub(feePerValidator.mul(validatorList.length));
if (diff > 0) {
randomValidatorIndex = random(validatorList.length);
}
for (uint256 i = 0; i < validatorList.length; i++) {
uint256 feeToDistribute = feePerValidator;
if (diff > 0 && randomValidatorIndex == i) {
feeToDistribute = feeToDistribute.add(diff);
}
address rewardAddress = validators.getValidatorRewardAddress(validatorList[i]);
onFeeDistribution(rewardAddress, feeToDistribute, _direction);
}
}
function onFeeDistribution(address _rewardAddress, uint256 _fee, bytes32 _direction) internal {
if (_direction == REWARD_FOR_TRANSFERRING_FROM_FOREIGN) {
onAffirmationFeeDistribution(_rewardAddress, _fee);
} else {
onSignatureFeeDistribution(_rewardAddress, _fee);
}
}
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal;
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal;
function rewardableValidatorContract() public view returns(IRewardableValidators) {
return IRewardableValidators(addressStorage[keccak256(abi.encodePacked("validatorContract"))]);
}
}

View File

@ -0,0 +1,24 @@
pragma solidity 0.4.24;
import "./BaseFeeManager.sol";
import "../IBlockReward.sol";
contract BlockRewardFeeManager is BaseFeeManager {
function distributeFeeFromAffirmation(uint256 _fee) external {
distributeFeeFromBlockReward(_fee);
}
function distributeFeeFromSignatures(uint256 _fee) external {
distributeFeeFromBlockReward(_fee);
}
function distributeFeeFromBlockReward(uint256 _fee) internal {
IBlockReward blockReward = _blockRewardContract();
blockReward.addBridgeTokenFeeReceivers(_fee);
}
function _blockRewardContract() internal view returns(IBlockReward) {
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
}
}

View File

@ -0,0 +1,52 @@
pragma solidity 0.4.24;
import "./BaseFeeManager.sol";
import "../IRewardableValidators.sol";
contract ValidatorsFeeManager is BaseFeeManager {
function distributeFeeFromAffirmation(uint256 _fee) external {
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_FOREIGN);
}
function distributeFeeFromSignatures(uint256 _fee) external {
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_HOME);
}
function rewardableValidatorContract() internal view returns(IRewardableValidators) {
return IRewardableValidators(addressStorage[keccak256(abi.encodePacked("validatorContract"))]);
}
function distributeFeeProportionally(uint256 _fee, bytes32 _direction) internal {
IRewardableValidators validators = rewardableValidatorContract();
address[] memory validatorList = validators.validatorList();
uint256 feePerValidator = _fee.div(validatorList.length);
uint256 randomValidatorIndex;
uint256 diff = _fee.sub(feePerValidator.mul(validatorList.length));
if (diff > 0) {
randomValidatorIndex = random(validatorList.length);
}
for (uint256 i = 0; i < validatorList.length; i++) {
uint256 feeToDistribute = feePerValidator;
if (diff > 0 && randomValidatorIndex == i) {
feeToDistribute = feeToDistribute.add(diff);
}
address rewardAddress = validators.getValidatorRewardAddress(validatorList[i]);
onFeeDistribution(rewardAddress, feeToDistribute, _direction);
}
}
function onFeeDistribution(address _rewardAddress, uint256 _fee, bytes32 _direction) internal {
if (_direction == REWARD_FOR_TRANSFERRING_FROM_FOREIGN) {
onAffirmationFeeDistribution(_rewardAddress, _fee);
} else {
onSignatureFeeDistribution(_rewardAddress, _fee);
}
}
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal;
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal;
}

View File

@ -1,36 +1,10 @@
pragma solidity 0.4.24;
import "../BaseFeeManager.sol";
import "../../IBlockReward.sol";
import "../BlockRewardFeeManager.sol";
contract FeeManagerErcToErcPOSDAO is BaseFeeManager {
contract FeeManagerErcToErcPOSDAO is BlockRewardFeeManager {
function getFeeManagerMode() public pure returns(bytes4) {
return bytes4(keccak256(abi.encodePacked("manages-both-directions")));
}
function blockRewardContract() internal view returns(IBlockReward) {
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
}
function distributeFeeFromAffirmation(uint256 _fee) external {
distributeFeeFromBlockReward(_fee);
}
function distributeFeeFromSignatures(uint256 _fee) external {
distributeFeeFromBlockReward(_fee);
}
function distributeFeeFromBlockReward(uint256 _fee) internal {
IBlockReward blockReward = blockRewardContract();
blockReward.addBridgeTokenFeeReceivers(_fee);
}
function isPOSDAOFeeManager() public pure returns(bool) {
return true;
}
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal {}
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal {}
}

View File

@ -1,11 +1,11 @@
pragma solidity 0.4.24;
import "../BaseFeeManager.sol";
import "../../IBlockReward.sol";
import "../Sacrifice.sol";
import "../ValidatorsFeeManager.sol";
contract FeeManagerErcToNative is BaseFeeManager {
contract FeeManagerErcToNative is ValidatorsFeeManager {
function getFeeManagerMode() public pure returns(bytes4) {
return bytes4(keccak256(abi.encodePacked("manages-both-directions")));

View File

@ -1,11 +1,11 @@
pragma solidity 0.4.24;
import "../BaseFeeManager.sol";
import "../../IBurnableMintableERC677Token.sol";
import "../Sacrifice.sol";
import "../ValidatorsFeeManager.sol";
contract FeeManagerNativeToErc is BaseFeeManager {
contract FeeManagerNativeToErc is ValidatorsFeeManager {
function getFeeManagerMode() public pure returns(bytes4) {
return bytes4(keccak256(abi.encodePacked("manages-one-direction")));