code-423n4 / 2021-11-overlay-findings

1 stars 0 forks source link

OverlayV1OVLCollateral.getCurrentBlockPositionId does excessive storage mapping read #136

Closed code423n4 closed 2 years ago

code423n4 commented 3 years ago

Handle

hyh

Vulnerability details

Impact

Gas is overspent on storage access.

Proof of Concept

https://github.com/code-423n4/2021-11-overlay/blob/main/contracts/collateral/OverlayV1OVLCollateral.sol#L177

Recommended Mitigation Steps

Access exact part of storage that is needed.

Now:

mapping(uint=>uint) storage _currentBlockPositions = _isLong
        ? currentBlockPositionsLong[_market]
        : currentBlockPositionsShort[_market];
positionId_ = _currentBlockPositions[_leverage];
...
_currentBlockPositions[_leverage] = positionId_;

To be:

uint storage _currentBlockPosition = _isLong
        ? currentBlockPositionsLong[_market][_leverage]
        : currentBlockPositionsShort[_market][_leverage];
positionId_ = _currentBlockPosition;
...
_currentBlockPosition = positionId_;
mikeyrf commented 2 years ago

sponsor disputed reason - suggested change seems to actually be more gas intensive

mesozoic-technology commented 2 years ago

When I tried this the way proposed, I actually encountered a greater gas expenditure.