forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_orgs_test.go
1068 lines (914 loc) · 41.6 KB
/
multiple_orgs_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
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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package orgs
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/multi"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
lcpackager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
mb "github.com/hyperledger/fabric-protos-go/msp"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/policydsl"
)
const (
org1 = "Org1"
org2 = "Org2"
ordererAdminUser = "Admin"
ordererOrgName = "OrdererOrg"
org1AdminUser = "Admin"
org2AdminUser = "Admin"
org1User = "User1"
org2User = "User1"
channelID = "orgchannel"
ccPath = "github.com/example_cc"
)
var (
// SDK
sdk *fabsdk.FabricSDK
// Org MSP clients
org1MspClient *mspclient.Client
org2MspClient *mspclient.Client
// Peers
orgTestPeer0 fab.Peer
orgTestPeer1 fab.Peer
exampleCC = "example_cc_fabtest_e2e" + metadata.TestRunID
)
// used to create context for different tests in the orgs package
type multiorgContext struct {
// client contexts
ordererClientContext contextAPI.ClientProvider
org1AdminClientContext contextAPI.ClientProvider
org2AdminClientContext contextAPI.ClientProvider
org1ResMgmt *resmgmt.Client
org2ResMgmt *resmgmt.Client
ccName string
ccVersion string
channelID string
}
func TestMain(m *testing.M) {
err := setup()
if err != nil {
panic(fmt.Sprintf("unable to setup [%s]", err))
}
r := m.Run()
teardown()
os.Exit(r)
}
func setup() error {
// Create SDK setup for the integration tests
var err error
sdk, err = fabsdk.New(integration.ConfigBackend)
if err != nil {
return errors.Wrap(err, "Failed to create new SDK")
}
org1MspClient, err = mspclient.New(sdk.Context(), mspclient.WithOrg(org1))
if err != nil {
return errors.Wrap(err, "failed to create org1MspClient")
}
org2MspClient, err = mspclient.New(sdk.Context(), mspclient.WithOrg(org2))
if err != nil {
return errors.Wrap(err, "failed to create org2MspClient")
}
return nil
}
func teardown() {
if sdk != nil {
sdk.Close()
}
}
// TestOrgsEndToEnd creates a channel with two organisations, installs chaincode
// on each of them, and finally invokes a transaction on an org2 peer and queries
// the result from an org1 peer
func TestOrgsEndToEnd(t *testing.T) {
// Delete all private keys from the crypto suite store
// and users from the user store at the end
integration.CleanupUserData(t, sdk)
defer integration.CleanupUserData(t, sdk)
// Load specific targets for move funds test
loadOrgPeers(t, sdk.Context(fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1)))
//prepare contexts
mc := multiorgContext{
ordererClientContext: sdk.Context(fabsdk.WithUser(ordererAdminUser), fabsdk.WithOrg(ordererOrgName)),
org1AdminClientContext: sdk.Context(fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1)),
org2AdminClientContext: sdk.Context(fabsdk.WithUser(org2AdminUser), fabsdk.WithOrg(org2)),
ccName: exampleCC, // basic multi orgs test uses exampleCC for testing
ccVersion: "0",
channelID: channelID,
}
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
_, err = integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
setupClientContextsAndChannel(t, sdk, &mc)
joined, err := integration.IsJoinedChannel(channelID, mc.org1ResMgmt, org1Peers[0])
require.NoError(t, err)
if !joined {
createAndJoinChannel(t, &mc)
}
expectedValue := testWithOrg1(t, sdk, &mc)
expectedValue = testWithOrg2(t, expectedValue, mc.ccName, channelID)
verifyWithOrg1(t, sdk, expectedValue, mc.ccName, channelID)
//test multi orgs with SDK config having single config
TestMultiOrgWithSingleOrgConfig(t, exampleCC)
//test Distributed signatures with 2 orgs (1 SDK per org, signature test done by SDK and another one done by OpenSSL)
DistributedSignaturesTests(t, exampleCC)
}
func createAndJoinChannel(t *testing.T, mc *multiorgContext) {
// Get signing identity that is used to sign create channel request
org1AdminUser, err := org1MspClient.GetSigningIdentity(org1AdminUser)
if err != nil {
t.Fatalf("failed to get org1AdminUser, err : %s", err)
}
org2AdminUser, err := org2MspClient.GetSigningIdentity(org2AdminUser)
if err != nil {
t.Fatalf("failed to get org2AdminUser, err : %s", err)
}
createChannel(org1AdminUser, org2AdminUser, mc, t)
// Org1 peers join channel
err = mc.org1ResMgmt.JoinChannel(mc.channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.NoError(t, err, "Org1 peers failed to JoinChannel")
// Org2 peers join channel
err = mc.org2ResMgmt.JoinChannel(mc.channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.NoError(t, err, "Org2 peers failed to JoinChannel")
}
func setupClientContextsAndChannel(t *testing.T, sdk *fabsdk.FabricSDK, mc *multiorgContext) {
// Org1 resource management client (Org1 is default org)
org1RMgmt, err := resmgmt.New(mc.org1AdminClientContext)
require.NoError(t, err, "failed to create org1 resource management client")
mc.org1ResMgmt = org1RMgmt
// Org2 resource management client
org2RMgmt, err := resmgmt.New(mc.org2AdminClientContext)
require.NoError(t, err, "failed to create org2 resource management client")
mc.org2ResMgmt = org2RMgmt
}
func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK, mc *multiorgContext) int {
org1AdminChannelContext := sdk.ChannelContext(mc.channelID, fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1))
org1ChannelClientContext := sdk.ChannelContext(mc.channelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1))
org2ChannelClientContext := sdk.ChannelContext(mc.channelID, fabsdk.WithUser(org2User), fabsdk.WithOrg(org2))
var ccPkg *resource.CCPackage
var err error
// Create chaincode package for example cc
if metadata.CCMode == "lscc" {
ccPkg, err = packager.NewCCPackage(ccPath, integration.GetDeployPath())
if err != nil {
t.Fatal(err)
}
createCC(t, mc, ccPkg, mc.ccName, mc.ccVersion)
} else {
createCCLifecycle(t, mc, mc.ccName, mc.ccVersion, 1, false, mc.channelID, sdk)
}
chClientOrg1User, chClientOrg2User := createOrgsChannelClients(org1ChannelClientContext, t, org2ChannelClientContext)
// Call with a dummy function and expect a fail with multiple errors
verifyErrorFromCC(chClientOrg1User, t, mc.ccName)
// Org1 user queries initial value on both peers
value := queryCC(chClientOrg1User, t, mc.ccName)
initial, _ := strconv.Atoi(string(value))
ledgerClient, err := ledger.New(org1AdminChannelContext)
if err != nil {
t.Fatalf("Failed to create new ledger client: %s", err)
}
// Ledger client will verify blockchain info
ledgerInfoBefore := getBlockchainInfo(ledgerClient, t)
// Org2 user moves funds
transactionID := moveFunds(chClientOrg2User, t, mc.ccName)
// Assert that funds have changed value on org1 peer
verifyValue(t, chClientOrg1User, initial+1, mc.ccName)
// Get latest blockchain info
checkLedgerInfo(ledgerClient, t, ledgerInfoBefore, transactionID)
// Start chaincode upgrade process (install and instantiate new version of exampleCC)
if metadata.CCMode == "lscc" {
upgradeCC(t, mc, ccPkg, mc.ccName, "1")
} else {
createCCLifecycle(t, mc, mc.ccName, "1", 2, true, mc.channelID, sdk)
//sleep 10s for chaincode cache
//time.Sleep(time.Duration(10) * time.Second)
}
// Org2 user moves funds on org2 peer (cc policy fails since both Org1 and Org2 peers should participate)
testCCPolicy(chClientOrg2User, t, mc.ccName)
// Assert that funds have changed value on org1 peer
beforeTxValue, _ := strconv.Atoi(integration.ExampleCCUpgradeB)
expectedValue := beforeTxValue + 1
verifyValue(t, chClientOrg1User, expectedValue, mc.ccName)
return expectedValue
}
func createOrgsChannelClients(org1ChannelClientContext contextAPI.ChannelProvider, t *testing.T, org2ChannelClientContext contextAPI.ChannelProvider) (*channel.Client, *channel.Client) {
// Org1 user connects to 'orgchannel'
chClientOrg1User, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client for Org1 user: %s", err)
}
// Org2 user connects to 'orgchannel'
chClientOrg2User, err := channel.New(org2ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client for Org2 user: %s", err)
}
return chClientOrg1User, chClientOrg2User
}
func checkLedgerInfo(ledgerClient *ledger.Client, t *testing.T, ledgerInfoBefore *fab.BlockchainInfoResponse, transactionID fab.TransactionID) {
ledgerInfoAfter, err := ledgerClient.QueryInfo(ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
if err != nil {
t.Fatalf("QueryInfo return error: %s", err)
}
if ledgerInfoAfter.BCI.Height-ledgerInfoBefore.BCI.Height <= 0 {
t.Fatal("Block size did not increase after transaction")
}
// Test Query Block by Hash - retrieve current block by number
//block, err := ledgerClient.QueryBlock(ledgerInfoAfter.BCI.Height-1, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
// invoke QueryBlock in retryable mode to ensure all peers have responded
block, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
b, e := ledgerClient.QueryBlock(ledgerInfoAfter.BCI.Height-1, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
if e != nil {
// return a retryable code if # of responses is less than the # of targets sent (in this case 2 responses needed)
if strings.Contains(e.Error(), "is less than MinTargets") {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("QueryBlock returned error: %v", e), nil)
}
}
return b, e
},
)
if err != nil {
t.Fatalf("QueryBlock return error: %s", err)
}
if block == nil {
t.Fatal("Block info not available")
}
// Get transaction info
transactionInfo, err := ledgerClient.QueryTransaction(transactionID, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
if err != nil {
t.Fatalf("QueryTransaction return error: %s", err)
}
if transactionInfo.TransactionEnvelope == nil {
t.Fatal("Transaction info missing")
}
}
func createChannel(org1AdminUser msp.SigningIdentity, org2AdminUser msp.SigningIdentity, mc *multiorgContext, t *testing.T) {
// Channel management client is responsible for managing channels (create/update channel)
chMgmtClient, err := resmgmt.New(mc.ordererClientContext)
require.NoError(t, err, "failed to get a new channel management client")
var lastConfigBlock uint64
configQueryClient, err := resmgmt.New(mc.org1AdminClientContext)
require.NoError(t, err, "failed to get a new channel management client")
// create a channel for orgchannel.tx
req := resmgmt.SaveChannelRequest{ChannelID: mc.channelID,
ChannelConfigPath: integration.GetChannelConfigTxPath("orgchannel.tx"),
SigningIdentities: []msp.SigningIdentity{org1AdminUser, org2AdminUser}}
txID, err := chMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.Nil(t, err, "error should be nil for SaveChannel of orgchannel")
require.NotEmpty(t, txID, "transaction ID should be populated")
lastConfigBlock = integration.WaitForOrdererConfigUpdate(t, configQueryClient, mc.channelID, true, lastConfigBlock)
//do the same get ch client and create channel for each anchor peer as well (first for Org1MSP)
chMgmtClient, err = resmgmt.New(mc.org1AdminClientContext)
require.NoError(t, err, "failed to get a new channel management client for org1Admin")
req = resmgmt.SaveChannelRequest{ChannelID: mc.channelID,
ChannelConfigPath: integration.GetChannelConfigTxPath("orgchannelOrg1MSPanchors.tx"),
SigningIdentities: []msp.SigningIdentity{org1AdminUser}}
txID, err = chMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.Nil(t, err, "error should be nil for SaveChannel for anchor peer 1")
require.NotEmpty(t, txID, "transaction ID should be populated for anchor peer 1")
lastConfigBlock = integration.WaitForOrdererConfigUpdate(t, configQueryClient, mc.channelID, false, lastConfigBlock)
// lastly create channel for Org2MSP anchor peer
chMgmtClient, err = resmgmt.New(mc.org2AdminClientContext)
require.NoError(t, err, "failed to get a new channel management client for org2Admin")
req = resmgmt.SaveChannelRequest{ChannelID: mc.channelID,
ChannelConfigPath: integration.GetChannelConfigTxPath("orgchannelOrg2MSPanchors.tx"),
SigningIdentities: []msp.SigningIdentity{org2AdminUser}}
txID, err = chMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.Nil(t, err, "error should be nil for SaveChannel for anchor peer 2")
require.NotEmpty(t, txID, "transaction ID should be populated for anchor peer 2")
integration.WaitForOrdererConfigUpdate(t, configQueryClient, mc.channelID, false, lastConfigBlock)
}
func testCCPolicy(chClientOrg2User *channel.Client, t *testing.T, ccName string) {
_, err := chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithTargets(orgTestPeer1),
channel.WithRetry(retry.DefaultChannelOpts))
if err == nil {
t.Fatal("Should have failed to move funds due to cc policy")
}
// Org2 user moves funds (cc policy ok since we have provided peers for both Orgs)
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
}
func upgradeCC(t *testing.T, mc *multiorgContext, ccPkg *resource.CCPackage, ccName, ccVersion string) {
installCCReq := resmgmt.InstallCCRequest{Name: ccName, Path: ccPath, Version: ccVersion, Package: ccPkg}
// Ensure that Gossip has propagated it's view of local peers before invoking
// install since some peers may be missed if we call InstallCC too early
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
// Install example cc version '1' to Org1 peers
_, err = mc.org1ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
require.Nil(t, err, "error should be nil for InstallCC version '1' or Org1 peers")
// Install example cc version '1' to Org2 peers
_, err = mc.org2ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
require.Nil(t, err, "error should be nil for InstallCC version '1' or Org2 peers")
// Ensure the CC is installed on all peers in both orgs
installed := queryInstalledCC(t, "Org1", mc.org1ResMgmt, ccName, ccVersion, org1Peers)
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org1")
installed = queryInstalledCC(t, "Org2", mc.org2ResMgmt, ccName, ccVersion, org2Peers)
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org2")
// New chaincode policy (both orgs have to approve)
org1Andorg2Policy, err := policydsl.FromString("AND ('Org1MSP.member','Org2MSP.member')")
require.Nil(t, err, "error should be nil for getting cc policy with both orgs to approve")
// Org1 resource manager will instantiate 'example_cc' version 1 on 'orgchannel'
upgradeResp, err := mc.org1ResMgmt.UpgradeCC(mc.channelID, resmgmt.UpgradeCCRequest{Name: ccName, Path: ccPath, Version: ccVersion, Args: integration.ExampleCCUpgradeArgs(), Policy: org1Andorg2Policy})
require.Nil(t, err, "error should be nil for UpgradeCC version '1' on 'orgchannel'")
require.NotEmpty(t, upgradeResp, "transaction response should be populated")
}
func moveFunds(chClientOrgUser *channel.Client, t *testing.T, ccName string) fab.TransactionID {
response, err := chClientOrgUser.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
if response.ChaincodeStatus == 0 {
t.Fatal("Expected ChaincodeStatus")
}
if response.Responses[0].ChaincodeStatus != response.ChaincodeStatus {
t.Fatal("Expected the chaincode status returned by successful Peer Endorsement to be same as Chaincode status for client response")
}
return response.TransactionID
}
func getBlockchainInfo(ledgerClient *ledger.Client, t *testing.T) *fab.BlockchainInfoResponse {
channelCfg, err := ledgerClient.QueryConfig(ledger.WithTargets(orgTestPeer0, orgTestPeer1), ledger.WithMinTargets(2))
if err != nil {
t.Fatalf("QueryConfig return error: %s", err)
}
if len(channelCfg.Orderers()) == 0 {
t.Fatal("Failed to retrieve channel orderers")
}
expectedOrderer := "orderer.example.com"
if !strings.Contains(channelCfg.Orderers()[0], expectedOrderer) {
t.Fatalf("Expecting %s, got %s", expectedOrderer, channelCfg.Orderers()[0])
}
ledgerInfoBefore, err := ledgerClient.QueryInfo(ledger.WithTargets(orgTestPeer0, orgTestPeer1), ledger.WithMinTargets(2), ledger.WithMaxTargets(3))
if err != nil {
t.Fatalf("QueryInfo return error: %s", err)
}
// Test Query Block by Hash - retrieve current block by hash
block, err := ledgerClient.QueryBlockByHash(ledgerInfoBefore.BCI.CurrentBlockHash, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
if err != nil {
t.Fatalf("QueryBlockByHash return error: %s", err)
}
if block == nil {
t.Fatal("Block info not available")
}
return ledgerInfoBefore
}
func queryCC(chClientOrg1User *channel.Client, t *testing.T, ccName string) []byte {
response, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
require.NoError(t, err, "Failed to query funds")
require.NotZero(t, response.ChaincodeStatus, "Expected ChaincodeStatus")
require.Equal(t, response.ChaincodeStatus, response.Responses[0].ChaincodeStatus, "Expected the chaincode status returned by successful Peer Endorsement to be same as Chaincode status for client response")
return response.Payload
}
func verifyErrorFromCC(chClientOrg1User *channel.Client, t *testing.T, ccName string) {
r, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
t.Logf("verifyErrorFromCC err: %s ***** responses: %v", err, r)
require.Error(t, err, "Should have failed with dummy function")
s, ok := status.FromError(err)
t.Logf("verifyErrorFromCC status.FromError s: %s, ok: %t", s, ok)
require.True(t, ok, "expected status error")
require.Equal(t, int32(status.MultipleErrors), s.Code)
for _, err := range err.(multi.Errors) {
s, ok := status.FromError(err)
require.True(t, ok, "expected status error")
require.EqualValues(t, int32(500), s.Code)
require.Equal(t, status.ChaincodeStatus, s.Group)
}
}
func queryInstalledCC(t *testing.T, orgID string, resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fab.Peer) bool {
installed, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
ok := isCCInstalled(t, orgID, resMgmt, ccName, ccVersion, peers)
if !ok {
return &ok, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Chaincode [%s:%s] is not installed on all peers in Org1", ccName, ccVersion), nil)
}
return &ok, nil
},
)
require.NoErrorf(t, err, "Got error checking if chaincode was installed")
return *(installed).(*bool)
}
func isCCInstalled(t *testing.T, orgID string, resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fab.Peer) bool {
t.Logf("Querying [%s] peers to see if chaincode [%s:%s] was installed", orgID, ccName, ccVersion)
installedOnAllPeers := true
for _, peer := range peers {
t.Logf("Querying [%s] ...", peer.URL())
resp, err := resMgmt.QueryInstalledChaincodes(resmgmt.WithTargets(peer))
require.NoErrorf(t, err, "QueryInstalledChaincodes for peer [%s] failed", peer.URL())
found := false
for _, ccInfo := range resp.Chaincodes {
t.Logf("... found chaincode [%s:%s]", ccInfo.Name, ccInfo.Version)
if ccInfo.Name == ccName && ccInfo.Version == ccVersion {
found = true
break
}
}
if !found {
t.Logf("... chaincode [%s:%s] is not installed on peer [%s]", ccName, ccVersion, peer.URL())
installedOnAllPeers = false
}
}
return installedOnAllPeers
}
func queryInstantiatedCC(t *testing.T, orgID string, resMgmt *resmgmt.Client, channelID, ccName, ccVersion string, peers []fab.Peer) bool {
require.Truef(t, len(peers) > 0, "Expecting one or more peers")
t.Logf("Querying [%s] peers to see if chaincode [%s] was instantiated on channel [%s]", orgID, ccName, channelID)
instantiated, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
ok := isCCInstantiated(t, resMgmt, channelID, ccName, ccVersion, peers)
if !ok {
return &ok, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Did NOT find instantiated chaincode [%s:%s] on one or more peers in [%s].", ccName, ccVersion, orgID), nil)
}
return &ok, nil
},
)
require.NoErrorf(t, err, "Got error checking if chaincode was instantiated")
return *(instantiated).(*bool)
}
func isCCInstantiated(t *testing.T, resMgmt *resmgmt.Client, channelID, ccName, ccVersion string, peers []fab.Peer) bool {
installedOnAllPeers := true
for _, peer := range peers {
t.Logf("Querying peer [%s] for instantiated chaincode [%s:%s]...", peer.URL(), ccName, ccVersion)
chaincodeQueryResponse, err := resMgmt.QueryInstantiatedChaincodes(channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithTargets(peer))
require.NoError(t, err, "QueryInstantiatedChaincodes return error")
t.Logf("Found %d instantiated chaincodes on peer [%s]:", len(chaincodeQueryResponse.Chaincodes), peer.URL())
found := false
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
t.Logf("Found instantiated chaincode Name: [%s], Version: [%s], Path: [%s] on peer [%s]", chaincode.Name, chaincode.Version, chaincode.Path, peer.URL())
if chaincode.Name == ccName && chaincode.Version == ccVersion {
found = true
break
}
}
if !found {
t.Logf("... chaincode [%s:%s] is not instantiated on peer [%s]", ccName, ccVersion, peer.URL())
installedOnAllPeers = false
}
}
return installedOnAllPeers
}
func createCC(t *testing.T, mc *multiorgContext, ccPkg *resource.CCPackage, ccName, ccVersion string) {
installCCReq := resmgmt.InstallCCRequest{Name: ccName, Path: ccPath, Version: ccVersion, Package: ccPkg}
// Ensure that Gossip has propagated it's view of local peers before invoking
// install since some peers may be missed if we call InstallCC too early
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
// Install example cc to Org1 peers
_, err = mc.org1ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
require.NoError(t, err, "InstallCC for Org1 failed")
// Install example cc to Org2 peers
_, err = mc.org2ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
require.NoError(t, err, "InstallCC for Org2 failed")
// Ensure the CC is installed on all peers in both orgs
installed := queryInstalledCC(t, "Org1", mc.org1ResMgmt, ccName, ccVersion, org1Peers)
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org1")
installed = queryInstalledCC(t, "Org2", mc.org2ResMgmt, ccName, ccVersion, org2Peers)
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org2")
instantiateCC(t, mc.org1ResMgmt, ccName, ccVersion, mc.channelID)
// Ensure the CC is instantiated on all peers in both orgs
found := queryInstantiatedCC(t, "Org1", mc.org1ResMgmt, mc.channelID, ccName, ccVersion, org1Peers)
require.True(t, found, "Failed to find instantiated chaincode [%s:%s] in at least one peer in Org1 on channel [%s]", ccName, ccVersion, mc.channelID)
found = queryInstantiatedCC(t, "Org2", mc.org2ResMgmt, mc.channelID, ccName, ccVersion, org2Peers)
require.True(t, found, "Failed to find instantiated chaincode [%s:%s] in at least one peer in Org2 on channel [%s]", ccName, ccVersion, mc.channelID)
}
func instantiateCC(t *testing.T, resMgmt *resmgmt.Client, ccName, ccVersion string, channelID string) {
instantiateResp, err := integration.InstantiateChaincode(resMgmt, channelID, ccName, ccPath, ccVersion, "AND ('Org1MSP.member','Org2MSP.member')", integration.ExampleCCInitArgs())
require.NoError(t, err)
require.NotEmpty(t, instantiateResp, "transaction response should be populated for instantateCC")
}
func testWithOrg2(t *testing.T, expectedValue int, ccName, channelID string) int {
// Create SDK setup for channel client with dynamic selection
sdk, err := fabsdk.New(integration.ConfigBackend)
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()
//prepare contexts
org2ChannelClientContext := sdk.ChannelContext(channelID, fabsdk.WithUser(org2User), fabsdk.WithOrg(org2))
// Create new client that will use dynamic selection
chClientOrg2User, err := channel.New(org2ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client for Org2 user: %s", err)
}
// Org2 user moves funds (dynamic selection will inspect chaincode policy to determine endorsers)
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
expectedValue++
return expectedValue
}
func verifyWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK, expectedValue int, ccName string, channelID string) {
//prepare context
org1ChannelClientContext := sdk.ChannelContext(channelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1))
// Org1 user connects to 'orgchannel'
chClientOrg1User, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client for Org1 user: %s", err)
}
verifyValue(t, chClientOrg1User, expectedValue, ccName)
}
func verifyValue(t *testing.T, chClient *channel.Client, expectedValue int, ccName string) {
req := channel.Request{
ChaincodeID: ccName,
Fcn: "invoke",
Args: integration.ExampleCCDefaultQueryArgs(),
}
_, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp, err := chClient.Query(req, channel.WithTargets(orgTestPeer0), channel.WithRetry(retry.DefaultChannelOpts))
require.NoError(t, err, "query funds failed")
// Verify that transaction changed block state
actualValue, _ := strconv.Atoi(string(resp.Payload))
if expectedValue != actualValue {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("ledger value didn't match expectation [%d, %d]", expectedValue, actualValue), nil)
}
return &actualValue, nil
},
)
require.NoError(t, err, "Org2 'move funds' transaction result was not propagated to Org1")
}
func loadOrgPeers(t *testing.T, ctxProvider contextAPI.ClientProvider) {
ctx, err := ctxProvider()
if err != nil {
t.Fatalf("context creation failed: %s", err)
}
org1Peers, ok := ctx.EndpointConfig().PeersConfig(org1)
assert.True(t, ok)
org2Peers, ok := ctx.EndpointConfig().PeersConfig(org2)
assert.True(t, ok)
orgTestPeer0, err = ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: org1Peers[0]})
if err != nil {
t.Fatal(err)
}
orgTestPeer1, err = ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: org2Peers[0]})
if err != nil {
t.Fatal(err)
}
}
// createCCLifecycle package cc, install cc, get installed cc package, query installed cc
// approve cc, query approve cc, check commit readiness, commit cc, query committed cc
func createCCLifecycle(t *testing.T, mc *multiorgContext, ccName, ccVersion string, sequence int64, upgrade bool, channelID string, sdk *fabsdk.FabricSDK) {
// Package cc
label, ccPkg := packageCC(t, ccName, ccVersion)
packageID := lcpackager.ComputePackageID(label, ccPkg)
// Install cc
installCC(t, label, ccPkg, mc)
// Get installed cc package
getInstalledCCPackage(t, packageID, ccPkg, mc)
// Query installed cc
queryInstalled(t, label, packageID, mc)
// Approve cc
approveCC(t, packageID, ccName, ccVersion, sequence, channelID, mc)
// Query approve cc
queryApprovedCC(t, ccName, sequence, channelID, mc)
// Check commit readiness
checkCCCommitReadiness(t, ccName, ccVersion, sequence, channelID, mc)
// Commit cc
commitCC(t, ccName, ccVersion, sequence, channelID, mc)
// Query committed cc
queryCommittedCC(t, ccName, channelID, sequence, mc)
// Init cc
initCC(t, ccName, upgrade, channelID, sdk)
}
func packageCC(t *testing.T, ccName, ccVersion string) (string, []byte) {
label := ccName + "_" + ccVersion
desc := &lcpackager.Descriptor{
Path: integration.GetLcDeployPath(),
Type: pb.ChaincodeSpec_GOLANG,
Label: label,
}
ccPkg, err := lcpackager.NewCCPackage(desc)
if err != nil {
t.Fatal(err)
}
return desc.Label, ccPkg
}
func installCC(t *testing.T, label string, ccPkg []byte, mc *multiorgContext) {
installCCReq := resmgmt.LifecycleInstallCCRequest{
Label: label,
Package: ccPkg,
}
packageID := lcpackager.ComputePackageID(installCCReq.Label, installCCReq.Package)
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
if !checkInstalled(t, packageID, org1Peers[0], mc.org1ResMgmt) {
resp1, err := mc.org1ResMgmt.LifecycleInstallCC(installCCReq, resmgmt.WithTargets(org1Peers...), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.Equal(t, packageID, resp1[0].PackageID)
}
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
if !checkInstalled(t, packageID, org2Peers[0], mc.org2ResMgmt) {
resp2, err := mc.org2ResMgmt.LifecycleInstallCC(installCCReq, resmgmt.WithTargets(org2Peers...), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.Equal(t, packageID, resp2[0].PackageID)
}
}
func getInstalledCCPackage(t *testing.T, packageID string, ccPkg []byte, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 1)
require.NoError(t, err)
resp1, err := mc.org1ResMgmt.LifecycleGetInstalledCCPackage(packageID, resmgmt.WithTargets([]fab.Peer{org1Peers[0]}...))
if err != nil {
t.Fatal(err)
}
require.Equal(t, ccPkg, resp1)
}
func queryInstalled(t *testing.T, label string, packageID string, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 1)
require.NoError(t, err)
resp1, err := mc.org1ResMgmt.LifecycleQueryInstalledCC(resmgmt.WithTargets([]fab.Peer{org1Peers[0]}...))
if err != nil {
t.Fatal(err)
}
packageID1 := ""
for _, t := range resp1 {
if t.PackageID == packageID {
packageID1 = t.PackageID
}
}
require.Equal(t, packageID, packageID1)
}
func checkInstalled(t *testing.T, packageID string, peer fab.Peer, client *resmgmt.Client) bool {
flag := false
resp1, err := client.LifecycleQueryInstalledCC(resmgmt.WithTargets(peer))
if err != nil {
t.Fatal(err)
}
for _, t := range resp1 {
if t.PackageID == packageID {
flag = true
}
}
return flag
}
func approveCC(t *testing.T, packageID string, ccName, ccVersion string, sequence int64, channelID string, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
ccPolicy := policydsl.SignedByNOutOfGivenRole(2, mb.MSPRole_MEMBER, []string{"Org1MSP", "Org2MSP"})
approveCCReq := resmgmt.LifecycleApproveCCRequest{
Name: ccName,
Version: ccVersion,
PackageID: packageID,
Sequence: sequence,
EndorsementPlugin: "escc",
ValidationPlugin: "vscc",
SignaturePolicy: ccPolicy,
InitRequired: true,
}
txnID1, err := mc.org1ResMgmt.LifecycleApproveCC(channelID, approveCCReq, resmgmt.WithTargets(org1Peers...), resmgmt.WithOrdererEndpoint("orderer.example.com"), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.NotEmpty(t, txnID1)
txnID2, err := mc.org2ResMgmt.LifecycleApproveCC(channelID, approveCCReq, resmgmt.WithTargets(org2Peers...), resmgmt.WithOrdererEndpoint("orderer.example.com"), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.NotEmpty(t, txnID2)
}
func queryApprovedCC(t *testing.T, ccName string, sequence int64, channelID string, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
// Query approve cc
queryApprovedCCReq := resmgmt.LifecycleQueryApprovedCCRequest{
Name: ccName,
Sequence: sequence,
}
for _, p := range org1Peers {
resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp1, err := mc.org1ResMgmt.LifecycleQueryApprovedCC(channelID, queryApprovedCCReq, resmgmt.WithTargets(p))
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryApprovedCC returned error: %v", err), nil)
}
return resp1, err
},
)
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp)
}
for _, p := range org2Peers {
resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp1, err := mc.org2ResMgmt.LifecycleQueryApprovedCC(channelID, queryApprovedCCReq, resmgmt.WithTargets(p), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryApprovedCC returned error: %v", err), nil)
}
return resp1, err
},
)
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp)
}
}
func checkCCCommitReadiness(t *testing.T, ccName, ccVersion string, sequence int64, channelID string, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
ccPolicy := policydsl.SignedByNOutOfGivenRole(2, mb.MSPRole_MEMBER, []string{"Org1MSP", "Org2MSP"})
req := resmgmt.LifecycleCheckCCCommitReadinessRequest{
Name: ccName,
Version: ccVersion,
EndorsementPlugin: "escc",
ValidationPlugin: "vscc",
SignaturePolicy: ccPolicy,
Sequence: sequence,
InitRequired: true,
}
/*resp1, err := mc.org1ResMgmt.LifecycleCheckCCCommitReadiness(channelID, req, resmgmt.WithTargets([]fab.Peer{org1Peers[0]}...), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp1)*/
for _, p := range org1Peers {
resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp1, err := mc.org1ResMgmt.LifecycleCheckCCCommitReadiness(channelID, req, resmgmt.WithTargets(p))
fmt.Printf("LifecycleCheckCCCommitReadiness cc = %v, = %v\n", ccName, resp1)
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned error: %v", err), nil)
}
flag := true
for _, r := range resp1.Approvals {
flag = flag && r
}
if !flag {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned : %v", resp1), nil)
}
return resp1, err
},
)
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp)
}
for _, p := range org2Peers {
resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp1, err := mc.org2ResMgmt.LifecycleCheckCCCommitReadiness(channelID, req, resmgmt.WithTargets(p))
fmt.Printf("LifecycleCheckCCCommitReadiness cc = %v, = %v\n", ccName, resp1)
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned error: %v", err), nil)
}
flag := true
for _, r := range resp1.Approvals {
flag = flag && r
}
if !flag {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned : %v", resp1), nil)
}
return resp1, err
},
)
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp)
}
}
func commitCC(t *testing.T, ccName, ccVersion string, sequence int64, channelID string, mc *multiorgContext) {
ccPolicy := policydsl.SignedByNOutOfGivenRole(2, mb.MSPRole_MEMBER, []string{"Org1MSP", "Org2MSP"})
req := resmgmt.LifecycleCommitCCRequest{
Name: ccName,
Version: ccVersion,
Sequence: sequence,
EndorsementPlugin: "escc",
ValidationPlugin: "vscc",
SignaturePolicy: ccPolicy,
InitRequired: true,
}
txnID, err := mc.org1ResMgmt.LifecycleCommitCC(channelID, req, resmgmt.WithOrdererEndpoint("orderer.example.com"), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
require.NotEmpty(t, txnID)
}
func queryCommittedCC(t *testing.T, ccName string, channelID string, sequence int64, mc *multiorgContext) {
org1Peers, err := integration.DiscoverLocalPeers(mc.org1AdminClientContext, 2)
require.NoError(t, err)
org2Peers, err := integration.DiscoverLocalPeers(mc.org2AdminClientContext, 2)
require.NoError(t, err)
req := resmgmt.LifecycleQueryCommittedCCRequest{
Name: ccName,
}
/*resp1, err := mc.org1ResMgmt.LifecycleQueryCommittedCC(channelID, req, resmgmt.WithTargets(org1Peers[0]), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}*/
for _, p := range org1Peers {
resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp1, err := mc.org1ResMgmt.LifecycleQueryCommittedCC(channelID, req, resmgmt.WithTargets(p))
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryCommittedCC returned error: %v", err), nil)
}
flag := false
for _, r := range resp1 {