forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gas price estimator (0xPolygonHermez#393)
* implemented simplest gas price estimator * adding default and lastNBlocks gas price estimation strategy * added comments to the function * added setting to default gpe * deleted auto rebases * fixing lint errors * fixing naming DefaultGasPriceWei * deleted redundant field from sequencer config * split gaspriceestimator.go file to different * renamed newXXXEstimator functions names * fixed config names * fixing Federico pr comments * added dots to cmd/run.go comments
- Loading branch information
Showing
21 changed files
with
386 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,6 @@ jobs: | |
go-version: 1.16.x | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Automatic Rebase | ||
uses: cirrus-actions/[email protected] | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
if: github.event_name == 'pull_request' | ||
- name: Lint | ||
run: | | ||
make install-linter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,6 @@ jobs: | |
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Automatic Rebase | ||
uses: cirrus-actions/[email protected] | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
if: github.event_name == 'pull_request' | ||
- name: Install Go | ||
uses: actions/setup-go@v1 | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package gasprice | ||
|
||
import ( | ||
"math/big" | ||
"sync" | ||
) | ||
|
||
// AllBatches struct for all batches avg price strategy. | ||
type AllBatches struct { | ||
// Average gas price (rolling average) | ||
averageGasPrice *big.Int // The average gas price that gets queried | ||
averageGasPriceCount *big.Int // Param used in the avg. gas price calculation | ||
|
||
agpMux sync.Mutex // Mutex for the averageGasPrice calculation | ||
} | ||
|
||
// NewEstimatorAllBatches init gas price estimator for all batches strategy. | ||
func NewEstimatorAllBatches() *AllBatches { | ||
return &AllBatches{ | ||
averageGasPrice: big.NewInt(0), | ||
averageGasPriceCount: big.NewInt(0), | ||
} | ||
} | ||
|
||
// UpdateGasPriceAvg Updates the rolling average value of the gas price. | ||
func (g *AllBatches) UpdateGasPriceAvg(newValue *big.Int) { | ||
g.agpMux.Lock() | ||
|
||
g.averageGasPriceCount.Add(g.averageGasPriceCount, big.NewInt(1)) | ||
|
||
differential := big.NewInt(0) | ||
differential.Div(newValue.Sub(newValue, g.averageGasPrice), g.averageGasPriceCount) | ||
|
||
g.averageGasPrice.Add(g.averageGasPrice, differential) | ||
|
||
g.agpMux.Unlock() | ||
} | ||
|
||
// GetAvgGasPrice get avg gas price from all blocks. | ||
func (g *AllBatches) GetAvgGasPrice() (*big.Int, error) { | ||
return g.averageGasPrice, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package gasprice | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
// EstimatorType different gas estimator types. | ||
type EstimatorType string | ||
|
||
const ( | ||
// DefaultType default gas price from config is set. | ||
DefaultType EstimatorType = "default" | ||
// AllBatchesType calculate average gas used from all batches. | ||
AllBatchesType EstimatorType = "allbatches" | ||
// LastNBatchesType calculate average gas tip from last n batches. | ||
LastNBatchesType EstimatorType = "lastnbatches" | ||
) | ||
|
||
// Config for gas price estimator. | ||
type Config struct { | ||
Type EstimatorType `mapstructure:"Type"` | ||
|
||
DefaultGasPriceWei uint64 `mapstructure:"DefaultGasPriceWei"` | ||
MaxPrice *big.Int `mapstructure:"MaxPrice"` | ||
IgnorePrice *big.Int `mapstructure:"IgnorePrice"` | ||
CheckBlocks int `mapstructure:"CheckBlocks"` | ||
Percentile int `mapstructure:"Percentile"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package gasprice | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/hermeznetwork/hermez-core/state" | ||
) | ||
|
||
// Default gas price from config is set. | ||
type Default struct { | ||
cfg Config | ||
pool pool | ||
} | ||
|
||
// GetAvgGasPrice get default gas price from the pool. | ||
func (d *Default) GetAvgGasPrice() (*big.Int, error) { | ||
ctx := context.Background() | ||
gasPrice, err := d.pool.GetGasPrice(ctx) | ||
if errors.Is(err, state.ErrNotFound) { | ||
return big.NewInt(0), nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return new(big.Int).SetUint64(gasPrice), nil | ||
} | ||
|
||
// UpdateGasPriceAvg not needed for default strategy. | ||
func (d *Default) UpdateGasPriceAvg(newValue *big.Int) {} | ||
|
||
func (d *Default) setDefaultGasPrice() { | ||
ctx := context.Background() | ||
err := d.pool.SetGasPrice(ctx, d.cfg.DefaultGasPriceWei) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to set default gas price, err: %v", err)) | ||
} | ||
} | ||
|
||
// NewDefaultEstimator init default gas price estimator. | ||
func NewDefaultEstimator(cfg Config, pool pool) *Default { | ||
gpe := &Default{cfg: cfg, pool: pool} | ||
gpe.setDefaultGasPrice() | ||
return gpe | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package gasprice | ||
|
||
import "context" | ||
|
||
type pool interface { | ||
SetGasPrice(ctx context.Context, gasPrice uint64) error | ||
GetGasPrice(ctx context.Context) (uint64, error) | ||
} |
Oops, something went wrong.