From fd7703e6ae9c7fee3f1780f22b9bff2a0d76a2b2 Mon Sep 17 00:00:00 2001 From: Evan Han Date: Tue, 3 Aug 2021 15:15:34 +0800 Subject: [PATCH] Merge PR: add rpc.disable-api flag (#934) * add rpc.disable-api flag * recover dev code * update go.mod * add nacos register tag --- app/rpc/apis.go | 15 +++++++++++++-- app/rpc/backend/backend.go | 11 ++++++++++- app/rpc/config.go | 3 ++- app/rpc/namespaces/eth/filters/api.go | 17 +++++++++++++++++ cmd/client/flags.go | 3 ++- go.mod | 2 +- go.sum | 7 ++----- x/stream/nacos/start.go | 3 +++ 8 files changed, 50 insertions(+), 11 deletions(-) diff --git a/app/rpc/apis.go b/app/rpc/apis.go index 470179247c..2494142c34 100644 --- a/app/rpc/apis.go +++ b/app/rpc/apis.go @@ -43,7 +43,8 @@ const ( func GetAPIs(clientCtx context.CLIContext, log log.Logger, keys ...ethsecp256k1.PrivKey) []rpc.API { nonceLock := new(rpctypes.AddrLocker) rateLimiters := getRateLimiter() - ethBackend := backend.New(clientCtx, log, rateLimiters) + disableAPI := getDisableAPI() + ethBackend := backend.New(clientCtx, log, rateLimiters, disableAPI) ethAPI := eth.NewAPI(clientCtx, log, ethBackend, nonceLock, keys...) if evmtypes.GetEnableBloomFilter() { server.TrapSignal(func() { @@ -105,7 +106,7 @@ func GetAPIs(clientCtx context.CLIContext, log log.Logger, keys ...ethsecp256k1. } func getRateLimiter() map[string]*rate.Limiter { - rateLimitApi := viper.GetString(FlagRateLimitApi) + rateLimitApi := viper.GetString(FlagRateLimitAPI) rateLimitCount := viper.GetInt(FlagRateLimitCount) rateLimitBurst := viper.GetInt(FlagRateLimitBurst) if rateLimitApi == "" || rateLimitCount == 0 { @@ -119,6 +120,16 @@ func getRateLimiter() map[string]*rate.Limiter { return rateLimiters } +func getDisableAPI() map[string]bool { + disableAPI := viper.GetString(FlagDisableAPI) + apiMap := make(map[string]bool) + apis := strings.Split(disableAPI, ",") + for _, api := range apis { + apiMap[api] = true + } + return apiMap +} + func makeMonitorMetrics(namespace string, service interface{}) { receiver := reflect.ValueOf(service) if !hasMetricsField(receiver.Elem()) { diff --git a/app/rpc/backend/backend.go b/app/rpc/backend/backend.go index 0a6bc8509d..140e1d8402 100644 --- a/app/rpc/backend/backend.go +++ b/app/rpc/backend/backend.go @@ -63,10 +63,11 @@ type EthermintBackend struct { closeBloomHandler chan struct{} wrappedBackend *watcher.Querier rateLimiters map[string]*rate.Limiter + disableAPI map[string]bool } // New creates a new EthermintBackend instance -func New(clientCtx clientcontext.CLIContext, log log.Logger, rateLimiters map[string]*rate.Limiter) *EthermintBackend { +func New(clientCtx clientcontext.CLIContext, log log.Logger, rateLimiters map[string]*rate.Limiter, disableAPI map[string]bool) *EthermintBackend { return &EthermintBackend{ ctx: context.Background(), clientCtx: clientCtx, @@ -76,6 +77,7 @@ func New(clientCtx clientcontext.CLIContext, log log.Logger, rateLimiters map[st closeBloomHandler: make(chan struct{}), wrappedBackend: watcher.NewQuerier(), rateLimiters: rateLimiters, + disableAPI: disableAPI, } } @@ -441,3 +443,10 @@ func (b *EthermintBackend) GetRateLimiter(apiName string) *rate.Limiter { } return b.rateLimiters[apiName] } + +func (b *EthermintBackend) IsDisabled(apiName string) bool { + if b.disableAPI == nil { + return false + } + return b.disableAPI[apiName] +} diff --git a/app/rpc/config.go b/app/rpc/config.go index c37df4c334..6e0210a67e 100644 --- a/app/rpc/config.go +++ b/app/rpc/config.go @@ -25,10 +25,11 @@ const ( flagWebsocket = "wsport" FlagPersonalAPI = "personal-api" - FlagRateLimitApi = "rpc.rate-limit-api" + FlagRateLimitAPI = "rpc.rate-limit-api" FlagRateLimitCount = "rpc.rate-limit-count" FlagRateLimitBurst = "rpc.rate-limit-burst" FlagEnableMonitor = "rpc.enable-monitor" + FlagDisableAPI = "rpc.disable-api" MetricsNamespace = "x" // MetricsSubsystem is a subsystem shared by all metrics exposed by this package. diff --git a/app/rpc/namespaces/eth/filters/api.go b/app/rpc/namespaces/eth/filters/api.go index 9266427dd2..9b124b0c6f 100644 --- a/app/rpc/namespaces/eth/filters/api.go +++ b/app/rpc/namespaces/eth/filters/api.go @@ -26,6 +26,7 @@ import ( ) var ErrServerBusy = errors.New("server is too busy") +var ErrMethodNotAllowed = errors.New("the method is not allowed") // Backend defines the methods requided by the PublicFilterAPI backend type Backend interface { @@ -39,6 +40,7 @@ type Backend interface { ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) GetBlockHashByHeight(height rpctypes.BlockNumber) (common.Hash, error) GetRateLimiter(apiName string) *rate.Limiter + IsDisabled(apiName string) bool } // consider a filter inactive if it has not been polled for within deadline @@ -120,6 +122,9 @@ func (api *PublicFilterAPI) timeoutLoop() { func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID { monitor := monitor.GetMonitor("eth_newPendingTransactionFilter", api.logger, api.Metrics).OnBegin() defer monitor.OnEnd() + if api.backend.IsDisabled("eth_newPendingTransactionFilter") { + return rpc.ID(fmt.Sprintf("error creating pending tx filter: %s", ErrMethodNotAllowed.Error())) + } rateLimiter := api.backend.GetRateLimiter("eth_newPendingTransactionFilter") if rateLimiter != nil && !rateLimiter.Allow() { return rpc.ID(fmt.Sprintf("error creating pending tx filter: %s", ErrServerBusy.Error())) @@ -214,6 +219,9 @@ func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Su func (api *PublicFilterAPI) NewBlockFilter() rpc.ID { monitor := monitor.GetMonitor("eth_newBlockFilter", api.logger, api.Metrics).OnBegin() defer monitor.OnEnd() + if api.backend.IsDisabled("eth_newBlockFilter") { + return rpc.ID(fmt.Sprintf("error creating block filter: %s", ErrMethodNotAllowed.Error())) + } rateLimiter := api.backend.GetRateLimiter("eth_newBlockFilter") if rateLimiter != nil && !rateLimiter.Allow() { return rpc.ID(fmt.Sprintf("error creating block filter: %s", ErrServerBusy.Error())) @@ -380,6 +388,9 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID, error) { monitor := monitor.GetMonitor("eth_newFilter", api.logger, api.Metrics).OnBegin() defer monitor.OnEnd("args", criteria) + if api.backend.IsDisabled("eth_newFilter") { + return rpc.ID(""), ErrMethodNotAllowed + } rateLimiter := api.backend.GetRateLimiter("eth_newFilter") if rateLimiter != nil && !rateLimiter.Allow() { return rpc.ID(""), ErrServerBusy @@ -443,6 +454,9 @@ func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID, func (api *PublicFilterAPI) GetLogs(ctx context.Context, criteria filters.FilterCriteria) ([]*ethtypes.Log, error) { monitor := monitor.GetMonitor("eth_getLogs", api.logger, api.Metrics).OnBegin() defer monitor.OnEnd("args", criteria) + if api.backend.IsDisabled("eth_getLogs") { + return nil, ErrMethodNotAllowed + } rateLimiter := api.backend.GetRateLimiter("eth_getLogs") if rateLimiter != nil && !rateLimiter.Allow() { return nil, ErrServerBusy @@ -546,6 +560,9 @@ func (api *PublicFilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*et func (api *PublicFilterAPI) GetFilterChanges(id rpc.ID) (interface{}, error) { monitor := monitor.GetMonitor("eth_getFilterChanges", api.logger, api.Metrics).OnBegin() defer monitor.OnEnd("id", id) + if api.backend.IsDisabled("eth_getFilterChanges") { + return nil, ErrMethodNotAllowed + } rateLimiter := api.backend.GetRateLimiter("eth_getFilterChanges") if rateLimiter != nil && !rateLimiter.Allow() { return nil, ErrServerBusy diff --git a/cmd/client/flags.go b/cmd/client/flags.go index 3ae7ac59f6..2338156e61 100644 --- a/cmd/client/flags.go +++ b/cmd/client/flags.go @@ -22,10 +22,11 @@ func RegisterAppFlag(cmd *cobra.Command) { cmd.Flags().String(stream.NacosTmrpcAppName, "", "Stream plugin`s tendermint rpc name in eureka or nacos") cmd.Flags().String(stream.RpcExternalAddr, "127.0.0.1:26657", "Set the rpc-server external ip and port, when it is launched by Docker (default \"127.0.0.1:26657\")") - 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().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().String(rpc.FlagDisableAPI, "", "Set the RPC API to be disabled, such as \"eth_getLogs,eth_newFilter,eth_newBlockFilter,eth_newPendingTransactionFilter,eth_getFilterChanges\"") 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") diff --git a/go.mod b/go.mod index 42aad4e3a8..f10929ab42 100644 --- a/go.mod +++ b/go.mod @@ -58,5 +58,5 @@ require ( replace ( github.com/cosmos/cosmos-sdk => github.com/okex/cosmos-sdk v0.39.3-0.20210727103206-6345fb1f29e8 github.com/tendermint/iavl => github.com/okex/iavl v0.14.3-exchain - github.com/tendermint/tendermint => github.com/okex/tendermint v0.33.9-exchain5 + github.com/tendermint/tendermint => github.com/okex/tendermint v0.33.9-exchain6 ) diff --git a/go.sum b/go.sum index e0d1440fad..ed54db7b9b 100644 --- a/go.sum +++ b/go.sum @@ -499,15 +499,12 @@ github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= -github.com/okex/cosmos-sdk v0.39.2-exchain8 h1:kyhTZQF9RIWR63s6cCRaWfvUnOX59QB2LJd/ylp2jso= -github.com/okex/cosmos-sdk v0.39.2-exchain8/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= -github.com/okex/cosmos-sdk v0.39.3-0.20210713020205-bc4366d6fc3a/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= github.com/okex/cosmos-sdk v0.39.3-0.20210727103206-6345fb1f29e8 h1:NrZgzvAloeUexA08O3Us75IqIa5CDr70Lk1wwPVo65w= github.com/okex/cosmos-sdk v0.39.3-0.20210727103206-6345fb1f29e8/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= github.com/okex/iavl v0.14.3-exchain h1:kwRIwpFD6B8mDDqoaxeUN3Pg2GW0Vr+sA+b86renWcA= github.com/okex/iavl v0.14.3-exchain/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU= -github.com/okex/tendermint v0.33.9-exchain5 h1:8nSeDVzcO+g2bFUMcT34Wkv0x/yEPD/eAFoSVExicQk= -github.com/okex/tendermint v0.33.9-exchain5/go.mod h1:EoGTbJUufUueNIigY3zyO6f7GOj29OdpFhuR8sxWdSU= +github.com/okex/tendermint v0.33.9-exchain6 h1:g2pinlQENrKwryRHwRCLOQygQ5Mq37vqxkEuAKcj72g= +github.com/okex/tendermint v0.33.9-exchain6/go.mod h1:EoGTbJUufUueNIigY3zyO6f7GOj29OdpFhuR8sxWdSU= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= diff --git a/x/stream/nacos/start.go b/x/stream/nacos/start.go index f0e3d3e2ff..2fd7ec58a9 100644 --- a/x/stream/nacos/start.go +++ b/x/stream/nacos/start.go @@ -2,6 +2,8 @@ package nacos import ( "fmt" + "strconv" + "time" "github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/common/constant" @@ -50,6 +52,7 @@ func StartNacosClient(logger log.Logger, urls string, namespace string, name str Ephemeral: true, Metadata: map[string]string{ "preserved.register.source": "GO", + "app_registry_tag": strconv.FormatInt(time.Now().Unix(), 10), }, }) if err != nil {