code4rena-2022-08-nounsdao-g02

[G‑02] Multiple address/ID mappings can be combined into a single mapping of an address/ID to a struct, where appropriate

보고서

Summary

동일한 key를 이용하는 여러개의 mapping 변수는 struct을 이용하여 하나의 mapping 변수에 합치라고 제안했다.

Keyword

gas optimization, storage optimization, struct

Vulnerability

File: contracts/base/ERC721Checkpointable.sol
 
53        mapping(address => mapping(uint32 => Checkpoint)) public checkpoints;
54    
55        /// @notice The number of checkpoints for each account
56:       mapping(address => uint32) public numCheckpoints;

동일한 key를 이용하는 여러개의 mapping 변수가 있다면, struct을 이용해 하나의 mapping 변수에 넣을 수 있다. 이를 통해 storage slot을 절약할 수 있고, 상황에 따라 gas를 절약할 수 있다. 만약 함수가 합쳐진 두 값이 동시에 필요한 경우가 많다면 읽기나 쓰기에 들어가는 비용이 줄어들 수 있다. 합쳐진 두 값이 동시에 필요한 경우, mapping 의 slot 찾기 계산을 1번만 하면 되기 때문에, keccak256 실행 횟수가 줄어든다. 이를 통해 엑세스 당 30~42 gas를 절약할 수 있다. (Gkeccak256 - 30 gas)

Impact

storage slot과 gas가 낭비된다.

Mitigation

struct을 이용하여 하나의 mapping 변수로 합친다.


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