subtract by the overhead from running gasleft() (#74)

* subtract by the overhead from running gasleft()

* Rename variable

* forge fmt

* Clamp to the upper gas limit

* Forge fmt

* Update ethereum/contracts/coreRelayer/CoreRelayer.sol

Co-authored-by: scnale <sebinale@gmail.com>

---------

Co-authored-by: scnale <sebinale@gmail.com>
This commit is contained in:
derpy-duck 2023-02-01 13:20:39 -05:00 committed by GitHub
parent 20bb4c7f8e
commit fc06699266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -417,11 +417,19 @@ contract CoreRelayer is CoreRelayerGovernance {
}(abi.encodeWithSignature("receiveWormholeMessages(bytes[],bytes[])", encodedVMs, new bytes[](0)));
uint256 postGas = gasleft();
// There's no easy way to measure the exact cost of the CALL instruction.
// This is due to the fact that the compiler probably emits DUPN or MSTORE instructions
// to setup the arguments for the call just after our measurement.
// This means the refund could be off by a few units of gas.
// Thus, we ensure the overhead doesn't cause an overflow in our refund formula here.
uint256 gasUsed = (preGas - postGas) > internalInstruction.executionParameters.gasLimit
? internalInstruction.executionParameters.gasLimit
: (preGas - postGas);
// refund unused gas budget
uint256 weiToRefund = internalInstruction.applicationBudgetTarget;
if (success) {
weiToRefund = (internalInstruction.executionParameters.gasLimit - (preGas - postGas))
weiToRefund = (internalInstruction.executionParameters.gasLimit - gasUsed)
* internalInstruction.maximumRefundTarget / internalInstruction.executionParameters.gasLimit;
}