Skip to content

Commit

Permalink
Fix gas price updater & add good values
Browse files Browse the repository at this point in the history
  • Loading branch information
karlfloersch authored and tynes committed Jun 25, 2021
1 parent 3c16ce0 commit 87c8757
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
6 changes: 5 additions & 1 deletion l2geth/rollup/gas_price_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type GasPriceUpdater struct {
gasPricer L2GasPricer
epochStartBlockNumber uint64
averageBlockGasLimit uint64
epochLengthSeconds uint64
getLatestBlockNumberFn GetLatestBlockNumberFn
updateL2GasPriceFn UpdateL2GasPriceFn
}
Expand All @@ -15,12 +16,14 @@ func NewGasPriceUpdater(
gasPricer *L2GasPricer,
epochStartBlockNumber uint64,
averageBlockGasLimit uint64,
epochLengthSeconds uint64,
getLatestBlockNumberFn GetLatestBlockNumberFn,
updateL2GasPriceFn UpdateL2GasPriceFn,
) *GasPriceUpdater {
return &GasPriceUpdater{
gasPricer: *gasPricer,
epochStartBlockNumber: epochStartBlockNumber,
epochLengthSeconds: epochLengthSeconds,
averageBlockGasLimit: averageBlockGasLimit,
getLatestBlockNumberFn: getLatestBlockNumberFn,
updateL2GasPriceFn: updateL2GasPriceFn,
Expand All @@ -32,7 +35,8 @@ func (g *GasPriceUpdater) UpdateGasPrice() error {
if err != nil {
return err
}
averageGasPerSecond := float64((latestBlockNumber - g.epochStartBlockNumber) * g.averageBlockGasLimit)
averageGasPerSecond := float64((latestBlockNumber - g.epochStartBlockNumber) * g.averageBlockGasLimit / g.epochLengthSeconds)
g.gasPricer.CompleteEpoch(averageGasPerSecond)
g.epochStartBlockNumber = latestBlockNumber
return g.updateL2GasPriceFn(g.gasPricer.curPrice)
}
87 changes: 55 additions & 32 deletions l2geth/rollup/gas_price_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,98 @@ import (
)

type MockEpoch struct {
timestamp float64
numBlocks uint64
}

// WIP
func TestUsageOfGasPriceUpdater(t *testing.T) {
startTimestamp := float64(0)
startGasPerSecond := float64(10)
endTimestamp := float64(100)
endGasPerSecond := float64(100)
mockTimestamp := float64(0) // start at timestamp 0
mockTimeNow := func() float64 {
return mockTimestamp
}
getGasTarget := GetLinearInterpolationFn(mockTimeNow, startTimestamp, endTimestamp, startGasPerSecond, endGasPerSecond)
gasPerSecond := 3300000.0
getGasTarget := func() float64 { return gasPerSecond }
epochLengthSeconds := 10.0
averageBlockGasLimit := 11000000.0
// Based on our 10 second epoch, we are targetting this number of blocks per second
numBlocksToTarget := (epochLengthSeconds * gasPerSecond) / averageBlockGasLimit
fmt.Println("Number of target blocks: ", numBlocksToTarget)

gasPricer := NewGasPricer(1, 1, getGasTarget, 10)

curBlock := uint64(10)
getLatestBlockNumber := func() (uint64, error) { return curBlock, nil }
updateL2GasPrice := func(x float64) error { return nil }
updateL2GasPrice := func(x float64) error {
fmt.Println("new gas price:", x)
return nil
}

// Example loop usage
startBlock, _ := getLatestBlockNumber()
gasUpdater := NewGasPriceUpdater(gasPricer, startBlock, 1, getLatestBlockNumber, updateL2GasPrice)
gasUpdater := NewGasPriceUpdater(gasPricer, startBlock, 11000000, 10, getLatestBlockNumber, updateL2GasPrice)

// In these mock epochs the gas price shold go up and then down again after the time has passed
mockEpochs := []MockEpoch{
// First jack up the price to show that it will grow over time
MockEpoch{
numBlocks: 10,
},
MockEpoch{
numBlocks: 10,
},
MockEpoch{
numBlocks: 10,
},
// Then stabilize around the GPS we want
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
numBlocks: 3,
},
MockEpoch{
timestamp: 0,
numBlocks: 5,
numBlocks: 3,
},
MockEpoch{
timestamp: 0,
numBlocks: 5,
numBlocks: 3,
},
// Then reduce the demand to show the fee goes back down to the floor
MockEpoch{
timestamp: 0,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 0,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 0,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 50,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 50,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 50,
numBlocks: 5,
numBlocks: 1,
},
MockEpoch{
timestamp: 50,
numBlocks: 5,
numBlocks: 1,
},
}
loop := func(epoch MockEpoch) {
mockTimestamp = epoch.timestamp
curBlock += epoch.numBlocks
gasUpdater.UpdateGasPrice()
fmt.Println("gas price:", gasUpdater.gasPricer.curPrice)
}
for _, epoch := range mockEpochs {
loop(epoch)
Expand Down

0 comments on commit 87c8757

Please sign in to comment.