-
Notifications
You must be signed in to change notification settings - Fork 0
/
invocation.go
1207 lines (972 loc) · 38.2 KB
/
invocation.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package keeper
import (
"encoding/hex"
"encoding/json"
"fmt"
gogotypes "github.com/gogo/protobuf/types"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furynet/furymod/modules/service/types"
)
// RegisterResponseCallback registers a module callback for response handling
func (k Keeper) RegisterResponseCallback(moduleName string, respCallback types.ResponseCallback) error {
if _, ok := k.respCallbacks[moduleName]; ok {
return sdkerrors.Wrapf(types.ErrCallbackRegistered, "%s already registered for module %s", "response callback", moduleName)
}
k.respCallbacks[moduleName] = respCallback
return nil
}
// RegisterStateCallback registers a module callback for state handling
func (k Keeper) RegisterStateCallback(moduleName string, stateCallback types.StateCallback) error {
if _, ok := k.stateCallbacks[moduleName]; ok {
return sdkerrors.Wrapf(types.ErrCallbackRegistered, "%s already registered for module %s", "state callback", moduleName)
}
k.stateCallbacks[moduleName] = stateCallback
return nil
}
// CreateRequestContext creates a request context with the specified params
func (k Keeper) CreateRequestContext(
ctx sdk.Context,
serviceName string,
providers []sdk.AccAddress,
consumer sdk.AccAddress,
input string,
serviceFeeCap sdk.Coins,
timeout int64,
repeated bool,
repeatedFrequency uint64,
repeatedTotal int64,
state types.RequestContextState,
responseThreshold uint32,
moduleName string,
) (
tmbytes.HexBytes, error,
) {
if len(moduleName) != 0 {
if _, err := k.GetResponseCallback(moduleName); err != nil {
return nil, err
}
if _, err := k.GetStateCallback(moduleName); err != nil {
return nil, err
}
if err := types.ValidateRequest(
serviceName, serviceFeeCap, providers, input,
timeout, repeated, repeatedFrequency, repeatedTotal,
); err != nil {
return nil, err
}
if responseThreshold < 1 || int(responseThreshold) > len(providers) {
return nil, sdkerrors.Wrapf(types.ErrInvalidResponseThreshold, "response threshold [%d] must be between [1,%d]", responseThreshold, len(providers))
}
}
_, found := k.GetServiceDefinition(ctx, serviceName)
if !found {
return nil, sdkerrors.Wrap(types.ErrUnknownServiceDefinition, serviceName)
}
if err := types.ValidateRequestInput(input); err != nil {
return nil, err
}
if err := k.validateServiceFeeCap(ctx, serviceFeeCap); err != nil {
return nil, err
}
maxRequestTimeout := k.MaxRequestTimeout(ctx)
if timeout > maxRequestTimeout {
return nil, sdkerrors.Wrapf(types.ErrInvalidTimeout, "timeout [%d] must not be greater than the max request timeout [%d]", timeout, maxRequestTimeout)
}
if repeated {
if repeatedFrequency == 0 {
repeatedFrequency = uint64(timeout)
}
} else {
repeatedFrequency = 0
repeatedTotal = 0
}
batchCounter := uint64(0)
batchRequestCount := uint32(0)
batchResponseCount := uint32(0)
batchResponseThreshold := responseThreshold
batchState := types.BATCHCOMPLETED
requestContext := types.NewRequestContext(
serviceName, providers, consumer, input, serviceFeeCap, timeout,
repeated, repeatedFrequency, repeatedTotal, batchCounter,
batchRequestCount, batchResponseCount, batchResponseThreshold,
batchState, state, responseThreshold, moduleName,
)
requestContextID := types.GenerateRequestContextID(TxHash(ctx), k.GetInternalIndex(ctx))
k.SetRequestContext(ctx, requestContextID, requestContext)
if requestContext.State == types.RUNNING {
k.AddNewRequestBatch(ctx, requestContextID, ctx.BlockHeight())
}
return requestContextID, nil
}
// UpdateRequestContext updates the specified request context
func (k Keeper) UpdateRequestContext(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
providers []sdk.AccAddress,
respThreshold uint32,
serviceFeeCap sdk.Coins,
timeout int64,
repeatedFreq uint64,
repeatedTotal int64,
consumer sdk.AccAddress,
) error {
pds := make([]string, len(providers))
for i, provider := range providers {
pds[i] = provider.String()
}
requestContext, found := k.GetRequestContext(ctx, requestContextID)
if !found {
return sdkerrors.Wrap(types.ErrUnknownRequestContext, requestContextID.String())
}
// check authority when called by module
if len(requestContext.ModuleName) > 0 {
if err := k.CheckAuthority(ctx, consumer, requestContextID, false); err != nil {
return err
}
}
if requestContext.State == types.COMPLETED {
return types.ErrRequestContextCompleted
}
if len(requestContext.ModuleName) > 0 {
if err := types.ValidateRequestContextUpdating(providers, serviceFeeCap, timeout, repeatedFreq, repeatedTotal); err != nil {
return err
}
if respThreshold == 0 {
respThreshold = requestContext.ResponseThreshold
}
if len(pds) == 0 {
pds = requestContext.Providers
}
if respThreshold > uint32(len(pds)) {
return sdkerrors.Wrapf(types.ErrInvalidResponseThreshold, "response threshold [%d] must be between [1,%d]", respThreshold, len(pds))
}
if respThreshold > 0 {
requestContext.ResponseThreshold = respThreshold
}
}
if !serviceFeeCap.Empty() {
if err := k.validateServiceFeeCap(ctx, serviceFeeCap); err != nil {
return err
}
requestContext.ServiceFeeCap = serviceFeeCap
}
maxRequestTimeout := k.MaxRequestTimeout(ctx)
if timeout > maxRequestTimeout {
return sdkerrors.Wrapf(types.ErrInvalidTimeout, "timeout [%d] must not be greater than the max request timeout [%d]", timeout, maxRequestTimeout)
}
if timeout == 0 {
timeout = requestContext.Timeout
}
if repeatedFreq == 0 {
repeatedFreq = requestContext.RepeatedFrequency
}
if repeatedFreq < uint64(timeout) {
return sdkerrors.Wrapf(types.ErrInvalidRepeatedFreq, "repeated frequency [%d] must not be less than the timeout [%d]", repeatedFreq, requestContext.Timeout)
}
if repeatedTotal >= 1 && repeatedTotal < int64(requestContext.BatchCounter) {
return sdkerrors.Wrapf(types.ErrInvalidRepeatedTotal, "updated repeated total [%d] must not be less than the current batch counter [%d]", repeatedTotal, requestContext.BatchCounter)
}
if len(pds) > 0 {
requestContext.Providers = pds
}
if timeout > 0 {
requestContext.Timeout = timeout
}
if repeatedFreq > 0 {
requestContext.RepeatedFrequency = repeatedFreq
}
if repeatedTotal != 0 {
requestContext.RepeatedTotal = repeatedTotal
}
k.SetRequestContext(ctx, requestContextID, requestContext)
return nil
}
// PauseRequestContext suspends the specified request context
func (k Keeper) PauseRequestContext(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
consumer sdk.AccAddress,
) error {
requestContext, found := k.GetRequestContext(ctx, requestContextID)
if !found {
return sdkerrors.Wrap(types.ErrUnknownRequestContext, requestContextID.String())
}
// check authority when called by module
if len(requestContext.ModuleName) > 0 {
if err := k.CheckAuthority(ctx, consumer, requestContextID, false); err != nil {
return err
}
}
if !requestContext.Repeated {
return types.ErrRequestContextNonRepeated
}
if requestContext.State != types.RUNNING {
return types.ErrRequestContextNotRunning
}
requestContext.State = types.PAUSED
k.SetRequestContext(ctx, requestContextID, requestContext)
return nil
}
// StartRequestContext starts the specified request context
func (k Keeper) StartRequestContext(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
consumer sdk.AccAddress,
) error {
requestContext, found := k.GetRequestContext(ctx, requestContextID)
if !found {
return sdkerrors.Wrap(types.ErrUnknownRequestContext, requestContextID.String())
}
// check authority when called by module
if len(requestContext.ModuleName) > 0 {
if err := k.CheckAuthority(ctx, consumer, requestContextID, false); err != nil {
return err
}
}
if requestContext.State != types.PAUSED {
return types.ErrRequestContextNotPaused
}
requestContext.State = types.RUNNING
k.SetRequestContext(ctx, requestContextID, requestContext)
// add to the new request batch queue if existing in neither expired nor new request batch queue
if !k.HasRequestBatchExpiration(ctx, requestContextID) && !k.HasNewRequestBatch(ctx, requestContextID) {
k.AddNewRequestBatch(ctx, requestContextID, ctx.BlockHeight())
}
return nil
}
// KillRequestContext terminates the specified request context
func (k Keeper) KillRequestContext(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
consumer sdk.AccAddress,
) error {
requestContext, found := k.GetRequestContext(ctx, requestContextID)
if !found {
return sdkerrors.Wrap(types.ErrUnknownRequestContext, requestContextID.String())
}
// check authority when called by module
if len(requestContext.ModuleName) > 0 {
if err := k.CheckAuthority(ctx, consumer, requestContextID, false); err != nil {
return err
}
}
if !requestContext.Repeated {
return types.ErrRequestContextNonRepeated
}
requestContext.State = types.COMPLETED
k.SetRequestContext(ctx, requestContextID, requestContext)
return nil
}
// SetRequestContext sets the specified request context
func (k Keeper) SetRequestContext(ctx sdk.Context, requestContextID tmbytes.HexBytes, requestContext types.RequestContext) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&requestContext)
store.Set(types.GetRequestContextKey(requestContextID), bz)
}
// DeleteRequestContext deletes the specified request context
func (k Keeper) DeleteRequestContext(ctx sdk.Context, requestContextID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetRequestContextKey(requestContextID))
}
// GetRequestContext retrieves the specified request context
func (k Keeper) GetRequestContext(ctx sdk.Context, requestContextID tmbytes.HexBytes) (requestContext types.RequestContext, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetRequestContextKey(requestContextID))
if bz == nil {
return requestContext, false
}
k.cdc.MustUnmarshal(bz, &requestContext)
return requestContext, true
}
// IterateRequestContexts iterates through all request contexts
func (k Keeper) IterateRequestContexts(
ctx sdk.Context,
op func(requestContextID tmbytes.HexBytes, requestContext types.RequestContext) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.RequestContextKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
requestContextID := iterator.Key()[1:]
var requestContext types.RequestContext
k.cdc.MustUnmarshal(iterator.Value(), &requestContext)
if stop := op(requestContextID, requestContext); stop {
break
}
}
}
// InitiateRequests creates requests for the given providers from the specified request context
// Note: make sure that request context is valid and running, and providers are valid
func (k Keeper) InitiateRequests(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
providers []sdk.AccAddress,
providerRequests map[string][]string,
) []tmbytes.HexBytes {
requestContext, _ := k.GetRequestContext(ctx, requestContextID)
requestContext.BatchCounter++
var requests []types.CompactRequest
var requestIDs []tmbytes.HexBytes
consumer, _ := sdk.AccAddressFromBech32(requestContext.Consumer)
for providerIndex, provider := range providers {
request := k.buildRequest(
ctx, requestContextID, requestContext.BatchCounter,
requestContext.ServiceName, provider,
consumer, requestContext.Timeout,
)
requestID := types.GenerateRequestID(requestContextID, requestContext.BatchCounter, ctx.BlockHeight(), int16(providerIndex))
k.SetCompactRequest(ctx, requestID, request)
k.AddActiveRequest(ctx, requestContext.ServiceName, provider, ctx.BlockHeight()+requestContext.Timeout, requestID)
requests = append(requests, request)
// tags for provider by one service
providerRequests[types.ActionTag(requestContext.ServiceName, provider.String())] = append(
providerRequests[types.ActionTag(requestContext.ServiceName, provider.String())],
requestID.String(),
)
requestIDs = append(requestIDs, requestID)
}
requestContext.BatchState = types.BATCHRUNNING
requestContext.BatchResponseCount = 0
requestContext.BatchRequestCount = uint32(len(providers))
requestContext.BatchResponseThreshold = requestContext.ResponseThreshold
k.SetRequestContext(ctx, requestContextID, requestContext)
if len(requests) > 0 {
requestsJSON, _ := json.Marshal(requests)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeNewBatchRequest,
sdk.NewAttribute(types.AttributeKeyServiceName, requestContext.ServiceName),
sdk.NewAttribute(types.AttributeKeyRequestContextID, requestContextID.String()),
sdk.NewAttribute(types.AttributeKeyRequests, string(requestsJSON)),
),
})
}
return requestIDs
}
// SkipCurrentRequestBatch skips the current request batch
func (k Keeper) SkipCurrentRequestBatch(ctx sdk.Context, requestContextID tmbytes.HexBytes, requestContext types.RequestContext) {
requestContext.BatchCounter++
requestContext.BatchState = types.BATCHRUNNING
requestContext.BatchRequestCount = 0
requestContext.BatchResponseCount = 0
requestContext.BatchResponseThreshold = requestContext.ResponseThreshold
k.SetRequestContext(ctx, requestContextID, requestContext)
k.AddRequestBatchExpiration(ctx, requestContextID, ctx.BlockHeight()+requestContext.Timeout)
}
// buildRequest builds a request to the given provider from the specified request context
// Note: make sure that the binding exists
func (k Keeper) buildRequest(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
batchCounter uint64,
serviceName string,
provider sdk.AccAddress,
consumer sdk.AccAddress,
timeout int64,
) types.CompactRequest {
binding, _ := k.GetServiceBinding(ctx, serviceName, provider)
serviceFee := k.GetPrice(ctx, consumer, binding)
return types.NewCompactRequest(
requestContextID,
batchCounter,
provider,
serviceFee,
ctx.BlockHeight(),
ctx.BlockHeight()+timeout,
)
}
// SetCompactRequest sets the specified compact request
func (k Keeper) SetCompactRequest(ctx sdk.Context, requestID tmbytes.HexBytes, request types.CompactRequest) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&request)
store.Set(types.GetRequestKey(requestID), bz)
}
// GetCompactRequest retrieves the specified compact request
func (k Keeper) GetCompactRequest(ctx sdk.Context, requestID tmbytes.HexBytes) (request types.CompactRequest, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetRequestKey(requestID))
if bz == nil {
return request, false
}
k.cdc.MustUnmarshal(bz, &request)
return request, true
}
// DeleteCompactRequest deletes the specified compact request
func (k Keeper) DeleteCompactRequest(ctx sdk.Context, requestID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetRequestKey(requestID))
}
// GetRequest returns the specified request
func (k Keeper) GetRequest(ctx sdk.Context, requestID tmbytes.HexBytes) (request types.Request, found bool) {
compactRequest, found := k.GetCompactRequest(ctx, requestID)
if !found {
return request, false
}
provider, err := sdk.AccAddressFromBech32(compactRequest.Provider)
if err != nil {
return request, false
}
requestContextId, err := hex.DecodeString(compactRequest.RequestContextId)
if err != nil {
return request, false
}
requestContext, found := k.GetRequestContext(ctx, requestContextId)
if !found {
return request, false
}
consumer, err := sdk.AccAddressFromBech32(requestContext.Consumer)
if err != nil {
return request, false
}
return types.NewRequest(
requestID,
requestContext.ServiceName,
provider,
consumer,
requestContext.Input,
compactRequest.ServiceFee,
compactRequest.RequestHeight,
compactRequest.ExpirationHeight,
requestContextId,
compactRequest.RequestContextBatchCounter,
), true
}
// IterateRequests iterates through all compact requests
func (k Keeper) IterateRequests(
ctx sdk.Context,
op func(requestID tmbytes.HexBytes, request types.CompactRequest) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.RequestKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
requestID := iterator.Key()[1:]
var request types.CompactRequest
k.cdc.MustUnmarshal(iterator.Value(), &request)
if stop := op(requestID, request); stop {
break
}
}
}
// RequestsIteratorByReqCtx returns an iterator for all requests of the specified request context ID and batch counter
func (k Keeper) RequestsIteratorByReqCtx(ctx sdk.Context, requestContextID tmbytes.HexBytes, batchCounter uint64) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetRequestSubspaceByReqCtx(requestContextID, batchCounter))
}
// AddActiveRequest adds the specified active request
func (k Keeper) AddActiveRequest(
ctx sdk.Context,
serviceName string,
provider sdk.AccAddress,
expirationHeight int64,
requestID tmbytes.HexBytes,
) {
k.AddActiveRequestByBinding(ctx, serviceName, provider, expirationHeight, requestID)
k.AddActiveRequestByID(ctx, requestID)
}
// DeleteActiveRequest deletes the specified active request
func (k Keeper) DeleteActiveRequest(
ctx sdk.Context,
serviceName string,
provider sdk.AccAddress,
expirationHeight int64,
requestID tmbytes.HexBytes,
) {
k.DeleteActiveRequestByBinding(ctx, serviceName, provider, expirationHeight, requestID)
k.DeleteActiveRequestByID(ctx, requestID)
}
// AddActiveRequestByBinding adds the specified active request by the binding
func (k Keeper) AddActiveRequestByBinding(
ctx sdk.Context,
serviceName string,
provider sdk.AccAddress,
expirationHeight int64,
requestID tmbytes.HexBytes,
) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.BytesValue{Value: requestID})
store.Set(types.GetActiveRequestKey(serviceName, provider, expirationHeight, requestID), bz)
}
// DeleteActiveRequestByBinding deletes the specified active request by the binding
func (k Keeper) DeleteActiveRequestByBinding(
ctx sdk.Context,
serviceName string,
provider sdk.AccAddress,
expirationHeight int64,
requestID tmbytes.HexBytes,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetActiveRequestKey(serviceName, provider, expirationHeight, requestID))
}
// AddActiveRequestByID adds the specified active request by request ID
func (k Keeper) AddActiveRequestByID(ctx sdk.Context, requestID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.BytesValue{Value: requestID})
store.Set(types.GetActiveRequestKeyByID(requestID), bz)
}
// DeleteActiveRequestByID deletes the specified active request by request ID
func (k Keeper) DeleteActiveRequestByID(ctx sdk.Context, requestID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetActiveRequestKeyByID(requestID))
}
// IsRequestActive checks if the specified request is active
func (k Keeper) IsRequestActive(ctx sdk.Context, requestID tmbytes.HexBytes) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetActiveRequestKeyByID(requestID))
}
// AddRequestBatchExpiration adds a request batch to the expiration queue
func (k Keeper) AddRequestBatchExpiration(ctx sdk.Context, requestContextID tmbytes.HexBytes, expirationHeight int64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.BytesValue{Value: requestContextID})
store.Set(types.GetExpiredRequestBatchKey(requestContextID, expirationHeight), bz)
k.SetRequestBatchExpirationHeight(ctx, requestContextID, expirationHeight)
}
// DeleteRequestBatchExpiration deletes the request batch from the expiration queue
func (k Keeper) DeleteRequestBatchExpiration(ctx sdk.Context, requestContextID tmbytes.HexBytes, expirationHeight int64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetExpiredRequestBatchKey(requestContextID, expirationHeight))
k.DeleteRequestBatchExpirationHeight(ctx, requestContextID)
}
// HasRequestBatchExpiration checks if the request batch expiration of the specified request context exists
func (k Keeper) HasRequestBatchExpiration(ctx sdk.Context, requestContextID tmbytes.HexBytes) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetExpiredRequestBatchHeightKey(requestContextID))
}
// AddNewRequestBatch adds a request batch to the new request batch queue
func (k Keeper) AddNewRequestBatch(ctx sdk.Context, requestContextID tmbytes.HexBytes, requestBatchHeight int64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.BytesValue{Value: requestContextID})
store.Set(types.GetNewRequestBatchKey(requestContextID, requestBatchHeight), bz)
k.SetNewRequestBatchHeight(ctx, requestContextID, requestBatchHeight)
}
// DeleteNewRequestBatch deletes the request batch in the given height from the new request batch queue
func (k Keeper) DeleteNewRequestBatch(ctx sdk.Context, requestContextID tmbytes.HexBytes, requestBatchHeight int64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetNewRequestBatchKey(requestContextID, requestBatchHeight))
k.DeleteNewRequestBatchHeight(ctx, requestContextID)
}
// HasNewRequestBatch checks if the new request batch of the specified request context exists
func (k Keeper) HasNewRequestBatch(ctx sdk.Context, requestContextID tmbytes.HexBytes) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetNewRequestBatchHeightKey(requestContextID))
}
// SetRequestBatchExpirationHeight sets the request batch expiration height for the specified request context
func (k Keeper) SetRequestBatchExpirationHeight(ctx sdk.Context, requestContextID tmbytes.HexBytes, expirationHeight int64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.Int64Value{Value: expirationHeight})
store.Set(types.GetExpiredRequestBatchHeightKey(requestContextID), bz)
}
// DeleteRequestBatchExpirationHeight deletes the request batch expiration height for the specified request context
func (k Keeper) DeleteRequestBatchExpirationHeight(ctx sdk.Context, requestContextID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetExpiredRequestBatchHeightKey(requestContextID))
}
// SetNewRequestBatchHeight sets the new request batch height for the specified request context
func (k Keeper) SetNewRequestBatchHeight(ctx sdk.Context, requestContextID tmbytes.HexBytes, requestBatchHeight int64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.Int64Value{Value: requestBatchHeight})
store.Set(types.GetNewRequestBatchHeightKey(requestContextID), bz)
}
// DeleteNewRequestBatchHeight deletes the new request batch height for the specified request context
func (k Keeper) DeleteNewRequestBatchHeight(ctx sdk.Context, requestContextID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetNewRequestBatchHeightKey(requestContextID))
}
// IterateExpiredRequestBatch iterates through the expired request batch queue in the specified height
func (k Keeper) IterateExpiredRequestBatch(
ctx sdk.Context,
expirationHeight int64,
op func(requestContextID tmbytes.HexBytes, requestContext types.RequestContext),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetExpiredRequestBatchSubspace(expirationHeight))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var requestContextID gogotypes.BytesValue
k.cdc.MustUnmarshal(iterator.Value(), &requestContextID)
requestContext, _ := k.GetRequestContext(ctx, requestContextID.Value)
op(requestContextID.Value, requestContext)
}
}
// IterateNewRequestBatch iterates through the new request batch queue in the specified height
func (k Keeper) IterateNewRequestBatch(
ctx sdk.Context,
requestBatchHeight int64,
op func(requestContextID tmbytes.HexBytes, requestContext *types.RequestContext),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetNewRequestBatchSubspace(requestBatchHeight))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var requestContextID gogotypes.BytesValue
k.cdc.MustUnmarshal(iterator.Value(), &requestContextID)
requestContext, _ := k.GetRequestContext(ctx, requestContextID.Value)
op(requestContextID.Value, &requestContext)
}
}
// ActiveRequestsIterator returns an iterator for all the active requests of the specified service binding
func (k Keeper) ActiveRequestsIterator(ctx sdk.Context, serviceName string, provider sdk.AccAddress) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetActiveRequestSubspace(serviceName, provider))
}
// ActiveRequestsIteratorByReqCtx returns an iterator for all the active requests of the specified service binding
func (k Keeper) ActiveRequestsIteratorByReqCtx(ctx sdk.Context, requestContextID tmbytes.HexBytes, batchCounter uint64) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetActiveRequestSubspaceByReqCtx(requestContextID, batchCounter))
}
// AllActiveRequestsIterator returns an iterator for all the active requests
func (k Keeper) AllActiveRequestsIterator(store sdk.KVStore) sdk.Iterator {
return sdk.KVStorePrefixIterator(store, types.ActiveRequestKey)
}
// IterateActiveRequests iterates through the active requests for the specified request context ID and batch counter
func (k Keeper) IterateActiveRequests(
ctx sdk.Context,
requestContextID tmbytes.HexBytes,
batchCounter uint64,
op func(requestID tmbytes.HexBytes, request types.Request),
) {
iterator := k.ActiveRequestsIteratorByReqCtx(ctx, requestContextID, batchCounter)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var requestID gogotypes.BytesValue
k.cdc.MustUnmarshal(iterator.Value(), &requestID)
request, _ := k.GetRequest(ctx, requestID.Value)
op(requestID.Value, request)
}
}
// FilterServiceProviders gets the providers which satisfy the specified requirement
func (k Keeper) FilterServiceProviders(
ctx sdk.Context,
serviceName string,
providers []sdk.AccAddress,
timeout int64,
serviceFeeCap sdk.Coins,
consumer sdk.AccAddress,
) (
[]sdk.AccAddress, sdk.Coins, string, error,
) {
var newProviders []sdk.AccAddress
var totalPrices sdk.Coins
for _, provider := range providers {
binding, found := k.GetServiceBinding(ctx, serviceName, provider)
if found && binding.Available {
if binding.QoS <= uint64(timeout) {
exchangedPrice, rawDenom, err := k.GetExchangedPrice(ctx, consumer, binding)
if err != nil {
return nil, nil, rawDenom, err
}
provider, err := sdk.AccAddressFromBech32(binding.Provider)
if err != nil {
return nil, nil, rawDenom, fmt.Errorf("invalid provider address: %s", provider)
}
price := k.GetPricing(ctx, binding.ServiceName, provider).Price
if exchangedPrice.IsAllLTE(serviceFeeCap) {
newProviders = append(newProviders, provider)
totalPrices = totalPrices.Add(price...)
}
}
}
}
return newProviders, totalPrices, "", nil
}
// DeductServiceFees deducts the given service fees from the specified consumer
func (k Keeper) DeductServiceFees(ctx sdk.Context, consumer sdk.AccAddress, serviceFees sdk.Coins) error {
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, consumer, types.RequestAccName, serviceFees)
}
func (k Keeper) GetPrice(
ctx sdk.Context,
consumer sdk.AccAddress,
binding types.ServiceBinding,
) sdk.Coins {
provider, _ := sdk.AccAddressFromBech32(binding.Provider)
pricing := k.GetPricing(ctx, binding.ServiceName, provider)
// get discounts
discountByTime := types.GetDiscountByTime(pricing, ctx.BlockTime())
discountByVolume := types.GetDiscountByVolume(
pricing, k.GetRequestVolume(ctx, consumer, binding.ServiceName, provider),
)
var price []sdk.Coin
for _, token := range pricing.Price {
priceAmount := sdk.NewDecFromInt(token.Amount).Mul(discountByTime).Mul(discountByVolume)
price = append(price, sdk.NewCoin(token.Denom, priceAmount.TruncateInt()))
}
return sdk.NewCoins(price...)
}
// AddResponse adds the response for the specified request ID
func (k Keeper) AddResponse(
ctx sdk.Context,
requestID tmbytes.HexBytes,
provider sdk.AccAddress,
result string,
output string,
) (
request types.Request,
response types.Response,
err error,
) {
request, found := k.GetRequest(ctx, requestID)
if !found {
return request, response, sdkerrors.Wrap(types.ErrUnknownRequest, requestID.String())
}
requestProvider, _ := sdk.AccAddressFromBech32(request.Provider)
if !provider.Equals(requestProvider) {
return request, response, sdkerrors.Wrap(types.ErrInvalidResponse, "provider does not match")
}
if !k.IsRequestActive(ctx, requestID) {
return request, response, sdkerrors.Wrap(types.ErrInvalidResponse, "request is not active")
}
if len(output) > 0 {
if err := types.ValidateResponseOutput(output); err != nil {
return request, response, sdkerrors.Wrap(types.ErrInvalidResponseOutput, err.Error())
}
}
if err := k.AddEarnedFee(ctx, provider, request.ServiceFee); err != nil {
return request, response, err
}
requestContextID, _ := hex.DecodeString(request.RequestContextId)
consumer, _ := sdk.AccAddressFromBech32(request.Consumer)
response = types.NewResponse(provider, consumer, result, output, requestContextID, request.RequestContextBatchCounter)
k.SetResponse(ctx, requestID, response)
k.DeleteActiveRequest(ctx, request.ServiceName, provider, request.ExpirationHeight, requestID)
k.IncreaseRequestVolume(ctx, consumer, request.ServiceName, provider)
requestContext, _ := k.GetRequestContext(ctx, requestContextID)
requestContext.BatchResponseCount++
if requestContext.BatchResponseCount == requestContext.BatchRequestCount {
requestContext = k.CompleteBatch(ctx, requestContext, requestContextID)
}
k.SetRequestContext(ctx, requestContextID, requestContext)
return request, response, nil
}
// Callback callbacks the corresponding response callback handler
func (k Keeper) Callback(ctx sdk.Context, requestContextID tmbytes.HexBytes) {
requestContext, _ := k.GetRequestContext(ctx, requestContextID)
respCallback, _ := k.GetResponseCallback(requestContext.ModuleName)
outputs := k.GetResponseOutputs(ctx, requestContextID, requestContext.BatchCounter)
if len(outputs) >= int(requestContext.BatchResponseThreshold) {
respCallback(ctx, requestContextID, outputs, nil)
} else {
respCallback(
ctx, requestContextID, outputs,
fmt.Errorf(
"batch %d at least %d valid outputs required, but %d received",
requestContext.BatchCounter, requestContext.BatchResponseThreshold, len(outputs),
),
)
}
}
// SetResponse sets the specified response
func (k Keeper) SetResponse(ctx sdk.Context, requestID tmbytes.HexBytes, response types.Response) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&response)
store.Set(types.GetResponseKey(requestID), bz)
}
// GetResponse returns a response with the speicified request ID
func (k Keeper) GetResponse(ctx sdk.Context, requestID tmbytes.HexBytes) (response types.Response, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetResponseKey(requestID))
if bz == nil {
return response, false
}
k.cdc.MustUnmarshal(bz, &response)
return response, true
}
// DeleteResponse deletes a response with the speicified request ID
func (k Keeper) DeleteResponse(ctx sdk.Context, requestID tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetResponseKey(requestID))
}
// IterateResponses iterates through all responses
func (k Keeper) IterateResponses(
ctx sdk.Context,
op func(requestID tmbytes.HexBytes, response types.Response) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ResponseKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
requestID := iterator.Key()[1:]
var response types.Response
k.cdc.MustUnmarshal(iterator.Value(), &response)
if stop := op(requestID, response); stop {
break
}
}
}
// ResponsesIteratorByReqCtx returns an iterator for all responses of the specified request context and batch counter
func (k Keeper) ResponsesIteratorByReqCtx(ctx sdk.Context, requestContextID tmbytes.HexBytes, batchCounter uint64) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetResponseSubspaceByReqCtx(requestContextID, batchCounter))
}
// GetResponseOutputs retrieves all response outputs of the specified request context and batch counter
func (k Keeper) GetResponseOutputs(ctx sdk.Context, requestContextID tmbytes.HexBytes, batchCounter uint64) []string {