code4rena-2022-08-nounsdao-g06

[G‑06] State variables should be cached in stack variables rather than re-reading them from storage

보고서

Summary

state 변수를 매번 storage에서 읽는 것이 가스 낭비적이라 지적했다. 지역 변수에 캐싱하여 가스를 절약하라 제안했다.

Keyword

gas optimization, storage, memory

Vulnerability

File: /contracts/governance/NounsDAOLogicV2.sol
 
/// @audit proposal.proposer on line 351
352:                 nouns.getPriorVotes(proposal.proposer, block.number - 1) < proposal.proposalThreshold,
File: /contracts/governance/NounsDAOLogicV1.sol
 
/// @audit proposal.proposer on line 340
341:                 nouns.getPriorVotes(proposal.proposer, block.number - 1) < proposal.proposalThreshold,
File: contracts/governance/NounsDAOLogicV1.sol
 
/// @audit vetoer on line 364
365:          require(msg.sender == vetoer, 'NounsDAO::veto: only vetoer');
 
/// @audit vetoer on line 638
640:          emit NewVetoer(vetoer, newVetoer);
 
/// @audit votingDelay on line 536
538:          emit VotingDelaySet(oldVotingDelay, votingDelay);
 
/// @audit votingPeriod on line 552
554:          emit VotingPeriodSet(oldVotingPeriod, votingPeriod);
 
/// @audit proposalThresholdBPS on line 570
572:          emit ProposalThresholdBPSSet(oldProposalThresholdBPS, proposalThresholdBPS);
 
/// @audit quorumVotesBPS on line 587
589:          emit QuorumVotesBPSSet(oldQuorumVotesBPS, quorumVotesBPS);
 
/// @audit proposalCount on line 216
217:          Proposal storage newProposal = proposals[proposalCount];
 
/// @audit proposalCount on line 217
219:          newProposal.id = proposalCount;
File: contracts/governance/NounsDAOProxy.sol
 
/// @audit implementation on line 83
85:           emit NewImplementation(oldImplementation, implementation);

state 변수를 매번 storage에서 읽는 것이 가스 낭비적이라 지적했다. state 변수를 한 번 읽는다면 이후에는 Gwarmaccess (100 gas)를 이용해 읽어온다. (컴파일러가 최적화 가능한 상황이라면) 컴파일러가 최적화할 수 없는 상황이라면 직접 stack에 캐싱해야 한다.

Impact

storage 변수를 반복적으로 로드하여 가스를 낭비한다.

Mitigation

로컬 캐시를 만들어 storage 변수를 매번 로드하지 않는다.


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