Skip to content

Commit

Permalink
CR's fixes: remove chainID caching
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 10, 2023
1 parent 2082357 commit 17b05ae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 119 deletions.
68 changes: 5 additions & 63 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ServiceCache struct {
evmClient EVMClient
cacheTTL time.Duration
decodedRequestContextKey any
chainID string

*logging.ServiceLogger
}
Expand All @@ -31,13 +32,15 @@ func NewServiceCache(
evmClient EVMClient,
cacheTTL time.Duration,
decodedRequestContextKey any,
chainID string,
logger *logging.ServiceLogger,
) *ServiceCache {
return &ServiceCache{
cacheClient: cacheClient,
evmClient: evmClient,
cacheTTL: cacheTTL,
decodedRequestContextKey: decodedRequestContextKey,
chainID: chainID,
ServiceLogger: logger,
}
}
Expand All @@ -58,14 +61,8 @@ func IsCacheable(ctx context.Context, evmClient EVMClient, req *decode.EVMRPCReq
func (c *ServiceCache) GetCachedQueryResponse(
ctx context.Context,
req *decode.EVMRPCRequestEnvelope,
host string,
) ([]byte, error) {
chainID, err := c.GetAndCacheChainID(ctx, host)
if err != nil {
return nil, err
}

key, err := GetQueryKey(chainID, req)
key, err := GetQueryKey(c.chainID, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,73 +96,18 @@ func (c *ServiceCache) CacheQueryResponse(
func (c *ServiceCache) ValidateAndCacheQueryResponse(
ctx context.Context,
req *decode.EVMRPCRequestEnvelope,
host string,
response []byte,
) error {
// TODO(yevhenii): add validation

chainID, err := c.GetAndCacheChainID(ctx, host)
if err != nil {
return err
}

if err := c.CacheQueryResponse(
ctx,
req,
chainID,
c.chainID,
response,
); err != nil {
return err
}

return nil
}

func (c *ServiceCache) GetCachedChainID(
ctx context.Context,
host string,
) (string, error) {
key := GetChainKey(host)

chainID, err := c.cacheClient.Get(ctx, key)
if err != nil {
return "", err
}

return string(chainID), nil
}

func (c *ServiceCache) GetAndCacheChainID(
ctx context.Context,
host string,
) (string, error) {
chainID, err := c.GetCachedChainID(ctx, host)
if err != nil && err != cache.ErrNotFound {
return "", err
}
if err == cache.ErrNotFound {
rawChainID, err := c.evmClient.ChainID(ctx)
if err != nil {
return "", err
}

err = c.CacheChainID(ctx, host, rawChainID.String())
if err != nil {
return "", err
}

chainID = rawChainID.String()
}

return chainID, nil
}

func (c *ServiceCache) CacheChainID(
ctx context.Context,
host string,
chainID string,
) error {
key := GetChainKey(host)

return c.cacheClient.Set(ctx, key, []byte(chainID), c.cacheTTL)
}
57 changes: 7 additions & 50 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ func TestUnitTestCacheQueryResponse(t *testing.T) {
cacheTTL := time.Hour
ctxb := context.Background()

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)
serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)

req := mkEVMRPCRequestEnvelope(defaultBlockNumber)
resp, err := serviceCache.GetCachedQueryResponse(ctxb, req, defaultHost)
resp, err := serviceCache.GetCachedQueryResponse(ctxb, req)
require.Equal(t, cache.ErrNotFound, err)
require.Empty(t, resp)

err = serviceCache.CacheQueryResponse(ctxb, req, defaultChainIDString, defaultQueryResp)
require.NoError(t, err)

resp, err = serviceCache.GetCachedQueryResponse(ctxb, req, defaultHost)
resp, err = serviceCache.GetCachedQueryResponse(ctxb, req)
require.NoError(t, err)
require.Equal(t, defaultQueryResp, resp)
}
Expand All @@ -103,64 +103,21 @@ func TestUnitTestValidateAndCacheQueryResponse(t *testing.T) {
cacheTTL := time.Hour
ctxb := context.Background()

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)
serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)

req := mkEVMRPCRequestEnvelope(defaultBlockNumber)
resp, err := serviceCache.GetCachedQueryResponse(ctxb, req, defaultHost)
resp, err := serviceCache.GetCachedQueryResponse(ctxb, req)
require.Equal(t, cache.ErrNotFound, err)
require.Empty(t, resp)

err = serviceCache.ValidateAndCacheQueryResponse(ctxb, req, defaultHost, defaultQueryResp)
err = serviceCache.ValidateAndCacheQueryResponse(ctxb, req, defaultQueryResp)
require.NoError(t, err)

resp, err = serviceCache.GetCachedQueryResponse(ctxb, req, defaultHost)
resp, err = serviceCache.GetCachedQueryResponse(ctxb, req)
require.NoError(t, err)
require.Equal(t, defaultQueryResp, resp)
}

func TestUnitTestGetCachedChainID(t *testing.T) {
logger, err := logging.New("TRACE")
require.NoError(t, err)

inMemoryCache := cache.NewInMemoryCache()
evmClient := NewMockEVMClient()
cacheTTL := time.Hour
ctxb := context.Background()

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)

resp, err := serviceCache.GetCachedChainID(ctxb, defaultHost)
require.Error(t, cache.ErrNotFound, err)
require.Empty(t, resp)

err = serviceCache.CacheChainID(ctxb, defaultHost, defaultChainIDString)
require.NoError(t, err)

resp, err = serviceCache.GetCachedChainID(ctxb, defaultHost)
require.NoError(t, err)
require.Equal(t, defaultChainIDString, resp)
}

func TestUnitTestGetAndCacheChainID(t *testing.T) {
logger, err := logging.New("TRACE")
require.NoError(t, err)

inMemoryCache := cache.NewInMemoryCache()
evmClient := NewMockEVMClient()
cacheTTL := time.Hour
ctxb := context.Background()

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)

resp, err := serviceCache.GetAndCacheChainID(ctxb, defaultHost)
require.NoError(t, err)
require.Equal(t, defaultChainIDString, resp)

resp, err = serviceCache.GetCachedChainID(ctxb, defaultHost)
require.NoError(t, err)
require.Equal(t, defaultChainIDString, resp)
}

func mkEVMRPCRequestEnvelope(blockNumber string) *decode.EVMRPCRequestEnvelope {
return &decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
Expand Down
3 changes: 1 addition & 2 deletions service/cachemdw/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *ServiceCache) Middleware(
return
}

cachedQueryResponse, err := c.GetCachedQueryResponse(r.Context(), decodedReq, r.Host)
cachedQueryResponse, err := c.GetCachedQueryResponse(r.Context(), decodedReq)
if err == nil {
c.respond(next, w, r, cachedQueryResponse)
return
Expand Down Expand Up @@ -96,7 +96,6 @@ func (c *ServiceCache) respondAndCache(
if err := c.ValidateAndCacheQueryResponse(
r.Context(),
decodedReq,
r.Host,
body,
); err != nil {
c.Logger.Error().Msg(fmt.Sprintf("can't validate and cache response: %v", err))
Expand Down
7 changes: 3 additions & 4 deletions service/cachemdw/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestE2ETestServiceCacheMiddleware(t *testing.T) {
evmClient := NewMockEVMClient()
cacheTTL := time.Duration(0) // TTL: no expiry

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)
serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !IsRequestCached(r.Context()) {
Expand All @@ -47,8 +47,7 @@ func TestE2ETestServiceCacheMiddleware(t *testing.T) {
require.Equal(t, CacheMissHeaderValue, resp.Header().Get(CacheHeaderKey))

cacheItems := inMemoryCache.GetAll(context.Background())
require.Len(t, cacheItems, 2)
require.Contains(t, cacheItems, "chain:api.kava.io:8545")
require.Len(t, cacheItems, 1)
require.Contains(t, cacheItems, "query:1:0x5236d50a560cff0174f14be10bd00a21e8d73e89a200fbd219769b6aee297131")
})

Expand Down Expand Up @@ -76,7 +75,7 @@ func TestE2ETestServiceCacheMiddlewareInvalidRequestBody(t *testing.T) {
evmClient := NewMockEVMClient()
cacheTTL := time.Duration(0) // TTL: no expiry

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger)
serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)

req, err := http.NewRequest(http.MethodPost, "/test", nil)
require.NoError(t, err)
Expand Down

0 comments on commit 17b05ae

Please sign in to comment.