The distributeProtocolAssetReward
function in the MarketMakingEngineConfiguration
contract has a potential gas issue due to the large number of fee recipients being processed. The function fails with an "OutOfGas" error when the number of fee recipients increases significantly.
Severity: High
Impact: High
Likelihood: Medium
The issue arises from the for-loop in the distributeProtocolAssetReward
function that iterates over all the fee recipients. When there is a large number of fee recipients (e.g., 12,000 in this case), the gas cost to process the loop and transfer the rewards becomes too high, causing the transaction to fail due to the out-of-gas error. This results in the function not being able to execute successfully.
Root Cause: High gas cost for looping over a large number of fee recipients.
Impact: The function may fail to distribute protocol rewards properly when the number of recipients is large, making the contract's functionality unreliable in cases with many fee recipients.
Denial of Service (DoS): The inability to distribute rewards to a large number of recipients may lead to service denial, as the contract may become stuck or fail to execute on such calls.
Operational Risk: This can affect users relying on the reward distribution mechanism for governance or other incentives, potentially leading to economic losses or contract failure.
Steps to Reproduce:
Deploy the MarketMakingEngineConfiguration
contract.
Add a large number of fee recipients (12,000 in this case).
Attempt to distribute protocol asset rewards to these recipients using the distributeProtocolAssetReward
function.
Test Code:
Test Result:
The test case failed with an "OutOfGas" error when executing the setUp()
function, indicating that the gas usage exceeded the limit due to the large number of fee recipients.
Manual Review
Foundry
Batch Processing: Instead of processing all fee recipients in a single transaction, implement batch processing or pagination for distributing rewards. This will reduce the gas cost per transaction and allow the contract to process rewards in multiple steps if necessary.
Gas Optimization: Consider optimizing the gas consumption in the loop, such as by caching values that do not change across iterations or optimizing the reward calculation logic.
Gas Limit Monitoring: Implement proper gas limit checks and reverts to handle cases where the gas consumption might exceed the block's gas limit, ensuring that transactions fail gracefully and don't hang.
Testing: Perform additional tests with varying numbers of fee recipients to evaluate the gas impact and ensure that the function can handle larger configurations without running into gas limit issues.
Batch Processing: Instead
Here’s an enhanced test suite that demonstrates the vulnerability and provides clear expectations for judges:
forge test test/foundry/MarketMakingEngineConfiguration.t.sol
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.