Skip to content

Commit

Permalink
Improve IsCacheable method
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 20, 2023
1 parent 50e72d8 commit 07b4347
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
37 changes: 23 additions & 14 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,36 @@ func NewServiceCache(
// IsCacheable checks if EVM request is cacheable.
// In current implementation we consider request is cacheable if it has specific block height
func IsCacheable(
ctx context.Context,
blockGetter decode.EVMBlockGetter,
logger *logging.ServiceLogger,
req *decode.EVMRPCRequestEnvelope,
) bool {
blockNumber, err := req.ExtractBlockNumberFromEVMRPCRequest(ctx, blockGetter)
if err != nil {
logger.Logger.Error().
Err(err).
Msg("can't extract block number from EVM RPC request")
if req.Method == "" {
return false
}

// blockNumber <= 0 means magic tag was used, one of the "latest", "pending", "earliest", etc...
// as of now we don't cache requests with magic tags
if blockNumber <= 0 {
return false
if decode.MethodRequiresNoHistory(req.Method) {
return true
}

if decode.MethodHasBlockHashParam(req.Method) {
return true
}

if decode.MethodHasBlockNumberParam(req.Method) {
blockNumber, err := decode.ParseBlockNumberFromParams(req.Method, req.Params)
if err != nil {
logger.Logger.Error().
Err(err).
Msg("can't parse block number from params")
return false
}

// blockNumber < 0 means magic tag was used, one of the "latest", "pending", "earliest", etc...
// we cache requests without magic tag or with the earliest magic tag
return blockNumber > 0 || blockNumber == decode.BlockTagToNumberCodec[decode.BlockTagEarliest]
}

// block number is specified and it's not a magic tag - cache the request
return true
return false
}

// GetCachedQueryResponse calculates cache key for request and then tries to get it from cache.
Expand Down Expand Up @@ -97,7 +106,7 @@ func (c *ServiceCache) CacheQueryResponse(
responseInBytes []byte,
) error {
// don't cache uncacheable requests
if !IsCacheable(ctx, c.blockGetter, c.ServiceLogger, req) {
if !IsCacheable(c.ServiceLogger, req) {
return errors.New("query isn't cacheable")
}

Expand Down
9 changes: 2 additions & 7 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cachemdw_test

import (
"context"
"math/big"
"testing"
"time"

Expand All @@ -23,8 +22,7 @@ const (
)

var (
defaultCachePrefix = big.NewInt(1)
defaultQueryResp = []byte(testEVMQueries[TestRequestWeb3ClientVersion].ResponseBody)
defaultQueryResp = []byte(testEVMQueries[TestRequestWeb3ClientVersion].ResponseBody)
)

type MockEVMBlockGetter struct{}
Expand All @@ -43,9 +41,6 @@ func TestUnitTestIsCacheable(t *testing.T) {
logger, err := logging.New("TRACE")
require.NoError(t, err)

blockGetter := NewMockEVMBlockGetter()
ctxb := context.Background()

for _, tc := range []struct {
desc string
req *decode.EVMRPCRequestEnvelope
Expand All @@ -63,7 +58,7 @@ func TestUnitTestIsCacheable(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
cacheable := cachemdw.IsCacheable(ctxb, blockGetter, &logger, tc.req)
cacheable := cachemdw.IsCacheable(&logger, tc.req)
require.Equal(t, tc.cacheable, cacheable)
})
}
Expand Down
2 changes: 1 addition & 1 deletion service/cachemdw/caching_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *ServiceCache) CachingMiddleware(
}

isCached := IsRequestCached(r.Context())
cacheable := IsCacheable(r.Context(), c.blockGetter, c.ServiceLogger, decodedReq)
cacheable := IsCacheable(c.ServiceLogger, decodedReq)
response := r.Context().Value(ResponseContextKey)
typedResponse, ok := response.([]byte)

Expand Down

0 comments on commit 07b4347

Please sign in to comment.