2022-05-05 00:51:24 +03:00
|
|
|
package proxyd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRPCCacheImmutableRPCs(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-05-16 05:20:08 +03:00
|
|
|
cache := newRPCCache(newMemoryCache())
|
2022-05-05 00:51:24 +03:00
|
|
|
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",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_getBlockTransactionCountByHash",
|
|
|
|
Params: mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"}),
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
res: &RPCRes{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Result: `{"eth_getBlockTransactionCountByHash":"!"}`,
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getBlockTransactionCountByHash",
|
2022-05-05 00:51:24 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_getUncleCountByBlockHash",
|
|
|
|
Params: mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"}),
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-09 20:26:01 +03:00
|
|
|
res: &RPCRes{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Result: `{"eth_getUncleCountByBlockHash":"!"}`,
|
2023-05-09 20:26:01 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getUncleCountByBlockHash",
|
2023-05-09 05:59:17 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
req: &RPCReq{
|
2022-05-05 00:51:24 +03:00
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_getBlockByHash",
|
|
|
|
Params: mustMarshalJSON([]string{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "false"}),
|
2023-05-09 05:59:17 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
res: &RPCRes{
|
2023-05-09 05:59:17 +03:00
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Result: `{"eth_getBlockByHash":"!"}`,
|
2023-05-09 05:59:17 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getBlockByHash",
|
2023-05-09 05:59:17 +03:00
|
|
|
},
|
2022-05-05 00:51:24 +03:00
|
|
|
{
|
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_getUncleByBlockHashAndIndex",
|
|
|
|
Params: mustMarshalJSON([]string{"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238", "0x90"}),
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
res: &RPCRes{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Result: `{"eth_getUncleByBlockHashAndIndex":"!"}`,
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getUncleByBlockHashAndIndex",
|
2022-05-05 00:51:24 +03:00
|
|
|
},
|
2023-05-31 20:48:21 +03:00
|
|
|
{
|
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
Method: "debug_getRawReceipts",
|
|
|
|
Params: mustMarshalJSON([]string{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"}),
|
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
res: &RPCRes{
|
|
|
|
JSONRPC: "2.0",
|
2023-10-19 22:58:54 +03:00
|
|
|
Result: []interface{}{"a"},
|
2023-05-31 20:48:21 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
name: "debug_getRawReceipts",
|
|
|
|
},
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rpc := range rpcs {
|
|
|
|
t.Run(rpc.name, func(t *testing.T) {
|
|
|
|
err := cache.PutRPC(ctx, rpc.req, rpc.res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cachedRes, err := cache.GetRPC(ctx, rpc.req)
|
|
|
|
require.NoError(t, err)
|
2023-05-16 05:20:08 +03:00
|
|
|
require.Equal(t, rpc.res, cachedRes)
|
2022-05-05 00:51:24 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 05:20:08 +03:00
|
|
|
func TestRPCCacheUnsupportedMethod(t *testing.T) {
|
2022-05-05 00:51:24 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-05-16 05:20:08 +03:00
|
|
|
cache := newRPCCache(newMemoryCache())
|
2022-05-05 00:51:24 +03:00
|
|
|
ID := []byte(strconv.Itoa(1))
|
|
|
|
|
|
|
|
rpcs := []struct {
|
|
|
|
req *RPCReq
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_syncing",
|
2022-05-05 00:51:24 +03:00
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_syncing",
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_blockNumber",
|
2022-05-05 00:51:24 +03:00
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_blockNumber",
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getBlockByNumber",
|
2022-05-05 00:51:24 +03:00
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_getBlockByNumber",
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_getBlockRange",
|
2022-05-05 00:51:24 +03:00
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
Method: "eth_getBlockRange",
|
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 05:20:08 +03:00
|
|
|
name: "eth_gasPrice",
|
2022-05-05 00:51:24 +03:00
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
2023-05-16 05:20:08 +03:00
|
|
|
Method: "eth_gasPrice",
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
2023-05-16 05:20:08 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "eth_call",
|
|
|
|
req: &RPCReq{
|
2022-05-05 00:51:24 +03:00
|
|
|
JSONRPC: "2.0",
|
2024-02-09 21:11:30 +03:00
|
|
|
Method: "eth_call",
|
2022-05-05 00:51:24 +03:00
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
},
|
2023-05-31 20:48:21 +03:00
|
|
|
{
|
|
|
|
req: &RPCReq{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
Method: "debug_getRawReceipts",
|
|
|
|
Params: mustMarshalJSON([]string{"0x100"}),
|
|
|
|
ID: ID,
|
|
|
|
},
|
|
|
|
name: "debug_getRawReceipts",
|
|
|
|
},
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rpc := range rpcs {
|
|
|
|
t.Run(rpc.name, func(t *testing.T) {
|
2023-05-16 05:20:08 +03:00
|
|
|
fakeval := mustMarshalJSON([]string{rpc.name})
|
|
|
|
err := cache.PutRPC(ctx, rpc.req, &RPCRes{Result: fakeval})
|
|
|
|
require.NoError(t, err)
|
2022-05-05 00:51:24 +03:00
|
|
|
|
|
|
|
cachedRes, err := cache.GetRPC(ctx, rpc.req)
|
2023-05-16 05:20:08 +03:00
|
|
|
require.NoError(t, err)
|
2022-05-05 00:51:24 +03:00
|
|
|
require.Nil(t, cachedRes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|