Skip to content

Commit

Permalink
Merge pull request poanetwork#5 from lukso-network/aura-to-hbbft-term…
Browse files Browse the repository at this point in the history
…inology

Aura to hbbft terminology
  • Loading branch information
i-stam authored Apr 8, 2020
2 parents 30dbc10 + c5927ee commit 6f5c9b9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 92 deletions.
2 changes: 1 addition & 1 deletion contracts/TxPermissionHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ contract TxPermissionHbbft is UpgradeableOwned, ITxPermission {
/// staking epoch: the block gas limit is temporarily reduced for the latest block of the epoch.
function blockGasLimit() public view returns(uint256) {
address stakingContract = validatorSetContract.stakingContract();
uint256 stakingEpochEndBlock = IStakingHbbft(stakingContract).stakingEpochEndBlock();
uint256 stakingEpochEndBlock = IStakingHbbft(stakingContract).stakingFixedEpochEndBlock();
if (block.number == stakingEpochEndBlock - 1 || block.number == stakingEpochEndBlock) {
return BLOCK_GAS_LIMIT_REDUCED;
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/ValidatorSetHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,9 @@ contract ValidatorSetHbbft is UpgradeabilityAdmin, IValidatorSetHbbft {
/// @dev Returns the future block number until which a validator is banned.
/// Used by the `_removeMaliciousValidator` internal function.
function _banUntil() internal view returns(uint256) {
uint256 blocksUntilEnd = stakingContract.stakingEpochEndBlock() - _getCurrentBlockNumber();
uint256 blocksUntilEnd = stakingContract.stakingFixedEpochEndBlock() - _getCurrentBlockNumber();
// ~90 days, at least 12 full staking epochs (for 5 seconds block)
return _getCurrentBlockNumber() + 12 * stakingContract.stakingEpochDuration() + blocksUntilEnd;
return _getCurrentBlockNumber() + 12 * stakingContract.stakingFixedEpochDuration() + blocksUntilEnd;
}

/// @dev Returns the current block number. Needed mostly for unit tests.
Expand Down
48 changes: 17 additions & 31 deletions contracts/base/BlockRewardHbbftBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {

/// @dev Called by the validator's node when producing and closing a block,
/// see https://wiki.parity.io/Block-Reward-Contract.html.
/// This function performs all of the automatic operations needed for controlling secrets revealing by validators,
/// accumulating block producing statistics, starting a new staking epoch, snapshotting staking amounts
/// for the upcoming staking epoch, rewards distributing at the end of a staking epoch, and minting
/// native coins needed for the `erc-to-native` bridge.
/// This function performs all of the automatic operations needed for accumulating block producing statistics,
/// starting a new staking epoch, snapshotting staking amounts for the upcoming staking epoch,
/// rewards distributing at the end of a staking epoch,
/// and minting native coins needed for the `erc-to-native` bridge.
function reward(address[] calldata benefactors, uint16[] calldata kind)
external
onlySystem
Expand All @@ -236,10 +236,6 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
return (new address[](0), new uint256[](0));
}

// Check the current validators at the end of each collection round whether
// they revealed their secrets, and remove a validator as a malicious if needed
// IRandomHbbft(validatorSetContract.randomContract()).onFinishCollectRound();

// Initialize the extra receivers queue
if (!_queueERInitialized) {
_queueERFirst = 1;
Expand All @@ -250,7 +246,7 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
uint256 bridgeQueueLimit = 100;
IStakingHbbft stakingContract = IStakingHbbft(validatorSetContract.stakingContract());
uint256 stakingEpoch = stakingContract.stakingEpoch();
uint256 stakingEpochEndBlock = stakingContract.stakingEpochEndBlock();
uint256 stakingFixedEpochEndBlock = stakingContract.stakingFixedEpochEndBlock();
uint256 nativeTotalRewardAmount = 0;

if (validatorSetContract.validatorSetApplyBlock() != 0) {
Expand All @@ -262,27 +258,27 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
}
}

if (_getCurrentBlockNumber() == stakingEpochEndBlock) {
if (_getCurrentBlockNumber() == stakingFixedEpochEndBlock) {
// Distribute rewards among validator pools
if (stakingEpoch != 0) {
nativeTotalRewardAmount = _distributeRewards(
stakingContract,
stakingEpoch,
stakingEpochEndBlock
stakingFixedEpochEndBlock
);
}

// Choose new validators
validatorSetContract.newValidatorSet();

// Snapshot total amounts staked into the pools
// Snapshot total amounts staked into the pools
uint256 i;
uint256 nextStakingEpoch = stakingEpoch + 1;
address[] memory miningAddresses;


// Choose new validators
validatorSetContract.newValidatorSet();

// We need to remember the total staked amounts for the pending addresses
// for the possible case when these pending addresses are finalized
// by the `ValidatorSetHbbft.finalizeChange` function and thus become validators
// for when these pending addresses are finalized by `ValidatorSetHbbft.finalizeChange()`.
miningAddresses = validatorSetContract.getPendingValidators();
for (i = 0; i < miningAddresses.length; i++) {
_snapshotPoolStakeAmounts(stakingContract, nextStakingEpoch, miningAddresses[i]);
Expand All @@ -297,16 +293,6 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
_snapshotPoolStakeAmounts(stakingContract, nextStakingEpoch, miningAddresses[i]);
}

// We need to remember the total staked amounts for the addresses currently
// being finalized but not yet finalized (i.e. the `InitiateChange` event is emitted
// for them but not yet handled by validator nodes thus the `ValidatorSetHbbft.finalizeChange`
// function is not called yet) for the possible case when these addresses finally
// become validators on the upcoming staking epoch
miningAddresses = validatorSetContract.getPendingValidators();
for (i = 0; i < miningAddresses.length; i++) {
_snapshotPoolStakeAmounts(stakingContract, nextStakingEpoch, miningAddresses[i]);
}

// Remember validator's min reward percent for the upcoming staking epoch
validatorMinRewardPercent[nextStakingEpoch] = VALIDATOR_MIN_REWARD_PERCENT;

Expand Down Expand Up @@ -614,13 +600,13 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
/// This function is called by the `reward` function.
/// @param _stakingContract The address of the StakingHbbft contract.
/// @param _stakingEpoch The number of the current staking epoch.
/// @param _stakingEpochEndBlock The number of the latest block of the current staking epoch.
/// @param _stakingFixedEpochEndBlock The number of the latest block before key generation begins.
/// @return Returns the reward amount in native coins needed to be minted
/// and accrued to the balance of this contract.
function _distributeRewards(
IStakingHbbft _stakingContract,
uint256 _stakingEpoch,
uint256 _stakingEpochEndBlock
uint256 _stakingFixedEpochEndBlock
) internal returns(uint256 nativeTotalRewardAmount) {
address[] memory validators = validatorSetContract.getValidators();

Expand All @@ -636,8 +622,8 @@ contract BlockRewardHbbftBase is UpgradeableOwned, IBlockRewardHbbft {
realFinalizeBlock = idealFinalizeBlock;
}

totalRewardShareNum = _stakingEpochEndBlock - realFinalizeBlock + 1;
totalRewardShareDenom = _stakingEpochEndBlock - idealFinalizeBlock + 1;
totalRewardShareNum = _stakingFixedEpochEndBlock - realFinalizeBlock + 1;
totalRewardShareDenom = _stakingFixedEpochEndBlock - idealFinalizeBlock + 1;
}

uint256[] memory blocksCreatedShareNum = new uint256[](validators.length);
Expand Down
22 changes: 11 additions & 11 deletions contracts/base/StakingHbbftBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
/// @dev The serial number of the current staking epoch.
uint256 public stakingEpoch;

/// @dev The duration of a staking epoch in blocks.
uint256 public stakingEpochDuration;
/// @dev The fixed duration of each staking epoch before KeyGen starts i.e. before the upcoming ("pending") validators are selected.
uint256 public stakingFixedEpochDuration;

/// @dev The number of the first block of the current staking epoch.
uint256 public stakingEpochStartBlock;
Expand Down Expand Up @@ -299,7 +299,7 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
/// @param _initialStakingAddresses The array of initial validators' staking addresses.
/// @param _delegatorMinStake The minimum allowed amount of delegator stake in Wei.
/// @param _candidateMinStake The minimum allowed amount of candidate/validator stake in Wei.
/// @param _stakingEpochDuration The duration of a staking epoch in blocks
/// @param _stakingFixedEpochDuration The fixed duration of each epoch before keyGen starts in blocks
/// @param _stakingEpochStartBlock The number of the first block of initial staking epoch
/// (must be zero if the network is starting from genesis block).
/// @param _stakeWithdrawDisallowPeriod The duration period (in blocks) at the end of a staking epoch
Expand All @@ -309,14 +309,14 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
address[] calldata _initialStakingAddresses,
uint256 _delegatorMinStake,
uint256 _candidateMinStake,
uint256 _stakingEpochDuration,
uint256 _stakingFixedEpochDuration,
uint256 _stakingEpochStartBlock,
uint256 _stakeWithdrawDisallowPeriod,
bytes32[] calldata _publicKeys,
bytes16[] calldata _internetAddresses
) external {
require(_stakingEpochDuration != 0);
require(_stakingEpochDuration > _stakeWithdrawDisallowPeriod);
require(_stakingFixedEpochDuration != 0);
require(_stakingFixedEpochDuration > _stakeWithdrawDisallowPeriod);
require(_stakeWithdrawDisallowPeriod != 0);
_initialize(
_validatorSetContract,
Expand All @@ -326,7 +326,7 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
_publicKeys,
_internetAddresses
);
stakingEpochDuration = _stakingEpochDuration;
stakingFixedEpochDuration = _stakingFixedEpochDuration;
stakeWithdrawDisallowPeriod = _stakeWithdrawDisallowPeriod;
stakingEpochStartBlock = _stakingEpochStartBlock;
}
Expand Down Expand Up @@ -600,7 +600,7 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
/// Used by all staking/withdrawal functions.
function areStakeAndWithdrawAllowed() public view returns(bool) {
uint256 currentBlock = _getCurrentBlockNumber();
uint256 allowedDuration = stakingEpochDuration - stakeWithdrawDisallowPeriod;
uint256 allowedDuration = stakingFixedEpochDuration - stakeWithdrawDisallowPeriod; //TODO: should it be extended to the start of the block, e.g. startBlock -1
if (currentBlock < stakingEpochStartBlock) return false;
return currentBlock - stakingEpochStartBlock <= allowedDuration; //TODO: should be < not <=?
}
Expand Down Expand Up @@ -709,10 +709,10 @@ contract StakingHbbftBase is UpgradeableOwned, IStakingHbbft {
return _stakeAmountByEpoch[_poolStakingAddress][_staker][stakingEpoch];
}

/// @dev Returns the number of the last block of the current staking epoch.
function stakingEpochEndBlock() public view returns(uint256) {
/// @dev Returns the number of the last block of the current staking epoch before key generation starts.
function stakingFixedEpochEndBlock() public view returns(uint256) {
uint256 startBlock = stakingEpochStartBlock;
return startBlock + stakingEpochDuration - (startBlock == 0 ? 0 : 1);
return startBlock + stakingFixedEpochDuration - (startBlock == 0 ? 0 : 1);
}

// ============================================== Internal ========================================================
Expand Down
4 changes: 2 additions & 2 deletions contracts/interfaces/IStakingHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface IStakingHbbft {
function stakeLastEpoch(address, address) external view returns(uint256);
function stakeWithdrawDisallowPeriod() external view returns(uint256);
function stakingEpoch() external view returns(uint256);
function stakingEpochDuration() external view returns(uint256);
function stakingEpochEndBlock() external view returns(uint256);
function stakingFixedEpochDuration() external view returns(uint256);
function stakingFixedEpochEndBlock() external view returns(uint256);
function stakingEpochStartBlock() external view returns(uint256);
}
56 changes: 23 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f5c9b9

Please sign in to comment.