Skip to content

Commit

Permalink
Merge PR: add recommend gas price (#940)
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 default of gas-limit-buffer to 50

Co-authored-by: MengXiangJian <[email protected]>
Co-authored-by: evan.han <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2021
1 parent d999c96 commit 904281e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
39 changes: 31 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package app

import (
"fmt"
"github.com/spf13/viper"
"io"
"math/big"
"os"

bam "github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -60,7 +62,10 @@ func init() {
okexchain.SetBip44CoinType(config)
}

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

var (
// DefaultCLIHome sets the default home directories for the application CLI
Expand Down Expand Up @@ -120,6 +125,8 @@ var (
farm.YieldFarmingAccount: nil,
farm.MintFarmingAccount: {supply.Burner},
}

GlobalGpIndex = GasPriceIndex{}
)

var _ simapp.App = (*OKExChainApp)(nil)
Expand Down Expand Up @@ -167,6 +174,9 @@ type OKExChainApp struct {

// simulation manager
sm *module.SimulationManager

blockGasPrice []*big.Int
enableGpSuggest bool
}

// NewOKExChainApp returns a reference to a new initialized OKExChain application.
Expand Down Expand Up @@ -204,12 +214,13 @@ func NewOKExChainApp(
tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)

app := &OKExChainApp{
BaseApp: bApp,
cdc: cdc,
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
subspaces: make(map[string]params.Subspace),
BaseApp: bApp,
cdc: cdc,
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
subspaces: make(map[string]params.Subspace),
enableGpSuggest: viper.GetBool(EnableDynamicGp),
}

// init params keeper and subspaces
Expand Down Expand Up @@ -437,6 +448,11 @@ 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)
app.blockGasPrice = app.blockGasPrice[:0]
}

return app.mm.EndBlock(ctx, req)
}

Expand All @@ -450,6 +466,13 @@ func (app *OKExChainApp) DeliverTx(req abci.RequestDeliverTx) (res abci.Response
app.syncTx(req.Tx)
}

if app.enableGpSuggest {
tx, err := evm.TxDecoder(app.Codec())(req.Tx)
if err == nil {
app.blockGasPrice = append(app.blockGasPrice, tx.GetTxInfo(app.GetDeliverStateCtx()).GasPrice)
}
}

return resp
}

Expand Down Expand Up @@ -597,4 +620,4 @@ func NewAccHandler(ak auth.AccountKeeper) sdk.AccHandler {
) uint64 {
return ak.GetAccount(ctx, addr).GetSequence()
}
}
}
48 changes: 48 additions & 0 deletions app/gp_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app

import (
"math"
"math/big"
"sort"
)

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"`
}

func CalBlockGasPriceIndex(blockGasPrice []*big.Int) GasPriceIndex {
num := len(blockGasPrice)
if num == 0 {
return GasPriceIndex{}
}

sort.SliceStable(blockGasPrice, func(i, j int) bool {
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],
}

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

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
}
4 changes: 1 addition & 3 deletions app/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package backend
import (
"context"
"fmt"
"golang.org/x/time/rate"

"github.com/okex/exchain/x/evm/watcher"

"github.com/tendermint/tendermint/libs/log"
"golang.org/x/time/rate"

rpctypes "github.com/okex/exchain/app/rpc/types"
evmtypes "github.com/okex/exchain/x/evm/types"
Expand Down
29 changes: 27 additions & 2 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"github.com/okex/exchain/app"
"math/big"
"strconv"
"sync"
Expand Down Expand Up @@ -49,8 +50,9 @@ import (
)

const (
FlagGasLimitBuffer = "gas-limit-buffer"
CacheOfEthCallLru = 40960
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 @@ -71,6 +73,7 @@ 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 @@ -96,6 +99,7 @@ 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 @@ -228,6 +232,27 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
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)
}

return api.gasPrice
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/client/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/okex/exchain/app"
"github.com/okex/exchain/app/rpc"
"github.com/okex/exchain/app/rpc/namespaces/eth"
"github.com/okex/exchain/app/rpc/namespaces/eth/filters"
Expand All @@ -25,8 +26,10 @@ func RegisterAppFlag(cmd *cobra.Command) {
cmd.Flags().String(rpc.FlagRateLimitAPI, "", "Set the RPC API to be controlled by the rate limit policy, such as \"eth_getLogs,eth_newFilter,eth_newBlockFilter,eth_newPendingTransactionFilter,eth_getFilterChanges\"")
cmd.Flags().Int(rpc.FlagRateLimitCount, 0, "Set the count of requests allowed per second of rpc rate limiter")
cmd.Flags().Int(rpc.FlagRateLimitBurst, 1, "Set the concurrent count of requests allowed of rpc rate limiter")
cmd.Flags().Uint64(eth.FlagGasLimitBuffer, 30, "Percentage to increase gas limit")
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().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")
cmd.Flags().String(token.FlagOSSEndpoint, "", "The OSS datacenter endpoint such as http://oss-cn-hangzhou.aliyuncs.com")
Expand Down

0 comments on commit 904281e

Please sign in to comment.