Skip to content

Commit e959c29

Browse files
committed
Prevent measuring request duration metric for invalid JSON-RPC endpoints
1 parent 1d6f908 commit e959c29

File tree

4 files changed

+74
-70
lines changed

4 files changed

+74
-70
lines changed

api/api.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -33,74 +33,6 @@ const maxFeeHistoryBlockCount = 1024
3333

3434
var baseFeesPerGas = big.NewInt(1)
3535

36-
// A map containing all the valid method names that are found
37-
// in the Ethereum JSON-RPC API specification.
38-
// Update accordingly if any new methods are added/removed.
39-
var validMethods = map[string]struct{}{
40-
// eth namespace
41-
"eth_blockNumber": {},
42-
"eth_syncing": {},
43-
"eth_sendRawTransaction": {},
44-
"eth_getBalance": {},
45-
"eth_getTransactionByHash": {},
46-
"eth_getTransactionByBlockHashAndIndex": {},
47-
"eth_getTransactionByBlockNumberAndIndex": {},
48-
"eth_getTransactionReceipt": {},
49-
"eth_getBlockByHash": {},
50-
"eth_getBlockByNumber": {},
51-
"eth_getBlockReceipts": {},
52-
"eth_getBlockTransactionCountByHash": {},
53-
"eth_getBlockTransactionCountByNumber": {},
54-
"eth_call": {},
55-
"eth_getLogs": {},
56-
"eth_getTransactionCount": {},
57-
"eth_estimateGas": {},
58-
"eth_getCode": {},
59-
"eth_feeHistory": {},
60-
"eth_getStorageAt": {},
61-
"eth_chainId": {},
62-
"eth_coinbase": {},
63-
"eth_gasPrice": {},
64-
"eth_getUncleCountByBlockHash": {},
65-
"eth_getUncleCountByBlockNumber": {},
66-
"eth_getUncleByBlockHashAndIndex": {},
67-
"eth_getUncleByBlockNumberAndIndex": {},
68-
"eth_maxPriorityFeePerGas": {},
69-
"eth_mining": {},
70-
"eth_hashrate": {},
71-
"eth_getProof": {},
72-
"eth_createAccessList": {},
73-
74-
// debug namespace
75-
"debug_traceTransaction": {},
76-
"debug_traceBlockByNumber": {},
77-
"debug_traceBlockByHash": {},
78-
"debug_traceCall": {},
79-
"debug_flowHeightByBlock": {},
80-
81-
// web3 namespace
82-
"web3_clientVersion": {},
83-
"web3_sha3": {},
84-
85-
// net namespace
86-
"net_listening": {},
87-
"net_peerCount": {},
88-
"net_version": {},
89-
90-
// txpool namespace
91-
"txpool_content": {},
92-
"txpool_contentFrom": {},
93-
"txpool_status": {},
94-
"txpool_inspect": {},
95-
}
96-
97-
// Returns whether the given method name is a valid method from
98-
// the Ethereum JSON-RPC API specification.
99-
func IsValidMethod(methodName string) bool {
100-
_, ok := validMethods[methodName]
101-
return ok
102-
}
103-
10436
var latestBlockNumberOrHash = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
10537

10638
func SupportedAPIs(

api/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
slogzerolog "github.com/samber/slog-zerolog"
2828

2929
"github.com/onflow/flow-evm-gateway/config"
30+
"github.com/onflow/flow-evm-gateway/eth"
3031
"github.com/onflow/flow-evm-gateway/metrics"
3132
errs "github.com/onflow/flow-evm-gateway/models/errors"
3233
)
@@ -253,7 +254,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
253254
// Do not log any debug info for methods that are not valid
254255
// JSON-RPC methods.
255256
if methodValue, ok := requestBody["method"]; ok {
256-
if methodStr, ok := methodValue.(string); ok && IsValidMethod(methodStr) {
257+
if methodStr, ok := methodValue.(string); ok && eth.IsValidMethod(methodStr) {
257258
h.logger.Debug().
258259
Str("IP", r.RemoteAddr).
259260
Str("url", r.URL.String()).

eth/utils.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package eth
2+
3+
// A map containing all the valid method names that are found
4+
// in the Ethereum JSON-RPC API specification.
5+
// Update accordingly if any new methods are added/removed.
6+
var validMethods = map[string]struct{}{
7+
// eth namespace
8+
"eth_blockNumber": {},
9+
"eth_syncing": {},
10+
"eth_sendRawTransaction": {},
11+
"eth_getBalance": {},
12+
"eth_getTransactionByHash": {},
13+
"eth_getTransactionByBlockHashAndIndex": {},
14+
"eth_getTransactionByBlockNumberAndIndex": {},
15+
"eth_getTransactionReceipt": {},
16+
"eth_getBlockByHash": {},
17+
"eth_getBlockByNumber": {},
18+
"eth_getBlockReceipts": {},
19+
"eth_getBlockTransactionCountByHash": {},
20+
"eth_getBlockTransactionCountByNumber": {},
21+
"eth_call": {},
22+
"eth_getLogs": {},
23+
"eth_getTransactionCount": {},
24+
"eth_estimateGas": {},
25+
"eth_getCode": {},
26+
"eth_feeHistory": {},
27+
"eth_getStorageAt": {},
28+
"eth_chainId": {},
29+
"eth_coinbase": {},
30+
"eth_gasPrice": {},
31+
"eth_getUncleCountByBlockHash": {},
32+
"eth_getUncleCountByBlockNumber": {},
33+
"eth_getUncleByBlockHashAndIndex": {},
34+
"eth_getUncleByBlockNumberAndIndex": {},
35+
"eth_maxPriorityFeePerGas": {},
36+
"eth_mining": {},
37+
"eth_hashrate": {},
38+
"eth_getProof": {},
39+
"eth_createAccessList": {},
40+
41+
// debug namespace
42+
"debug_traceTransaction": {},
43+
"debug_traceBlockByNumber": {},
44+
"debug_traceBlockByHash": {},
45+
"debug_traceCall": {},
46+
"debug_flowHeightByBlock": {},
47+
48+
// web3 namespace
49+
"web3_clientVersion": {},
50+
"web3_sha3": {},
51+
52+
// net namespace
53+
"net_listening": {},
54+
"net_peerCount": {},
55+
"net_version": {},
56+
57+
// txpool namespace
58+
"txpool_content": {},
59+
"txpool_contentFrom": {},
60+
"txpool_status": {},
61+
"txpool_inspect": {},
62+
}
63+
64+
// Returns whether the given method name is a valid method from
65+
// the Ethereum JSON-RPC API specification.
66+
func IsValidMethod(methodName string) bool {
67+
_, ok := validMethods[methodName]
68+
return ok
69+
}

metrics/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
"github.com/rs/zerolog"
12+
13+
"github.com/onflow/flow-evm-gateway/eth"
1214
)
1315

1416
// HttpHandler is a thin middleware for gathering metrics about http request.
@@ -34,7 +36,7 @@ func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3436
method, err := extractMethod(r, h.logger)
3537
if err != nil {
3638
h.logger.Debug().Err(err).Msg("error extracting method")
37-
} else {
39+
} else if eth.IsValidMethod(method) {
3840
start = time.Now()
3941
defer h.collector.MeasureRequestDuration(start, method)
4042
}

0 commit comments

Comments
 (0)