Skip to content

Commit

Permalink
Add metric to count the rate-limited requests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Feb 21, 2025
1 parent da6552b commit 33fd540
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 111 deletions.
45 changes: 22 additions & 23 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/onflow/go-ethereum/rlp"
"github.com/onflow/go-ethereum/rpc"
"github.com/rs/zerolog"
"github.com/sethvargo/go-limiter"

evmTypes "github.com/onflow/flow-go/fvm/evm/types"

Expand Down Expand Up @@ -87,7 +86,7 @@ type BlockChainAPI struct {
transactions storage.TransactionIndexer
receipts storage.ReceiptIndexer
indexingResumedHeight uint64
limiter limiter.Store
rateLimiter RateLimiter
collector metrics.Collector
}

Expand All @@ -98,7 +97,7 @@ func NewBlockChainAPI(
blocks storage.BlockIndexer,
transactions storage.TransactionIndexer,
receipts storage.ReceiptIndexer,
ratelimiter limiter.Store,
rateLimiter RateLimiter,
collector metrics.Collector,
indexingResumedHeight uint64,
) *BlockChainAPI {
Expand All @@ -110,14 +109,14 @@ func NewBlockChainAPI(
transactions: transactions,
receipts: receipts,
indexingResumedHeight: indexingResumedHeight,
limiter: ratelimiter,
rateLimiter: rateLimiter,
collector: collector,
}
}

// BlockNumber returns the block number of the chain head.
func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error) {
if err := rateLimit(ctx, b.limiter, b.logger); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return 0, err
}

Expand All @@ -136,7 +135,7 @@ func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error)
// - currentBlock: block number this node is currently importing
// - highestBlock: block number of the highest block header this node has received from peers
func (b *BlockChainAPI) Syncing(ctx context.Context) (interface{}, error) {
if err := rateLimit(ctx, b.limiter, b.logger); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -176,7 +175,7 @@ func (b *BlockChainAPI) SendRawTransaction(
Str("input", input.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return common.Hash{}, err
}

Expand All @@ -201,7 +200,7 @@ func (b *BlockChainAPI) GetBalance(
Str("address", address.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand All @@ -228,7 +227,7 @@ func (b *BlockChainAPI) GetTransactionByHash(
Str("hash", hash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -257,7 +256,7 @@ func (b *BlockChainAPI) GetTransactionByBlockHashAndIndex(
Str("index", index.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -292,7 +291,7 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
Str("index", index.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -332,7 +331,7 @@ func (b *BlockChainAPI) GetTransactionReceipt(
Str("hash", hash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -366,7 +365,7 @@ func (b *BlockChainAPI) GetBlockByHash(
Str("hash", hash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -400,7 +399,7 @@ func (b *BlockChainAPI) GetBlockByNumber(
Str("blockNumber", blockNumber.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -437,7 +436,7 @@ func (b *BlockChainAPI) GetBlockReceipts(
Str("hash", blockNumberOrHash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -483,7 +482,7 @@ func (b *BlockChainAPI) GetBlockTransactionCountByHash(
Str("hash", blockHash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand All @@ -507,7 +506,7 @@ func (b *BlockChainAPI) GetBlockTransactionCountByNumber(
Str("number", blockNumber.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -544,7 +543,7 @@ func (b *BlockChainAPI) Call(
Str("args", fmt.Sprintf("%v", args)).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -592,7 +591,7 @@ func (b *BlockChainAPI) GetLogs(
Str("criteria", fmt.Sprintf("%v", criteria)).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -681,7 +680,7 @@ func (b *BlockChainAPI) GetTransactionCount(
Str("address", address.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -714,7 +713,7 @@ func (b *BlockChainAPI) EstimateGas(
Str("args", fmt.Sprintf("%v", args)).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return 0, err
}

Expand Down Expand Up @@ -763,7 +762,7 @@ func (b *BlockChainAPI) GetCode(
Str("address", address.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -879,7 +878,7 @@ func (b *BlockChainAPI) GetStorageAt(
Str("address", address.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
if err := b.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down
21 changes: 8 additions & 13 deletions api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import (
"github.com/onflow/go-ethereum/eth/tracers/logger"
"github.com/onflow/go-ethereum/rpc"
"github.com/rs/zerolog"
"github.com/sethvargo/go-limiter"

"github.com/onflow/flow-evm-gateway/config"
ethTypes "github.com/onflow/flow-evm-gateway/eth/types"
"github.com/onflow/flow-evm-gateway/metrics"
"github.com/onflow/flow-evm-gateway/models"
errs "github.com/onflow/flow-evm-gateway/models/errors"
"github.com/onflow/flow-evm-gateway/services/evm"
Expand Down Expand Up @@ -53,8 +51,7 @@ type DebugAPI struct {
receipts storage.ReceiptIndexer
client *requester.CrossSporkClient
config config.Config
collector metrics.Collector
limiter limiter.Store
rateLimiter RateLimiter
}

func NewDebugAPI(
Expand All @@ -66,8 +63,7 @@ func NewDebugAPI(
client *requester.CrossSporkClient,
config config.Config,
logger zerolog.Logger,
collector metrics.Collector,
limiter limiter.Store,
rateLimiter RateLimiter,
) *DebugAPI {
return &DebugAPI{
registerStore: registerStore,
Expand All @@ -78,8 +74,7 @@ func NewDebugAPI(
receipts: receipts,
client: client,
config: config,
collector: collector,
limiter: limiter,
rateLimiter: rateLimiter,
}
}

Expand All @@ -89,7 +84,7 @@ func (d *DebugAPI) TraceTransaction(
hash gethCommon.Hash,
config *tracers.TraceConfig,
) (json.RawMessage, error) {
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
if err := d.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand All @@ -101,7 +96,7 @@ func (d *DebugAPI) TraceBlockByNumber(
number rpc.BlockNumber,
config *tracers.TraceConfig,
) ([]*txTraceResult, error) {
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
if err := d.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand All @@ -118,7 +113,7 @@ func (d *DebugAPI) TraceBlockByHash(
hash gethCommon.Hash,
config *tracers.TraceConfig,
) ([]*txTraceResult, error) {
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
if err := d.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand All @@ -136,7 +131,7 @@ func (d *DebugAPI) TraceCall(
blockNrOrHash rpc.BlockNumberOrHash,
config *tracers.TraceCallConfig,
) (interface{}, error) {
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
if err := d.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -259,7 +254,7 @@ func (d *DebugAPI) FlowHeightByBlock(
ctx context.Context,
blockNrOrHash rpc.BlockNumberOrHash,
) (uint64, error) {
if err := rateLimit(ctx, d.limiter, d.logger); err != nil {
if err := d.rateLimiter.Apply(ctx); err != nil {
return 0, err
}

Expand Down
17 changes: 8 additions & 9 deletions api/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/onflow/go-ethereum/eth/filters"
"github.com/onflow/go-ethereum/rpc"
"github.com/rs/zerolog"
"github.com/sethvargo/go-limiter"

"github.com/onflow/flow-evm-gateway/config"
ethTypes "github.com/onflow/flow-evm-gateway/eth/types"
Expand Down Expand Up @@ -135,7 +134,7 @@ type PullAPI struct {
receipts storage.ReceiptIndexer
filters map[rpc.ID]filter
mux sync.Mutex
ratelimiter limiter.Store
rateLimiter RateLimiter
}

func NewPullAPI(
Expand All @@ -144,7 +143,7 @@ func NewPullAPI(
blocks storage.BlockIndexer,
transactions storage.TransactionIndexer,
receipts storage.ReceiptIndexer,
ratelimiter limiter.Store,
rateLimiter RateLimiter,
) *PullAPI {
api := &PullAPI{
logger: logger,
Expand All @@ -153,7 +152,7 @@ func NewPullAPI(
transactions: transactions,
receipts: receipts,
filters: make(map[rpc.ID]filter),
ratelimiter: ratelimiter,
rateLimiter: rateLimiter,
}

go api.filterExpiryChecker()
Expand All @@ -170,7 +169,7 @@ func (api *PullAPI) NewPendingTransactionFilter(
ctx context.Context,
fullTx *bool,
) (rpc.ID, error) {
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
if err := api.rateLimiter.Apply(ctx); err != nil {
return "", err
}

Expand Down Expand Up @@ -198,7 +197,7 @@ func (api *PullAPI) NewPendingTransactionFilter(
// NewBlockFilter creates a filter that fetches blocks that are imported into the chain.
// It is part of the filter package since polling goes with eth_getFilterChanges.
func (api *PullAPI) NewBlockFilter(ctx context.Context) (rpc.ID, error) {
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
if err := api.rateLimiter.Apply(ctx); err != nil {
return "", err
}

Expand Down Expand Up @@ -247,7 +246,7 @@ func (api *PullAPI) uninstallFilter(id rpc.ID) bool {
//
// In case "fromBlock" > "toBlock" an error is returned.
func (api *PullAPI) NewFilter(ctx context.Context, criteria filters.FilterCriteria) (rpc.ID, error) {
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
if err := api.rateLimiter.Apply(ctx); err != nil {
return "", err
}

Expand Down Expand Up @@ -291,7 +290,7 @@ func (api *PullAPI) GetFilterLogs(
ctx context.Context,
id rpc.ID,
) ([]*gethTypes.Log, error) {
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
if err := api.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -341,7 +340,7 @@ func (api *PullAPI) GetFilterLogs(
// For pending transaction and block filters the result is []common.Hash.
// (pending)Log filters return []Log.
func (api *PullAPI) GetFilterChanges(ctx context.Context, id rpc.ID) (any, error) {
if err := rateLimit(ctx, api.ratelimiter, api.logger); err != nil {
if err := api.rateLimiter.Apply(ctx); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 33fd540

Please sign in to comment.