re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[RWV-05C] Inefficient Lookup Methods #88

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

RWV-05C: Inefficient Lookup Methods

Type Severity Location
Gas Optimization RWAVotingEscrow.sol:L465, L479

Description:

The referenced statements will make use of the Checkpoints::upperLookupRecent functions that are meant to be efficient if the checkpoint being looked up for is recent.

As the functions they are performed in explicitly include the keyword Past, this assumption is incorrect.

Example:

/**
 * @notice Returns the most recent checkpoint (given a timestamp) for total voting power.
 * @param timepoint Timestamp of when to query most recent total voting power.
 */
function getPastTotalVotingPower(uint256 timepoint) external view returns (uint256) {
    VotingEscrowStorage storage $ = _getVotingEscrowStorage();
    uint48 currentTimepoint = clock();
    if (timepoint >= currentTimepoint) {
        revert ERC5805FutureLookup(timepoint, currentTimepoint);
    }
    return $._totalVotingPowerCheckpoints.upperLookupRecent(SafeCast.toUint48(timepoint));
}

/**
 * @notice Returns the most recent voting power checkpoint (given a timestamp) for a specific tokenId.
 * @param tokenId TokenId with historic voting power being fetched.
 * @param timepoint Timestamp of when to query most recent voting power for token.
 */
function getPastVotingPower(uint256 tokenId, uint256 timepoint) external view returns (uint256) {
    VotingEscrowStorage storage $ = _getVotingEscrowStorage();
    uint48 currentTimepoint = clock();
    if (timepoint >= currentTimepoint) {
        revert ERC5805FutureLookup(timepoint, currentTimepoint);
    }
    return $._votingPowerCheckpoints[tokenId].upperLookupRecent(SafeCast.toUint48(timepoint));
}

Recommendation:

We advise either both mechanisms to be exposed by the contract permitting callers to select the one they deem applicable, or the code to by-default utilize the mechanism most efficient for the median use case (i.e. a query in the past).

chasebrownn commented 5 months ago

Acknowledged