Skip to content

Commit

Permalink
Merge PR: add dynamic dp flag (#2764)
Browse files Browse the repository at this point in the history
* gp bug fix

* modify code

* add flag DynamicGpAdaptUncongest

* modify ut

* adapt congest

* bug fix

* code optimize

* add flag: gp maxGas maxTx

* Update config.go

* Update flags.go

Co-authored-by: KamiD <[email protected]>
  • Loading branch information
LeoGuo621 and KamiD authored Nov 20, 2022
1 parent 3dcee9f commit 62c7834
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
52 changes: 51 additions & 1 deletion app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type OecConfig struct {
dynamicGpAdaptCongest bool
// dynamic-gp-coefficient
dynamicGpCoefficient int
// dynamic-gp-max-gas-used
dynamicGpMaxGasUsed int64
// dynamic-gp-max-tx-num
dynamicGpMaxTxNum int64


// consensus.timeout_propose
csTimeoutPropose time.Duration
Expand Down Expand Up @@ -116,6 +121,10 @@ const (
FlagDynamicGpAdaptUncongest = "dynamic-gp-adapt-uncongest"
FlagDynamicGpAdaptCongest = "dynamic-gp-adapt-congest"
FlagDynamicGpCoefficient = "dynamic-gp-coefficient"

FlagDynamicGpMaxGasUsed = "dynamic-gp-max-gas-used"
FlagDynamicGpMaxTxNum = "dynamic-gp-max-tx-num"

FlagEnableWrappedTx = "enable-wtx"
FlagSentryAddrs = "p2p.sentry_addrs"

Expand Down Expand Up @@ -234,6 +243,9 @@ func (c *OecConfig) loadFromConfig() {
c.SetDynamicGpWeight(viper.GetInt(FlagDynamicGpWeight))
c.SetDynamicGpCheckBlocks(viper.GetInt(FlagDynamicGpCheckBlocks))
c.SetDynamicGpCoefficient(viper.GetInt(FlagDynamicGpCoefficient))
c.SetDynamicGpMaxGasUsed(viper.GetInt64(FlagDynamicGpMaxGasUsed))
c.SetDynamicGpMaxTxNum(viper.GetInt64(FlagDynamicGpMaxTxNum))

c.SetDynamicGpAdaptCongest(viper.GetBool(FlagDynamicGpAdaptCongest))
c.SetDynamicGpAdaptUncongest(viper.GetBool(FlagDynamicGpAdaptUncongest))
c.SetCsTimeoutPropose(viper.GetDuration(FlagCsTimeoutPropose))
Expand Down Expand Up @@ -289,7 +301,10 @@ func (c *OecConfig) format() string {
dynamic-gp-check-blocks: %d
dynamic-gp-adapt-uncongest: %v
dynamic-gp-adapt-congest: %v
dynamic-gp-adapt-coefficient: %d
dynamic-gp-coefficient: %d
dynamic-gp-max-gas-used: %d
dynamic-gp-max-tx-num: %d
consensus.timeout_propose: %s
consensus.timeout_propose_delta: %s
Expand All @@ -316,6 +331,9 @@ func (c *OecConfig) format() string {
c.GetDynamicGpAdaptUncongest(),
c.GetDynamicGpAdaptCongest(),
c.GetDynamicGpCoefficient(),
c.GetDynamicGpMaxGasUsed(),
c.GetDynamicGpMaxTxNum(),

c.GetCsTimeoutPropose(),
c.GetCsTimeoutProposeDelta(),
c.GetCsTimeoutPrevote(),
Expand Down Expand Up @@ -428,6 +446,18 @@ func (c *OecConfig) update(key, value interface{}) {
return
}
c.SetDynamicGpCoefficient(r)
case FlagDynamicGpMaxGasUsed:
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return
}
c.SetDynamicGpMaxGasUsed(r)
case FlagDynamicGpMaxTxNum:
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return
}
c.SetDynamicGpMaxTxNum(r)
case FlagCsTimeoutPropose:
r, err := time.ParseDuration(v)
if err != nil {
Expand Down Expand Up @@ -677,6 +707,26 @@ func (c *OecConfig) SetDynamicGpCoefficient(value int) {
}
c.dynamicGpCoefficient = value
}
func (c *OecConfig) GetDynamicGpMaxGasUsed() int64 {
return c.dynamicGpMaxGasUsed
}

func (c *OecConfig) SetDynamicGpMaxGasUsed(value int64) {
if value > 0 {
c.dynamicGpMaxGasUsed = value
}
}

func (c *OecConfig) GetDynamicGpMaxTxNum() int64 {
return c.dynamicGpMaxTxNum
}

func (c *OecConfig) SetDynamicGpMaxTxNum(value int64) {
if value < 0 {
return
}
c.dynamicGpMaxTxNum = value
}

func (c *OecConfig) GetDynamicGpCheckBlocks() int {
return c.dynamicGpCheckBlocks
Expand Down
14 changes: 7 additions & 7 deletions app/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func NewGPOConfig(weight int, checkBlocks int) GPOConfig {
}
}

func DefaultGPOConfig() GPOConfig {
return NewGPOConfig(80, 5)
}

// Oracle recommends gas prices based on the content of recent blocks.
type Oracle struct {
CurrentBlockGPs *types.SingleBlockGPs
Expand Down Expand Up @@ -66,13 +62,17 @@ func NewOracle(params GPOConfig) *Oracle {
}

func (gpo *Oracle) RecommendGP() *big.Int {
maxGasUsed := appconfig.GetOecConfig().GetMaxGasUsedPerBlock()
maxTxNum := appconfig.GetOecConfig().GetMaxTxNumPerBlock()

maxGasUsed := appconfig.GetOecConfig().GetDynamicGpMaxGasUsed()
maxTxNum := appconfig.GetOecConfig().GetDynamicGpMaxTxNum()

allTxsLen := int64(len(gpo.CurrentBlockGPs.GetAll()))
// If maxGasUsed is not negative and the current block's total gas consumption is more than 80% of it,
// or the number of tx in the current block is more than 80% of MaxTxNumPerBlock in mempool config,
// then we consider the chain to be congested.
isCongested := (gpo.CurrentBlockGPs.GetGasUsed() >= uint64(maxGasUsed*80/100)) || (allTxsLen >= maxTxNum*80/100)

isCongested := (gpo.CurrentBlockGPs.GetGasUsed() >= uint64(maxGasUsed)) || (allTxsLen >= maxTxNum)


adoptHigherGp := appconfig.GetOecConfig().GetDynamicGpAdaptCongest() && isCongested

Expand Down
4 changes: 4 additions & 0 deletions cmd/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ func RegisterAppFlag(cmd *cobra.Command) {
cmd.Flags().Int(rpc.FlagRateLimitBurst, 1, "Set the concurrent count of requests allowed of rpc rate limiter")
cmd.Flags().Uint64(config.FlagGasLimitBuffer, 50, "Percentage to increase gas limit")
cmd.Flags().String(rpc.FlagDisableAPI, "", "Set the RPC API to be disabled, such as \"eth_getLogs,eth_newFilter,eth_newBlockFilter,eth_newPendingTransactionFilter,eth_getFilterChanges\"")

cmd.Flags().Int64(config.FlagDynamicGpMaxTxNum, 300, "If tx number in the block is more than this, the network is congested.")
cmd.Flags().Int64(config.FlagDynamicGpMaxGasUsed, 0, "If the block gas used is more than this, the network is congested.")
cmd.Flags().Int(config.FlagDynamicGpWeight, 80, "The recommended weight of dynamic gas price [1,100])")
cmd.Flags().Int(config.FlagDynamicGpCheckBlocks, 5, "The recommended number of blocks checked of dynamic gas price [1,100])")
cmd.Flags().Bool(config.FlagDynamicGpAdaptUncongest, true, "Default gas price will be recommended when the network is not congested")
cmd.Flags().Bool(config.FlagDynamicGpAdaptCongest, true, "Higher gas price will be recommended when the network is congested")
cmd.Flags().Int(config.FlagDynamicGpCoefficient, 1, "Adjustment coefficient of dynamic gas price [1,100])")
cmd.Flags().Bool(config.FlagEnableDynamicGp, true, "Enable node to dynamic support gas price suggest")

cmd.Flags().Bool(config.FlagEnableHasBlockPartMsg, false, "Enable peer to broadcast HasBlockPartMessage")
cmd.Flags().Bool(eth.FlagEnableMultiCall, false, "Enable node to support the eth_multiCall RPC API")

Expand Down

0 comments on commit 62c7834

Please sign in to comment.