Skip to content

Commit

Permalink
fix(Validator): correct return of getValidators and `getBlockProduc…
Browse files Browse the repository at this point in the history
…ers`
  • Loading branch information
nxqbao committed Nov 24, 2023
1 parent 4441a94 commit 2932d71
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 17 deletions.
14 changes: 12 additions & 2 deletions contracts/interfaces/validator/info-fragments/IValidatorInfoV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ interface IValidatorInfoV2 {
/**
* @dev Returns the current validator list.
*/
function getValidators() external view returns (address[] memory _validatorList);
function getValidators() external view returns (TConsensus[] memory validatorList);

/**
* @dev Returns the ids of current validator list.
*/
function getValidatorIds() external view returns (address[] memory cids);

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

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

/**
* @dev Returns whether the address is block producer or not.
Expand Down
12 changes: 10 additions & 2 deletions contracts/mocks/validator/MockValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ contract MockValidatorSet is

function epochOf(uint256 _block) external view override returns (uint256) {}

function getValidators() external view override returns (address[] memory) {}
function getValidators() external view override returns (TConsensus[] memory) {}

function getValidatorIds() external view override returns (address[] memory) {}

function epochEndingAt(uint256 _block) external view override returns (bool) {}

Expand All @@ -84,7 +86,9 @@ contract MockValidatorSet is
return _numberOfBlocksInEpoch;
}

function getBlockProducers() external view override returns (address[] memory) {}
function getBlockProducers() external view override returns (TConsensus[] memory) {}

function getBlockProducerIds() external view override returns (address[] memory) {}

function isBlockProducer(TConsensus) external pure override returns (bool) {
return true;
Expand Down Expand Up @@ -125,6 +129,10 @@ contract MockValidatorSet is
return IProfile(getContract(ContractType.PROFILE)).getManyConsensus2Id(consensusAddrs);
}

function __cid2cssBatch(address[] memory cids) internal view override returns (TConsensus[] memory) {
return IProfile(getContract(ContractType.PROFILE)).getManyId2Consensus(cids);
}

function execReleaseLockedFundForEmergencyExitRequest(
address _candidateId,
address payable _recipient
Expand Down
2 changes: 1 addition & 1 deletion contracts/ronin/Maintenance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ contract Maintenance is IMaintenance, HasContracts, HasValidatorDeprecated, Init
* @inheritdoc IMaintenance
*/
function totalSchedule() public view override returns (uint256 count) {
address[] memory validatorIds = IRoninValidatorSet(getContract(ContractType.VALIDATOR)).getValidators();
address[] memory validatorIds = IRoninValidatorSet(getContract(ContractType.VALIDATOR)).getValidatorIds();
unchecked {
for (uint i = 0; i < validatorIds.length; i++) {
if (_checkScheduledById(validatorIds[i])) {
Expand Down
5 changes: 4 additions & 1 deletion contracts/ronin/validator/CandidateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ abstract contract CandidateManager is
* @inheritdoc ICandidateManager
*/
function getValidatorCandidates() external view override returns (TConsensus[] memory) {
return IProfile(getContract(ContractType.PROFILE)).getManyId2Consensus(_candidateIds);
return __cid2cssBatch(getValidatorCandidateIds());
}

/**
Expand Down Expand Up @@ -285,4 +285,7 @@ abstract contract CandidateManager is

/// @dev See {RoninValidatorSet-__css2cidBatch}
function __css2cidBatch(TConsensus[] memory consensusAddrs) internal view virtual returns (address[] memory);

/// @dev See {RoninValidatorSet-__cid2cssBatch}
function __cid2cssBatch(address[] memory cids) internal view virtual returns (TConsensus[] memory);
}
4 changes: 2 additions & 2 deletions contracts/ronin/validator/CoinbaseExecution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ abstract contract CoinbaseExecution is
uint256 newPeriod = _computePeriod(block.timestamp);
bool periodEnding = _isPeriodEnding(newPeriod);

address[] memory currValidatorIds = getValidators();
address[] memory currValidatorIds = getValidatorIds();
address[] memory revokedCandidateIds;
uint256 epoch = epochOf(block.number);
uint256 nextEpoch = epoch + 1;
Expand Down Expand Up @@ -410,7 +410,7 @@ abstract contract CoinbaseExecution is
++_i;
}
}
emit BlockProducerSetUpdated(_newPeriod, _nextEpoch, getBlockProducers());
emit BlockProducerSetUpdated(_newPeriod, _nextEpoch, getBlockProducerIds());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions contracts/ronin/validator/EmergencyExit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ abstract contract EmergencyExit is IEmergencyExit, RONTransferHelper, CandidateM
TConsensus[] memory consensusAddrs
) internal view virtual override(CandidateManager, CommonStorage) returns (address[] memory);

/// @dev See {RoninValidatorSet-__cid2cssBatch}
function __cid2cssBatch(
address[] memory cids
) internal view virtual override(CandidateManager, ValidatorInfoStorageV2) returns (TConsensus[] memory);

/**
* @dev See `setEmergencyExitLockedAmount.
*/
Expand Down
9 changes: 9 additions & 0 deletions contracts/ronin/validator/RoninValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ contract RoninValidatorSet is Initializable, CoinbaseExecution, SlashingExecutio
) internal view override(EmergencyExit, CommonStorage) returns (address[] memory) {
return IProfile(getContract(ContractType.PROFILE)).getManyConsensus2Id(consensusAddrs);
}

/**
* @dev Convert many id to corresponding consensus addresses from the Profile contract.
*/
function __cid2cssBatch(
address[] memory cids
) internal view override(EmergencyExit, ValidatorInfoStorageV2) returns (TConsensus[] memory) {
return IProfile(getContract(ContractType.PROFILE)).getManyId2Consensus(cids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ abstract contract ValidatorInfoStorageV2 is IValidatorInfoV2, HasContracts, HasT
/**
* @inheritdoc IValidatorInfoV2
*/
function getValidators() public view override returns (address[] memory validatorList_) {
validatorList_ = new address[](_validatorCount);
function getValidators() public view override returns (TConsensus[] memory consensusList) {
return __cid2cssBatch(getValidatorIds());
}

/**
* @inheritdoc IValidatorInfoV2
*/
function getValidatorIds() public view override returns (address[] memory cids) {
cids = new address[](_validatorCount);
address iValidator;
for (uint i; i < validatorList_.length; ) {
for (uint i; i < cids.length; ) {
iValidator = _validatorIds[i];
validatorList_[i] = iValidator;
cids[i] = iValidator;

unchecked {
++i;
Expand All @@ -53,13 +60,20 @@ abstract contract ValidatorInfoStorageV2 is IValidatorInfoV2, HasContracts, HasT
/**
* @inheritdoc IValidatorInfoV2
*/
function getBlockProducers() public view override returns (address[] memory result) {
result = new address[](_validatorCount);
function getBlockProducers() public view override returns (TConsensus[] memory consensusList) {
return __cid2cssBatch(getBlockProducerIds());
}

/**
* @inheritdoc IValidatorInfoV2
*/
function getBlockProducerIds() public view override returns (address[] memory cids) {
cids = new address[](_validatorCount);
uint256 count = 0;
for (uint i; i < result.length; ) {
for (uint i; i < cids.length; ) {
address validatorId = _validatorIds[i];
if (_isBlockProducerById(validatorId)) {
result[count++] = validatorId;
cids[count++] = validatorId;
}

unchecked {
Expand All @@ -68,7 +82,7 @@ abstract contract ValidatorInfoStorageV2 is IValidatorInfoV2, HasContracts, HasT
}

assembly {
mstore(result, count)
mstore(cids, count)
}
}

Expand Down Expand Up @@ -146,4 +160,7 @@ abstract contract ValidatorInfoStorageV2 is IValidatorInfoV2, HasContracts, HasT

/// @dev See {RoninValidatorSet-__css2cidBatch}
function __css2cidBatch(TConsensus[] memory consensusAddrs) internal view virtual returns (address[] memory);

/// @dev See {RoninValidatorSet-__cid2cssBatch}
function __cid2cssBatch(address[] memory cids) internal view virtual returns (TConsensus[] memory);
}

0 comments on commit 2932d71

Please sign in to comment.