Skip to content

Commit

Permalink
v0.4.3 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
nxqbao authored Mar 7, 2023
2 parents 1d3f5e3 + db8212c commit c712835
Show file tree
Hide file tree
Showing 107 changed files with 11,097 additions and 959 deletions.
2 changes: 1 addition & 1 deletion contracts/extensions/GatewayV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract contract GatewayV2 is HasProxyAdmin, Pausable, IQuorum {
/**
* @inheritdoc IQuorum
*/
function getThreshold() external view virtual returns (uint256, uint256) {
function getThreshold() external view virtual returns (uint256 num_, uint256 denom_) {
return (_num, _denom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ abstract contract BOsGovernanceProposal is SignatureConsumer, IsolatedGovernance
_lastVotedBlock[_signer] = block.number;
_info.signatureOf[_signer] = _signatures[_i];
_info.voters.push(_signer);
if (_castVote(_v, _signer, _weight, _minimumVoteWeight, _hash) == VoteStatus.Approved) {
return;
}
_castVote(_v, _signer, _weight, _minimumVoteWeight, _hash);
}
}

Expand Down
69 changes: 34 additions & 35 deletions contracts/extensions/sequential-governance/CoreGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,36 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
}

/**
* @dev Creates new round voting for the proposal `_proposalHash` of chain `_chainId`.
* @dev Creates new voting round by calculating the `_round` number of chain `_chainId`.
* Increases the `_round` number if the previous one is not expired. Delete the previous proposal
* if it is expired and not increase the `_round`.
*/
function _createVotingRound(
uint256 _chainId,
bytes32 _proposalHash,
uint256 _expiryTimestamp
) internal returns (uint256 _round) {
function _createVotingRound(uint256 _chainId) internal returns (uint256 _round) {
_round = round[_chainId];

// Skip checking for the first ever round
if (_round == 0) {
_round = round[_chainId] = 1;
} else {
ProposalVote storage _latestProposalVote = vote[_chainId][_round];
bool _isExpired = _tryDeleteExpiredVotingRound(_latestProposalVote);
// Skip increase round number if the latest round is expired, allow the vote to be overridden
// Skip increasing round number if the latest round is expired, allow the vote to be overridden
if (!_isExpired) {
require(_latestProposalVote.status != VoteStatus.Pending, "CoreGovernance: current proposal is not completed");
_round = ++round[_chainId];
}
}
}

vote[_chainId][_round].hash = _proposalHash;
vote[_chainId][_round].expiryTimestamp = _expiryTimestamp;
/**
* @dev Saves new round voting for the proposal `_proposalHash` of chain `_chainId`.
*/
function _saveVotingRound(
ProposalVote storage _vote,
bytes32 _proposalHash,
uint256 _expiryTimestamp
) internal {
_vote.hash = _proposalHash;
_vote.expiryTimestamp = _expiryTimestamp;
}

/**
Expand All @@ -111,20 +117,13 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
address _creator
) internal virtual returns (Proposal.ProposalDetail memory _proposal) {
require(_chainId != 0, "CoreGovernance: invalid chain id");
uint256 _round = _createVotingRound(_chainId);

_proposal = Proposal.ProposalDetail(
round[_chainId] + 1,
_chainId,
_expiryTimestamp,
_targets,
_values,
_calldatas,
_gasAmounts
);
_proposal = Proposal.ProposalDetail(_round, _chainId, _expiryTimestamp, _targets, _values, _calldatas, _gasAmounts);
_proposal.validate(_proposalExpiryDuration);

bytes32 _proposalHash = _proposal.hash();
uint256 _round = _createVotingRound(_chainId, _proposalHash, _expiryTimestamp);
_saveVotingRound(vote[_chainId][_round], _proposalHash, _expiryTimestamp);
emit ProposalCreated(_chainId, _round, _proposalHash, _proposal, _creator);
}

Expand All @@ -148,7 +147,8 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
_proposal.validate(_proposalExpiryDuration);

bytes32 _proposalHash = _proposal.hash();
_round = _createVotingRound(_chainId, _proposalHash, _proposal.expiryTimestamp);
_round = _createVotingRound(_chainId);
_saveVotingRound(vote[_chainId][_round], _proposalHash, _proposal.expiryTimestamp);
require(_round == _proposal.nonce, "CoreGovernance: invalid proposal nonce");
emit ProposalCreated(_chainId, _round, _proposalHash, _proposal, _creator);
}
Expand All @@ -168,9 +168,10 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
address _roninTrustedOrganizationContract,
address _gatewayContract,
address _creator
) internal virtual returns (uint256 _round) {
) internal virtual {
uint256 _round = _createVotingRound(0);
GlobalProposal.GlobalProposalDetail memory _globalProposal = GlobalProposal.GlobalProposalDetail(
round[0] + 1,
_round,
_expiryTimestamp,
_targetOptions,
_values,
Expand All @@ -184,7 +185,7 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
_proposal.validate(_proposalExpiryDuration);

bytes32 _proposalHash = _proposal.hash();
_round = _createVotingRound(0, _proposalHash, _expiryTimestamp);
_saveVotingRound(vote[0][_round], _proposalHash, _expiryTimestamp);
emit GlobalProposalCreated(_round, _proposalHash, _proposal, _globalProposal.hash(), _globalProposal, _creator);
}

Expand All @@ -202,12 +203,13 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
address _roninTrustedOrganizationContract,
address _gatewayContract,
address _creator
) internal virtual returns (Proposal.ProposalDetail memory _proposal, uint256 _round) {
) internal virtual returns (Proposal.ProposalDetail memory _proposal) {
_proposal = _globalProposal.into_proposal_detail(_roninTrustedOrganizationContract, _gatewayContract);
_proposal.validate(_proposalExpiryDuration);

bytes32 _proposalHash = _proposal.hash();
_round = _createVotingRound(0, _proposalHash, _globalProposal.expiryTimestamp);
uint256 _round = _createVotingRound(0);
_saveVotingRound(vote[0][_round], _proposalHash, _globalProposal.expiryTimestamp);
require(_round == _proposal.nonce, "CoreGovernance: invalid proposal nonce");
emit GlobalProposalCreated(_round, _proposalHash, _proposal, _globalProposal.hash(), _globalProposal, _creator);
}
Expand Down Expand Up @@ -278,18 +280,15 @@ abstract contract CoreGovernance is SignatureConsumer, VoteStatusConsumer, Chain
}
}

/**
* @dev Delete the expired proposal by its chainId and nonce, without creating a new proposal.
*/
function _deleteExpiredVotingRound(uint256 _chainId, uint256 _round) internal {
ProposalVote storage _vote = vote[_chainId][_round];
_tryDeleteExpiredVotingRound(_vote);
}

/**
* @dev When the contract is on Ronin chain, checks whether the proposal is expired and delete it if is expired.
*
* Emits the event `ProposalExpired` if the vote is expired.
*
* Note: This function assumes the vote `_proposalVote` is already created, consider verifying the vote's existence
* before or it will emit an unexpected event of `ProposalExpired`.
*/
function _tryDeleteExpiredVotingRound(ProposalVote storage _proposalVote) private returns (bool _isExpired) {
function _tryDeleteExpiredVotingRound(ProposalVote storage _proposalVote) internal returns (bool _isExpired) {
_isExpired =
_getChainType() == ChainType.RoninChain &&
_proposalVote.status == VoteStatus.Pending &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ abstract contract GovernanceProposal is CoreGovernance {
address _gatewayContract,
address _creator
) internal returns (Proposal.ProposalDetail memory _proposal) {
(_proposal, ) = _proposeGlobalStruct(
_globalProposal,
_roninTrustedOrganizationContract,
_gatewayContract,
_creator
);
_proposal = _proposeGlobalStruct(_globalProposal, _roninTrustedOrganizationContract, _gatewayContract, _creator);
bytes32 _globalProposalHash = _globalProposal.hash();
_castVotesBySignatures(
_proposal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ abstract contract GovernanceRelay is CoreGovernance {
address _gatewayContract,
address _creator
) internal {
(Proposal.ProposalDetail memory _proposal, ) = _proposeGlobalStruct(
Proposal.ProposalDetail memory _proposal = _proposeGlobalStruct(
_globalProposal,
_roninTrustedOrganizationContract,
_gatewayContract,
Expand Down
26 changes: 24 additions & 2 deletions contracts/interfaces/IMaintenance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ interface IMaintenance {
uint256 from;
uint256 to;
uint256 lastUpdatedBlock;
uint256 requestTimestamp;
}

/// @dev Emitted when a maintenance is scheduled.
event MaintenanceScheduled(address indexed consensusAddr, Schedule);
/// @dev Emitted when a schedule of maintenance is cancelled.
event MaintenanceScheduleCancelled(address indexed consensusAddr);
/// @dev Emitted when the maintenance config is updated.
event MaintenanceConfigUpdated(
uint256 minMaintenanceDurationInBlock,
uint256 maxMaintenanceDurationInBlock,
uint256 minOffsetToStartSchedule,
uint256 maxOffsetToStartSchedule,
uint256 maxSchedules
uint256 maxSchedules,
uint256 cooldownSecsToMaintain
);

/**
Expand Down Expand Up @@ -53,6 +57,11 @@ interface IMaintenance {
*/
function checkScheduled(address _consensusAddr) external view returns (bool);

/**
* @dev Returns whether the validator `_consensusAddr`
*/
function checkCooldownEnds(address _consensusAddr) external view returns (bool);

/**
* @dev Returns the detailed schedule of the validator `_consensusAddr`.
*/
Expand All @@ -79,7 +88,8 @@ interface IMaintenance {
uint256 _maxMaintenanceDurationInBlock,
uint256 _minOffsetToStartSchedule,
uint256 _maxOffsetToStartSchedule,
uint256 _maxSchedules
uint256 _maxSchedules,
uint256 _cooldownSecsToMaintain
) external;

/**
Expand Down Expand Up @@ -129,4 +139,16 @@ interface IMaintenance {
uint256 _startedAtBlock,
uint256 _endedAtBlock
) external;

/**
* @dev Cancel the schedule of maintenance for the `_consensusAddr`.
*
* Requirements:
* - The candidate `_consensusAddr` is the block producer.
* - The method caller is candidate admin of the candidate `_consensusAddr`.
* - A schedule for the `_consensusAddr` must be existent and not executed yet.
*
* Emits the event `MaintenanceScheduleCancelled`.
*/
function cancelSchedule(address _consensusAddr) external;
}
3 changes: 2 additions & 1 deletion contracts/interfaces/slash-indicator/IBaseSlash.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface IBaseSlash {
DOUBLE_SIGNING,
BRIDGE_VOTING,
BRIDGE_OPERATOR_MISSING_VOTE_TIER_1,
BRIDGE_OPERATOR_MISSING_VOTE_TIER_2
BRIDGE_OPERATOR_MISSING_VOTE_TIER_2,
UNAVAILABILITY_TIER_3
}

/// @dev Emitted when the validator is slashed.
Expand Down
18 changes: 15 additions & 3 deletions contracts/interfaces/slash-indicator/ICreditScore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ interface ICreditScore {
uint256 bailOutCostMultiplier,
uint256 cutOffPercentageAfterBailout
);
/// @dev Emitted the credit score of validators is updated
/// @dev Emitted the credit score of validators is updated.
event CreditScoresUpdated(address[] validators, uint256[] creditScores);
/// @dev Emitted when a validator bailed out of jail
event BailedOut(address indexed validator, uint256 period);
/// @dev Emitted when a validator bailed out of jail.
event BailedOut(address indexed validator, uint256 period, uint256 usedCreditScore);

/**
* @dev Updates the credit score for the validators.
Expand All @@ -27,6 +27,18 @@ interface ICreditScore {
*/
function updateCreditScores(address[] calldata _validators, uint256 _period) external;

/**
* @dev Resets the credit score for the revoked validators.
*
* Requirements:
* - Only validator contract can call this method.
* - This method is only called at the end of each period.
*
* Emits the event `CreditScoresUpdated`.
*
*/
function execResetCreditScores(address[] calldata _validators) external;

/**
* @dev A slashed validator use this method to get out of jail.
*
Expand Down
22 changes: 21 additions & 1 deletion contracts/interfaces/slash-indicator/ISlashBridgeOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

pragma solidity ^0.8.9;

interface ISlashBridgeOperator {
import "./IBaseSlash.sol";

interface ISlashBridgeOperator is IBaseSlash {
/**
* @dev Emitted when the configs to slash bridge operator is updated. See the method
* `getBridgeOperatorSlashingConfigs` for param details.
Expand All @@ -14,6 +16,24 @@ interface ISlashBridgeOperator {
uint256 skipBridgeOperatorSlashingThreshold
);

/**
* @dev Acknowledges bridge operator slash and emit `Slashed` event correspondingly.
* @param _tier The tier of the slash, in value of {1, 2}, corresponding to `SlashType.BRIDGE_OPERATOR_MISSING_VOTE_TIER_1`
* and `SlashType.BRIDGE_OPERATOR_MISSING_VOTE_TIER_2`
*
* Requirements:
* - Only validator contract can invoke this method.
* - Should be called only at the end of period.
* - Should be called only when there is slash of bridge operator.
*
* Emits the event `Slashed`.
*/
function execSlashBridgeOperator(
address _consensusAddr,
uint256 _tier,
uint256 _period
) external;

/**
* @dev Returns the configs related to bridge operator slashing.
*
Expand Down
22 changes: 19 additions & 3 deletions contracts/interfaces/slash-indicator/ISlashDoubleSign.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ interface ISlashDoubleSign is IBaseSlash {
* @dev Emitted when the configs to slash double sign is updated. See the method `getDoubleSignSlashingConfigs`
* for param details.
*/
event DoubleSignSlashingConfigsUpdated(uint256 slashDoubleSignAmount, uint256 doubleSigningJailUntilBlock);
event DoubleSignSlashingConfigsUpdated(
uint256 slashDoubleSignAmount,
uint256 doubleSigningJailUntilBlock,
uint256 doubleSigningOffsetLimitBlock
);

/**
* @dev Slashes for double signing.
Expand All @@ -31,12 +35,18 @@ interface ISlashDoubleSign is IBaseSlash {
* @return _slashDoubleSignAmount The amount of RON to slash double sign.
* @return _doubleSigningJailUntilBlock The block number that the punished validator will be jailed until, due to
* double signing.
* @param _doubleSigningOffsetLimitBlock The number of block that the current block is at most far from the double
* signing block.
*
*/
function getDoubleSignSlashingConfigs()
external
view
returns (uint256 _slashDoubleSignAmount, uint256 _doubleSigningJailUntilBlock);
returns (
uint256 _slashDoubleSignAmount,
uint256 _doubleSigningJailUntilBlock,
uint256 _doubleSigningOffsetLimitBlock
);

/**
* @dev Sets the configs to slash block producers.
Expand All @@ -48,7 +58,13 @@ interface ISlashDoubleSign is IBaseSlash {
*
* @param _slashAmount The amount of RON to slash double sign.
* @param _jailUntilBlock The block number that the punished validator will be jailed until, due to double signing.
* @param _doubleSigningOffsetLimitBlock The number of block that the current block is at most far from the double
* signing block.
*
*/
function setDoubleSignSlashingConfigs(uint256 _slashAmount, uint256 _jailUntilBlock) external;
function setDoubleSignSlashingConfigs(
uint256 _slashAmount,
uint256 _jailUntilBlock,
uint256 _doubleSigningOffsetLimitBlock
) external;
}
Loading

0 comments on commit c712835

Please sign in to comment.