Skip to content

Commit

Permalink
Merge pull request #24 from PositionExchange/feature/configable-evm-c…
Browse files Browse the repository at this point in the history
…all-timeout

Make evmCallTimeout configable
  • Loading branch information
dannyposi committed Feb 7, 2023
2 parents 3e87564 + ba8fe61 commit 67015a1
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 24 deletions.
6 changes: 6 additions & 0 deletions cmd/posichain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
utils.Logger().Warn().Err(err).Msg("Check Local Time Accuracy Error")
}

evmCallTimeoutMs := hc.RPCOpt.EvmCallTimeoutMs
if evmCallTimeoutMs <= 0 {
evmCallTimeoutMs = 5000
}
evmCallTimeout := time.Duration(evmCallTimeoutMs) * time.Millisecond
// Parse RPC config
nodeConfig.RPCServer = nodeconfig.RPCServerConfig{
HTTPEnabled: hc.HTTP.Enabled,
Expand All @@ -346,6 +351,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
RpcFilterFile: hc.RPCOpt.RpcFilterFile,
RateLimiterEnabled: hc.RPCOpt.RateLimterEnabled,
RequestsPerSecond: hc.RPCOpt.RequestsPerSecond,
EvmCallTimeout: evmCallTimeout,
}

// Parse rosetta config
Expand Down
1 change: 1 addition & 0 deletions internal/configs/harmony/harmony.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ type RpcOptConfig struct {
RpcFilterFile string // Define filters to enable/disable RPC exposure
RateLimterEnabled bool // Enable Rate limiter for RPC
RequestsPerSecond int // for RPC rate limiter
EvmCallTimeoutMs int64 // EVM call timeout in milliseconds
}

type DevnetConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/configs/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"strings"
"sync"
"time"

bls_core "github.com/PositionExchange/bls/ffi/go/bls"
"github.com/PositionExchange/posichain/crypto/bls"
Expand Down Expand Up @@ -118,6 +119,8 @@ type RPCServerConfig struct {

RateLimiterEnabled bool
RequestsPerSecond int

EvmCallTimeout time.Duration
}

// RosettaServerConfig is the config for the rosetta server
Expand Down
2 changes: 1 addition & 1 deletion node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (node *Node) StopRPC() error {
// StartRosetta start rosetta service
func (node *Node) StartRosetta() error {
harmony := hmy.New(node, node.TxPool, node.CxPool, node.Consensus.ShardID)
return rosetta.StartServers(harmony, node.NodeConfig.RosettaServer, node.NodeConfig.RPCServer.RateLimiterEnabled, node.NodeConfig.RPCServer.RequestsPerSecond)
return rosetta.StartServers(harmony, node.NodeConfig.RosettaServer, node.NodeConfig.RPCServer.RateLimiterEnabled, node.NodeConfig.RPCServer.RequestsPerSecond, node.NodeConfig.RPCServer.EvmCallTimeout)
}

// StopRosetta stops rosetta service
Expand Down
10 changes: 5 additions & 5 deletions rosetta/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var listener net.Listener

// StartServers starts the rosetta http server
// TODO (dm): optimize rosetta to use single flight & use extra caching type DB to avoid re-processing data
func StartServers(hmy *hmy.Harmony, config nodeconfig.RosettaServerConfig, limiterEnable bool, rateLimit int) error {
func StartServers(hmy *hmy.Harmony, config nodeconfig.RosettaServerConfig, limiterEnable bool, rateLimit int, evmCallTimeout time.Duration) error {
if !config.HTTPEnabled {
utils.Logger().Info().Msg("Rosetta http server disabled...")
return nil
Expand All @@ -43,7 +43,7 @@ func StartServers(hmy *hmy.Harmony, config nodeconfig.RosettaServerConfig, limit
return err
}

router := recoverMiddleware(server.CorsMiddleware(loggerMiddleware(getRouter(serverAsserter, hmy, limiterEnable, rateLimit))))
router := recoverMiddleware(server.CorsMiddleware(loggerMiddleware(getRouter(serverAsserter, hmy, limiterEnable, rateLimit, evmCallTimeout))))
utils.Logger().Info().
Int("port", config.HTTPPort).
Str("ip", config.HTTPIp).
Expand Down Expand Up @@ -77,14 +77,14 @@ func newHTTPServer(handler http.Handler) *http.Server {
}
}

func getRouter(asserter *asserter.Asserter, hmy *hmy.Harmony, limiterEnable bool, rateLimit int) http.Handler {
func getRouter(asserter *asserter.Asserter, hmy *hmy.Harmony, limiterEnable bool, rateLimit int, evmCallTimeout time.Duration) http.Handler {
return server.NewRouter(
server.NewAccountAPIController(services.NewAccountAPI(hmy), asserter),
server.NewBlockAPIController(services.NewBlockAPI(hmy), asserter),
server.NewMempoolAPIController(services.NewMempoolAPI(hmy), asserter),
server.NewNetworkAPIController(services.NewNetworkAPI(hmy), asserter),
server.NewConstructionAPIController(services.NewConstructionAPI(hmy), asserter),
server.NewCallAPIController(services.NewCallAPIService(hmy, limiterEnable, rateLimit), asserter),
server.NewConstructionAPIController(services.NewConstructionAPI(hmy, evmCallTimeout), asserter),
server.NewCallAPIController(services.NewCallAPIService(hmy, limiterEnable, rateLimit, evmCallTimeout), asserter),
server.NewEventsAPIController(services.NewEventAPI(hmy), asserter),
server.NewSearchAPIController(services.NewSearchAPI(hmy), asserter),
)
Expand Down
5 changes: 3 additions & 2 deletions rosetta/services/call_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"encoding/json"
"time"

ethCommon "github.com/ethereum/go-ethereum/common"

Expand Down Expand Up @@ -83,10 +84,10 @@ func (c *CallAPIService) Call(

}

func NewCallAPIService(hmy *hmy.Harmony, limiterEnable bool, rateLimit int) server.CallAPIServicer {
func NewCallAPIService(hmy *hmy.Harmony, limiterEnable bool, rateLimit int, evmCallTimeout time.Duration) server.CallAPIServicer {
return &CallAPIService{
hmy: hmy,
publicContractAPI: rpc2.NewPublicContractAPI(hmy, rpc2.V2, limiterEnable, rateLimit),
publicContractAPI: rpc2.NewPublicContractAPI(hmy, rpc2.V2, limiterEnable, rateLimit, evmCallTimeout),
publicStakingAPI: rpc2.NewPublicStakingAPI(hmy, rpc2.V2),
publicBlockChainAPI: rpc2.NewPublicBlockchainAPI(hmy, rpc2.V2, limiterEnable, rateLimit),
}
Expand Down
17 changes: 10 additions & 7 deletions rosetta/services/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/big"
"time"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
Expand All @@ -24,17 +25,19 @@ const (

// ConstructAPI implements the server.ConstructAPIServicer interface.
type ConstructAPI struct {
hmy *hmy.Harmony
signer hmyTypes.Signer
stakingSigner stakingTypes.Signer
hmy *hmy.Harmony
signer hmyTypes.Signer
stakingSigner stakingTypes.Signer
evmCallTimeout time.Duration
}

// NewConstructionAPI creates a new instance of a ConstructAPI.
func NewConstructionAPI(hmy *hmy.Harmony) server.ConstructionAPIServicer {
func NewConstructionAPI(hmy *hmy.Harmony, evmCallTimeout time.Duration) server.ConstructionAPIServicer {
return &ConstructAPI{
hmy: hmy,
signer: hmyTypes.NewEIP155Signer(new(big.Int).SetUint64(hmy.ChainID)),
stakingSigner: stakingTypes.NewEIP155Signer(new(big.Int).SetUint64(hmy.ChainID)),
hmy: hmy,
signer: hmyTypes.NewEIP155Signer(new(big.Int).SetUint64(hmy.ChainID)),
stakingSigner: stakingTypes.NewEIP155Signer(new(big.Int).SetUint64(hmy.ChainID)),
evmCallTimeout: evmCallTimeout,
}
}

Expand Down
2 changes: 1 addition & 1 deletion rosetta/services/construction_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (s *ConstructAPI) ConstructionMetadata(
callArgs.To = &contractAddress
}
evmExe, err := rpc.DoEVMCall(
ctx, s.hmy, callArgs, latest, rpc.CallTimeout,
ctx, s.hmy, callArgs, latest, s.evmCallTimeout,
)
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
Expand Down
9 changes: 5 additions & 4 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type PublicContractService struct {
hmy *hmy.Harmony
version Version
// TEMP SOLUTION to rpc node spamming issue
limiterCall *rate.Limiter
limiterCall *rate.Limiter
evmCallTimeout time.Duration
}

// NewPublicContractAPI creates a new API for the RPC interface
func NewPublicContractAPI(hmy *hmy.Harmony, version Version, limiterEnable bool, limit int) rpc.API {
func NewPublicContractAPI(hmy *hmy.Harmony, version Version, limiterEnable bool, limit int, evmCallTimeout time.Duration) rpc.API {
var limiter *rate.Limiter
if limiterEnable {
limiter = rate.NewLimiter(rate.Limit(limit), limit)
Expand All @@ -44,7 +45,7 @@ func NewPublicContractAPI(hmy *hmy.Harmony, version Version, limiterEnable bool,
return rpc.API{
Namespace: version.Namespace(),
Version: APIVersion,
Service: &PublicContractService{hmy, version, limiter},
Service: &PublicContractService{hmy, version, limiter, evmCallTimeout},
Public: true,
}
}
Expand Down Expand Up @@ -80,7 +81,7 @@ func (s *PublicContractService) Call(
}

// Execute call
result, err := DoEVMCall(ctx, s.hmy, args, blockNrOrHash, CallTimeout)
result, err := DoEVMCall(ctx, s.hmy, args, blockNrOrHash, s.evmCallTimeout)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const (
const (
// APIVersion used for DApp's, bumped after RPC refactor (7/2020)
APIVersion = "1.1"
// CallTimeout is the timeout given to all contract calls
// Deprecated: CallTimeout is the timeout given to all contract calls
// Use RpcOptConfig instead
CallTimeout = 5 * time.Second
// LogTag is the tag found in the log for all RPC logs
LogTag = "[RPC]"
Expand Down Expand Up @@ -158,8 +159,8 @@ func getAPIs(hmy *hmy.Harmony, config nodeconfig.RPCServerConfig) []rpc.API {
NewPublicHarmonyAPI(hmy, V2),
NewPublicBlockchainAPI(hmy, V1, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicBlockchainAPI(hmy, V2, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(hmy, V1, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(hmy, V2, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(hmy, V1, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicContractAPI(hmy, V2, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicTransactionAPI(hmy, V1),
NewPublicTransactionAPI(hmy, V2),
NewPublicPoolAPI(hmy, V1, config.RateLimiterEnabled, config.RequestsPerSecond),
Expand All @@ -185,7 +186,7 @@ func getAPIs(hmy *hmy.Harmony, config nodeconfig.RPCServerConfig) []rpc.API {
publicAPIs = append(publicAPIs,
NewPublicHarmonyAPI(hmy, Eth),
NewPublicBlockchainAPI(hmy, Eth, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(hmy, Eth, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(hmy, Eth, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicTransactionAPI(hmy, Eth),
NewPublicPoolAPI(hmy, Eth, config.RateLimiterEnabled, config.RequestsPerSecond),
eth.NewPublicEthService(hmy, "eth"),
Expand Down

0 comments on commit 67015a1

Please sign in to comment.