Skip to content

Commit

Permalink
TMP Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 9, 2023
1 parent 8e9fc62 commit 54864d4
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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,
},
} {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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},
}
}

0 comments on commit 54864d4

Please sign in to comment.