From 685c0b0eff5c25c9a635f625516cd62df42f0393 Mon Sep 17 00:00:00 2001 From: Niven Date: Thu, 4 Jul 2024 14:02:36 +0800 Subject: [PATCH 1/6] Refactor configs and flags, fix gp defaults --- cmd/rpcdaemon/commands/eth_api.go | 16 +- cmd/rpcdaemon/commands/eth_system_xlayer.go | 18 +-- cmd/rpcdaemon/commands/eth_system_zk.go | 4 +- cmd/utils/flags.go | 127 +-------------- cmd/utils/flags_xlayer.go | 145 ++++++++++++++++++ eth/ethconfig/config.go | 2 + eth/gasprice/fixed_xlayer.go | 4 +- eth/gasprice/fixed_xlayer_test.go | 127 ++++++++------- eth/gasprice/follower_xlayer.go | 8 +- eth/gasprice/follower_xlayer_test.go | 24 +-- eth/gasprice/gaspricecfg/config_xlayer.go | 42 +++++ eth/gasprice/gaspricecfg/gaspricecfg.go | 34 +--- ...ggester.go => gaspricesuggester_xlayer.go} | 4 +- eth/gasprice/kafka_proc_xlayer.go | 6 +- eth/gasprice/kafka_proc_xlayer_test.go | 24 +-- 15 files changed, 321 insertions(+), 264 deletions(-) create mode 100644 cmd/utils/flags_xlayer.go create mode 100644 eth/gasprice/gaspricecfg/config_xlayer.go rename eth/gasprice/{gaspricesuggester.go => gaspricesuggester_xlayer.go} (90%) diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index 466a7351ec8..f7bf04b00dd 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -348,8 +348,10 @@ type APIImpl struct { MaxGasPrice uint64 GasPriceFactor float64 L1GasPrice L1GasPrice - L2GasPircer gasprice.L2GasPricer - EnableInnerTx bool // XLayer + + // For X Layer + L2GasPricer gasprice.L2GasPricer + EnableInnerTx bool } // NewEthAPI returns APIImpl instance @@ -376,12 +378,14 @@ func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpoo MaxGasPrice: ethCfg.MaxGasPrice, GasPriceFactor: ethCfg.GasPriceFactor, L1GasPrice: L1GasPrice{}, - L2GasPircer: gasprice.NewL2GasPriceSuggester(context.Background(), ethconfig.Defaults.GPO), - EnableInnerTx: ethCfg.EnableInnerTx, + // For X Layer + L2GasPricer: gasprice.NewL2GasPriceSuggester(context.Background(), ethCfg.GPO), + EnableInnerTx: ethCfg.EnableInnerTx, } - // set default gas price - apii.gasCache.SetLatest(common.Hash{}, apii.L2GasPircer.GetConfig().Default) + // For X Layer + // Set default gas price + apii.gasCache.SetLatest(common.Hash{}, apii.L2GasPricer.GetConfig().Default) go apii.runL2GasPriceSuggester() return apii diff --git a/cmd/rpcdaemon/commands/eth_system_xlayer.go b/cmd/rpcdaemon/commands/eth_system_xlayer.go index bf318b5fe28..9ad7e2914ff 100644 --- a/cmd/rpcdaemon/commands/eth_system_xlayer.go +++ b/cmd/rpcdaemon/commands/eth_system_xlayer.go @@ -35,7 +35,7 @@ func (api *APIImpl) gasPriceXL(ctx context.Context) (*hexutil.Big, error) { price, err := api.getGPFromTrustedNode() if err != nil { log.Error("eth_gasPrice error: ", err) - return (*hexutil.Big)(api.L2GasPircer.GetConfig().Default), nil + return (*hexutil.Big)(api.L2GasPricer.GetConfig().Default), nil } return (*hexutil.Big)(price), nil @@ -62,7 +62,7 @@ func (api *APIImpl) gasPriceNonRedirectedXL(ctx context.Context) (*hexutil.Big, gasResult.Add(tipcap, head.BaseFee) } - rgp := api.L2GasPircer.GetLastRawGP() + rgp := api.L2GasPricer.GetLastRawGP() if gasResult.Cmp(rgp) < 0 { gasResult = new(big.Int).Set(rgp) } @@ -87,7 +87,7 @@ func (api *APIImpl) isCongested(ctx context.Context) bool { return false } - isPendingTxCongested := int(poolStatus.PendingCount) >= api.L2GasPircer.GetConfig().CongestionThreshold + isPendingTxCongested := int(poolStatus.PendingCount) >= api.L2GasPricer.GetConfig().XLayer.CongestionThreshold return !isLatestBlockEmpty && isPendingTxCongested } @@ -125,16 +125,16 @@ func (api *APIImpl) getGPFromTrustedNode() (*big.Int, error) { } func (api *APIImpl) runL2GasPriceSuggester() { - cfg := api.L2GasPircer.GetConfig() - ctx := api.L2GasPircer.GetCtx() + cfg := api.L2GasPricer.GetConfig() + ctx := api.L2GasPricer.GetCtx() //todo: apollo l1gp, err := gasprice.GetL1GasPrice(api.L1RpcUrl) // if err != nil, do nothing if err == nil { - api.L2GasPircer.UpdateGasPriceAvg(l1gp) + api.L2GasPricer.UpdateGasPriceAvg(l1gp) } - updateTimer := time.NewTimer(cfg.UpdatePeriod) + updateTimer := time.NewTimer(cfg.XLayer.UpdatePeriod) for { select { case <-ctx.Done(): @@ -143,9 +143,9 @@ func (api *APIImpl) runL2GasPriceSuggester() { case <-updateTimer.C: l1gp, err := gasprice.GetL1GasPrice(api.L1RpcUrl) if err == nil { - api.L2GasPircer.UpdateGasPriceAvg(l1gp) + api.L2GasPricer.UpdateGasPriceAvg(l1gp) } - updateTimer.Reset(cfg.UpdatePeriod) + updateTimer.Reset(cfg.XLayer.UpdatePeriod) } } } diff --git a/cmd/rpcdaemon/commands/eth_system_zk.go b/cmd/rpcdaemon/commands/eth_system_zk.go index fc982ffb79b..69d59241612 100644 --- a/cmd/rpcdaemon/commands/eth_system_zk.go +++ b/cmd/rpcdaemon/commands/eth_system_zk.go @@ -20,8 +20,8 @@ type L1GasPrice struct { } func (api *APIImpl) GasPrice(ctx context.Context) (*hexutil.Big, error) { - // xlayer handler - if api.L2GasPircer.GetConfig().Type != "" { + // X Layer handler + if api.L2GasPricer.GetConfig().XLayer.Type != "" { return api.gasPriceXL(ctx) } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index b7f694e3bf5..cbe40308c3e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -204,21 +204,6 @@ var ( Usage: "How often transactions should be committed to the storage", Value: txpoolcfg.DefaultConfig.CommitEvery, } - TxPoolEnableWhitelistFlag = cli.BoolFlag{ - Name: "txpool.enable.whitelist", - Usage: "Enable or disable tx sender white list", - Value: false, - } - TxPoolWhiteList = cli.StringFlag{ - Name: "txpool.whitelist", - Usage: "Comma separated list of addresses, who can send transactions", - Value: "", - } - TxPoolBlockedList = cli.StringFlag{ - Name: "txpool.blockedlist", - Usage: "Comma separated list of addresses, who can't send and receive transactions", - Value: "", - } // Miner settings MiningEnabledFlag = cli.BoolFlag{ Name: "mine", @@ -621,27 +606,6 @@ var ( Name: "debug.step-after", Usage: "Start incrementing by debug.step after this block", } - // XLayer nacos - NacosURLsFlag = cli.StringFlag{ - Name: "zkevm.nacos-urls", - Usage: "Nacos urls.", - Value: "", - } - NacosNamespaceIdFlag = cli.StringFlag{ - Name: "zkevm.nacos-namespace-id", - Usage: "Nacos namespace Id.", - Value: "", - } - NacosApplicationNameFlag = cli.StringFlag{ - Name: "zkevm.nacos-application-name", - Usage: "Nacos application name", - Value: "", - } - NacosExternalListenAddrFlag = cli.StringFlag{ - Name: "zkevm.nacos-external-listen-addr", - Usage: "Nacos external listen addr.", - Value: "", - } RpcBatchConcurrencyFlag = cli.UintFlag{ Name: "rpc.batch.concurrency", Usage: "Does limit amount of goroutines to process 1 batch request. Means 1 bach request can't overload server. 1 batch still can have unlimited amount of request", @@ -1603,73 +1567,8 @@ func setGPO(ctx *cli.Context, cfg *gaspricecfg.Config) { cfg.Default = big.NewInt(ctx.Int64(GpoDefaultGasPriceFlag.Name)) } - if ctx.IsSet(GpoTypeFlag.Name) { - cfg.Type = ctx.String(GpoTypeFlag.Name) - } - - if ctx.IsSet(GpoUpdatePeriodFlag.Name) { - period, err := time.ParseDuration(ctx.String(GpoUpdatePeriodFlag.Name)) - if err != nil { - panic(fmt.Sprintf("could not parse GpoUpdatePeriodFlag value %s", ctx.String(GpoUpdatePeriodFlag.Name))) - } - cfg.UpdatePeriod = period - } - - if ctx.IsSet(GpoFactorFlag.Name) { - cfg.Factor = ctx.Float64(GpoFactorFlag.Name) - } - - if ctx.IsSet(GpoKafkaURLFlag.Name) { - cfg.KafkaURL = ctx.String(GpoKafkaURLFlag.Name) - } - - if ctx.IsSet(GpoTopicFlag.Name) { - cfg.Topic = ctx.String(GpoTopicFlag.Name) - } - - if ctx.IsSet(GpoGroupIDFlag.Name) { - cfg.GroupID = ctx.String(GpoGroupIDFlag.Name) - } - - if ctx.IsSet(GpoUsernameFlag.Name) { - cfg.Username = ctx.String(GpoUsernameFlag.Name) - } - - if ctx.IsSet(GpoPasswordFlag.Name) { - cfg.Password = ctx.String(GpoPasswordFlag.Name) - } - - if ctx.IsSet(GpoRootCAPathFlag.Name) { - cfg.RootCAPath = ctx.String(GpoRootCAPathFlag.Name) - } - - if ctx.IsSet(GpoL1CoinIdFlag.Name) { - cfg.L1CoinId = ctx.Int(GpoL1CoinIdFlag.Name) - } - - if ctx.IsSet(GpoL2CoinIdFlag.Name) { - cfg.L2CoinId = ctx.Int(GpoL2CoinIdFlag.Name) - } - - if ctx.IsSet(GpoDefaultL1CoinPriceFlag.Name) { - cfg.DefaultL1CoinPrice = ctx.Float64(GpoDefaultL1CoinPriceFlag.Name) - } - - if ctx.IsSet(GpoDefaultL2CoinPriceFlag.Name) { - cfg.DefaultL2CoinPrice = ctx.Float64(GpoDefaultL2CoinPriceFlag.Name) - } - - if ctx.IsSet(GpoGasPriceUsdtFlag.Name) { - cfg.GasPriceUsdt = ctx.Float64(GpoGasPriceUsdtFlag.Name) - } - - if ctx.IsSet(GpoEnableFollowerAdjustByL2L1PriceFlag.Name) { - cfg.EnableFollowerAdjustByL2L1Price = ctx.Bool(GpoEnableFollowerAdjustByL2L1PriceFlag.Name) - } - - if ctx.IsSet(GpoCongestionThresholdFlag.Name) { - cfg.CongestionThreshold = ctx.Int(GpoCongestionThresholdFlag.Name) - } + // For X Layer + setGPOXLayer(ctx, &cfg.XLayer) } // nolint @@ -1739,27 +1638,7 @@ func setTxPool(ctx *cli.Context, cfg *ethconfig.DeprecatedTxPoolConfig) { cfg.CommitEvery = common2.RandomizeDuration(ctx.Duration(TxPoolCommitEveryFlag.Name)) // XLayer config - if ctx.IsSet(TxPoolEnableWhitelistFlag.Name) { - cfg.EnableWhitelist = ctx.Bool(TxPoolEnableWhitelistFlag.Name) - } - if ctx.IsSet(TxPoolWhiteList.Name) { - // Parse the command separated flag - addrHexes := SplitAndTrim(ctx.String(TxPoolWhiteList.Name)) - cfg.WhiteList = make([]string, len(addrHexes)) - for i, senderHex := range addrHexes { - sender := libcommon.HexToAddress(senderHex) - cfg.WhiteList[i] = sender.String() - } - } - if ctx.IsSet(TxPoolBlockedList.Name) { - // Parse the command separated flag - addrHexes := SplitAndTrim(ctx.String(TxPoolBlockedList.Name)) - cfg.BlockedList = make([]string, len(addrHexes)) - for i, senderHex := range addrHexes { - sender := libcommon.HexToAddress(senderHex) - cfg.BlockedList[i] = sender.String() - } - } + setTxPoolXLayer(ctx, cfg) } func setEthash(ctx *cli.Context, datadir string, cfg *ethconfig.Config) { diff --git a/cmd/utils/flags_xlayer.go b/cmd/utils/flags_xlayer.go new file mode 100644 index 00000000000..bef0cbec755 --- /dev/null +++ b/cmd/utils/flags_xlayer.go @@ -0,0 +1,145 @@ +package utils + +import ( + "fmt" + "time" + + libcommon "github.com/gateway-fm/cdk-erigon-lib/common" + "github.com/urfave/cli/v2" + + "github.com/ledgerwatch/erigon/eth/ethconfig" + "github.com/ledgerwatch/erigon/eth/gasprice/gaspricecfg" +) + +var ( + // X Layer nacos + NacosURLsFlag = cli.StringFlag{ + Name: "zkevm.nacos-urls", + Usage: "Nacos urls.", + Value: "", + } + NacosNamespaceIdFlag = cli.StringFlag{ + Name: "zkevm.nacos-namespace-id", + Usage: "Nacos namespace Id.", + Value: "", + } + NacosApplicationNameFlag = cli.StringFlag{ + Name: "zkevm.nacos-application-name", + Usage: "Nacos application name", + Value: "", + } + NacosExternalListenAddrFlag = cli.StringFlag{ + Name: "zkevm.nacos-external-listen-addr", + Usage: "Nacos external listen addr.", + Value: "", + } + TxPoolEnableWhitelistFlag = cli.BoolFlag{ + Name: "txpool.enable.whitelist", + Usage: "Enable or disable tx sender white list", + Value: false, + } + TxPoolWhiteList = cli.StringFlag{ + Name: "txpool.whitelist", + Usage: "Comma separated list of addresses, who can send transactions", + Value: "", + } + TxPoolBlockedList = cli.StringFlag{ + Name: "txpool.blockedlist", + Usage: "Comma separated list of addresses, who can't send and receive transactions", + Value: "", + } +) + +func setGPOXLayer(ctx *cli.Context, cfg *gaspricecfg.XLayerConfig) { + if ctx.IsSet(GpoTypeFlag.Name) { + cfg.Type = ctx.String(GpoTypeFlag.Name) + } + + if ctx.IsSet(GpoUpdatePeriodFlag.Name) { + period, err := time.ParseDuration(ctx.String(GpoUpdatePeriodFlag.Name)) + if err != nil { + panic(fmt.Sprintf("could not parse GpoUpdatePeriodFlag value %s", ctx.String(GpoUpdatePeriodFlag.Name))) + } + cfg.UpdatePeriod = period + } + + if ctx.IsSet(GpoFactorFlag.Name) { + cfg.Factor = ctx.Float64(GpoFactorFlag.Name) + } + + if ctx.IsSet(GpoKafkaURLFlag.Name) { + cfg.KafkaURL = ctx.String(GpoKafkaURLFlag.Name) + } + + if ctx.IsSet(GpoTopicFlag.Name) { + cfg.Topic = ctx.String(GpoTopicFlag.Name) + } + + if ctx.IsSet(GpoGroupIDFlag.Name) { + cfg.GroupID = ctx.String(GpoGroupIDFlag.Name) + } + + if ctx.IsSet(GpoUsernameFlag.Name) { + cfg.Username = ctx.String(GpoUsernameFlag.Name) + } + + if ctx.IsSet(GpoPasswordFlag.Name) { + cfg.Password = ctx.String(GpoPasswordFlag.Name) + } + + if ctx.IsSet(GpoRootCAPathFlag.Name) { + cfg.RootCAPath = ctx.String(GpoRootCAPathFlag.Name) + } + + if ctx.IsSet(GpoL1CoinIdFlag.Name) { + cfg.L1CoinId = ctx.Int(GpoL1CoinIdFlag.Name) + } + + if ctx.IsSet(GpoL2CoinIdFlag.Name) { + cfg.L2CoinId = ctx.Int(GpoL2CoinIdFlag.Name) + } + + if ctx.IsSet(GpoDefaultL1CoinPriceFlag.Name) { + cfg.DefaultL1CoinPrice = ctx.Float64(GpoDefaultL1CoinPriceFlag.Name) + } + + if ctx.IsSet(GpoDefaultL2CoinPriceFlag.Name) { + cfg.DefaultL2CoinPrice = ctx.Float64(GpoDefaultL2CoinPriceFlag.Name) + } + + if ctx.IsSet(GpoGasPriceUsdtFlag.Name) { + cfg.GasPriceUsdt = ctx.Float64(GpoGasPriceUsdtFlag.Name) + } + + if ctx.IsSet(GpoEnableFollowerAdjustByL2L1PriceFlag.Name) { + cfg.EnableFollowerAdjustByL2L1Price = ctx.Bool(GpoEnableFollowerAdjustByL2L1PriceFlag.Name) + } + + if ctx.IsSet(GpoCongestionThresholdFlag.Name) { + cfg.CongestionThreshold = ctx.Int(GpoCongestionThresholdFlag.Name) + } +} + +func setTxPoolXLayer(ctx *cli.Context, cfg *ethconfig.DeprecatedTxPoolConfig) { + if ctx.IsSet(TxPoolEnableWhitelistFlag.Name) { + cfg.EnableWhitelist = ctx.Bool(TxPoolEnableWhitelistFlag.Name) + } + if ctx.IsSet(TxPoolWhiteList.Name) { + // Parse the command separated flag + addrHexes := SplitAndTrim(ctx.String(TxPoolWhiteList.Name)) + cfg.WhiteList = make([]string, len(addrHexes)) + for i, senderHex := range addrHexes { + sender := libcommon.HexToAddress(senderHex) + cfg.WhiteList[i] = sender.String() + } + } + if ctx.IsSet(TxPoolBlockedList.Name) { + // Parse the command separated flag + addrHexes := SplitAndTrim(ctx.String(TxPoolBlockedList.Name)) + cfg.BlockedList = make([]string, len(addrHexes)) + for i, senderHex := range addrHexes { + sender := libcommon.HexToAddress(senderHex) + cfg.BlockedList[i] = sender.String() + } + } +} diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 7fa31931e14..a2fe012eb11 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -55,6 +55,8 @@ var FullNodeGPO = gaspricecfg.Config{ MaxBlockHistory: 0, MaxPrice: gaspricecfg.DefaultMaxPrice, IgnorePrice: gaspricecfg.DefaultIgnorePrice, + // For X Layer + XLayer: gaspricecfg.DefaultXLayerConfig, } // LightClientGPO contains default gasprice oracle settings for light client. diff --git a/eth/gasprice/fixed_xlayer.go b/eth/gasprice/fixed_xlayer.go index 85308fb3a71..02654bd6e31 100644 --- a/eth/gasprice/fixed_xlayer.go +++ b/eth/gasprice/fixed_xlayer.go @@ -23,7 +23,7 @@ func newFixedGasPriceSuggester(ctx context.Context, cfg gaspricecfg.Config) *Fix gps := &FixedGasPrice{ cfg: cfg, ctx: ctx, - ratePrc: newKafkaProcessor(cfg, ctx), + ratePrc: newKafkaProcessor(cfg.XLayer, ctx), } return gps } @@ -37,7 +37,7 @@ func (f *FixedGasPrice) UpdateGasPriceAvg(l1GasPrice *big.Int) { 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)) + res := new(big.Float).Mul(big.NewFloat(0).SetFloat64(f.cfg.XLayer.GasPriceUsdt/l2CoinPrice), big.NewFloat(0).SetFloat64(OKBWei)) // Store l2 gasPrice calculated result := new(big.Int) res.Int(result) diff --git a/eth/gasprice/fixed_xlayer_test.go b/eth/gasprice/fixed_xlayer_test.go index b8646032493..f9375779328 100644 --- a/eth/gasprice/fixed_xlayer_test.go +++ b/eth/gasprice/fixed_xlayer_test.go @@ -13,15 +13,17 @@ func TestUpdateGasPriceFixed(t *testing.T) { ctx := context.Background() var d time.Duration = 1000000000 cfg := Config{ - Default: new(big.Int).SetUint64(1000000000), - MaxPrice: new(big.Int).SetUint64(0), - Type: FixedType, - UpdatePeriod: d, - Factor: 0.5, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 40, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(1000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: d, + Factor: 0.5, + KafkaURL: "127.0.0.1:9092", + Topic: "middle_coinPrice_push", + DefaultL2CoinPrice: 40, + GasPriceUsdt: 0.001, + }, } l1GasPrice := big.NewInt(10000000000) f := newFixedGasPriceSuggester(ctx, cfg) @@ -38,84 +40,95 @@ func TestUpdateGasPriceAvgCases(t *testing.T) { }{ { cfg: Config{ - Type: FixedType, - Default: new(big.Int).SetUint64(1000000000), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 40, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(1000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: 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, - Default: new(big.Int).SetUint64(1000000000), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 1e-19, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(1000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: 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 less than the min gas price cfg: Config{ - Type: FixedType, - Default: new(big.Int).SetUint64(26000000000000), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 40, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(26000000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: 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, - Default: new(big.Int).SetUint64(1000000000000), - MaxPrice: new(big.Int).SetUint64(23000000000000), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 40, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(1000000000000), + MaxPrice: new(big.Int).SetUint64(23000000000000), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: 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, - Default: new(big.Int).SetUint64(1000000000), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 30, - GasPriceUsdt: 0.001, + Default: new(big.Int).SetUint64(1000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + UpdatePeriod: 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, - Default: new(big.Int).SetUint64(10), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - KafkaURL: "127.0.0.1:9092", - Topic: "middle_coinPrice_push", - DefaultL2CoinPrice: 30, - GasPriceUsdt: 1e-15, + Default: new(big.Int).SetUint64(10), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FixedType, + UpdatePeriod: d, + KafkaURL: "127.0.0.1:9092", + Topic: "middle_coinPrice_push", + DefaultL2CoinPrice: 30, + GasPriceUsdt: 1e-15, + }, }, l1GasPrice: big.NewInt(10000000000), l2GasPrice: uint64(33), diff --git a/eth/gasprice/follower_xlayer.go b/eth/gasprice/follower_xlayer.go index f4f0b31bac2..fd7f75e80e3 100644 --- a/eth/gasprice/follower_xlayer.go +++ b/eth/gasprice/follower_xlayer.go @@ -31,8 +31,8 @@ func newFollowerGasPriceSuggester(ctx context.Context, cfg gaspricecfg.Config) * ctx: ctx, lastRawGP: new(big.Int).SetUint64(1), } - if cfg.EnableFollowerAdjustByL2L1Price { - gps.kafkaPrc = newKafkaProcessor(cfg, ctx) + if cfg.XLayer.EnableFollowerAdjustByL2L1Price { + gps.kafkaPrc = newKafkaProcessor(cfg.XLayer, ctx) } return gps @@ -48,11 +48,11 @@ func (f *FollowerGasPrice) UpdateGasPriceAvg(l1GasPrice *big.Int) { } // Apply factor to calculate l2 gasPrice - factor := big.NewFloat(0).SetFloat64(f.cfg.Factor) + factor := big.NewFloat(0).SetFloat64(f.cfg.XLayer.Factor) res := new(big.Float).Mul(factor, big.NewFloat(0).SetInt(l1GasPrice)) // convert the eth gas price to okb gas price - if f.cfg.EnableFollowerAdjustByL2L1Price { + if f.cfg.XLayer.EnableFollowerAdjustByL2L1Price { l1CoinPrice, l2CoinPrice := f.kafkaPrc.GetL1L2CoinPrice() if l1CoinPrice < minCoinPrice || l2CoinPrice < minCoinPrice { log.Warn("the L1 or L2 native coin price too small...") diff --git a/eth/gasprice/follower_xlayer_test.go b/eth/gasprice/follower_xlayer_test.go index 1b967c334b0..0ea9ada2ce9 100644 --- a/eth/gasprice/follower_xlayer_test.go +++ b/eth/gasprice/follower_xlayer_test.go @@ -14,11 +14,13 @@ func TestUpdateGasPriceFollower(t *testing.T) { var d time.Duration = 1000000000 cfg := Config{ - Type: FollowerType, - Default: new(big.Int).SetUint64(1000000000), - MaxPrice: new(big.Int).SetUint64(0), - UpdatePeriod: d, - Factor: 0.5, + Default: new(big.Int).SetUint64(1000000000), + MaxPrice: new(big.Int).SetUint64(0), + XLayer: XLayerConfig{ + Type: FollowerType, + UpdatePeriod: d, + Factor: 0.5, + }, } l1GasPrice := big.NewInt(10000000000) f := newFollowerGasPriceSuggester(ctx, cfg) @@ -31,11 +33,13 @@ func TestLimitMasGasPrice(t *testing.T) { var d time.Duration = 1000000000 cfg := Config{ - Type: FollowerType, - Default: new(big.Int).SetUint64(100000000), - MaxPrice: new(big.Int).SetUint64(50000000), - UpdatePeriod: d, - Factor: 0.5, + Default: new(big.Int).SetUint64(100000000), + MaxPrice: new(big.Int).SetUint64(50000000), + XLayer: XLayerConfig{ + Type: FollowerType, + UpdatePeriod: d, + Factor: 0.5, + }, } l1GasPrice := big.NewInt(1000000000) f := newFollowerGasPriceSuggester(ctx, cfg) diff --git a/eth/gasprice/gaspricecfg/config_xlayer.go b/eth/gasprice/gaspricecfg/config_xlayer.go new file mode 100644 index 00000000000..5aab5aa69ba --- /dev/null +++ b/eth/gasprice/gaspricecfg/config_xlayer.go @@ -0,0 +1,42 @@ +package gaspricecfg + +import "time" + +type XLayerConfig struct { + Type string `toml:",omitempty"` + UpdatePeriod time.Duration `toml:",omitempty"` + Factor float64 `toml:",omitempty"` + KafkaURL string `toml:",omitempty"` + Topic string `toml:",omitempty"` + GroupID string `toml:",omitempty"` + Username string `toml:",omitempty"` + Password string `toml:",omitempty"` + RootCAPath string `toml:",omitempty"` + L1CoinId int `toml:",omitempty"` + L2CoinId int `toml:",omitempty"` + // DefaultL1CoinPrice is the L1 token's coin price + DefaultL1CoinPrice float64 `toml:",omitempty"` + // DefaultL2CoinPrice is the native token's coin price + DefaultL2CoinPrice float64 `toml:",omitempty"` + GasPriceUsdt float64 `toml:",omitempty"` + + // EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy + EnableFollowerAdjustByL2L1Price bool `toml:",omitempty"` + + CongestionThreshold int `toml:",omitempty"` +} + +var DefaultXLayerConfig = XLayerConfig{ + Type: DefaultType, +} + +const ( + // DefaultType default gas price from config is set. + DefaultType string = "default" + + // FollowerType calculate the gas price basing on the L1 gasPrice. + FollowerType string = "follower" + + // FixedType the gas price from config that the unit is usdt, XLayer config + FixedType string = "fixed" +) diff --git a/eth/gasprice/gaspricecfg/gaspricecfg.go b/eth/gasprice/gaspricecfg/gaspricecfg.go index 6f84787a11c..81b48fa0561 100644 --- a/eth/gasprice/gaspricecfg/gaspricecfg.go +++ b/eth/gasprice/gaspricecfg/gaspricecfg.go @@ -2,7 +2,6 @@ package gaspricecfg import ( "math/big" - "time" "github.com/ledgerwatch/erigon/params" ) @@ -23,36 +22,5 @@ type Config struct { IgnorePrice *big.Int `toml:",omitempty"` // XLayer config - Type string `toml:",omitempty"` - UpdatePeriod time.Duration `toml:",omitempty"` - Factor float64 `toml:",omitempty"` - KafkaURL string `toml:",omitempty"` - Topic string `toml:",omitempty"` - GroupID string `toml:",omitempty"` - Username string `toml:",omitempty"` - Password string `toml:",omitempty"` - RootCAPath string `toml:",omitempty"` - L1CoinId int `toml:",omitempty"` - L2CoinId int `toml:",omitempty"` - // DefaultL1CoinPrice is the L1 token's coin price - DefaultL1CoinPrice float64 `toml:",omitempty"` - // DefaultL2CoinPrice is the native token's coin price - DefaultL2CoinPrice float64 `toml:",omitempty"` - GasPriceUsdt float64 `toml:",omitempty"` - - // EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy - EnableFollowerAdjustByL2L1Price bool `toml:",omitempty"` - - CongestionThreshold int `toml:",omitempty"` + XLayer XLayerConfig } - -const ( - // DefaultType default gas price from config is set. - DefaultType string = "default" - - // FollowerType calculate the gas price basing on the L1 gasPrice. - FollowerType string = "follower" - - // FixedType the gas price from config that the unit is usdt, XLayer config - FixedType string = "fixed" -) diff --git a/eth/gasprice/gaspricesuggester.go b/eth/gasprice/gaspricesuggester_xlayer.go similarity index 90% rename from eth/gasprice/gaspricesuggester.go rename to eth/gasprice/gaspricesuggester_xlayer.go index 151e54fc904..501c709d8dd 100644 --- a/eth/gasprice/gaspricesuggester.go +++ b/eth/gasprice/gaspricesuggester_xlayer.go @@ -22,7 +22,7 @@ type L2GasPricer interface { // NewL2GasPriceSuggester init. func NewL2GasPriceSuggester(ctx context.Context, cfg gaspricecfg.Config) L2GasPricer { var gpricer L2GasPricer - switch cfg.Type { + switch cfg.XLayer.Type { case gaspricecfg.FollowerType: log.Info("Follower type selected") gpricer = newFollowerGasPriceSuggester(ctx, cfg) @@ -33,7 +33,7 @@ func NewL2GasPriceSuggester(ctx context.Context, cfg gaspricecfg.Config) L2GasPr log.Info("Fixed type selected") gpricer = newFixedGasPriceSuggester(ctx, cfg) default: - log.Error("unknown l2 gas price suggester type ", cfg.Type, ". Please specify a valid one: 'follower', 'fixed' or 'default'") + log.Error("unknown l2 gas price suggester type ", cfg.XLayer.Type, ". Please specify a valid one: 'follower', 'fixed' or 'default'") } return gpricer diff --git a/eth/gasprice/kafka_proc_xlayer.go b/eth/gasprice/kafka_proc_xlayer.go index dacfbb86d03..565a8267a4d 100644 --- a/eth/gasprice/kafka_proc_xlayer.go +++ b/eth/gasprice/kafka_proc_xlayer.go @@ -82,7 +82,7 @@ type L1L2PriceRecord struct { // KafkaProcessor kafka processor type KafkaProcessor struct { - cfg gaspricecfg.Config + cfg gaspricecfg.XLayerConfig kreader *kafka.Reader ctx context.Context rwLock sync.RWMutex @@ -93,7 +93,7 @@ type KafkaProcessor struct { tmpPrices L1L2PriceRecord } -func newKafkaProcessor(cfg gaspricecfg.Config, ctx context.Context) *KafkaProcessor { +func newKafkaProcessor(cfg gaspricecfg.XLayerConfig, ctx context.Context) *KafkaProcessor { rp := &KafkaProcessor{ cfg: cfg, kreader: getKafkaReader(cfg), @@ -114,7 +114,7 @@ func newKafkaProcessor(cfg gaspricecfg.Config, ctx context.Context) *KafkaProces return rp } -func getKafkaReader(cfg gaspricecfg.Config) *kafka.Reader { +func getKafkaReader(cfg gaspricecfg.XLayerConfig) *kafka.Reader { brokers := strings.Split(cfg.KafkaURL, ",") var dialer *kafka.Dialer diff --git a/eth/gasprice/kafka_proc_xlayer_test.go b/eth/gasprice/kafka_proc_xlayer_test.go index 0fa662be83c..1b91f8e8dc7 100644 --- a/eth/gasprice/kafka_proc_xlayer_test.go +++ b/eth/gasprice/kafka_proc_xlayer_test.go @@ -107,7 +107,7 @@ func TestParseCoinPrice(t *testing.T) { } for _, tc := range testcases { - rp := newKafkaProcessor(Config{Topic: "middle_coinPrice_push"}, context.Background()) + rp := newKafkaProcessor(XLayerConfig{Topic: "middle_coinPrice_push"}, context.Background()) rt, err := rp.parseCoinPrice([]byte(tc.msg), tc.coinIds) tc.check(rt, err) } @@ -119,7 +119,7 @@ func TestUpdateL1L2CoinPrice(t *testing.T) { }{ { check: func() { - rp := newKafkaProcessor(Config{Topic: "middle_coinPrice_push"}, context.Background()) + rp := newKafkaProcessor(XLayerConfig{Topic: "middle_coinPrice_push"}, context.Background()) prices := map[int]float64{ethcoinId: 1.5, okbcoinId: 0.5} rp.updateL1L2CoinPrice(prices) l1, l2 := rp.GetL1L2CoinPrice() @@ -129,7 +129,7 @@ func TestUpdateL1L2CoinPrice(t *testing.T) { }, { check: func() { - rp := newKafkaProcessor(Config{Topic: "middle_coinPrice_push"}, context.Background()) + rp := newKafkaProcessor(XLayerConfig{Topic: "middle_coinPrice_push"}, context.Background()) prices := map[int]float64{ethcoinId: 1.5} rp.updateL1L2CoinPrice(prices) l1, l2 := rp.GetL1L2CoinPrice() @@ -149,7 +149,7 @@ func TestUpdateL1L2CoinPrice(t *testing.T) { }, { check: func() { - rp := newKafkaProcessor(Config{Topic: "middle_coinPrice_push"}, context.Background()) + rp := newKafkaProcessor(XLayerConfig{Topic: "middle_coinPrice_push"}, context.Background()) prices := map[int]float64{okbcoinId: 0.5} rp.updateL1L2CoinPrice(prices) l1, l2 := rp.GetL1L2CoinPrice() @@ -176,13 +176,13 @@ func TestUpdateL1L2CoinPrice(t *testing.T) { func TestUpdate(t *testing.T) { testcases := []struct { msg string - cfg Config + cfg XLayerConfig check func(rp *KafkaProcessor, err error) }{ // FixedType { // correct msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}, {\"coinId\":%d,\"price\":0.002}, {\"coinId\":123,\"price\":0.005}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", ethcoinId, okbcoinId), - cfg: Config{Topic: "middle_coinPrice_push", Type: FixedType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FixedType}, check: func(rp *KafkaProcessor, err error) { require.NoError(t, err) require.Equal(t, rp.GetL2CoinPrice(), 0.002) @@ -190,7 +190,7 @@ func TestUpdate(t *testing.T) { }, { // not find msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", ethcoinId), - cfg: Config{Topic: "middle_coinPrice_push", Type: FixedType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FixedType}, check: func(rp *KafkaProcessor, err error) { require.Equal(t, err, ErrNotFindCoinPrice) require.Equal(t, rp.GetL2CoinPrice(), float64(0)) @@ -198,7 +198,7 @@ func TestUpdate(t *testing.T) { }, { // not find msg: "{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", - cfg: Config{Topic: "middle_coinPrice_push", Type: FixedType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FixedType}, check: func(rp *KafkaProcessor, err error) { require.EqualError(t, err, "the data PriceList is empty") require.Equal(t, rp.GetL2CoinPrice(), float64(0)) @@ -208,7 +208,7 @@ func TestUpdate(t *testing.T) { // FollowerType { // correct msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}, {\"coinId\":%d,\"price\":0.002}, {\"coinId\":123,\"price\":0.005}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", ethcoinId, okbcoinId), - cfg: Config{Topic: "middle_coinPrice_push", Type: FollowerType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FollowerType}, check: func(rp *KafkaProcessor, err error) { require.NoError(t, err) l1, l2 := rp.GetL1L2CoinPrice() @@ -218,7 +218,7 @@ func TestUpdate(t *testing.T) { }, { // not find msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", ethcoinId+1), - cfg: Config{Topic: "middle_coinPrice_push", Type: FollowerType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FollowerType}, check: func(rp *KafkaProcessor, err error) { require.Equal(t, err, ErrNotFindCoinPrice) l1, l2 := rp.GetL1L2CoinPrice() @@ -228,7 +228,7 @@ func TestUpdate(t *testing.T) { }, { // find one but not update msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", ethcoinId), - cfg: Config{Topic: "middle_coinPrice_push", Type: FollowerType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FollowerType}, check: func(rp *KafkaProcessor, err error) { require.NoError(t, err) l1, l2 := rp.GetL1L2CoinPrice() @@ -238,7 +238,7 @@ func TestUpdate(t *testing.T) { }, { // find one but not update msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.04}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", okbcoinId), - cfg: Config{Topic: "middle_coinPrice_push", Type: FollowerType}, + cfg: XLayerConfig{Topic: "middle_coinPrice_push", Type: FollowerType}, check: func(rp *KafkaProcessor, err error) { require.NoError(t, err) l1, l2 := rp.GetL1L2CoinPrice() From 3123ebcc3177f06a79d17cb57ac137ea36ce1bb7 Mon Sep 17 00:00:00 2001 From: Niven Date: Thu, 4 Jul 2024 14:13:17 +0800 Subject: [PATCH 2/6] Clean up commentst --- cmd/rpcdaemon/commands/eth_api.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index f7bf04b00dd..e6466de99da 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -383,8 +383,7 @@ func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpoo EnableInnerTx: ethCfg.EnableInnerTx, } - // For X Layer - // Set default gas price + // For X Layer. Set default gas price apii.gasCache.SetLatest(common.Hash{}, apii.L2GasPricer.GetConfig().Default) go apii.runL2GasPriceSuggester() From e23f95f79a76aa3841d792e0d6c5bc6add9a3e4f Mon Sep 17 00:00:00 2001 From: Niven Date: Fri, 5 Jul 2024 12:38:04 +0800 Subject: [PATCH 3/6] Add desc --- eth/gasprice/gaspricecfg/config_xlayer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/eth/gasprice/gaspricecfg/config_xlayer.go b/eth/gasprice/gaspricecfg/config_xlayer.go index 5aab5aa69ba..952de542692 100644 --- a/eth/gasprice/gaspricecfg/config_xlayer.go +++ b/eth/gasprice/gaspricecfg/config_xlayer.go @@ -2,6 +2,7 @@ package gaspricecfg import "time" +// XLayerConfig is the X Layer gas price config type XLayerConfig struct { Type string `toml:",omitempty"` UpdatePeriod time.Duration `toml:",omitempty"` From 7a75a5ee2af83d852eb5c839f0cdbc5f5e2645c0 Mon Sep 17 00:00:00 2001 From: Niven Date: Fri, 5 Jul 2024 14:19:34 +0800 Subject: [PATCH 4/6] Refactor configs --- cmd/rpcdaemon/commands/eth_api.go | 2 +- cmd/txpool/main.go | 3 +++ eth/backend.go | 6 +++--- eth/ethconfig/config_xlayer.go | 17 +++++++++++++++++ eth/ethconfig/config_zkevm.go | 15 +++++++-------- eth/stagedsync/stage_execute_zkevm.go | 2 +- turbo/cli/default_flags.go | 11 ++++++----- turbo/cli/flags_xlayer.go | 19 +++++++++++++++++++ turbo/cli/flags_zkevm.go | 8 +++----- 9 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 eth/ethconfig/config_xlayer.go create mode 100644 turbo/cli/flags_xlayer.go diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index e6466de99da..667e62dfdf1 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -380,7 +380,7 @@ func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpoo L1GasPrice: L1GasPrice{}, // For X Layer L2GasPricer: gasprice.NewL2GasPriceSuggester(context.Background(), ethCfg.GPO), - EnableInnerTx: ethCfg.EnableInnerTx, + EnableInnerTx: ethCfg.XLayer.EnableInnerTx, } // For X Layer. Set default gas price diff --git a/cmd/txpool/main.go b/cmd/txpool/main.go index 56005950dff..5d425dcf408 100644 --- a/cmd/txpool/main.go +++ b/cmd/txpool/main.go @@ -56,6 +56,7 @@ var ( commitEvery time.Duration + // For X Layer enableWhiteList bool whiteList []string blockList []string @@ -161,6 +162,8 @@ func doTxpool(ctx context.Context) error { sender := common.HexToAddress(senderHex) cfg.TracedSenders[i] = string(sender[:]) } + + // For X Layer ethCfg := ðconfig.Defaults ethCfg.DeprecatedTxPool.EnableWhitelist = enableWhiteList ethCfg.DeprecatedTxPool.WhiteList = make([]string, len(whiteList)) diff --git a/eth/backend.go b/eth/backend.go index 77ef0adbd5d..534e11474b2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -749,9 +749,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { // entering ZK territory! cfg := backend.config - // For Xlayer - if len(cfg.NacosURLs) > 0 { - nacos.StartNacosClient(cfg.NacosURLs, cfg.NacosNamespaceId, cfg.NacosApplicationName, cfg.NacosExternalListenAddr) + // For X Layer + if len(cfg.XLayer.Nacos.URLs) > 0 { + nacos.StartNacosClient(cfg.XLayer.Nacos.URLs, cfg.XLayer.Nacos.NamespaceId, cfg.XLayer.Nacos.ApplicationName, cfg.XLayer.Nacos.ExternalListenAddr) } // update the chain config with the zero gas from the flags diff --git a/eth/ethconfig/config_xlayer.go b/eth/ethconfig/config_xlayer.go new file mode 100644 index 00000000000..bf3582c3251 --- /dev/null +++ b/eth/ethconfig/config_xlayer.go @@ -0,0 +1,17 @@ +package ethconfig + +// XLayerConfig is the X Layer config used on the eth backend +type XLayerConfig struct { + Nacos NacosConfig + EnableInnerTx bool +} + +var DefaultXLayerConfig = &XLayerConfig{} + +// NacosConfig is the config for nacos +type NacosConfig struct { + URLs string + NamespaceId string + ApplicationName string + ExternalListenAddr string +} diff --git a/eth/ethconfig/config_zkevm.go b/eth/ethconfig/config_zkevm.go index 2d065498d76..5f65ddc230d 100644 --- a/eth/ethconfig/config_zkevm.go +++ b/eth/ethconfig/config_zkevm.go @@ -55,19 +55,18 @@ type Zk struct { DebugStep uint64 DebugStepAfter uint64 - // For Xlayer - NacosURLs string - NacosNamespaceId string - NacosApplicationName string - NacosExternalListenAddr string - PoolManagerUrl string DisableVirtualCounters bool ExecutorPayloadOutput string - EnableInnerTx bool // XLayer + + // For X Layer + XLayer *XLayerConfig } -var DefaultZkConfig = &Zk{} +var DefaultZkConfig = &Zk{ + // For X Layer + XLayer: DefaultXLayerConfig, +} func (c *Zk) ShouldCountersBeUnlimited(l1Recovery bool) bool { return l1Recovery || (c.DisableVirtualCounters && !c.ExecutorStrictMode && len(c.ExecutorUrls) != 0) diff --git a/eth/stagedsync/stage_execute_zkevm.go b/eth/stagedsync/stage_execute_zkevm.go index ce6fc6f08b8..154b457a3f4 100644 --- a/eth/stagedsync/stage_execute_zkevm.go +++ b/eth/stagedsync/stage_execute_zkevm.go @@ -130,7 +130,7 @@ Loop: writeChangeSets := nextStagesExpectData || blockNum > cfg.prune.History.PruneTo(to) writeReceipts := nextStagesExpectData || blockNum > cfg.prune.Receipts.PruneTo(to) writeCallTraces := nextStagesExpectData || blockNum > cfg.prune.CallTraces.PruneTo(to) - writeInnerTxs := cfg.zk.EnableInnerTx && (nextStagesExpectData || blockNum > cfg.prune.InnerTxs.PruneTo(to)) + writeInnerTxs := cfg.zk.XLayer.EnableInnerTx && (nextStagesExpectData || blockNum > cfg.prune.InnerTxs.PruneTo(to)) execRs, err := executeBlockZk(block, &prevBlockRoot, tx, batch, cfg, *cfg.vmConfig, writeChangeSets, writeReceipts, writeCallTraces, writeInnerTxs, initialCycle, stateStream, hermezDb) if err != nil { diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 9b80c01ee33..ed46e631ab7 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -26,9 +26,6 @@ var DefaultFlags = []cli.Flag{ &utils.TxPoolLifetimeFlag, &utils.TxPoolTraceSendersFlag, &utils.TxPoolCommitEveryFlag, - &utils.TxPoolEnableWhitelistFlag, - &utils.TxPoolWhiteList, - &utils.TxPoolBlockedList, &PruneFlag, &PruneHistoryFlag, &PruneReceiptFlag, @@ -234,10 +231,14 @@ var DefaultFlags = []cli.Flag{ &utils.DebugLimit, &utils.DebugStep, &utils.DebugStepAfter, + &utils.PoolManagerUrl, + &utils.DisableVirtualCounters, + // X Layer Flags &utils.NacosURLsFlag, &utils.NacosNamespaceIdFlag, &utils.NacosApplicationNameFlag, &utils.NacosExternalListenAddrFlag, - &utils.PoolManagerUrl, - &utils.DisableVirtualCounters, + &utils.TxPoolEnableWhitelistFlag, + &utils.TxPoolWhiteList, + &utils.TxPoolBlockedList, } diff --git a/turbo/cli/flags_xlayer.go b/turbo/cli/flags_xlayer.go new file mode 100644 index 00000000000..ab92d0a023f --- /dev/null +++ b/turbo/cli/flags_xlayer.go @@ -0,0 +1,19 @@ +package cli + +import ( + "github.com/ledgerwatch/erigon/cmd/utils" + "github.com/ledgerwatch/erigon/eth/ethconfig" + "github.com/urfave/cli/v2" +) + +func ApplyFlagsForXLayerConfig(ctx *cli.Context, cfg *ethconfig.Config) { + cfg.XLayer = ðconfig.XLayerConfig{ + Nacos: ethconfig.NacosConfig{ + URLs: ctx.String(utils.NacosURLsFlag.Name), + NamespaceId: ctx.String(utils.NacosNamespaceIdFlag.Name), + ApplicationName: ctx.String(utils.NacosApplicationNameFlag.Name), + ExternalListenAddr: ctx.String(utils.NacosExternalListenAddrFlag.Name), + }, + EnableInnerTx: ctx.Bool(utils.AllowInternalTransactions.Name), + } +} diff --git a/turbo/cli/flags_zkevm.go b/turbo/cli/flags_zkevm.go index 42479cb0963..7d96bdbd08a 100644 --- a/turbo/cli/flags_zkevm.go +++ b/turbo/cli/flags_zkevm.go @@ -116,16 +116,14 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) { DebugLimit: ctx.Uint64(utils.DebugLimit.Name), DebugStep: ctx.Uint64(utils.DebugStep.Name), DebugStepAfter: ctx.Uint64(utils.DebugStepAfter.Name), - NacosURLs: ctx.String(utils.NacosURLsFlag.Name), - NacosNamespaceId: ctx.String(utils.NacosNamespaceIdFlag.Name), - NacosApplicationName: ctx.String(utils.NacosApplicationNameFlag.Name), - NacosExternalListenAddr: ctx.String(utils.NacosExternalListenAddrFlag.Name), PoolManagerUrl: ctx.String(utils.PoolManagerUrl.Name), DisableVirtualCounters: ctx.Bool(utils.DisableVirtualCounters.Name), ExecutorPayloadOutput: ctx.String(utils.ExecutorPayloadOutput.Name), - EnableInnerTx: ctx.Bool(utils.AllowInternalTransactions.Name), } + // For X Layer + ApplyFlagsForXLayerConfig(ctx, cfg) + checkFlag(utils.L2ChainIdFlag.Name, cfg.L2ChainId) if !sequencer.IsSequencer() { checkFlag(utils.L2RpcUrlFlag.Name, cfg.L2RpcUrl) From 6eef6a73597c2dadfa4cec7bff82e6f72fd52eba Mon Sep 17 00:00:00 2001 From: Niven Date: Fri, 5 Jul 2024 14:29:29 +0800 Subject: [PATCH 5/6] Fix comment --- eth/gasprice/gaspricecfg/gaspricecfg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth/gasprice/gaspricecfg/gaspricecfg.go b/eth/gasprice/gaspricecfg/gaspricecfg.go index 81b48fa0561..5e679d179bc 100644 --- a/eth/gasprice/gaspricecfg/gaspricecfg.go +++ b/eth/gasprice/gaspricecfg/gaspricecfg.go @@ -21,6 +21,6 @@ type Config struct { MaxPrice *big.Int `toml:",omitempty"` IgnorePrice *big.Int `toml:",omitempty"` - // XLayer config + // For X Layer XLayer XLayerConfig } From 64e44ef870999ef0be36fb4fb62f2d665962a11f Mon Sep 17 00:00:00 2001 From: Niven Date: Mon, 8 Jul 2024 00:06:53 +0800 Subject: [PATCH 6/6] Shift gpo flags into xlayer file --- cmd/utils/flags.go | 102 ------------------------------------- cmd/utils/flags_xlayer.go | 103 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index cbe40308c3e..0532a47e2d6 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -851,108 +851,6 @@ var ( Usage: "Maximum gas price will be recommended by gpo", Value: ethconfig.Defaults.GPO.MaxPrice.Int64(), } - - GpoDefaultGasPriceFlag = cli.Int64Flag{ - Name: "gpo.default-price", - Usage: "Default gas price will be recommended by gpo", - Value: ethconfig.Defaults.GPO.Default.Int64(), - } - - GpoTypeFlag = cli.StringFlag{ - Name: "gpo.type", - Usage: "raw gas price strategy type: default, follower, fixed", - Value: "default", - } - - GpoUpdatePeriodFlag = cli.StringFlag{ - Name: "gpo.update-period", - Usage: "raw gas price update period", - Value: "10s", - } - - GpoFactorFlag = cli.Float64Flag{ - Name: "gpo.factor", - Usage: "raw gas price facotr", - Value: 0.15, - } - - GpoKafkaURLFlag = cli.StringFlag{ - Name: "gpo.kafka-url", - Usage: "raw gas price kafka url", - Value: "", - } - - GpoTopicFlag = cli.StringFlag{ - Name: "gpo.topic", - Usage: "raw gas price topic", - Value: "", - } - - GpoGroupIDFlag = cli.StringFlag{ - Name: "gpo.group-id", - Usage: "raw gas price group id", - Value: "", - } - - GpoUsernameFlag = cli.StringFlag{ - Name: "gpo.username", - Usage: "raw gas price username", - Value: "", - } - - GpoPasswordFlag = cli.StringFlag{ - Name: "gpo.password", - Usage: "raw gas price password", - Value: "", - } - - GpoRootCAPathFlag = cli.StringFlag{ - Name: "gpo.root-ca-path", - Usage: "raw gas price root ca path", - Value: "", - } - - GpoL1CoinIdFlag = cli.IntFlag{ - Name: "gpo.l1-coin-id", - Usage: "raw gas price l1 coin id", - Value: 0, - } - - GpoL2CoinIdFlag = cli.IntFlag{ - Name: "gpo.l2-coin-id", - Usage: "raw gas price l2 coin id", - Value: 0, - } - - GpoDefaultL1CoinPriceFlag = cli.Float64Flag{ - Name: "gpo.default-l1-coin-price", - Usage: "raw gas price default l1 coin price", - Value: 0, - } - - GpoDefaultL2CoinPriceFlag = cli.Float64Flag{ - Name: "gpo.default-l2-coin-price", - Usage: "raw gas price default l2 coin price", - Value: 0, - } - - GpoGasPriceUsdtFlag = cli.Float64Flag{ - Name: "gpo.gas-price-usdt", - Usage: "raw gas price usdt", - Value: 0, - } - - GpoEnableFollowerAdjustByL2L1PriceFlag = cli.BoolFlag{ - Name: "gpo.enable-follower-adjust", - Usage: "enable dynamic adjust the factor through the L1 and L2 coins price in follower strategy", - Value: true, - } - - GpoCongestionThresholdFlag = cli.IntFlag{ - Name: "gpo.congestion-threshold", - Usage: "Used to determine whether pending tx has reached the threshold for congestion", - Value: 0, - } // Metrics flags MetricsEnabledFlag = cli.BoolFlag{ Name: "metrics", diff --git a/cmd/utils/flags_xlayer.go b/cmd/utils/flags_xlayer.go index bef0cbec755..d3eaa025c98 100644 --- a/cmd/utils/flags_xlayer.go +++ b/cmd/utils/flags_xlayer.go @@ -33,6 +33,7 @@ var ( Usage: "Nacos external listen addr.", Value: "", } + // Pool TxPoolEnableWhitelistFlag = cli.BoolFlag{ Name: "txpool.enable.whitelist", Usage: "Enable or disable tx sender white list", @@ -48,6 +49,108 @@ var ( Usage: "Comma separated list of addresses, who can't send and receive transactions", Value: "", } + // Gas Price + GpoDefaultGasPriceFlag = cli.Int64Flag{ + Name: "gpo.default-price", + Usage: "Default gas price will be recommended by gpo", + Value: ethconfig.Defaults.GPO.Default.Int64(), + } + + GpoTypeFlag = cli.StringFlag{ + Name: "gpo.type", + Usage: "raw gas price strategy type: default, follower, fixed", + Value: "default", + } + + GpoUpdatePeriodFlag = cli.StringFlag{ + Name: "gpo.update-period", + Usage: "raw gas price update period", + Value: "10s", + } + + GpoFactorFlag = cli.Float64Flag{ + Name: "gpo.factor", + Usage: "raw gas price facotr", + Value: 0.15, + } + + GpoKafkaURLFlag = cli.StringFlag{ + Name: "gpo.kafka-url", + Usage: "raw gas price kafka url", + Value: "", + } + + GpoTopicFlag = cli.StringFlag{ + Name: "gpo.topic", + Usage: "raw gas price topic", + Value: "", + } + + GpoGroupIDFlag = cli.StringFlag{ + Name: "gpo.group-id", + Usage: "raw gas price group id", + Value: "", + } + + GpoUsernameFlag = cli.StringFlag{ + Name: "gpo.username", + Usage: "raw gas price username", + Value: "", + } + + GpoPasswordFlag = cli.StringFlag{ + Name: "gpo.password", + Usage: "raw gas price password", + Value: "", + } + + GpoRootCAPathFlag = cli.StringFlag{ + Name: "gpo.root-ca-path", + Usage: "raw gas price root ca path", + Value: "", + } + + GpoL1CoinIdFlag = cli.IntFlag{ + Name: "gpo.l1-coin-id", + Usage: "raw gas price l1 coin id", + Value: 0, + } + + GpoL2CoinIdFlag = cli.IntFlag{ + Name: "gpo.l2-coin-id", + Usage: "raw gas price l2 coin id", + Value: 0, + } + + GpoDefaultL1CoinPriceFlag = cli.Float64Flag{ + Name: "gpo.default-l1-coin-price", + Usage: "raw gas price default l1 coin price", + Value: 0, + } + + GpoDefaultL2CoinPriceFlag = cli.Float64Flag{ + Name: "gpo.default-l2-coin-price", + Usage: "raw gas price default l2 coin price", + Value: 0, + } + + GpoGasPriceUsdtFlag = cli.Float64Flag{ + Name: "gpo.gas-price-usdt", + Usage: "raw gas price usdt", + Value: 0, + } + + GpoEnableFollowerAdjustByL2L1PriceFlag = cli.BoolFlag{ + Name: "gpo.enable-follower-adjust", + Usage: "enable dynamic adjust the factor through the L1 and L2 coins price in follower strategy", + Value: true, + } + + GpoCongestionThresholdFlag = cli.IntFlag{ + Name: "gpo.congestion-threshold", + Usage: "Used to determine whether pending tx has reached the threshold for congestion", + Value: 0, + } ) func setGPOXLayer(ctx *cli.Context, cfg *gaspricecfg.XLayerConfig) {