Skip to content

Commit

Permalink
CR's fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 18, 2023
1 parent 8039d95 commit 40d0c59
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 31 deletions.
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ type Config struct {
MetricPartitioningPrefillPeriodDays int
RedisEndpointURL string
RedisPassword string
CacheTTL time.Duration
ChainID string
// TTL for cached evm requests
CacheTTL time.Duration
// CachePrefix is used as prefix for any key in the cache, key has such structure:
// query:<cache_prefix>:<method_name>:<keccak256(body)>
// Possible values are testnet, mainnet, etc...
CachePrefix string
}

const (
Expand Down Expand Up @@ -216,6 +220,6 @@ func ReadConfig() Config {
RedisEndpointURL: os.Getenv(REDIS_ENDPOINT_URL_ENVIRONMENT_KEY),
RedisPassword: os.Getenv(REDIS_PASSWORD_ENVIRONMENT_KEY),
CacheTTL: time.Duration(EnvOrDefaultInt(CACHE_TTL_ENVIRONMENT_KEY, DEFAULT_CACHE_TTL_SECONDS)) * time.Second,
ChainID: os.Getenv(CHAIN_ID_ENVIRONMENT_KEY),
CachePrefix: os.Getenv(CHAIN_ID_ENVIRONMENT_KEY),
}
}
12 changes: 6 additions & 6 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type ServiceCache struct {
blockGetter decode.EVMBlockGetter
cacheTTL time.Duration
decodedRequestContextKey any
// chainID is used as prefix for any key in the cache
chainID string
// cachePrefix is used as prefix for any key in the cache
cachePrefix string

*logging.ServiceLogger
}
Expand All @@ -29,15 +29,15 @@ func NewServiceCache(
blockGetter decode.EVMBlockGetter,
cacheTTL time.Duration,
decodedRequestContextKey any,
chainID string,
cachePrefix string,
logger *logging.ServiceLogger,
) *ServiceCache {
return &ServiceCache{
cacheClient: cacheClient,
blockGetter: blockGetter,
cacheTTL: cacheTTL,
decodedRequestContextKey: decodedRequestContextKey,
chainID: chainID,
cachePrefix: cachePrefix,
ServiceLogger: logger,
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *ServiceCache) GetCachedQueryResponse(
ctx context.Context,
req *decode.EVMRPCRequestEnvelope,
) ([]byte, error) {
key, err := GetQueryKey(c.chainID, req)
key, err := GetQueryKey(c.cachePrefix, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *ServiceCache) CacheQueryResponse(
return fmt.Errorf("response isn't cacheable")
}

key, err := GetQueryKey(c.chainID, req)
key, err := GetQueryKey(c.cachePrefix, req)
if err != nil {
return err
}
Expand Down
15 changes: 5 additions & 10 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
)

const (
defaultChainIDString = "1"
defaultHost = "api.kava.io"
defaultBlockNumber = "42"
defaultCachePrefixString = "1"
defaultBlockNumber = "42"
)

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

type MockEVMBlockGetter struct{}
Expand All @@ -40,10 +39,6 @@ func (c *MockEVMBlockGetter) BlockByHash(ctx context.Context, hash common.Hash)
panic("not implemented")
}

func (c *MockEVMBlockGetter) ChainID(ctx context.Context) (*big.Int, error) {
return defaultChainID, nil
}

func TestUnitTestIsCacheable(t *testing.T) {
logger, err := logging.New("TRACE")
require.NoError(t, err)
Expand Down Expand Up @@ -83,7 +78,7 @@ func TestUnitTestCacheQueryResponse(t *testing.T) {
cacheTTL := time.Hour
ctxb := context.Background()

serviceCache := cachemdw.NewServiceCache(inMemoryCache, blockGetter, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)
serviceCache := cachemdw.NewServiceCache(inMemoryCache, blockGetter, cacheTTL, service.DecodedRequestContextKey, defaultCachePrefixString, &logger)

req := mkEVMRPCRequestEnvelope(defaultBlockNumber)
resp, err := serviceCache.GetCachedQueryResponse(ctxb, req)
Expand Down
4 changes: 2 additions & 2 deletions service/cachemdw/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func BuildCacheKey(cacheItemType CacheItemType, parts []string) string {

// GetQueryKey calculates cache key for request
func GetQueryKey(
chainID string,
cachePrefix string,
req *decode.EVMRPCRequestEnvelope,
) (string, error) {
if req == nil {
Expand All @@ -58,7 +58,7 @@ func GetQueryKey(
hashedReq := crypto.Keccak256Hash(data)

parts := []string{
chainID,
cachePrefix,
req.Method,
hashedReq.Hex(),
}
Expand Down
16 changes: 8 additions & 8 deletions service/cachemdw/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func TestUnitTestBuildCacheKey(t *testing.T) {
func TestUnitTestGetQueryKey(t *testing.T) {
for _, tc := range []struct {
desc string
chainID string
cachePrefix string
req *decode.EVMRPCRequestEnvelope
expectedCacheKey string
errMsg string
}{
{
desc: "test case #1",
chainID: "chain1",
desc: "test case #1",
cachePrefix: "chain1",
req: &decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
ID: 1,
Expand All @@ -50,14 +50,14 @@ func TestUnitTestGetQueryKey(t *testing.T) {
expectedCacheKey: "query:chain1:eth_getBlockByHash:0xb2b69f976d9aa41cd2065e2a2354254f6cba682a6fe2b3996571daa27ea4a6f4",
},
{
desc: "test case #1",
chainID: "chain1",
req: nil,
errMsg: "request shouldn't be nil",
desc: "test case #1",
cachePrefix: "chain1",
req: nil,
errMsg: "request shouldn't be nil",
},
} {
t.Run(tc.desc, func(t *testing.T) {
cacheKey, err := cachemdw.GetQueryKey(tc.chainID, tc.req)
cacheKey, err := cachemdw.GetQueryKey(tc.cachePrefix, tc.req)
if tc.errMsg == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedCacheKey, cacheKey)
Expand Down
2 changes: 1 addition & 1 deletion service/cachemdw/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestE2ETestServiceCacheMiddleware(t *testing.T) {
blockGetter := NewMockEVMBlockGetter()
cacheTTL := time.Duration(0) // TTL: no expiry

serviceCache := cachemdw.NewServiceCache(inMemoryCache, blockGetter, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger)
serviceCache := cachemdw.NewServiceCache(inMemoryCache, blockGetter, cacheTTL, service.DecodedRequestContextKey, defaultCachePrefixString, &logger)

emptyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
cachingMdw := serviceCache.CachingMiddleware(emptyHandler)
Expand Down
2 changes: 1 addition & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func createServiceCache(
evmclient,
config.CacheTTL,
DecodedRequestContextKey,
config.ChainID,
config.CachePrefix,
logger,
)

Expand Down

0 comments on commit 40d0c59

Please sign in to comment.