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 6, 2023
1 parent 20709a4 commit e035e5f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
6 changes: 3 additions & 3 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func NewServiceCache(
}
}

func (c *ServiceCache) IsCacheable(ctx context.Context, req *decode.EVMRPCRequestEnvelope) bool {
blockNumber, err := req.ExtractBlockNumberFromEVMRPCRequest(ctx, c.evmClient)
func IsCacheable(ctx context.Context, evmClient EVMClient, req *decode.EVMRPCRequestEnvelope) bool {
blockNumber, err := req.ExtractBlockNumberFromEVMRPCRequest(ctx, evmClient)
if err != nil {
return false
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func (c *ServiceCache) CacheQueryResponse(
req *decode.EVMRPCRequestEnvelope,
response []byte,
) error {
if !c.IsCacheable(ctx, req) {
if !IsCacheable(ctx, c.evmClient, req) {
return errors.New("query isn't cacheable")
}

Expand Down
65 changes: 65 additions & 0 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cachemdw

import (
"context"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
ethctypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"

"github.com/kava-labs/kava-proxy-service/decode"
)

type MockEVMClient struct{}

func NewMockEVMClient() *MockEVMClient {
return &MockEVMClient{}
}

var _ EVMClient = (*MockEVMClient)(nil)

func (c *MockEVMClient) BlockByHash(ctx context.Context, hash common.Hash) (*ethctypes.Block, error) {
panic("not implemented")
}

func (c *MockEVMClient) ChainID(ctx context.Context) (*big.Int, error) {
panic("not implemented")
}

func TestUnitTestIsCacheable(t *testing.T) {
evmClient := NewMockEVMClient()

for _, tc := range []struct {
desc string
req *decode.EVMRPCRequestEnvelope
cacheable bool
}{
{
desc: "test case #1",
req: &decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
ID: 1,
Method: "eth_getBalance",
Params: []interface{}{"0x1234", "42"},
},
cacheable: true,
},
{
desc: "test case #2",
req: &decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
ID: 1,
Method: "eth_getBalance",
Params: []interface{}{"0x1234", "0"},
},
cacheable: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
cacheable := IsCacheable(context.Background(), evmClient, tc.req)
require.Equal(t, tc.cacheable, cacheable)
})
}
}
2 changes: 1 addition & 1 deletion service/cachemdw/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *ServiceCache) Middleware(
return
}

cacheable := c.IsCacheable(r.Context(), decodedReq)
cacheable := IsCacheable(r.Context(), c.evmClient, decodedReq)
if !cacheable {
next.ServeHTTP(w, r.WithContext(uncachedContext))
return
Expand Down

0 comments on commit e035e5f

Please sign in to comment.