From 64dab786cf6f2e2ce808be612580576b5f7bb05b Mon Sep 17 00:00:00 2001 From: Felipe Andrade Date: Wed, 31 May 2023 10:48:21 -0700 Subject: [PATCH] add cache support for debug_getRawReceipts when request is for block hash --- proxyd/proxyd/README.md | 2 +- proxyd/proxyd/cache.go | 18 ++++++++++++++++++ proxyd/proxyd/cache_test.go | 23 +++++++++++++++++++++++ proxyd/proxyd/methods.go | 12 ++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/proxyd/proxyd/README.md b/proxyd/proxyd/README.md index e0735c0..d3bfd50 100644 --- a/proxyd/proxyd/README.md +++ b/proxyd/proxyd/README.md @@ -87,7 +87,7 @@ Cache use Redis and can be enabled for the following immutable methods: * `eth_getBlockByHash` * `eth_getTransactionByBlockHashAndIndex` * `eth_getUncleByBlockHashAndIndex` - +* `debug_getRawReceipts` (block hash only) ## Metrics diff --git a/proxyd/proxyd/cache.go b/proxyd/proxyd/cache.go index 9684b75..0a88f7b 100644 --- a/proxyd/proxyd/cache.go +++ b/proxyd/proxyd/cache.go @@ -2,6 +2,8 @@ package proxyd import ( "context" + "encoding/json" + "github.com/ethereum/go-ethereum/rpc" "strings" "time" @@ -124,6 +126,21 @@ type rpcCache struct { func newRPCCache(cache Cache) RPCCache { staticHandler := &StaticMethodHandler{cache: cache} + debugGetRawReceiptsHandler := &StaticMethodHandler{cache: cache, + filter: func(req *RPCReq) bool { + // cache only if the request is for a block hash + + var p []rpc.BlockNumberOrHash + err := json.Unmarshal(req.Params, &p) + if err != nil { + return false + } + if len(p) != 1 { + return false + } + return p[0].BlockHash != nil + }, + } handlers := map[string]RPCMethodHandler{ "eth_chainId": staticHandler, "net_version": staticHandler, @@ -132,6 +149,7 @@ func newRPCCache(cache Cache) RPCCache { "eth_getBlockByHash": staticHandler, "eth_getTransactionByBlockHashAndIndex": staticHandler, "eth_getUncleByBlockHashAndIndex": staticHandler, + "debug_getRawReceipts": debugGetRawReceiptsHandler, } return &rpcCache{ cache: cache, diff --git a/proxyd/proxyd/cache_test.go b/proxyd/proxyd/cache_test.go index 727cdc7..93d0b8e 100644 --- a/proxyd/proxyd/cache_test.go +++ b/proxyd/proxyd/cache_test.go @@ -101,6 +101,20 @@ func TestRPCCacheImmutableRPCs(t *testing.T) { }, name: "eth_getUncleByBlockHashAndIndex", }, + { + req: &RPCReq{ + JSONRPC: "2.0", + Method: "debug_getRawReceipts", + Params: mustMarshalJSON([]string{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"}), + ID: ID, + }, + res: &RPCRes{ + JSONRPC: "2.0", + Result: `{"debug_getRawReceipts":"!"}`, + ID: ID, + }, + name: "debug_getRawReceipts", + }, } for _, rpc := range rpcs { @@ -173,6 +187,15 @@ func TestRPCCacheUnsupportedMethod(t *testing.T) { ID: ID, }, }, + { + req: &RPCReq{ + JSONRPC: "2.0", + Method: "debug_getRawReceipts", + Params: mustMarshalJSON([]string{"0x100"}), + ID: ID, + }, + name: "debug_getRawReceipts", + }, } for _, rpc := range rpcs { diff --git a/proxyd/proxyd/methods.go b/proxyd/proxyd/methods.go index 60c0595..ffaf89d 100644 --- a/proxyd/proxyd/methods.go +++ b/proxyd/proxyd/methods.go @@ -17,8 +17,9 @@ type RPCMethodHandler interface { } type StaticMethodHandler struct { - cache Cache - m sync.RWMutex + cache Cache + m sync.RWMutex + filter func(*RPCReq) bool } func (e *StaticMethodHandler) key(req *RPCReq) string { @@ -33,6 +34,10 @@ func (e *StaticMethodHandler) GetRPCMethod(ctx context.Context, req *RPCReq) (*R if e.cache == nil { return nil, nil } + if e.filter != nil && !e.filter(req) { + return nil, nil + } + e.m.RLock() defer e.m.RUnlock() @@ -62,6 +67,9 @@ func (e *StaticMethodHandler) PutRPCMethod(ctx context.Context, req *RPCReq, res if e.cache == nil { return nil } + if e.filter != nil && !e.filter(req) { + return nil + } e.m.Lock() defer e.m.Unlock()