Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RewardThrottle:handleReward gas optimizations #144

Open
code423n4 opened this issue Nov 29, 2021 · 1 comment
Open

RewardThrottle:handleReward gas optimizations #144

code423n4 opened this issue Nov 29, 2021 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

GiveMeTestEther

Vulnerability details

Impact

Storage variable _activeEpoch is read a lot and can be cached in a local variable (epochTmp, maybe choose a better name =)) to save gas. Also the State struct can be loaded into a State storage (currentState, maybe also choose a better name) variable such that we don't have to access the storage array each time.

In the gas optimized code of "Recommended Mitigation Steps" section, the _activeEpoch only gets read once.
Also note after the first "if" we write epoch to the storage variable "_activeEpoch" but then also write epoch to the local var "epochTmp" so we can use this local var in the whole function.

Proof of Concept

https://github.com/code-423n4/2021-11-malt/blob/c3a204a2c0f7c653c6c2dda9f4563fd1dc1cecf3/src/contracts/RewardSystem/RewardThrottle.sol#L63

Tools Used

Recommended Mitigation Steps

function handleReward() public {
uint256 balance = rewardToken.balanceOf(address(this));

uint256 epoch = dao.epoch();
uint256 epochTmp = _activeEpoch;
State storage currentState;

checkRewardUnderflow();

if (epoch > epochTmp) {
  _activeEpoch = epoch;
  epochTmp = epoch;

  currentState = _state[epochTmp];

  currentState.bondedValue = bonding.averageBondedValue(epochTmp); 
  currentState.profit = balance;
  currentState.rewarded = 0;
  currentState.throttle = throttle;
} else {
  currentState = _state[epochTmp];
  currentState.profit = currentState.profit.add(balance);
  currentState.throttle = throttle; 
}

// Fetch targetAPR before we update current epoch state
uint256 aprTarget = targetAPR();

// Distribute balance to the correct places
if (aprTarget > 0 && _epochAprGivenReward(epoch, balance) > aprTarget) {
  uint256 remainder = _getRewardOverflow(balance, aprTarget);
  emit RewardOverflow(epochTmp, remainder);

  if (remainder > 0) {
    rewardToken.safeTransfer(address(overflowPool), remainder);

    if (balance > remainder) {
      _sendToDistributor(balance - remainder, epochTmp);
    }
  }
} else {
  _sendToDistributor(balance, epochTmp);
}

emit HandleReward(epoch, balance);

}

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 29, 2021
code423n4 added a commit that referenced this issue Nov 29, 2021
@0xScotch 0xScotch added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 9, 2021
@GalloDaSballo
Copy link
Collaborator

Agree with the warden, I believe the optimization can be further pushed by caching the storage variable in memory so you only pay for one SLOAD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants