diff --git a/.env b/.env index 38e1691..1324376 100644 --- a/.env +++ b/.env @@ -40,7 +40,7 @@ TEST_PROXY_BACKEND_EVM_RPC_HOST_URL=http://localhost:8545 TEST_DATABASE_ENDPOINT_URL=localhost:5432 TEST_PROXY_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-validator:8545,localhost:7778>http://kava-pruning:8545 TEST_PROXY_HEIGHT_BASED_ROUTING_ENABLED=true -TEST_PROXY_PRUNING_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-validator:8545,localhost:7778>http://kava-pruning:8545 +TEST_PROXY_PRUNING_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-pruning:8545,localhost:7778>http://kava-pruning:8545 # What level of logging to use for service objects constructed during # unit tests TEST_SERVICE_LOG_LEVEL=ERROR @@ -62,8 +62,8 @@ PROXY_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-validator:8545,localhost:7 # height-based routing will look at the height of an incoming EVM request # iff. the height is "latest", it routes to the corresponding PROXY_PRUNING_BACKEND_HOST_URL_MAP value # otherwise, it falls back to the value in PROXY_BACKEND_HOST_URL_MAP -PROXY_HEIGHT_BASED_ROUTING_ENABLED=false -PROXY_PRUNING_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-validator:8545,localhost:7778>http://kava-pruning:8545 +PROXY_HEIGHT_BASED_ROUTING_ENABLED=true +PROXY_PRUNING_BACKEND_HOST_URL_MAP=localhost:7777>http://kava-pruning:8545,localhost:7778>http://kava-pruning:8545 # Configuration for the servcie to connect to it's database DATABASE_NAME=postgres DATABASE_ENDPOINT_URL=postgres:5432 diff --git a/main_test.go b/main_test.go index b4d3e26..560db62 100644 --- a/main_test.go +++ b/main_test.go @@ -2,18 +2,24 @@ package main_test import ( "context" + "fmt" "os" + "strconv" "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/rpc" + "github.com/kava-labs/kava-proxy-service/clients/database" "github.com/kava-labs/kava-proxy-service/decode" "github.com/kava-labs/kava-proxy-service/logging" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/kava-labs/kava-proxy-service/service" ) const ( @@ -36,6 +42,8 @@ var ( proxyServiceHostname = os.Getenv("TEST_PROXY_SERVICE_EVM_RPC_HOSTNAME") proxyServicePruningURL = os.Getenv("TEST_PROXY_SERVICE_EVM_RPC_PRUNING_URL") + proxyServiceHeightBasedRouting, _ = strconv.ParseBool(os.Getenv("TEST_PROXY_HEIGHT_BASED_ROUTING_ENABLED")) + databaseURL = os.Getenv("TEST_DATABASE_ENDPOINT_URL") databasePassword = os.Getenv("DATABASE_PASSWORD") databaseUsername = os.Getenv("DATABASE_USERNAME") @@ -344,3 +352,86 @@ func TestE2ETestProxyTracksBlockNumberForMethodsWithBlockHashParam(t *testing.T) assert.Equal(t, *requestMetricDuringRequestWindow.BlockNumber, requestBlockNumber) } } + +func TestE2ETest_HeightBasedRouting(t *testing.T) { + if !proxyServiceHeightBasedRouting { + t.Skip("TEST_PROXY_HEIGHT_BASED_ROUTING_ENABLED is false. skipping height-based routing e2e test") + } + + rpc, err := rpc.Dial(proxyServiceURL) + require.NoError(t, err) + + databaseClient, err := database.NewPostgresClient(databaseConfig) + require.NoError(t, err) + + testCases := []struct { + name string + method string + params []interface{} + expectRoute string + }{ + { + name: "request for non-latest height -> default", + method: "eth_getBlockByNumber", + params: []interface{}{"0x2", false}, + expectRoute: service.ResponseBackendDefault, + }, + { + name: "request for earliest height -> default", + method: "eth_getBlockByNumber", + params: []interface{}{"earliest", false}, + expectRoute: service.ResponseBackendDefault, + }, + { + name: "request for latest height -> pruning", + method: "eth_getBlockByNumber", + params: []interface{}{"latest", false}, + expectRoute: service.ResponseBackendPruning, + }, + { + name: "request for finalized height -> pruning", + method: "eth_getBlockByNumber", + params: []interface{}{"finalized", false}, + expectRoute: service.ResponseBackendPruning, + }, + { + name: "request with empty height -> pruning", + method: "eth_getBlockByNumber", + params: []interface{}{nil, false}, + expectRoute: service.ResponseBackendPruning, + }, + { + name: "request not requiring height -> pruning", + method: "eth_chainId", + params: []interface{}{}, + expectRoute: service.ResponseBackendPruning, + }, + { + name: "request by hash -> default", + method: "eth_getBlockByHash", + params: []interface{}{"0xe9bd10bc1d62b4406dd1fb3dbf3adb54f640bdb9ebbe3dd6dfc6bcc059275e54", false}, + expectRoute: service.ResponseBackendDefault, + }, + { + name: "un-parseable (invalid) height -> default", + method: "eth_getBlockByNumber", + params: []interface{}{"not-a-block-tag", false}, + expectRoute: service.ResponseBackendDefault, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + startTime := time.Now() + err := rpc.Call(nil, tc.method, tc.params...) + require.NoError(t, err) + + metrics := findMetricsInWindowForMethods(databaseClient, startTime, time.Now(), []string{tc.method}) + + require.Len(t, metrics, 1) + fmt.Printf("%+v\n", metrics[0]) + require.Equal(t, metrics[0].MethodName, tc.method) + require.Equal(t, metrics[0].ResponseBackend, tc.expectRoute) + }) + } +}