Skip to content

Commit

Permalink
add e2e tests for HeightBasedRouting
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed Oct 13, 2023
1 parent b5a799f commit 36799d7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
95 changes: 93 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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")
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 36799d7

Please sign in to comment.