Commit 9eed33c4 authored by Conner Fromknecht's avatar Conner Fromknecht

fix: don't round down average gas per epoch

Also corrects the targetGps constant in the test harness. The constant
was chosen to be the expected Gps of the initial burst, causing the Gps
to stablize in the second part of the test at the floor.

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