feat(proxyd): avoid caching debug_getRawReceipts responses with 0 receipts

This commit is contained in:
Felipe Andrade 2023-10-19 11:29:24 -07:00
parent 4e0100bbe9
commit 42b2f6db61
4 changed files with 30 additions and 8 deletions

@ -128,7 +128,7 @@ type rpcCache struct {
func newRPCCache(cache Cache) RPCCache {
staticHandler := &StaticMethodHandler{cache: cache}
debugGetRawReceiptsHandler := &StaticMethodHandler{cache: cache,
filter: func(req *RPCReq) bool {
filterGet: func(req *RPCReq) bool {
// cache only if the request is for a block hash
var p []rpc.BlockNumberOrHash
@ -141,6 +141,14 @@ func newRPCCache(cache Cache) RPCCache {
}
return p[0].BlockHash != nil
},
filterPut: func(req *RPCReq, res *RPCRes) bool {
// don't cache if response contains 0 receipts
rawReceipts, ok := res.Result.([]interface{})
if !ok {
return false
}
return len(rawReceipts) > 0
},
}
handlers := map[string]RPCMethodHandler{
"eth_chainId": staticHandler,

@ -17,9 +17,10 @@ type RPCMethodHandler interface {
}
type StaticMethodHandler struct {
cache Cache
m sync.RWMutex
filter func(*RPCReq) bool
cache Cache
m sync.RWMutex
filterGet func(*RPCReq) bool
filterPut func(*RPCReq, *RPCRes) bool
}
func (e *StaticMethodHandler) key(req *RPCReq) string {
@ -34,7 +35,7 @@ func (e *StaticMethodHandler) GetRPCMethod(ctx context.Context, req *RPCReq) (*R
if e.cache == nil {
return nil, nil
}
if e.filter != nil && !e.filter(req) {
if e.filterGet != nil && !e.filterGet(req) {
return nil, nil
}
@ -67,7 +68,12 @@ func (e *StaticMethodHandler) PutRPCMethod(ctx context.Context, req *RPCReq, res
if e.cache == nil {
return nil
}
if e.filter != nil && !e.filter(req) {
// if there is a filter on get, we don't want to cache it because its irretrievable
if e.filterGet != nil && !e.filterGet(req) {
return nil
}
// response filter
if e.filterPut != nil && !e.filterPut(req, res) {
return nil
}

@ -77,7 +77,7 @@ func (mh *MockedHandler) Handler(w http.ResponseWriter, req *http.Request) {
for _, r := range requests {
method := r["method"]
block := ""
if method == "eth_getBlockByNumber" {
if method == "eth_getBlockByNumber" || method == "debug_getRawReceipts" {
block = (r["params"].([]interface{})[0]).(string)
}

File diff suppressed because one or more lines are too long