code4rena-2022-08-nounsdao-g08

[G‑08] internal functions only called once can be inlined to save gas

보고서

Summary

한군데에서만 호출되는 internal 함수의 경우 함수를 쪼개지 말고, 그냥 코드를 합치는 게 가스 절약적이라고 제안했다.

Keyword

gas optimization, internal

Vulnerability

File: contracts/governance/NounsDAOLogicV1.sol
 
294       function queueOrRevertInternal(
295           address target,
296           uint256 value,
297           string memory signature,
298           bytes memory data,
299:          uint256 eta
 
676:      function getChainIdInternal() internal view returns (uint256) {
File: contracts/governance/NounsDAOLogicV2.sol
 
305       function queueOrRevertInternal(
306           address target,
307           uint256 value,
308           string memory signature,
309           bytes memory data,
310:          uint256 eta
 
866:      function proposalCreationBlock(Proposal storage proposal) internal view returns (uint256) {
 
974:      function _refundGas(uint256 startGas) internal {
 
1010:     function getChainIdInternal() internal view returns (uint256) {
File: contracts/governance/NounsDAOProxy.sol
 
94:       function delegateTo(address callee, bytes memory data) internal {

한군데에서만 호출되는 internal 함수의 경우 함수를 쪼개지 말고, 그냥 internal 함수를 호출하는 쪽으로 코드를 합치는 게 낫다고 제안했다.

internal 함수를 호출하기 위해서는 JUMP 인스트럭션을 이용해야 하며 스택 확보 작업이 필요한데, 이에 의해 20~40 gas가 낭비된다.

Impact

internal 함수를 호출하기 위해 불필요한 gas가 낭비된다.

Mitigation

internal 함수의 코드를 caller측에 inline으로 쓴다.

Memo

클린 코드 입장에서는 쪼개는 쪽이 나은 것 같은데… 판단은 나의 몫이 아니며 그냥 제안을 할 뿐인가..


tags: bughunting, nouns dao, smart contract, solidity, gas optimization, gas, solidity internal, severity gas