Skip to content

Commit

Permalink
Merge pull request #1736 from kjeom/addEVMExecTimeout
Browse files Browse the repository at this point in the history
Add evm execution timeout
  • Loading branch information
jack committed Dec 9, 2022
2 parents 779a136 + 0f859dc commit b0bf10f
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 126 deletions.
2 changes: 1 addition & 1 deletion api/api_ethereum.go
Expand Up @@ -611,7 +611,7 @@ func (api *EthereumAPI) Call(ctx context.Context, args EthTransactionArgs, block
if rpcGasCap := bcAPI.RPCGasCap(); rpcGasCap != nil {
gasCap = rpcGasCap.Uint64()
}
result, _, status, err := EthDoCall(ctx, bcAPI, args, blockNrOrHash, overrides, localTxExecutionTime, gasCap)
result, _, status, err := EthDoCall(ctx, bcAPI, args, blockNrOrHash, overrides, bcAPI.RPCEVMTimeout(), gasCap)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions api/api_public_blockchain.go
Expand Up @@ -44,8 +44,7 @@ import (
)

const (
defaultGasPrice = 25 * params.Ston
localTxExecutionTime = 5 * time.Second
defaultGasPrice = 25 * params.Ston
)

var logger = log.NewModuleLogger(log.API)
Expand Down Expand Up @@ -364,7 +363,7 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
if rpcGasCap := s.b.RPCGasCap(); rpcGasCap != nil {
gasCap = rpcGasCap
}
result, _, _, status, err := DoCall(ctx, s.b, args, blockNrOrHash, vm.Config{}, localTxExecutionTime, gasCap)
result, _, _, status, err := DoCall(ctx, s.b, args, blockNrOrHash, vm.Config{}, s.b.RPCEVMTimeout(), gasCap)
if err != nil {
return nil, err
}
Expand All @@ -381,7 +380,7 @@ func (s *PublicBlockChainAPI) EstimateComputationCost(ctx context.Context, args
if rpcGasCap := s.b.RPCGasCap(); rpcGasCap != nil {
gasCap = rpcGasCap
}
_, _, computationCost, _, err := DoCall(ctx, s.b, args, blockNrOrHash, vm.Config{UseOpcodeComputationCost: true}, localTxExecutionTime, gasCap)
_, _, computationCost, _, err := DoCall(ctx, s.b, args, blockNrOrHash, vm.Config{UseOpcodeComputationCost: true}, s.b.RPCEVMTimeout(), gasCap)
return (hexutil.Uint64)(computationCost), err
}

Expand Down
6 changes: 4 additions & 2 deletions api/backend.go
Expand Up @@ -23,6 +23,7 @@ package api
import (
"context"
"math/big"
"time"

"github.com/klaytn/klaytn"
"github.com/klaytn/klaytn/accounts"
Expand Down Expand Up @@ -51,8 +52,9 @@ type Backend interface {
ChainDB() database.DBManager
EventMux() *event.TypeMux
AccountManager() accounts.AccountManager
RPCGasCap() *big.Int // global gas cap for klay_call over rpc: DoS protection
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
RPCEVMTimeout() time.Duration // global timeout for klay_call
RPCGasCap() *big.Int // global gas cap for klay_call over rpc: DoS protection
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
Engine() consensus.Engine
FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)

Expand Down
237 changes: 126 additions & 111 deletions api/mocks/backend_mock.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cmd/utils/config.go
Expand Up @@ -591,7 +591,9 @@ func (kCfg *KlayConfig) SetKlayConfig(ctx *cli.Context, stack *node.Node) {
if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
}

if ctx.GlobalIsSet(RPCGlobalEVMTimeoutFlag.Name) {
cfg.RPCEVMTimeout = ctx.GlobalDuration(RPCGlobalEVMTimeoutFlag.Name)
}
if ctx.GlobalIsSet(RPCGlobalEthTxFeeCapFlag.Name) {
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalEthTxFeeCapFlag.Name)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/flaggroup.go
Expand Up @@ -251,6 +251,7 @@ var FlagGroups = []FlagGroup{
RPCVirtualHostsFlag,
RPCApiFlag,
RPCGlobalGasCap,
RPCGlobalEVMTimeoutFlag,
RPCGlobalEthTxFeeCapFlag,
RPCConcurrencyLimit,
RPCNonEthCompatibleFlag,
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Expand Up @@ -563,6 +563,11 @@ var (
Usage: "Sets a cap on gas that can be used in klay_call/estimateGas",
EnvVar: "KLAYTN_RPC_GASCAP",
}
RPCGlobalEVMTimeoutFlag = cli.DurationFlag{
Name: "rpc.evmtimeout",
Usage: "Sets a timeout used for eth_call (0=infinite)",
EnvVar: "KLAYTN_RPC_EVMTIMEOUT",
}
RPCGlobalEthTxFeeCapFlag = cli.Float64Flag{
Name: "rpc.ethtxfeecap",
Usage: "Sets a cap on transaction fee (in klay) that can be sent via the eth namespace RPC APIs (0 = no cap)",
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/nodecmd/nodeflags.go
Expand Up @@ -214,6 +214,7 @@ var CommonRPCFlags = []cli.Flag{
altsrc.NewIntFlag(utils.RPCPortFlag),
altsrc.NewStringFlag(utils.RPCApiFlag),
altsrc.NewUint64Flag(utils.RPCGlobalGasCap),
altsrc.NewDurationFlag(utils.RPCGlobalEVMTimeoutFlag),
altsrc.NewFloat64Flag(utils.RPCGlobalEthTxFeeCapFlag),
altsrc.NewBoolFlag(utils.WSEnabledFlag),
altsrc.NewStringFlag(utils.WSListenAddrFlag),
Expand Down
5 changes: 5 additions & 0 deletions node/cn/api_backend.go
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"
"math/big"
"time"

"github.com/klaytn/klaytn"
"github.com/klaytn/klaytn/accounts"
Expand Down Expand Up @@ -335,6 +336,10 @@ func (b *CNAPIBackend) RPCGasCap() *big.Int {
return b.cn.config.RPCGasCap
}

func (b *CNAPIBackend) RPCEVMTimeout() time.Duration {
return b.cn.config.RPCEVMTimeout
}

func (b *CNAPIBackend) RPCTxFeeCap() float64 {
return b.cn.config.RPCTxFeeCap
}
Expand Down
6 changes: 5 additions & 1 deletion node/cn/config.go
Expand Up @@ -64,7 +64,8 @@ func GetDefaultConfig() *Config {
},
WsEndpoint: "localhost:8546",

Istanbul: *istanbul.DefaultConfig,
Istanbul: *istanbul.DefaultConfig,
RPCEVMTimeout: 5 * time.Second,
}
}

Expand Down Expand Up @@ -171,6 +172,9 @@ type Config struct {
// RPCGasCap is the global gas cap for eth-call variants.
RPCGasCap *big.Int `toml:",omitempty"`

// RPCEVMTimeout is the global timeout for klay/eth-call.
RPCEVMTimeout time.Duration

// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
// send-transction variants. The unit is klay.
// This is used by eth namespace RPC APIs
Expand Down
33 changes: 27 additions & 6 deletions node/cn/gen_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0bf10f

Please sign in to comment.