Skip to content

Commit

Permalink
Merge PR: suggest gp (#944)
Browse files Browse the repository at this point in the history
* add recommend gas price

* update gpIndex flag

* use global var to store gas price index instand of watchdb

* default to support gasprice suggest

* update flag name

* update dynamic gp recommend strategy

* handle boundary exception of idx

Co-authored-by: MengXiangJian <[email protected]>
  • Loading branch information
louisliu2048 and xiangjianmeng authored Aug 6, 2021
1 parent 1dd8e75 commit 9fe8e4c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 48 deletions.
12 changes: 11 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {

const (
appName = "OKExChain"
DynamicGpWeight = "dynamic-gp-weight"
EnableDynamicGp = "enable-dynamic-gp"
)

Expand Down Expand Up @@ -177,6 +178,7 @@ type OKExChainApp struct {

blockGasPrice []*big.Int
enableGpSuggest bool
dynamicGpWeight int
}

// NewOKExChainApp returns a reference to a new initialized OKExChain application.
Expand Down Expand Up @@ -223,6 +225,14 @@ func NewOKExChainApp(
enableGpSuggest: viper.GetBool(EnableDynamicGp),
}

gpWeight := viper.GetInt(DynamicGpWeight)
if gpWeight == 0 {
gpWeight = 1
} else if gpWeight > 100 {
gpWeight = 100
}
app.dynamicGpWeight = gpWeight

// init params keeper and subspaces
app.ParamsKeeper = params.NewKeeper(cdc, keys[params.StoreKey], tkeys[params.TStoreKey])
app.subspaces[auth.ModuleName] = app.ParamsKeeper.Subspace(auth.DefaultParamspace)
Expand Down Expand Up @@ -449,7 +459,7 @@ func (app *OKExChainApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBloc
// EndBlocker updates every end block
func (app *OKExChainApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
if app.enableGpSuggest {
GlobalGpIndex = CalBlockGasPriceIndex(app.blockGasPrice)
GlobalGpIndex = CalBlockGasPriceIndex(app.blockGasPrice, app.dynamicGpWeight)
app.blockGasPrice = app.blockGasPrice[:0]
}

Expand Down
33 changes: 8 additions & 25 deletions app/gp_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import (
)

type GasPriceIndex struct {
Min *big.Int `json:"min"`
Q1 *big.Int `json:"q1"`
Q2 *big.Int `json:"q2"`
Q3 *big.Int `json:"q3"`
Max *big.Int `json:"max"`
RecommendGp *big.Int `json:"recommend-gp"`
}

func CalBlockGasPriceIndex(blockGasPrice []*big.Int) GasPriceIndex {
func CalBlockGasPriceIndex(blockGasPrice []*big.Int, weight int) GasPriceIndex {
num := len(blockGasPrice)
if num == 0 {
return GasPriceIndex{}
Expand All @@ -24,25 +20,12 @@ func CalBlockGasPriceIndex(blockGasPrice []*big.Int) GasPriceIndex {
return blockGasPrice[i].Cmp(blockGasPrice[j]) < 0
})

gpIndex := GasPriceIndex{
Min: blockGasPrice[0],
Q1: blockGasPrice[0],
Q2: blockGasPrice[0],
Q3: blockGasPrice[0],
Max: blockGasPrice[num-1],
idx := int(math.Round(float64(weight) / 100.0 * float64(num)))
if idx > 0 {
idx -= 1
}

if q1 := int(math.Round(float64(num) * 0.25)); q1 > 0 {
gpIndex.Q1 = blockGasPrice[q1-1]
return GasPriceIndex{
RecommendGp: blockGasPrice[idx],
}

if q2 := int(math.Round(float64(num) * 0.5)); q2 > 0 {
gpIndex.Q2 = blockGasPrice[q2-1]
}

if q3 := int(math.Round(float64(num) * 0.75)); q3 > 0 {
gpIndex.Q3 = blockGasPrice[q3-1]
}

return gpIndex
}
}
23 changes: 2 additions & 21 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
const (
FlagGasLimitBuffer = "gas-limit-buffer"
CacheOfEthCallLru = 40960
FlagGasPriceIndex = "gas-price-index"
)

// PublicEthereumAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
Expand All @@ -73,7 +72,6 @@ type PublicEthereumAPI struct {
Metrics map[string]*monitor.RpcMetrics
callCache *lru.Cache
gasLimitBuffer uint64
gasPriceIndex string
}

// NewAPI creates an instance of the public ETH Web3 API.
Expand All @@ -99,7 +97,6 @@ func NewAPI(
wrappedBackend: watcher.NewQuerier(),
watcherBackend: watcher.NewWatcher(),
gasLimitBuffer: viper.GetUint64(FlagGasLimitBuffer),
gasPriceIndex: viper.GetString(FlagGasPriceIndex),
}
api.evmFactory = simulation.NewEvmFactory(clientCtx.ChainID, api.wrappedBackend)

Expand Down Expand Up @@ -233,24 +230,8 @@ func (api *PublicEthereumAPI) GasPrice() *hexutil.Big {
monitor := monitor.GetMonitor("eth_gasPrice", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()

gpIndex := app.GlobalGpIndex
gp := gpIndex.Q3
switch api.gasPriceIndex {
case "Min":
gp = gpIndex.Min
case "Q1":
gp = gpIndex.Q1
case "Q2":
gp = gpIndex.Q2
case "Q3":
gp = gpIndex.Q3
case "Max":
gp = gpIndex.Max
default:
}

if gp != nil {
return (*hexutil.Big)(gp)
if app.GlobalGpIndex.RecommendGp != nil {
return (*hexutil.Big)(app.GlobalGpIndex.RecommendGp)
}

return api.gasPrice
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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(eth.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().String(eth.FlagGasPriceIndex, "Q3", "Recommend gas price to user [Min, Q1, Q2, Q3, Max]")
cmd.Flags().Int(app.DynamicGpWeight, 80, "The recommended weight of dynamic gas price [1,100])")
cmd.Flags().Bool(app.EnableDynamicGp, true, "Enable node to dynamic support gas price suggest")

cmd.Flags().Bool(token.FlagOSSEnable, false, "Enable the function of exporting account data and uploading to oss")
Expand Down

0 comments on commit 9fe8e4c

Please sign in to comment.