Skip to content

Commit

Permalink
Integrate with precompile of picking validator set. Feat bridge opera…
Browse files Browse the repository at this point in the history
…tors. (#32)

* Squad commit

* Add ValidatorFlag

* Clean up

* Fix pick new validator set contract. Add precompile.

* Fix arrange validators test

* Fix wrapupEpoch function

- Refactor wrapUpEpoch function, separate into smaller modules
- Update to new requirements. Sorting & arranging validators, then disable their ability of producing blocks regarding their in jail and maintaining status
- Fix distribute delegating amount

* Refactor function order

* Update doc

* Fix precompile and precompile-usage

* Fix arrange validator precompile

* Fix RoninValidatorSet test

* Fix contract to deactivate block producers

* Emit BridgeOperatorSetUpdated. Fix query operators function.

* Fix mock contracts

* Fix test

* Fix EnumFlags. Fix test.

* Fix (de)activate block producers function. Fix test.

* Move precompile address to precompile-usage contracts

* Fix test of slashing indicator

* Fix test.

* Fix bug in _removeCandidate function

* Fix WrapUpEpoch integration test

* Fix submit reward integration test

* Clean up precompile contracts

* Add docs for private functions

* Clean up wrapping up epoch function

* Add more parameters for WrappedUpEpoch event

* Update docs for MiningRewardDistributed event

Co-authored-by: Duc Tho Tran <[email protected]>

* Update docs for BridgeOperatorRewardDistributed event

Co-authored-by: Duc Tho Tran <[email protected]>

* Minor saving gas

Co-authored-by: Duc Tho Tran <[email protected]>

* Minor saving gas

Co-authored-by: Duc Tho Tran <[email protected]>

* Minor fix for test of validator set

* Refactor Sorting contract

* Update docs for isValidator method

* Add epoch number for WrappedUpEpoch event

* Remove redundant casting to `IStaking`

* Remove duplicated code

* Fix `isValidator` callers

* Fix revert message in test

* Remove (De)ActivatedBlockProducers event. Add BlockProducerSetUpdated event.

* Fix `_pcPickValidatorSet` result length

* Fix catch precompile result length

Co-authored-by: Duc Tho Tran <[email protected]>
  • Loading branch information
nxqbao and ducthotran2010 authored Oct 24, 2022
1 parent 2d51d29 commit 3eb0823
Show file tree
Hide file tree
Showing 44 changed files with 1,645 additions and 780 deletions.
10 changes: 9 additions & 1 deletion contracts/interfaces/ICandidateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface ICandidateManager {
address consensusAddr;
// Address that receives mining reward of the validator
address payable treasuryAddr;
// Address of the bridge operator corresponding to the candidate
address bridgeOperatorAddr;
// The percentile of reward that validators can be received, the rest goes to the delegators.
// Values in range [0; 100_00] stands for 0-100%
uint256 commissionRate;
Expand All @@ -22,7 +24,12 @@ interface ICandidateManager {
/// @dev Emitted when the maximum number of validator candidates is updated.
event MaxValidatorCandidateUpdated(uint256 threshold);
/// @dev Emitted when the validator candidate is granted.
event CandidateGranted(address indexed consensusAddr, address indexed treasuryAddr, address indexed admin);
event CandidateGranted(
address indexed consensusAddr,
address indexed treasuryAddr,
address indexed admin,
address bridgeOperator
);
/// @dev Emitted when the revoked block of a candidate is updated.
event CandidateRevokedBlockUpdated(address indexed consensusAddr, uint256 revokedBlock);
/// @dev Emitted when the validator candidate is revoked.
Expand Down Expand Up @@ -57,6 +64,7 @@ interface ICandidateManager {
address _admin,
address _consensusAddr,
address payable _treasuryAddr,
address _bridgeOperatorAddr,
uint256 _commissionRate
) external;

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IMaintenance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface IMaintenance {
* @dev Schedules for maintenance from `_startedAtBlock` to `_startedAtBlock`.
*
* Requirements:
* - The candidate `_consensusAddr` is the validator.
* - The candidate `_consensusAddr` is the block producer.
* - The method caller is candidate admin of the candidate `_consensusAddr`.
* - The candidate `_consensusAddr` has no schedule yet or the previous is done.
* - The total number of schedules is not larger than `maxSchedules()`.
Expand Down
47 changes: 44 additions & 3 deletions contracts/interfaces/IRoninValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ interface IRoninValidatorSet is ICandidateManager {
event NumberOfEpochsInPeriodUpdated(uint256);
/// @dev Emitted when the validator set is updated
event ValidatorSetUpdated(address[]);
/// @dev Emitted when the bridge operator set is updated, to mirror the in-jail and maintaining status of the validator.
event BlockProducerSetUpdated(address[]);
/// @dev Emitted when the bridge operator set is updated.
event BridgeOperatorSetUpdated(address[]);
/// @dev Emitted when the reward of the valdiator is deprecated.
event RewardDeprecated(address coinbaseAddr, uint256 rewardAmount);
/// @dev Emitted when the block reward is submitted.
event BlockRewardSubmitted(address coinbaseAddr, uint256 submittedAmount, uint256 bonusAmount);
/// @dev Emitted when the validator is punished.
event ValidatorPunished(address validatorAddr, uint256 jailedUntil, uint256 deductedStakingAmount);
/// @dev Emitted when the validator reward is distributed.
event MiningRewardDistributed(address validatorAddr, uint256 amount);
event MiningRewardDistributed(address indexed validatorAddr, address indexed recipientAddr, uint256 amount);
/// @dev Emitted when the bridge operator reward is distributed.
event BridgeOperatorRewardDistributed(address indexed validatorAddr, address indexed recipientAddr, uint256 amount);
/// @dev Emitted when the amount of RON reward is distributed.
event StakingRewardDistributed(uint256 amount);
/// @dev Emitted when the epoch is wrapped up.
event WrappedUpEpoch(uint256 indexed periodNumber, uint256 indexed epochNumber, bool periodEnding);

///////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS FOR COINBASE //
Expand Down Expand Up @@ -52,7 +60,10 @@ interface IRoninValidatorSet is ICandidateManager {
*
* Emits the event `MiningRewardDistributed` when some validator has reward distributed.
* Emits the event `StakingRewardDistributed` when some staking pool has reward distributed.
* Emits the event `ValidatorSetUpdated`.
* Emits the event `BlockProducerSetUpdated` when the epoch is wrapped up.
* Emits the event `BridgeOperatorSetUpdated` when the epoch is wrapped up at period ending.
* Emits the event `ValidatorSetUpdated` when the epoch is wrapped up at period ending, and the validator set gets updated.
* Emits the event `WrappedUpEpoch`.
*
*/
function wrapUpEpoch() external payable;
Expand Down Expand Up @@ -121,10 +132,40 @@ interface IRoninValidatorSet is ICandidateManager {
function getValidators() external view returns (address[] memory);

/**
* @dev Returns whether the address is validator or not.
* @dev Returns whether the address is either a bridge operator or a block producer.
*/
function isValidator(address _addr) external view returns (bool);

/**
* @dev Returns the current block producer list.
*/
function getBlockProducers() external view returns (address[] memory);

/**
* @dev Returns whether the address is block producer or not.
*/
function isBlockProducer(address _addr) external view returns (bool);

/**
* @dev Returns total numbers of the block producers.
*/
function totalBlockProducers() external view returns (uint256);

/**
* @dev Returns the current bridge operator list.
*/
function getBridgeOperators() external view returns (address[] memory);

/**
* @dev Returns whether the address is bridge operator or not.
*/
function isBridgeOperator(address _addr) external view returns (bool);

/**
* @dev Returns total numbers of the bridge operators.
*/
function totalBridgeOperators() external view returns (uint256);

/**
* @dev Returns whether the epoch ending is at the block number `_block`.
*/
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ interface IStaking is IRewardPool {
address _candidateAdmin,
address _consensusAddr,
address payable _treasuryAddr,
address _bridgeOperatorAddr,
uint256 _commissionRate
) external payable;

Expand Down
62 changes: 51 additions & 11 deletions contracts/interfaces/IStakingVesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,76 @@
pragma solidity ^0.8.9;

interface IStakingVesting {
/// @dev Emitted when the block bonus is transferred.
event BlockBonusTransferred(uint256 indexed blockNumber, address indexed recipient, uint256 amount);
/// @dev Emitted when the block bonus is updated
event BonusPerBlockUpdated(uint256);
/// @dev Emitted when the block bonus for validator is transferred.
event ValidatorBonusTransferred(uint256 indexed blockNumber, address indexed recipient, uint256 amount);
/// @dev Emitted when the block bonus for bridge operator is transferred.
event BridgeOperatorBonusTransferred(uint256 indexed blockNumber, address indexed recipient, uint256 amount);
/// @dev Emitted when the block bonus for validator is updated
event ValidatorBonusPerBlockUpdated(uint256);
/// @dev Emitted when the block bonus for bridge operator is updated
event BridgeOperatorBonusPerBlockUpdated(uint256);

/**
* @dev Returns the bonus amount for the block `_block`.
* @dev Returns the bonus amount for the validator at `_block`.
*/
function blockBonus(uint256 _block) external view returns (uint256);
function validatorBlockBonus(uint256 _block) external view returns (uint256);

/**
* @dev Returns the bonus amount for the bridge validator at `_block`.
*/
function bridgeOperatorBlockBonus(uint256 _block) external view returns (uint256);

/**
* @dev Receives RON from any address.
*/
function receiveRON() external payable;

/**
* @dev Returns the last block number that the bonus reward is sent.
* @dev Returns the last block number that the staking vesting is sent for the validator.
*/
function lastBlockSendingValidatorBonus() external view returns (uint256);

/**
* @dev Returns the last block number that the staking vesting is sent for the bridge operator.
*/
function lastBlockSendingBridgeOperatorBonus() external view returns (uint256);

/**
* @dev Does two actions as in `requestValidatorBonus` and `requestBridgeOperatorBonus`. Returns
* two amounts of bonus correspondingly.
*
* Requirements:
* - The method caller is validator contract.
* - The method must be called only once per block.
*
* Emits the event `ValidatorBonusTransferred` and/or `BridgeOperatorBonusTransferred`
*
*/
function requestBonus() external returns (uint256 _validatorBonus, uint256 _bridgeOperatorBonus);

/**
* @dev Transfers the staking vesting for the validator whenever a new block is mined.
* Returns the amount of RON sent to validator contract.
*
* Requirements:
* - The method caller is validator contract.
* - The method must be called only once per block.
*
* Emits the event `ValidatorBonusTransferred`.
*
*/
function lastBonusSentBlock() external view returns (uint256);
function requestValidatorBonus() external returns (uint256);

/**
* @dev Transfers the bonus reward whenever a new block is mined.
* @dev Transfers the staking vesting for the bridge operator whenever a new block is mined.
* Returns the amount of RON sent to validator contract.
*
* Requirements:
* - The method caller is validator contract.
* - The method must be called only once per block.
*
* Emits the event `BlockBonusTransferred`.
* Emits the event `BridgeOperatorBonusTransferred`.
*
*/
function requestBlockBonus() external returns (uint256);
function requestBridgeOperatorBonus() external returns (uint256);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface IHasStakingVestingContract {
event StakingVestingContractUpdated(address);

/**
* @dev Returns the staking contract.
* @dev Returns the staking vesting contract.
*/
function stakingVestingContract() external view returns (address);

Expand Down
41 changes: 41 additions & 0 deletions contracts/libraries/EnumFlags.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

/**
* @dev This library implements checking flag of an enumerated value.
* The originated idea is inherited from [Enum.HashFlag(Enum)](https://learn.microsoft.com/en-us/dotnet/api/system.enum.hasflag?view=net-6.0) method of C#.
*/
library EnumFlags {
enum ValidatorFlag {
None, // bit(00)
BlockProducer, // bit(01)
BridgeOperator, // bit(10)
Both // bit(11)
}

function isNone(ValidatorFlag _value) internal pure returns (bool) {
return uint8(_value) == 0;
}

/**
* @dev Checks if `_value` has `_flag`.
*/
function hasFlag(ValidatorFlag _value, ValidatorFlag _flag) internal pure returns (bool) {
return (uint8(_value) & uint8(_flag)) != 0;
}

/**
* @dev Calculate new value of `_value` after adding `_flag`.
*/
function addFlag(ValidatorFlag _value, ValidatorFlag _flag) internal pure returns (ValidatorFlag) {
return ValidatorFlag(uint8(_value) | uint8(_flag));
}

/**
* @dev Calculate new value of `_value` after remove `_flag`.
*/
function removeFlag(ValidatorFlag _value, ValidatorFlag _flag) internal pure returns (ValidatorFlag) {
return ValidatorFlag(uint8(_value) & ~uint8(_flag));
}
}
104 changes: 0 additions & 104 deletions contracts/libraries/Sorting.sol

This file was deleted.

Loading

0 comments on commit 3eb0823

Please sign in to comment.