Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Session, Test] Session rollover test #332

Merged
merged 21 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 112 additions & 37 deletions pkg/relayer/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import (
"github.com/pokt-network/poktroll/pkg/relayer/proxy"
"github.com/pokt-network/poktroll/testutil/testproxy"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

const blockHeight = 1
const (
blockHeight = 1
defaultService = "service1"
defaultServer = "server1"
)

var (
// helpers used for tests that are initialized in init()
Expand Down Expand Up @@ -75,12 +80,12 @@ func init() {

proxiedServices = map[string]*config.RelayMinerProxyConfig{
"server1": {
ProxyName: "server1",
ProxyName: defaultServer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an ultra NIT, but something I'm asking for just because I also always get confused between proxy/server/etc...

Do the key and proxyName need to be the same or can they be different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it's confusing.

The key and ProxyName have to be the same, as the config parser maps the proxy servers using their ProxyName as keys.

I was uncomfortable having a variable as a key but afterthought, I believe it will add more clarity. Let's having as

defaultProxyServer: {
  ProxyName: defaultProxyServer,
  ...
}

Type: config.ProxyTypeHTTP,
Host: "127.0.0.1:8080",
Suppliers: map[string]*config.RelayMinerSupplierConfig{
"service1": {
ServiceId: "service1",
ServiceId: defaultService,
Type: config.ProxyTypeHTTP,
Hosts: []string{"supplier:8545"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
Expand Down Expand Up @@ -115,11 +120,11 @@ func init() {
}

defaultRelayerProxyBehavior = []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependencies(supplierKeyName),
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierKeyName, blockHeight),
testproxy.WithRelayerProxiedServices(proxiedServices),
testproxy.WithDefaultSupplier(supplierKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, "service1", appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, defaultService, appPrivateKey),
}
}

Expand All @@ -143,7 +148,7 @@ func TestRelayerProxy_StartAndStop(t *testing.T) {
time.Sleep(100 * time.Millisecond)

// Test that RelayerProxy is handling requests (ignoring the actual response content)
res, err := http.DefaultClient.Get(fmt.Sprintf("http://%s/", proxiedServices["server1"].Host))
res, err := http.DefaultClient.Get(fmt.Sprintf("http://%s/", proxiedServices[defaultServer].Host))
require.NoError(t, err)
require.NotNil(t, res)

Expand Down Expand Up @@ -216,13 +221,13 @@ func TestRelayerProxy_UnsupportedRpcType(t *testing.T) {
}

unsupportedRPCTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependencies(supplierKeyName),
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierKeyName, blockHeight),
testproxy.WithRelayerProxiedServices(proxiedServices),

// The supplier is staked on-chain but the service it provides is not supported by the proxy
testproxy.WithDefaultSupplier(supplierKeyName, unsupportedSupplierEndpoint),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, "service1", appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, defaultService, appPrivateKey),
}

test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedRPCTypeBehavior...)
Expand Down Expand Up @@ -252,13 +257,13 @@ func TestRelayerProxy_UnsupportedTransportType(t *testing.T) {

unsupportedTransportProxy := map[string]*config.RelayMinerProxyConfig{
"server1": {
ProxyName: "server1",
ProxyName: defaultServer,
// The proxy is configured with an unsupported transport type
Type: config.ProxyType(100),
Host: "127.0.0.1:8080",
Suppliers: map[string]*config.RelayMinerSupplierConfig{
"service1": {
ServiceId: "service1",
ServiceId: defaultService,
// The proxy is configured with an unsupported transport type
Type: config.ProxyType(100),
Hosts: []string{"supplier:8545"},
Expand All @@ -271,13 +276,13 @@ func TestRelayerProxy_UnsupportedTransportType(t *testing.T) {
}

unsupportedTransportTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependencies(supplierKeyName),
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierKeyName, blockHeight),

// The proxy is configured with an unsupported transport type for the proxy
testproxy.WithRelayerProxiedServices(unsupportedTransportProxy),
testproxy.WithDefaultSupplier(supplierKeyName, badTransportSupplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, "service1", appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, defaultService, appPrivateKey),
}

test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedTransportTypeBehavior...)
Expand All @@ -298,12 +303,12 @@ func TestRelayerProxy_NonConfiguredSupplierServices(t *testing.T) {

missingServicesProxy := map[string]*config.RelayMinerProxyConfig{
"server1": {
ProxyName: "server1",
ProxyName: defaultServer,
Type: config.ProxyTypeHTTP,
Host: "127.0.0.1:8080",
Suppliers: map[string]*config.RelayMinerSupplierConfig{
"service1": {
ServiceId: "service1",
ServiceId: defaultService,
Type: config.ProxyTypeHTTP,
Hosts: []string{"supplier:8545"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
Expand All @@ -315,13 +320,13 @@ func TestRelayerProxy_NonConfiguredSupplierServices(t *testing.T) {
}

unsupportedTransportTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependencies(supplierKeyName),
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierKeyName, blockHeight),

// The proxy is configured with an unsupported transport type for the proxy
testproxy.WithRelayerProxiedServices(missingServicesProxy),
testproxy.WithDefaultSupplier(supplierKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, "service1", appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierKeyName, defaultService, appPrivateKey),
}

test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedTransportTypeBehavior...)
Expand All @@ -339,6 +344,12 @@ func TestRelayerProxy_NonConfiguredSupplierServices(t *testing.T) {

// Test different RelayRequest scenarios
func TestRelayerProxy_Relays(t *testing.T) {
// blockWithinTheThirdSession is the block height that is past the first session's
// grace period and within the second session's grace period
var blockWithinTheThirdSession int64 = blockHeight +
red-0ne marked this conversation as resolved.
Show resolved Hide resolved
sessionkeeper.NumBlocksPerSession +
sessionkeeper.SessionGracePeriod

tests := []struct {
desc string
// RelayerProxy instrumented behavior
Expand Down Expand Up @@ -432,12 +443,12 @@ func TestRelayerProxy_Relays(t *testing.T) {
desc: "Invalid relay supplier",

relayerProxyBehavior: []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependencies(supplierKeyName),
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierKeyName, blockHeight),
testproxy.WithRelayerProxiedServices(proxiedServices),
testproxy.WithDefaultSupplier(supplierKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
// Missing session supplier
testproxy.WithDefaultSessionSupplier("", "service1", appPrivateKey),
testproxy.WithDefaultSessionSupplier("", defaultService, appPrivateKey),
},
inputScenario: sendRequestWithInvalidRelaySupplier,

Expand All @@ -462,6 +473,50 @@ func TestRelayerProxy_Relays(t *testing.T) {
expectedErrCode: 0,
expectedErrMsg: "",
},
{
desc: "Successful late relay with session grace period",

relayerProxyBehavior: []func(*testproxy.TestBehavior){
// blockHeight is past the first session but within its session grace period
testproxy.WithRelayerProxyDependenciesForBlockHeight(
supplierKeyName,
blockHeight+sessionkeeper.SessionGracePeriod,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be solved by the base PR, but it's unclear if SessionGracePeriod is NumBlocks, NumSessions, etc... After renaming it in the other PR, this should be solved though.

),
testproxy.WithRelayerProxiedServices(proxiedServices),
testproxy.WithDefaultSupplier(supplierKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
// Add 2 sessions, with the first one being within the withing grace period
// and the second one being the current session
testproxy.WithSuccessiveSessions(supplierKeyName, defaultService, appPrivateKey, 2),
},
inputScenario: sendRequestWithCustomSessionHeight(blockHeight),

expectedErrCode: 0,
expectedErrMsg: "",
red-0ne marked this conversation as resolved.
Show resolved Hide resolved
},
{
desc: "Failed late relay outside session grace period",

relayerProxyBehavior: []func(*testproxy.TestBehavior){
// blockHeight is past the first session's grace period
testproxy.WithRelayerProxyDependenciesForBlockHeight(
supplierKeyName,
// Set the current block height value returned by the block provider
blockWithinTheThirdSession,
),
testproxy.WithRelayerProxiedServices(proxiedServices),
testproxy.WithDefaultSupplier(supplierKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
// Add 3 sessions, with the first one that is no longer within its
// session grace period
testproxy.WithSuccessiveSessions(supplierKeyName, defaultService, appPrivateKey, 3),
},
// Send a request that has a late session past the grace period
inputScenario: sendRequestWithCustomSessionHeight(blockHeight),

expectedErrCode: -32000,
expectedErrMsg: "session expired",
red-0ne marked this conversation as resolved.
Show resolved Hide resolved
},
}

ctx := context.TODO()
Expand Down Expand Up @@ -498,7 +553,7 @@ func sendRequestWithUnparsableBody(
reader := io.NopCloser(bytes.NewReader([]byte("invalid request")))

res, err := http.DefaultClient.Post(
fmt.Sprintf("http://%s", proxiedServices["server1"].Host),
fmt.Sprintf("http://%s", proxiedServices[defaultServer].Host),
"application/json",
reader,
)
Expand All @@ -518,7 +573,7 @@ func sendRequestWithMissingMeta(
Payload: testproxy.PrepareJsonRPCRequestPayload(),
}

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithMissingSignature(
Expand All @@ -528,12 +583,12 @@ func sendRequestWithMissingSignature(
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
req.Meta.Signature = nil
return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithInvalidSignature(
Expand All @@ -543,13 +598,13 @@ func sendRequestWithInvalidSignature(
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
req.Meta.Signature = []byte("invalid signature")

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithMissingSessionHeaderApplicationAddress(
Expand All @@ -560,7 +615,7 @@ func sendRequestWithMissingSessionHeaderApplicationAddress(
req := testproxy.GenerateRelayRequest(
test,
randomPrivKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
Expand All @@ -572,7 +627,7 @@ func sendRequestWithMissingSessionHeaderApplicationAddress(
// before looking at the application address
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithNonStakedApplicationAddress(
Expand All @@ -583,15 +638,15 @@ func sendRequestWithNonStakedApplicationAddress(
req := testproxy.GenerateRelayRequest(
test,
randomPrivKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)

// Have a valid signature from the non staked key
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithRingSignatureMismatch(
Expand All @@ -601,7 +656,7 @@ func sendRequestWithRingSignatureMismatch(
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
Expand All @@ -610,7 +665,7 @@ func sendRequestWithRingSignatureMismatch(
randomPrivKey := secp256k1.GenPrivKey()
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithDifferentSession(
Expand All @@ -627,7 +682,7 @@ func sendRequestWithDifferentSession(
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithInvalidRelaySupplier(
Expand All @@ -637,13 +692,13 @@ func sendRequestWithInvalidRelaySupplier(
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithSignatureForDifferentPayload(
Expand All @@ -652,7 +707,7 @@ func sendRequestWithSignatureForDifferentPayload(
) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test, appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
Expand All @@ -661,7 +716,7 @@ func sendRequestWithSignatureForDifferentPayload(
// Alter the request payload so the hash doesn't match the one used by the signature
req.Payload = []byte(`{"method":"someMethod","id":1,"jsonrpc":"2.0","params":["alteredParam"]}`)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

func sendRequestWithSuccessfulReply(
Expand All @@ -671,11 +726,31 @@ func sendRequestWithSuccessfulReply(
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
"service1",
defaultService,
blockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)

return testproxy.MarshalAndSend(test, proxiedServices, "server1", "service1", req)
return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}

// sendRequestWithCustomSessionHeight is a helper function that generates a `RelayRequest`
// with a `Session` that contains the given `requestSessionBlockHeight` and sends it to the
// `RelayerProxy`.
func sendRequestWithCustomSessionHeight(
red-0ne marked this conversation as resolved.
Show resolved Hide resolved
requestSessionBlockHeight int64,
) func(t *testing.T, test *testproxy.TestBehavior) (errCode int32, errorMessage string) {
return func(t *testing.T, test *testproxy.TestBehavior) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
requestSessionBlockHeight,
testproxy.PrepareJsonRPCRequestPayload(),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)

return testproxy.MarshalAndSend(test, proxiedServices, defaultServer, defaultService, req)
}
}
Loading