Commit fdbe72d8 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

Merge pull request #1847 from cfromknecht/gas-oracle-rounding

fix: don't round down average gas per epoch
parents d504fbab 9eed33c4
---
'@eth-optimism/gas-oracle': patch
---
fix rounding error in average gas/epoch calculation
......@@ -78,7 +78,7 @@ func (g *GasPriceUpdater) UpdateGasPrice() error {
totalGasUsed += gasUsed
}
averageGasPerSecond := float64(totalGasUsed / g.epochLengthSeconds)
averageGasPerSecond := float64(totalGasUsed) / float64(g.epochLengthSeconds)
log.Debug("UpdateGasPrice", "average-gas-per-second", averageGasPerSecond, "current-price", g.gasPricer.curPrice)
_, err = g.gasPricer.CompleteEpoch(averageGasPerSecond)
......
......@@ -13,7 +13,7 @@ type MockEpoch struct {
// Return a gas pricer that targets 3 blocks per epoch & 10% max change per epoch.
func makeTestGasPricerAndUpdater(curPrice uint64) (*GasPricer, *GasPriceUpdater, func(uint64), error) {
gpsTarget := 3300000.0
gpsTarget := 990000.3
getGasTarget := func() float64 { return gpsTarget }
epochLengthSeconds := uint64(10)
averageBlockGasLimit := uint64(11000000)
......@@ -136,6 +136,13 @@ func TestUsageOfGasPriceUpdater(t *testing.T) {
if prevGasPrice != curPrice {
t.Fatalf("Expected gas price to stablize. Got %d, was %d", curPrice, prevGasPrice)
}
targetGps := gasPriceUpdater.gasPricer.getTargetGasPerSecond()
averageGps := gasPriceUpdater.gasPricer.avgGasPerSecondLastEpoch
if targetGps != averageGps {
t.Fatalf("Average gas/second (%f) did not converge to target (%f)",
averageGps, targetGps)
}
},
},
// Then reduce the demand to show the fee goes back down to the floor
......
......@@ -11,10 +11,11 @@ import (
type GetTargetGasPerSecond func() float64
type GasPricer struct {
curPrice uint64
floorPrice uint64
getTargetGasPerSecond GetTargetGasPerSecond
maxChangePerEpoch float64
curPrice uint64
avgGasPerSecondLastEpoch float64
floorPrice uint64
getTargetGasPerSecond GetTargetGasPerSecond
maxChangePerEpoch float64
}
// LinearInterpolation can be used to dynamically update target gas per second
......@@ -80,6 +81,7 @@ func (p *GasPricer) CompleteEpoch(avgGasPerSecondLastEpoch float64) (uint64, err
return gp, err
}
p.curPrice = gp
p.avgGasPerSecondLastEpoch = avgGasPerSecondLastEpoch
return gp, nil
}
......
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