From 54864d408ee7c55359966a1cf4033ec03154f7cb Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Mon, 9 Oct 2023 16:57:55 -0400 Subject: [PATCH] TMP Commit --- service/cachemdw/cache_test.go | 84 ++++++++++++++++------------------ 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/service/cachemdw/cache_test.go b/service/cachemdw/cache_test.go index 0a005f8..26082da 100644 --- a/service/cachemdw/cache_test.go +++ b/service/cachemdw/cache_test.go @@ -16,6 +16,16 @@ import ( "github.com/kava-labs/kava-proxy-service/service" ) +const ( + defaultChainIDString = "1" + defaultHost = "api.kava.io" + defaultBlockNumber = "42" +) + +var ( + defaultChainID = big.NewInt(1) +) + type MockEVMClient struct{} func NewMockEVMClient() *MockEVMClient { @@ -29,7 +39,7 @@ func (c *MockEVMClient) BlockByHash(ctx context.Context, hash common.Hash) (*eth } func (c *MockEVMClient) ChainID(ctx context.Context) (*big.Int, error) { - return big.NewInt(1), nil + return defaultChainID, nil } func TestUnitTestIsCacheable(t *testing.T) { @@ -41,23 +51,13 @@ func TestUnitTestIsCacheable(t *testing.T) { cacheable bool }{ { - desc: "test case #1", - req: &decode.EVMRPCRequestEnvelope{ - JSONRPCVersion: "2.0", - ID: 1, - Method: "eth_getBalance", - Params: []interface{}{"0x1234", "42"}, - }, + desc: "test case #1", + req: mkEVMRPCRequestEnvelope(defaultBlockNumber), cacheable: true, }, { - desc: "test case #2", - req: &decode.EVMRPCRequestEnvelope{ - JSONRPCVersion: "2.0", - ID: 1, - Method: "eth_getBalance", - Params: []interface{}{"0x1234", "0"}, - }, + desc: "test case #2", + req: mkEVMRPCRequestEnvelope("0"), cacheable: false, }, } { @@ -78,20 +78,15 @@ func TestUnitTestCacheQueryResponse(t *testing.T) { serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger) - req := decode.EVMRPCRequestEnvelope{ - JSONRPCVersion: "2.0", - ID: 1, - Method: "eth_getBalance", - Params: []interface{}{"0x1234", "42"}, - } - resp, err := serviceCache.GetCachedQueryResponse(context.Background(), &req, "api.kava.io") + req := mkEVMRPCRequestEnvelope(defaultBlockNumber) + resp, err := serviceCache.GetCachedQueryResponse(context.Background(), req, defaultHost) require.Equal(t, cache.ErrNotFound, err) require.Empty(t, resp) - err = serviceCache.CacheQueryResponse(context.Background(), &req, "1", []byte("resp")) + err = serviceCache.CacheQueryResponse(context.Background(), req, defaultChainIDString, []byte("resp")) require.NoError(t, err) - resp, err = serviceCache.GetCachedQueryResponse(context.Background(), &req, "api.kava.io") + resp, err = serviceCache.GetCachedQueryResponse(context.Background(), req, defaultHost) require.NoError(t, err) require.Equal(t, []byte("resp"), resp) } @@ -106,20 +101,15 @@ func TestUnitTestValidateAndCacheQueryResponse(t *testing.T) { serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger) - req := decode.EVMRPCRequestEnvelope{ - JSONRPCVersion: "2.0", - ID: 1, - Method: "eth_getBalance", - Params: []interface{}{"0x1234", "42"}, - } - resp, err := serviceCache.GetCachedQueryResponse(context.Background(), &req, "api.kava.io") + req := mkEVMRPCRequestEnvelope(defaultBlockNumber) + resp, err := serviceCache.GetCachedQueryResponse(context.Background(), req, defaultHost) require.Equal(t, cache.ErrNotFound, err) require.Empty(t, resp) - err = serviceCache.ValidateAndCacheQueryResponse(context.Background(), &req, "api.kava.io", []byte("resp")) + err = serviceCache.ValidateAndCacheQueryResponse(context.Background(), req, defaultHost, []byte("resp")) require.NoError(t, err) - resp, err = serviceCache.GetCachedQueryResponse(context.Background(), &req, "api.kava.io") + resp, err = serviceCache.GetCachedQueryResponse(context.Background(), req, defaultHost) require.NoError(t, err) require.Equal(t, []byte("resp"), resp) } @@ -134,16 +124,16 @@ func TestUnitTestGetCachedChainID(t *testing.T) { serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger) - resp, err := serviceCache.GetCachedChainID(context.Background(), "api.kava.io") + resp, err := serviceCache.GetCachedChainID(context.Background(), defaultHost) require.Error(t, cache.ErrNotFound, err) require.Empty(t, resp) - err = serviceCache.CacheChainID(context.Background(), "api.kava.io", "1") + err = serviceCache.CacheChainID(context.Background(), defaultHost, defaultChainIDString) require.NoError(t, err) - resp, err = serviceCache.GetCachedChainID(context.Background(), "api.kava.io") + resp, err = serviceCache.GetCachedChainID(context.Background(), defaultHost) require.NoError(t, err) - require.Equal(t, "1", resp) + require.Equal(t, defaultChainIDString, resp) } func TestUnitTestGetAndCacheChainID(t *testing.T) { @@ -156,14 +146,20 @@ func TestUnitTestGetAndCacheChainID(t *testing.T) { serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger) - resp, err := serviceCache.GetAndCacheChainID(context.Background(), "api.kava.io") + resp, err := serviceCache.GetAndCacheChainID(context.Background(), defaultHost) require.NoError(t, err) - require.Equal(t, "1", resp) - - //err := serviceCache.CacheChainID(context.Background(), "api.kava.io", "1") - //require.NoError(t, err) + require.Equal(t, defaultChainIDString, resp) - resp, err = serviceCache.GetCachedChainID(context.Background(), "api.kava.io") + resp, err = serviceCache.GetCachedChainID(context.Background(), defaultHost) require.NoError(t, err) - require.Equal(t, "1", resp) + require.Equal(t, defaultChainIDString, resp) +} + +func mkEVMRPCRequestEnvelope(blockNumber string) *decode.EVMRPCRequestEnvelope { + return &decode.EVMRPCRequestEnvelope{ + JSONRPCVersion: "2.0", + ID: 1, + Method: "eth_getBalance", + Params: []interface{}{"0x1234", blockNumber}, + } }