-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy_test.go
772 lines (661 loc) · 26.1 KB
/
proxy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
package proxy_test
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdktypes "github.com/pokt-network/shannon-sdk/types"
"github.com/stretchr/testify/require"
"github.com/pokt-network/poktroll/pkg/relayer/config"
"github.com/pokt-network/poktroll/pkg/relayer/proxy"
"github.com/pokt-network/poktroll/testutil/testproxy"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)
const (
blockHeight = 1
defaultService = "service1"
secondaryService = "service2"
thirdService = "service3"
defaultRelayMinerServer = "127.0.0.1:8080"
secondaryRelayMinerServer = "127.0.0.1:8081"
)
var (
// helpers used for tests that are initialized in init()
supplierOperatorKeyName string
// supplierEndpoints is the map of serviceName -> []SupplierEndpoint
// where serviceName is the name of the service the supplier staked for
// and SupplierEndpoint is the endpoint of the service advertised on-chain
// by the supplier
supplierEndpoints map[string][]*sharedtypes.SupplierEndpoint
// appPrivateKey is the private key of the application that is used to sign
// relay responses.
// It is also used in these tests to derive the public key and address of the
// application.
appPrivateKey *secp256k1.PrivKey
// servicesConfigMap is a map from the service endpoint to its respective
// respective parsed RelayMiner configuration.
servicesConfigMap map[string]*config.RelayMinerServerConfig
// defaultRelayerServerBehavior is the list of functions that are used to
// define the behavior of the RelayerProxy in the tests.
defaultRelayerProxyBehavior []func(*testproxy.TestBehavior)
)
func init() {
supplierOperatorKeyName = "supplierKeyName"
appPrivateKey = secp256k1.GenPrivKey()
supplierEndpoints = map[string][]*sharedtypes.SupplierEndpoint{
defaultService: {
{
Url: "http://supplier:8545/",
// TODO_EXTEND: Consider adding support for non JSON RPC services in the future
RpcType: sharedtypes.RPCType_JSON_RPC,
},
},
secondaryService: {
{
Url: "http://supplier1:8546/",
RpcType: sharedtypes.RPCType_GRPC,
},
},
thirdService: {
{
Url: "http://supplier2:8547/",
RpcType: sharedtypes.RPCType_GRPC,
},
},
}
servicesConfigMap = map[string]*config.RelayMinerServerConfig{
defaultRelayMinerServer: {
ServerType: config.RelayMinerServerTypeHTTP,
ListenAddress: defaultRelayMinerServer,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
defaultService: {
ServiceId: defaultService,
ServerType: config.RelayMinerServerTypeHTTP,
PubliclyExposedEndpoints: []string{"supplier"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{Scheme: "http", Host: "127.0.0.1:8545", Path: "/"},
},
SigningKeyNames: []string{supplierOperatorKeyName},
},
secondaryService: {
ServiceId: secondaryService,
ServerType: config.RelayMinerServerTypeHTTP,
PubliclyExposedEndpoints: []string{"supplier1"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{Scheme: "http", Host: "127.0.0.1:8546", Path: "/"},
},
SigningKeyNames: []string{supplierOperatorKeyName},
},
},
},
secondaryRelayMinerServer: {
ServerType: config.RelayMinerServerTypeHTTP,
ListenAddress: secondaryRelayMinerServer,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
thirdService: {
ServiceId: thirdService,
ServerType: config.RelayMinerServerTypeHTTP,
PubliclyExposedEndpoints: []string{"supplier2"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{Scheme: "http", Host: "127.0.0.1:8547", Path: "/"},
},
},
},
},
}
defaultRelayerProxyBehavior = []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierOperatorKeyName, blockHeight),
testproxy.WithServicesConfigMap(servicesConfigMap),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierOperatorKeyName, defaultService, appPrivateKey),
}
}
// RelayerProxy should start and stop without errors
func TestRelayerProxy_StartAndStop(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
// Setup the RelayerProxy instrumented behavior
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, defaultRelayerProxyBehavior...)
// Create a RelayerProxy
rp, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(servicesConfigMap),
)
require.NoError(t, err)
// Start RelayerProxy
go rp.Start(ctx)
// Block so relayerProxy has sufficient time to start
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/", servicesConfigMap[defaultRelayMinerServer].ListenAddress))
require.NoError(t, err)
require.NotNil(t, res)
// Test that RelayerProxy is handling requests from the other server
res, err = http.DefaultClient.Get(fmt.Sprintf("http://%s/", servicesConfigMap[secondaryRelayMinerServer].ListenAddress))
require.NoError(t, err)
require.NotNil(t, res)
// Stop RelayerProxy
err = rp.Stop(ctx)
require.NoError(t, err)
}
// RelayerProxy should fail to start if the signing key is not found in the keyring
func TestRelayerProxy_InvalidSupplierOperatorKeyName(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, defaultRelayerProxyBehavior...)
rp, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{"wrongKeyName"}),
proxy.WithServicesConfigMap(servicesConfigMap),
)
require.NoError(t, err)
err = rp.Start(ctx)
require.Error(t, err)
}
// RelayerProxy should fail to build if the signing key name is not provided
func TestRelayerProxy_MissingSupplierOperatorKeyName(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, defaultRelayerProxyBehavior...)
_, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{""}),
proxy.WithServicesConfigMap(servicesConfigMap),
)
require.Error(t, err)
}
// RelayerProxy should fail to build if the service configs are not provided
func TestRelayerProxy_EmptyServicesConfigMap(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, defaultRelayerProxyBehavior...)
_, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(make(map[string]*config.RelayMinerServerConfig)),
)
require.Error(t, err)
}
// RelayerProxy should fail to start if it cannot spawn a server for the
// services it advertized on-chain.
func TestRelayerProxy_UnsupportedRpcType(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
unsupportedSupplierEndpoint := map[string][]*sharedtypes.SupplierEndpoint{
defaultService: {
{
Url: "http://unsupported:8545/jsonrpc",
// TODO_EXTEND: Consider adding support for non JSON RPC services in the future
RpcType: sharedtypes.RPCType_JSON_RPC,
},
},
}
unsupportedRPCTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierOperatorKeyName, blockHeight),
testproxy.WithServicesConfigMap(servicesConfigMap),
// The supplier is staked on-chain but the service it provides is not supported by the proxy
testproxy.WithDefaultSupplier(supplierOperatorKeyName, unsupportedSupplierEndpoint),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierOperatorKeyName, defaultService, appPrivateKey),
}
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedRPCTypeBehavior...)
rp, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(servicesConfigMap),
)
require.NoError(t, err)
err = rp.Start(ctx)
require.Error(t, err)
}
func TestRelayerProxy_UnsupportedTransportType(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
badTransportSupplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
defaultService: {
{
Url: "xttp://supplier:8545/",
RpcType: sharedtypes.RPCType_JSON_RPC,
},
},
}
unsupportedTransportProxy := map[string]*config.RelayMinerServerConfig{
defaultRelayMinerServer: {
// The proxy is configured with an unsupported transport type
ServerType: config.RelayMinerServerType(100),
ListenAddress: defaultRelayMinerServer,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
defaultService: {
ServiceId: defaultService,
// The proxy is configured with an unsupported transport type
ServerType: config.RelayMinerServerType(100),
PubliclyExposedEndpoints: []string{"supplier"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{Scheme: "http", Host: "127.0.0.1:8545", Path: "/"},
},
},
},
},
}
unsupportedTransportTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierOperatorKeyName, blockHeight),
// The proxy is configured with an unsupported transport type for the proxy
testproxy.WithServicesConfigMap(unsupportedTransportProxy),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, badTransportSupplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierOperatorKeyName, defaultService, appPrivateKey),
}
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedTransportTypeBehavior...)
rp, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(unsupportedTransportProxy),
)
require.NoError(t, err)
err = rp.Start(ctx)
require.ErrorIs(t, err, proxy.ErrRelayerProxyUnsupportedTransportType)
}
func TestRelayerProxy_NonConfiguredSupplierServices(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
missingServicesProxy := map[string]*config.RelayMinerServerConfig{
defaultRelayMinerServer: {
ServerType: config.RelayMinerServerTypeHTTP,
ListenAddress: defaultRelayMinerServer,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
defaultService: {
ServiceId: defaultService,
ServerType: config.RelayMinerServerTypeHTTP,
PubliclyExposedEndpoints: []string{"supplier"},
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{Scheme: "http", Host: "127.0.0.1:8545", Path: "/"},
},
},
},
},
}
unsupportedTransportTypeBehavior := []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierOperatorKeyName, blockHeight),
// The proxy is configured with an unsupported transport type for the proxy
testproxy.WithServicesConfigMap(missingServicesProxy),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(supplierOperatorKeyName, defaultService, appPrivateKey),
}
test := testproxy.NewRelayerProxyTestBehavior(ctx, t, unsupportedTransportTypeBehavior...)
rp, err := proxy.NewRelayerProxy(
test.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(missingServicesProxy),
)
require.NoError(t, err)
err = rp.Start(ctx)
require.ErrorIs(t, err, proxy.ErrRelayerProxyServiceEndpointNotHandled)
}
// Test different RelayRequest scenarios
func TestRelayerProxy_Relays(t *testing.T) {
sharedParams := sharedtypes.DefaultParams()
sessionTwoStartHeight := sharedtypes.GetSessionEndHeight(&sharedParams, blockHeight) + 1
// blockOutsideSessionGracePeriod is the block height that is after the first
// session's grace period and within the second session's grace period,
// meaning a relay should be handled as part of the session two AND NOT session one.
blockOutsideSessionGracePeriod := sharedtypes.GetSessionGracePeriodEndHeight(&sharedParams, sessionTwoStartHeight)
// blockWithinSessionGracePeriod is the block height that is after the first
// session but within its session's grace period, meaning a relay should be
// handled at this block height.
blockWithinSessionGracePeriod := sharedtypes.GetSessionGracePeriodEndHeight(&sharedParams, blockHeight) - 1
tests := []struct {
desc string
// RelayerProxy instrumented behavior
relayerProxyBehavior []func(*testproxy.TestBehavior)
// Input scenario builds a RelayRequest, marshals it and sends it to the RelayerProxy
inputScenario func(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errMsg string)
// The request result should return any error form the http.DefaultClient.Do call.
// We infer the behavior from the response's code and message prefix
expectedErrCode int32
expectedErrMsg string
}{
{
desc: "Unparsable relay request",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithUnparsableBody,
// expectedErrCode is because the proxy won't be able to unmarshal the request
// so it does not know how to format the error response
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "proto: RelayRequest: wiretype end group for non-group",
},
{
desc: "Missing signature from relay request",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithMissingSignature,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "missing application signature",
},
{
desc: "Invalid signature associated with relay request",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithInvalidSignature,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "error deserializing ring signature",
},
{
desc: "Missing session header application address associated with relay request",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithMissingSessionHeaderApplicationAddress,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "invalid session header: invalid application address",
},
{
desc: "Non staked application address",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithNonStakedApplicationAddress,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "error getting ring for application address",
},
{
desc: "Ring signature mismatch",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithRingSignatureMismatch,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "ring signature in the relay request does not match the expected one for the app",
},
{
desc: "Session mismatch",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithDifferentSession,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "session ID mismatch",
},
{
desc: "Invalid relay supplier",
relayerProxyBehavior: []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(supplierOperatorKeyName, blockHeight),
testproxy.WithServicesConfigMap(servicesConfigMap),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
// Missing session supplier
testproxy.WithDefaultSessionSupplier("", defaultService, appPrivateKey),
},
inputScenario: sendRequestWithInvalidRelaySupplier,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "error while trying to retrieve a session",
},
{
desc: "Relay request signature does not match the request payload",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithSignatureForDifferentPayload,
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "invalid relay request signature or bytes",
},
{
desc: "Successful relay",
relayerProxyBehavior: defaultRelayerProxyBehavior,
inputScenario: sendRequestWithSuccessfulReply,
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(
supplierOperatorKeyName,
blockWithinSessionGracePeriod,
),
testproxy.WithServicesConfigMap(servicesConfigMap),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, 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(supplierOperatorKeyName, defaultService, appPrivateKey, 2),
},
inputScenario: sendRequestWithCustomSessionHeight(blockHeight),
expectedErrCode: 0,
expectedErrMsg: "", // Relay handled successfully
},
{
desc: "Failed late relay outside session grace period",
relayerProxyBehavior: []func(*testproxy.TestBehavior){
// blockHeight is past the first session's grace period
testproxy.WithRelayerProxyDependenciesForBlockHeight(
supplierOperatorKeyName,
// Set the current block height value returned by the block provider
blockOutsideSessionGracePeriod,
),
testproxy.WithServicesConfigMap(servicesConfigMap),
testproxy.WithDefaultSupplier(supplierOperatorKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
// Add 3 sessions, with the first one that is no longer within its
// session grace period
testproxy.WithSuccessiveSessions(supplierOperatorKeyName, defaultService, appPrivateKey, 3),
},
// Send a request that has a late session past the grace period
inputScenario: sendRequestWithCustomSessionHeight(blockHeight),
expectedErrCode: testproxy.JSONRPCInternalErrorCode,
expectedErrMsg: "session expired", // Relay rejected by the supplier
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
testBehavior := testproxy.NewRelayerProxyTestBehavior(ctx, t, test.relayerProxyBehavior...)
rp, err := proxy.NewRelayerProxy(
testBehavior.Deps,
proxy.WithSigningKeyNames([]string{supplierOperatorKeyName}),
proxy.WithServicesConfigMap(servicesConfigMap),
)
require.NoError(t, err)
go rp.Start(ctx)
// Block so relayerProxy has sufficient time to start
time.Sleep(100 * time.Millisecond)
errCode, errMsg := test.inputScenario(t, testBehavior)
require.Equal(t, test.expectedErrCode, errCode)
require.True(t, strings.HasPrefix(errMsg, test.expectedErrMsg))
cancel()
})
}
}
func sendRequestWithUnparsableBody(
t *testing.T,
test *testproxy.TestBehavior,
) (errorCode int32, errorMessage string) {
// Send non JSONRpc payload when the post request specifies json
reader := io.NopCloser(bytes.NewReader([]byte("invalid request")))
res, err := http.DefaultClient.Post(
fmt.Sprintf("http://%s", servicesConfigMap[defaultRelayMinerServer].ListenAddress),
"application/json",
reader,
)
require.NoError(t, err)
require.NotNil(t, res)
return testproxy.GetRelayResponseError(t, res)
}
func sendRequestWithMissingSignature(
t *testing.T,
test *testproxy.TestBehavior,
) (errorCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = nil
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithInvalidSignature(
t *testing.T,
test *testproxy.TestBehavior,
) (errorCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = []byte("invalid signature")
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithMissingSessionHeaderApplicationAddress(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
randomPrivKey := secp256k1.GenPrivKey()
req := testproxy.GenerateRelayRequest(
test,
randomPrivKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
// The application address is missing from the session header
req.Meta.SessionHeader.ApplicationAddress = ""
// Assign a valid but random ring signature so that the request is not rejected
// before looking at the application address
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithNonStakedApplicationAddress(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
randomPrivKey := secp256k1.GenPrivKey()
req := testproxy.GenerateRelayRequest(
test,
randomPrivKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
// Have a valid signature from the non staked key
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithRingSignatureMismatch(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
// The signature is valid but does not match the ring for the application address
randomPrivKey := secp256k1.GenPrivKey()
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, randomPrivKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithDifferentSession(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
// Use a block height that generates a different session ID
sharedParams := sharedtypes.DefaultParams()
blockHeightAfterSessionGracePeriod := int64(blockHeight + sharedParams.NumBlocksPerSession + 1)
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeightAfterSessionGracePeriod,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithInvalidRelaySupplier(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithSignatureForDifferentPayload(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test, appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)
bodyBz := []byte(`{"method":"someMethod","id":1,"jsonrpc":"2.0","params":["alteredParam"]}`)
request := &http.Request{
Method: http.MethodPost,
URL: &url.URL{},
Header: http.Header{},
Body: io.NopCloser(bytes.NewReader(bodyBz)),
}
request.Header.Set("Content-Type", "application/json")
_, requestBz, err := sdktypes.SerializeHTTPRequest(request)
require.NoError(t, err)
// Alter the request payload so the hash doesn't match the one used by the signature
req.Payload = requestBz
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
func sendRequestWithSuccessfulReply(
t *testing.T,
test *testproxy.TestBehavior,
) (errCode int32, errorMessage string) {
req := testproxy.GenerateRelayRequest(
test,
appPrivateKey,
defaultService,
blockHeight,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, 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(
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,
supplierOperatorKeyName,
testproxy.PrepareJSONRPCRequest(t),
)
req.Meta.Signature = testproxy.GetApplicationRingSignature(t, req, appPrivateKey)
return testproxy.MarshalAndSend(test, servicesConfigMap, defaultRelayMinerServer, defaultService, req)
}
}