forked from algorand/go-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplications_integration_test.go
903 lines (774 loc) · 27.2 KB
/
applications_integration_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
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
package test
import (
"bytes"
"context"
"crypto/sha512"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/cucumber/godog"
"github.com/algorand/go-algorand-sdk/abi"
"github.com/algorand/go-algorand-sdk/client/v2/algod"
"github.com/algorand/go-algorand-sdk/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/crypto"
sdkJson "github.com/algorand/go-algorand-sdk/encoding/json"
"github.com/algorand/go-algorand-sdk/future"
"github.com/algorand/go-algorand-sdk/types"
)
var algodV2client *algod.Client
var tx types.Transaction
var transientAccount crypto.Account
var applicationId uint64
var applicationIds []uint64
var txComposerResult future.ExecuteResult
func anAlgodVClientConnectedToPortWithToken(v int, host string, port int, token string) error {
var err error
portAsString := strconv.Itoa(port)
fullHost := "http://" + host + ":" + portAsString
algodV2client, err = algod.MakeClient(fullHost, token)
gh = []byte("MLWBXKMRJ5W3USARAFOHPQJAF4DN6KY3ZJVPIXKODKNN5ZXSZ2DQ")
return err
}
func iCreateANewTransientAccountAndFundItWithMicroalgos(microalgos int) error {
var err error
transientAccount = crypto.GenerateAccount()
params, err := algodV2client.SuggestedParams().Do(context.Background())
if err != nil {
return err
}
params.Fee = types.MicroAlgos(fee)
ltxn, err := future.MakePaymentTxn(accounts[1], transientAccount.Address.String(), uint64(microalgos), note, close, params)
if err != nil {
return err
}
lsk, err := kcl.ExportKey(handle, walletPswd, accounts[1])
if err != nil {
return err
}
ltxid, lstx, err := crypto.SignTransaction(lsk.PrivateKey, ltxn)
if err != nil {
return err
}
_, err = algodV2client.SendRawTransaction(lstx).Do(context.Background())
if err != nil {
return err
}
_, err = future.WaitForConfirmation(algodV2client, ltxid, 5, context.Background())
if err != nil {
return err
}
return nil
}
func iFundTheCurrentApplicationsAddress(microalgos int) error {
address := crypto.GetApplicationAddress(applicationId)
params, err := algodV2client.SuggestedParams().Do(context.Background())
if err != nil {
return err
}
txn, err := future.MakePaymentTxn(accounts[0], address.String(), uint64(microalgos), nil, "", params)
if err != nil {
return err
}
res, err := kcl.SignTransaction(handle, walletPswd, txn)
if err != nil {
return err
}
txid, err = algodV2client.SendRawTransaction(res.SignedTransaction).Do(context.Background())
if err != nil {
return err
}
_, err = future.WaitForConfirmation(algodV2client, txid, 5, context.Background())
return err
}
func readTealProgram(fileName string) ([]byte, error) {
fileContents, err := loadResource(fileName)
if err != nil {
return nil, err
}
if strings.HasSuffix(fileName, ".teal") {
// need to compile TEAL source file
response, err := algodV2client.TealCompile(fileContents).Do(context.Background())
if err != nil {
return nil, err
}
return base64.StdEncoding.DecodeString(response.Result)
}
return fileContents, nil
}
func iBuildAnApplicationTransaction(
operation, approvalProgram, clearProgram string,
globalBytes, globalInts, localBytes, localInts int,
appArgs, foreignApps, foreignAssets, appAccounts string, extraPages int) error {
var clearP []byte
var approvalP []byte
var err error
var ghbytes [32]byte
copy(ghbytes[:], gh)
var suggestedParams types.SuggestedParams
suggestedParams, err = algodV2client.SuggestedParams().Do(context.Background())
if err != nil {
return err
}
if approvalProgram != "" {
approvalP, err = readTealProgram(approvalProgram)
if err != nil {
return err
}
}
if clearProgram != "" {
clearP, err = readTealProgram(clearProgram)
if err != nil {
return err
}
}
args, err := parseAppArgs(appArgs)
if err != nil {
return err
}
var accs []string
if appAccounts != "" {
accs = strings.Split(appAccounts, ",")
}
fApp, err := splitUint64(foreignApps)
if err != nil {
return err
}
fAssets, err := splitUint64(foreignAssets)
if err != nil {
return err
}
gSchema := types.StateSchema{NumUint: uint64(globalInts), NumByteSlice: uint64(globalBytes)}
lSchema := types.StateSchema{NumUint: uint64(localInts), NumByteSlice: uint64(localBytes)}
switch operation {
case "create":
if extraPages > 0 {
tx, err = future.MakeApplicationCreateTxWithExtraPages(false, approvalP, clearP,
gSchema, lSchema, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{}, uint32(extraPages))
} else {
tx, err = future.MakeApplicationCreateTx(false, approvalP, clearP,
gSchema, lSchema, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
}
if err != nil {
return err
}
case "create_optin":
if extraPages > 0 {
tx, err = future.MakeApplicationCreateTxWithExtraPages(true, approvalP, clearP,
gSchema, lSchema, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{}, uint32(extraPages))
} else {
tx, err = future.MakeApplicationCreateTx(true, approvalP, clearP,
gSchema, lSchema, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
}
if err != nil {
return err
}
case "update":
tx, err = future.MakeApplicationUpdateTx(applicationId, args, accs, fApp, fAssets,
approvalP, clearP,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
if err != nil {
return err
}
case "call":
tx, err = future.MakeApplicationCallTx(applicationId, args, accs,
fApp, fAssets, types.NoOpOC, approvalP, clearP, gSchema, lSchema,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
case "optin":
tx, err = future.MakeApplicationOptInTx(applicationId, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
if err != nil {
return err
}
case "clear":
tx, err = future.MakeApplicationClearStateTx(applicationId, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
if err != nil {
return err
}
case "closeout":
tx, err = future.MakeApplicationCloseOutTx(applicationId, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
if err != nil {
return err
}
case "delete":
tx, err = future.MakeApplicationDeleteTx(applicationId, args, accs, fApp, fAssets,
suggestedParams, transientAccount.Address, nil, types.Digest{}, [32]byte{}, types.Address{})
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported tx type: %s", operation)
}
return nil
}
func iSignAndSubmitTheTransactionSavingTheTxidIfThereIsAnErrorItIs(expectedErr string) error {
var err error
var lstx []byte
txid, lstx, err = crypto.SignTransaction(transientAccount.PrivateKey, tx)
if err != nil {
return err
}
txid, err = algodV2client.SendRawTransaction(lstx).Do(context.Background())
if err != nil && len(expectedErr) != 0 {
if strings.Contains(err.Error(), expectedErr) {
return nil
}
}
return err
}
func iWaitForTheTransactionToBeConfirmed() error {
_, err := future.WaitForConfirmation(algodV2client, txid, 5, context.Background())
if err != nil {
return err
}
return nil
}
func iRememberTheNewApplicationID() error {
response, _, err := algodV2client.PendingTransactionInformation(txid).Do(context.Background())
applicationId = response.ApplicationIndex
applicationIds = append(applicationIds, applicationId)
return err
}
func iResetTheArrayOfApplicationIDsToRemember() error {
applicationIds = nil
return nil
}
func iGetTheAccountAddressForTheCurrentApp() error {
actual := crypto.GetApplicationAddress(applicationId)
prefix := []byte("appID")
preimage := make([]byte, len(prefix)+8) // 8 = number of bytes in a uint64
copy(preimage, prefix)
binary.BigEndian.PutUint64(preimage[len(prefix):], applicationId)
expected := types.Address(sha512.Sum512_256(preimage))
if expected != actual {
return fmt.Errorf("Addresses do not match. Expected %s, got %s", expected.String(), actual.String())
}
return nil
}
func parseAppArgs(appArgsString string) (appArgs [][]byte, err error) {
if appArgsString == "" {
return make([][]byte, 0), nil
}
argsArray := strings.Split(appArgsString, ",")
resp := make([][]byte, len(argsArray))
for idx, arg := range argsArray {
typeArg := strings.Split(arg, ":")
switch typeArg[0] {
case "str":
resp[idx] = []byte(typeArg[1])
case "int":
intval, _ := strconv.ParseUint(typeArg[1], 10, 64)
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, intval)
if err != nil {
return nil, fmt.Errorf("failed to convert %s to bytes", arg)
}
resp[idx] = buf.Bytes()
default:
return nil, fmt.Errorf("Applications doesn't currently support argument of type %s", typeArg[0])
}
}
return resp, err
}
func splitUint64(uint64s string) ([]uint64, error) {
if uint64s == "" {
return make([]uint64, 0), nil
}
uintarr := strings.Split(uint64s, ",")
resp := make([]uint64, len(uintarr))
var err error
for idx, val := range uintarr {
resp[idx], err = strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, err
}
}
return resp, nil
}
func theTransientAccountShouldHave(appCreated string, byteSlices, uints int,
applicationState, key, value string) error {
acct, err := algodV2client.AccountInformation(transientAccount.Address.String()).Do(context.Background())
if err != nil {
return err
}
if acct.AppsTotalSchema.NumByteSlice != uint64(byteSlices) {
return fmt.Errorf("expected NumByteSlices %d, got %d",
byteSlices, acct.AppsTotalSchema.NumByteSlice)
}
if acct.AppsTotalSchema.NumUint != uint64(uints) {
return fmt.Errorf("expected NumUnit %d, got %d",
uints, acct.AppsTotalSchema.NumUint)
}
created, err := strconv.ParseBool(appCreated)
if err != nil {
return err
}
// If we don't expect the app to exist, verify that it isn't there and exit.
if !created {
for _, ap := range acct.CreatedApps {
if ap.Id == applicationId {
return fmt.Errorf("AppId is not expected to be in the account")
}
}
return nil
}
appIdFound := false
for _, ap := range acct.CreatedApps {
if ap.Id == applicationId {
appIdFound = true
break
}
}
if !appIdFound {
return fmt.Errorf("AppId %d is not found in the account", applicationId)
}
// If there is no key to check, we're done.
if key == "" {
return nil
}
// Verify the key-value is set
found := false
var keyValues []models.TealKeyValue
switch strings.ToLower(applicationState) {
case "local":
count := 0
for _, als := range acct.AppsLocalState {
if als.Id == applicationId {
keyValues = als.KeyValue
count++
}
}
if count != 1 {
return fmt.Errorf("Expected only one matching AppsLocalState, found %d",
count)
}
case "global":
count := 0
for _, als := range acct.CreatedApps {
if als.Id == applicationId {
keyValues = als.Params.GlobalState
count++
}
}
if count != 1 {
return fmt.Errorf("Expected only one matching CreatedApps, found %d",
count)
}
default:
return fmt.Errorf("Unknown application state: %s", applicationState)
}
if len(keyValues) == 0 {
return fmt.Errorf("Expected keyvalues length to be greater than 0")
}
for _, kv := range keyValues {
if kv.Key == key {
if kv.Value.Type == 1 {
if kv.Value.Bytes != value {
return fmt.Errorf("Value mismatch: expected: '%s', got '%s'",
value, kv.Value.Bytes)
}
} else if kv.Value.Type == 0 {
val, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
if kv.Value.Uint != val {
return fmt.Errorf("Value mismatch: expected: '%s', got '%s'",
value, kv.Value.Bytes)
}
}
found = true
}
}
if !found {
fmt.Errorf("Could not find key '%s'", key)
}
return nil
}
func theUnconfirmedPendingTransactionByIDShouldHaveNoApplyDataFields() error {
status, _, err := algodV2client.PendingTransactionInformation(txid).Do(context.Background())
if err != nil {
return err
}
if status.ConfirmedRound == 0 {
if len(status.GlobalStateDelta) != 0 {
return fmt.Errorf("unexpected global state delta, there should be none: %v", status.GlobalStateDelta)
}
if len(status.LocalStateDelta) != 0 {
return fmt.Errorf("unexpected local state delta, there should be none: %v", status.LocalStateDelta)
}
}
return nil
}
func getAccountDelta(addr string, data []models.AccountStateDelta) []models.EvalDeltaKeyValue {
for _, v := range data {
if v.Address == addr {
return v.Delta
}
}
return nil
}
func theConfirmedPendingTransactionByIDShouldHaveAStateChangeForToIndexerShouldAlsoConfirmThis(stateLocation, key, newValue string, indexer int) error {
status, _, err := algodV2client.PendingTransactionInformation(txid).Do(context.Background())
if err != nil {
return err
}
c1 := make(chan models.Transaction, 1)
go func() {
for true {
indexerResponse, _ := indexerClients[indexer].SearchForTransactions().TXID(txid).Do(context.Background())
if len(indexerResponse.Transactions) == 1 {
c1 <- indexerResponse.Transactions[0]
}
time.Sleep(time.Second)
}
}()
var indexerTx models.Transaction
select {
case res := <-c1:
indexerTx = res
case <-time.After(5 * time.Second):
return fmt.Errorf("timeout waiting for indexer trasaction")
}
var algodKeyValues []models.EvalDeltaKeyValue
var indexerKeyValues []models.EvalDeltaKeyValue
switch stateLocation {
case "local":
addr := indexerTx.Sender
algodKeyValues = getAccountDelta(addr, status.LocalStateDelta)
indexerKeyValues = getAccountDelta(addr, indexerTx.LocalStateDelta)
case "global":
algodKeyValues = status.GlobalStateDelta
indexerKeyValues = indexerTx.GlobalStateDelta
default:
return fmt.Errorf("unknown location: " + stateLocation)
}
// algod
if len(algodKeyValues) != 1 {
return fmt.Errorf("expected 1 key value, found: %d", len(algodKeyValues))
}
if algodKeyValues[0].Key != key {
return fmt.Errorf("wrong key in algod: %s != %s", algodKeyValues[0].Key, key)
}
// indexer
if len(indexerKeyValues) != 1 {
return fmt.Errorf("expected 1 key value, found: %d", len(indexerKeyValues))
}
if indexerKeyValues[0].Key != key {
return fmt.Errorf("wrong key in indexer: %s != %s", indexerKeyValues[0].Key, key)
}
if indexerKeyValues[0].Value.Action != algodKeyValues[0].Value.Action {
return fmt.Errorf("action mismatch between algod and indexer")
}
switch algodKeyValues[0].Value.Action {
case uint64(1):
// bytes
if algodKeyValues[0].Value.Bytes != newValue {
return fmt.Errorf("algod value mismatch: %s != %s", algodKeyValues[0].Value.Bytes, newValue)
}
if indexerKeyValues[0].Value.Bytes != newValue {
return fmt.Errorf("indexer value mismatch: %s != %s", indexerKeyValues[0].Value.Bytes, newValue)
}
case uint64(2):
// int
newValueInt, err := strconv.ParseUint(newValue, 10, 64)
if err != nil {
return fmt.Errorf("problem parsing new int value: %s", newValue)
}
if algodKeyValues[0].Value.Uint != newValueInt {
return fmt.Errorf("algod value mismatch: %d != %s", algodKeyValues[0].Value.Uint, newValue)
}
if indexerKeyValues[0].Value.Uint != newValueInt {
return fmt.Errorf("indexer value mismatch: %d != %s", indexerKeyValues[0].Value.Uint, newValue)
}
default:
return fmt.Errorf("unexpected action: %d", algodKeyValues[0].Value.Action)
}
return nil
}
func suggestedParamsAlgodV2() error {
var err error
sugParams, err = aclv2.SuggestedParams().Do(context.Background())
return err
}
func iAddTheCurrentTransactionWithSignerToTheComposer() error {
err := txComposer.AddTransaction(accountTxAndSigner)
return err
}
func iCloneTheComposer() error {
txComposer = txComposer.Clone()
return nil
}
func iExecuteTheCurrentTransactionGroupWithTheComposer() error {
var err error
txComposerResult, err = txComposer.Execute(algodV2client, context.Background(), 10)
return err
}
func theAppShouldHaveReturned(commaSeparatedB64Results string) error {
b64ExpectedResults := strings.Split(commaSeparatedB64Results, ",")
if len(b64ExpectedResults) != len(txComposerResult.MethodResults) {
return fmt.Errorf("length of expected results doesn't match actual: %d != %d", len(b64ExpectedResults), len(txComposerResult.MethodResults))
}
for i, b64ExpectedResult := range b64ExpectedResults {
expectedResult, err := base64.StdEncoding.DecodeString(b64ExpectedResult)
if err != nil {
return err
}
actualResult := txComposerResult.MethodResults[i]
method := actualResult.Method
if actualResult.DecodeError != nil {
return actualResult.DecodeError
}
if !bytes.Equal(actualResult.RawReturnValue, expectedResult) {
return fmt.Errorf("Actual result does not match expected result. Actual: %s\n", base64.StdEncoding.EncodeToString(actualResult.RawReturnValue))
}
if method.Returns.IsVoid() {
if len(expectedResult) > 0 {
return fmt.Errorf("Expected result should be empty")
}
continue
}
abiReturnType, err := method.Returns.GetTypeObject()
if err != nil {
return err
}
expectedValue, err := abiReturnType.Decode(expectedResult)
if err != nil {
return err
}
if !reflect.DeepEqual(expectedValue, actualResult.ReturnValue) {
return fmt.Errorf("the decoded expected value doesn't match the actual result")
}
}
return nil
}
func theAppShouldHaveReturnedABITypes(colonSeparatedExpectedTypeStrings string) error {
expectedTypeStrings := strings.Split(colonSeparatedExpectedTypeStrings, ":")
if len(expectedTypeStrings) != len(txComposerResult.MethodResults) {
return fmt.Errorf("length of expected results doesn't match actual: %d != %d", len(expectedTypeStrings), len(txComposerResult.MethodResults))
}
for i, expectedTypeString := range expectedTypeStrings {
actualResult := txComposerResult.MethodResults[i]
if actualResult.DecodeError != nil {
return actualResult.DecodeError
}
if expectedTypeString == abi.VoidReturnType {
if len(actualResult.RawReturnValue) != 0 {
return fmt.Errorf("No return bytes were expected, but some are present")
}
continue
}
expectedType, err := abi.TypeOf(expectedTypeString)
if err != nil {
return err
}
decoded, err := expectedType.Decode(actualResult.RawReturnValue)
if err != nil {
return err
}
reencoded, err := expectedType.Encode(decoded)
if err != nil {
return err
}
if !bytes.Equal(reencoded, actualResult.RawReturnValue) {
return fmt.Errorf("The round trip result does not match the original result")
}
}
return nil
}
func checkAtomicResultAgainstValue(resultIndex int, path, expectedValue string) error {
keys := strings.Split(path, ".")
info := txComposerResult.MethodResults[resultIndex].TransactionInfo
jsonBytes := sdkJson.Encode(&info)
var genericJson interface{}
decoder := json.NewDecoder(bytes.NewReader(jsonBytes))
decoder.UseNumber()
err := decoder.Decode(&genericJson)
if err != nil {
return err
}
for i, key := range keys {
var value interface{}
intKey, err := strconv.Atoi(key)
if err == nil {
// key is an array index
genericArray, ok := genericJson.([]interface{})
if !ok {
return fmt.Errorf("Path component %d is an array index (%d), but the parent is not an array. Parent type: %s", i, intKey, reflect.TypeOf(genericJson))
}
if intKey < 0 || intKey >= len(genericArray) {
return fmt.Errorf("Path component %d is an array index (%d) outside of the parent array's range. Parent length: %d", i, intKey, len(genericArray))
}
value = genericArray[intKey]
} else {
// key is an object field
genericObject, ok := genericJson.(map[string]interface{})
if !ok {
return fmt.Errorf("Path component %d is an object field ('%s'), but the parent is not an object. Parent type: %s", i, key, reflect.TypeOf(genericJson))
}
value, ok = genericObject[key]
if !ok {
var parentFields []string
for field := range genericObject {
parentFields = append(parentFields, "'"+field+"'")
}
return fmt.Errorf("Path component %d is an object field ('%s'), but the parent does not contain the field. Parent fields are: %s", i, key, strings.Join(parentFields, ","))
}
}
genericJson = value
}
// we have arrived at the target object
switch actual := genericJson.(type) {
case string:
if actual != expectedValue {
return fmt.Errorf("String values not equal. Expected '%s', got '%s'", expectedValue, actual)
}
case json.Number:
actualParsed, err := strconv.ParseUint(actual.String(), 10, 64)
if err != nil {
return err
}
expectedParsed, err := strconv.ParseUint(expectedValue, 10, 64)
if err != nil {
return err
}
if actualParsed != expectedParsed {
return fmt.Errorf("Int values not equal. Expected %d, got %d", expectedParsed, actualParsed)
}
default:
return fmt.Errorf("The final path element does not resolve to a support type. Type is %s", reflect.TypeOf(genericJson))
}
return nil
}
func checkInnerTxnGroupIDs(colonSeparatedPathsString string) error {
paths := [][]int{}
commaSeparatedPathStrings := strings.Split(colonSeparatedPathsString, ":")
for _, commaSeparatedPathString := range commaSeparatedPathStrings {
pathOfStrings := strings.Split(commaSeparatedPathString, ",")
path := make([]int, len(pathOfStrings))
for i, stringComponent := range pathOfStrings {
intComponent, err := strconv.Atoi(stringComponent)
if err != nil {
return err
}
path[i] = intComponent
}
paths = append(paths, path)
}
var txInfosToCheck []models.PendingTransactionResponse
for _, path := range paths {
var current models.PendingTransactionResponse
for pathIndex, innerTxnIndex := range path {
if pathIndex == 0 {
current = models.PendingTransactionResponse(txComposerResult.MethodResults[innerTxnIndex].TransactionInfo)
} else {
current = current.InnerTxns[innerTxnIndex]
}
}
txInfosToCheck = append(txInfosToCheck, current)
}
var group types.Digest
for i, txInfo := range txInfosToCheck {
if i == 0 {
group = txInfo.Transaction.Txn.Group
}
if group != txInfo.Transaction.Txn.Group {
return fmt.Errorf("Group hashes differ: %s != %s", group, txInfo.Transaction.Txn.Group)
}
}
return nil
}
func checkSpinResult(resultIndex int, method, r string) error {
if method != "spin()" {
return fmt.Errorf("Incorrect method name, expected 'spin()', got '%s'", method)
}
result := txComposerResult.MethodResults[resultIndex]
decodedResult := result.ReturnValue.([]interface{})
spin := decodedResult[0].([]interface{})
spinBytes := make([]byte, len(spin))
for i, value := range spin {
spinBytes[i] = value.(byte)
}
matched, err := regexp.Match(r, spinBytes)
if err != nil {
return err
}
if !matched {
return fmt.Errorf("Result did not match regex. Spin result: %s", string(spinBytes))
}
return nil
}
func sha512_256AsUint64(preimage []byte) uint64 {
hashed := sha512.Sum512_256(preimage)
return binary.BigEndian.Uint64(hashed[:8])
}
func checkRandomIntResult(resultIndex, input int) error {
result := txComposerResult.MethodResults[resultIndex]
decodedResult := result.ReturnValue.([]interface{})
randInt := decodedResult[0].(uint64)
witness := decodedResult[1].([]interface{})
witnessBytes := make([]byte, len(witness))
for i, value := range witness {
witnessBytes[i] = value.(byte)
}
x := sha512_256AsUint64(witnessBytes)
quotient := x % uint64(input)
if quotient != randInt {
return fmt.Errorf("Unexpected result: quotient is %d and randInt is %d", quotient, randInt)
}
return nil
}
func checkRandomElementResult(resultIndex int, input string) error {
result := txComposerResult.MethodResults[resultIndex]
decodedResult := result.ReturnValue.([]interface{})
randElt := decodedResult[0].(byte)
witness := decodedResult[1].([]interface{})
witnessBytes := make([]byte, len(witness))
for i, value := range witness {
witnessBytes[i] = value.(byte)
}
x := sha512_256AsUint64(witnessBytes)
quotient := x % uint64(len(input))
if input[quotient] != randElt {
return fmt.Errorf("Unexpected result: input[quotient] is %d and randElt is %d", input[quotient], randElt)
}
return nil
}
//@applications.verified
func ApplicationsContext(s *godog.Suite) {
s.Step(`^an algod v(\d+) client connected to "([^"]*)" port (\d+) with token "([^"]*)"$`, anAlgodVClientConnectedToPortWithToken)
s.Step(`^I create a new transient account and fund it with (\d+) microalgos\.$`, iCreateANewTransientAccountAndFundItWithMicroalgos)
s.Step(`^I build an application transaction with the transient account, the current application, suggested params, operation "([^"]*)", approval-program "([^"]*)", clear-program "([^"]*)", global-bytes (\d+), global-ints (\d+), local-bytes (\d+), local-ints (\d+), app-args "([^"]*)", foreign-apps "([^"]*)", foreign-assets "([^"]*)", app-accounts "([^"]*)", extra-pages (\d+)$`, iBuildAnApplicationTransaction)
s.Step(`^I sign and submit the transaction, saving the txid\. If there is an error it is "([^"]*)"\.$`, iSignAndSubmitTheTransactionSavingTheTxidIfThereIsAnErrorItIs)
s.Step(`^I wait for the transaction to be confirmed\.$`, iWaitForTheTransactionToBeConfirmed)
s.Step(`^I remember the new application ID\.$`, iRememberTheNewApplicationID)
s.Step(`^I reset the array of application IDs to remember\.$`, iResetTheArrayOfApplicationIDsToRemember)
s.Step(`^I get the account address for the current application and see that it matches the app id\'s hash$`, iGetTheAccountAddressForTheCurrentApp)
s.Step(`^The transient account should have the created app "([^"]*)" and total schema byte-slices (\d+) and uints (\d+), the application "([^"]*)" state contains key "([^"]*)" with value "([^"]*)"$`,
theTransientAccountShouldHave)
s.Step(`^the unconfirmed pending transaction by ID should have no apply data fields\.$`, theUnconfirmedPendingTransactionByIDShouldHaveNoApplyDataFields)
s.Step(`^the confirmed pending transaction by ID should have a "([^"]*)" state change for "([^"]*)" to "([^"]*)", indexer (\d+) should also confirm this\.$`, theConfirmedPendingTransactionByIDShouldHaveAStateChangeForToIndexerShouldAlsoConfirmThis)
s.Step(`^suggested transaction parameters from the algod v2 client$`, suggestedParamsAlgodV2)
s.Step(`^I add the current transaction with signer to the composer\.$`, iAddTheCurrentTransactionWithSignerToTheComposer)
s.Step(`^I clone the composer\.$`, iCloneTheComposer)
s.Step(`^I execute the current transaction group with the composer\.$`, iExecuteTheCurrentTransactionGroupWithTheComposer)
s.Step(`^The app should have returned "([^"]*)"\.$`, theAppShouldHaveReturned)
s.Step(`^The app should have returned ABI types "([^"]*)"\.$`, theAppShouldHaveReturnedABITypes)
s.Step(`^I fund the current application\'s address with (\d+) microalgos\.$`, iFundTheCurrentApplicationsAddress)
s.Step(`^I can dig the (\d+)th atomic result with path "([^"]*)" and see the value "([^"]*)"$`, checkAtomicResultAgainstValue)
s.Step(`^I dig into the paths "([^"]*)" of the resulting atomic transaction tree I see group ids and they are all the same$`, checkInnerTxnGroupIDs)
s.Step(`^The (\d+)th atomic result for "([^"]*)" satisfies the regex "([^"]*)"$`, checkSpinResult)
s.Step(`^The (\d+)th atomic result for randomInt\((\d+)\) proves correct$`, checkRandomIntResult)
s.Step(`^The (\d+)th atomic result for randElement\("([^"]*)"\) proves correct$`, checkRandomElementResult)
}