Commit 75080ba4 authored by Mark Tyneway's avatar Mark Tyneway

contracts-bedrock: prevent overflows in ResourceMetering

When multiplying a uint64 and uint128 and assigning to
a uint256, the solidity checked math applies to the
types of the numbers being multiplied. This means that
solidity catches an overflow when the result of larger than
a uint128 even though its assigned to a uint256.
This PR uses unchecked math to prevent this kind of overflow
instead of casting the values to uint256 because it is
safe to use unchecked math here. The following inequality
always holds true, which shows that it is safe:

``` solidity
type(uint64).max * type(uint128).max < type(uint256).max
```
parent 68c357c5
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -134,7 +134,14 @@ abstract contract ResourceMetering is Initializable {
);
// Determine the amount of ETH to be paid.
uint256 resourceCost = _amount * params.prevBaseFee;
// Safety: _amount is a uint64
// params.prevBaseFee is a uint128
// resourceCost is a uint256
// type(uint64).max * type(uint128).max < type(uint256).max
uint256 resourceCost;
unchecked {
resourceCost = _amount * params.prevBaseFee;
}
// We currently charge for this ETH amount as an L1 gas burn, so we convert the ETH amount
// into gas by dividing by the L1 base fee. We assume a minimum base fee of 1 gwei to avoid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment