Closed howlbot-integration[bot] closed 3 months ago
adding sponsor disputed: the standard deviation formula for samples does have (N-1) in the denominator, but for populations it has (N) in the denominator. Since the updateGasPriceFromCache
is considering all gas prices currently in the cache, the population standard deviation formula seems more appropriate.
trust1995 marked the issue as unsatisfactory: Invalid
Sponsor explained the conscious choice to use (N) for the denominator.
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-L289The 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