Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gp adjust #17

Merged
merged 9 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ Type = "default"
DefaultGasPriceWei = 1000000000
MaxGasPriceWei = 0

#Type = "fixed"
#UpdatePeriod = "10s"
#DefaultGasPriceWei = 1000000000
#KafkaURL = "127.0.0.1:9092"
#Topic = "middle_coinPrice_push"
#GroupID = "web3_okbc_explorerchainprice"
## just in SASL_SSL mode
#Username = ""
#Password = ""
#DefaultL2CoinPrice = 40
#GasPriceUsdt = 0.0001
#L2CoinId = 7184

[MTClient]
URI = "zkevm-prover:50061"

Expand Down
12 changes: 12 additions & 0 deletions gasprice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
LastNBatchesType EstimatorType = "lastnbatches"
// FollowerType calculate the gas price basing on the L1 gasPrice.
FollowerType EstimatorType = "follower"
// FixedType the gas price from config that the unit is usdt
FixedType EstimatorType = "fixed"
zjg555543 marked this conversation as resolved.
Show resolved Hide resolved
)

// Config for gas price estimator.
Expand All @@ -34,5 +36,15 @@ type Config struct {
CleanHistoryPeriod types.Duration `mapstructure:"CleanHistoryPeriod"`
CleanHistoryTimeRetention types.Duration `mapstructure:"CleanHistoryTimeRetention"`

KafkaURL string `mapstructure:"KafkaURL"`
zjg555543 marked this conversation as resolved.
Show resolved Hide resolved
Topic string `mapstructure:"Topic"`
GroupID string `mapstructure:"GroupID"`
Username string `mapstructure:"Username"`
Password string `mapstructure:"Password"`
L2CoinId int `mapstructure:"L2CoinId"`
// DefaultL2CoinPrice is the native token's coin price
DefaultL2CoinPrice float64 `mapstructure:"DefaultL2CoinPrice"`
GasPriceUsdt float64 `mapstructure:"GasPriceUsdt"`

Factor float64 `mapstructure:"Factor"`
}
93 changes: 93 additions & 0 deletions gasprice/fixed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package gasprice

import (
"context"
"fmt"
"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/log"
"math/big"
"strconv"
)

const (
// OKBWei OKB wei
OKBWei = 1e18
minOKBWei = 1e-18
)

// FixedGasPrice struct
type FixedGasPrice struct {
cfg Config
pool poolInterface
ctx context.Context
eth ethermanInterface
ratePrc *KafkaProcessor
}

// newFixedGasPriceSuggester inits l2 fixed price suggester.
func newFixedGasPriceSuggester(ctx context.Context, cfg Config, pool poolInterface, ethMan ethermanInterface) *FixedGasPrice {
gps := &FixedGasPrice{
cfg: cfg,
pool: pool,
ctx: ctx,
eth: ethMan,
ratePrc: newKafkaProcessor(cfg, ctx),
}
gps.UpdateGasPriceAvg()
return gps
}

// UpdateGasPriceAvg updates the gas price.
func (f *FixedGasPrice) UpdateGasPriceAvg() {
ctx := context.Background()
// Get L1 gasprice
l1GasPrice := f.eth.GetL1GasPrice(f.ctx)
if big.NewInt(0).Cmp(l1GasPrice) == 0 {
log.Warn("gas price 0 received. Skipping update...")
return
}

l2CoinPrice := f.ratePrc.GetL2CoinPrice()
if l2CoinPrice < minOKBWei {
log.Warn("the L2 native coin price too small...")
return
}
res := new(big.Float).Mul(big.NewFloat(0).SetFloat64(f.cfg.GasPriceUsdt/l2CoinPrice), big.NewFloat(0).SetFloat64(OKBWei))
// Store l2 gasPrice calculated
result := new(big.Int)
res.Int(result)
minGasPrice := big.NewInt(0).SetUint64(f.cfg.DefaultGasPriceWei)
if minGasPrice.Cmp(result) == 1 { // minGasPrice > result
log.Warn("setting DefaultGasPriceWei for L2")
result = minGasPrice
}
maxGasPrice := new(big.Int).SetUint64(f.cfg.MaxGasPriceWei)
if f.cfg.MaxGasPriceWei > 0 && result.Cmp(maxGasPrice) == 1 { // result > maxGasPrice
log.Warn("setting MaxGasPriceWei for L2")
result = maxGasPrice
}
var truncateValue *big.Int
log.Debug("Full L2 gas price value: ", result, ". Length: ", len(result.String()), ". L1 gas price value: ", l1GasPrice)

numLength := len(result.String())
if numLength > 3 { //nolint:gomnd
aux := "%0" + strconv.Itoa(numLength-3) + "d" //nolint:gomnd
var ok bool
value := result.String()[:3] + fmt.Sprintf(aux, 0)
truncateValue, ok = new(big.Int).SetString(value, encoding.Base10)
if !ok {
log.Error("error converting: ", truncateValue)
}
} else {
truncateValue = result
}
log.Debugf("Storing truncated L2 gas price: %v, L2 native coin price: %v", truncateValue, l2CoinPrice)
if truncateValue != nil {
err := f.pool.SetGasPrices(ctx, truncateValue.Uint64(), l1GasPrice.Uint64())
if err != nil {
log.Errorf("failed to update gas price in poolDB, err: %v", err)
}
} else {
log.Error("nil value detected. Skipping...")
}
}
144 changes: 144 additions & 0 deletions gasprice/fixed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package gasprice

import (
"context"
"math/big"
"testing"
"time"

"github.com/0xPolygonHermez/zkevm-node/config/types"
"github.com/0xPolygonHermez/zkevm-node/log"
)

func init() {
log.Init(log.Config{
Level: "debug",
Outputs: []string{"stdout"},
})
}

func TestUpdateGasPriceFixed(t *testing.T) {
ctx := context.Background()
var d time.Duration = 1000000000

cfg := Config{
Type: FixedType,
DefaultGasPriceWei: 1000000000,
UpdatePeriod: types.NewDuration(d),
Factor: 0.5,
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 40,
GasPriceUsdt: 0.001,
}
l1GasPrice := big.NewInt(10000000000)
l2GasPrice := uint64(25000000000000)
poolM := new(poolMock)
ethM := new(ethermanMock)
ethM.On("GetL1GasPrice", ctx).Return(l1GasPrice).Once()
poolM.On("SetGasPrices", ctx, l2GasPrice, l1GasPrice.Uint64()).Return(nil).Once()
f := newFixedGasPriceSuggester(ctx, cfg, poolM, ethM)

ethM.On("GetL1GasPrice", ctx).Return(l1GasPrice, l1GasPrice).Once()
poolM.On("SetGasPrices", ctx, l2GasPrice, l1GasPrice.Uint64()).Return(nil).Once()
f.UpdateGasPriceAvg()
}

func TestUpdateGasPriceAvgCases(t *testing.T) {
var d time.Duration = 1000000000
testcases := []struct {
cfg Config
l1GasPrice *big.Int
l2GasPrice uint64
}{
{
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 1000000000,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 40,
GasPriceUsdt: 0.001,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(25000000000000),
},
{
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 1000000000,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 1e-19,
GasPriceUsdt: 0.001,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(25000000000000),
},
{ // the gas price small than the min gas price
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 26000000000000,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 40,
GasPriceUsdt: 0.001,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(26000000000000),
},
{ // the gas price bigger than the max gas price
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 1000000000000,
MaxGasPriceWei: 23000000000000,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 40,
GasPriceUsdt: 0.001,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(23000000000000),
},
{
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 1000000000,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 30,
GasPriceUsdt: 0.001,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(33300000000000),
},
{
cfg: Config{
Type: FixedType,
DefaultGasPriceWei: 10,
UpdatePeriod: types.NewDuration(d),
KafkaURL: "127.0.0.1:9092",
Topic: "middle_coinPrice_push",
DefaultL2CoinPrice: 30,
GasPriceUsdt: 1e-15,
},
l1GasPrice: big.NewInt(10000000000),
l2GasPrice: uint64(33),
},
}

for _, tc := range testcases {
ctx := context.Background()
poolM := new(poolMock)
ethM := new(ethermanMock)
ethM.On("GetL1GasPrice", ctx).Return(tc.l1GasPrice).Twice()
poolM.On("SetGasPrices", ctx, tc.l2GasPrice, tc.l1GasPrice.Uint64()).Return(nil).Twice()
f := newFixedGasPriceSuggester(ctx, tc.cfg, poolM, ethM)
f.UpdateGasPriceAvg()
zjg555543 marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions gasprice/gaspricesuggester.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func NewL2GasPriceSuggester(ctx context.Context, cfg Config, pool poolInterface,
case DefaultType:
log.Info("Default type selected")
gpricer = newDefaultGasPriceSuggester(ctx, cfg, pool)
case FixedType:
log.Info("Fixed type selected")
gpricer = newFixedGasPriceSuggester(ctx, cfg, pool, ethMan)
default:
log.Fatal("unknown l2 gas price suggester type ", cfg.Type, ". Please specify a valid one: 'lastnbatches', 'follower' or 'default'")
}
Expand Down
Loading