Skip to content

Commit

Permalink
Merge PR: add rpc.disable-api flag (#934)
Browse files Browse the repository at this point in the history
* add rpc.disable-api flag

* recover dev code

* update go.mod

* add nacos register tag
  • Loading branch information
ilovers authored Aug 3, 2021
1 parent 30ec3e4 commit fd7703e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 11 deletions.
15 changes: 13 additions & 2 deletions app/rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()) {
Expand Down
11 changes: 10 additions & 1 deletion app/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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]
}
3 changes: 2 additions & 1 deletion app/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions app/rpc/namespaces/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions x/stream/nacos/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fd7703e

Please sign in to comment.