cache_test.go 4.26 KB
Newer Older
inphi's avatar
inphi committed
1 2 3 4 5 6 7 8 9 10
package proxyd

import (
	"context"
	"strconv"
	"testing"

	"github.com/stretchr/testify/require"
)

inphi's avatar
inphi committed
11
func TestRPCCacheImmutableRPCs(t *testing.T) {
inphi's avatar
inphi committed
12 13
	ctx := context.Background()

14
	cache := newRPCCache(newMemoryCache())
inphi's avatar
inphi committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	ID := []byte(strconv.Itoa(1))

	rpcs := []struct {
		req  *RPCReq
		res  *RPCRes
		name string
	}{
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_chainId",
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  "0xff",
				ID:      ID,
			},
			name: "eth_chainId",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "net_version",
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  "9999",
				ID:      ID,
			},
			name: "net_version",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
51 52
				Method:  "eth_getBlockTransactionCountByHash",
				Params:  mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"}),
inphi's avatar
inphi committed
53 54 55 56
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
57
				Result:  `{"eth_getBlockTransactionCountByHash":"!"}`,
inphi's avatar
inphi committed
58 59
				ID:      ID,
			},
60
			name: "eth_getBlockTransactionCountByHash",
inphi's avatar
inphi committed
61 62 63 64
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
65 66
				Method:  "eth_getUncleCountByBlockHash",
				Params:  mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"}),
inphi's avatar
inphi committed
67 68
				ID:      ID,
			},
Felipe Andrade's avatar
Felipe Andrade committed
69 70
			res: &RPCRes{
				JSONRPC: "2.0",
71
				Result:  `{"eth_getUncleCountByBlockHash":"!"}`,
Felipe Andrade's avatar
Felipe Andrade committed
72 73
				ID:      ID,
			},
74
			name: "eth_getUncleCountByBlockHash",
75 76 77
		},
		{
			req: &RPCReq{
inphi's avatar
inphi committed
78
				JSONRPC: "2.0",
79 80
				Method:  "eth_getBlockByHash",
				Params:  mustMarshalJSON([]string{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "false"}),
81 82
				ID:      ID,
			},
83
			res: &RPCRes{
84
				JSONRPC: "2.0",
85
				Result:  `{"eth_getBlockByHash":"!"}`,
86 87
				ID:      ID,
			},
88
			name: "eth_getBlockByHash",
89
		},
inphi's avatar
inphi committed
90 91 92
		{
			req: &RPCReq{
				JSONRPC: "2.0",
93 94
				Method:  "eth_getUncleByBlockHashAndIndex",
				Params:  mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238", "0x90"}),
inphi's avatar
inphi committed
95 96 97 98
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
99
				Result:  `{"eth_getUncleByBlockHashAndIndex":"!"}`,
inphi's avatar
inphi committed
100 101
				ID:      ID,
			},
102
			name: "eth_getUncleByBlockHashAndIndex",
inphi's avatar
inphi committed
103
		},
104 105 106 107 108 109 110 111 112 113 114 115 116 117
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "debug_getRawReceipts",
				Params:  mustMarshalJSON([]string{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"}),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `{"debug_getRawReceipts":"!"}`,
				ID:      ID,
			},
			name: "debug_getRawReceipts",
		},
inphi's avatar
inphi committed
118 119 120 121
	}

	for _, rpc := range rpcs {
		t.Run(rpc.name, func(t *testing.T) {
122
			err := cache.PutRPC(ctx, rpc.req, rpc.res)
inphi's avatar
inphi committed
123 124 125 126
			require.NoError(t, err)

			cachedRes, err := cache.GetRPC(ctx, rpc.req)
			require.NoError(t, err)
127
			require.Equal(t, rpc.res, cachedRes)
inphi's avatar
inphi committed
128 129 130 131
		})
	}
}

132
func TestRPCCacheUnsupportedMethod(t *testing.T) {
inphi's avatar
inphi committed
133 134
	ctx := context.Background()

135
	cache := newRPCCache(newMemoryCache())
inphi's avatar
inphi committed
136 137 138 139 140 141 142
	ID := []byte(strconv.Itoa(1))

	rpcs := []struct {
		req  *RPCReq
		name string
	}{
		{
143
			name: "eth_syncing",
inphi's avatar
inphi committed
144 145
			req: &RPCReq{
				JSONRPC: "2.0",
146
				Method:  "eth_syncing",
inphi's avatar
inphi committed
147 148 149 150
				ID:      ID,
			},
		},
		{
151
			name: "eth_blockNumber",
inphi's avatar
inphi committed
152 153
			req: &RPCReq{
				JSONRPC: "2.0",
154
				Method:  "eth_blockNumber",
inphi's avatar
inphi committed
155 156 157 158
				ID:      ID,
			},
		},
		{
159
			name: "eth_getBlockByNumber",
inphi's avatar
inphi committed
160 161
			req: &RPCReq{
				JSONRPC: "2.0",
162
				Method:  "eth_getBlockByNumber",
inphi's avatar
inphi committed
163 164 165
				ID:      ID,
			},
		},
inphi's avatar
inphi committed
166
		{
167
			name: "eth_getBlockRange",
inphi's avatar
inphi committed
168 169 170 171 172 173 174
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				ID:      ID,
			},
		},
		{
175
			name: "eth_gasPrice",
inphi's avatar
inphi committed
176 177
			req: &RPCReq{
				JSONRPC: "2.0",
178
				Method:  "eth_gasPrice",
inphi's avatar
inphi committed
179 180
				ID:      ID,
			},
181 182 183 184
		},
		{
			name: "eth_call",
			req: &RPCReq{
inphi's avatar
inphi committed
185
				JSONRPC: "2.0",
186
				Method:  "eth_gasPrice",
inphi's avatar
inphi committed
187 188 189
				ID:      ID,
			},
		},
190 191 192 193 194 195 196 197 198
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "debug_getRawReceipts",
				Params:  mustMarshalJSON([]string{"0x100"}),
				ID:      ID,
			},
			name: "debug_getRawReceipts",
		},
inphi's avatar
inphi committed
199 200
	}

inphi's avatar
inphi committed
201
	for _, rpc := range rpcs {
inphi's avatar
inphi committed
202
		t.Run(rpc.name, func(t *testing.T) {
203 204 205
			fakeval := mustMarshalJSON([]string{rpc.name})
			err := cache.PutRPC(ctx, rpc.req, &RPCRes{Result: fakeval})
			require.NoError(t, err)
inphi's avatar
inphi committed
206

inphi's avatar
inphi committed
207
			cachedRes, err := cache.GetRPC(ctx, rpc.req)
208
			require.NoError(t, err)
inphi's avatar
inphi committed
209 210 211
			require.Nil(t, cachedRes)
		})
	}
212

inphi's avatar
inphi committed
213
}