diff --git a/config/config.go b/config/config.go index 2963d37..baa784a 100644 --- a/config/config.go +++ b/config/config.go @@ -42,7 +42,7 @@ type Config struct { // TTL for cached evm requests CacheTTL time.Duration // CachePrefix is used as prefix for any key in the cache, key has such structure: - // query::: + // :evm-request::sha256: // Possible values are testnet, mainnet, etc... CachePrefix string } diff --git a/service/cachemdw/keys.go b/service/cachemdw/keys.go index 9a51b25..fb83a0c 100644 --- a/service/cachemdw/keys.go +++ b/service/cachemdw/keys.go @@ -1,33 +1,34 @@ package cachemdw import ( + "crypto/sha256" + "encoding/hex" "encoding/json" "fmt" "strings" - "github.com/ethereum/go-ethereum/crypto" - "github.com/kava-labs/kava-proxy-service/decode" ) type CacheItemType int const ( - CacheItemTypeQuery CacheItemType = iota + 1 + CacheItemTypeEVMRequest CacheItemType = iota + 1 ) func (t CacheItemType) String() string { switch t { - case CacheItemTypeQuery: - return "query" + case CacheItemTypeEVMRequest: + return "evm-request" default: return "unknown" } } -func BuildCacheKey(cacheItemType CacheItemType, parts []string) string { +func BuildCacheKey(cachePrefix string, cacheItemType CacheItemType, parts []string) string { fullParts := append( []string{ + cachePrefix, cacheItemType.String(), }, parts..., @@ -55,13 +56,14 @@ func GetQueryKey( data = append(data, []byte(req.Method)...) data = append(data, serializedParams...) - hashedReq := crypto.Keccak256Hash(data) + hashedReq := sha256.Sum256(data) + hashedReqInHex := hex.EncodeToString(hashedReq[:]) parts := []string{ - cachePrefix, req.Method, - hashedReq.Hex(), + "sha256", + hashedReqInHex, } - return BuildCacheKey(CacheItemTypeQuery, parts), nil + return BuildCacheKey(cachePrefix, CacheItemTypeEVMRequest, parts), nil } diff --git a/service/cachemdw/keys_test.go b/service/cachemdw/keys_test.go index 06cdd01..703dd94 100644 --- a/service/cachemdw/keys_test.go +++ b/service/cachemdw/keys_test.go @@ -12,19 +12,21 @@ import ( func TestUnitTestBuildCacheKey(t *testing.T) { for _, tc := range []struct { desc string + cachePrefix string cacheItemType cachemdw.CacheItemType parts []string expectedCacheKey string }{ { desc: "test case #1", - cacheItemType: cachemdw.CacheItemTypeQuery, + cachePrefix: "chain1", + cacheItemType: cachemdw.CacheItemTypeEVMRequest, parts: []string{"1", "2", "3"}, - expectedCacheKey: "query:1:2:3", + expectedCacheKey: "chain1:evm-request:1:2:3", }, } { t.Run(tc.desc, func(t *testing.T) { - cacheKey := cachemdw.BuildCacheKey(tc.cacheItemType, tc.parts) + cacheKey := cachemdw.BuildCacheKey(tc.cachePrefix, tc.cacheItemType, tc.parts) require.Equal(t, tc.expectedCacheKey, cacheKey) }) } @@ -47,7 +49,7 @@ func TestUnitTestGetQueryKey(t *testing.T) { Method: "eth_getBlockByHash", Params: []interface{}{"0x1234", true}, }, - expectedCacheKey: "query:chain1:eth_getBlockByHash:0xb2b69f976d9aa41cd2065e2a2354254f6cba682a6fe2b3996571daa27ea4a6f4", + expectedCacheKey: "chain1:evm-request:eth_getBlockByHash:sha256:2db366278f2cb463f92147bd888bdcad528b44baa94b7920fdff35f4c11ee617", }, { desc: "test case #1", diff --git a/service/cachemdw/middleware_test.go b/service/cachemdw/middleware_test.go index c08ad56..03e8eb3 100644 --- a/service/cachemdw/middleware_test.go +++ b/service/cachemdw/middleware_test.go @@ -65,7 +65,7 @@ func TestE2ETestServiceCacheMiddleware(t *testing.T) { cacheItems := inMemoryCache.GetAll(context.Background()) require.Len(t, cacheItems, 1) - require.Contains(t, cacheItems, "query:1:eth_getBlockByNumber:0x885d3d84b42d647be47d94a001428be7e88ab787251031ddbfb247a581d0505a") + require.Contains(t, cacheItems, "1:evm-request:eth_getBlockByNumber:sha256:bf79de57723b25b85391513b470ea6989e7c44dd9afc0c270ee961c9f12f578d") }) t.Run("cache hit", func(t *testing.T) {