rpc: fix gas price (#568)

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Thomas Nguy 2021-09-18 08:47:13 +09:00 committed by GitHub
parent b59d9a23ea
commit b8ae5984c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 7 deletions

View File

@ -56,6 +56,7 @@ type Backend interface {
GetTxByEthHash(txHash common.Hash) (*tmrpctypes.ResultTx, error) GetTxByEthHash(txHash common.Hash) (*tmrpctypes.ResultTx, error)
EstimateGas(args evmtypes.CallArgs, blockNrOptional *types.BlockNumber) (hexutil.Uint64, error) EstimateGas(args evmtypes.CallArgs, blockNrOptional *types.BlockNumber) (hexutil.Uint64, error)
RPCGasCap() uint64 RPCGasCap() uint64
RPCMinGasPrice() int64
} }
var _ Backend = (*EVMBackend)(nil) var _ Backend = (*EVMBackend)(nil)
@ -77,10 +78,7 @@ func NewEVMBackend(ctx *server.Context, logger log.Logger, clientCtx client.Cont
panic(err) panic(err)
} }
appConf, err := config.ParseConfig(ctx.Viper) appConf := config.GetConfig(ctx.Viper)
if err != nil {
panic(err)
}
return &EVMBackend{ return &EVMBackend{
ctx: context.Background(), ctx: context.Background(),
@ -88,7 +86,7 @@ func NewEVMBackend(ctx *server.Context, logger log.Logger, clientCtx client.Cont
queryClient: types.NewQueryClient(clientCtx), queryClient: types.NewQueryClient(clientCtx),
logger: logger.With("module", "evm-backend"), logger: logger.With("module", "evm-backend"),
chainID: chainID, chainID: chainID,
cfg: *appConf, cfg: appConf,
} }
} }
@ -700,3 +698,18 @@ func (e *EVMBackend) GetTransactionCount(address common.Address, blockNum types.
func (e *EVMBackend) RPCGasCap() uint64 { func (e *EVMBackend) RPCGasCap() uint64 {
return e.cfg.JSONRPC.GasCap return e.cfg.JSONRPC.GasCap
} }
// RPCMinGasPrice return the minimum gas price for a transaction.
func (e *EVMBackend) RPCMinGasPrice() int64 {
evmParams, err := e.queryClient.Params(context.Background(), &evmtypes.QueryParamsRequest{})
if err == nil {
minGasPrice := e.cfg.GetMinGasPrices()
for _, coin := range minGasPrice {
if coin.Denom == evmParams.Params.EvmDenom {
return coin.Amount.TruncateInt64()
}
}
}
return ethermint.DefaultGasPrice
}

View File

@ -23,7 +23,7 @@ import (
func (e *EVMBackend) setTxDefaults(args types.SendTxArgs) (types.SendTxArgs, error) { func (e *EVMBackend) setTxDefaults(args types.SendTxArgs) (types.SendTxArgs, error) {
if args.GasPrice == nil { if args.GasPrice == nil {
// TODO: Suggest a gas price based on the previous included txs // TODO: Suggest a gas price based on the previous included txs
args.GasPrice = (*hexutil.Big)(new(big.Int).SetUint64(e.RPCGasCap())) args.GasPrice = (*hexutil.Big)(new(big.Int).SetInt64(e.RPCMinGasPrice()))
} }
if args.Nonce == nil { if args.Nonce == nil {

View File

@ -164,7 +164,7 @@ func (e *PublicAPI) Hashrate() hexutil.Uint64 {
// GasPrice returns the current gas price based on Ethermint's gas price oracle. // GasPrice returns the current gas price based on Ethermint's gas price oracle.
func (e *PublicAPI) GasPrice() *hexutil.Big { func (e *PublicAPI) GasPrice() *hexutil.Big {
e.logger.Debug("eth_gasPrice") e.logger.Debug("eth_gasPrice")
out := new(big.Int).SetUint64(e.backend.RPCGasCap()) out := new(big.Int).SetInt64(e.backend.RPCMinGasPrice())
return (*hexutil.Big)(out) return (*hexutil.Big)(out)
} }

View File

@ -221,6 +221,7 @@ func New(t *testing.T, cfg Config) *Network {
appCfg.Telemetry.Enabled = false appCfg.Telemetry.Enabled = false
ctx := server.NewDefaultContext() ctx := server.NewDefaultContext()
tmCfg := ctx.Config tmCfg := ctx.Config
tmCfg.Consensus.TimeoutCommit = cfg.TimeoutCommit tmCfg.Consensus.TimeoutCommit = cfg.TimeoutCommit
@ -359,6 +360,13 @@ func New(t *testing.T, cfg Config) *Network {
require.NoError(t, writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz)) require.NoError(t, writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz))
config.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), appCfg) config.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), appCfg)
ctx.Viper.AddConfigPath(fmt.Sprintf("%s/config", nodeDir))
ctx.Viper.SetConfigName("app")
ctx.Viper.SetConfigType("toml")
err = ctx.Viper.ReadInConfig()
if err != nil {
panic(err)
}
clientCtx := client.Context{}. clientCtx := client.Context{}.
WithKeyringDir(nodeDir). WithKeyringDir(nodeDir).

View File

@ -19,6 +19,9 @@ const (
// BaseDenomUnit defines the base denomination unit for Photons. // BaseDenomUnit defines the base denomination unit for Photons.
// 1 photon = 1x10^{BaseDenomUnit} aphoton // 1 photon = 1x10^{BaseDenomUnit} aphoton
BaseDenomUnit = 18 BaseDenomUnit = 18
// DefaultGasPrice is default gas price for evm transactions
DefaultGasPrice = 20
) )
// PowerReduction defines the default power reduction value for staking // PowerReduction defines the default power reduction value for staking