code-423n4 / 2021-10-union-findings

0 stars 0 forks source link

caching multiple used variables #106

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

pants

Vulnerability details

In Treasury.editSchedule

function editSchedule(
    uint256 dripStart_,
    uint256 dripRate_,
    address target_,
    uint256 amount_
) public onlyAdmin {
    require(tokenSchedules[target_].target != address(0), "Target schedule doesn't exist");
    tokenSchedules[target_].dripStart = dripStart_;
    tokenSchedules[target_].dripRate = dripRate_;
    tokenSchedules[target_].amount = amount_;
}

We suggest to cache tokenSchedules[target_] at start and then use the cached value to save repeated access to a storage state variable.

GalloDaSballo commented 3 years ago

Am not convinced that caching the tokenSchedules[target_] would actually save the gas of a storage read as implied by the warden

The tokenSchedules variable is fundamentally a pointer to storage, the position is calculated via KECCAK256 of the name of the variable and then the offset is calculated using the size of the struct and further offset based on the type of variable for target

I'm guessing by saving the pointer you'd save the extra computation necessary to recompute the pointer

Will agree in principle but would like to see a POC of the gas savings