Gas Price Overestimation Due to Incorrect Standard Deviation Calculation. #108
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
🤖_primary
AI based primary recommendation
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/code-423n4/2024-06-thorchain/blob/e3fd3c75ff994dce50d6eb66eb290d467bd494f5/bifrost/pkg/chainclients/ethereum/ethereum_block_scanner.go#L281-L289
Vulnerability details
Impact
If the standard deviation is underestimated due to the missing
(n - 1)
division, the calculated gas price (mean + 3x standard deviation
) may be higher than necessary. This can result in overpaying for gas and wasting resources.@>/bifrost/pkg/chainclients/ethereum/ethereum_block_scanner.go#L282-L289
The issue is that the standard deviation is being calculated incorrectly. The correct formula for standard deviation is.
However, in the code, the division by
(n - 1)
is missing. Instead, it divides byn
(which ise.cfg.GasCacheBlocks
).Proof of Concept
Suppose the gas prices in the cache are as follows: [100, 120, 110, 130, 90]. The correct calculations would be:
However, with the missing (n - 1) division, the incorrect calculation would be:
As a result, the estimated gas price would be:
110 + 3 * 15.81 ≈ 157.43
110 + 3 * 14.14 ≈ 152.42
The difference in the estimated gas prices can lead to the impacts mentioned above, such as overpaying or underpaying for gas and inconsistent behavior.
Tools Used
Vs
Recommended Mitigation Steps
By dividing by
(e.cfg.GasCacheBlocks - 1)
instead ofe.cfg.GasCacheBlocks
, the standard deviation will be calculated correctly using the unbiased estimator.Assessed type
Math
The text was updated successfully, but these errors were encountered: