Skip to content

Commit

Permalink
feat: made getstate and getepoch functions public (#795)
Browse files Browse the repository at this point in the history
* feat: made getstate and getepoch function public

* fix: linting errors

* fix: fixed naming issue in RandomNoManager
  • Loading branch information
GauravJain9 committed May 11, 2022
1 parent 8e93247 commit 25d0cd1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions contracts/Core/BlockManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ contract BlockManager is Initializable, BlockStorage, StateManager, BlockManager
* and is valid. This will confirm the block and rewards the selected staker with the block reward
*/
function claimBlockReward() external initialized checkState(State.Confirm, buffer) {
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
uint32 stakerId = stakeManager.getStakerId(msg.sender);
require(stakerId > 0, "Structs.Staker does not exist");
// slither-disable-next-line timestamp
Expand All @@ -195,7 +195,7 @@ contract BlockManager is Initializable, BlockStorage, StateManager, BlockManager

/// @inheritdoc IBlockManager
function confirmPreviousEpochBlock(uint32 stakerId) external override initialized onlyRole(BLOCK_CONFIRMER_ROLE) {
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();

if (sortedProposedBlockIds[epoch - 1].length != 0 && blockIndexToBeConfirmed != -1) {
_confirmBlock(epoch - 1, stakerId);
Expand Down
8 changes: 4 additions & 4 deletions contracts/Core/CollectionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ contract CollectionManager is Initializable, CollectionStorage, StateManager, Co
require(jobs[jobID].id == jobID, "Job ID not present");
require(weight <= 100, "Weight beyond max");

uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();

jobs[jobID].url = url;
jobs[jobID].selector = selector;
Expand All @@ -156,7 +156,7 @@ contract CollectionManager is Initializable, CollectionStorage, StateManager, Co
require(id <= numCollections, "ID does not exist");
require(assetStatus != collections[id].active, "status not being changed");

uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();

// slither-disable-next-line incorrect-equality,timestamp
if (updateRegistryEpoch <= epoch) {
Expand Down Expand Up @@ -196,7 +196,7 @@ contract CollectionManager is Initializable, CollectionStorage, StateManager, Co
require(jobIDs.length > 0, "no jobs added");
require(tolerance <= maxTolerance, "Invalid tolerance value");

uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();

// slither-disable-next-line incorrect-equality,timestamp
if (updateRegistryEpoch <= epoch) {
Expand Down Expand Up @@ -235,7 +235,7 @@ contract CollectionManager is Initializable, CollectionStorage, StateManager, Co
require(collectionID <= numCollections, "Collection ID not present");
require(collections[collectionID].active, "Collection is inactive");
require(tolerance <= maxTolerance, "Invalid tolerance value");
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
collections[collectionID].power = power;
collections[collectionID].tolerance = tolerance;
collections[collectionID].aggregationMethod = aggregationMethod;
Expand Down
22 changes: 11 additions & 11 deletions contracts/Core/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
* @param stakerId The Id of staker whom you want to delegate
*/
function delegate(uint32 stakerId, uint256 amount) external initialized whenNotPaused {
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
require(stakers[stakerId].acceptDelegation, "Delegetion not accpected");
// slither-disable-next-line timestamp
require(_isStakerActive(stakerId, epoch), "Staker is inactive");
Expand Down Expand Up @@ -321,7 +321,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
require(stakers[stakerId].stake > 0, "Nonpositive stake");
// slither-disable-next-line timestamp
require(locks[msg.sender][stakers[stakerId].tokenAddress][LockType.Unstake].amount == 0, "Existing Unstake Lock");
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
Structs.Staker storage staker = stakers[stakerId];
IStakedToken sToken = IStakedToken(staker.tokenAddress);

Expand All @@ -339,15 +339,15 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
* @param stakerId The Id of staker associated with sRZR which user want to initiateWithdraw
*/
function initiateWithdraw(uint32 stakerId) external initialized whenNotPaused {
State currentState = _getState(buffer);
State currentState = getState(buffer);
// slither-disable-next-line timestamp
require(currentState != State.Propose, "Unstake: NA Propose");
// slither-disable-next-line timestamp
require(currentState != State.Dispute, "Unstake: NA Dispute");

require(stakerId != 0, "staker doesnt exist");
// slither-disable-next-line timestamp
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
Structs.Staker storage staker = stakers[stakerId];
Structs.Lock storage lock = locks[msg.sender][staker.tokenAddress][LockType.Unstake];
require(lock.unlockAfter != 0, "Did not unstake");
Expand Down Expand Up @@ -376,7 +376,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
*/
function unlockWithdraw(uint32 stakerId) external initialized whenNotPaused {
// slither-disable-next-line timestamp
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
require(stakerId != 0, "staker doesnt exist");

Structs.Staker storage staker = stakers[stakerId];
Expand All @@ -402,7 +402,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
uint32 stakerId = stakerIds[msg.sender];
require(stakerId != 0, "staker doesnt exist");
require(stakers[stakerId].stakerReward != 0, "no stakerReward to transfer");
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
uint256 stakerRewardToBeClaimed = stakers[stakerId].stakerReward;
_setStakerReward(epoch, stakerId, StakerRewardChanged.StakerRewardClaimed, stakers[stakerId].stakerReward, 0);
require(razor.transfer(msg.sender, stakerRewardToBeClaimed), "couldnt transfer");
Expand Down Expand Up @@ -446,7 +446,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
require(commission <= maxCommission, "Commission exceeds maxlimit");
uint32 stakerId = stakerIds[msg.sender];
require(stakerId != 0, "staker id = 0");
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
if (stakers[stakerId].epochCommissionLastUpdated != 0) {
// slither-disable-next-line timestamp
require((stakers[stakerId].epochCommissionLastUpdated + epochLimitForUpdateCommission) <= epoch, "Invalid Epoch For Updation");
Expand All @@ -464,7 +464,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
*/
function resetUnstakeLock(uint32 stakerId) external initialized whenNotPaused {
// Lock should be expired if you want to extend
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
// slither-disable-next-line timestamp
require(locks[msg.sender][stakers[stakerId].tokenAddress][LockType.Unstake].amount != 0, "Unstake Lock doesnt exist");
require(locks[msg.sender][stakers[stakerId].tokenAddress][LockType.Withdraw].unlockAfter == 0, "Withdraw Lock exists");
Expand All @@ -480,7 +480,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
lock.amount = lock.amount - penalty;
staker.stake = staker.stake - rPenalty;
lock.unlockAfter = epoch + unstakeLockPeriod;
emit ResetUnstakeLock(stakerId, msg.sender, _getEpoch());
emit ResetUnstakeLock(stakerId, msg.sender, getEpoch());
// Ignoring below line for testing as this is standard erc20 function
require(sToken.burn(address(this), penalty), "Token burn Failed");
}
Expand Down Expand Up @@ -551,7 +551,7 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
* @param bountyId The ID of the bounty
*/
function redeemBounty(uint32 bountyId) external {
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
uint256 bounty = bountyLocks[bountyId].amount;

require(msg.sender == bountyLocks[bountyId].bountyHunter, "Incorrect Caller");
Expand Down Expand Up @@ -722,6 +722,6 @@ contract StakeManager is Initializable, StakeStorage, StateManager, Pause, Stake
function _resetLock(uint32 stakerId) private {
locks[msg.sender][stakers[stakerId].tokenAddress][LockType.Unstake] = Structs.Lock({amount: 0, unlockAfter: 0});
locks[msg.sender][stakers[stakerId].tokenAddress][LockType.Withdraw] = Structs.Lock({amount: 0, unlockAfter: 0});
emit ResetLock(stakerId, msg.sender, _getEpoch());
emit ResetLock(stakerId, msg.sender, getEpoch());
}
}
14 changes: 7 additions & 7 deletions contracts/Core/StateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract StateManager is Constants {
*/
modifier checkEpoch(uint32 epoch) {
// slither-disable-next-line incorrect-equality
require(epoch == _getEpoch(), "incorrect epoch");
require(epoch == getEpoch(), "incorrect epoch");
_;
}

Expand All @@ -22,7 +22,7 @@ contract StateManager is Constants {
*/
modifier checkState(State state, uint8 buffer) {
// slither-disable-next-line incorrect-equality
require(state == _getState(buffer), "incorrect state");
require(state == getState(buffer), "incorrect state");
_;
}

Expand All @@ -31,7 +31,7 @@ contract StateManager is Constants {
*/
modifier notState(State state, uint8 buffer) {
// slither-disable-next-line incorrect-equality
require(state != _getState(buffer), "incorrect state");
require(state != getState(buffer), "incorrect state");
_;
}

Expand All @@ -44,23 +44,23 @@ contract StateManager is Constants {
uint8 buffer
) {
// slither-disable-next-line incorrect-equality
require(epoch == _getEpoch(), "incorrect epoch");
require(epoch == getEpoch(), "incorrect epoch");
// slither-disable-next-line incorrect-equality
require(state == _getState(buffer), "incorrect state");
require(state == getState(buffer), "incorrect state");
_;
}

/**
* @return the value of current epoch
*/
function _getEpoch() internal view returns (uint32) {
function getEpoch() public view returns (uint32) {
return (uint32(block.timestamp) / (EPOCH_LENGTH));
}

/**
* @return the value of current state
*/
function _getState(uint8 buffer) internal view returns (State) {
function getState(uint8 buffer) public view returns (State) {
uint8 lowerLimit = buffer;

uint16 upperLimit = EPOCH_LENGTH / NUM_STATES - buffer;
Expand Down
6 changes: 3 additions & 3 deletions contracts/randomNumber/RandomNoManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ contract RandomNoManager is Initializable, StateManager, RandomNoStorage, Random

/// @inheritdoc IRandomNoClient
function register() external override initialized returns (bytes32 requestId) {
uint32 epoch = _getEpoch();
State state = _getState(buffer);
uint32 epoch = getEpoch();
State state = getState(buffer);
nonce[msg.sender] = nonce[msg.sender] + 1;
requestId = keccak256(abi.encodePacked(nonce[msg.sender], msg.sender));
// slither-disable-next-line incorrect-equality,timestamp
Expand Down Expand Up @@ -56,7 +56,7 @@ contract RandomNoManager is Initializable, StateManager, RandomNoStorage, Random

/// @inheritdoc IRandomNoClient
function getGenericRandomNumberOfLastEpoch() external view override returns (uint256) {
uint32 epoch = _getEpoch();
uint32 epoch = getEpoch();
return _generateRandomNumber(epoch - 1, 0);
}

Expand Down

0 comments on commit 25d0cd1

Please sign in to comment.