The restart function in the Yeet contract and the addYeetback function in the Yeetback contract both calculate the entropy fee, resulting in redundant operations and unnecessary gas consumption.
Vulnerability Details
In the restart function, yeetback.getEntropyFee() is called to get the fee and also verify that sufficient ETH is provided to cover the entropy fee. Later, when calling yeetback.addYeetback, the fee is passed along with the potToYeetback amount. Inside the addYeetback function, getEntropyFee() is called again before using the fee to request entropy.
This double calculation is unnecessary and consumes additional gas. The fee calculation should be performed once and then reused.
This inefficiency increases the gas cost for users who call the restart function. While not a security vulnerability, it represents a suboptimal implementation that costs users additional gas.
References
Add any relevant links to documentation or code
Proof of Concept
Proof of Concept
In yeet.test.sol, under the Yeet_Claim contract, Copy and paste the following test and run it:
forge test --mt test_gas_Usage -vvvv
function test_gas_Usage() public {
yeet.yeet{value: 1 ether}();
skip(2 hours);
bytes32 randomNumber = 0x3b67d060cb9b8abcf5d29e15600b152af66a881e8867446e798f5752845be90d;
uint128 fee = yeet.yeetback().getEntropyFee();
uint256 startGas = gasleft();
yeet.restart{value: fee}(randomNumber);
uint256 endGas = gasleft();
console.log("Gas used for restart:", startGas - endGas);
}
Results:
├─ [0] console::log("Gas used for restart:", 239126 [2.391e5]) [staticcall]
The original restart function cost 239,126 gas.
Now, edit the addYeetback function in Yeetback.sol, replacing:
uint256 fee = getEntropyFee(); // redundant call
with:
uint256 fee = msg.value - amount;
and ran the test again.
New results:
├─ [0] console::log("Gas used for restart:", 238149 [2.381e5]) [staticcall]
The modified restart function now costs 238,149 gas, saving 977 gas.