Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 63d2604

Browse files
committed
[FAB-10551] Fix CI error: could not find CC
The error 'could not find chaincode with name exampleCC' happens intermittently. There are two causes that this patch fixes: 1) When using dynamic local discovery, the non-anchor peer does not show up in the list of peers right away, so this patch will wait until all peers are there before attempting to install chaincode 2) After instatiating the CC, the non-anchor peer does not get the instantiate block right away, so any attempt to invoke the CC on that peer will result in the 'could not find CC' error. This patch will query for the instatiated chaincode until all peers have it, before attempting to invoke. Change-Id: I920ed3b7de8a3e985f8c5fed2ba96dc317fe2d39 Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
1 parent 37c1b5a commit 63d2604

File tree

2 files changed

+82
-75
lines changed

2 files changed

+82
-75
lines changed

test/integration/orgs/multiple_orgs_minconfig_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func TestOrgsEndToEndWithBootstrapConfigs(t *testing.T) {
6969
}
7070

7171
func testDynamicDiscovery(t *testing.T, sdk *fabsdk.FabricSDK, mc *multiorgContext) {
72-
peersList := discoverLocalPeers(t, sdk, mc.org1AdminClientContext, 2)
72+
peersList := discoverLocalPeers(t, mc.org1AdminClientContext, 2)
7373
assert.Equal(t, 2, len(peersList), "Expected exactly 2 peers as per %s's channel and %s's org configs", channelID, org1)
74-
peersList = discoverLocalPeers(t, sdk, mc.org2AdminClientContext, 1)
74+
peersList = discoverLocalPeers(t, mc.org2AdminClientContext, 1)
7575
assert.Equal(t, 1, len(peersList), "Expected exactly 1 peer as per %s's channel and %s's org configs", channelID, org2)
7676

7777
// example discovering the peers from the bootstap peer

test/integration/orgs/multiple_orgs_test.go

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,8 @@ func TestOrgsEndToEnd(t *testing.T) {
141141
ccVersion: "0",
142142
}
143143

144-
peersList := discoverLocalPeers(t, sdk, mc.org1AdminClientContext, 2)
145-
assert.Equal(t, 2, len(peersList), "Expected exactly 2 peers for MSP [%s]", org1)
146-
peersList = discoverLocalPeers(t, sdk, mc.org2AdminClientContext, 1)
147-
assert.Equal(t, 1, len(peersList), "Expected exactly 1 peer for MSP [%s]", org2)
144+
discoverLocalPeers(t, mc.org1AdminClientContext, 2)
145+
discoverLocalPeers(t, mc.org2AdminClientContext, 1)
148146

149147
expectedValue := testWithOrg1(t, sdk, &mc)
150148
expectedValue = testWithOrg2(t, expectedValue, mc.ccName)
@@ -189,7 +187,7 @@ func setupClientContextsAndChannel(t *testing.T, sdk *fabsdk.FabricSDK, mc *mult
189187
}
190188
}
191189

192-
func discoverLocalPeers(t *testing.T, sdk *fabsdk.FabricSDK, ctxProvider contextAPI.ClientProvider, expected int) []fab.Peer {
190+
func discoverLocalPeers(t *testing.T, ctxProvider contextAPI.ClientProvider, expected int) []fab.Peer {
193191
ctx, err := ctxProvider()
194192
require.NoError(t, err, "Error creating context")
195193

@@ -213,6 +211,7 @@ func discoverLocalPeers(t *testing.T, sdk *fabsdk.FabricSDK, ctxProvider context
213211
// wait some time to allow the gossip to propagate the peers discovery
214212
time.Sleep(3 * time.Second)
215213
}
214+
require.Equalf(t, expected, len(peers), "Did not get the required number of peers")
216215
return peers
217216
}
218217

@@ -453,32 +452,88 @@ func verifyErrorFromCC(chClientOrg1User *channel.Client, t *testing.T, ccName st
453452
}
454453
}
455454

456-
func queryInstantiatedCC(t *testing.T, resMgmt *resmgmt.Client, channelID, ccName string) bool {
457-
found := false
458-
for i := 0; i < 5; i++ {
459-
// Verify that example CC is instantiated on Org1 peer
460-
chaincodeQueryResponse, err := resMgmt.QueryInstantiatedChaincodes(channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
461-
require.NoError(t, err, "QueryInstantiatedChaincodes return error")
455+
func queryInstalledCC(t *testing.T, orgID string, resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fab.Peer) bool {
456+
for i := 0; i < 10; i++ {
457+
if isCCInstalled(t, orgID, resMgmt, ccName, ccVersion, peers) {
458+
t.Logf("Chaincode [%s:%s] is installed on all peers in Org1", ccName, ccVersion)
459+
return true
460+
}
461+
t.Logf("Chaincode [%s:%s] is NOT installed on all peers in Org1. Trying again in 2 seconds...", ccName, ccVersion)
462+
time.Sleep(2 * time.Second)
463+
}
464+
return false
465+
}
462466

463-
t.Logf("Found %d instantiated chaincodes:", len(chaincodeQueryResponse.Chaincodes))
464-
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
465-
t.Logf("Found instantiated chaincode Name: [%s], Version: [%s], Path: [%s]", chaincode.Name, chaincode.Version, chaincode.Path)
466-
if chaincode.Name == ccName {
467+
func isCCInstalled(t *testing.T, orgID string, resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fab.Peer) bool {
468+
t.Logf("Querying [%s] peers to see if chaincode [%s:%s] was installed", orgID, ccName, ccVersion)
469+
installedOnAllPeers := true
470+
for _, peer := range peers {
471+
t.Logf("Querying [%s] ...", peer.URL())
472+
resp, err := resMgmt.QueryInstalledChaincodes(resmgmt.WithTargets(peer))
473+
require.NoErrorf(t, err, "QueryInstalledChaincodes for peer [%s] failed", peer.URL())
474+
475+
found := false
476+
for _, ccInfo := range resp.Chaincodes {
477+
t.Logf("... found chaincode [%s:%s]", ccInfo.Name, ccInfo.Version)
478+
if ccInfo.Name == ccName && ccInfo.Version == ccVersion {
467479
found = true
468480
break
469481
}
470482
}
471-
if found {
483+
if !found {
484+
t.Logf("... chaincode [%s:%s] is not installed on peer [%s]", ccName, ccVersion, peer.URL())
485+
installedOnAllPeers = false
486+
}
487+
}
488+
return installedOnAllPeers
489+
}
490+
491+
func queryInstantiatedCC(t *testing.T, orgID string, resMgmt *resmgmt.Client, channelID, ccName, ccVersion string, peers []fab.Peer) bool {
492+
require.Truef(t, len(peers) > 0, "Expecting one or more peers")
493+
494+
t.Logf("Querying [%s] peers to see if chaincode [%s] was instantiated on channel [%s]", orgID, ccName, channelID)
495+
for i := 0; i < 10; i++ {
496+
if isCCInstantiated(t, resMgmt, channelID, ccName, ccVersion, peers) {
472497
return true
473498
}
474-
time.Sleep(5 * time.Second)
499+
t.Logf("Did NOT find instantiated chaincode [%s:%s] on one or more peers in [%s]. Trying again in 2 seconds...", ccName, ccVersion, orgID)
500+
time.Sleep(2 * time.Second)
475501
}
476502
return false
477503
}
478504

505+
func isCCInstantiated(t *testing.T, resMgmt *resmgmt.Client, channelID, ccName, ccVersion string, peers []fab.Peer) bool {
506+
installedOnAllPeers := true
507+
for _, peer := range peers {
508+
t.Logf("Querying peer [%s] for instantiated chaincode [%s:%s]...", peer.URL(), ccName, ccVersion)
509+
chaincodeQueryResponse, err := resMgmt.QueryInstantiatedChaincodes(channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithTargets(peer))
510+
require.NoError(t, err, "QueryInstantiatedChaincodes return error")
511+
512+
t.Logf("Found %d instantiated chaincodes on peer [%s]:", len(chaincodeQueryResponse.Chaincodes), peer.URL())
513+
found := false
514+
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
515+
t.Logf("Found instantiated chaincode Name: [%s], Version: [%s], Path: [%s] on peer [%s]", chaincode.Name, chaincode.Version, chaincode.Path, peer.URL())
516+
if chaincode.Name == ccName && chaincode.Version == ccVersion {
517+
found = true
518+
break
519+
}
520+
}
521+
if !found {
522+
t.Logf("... chaincode [%s:%s] is not instantiated on peer [%s]", ccName, ccVersion, peer.URL())
523+
installedOnAllPeers = false
524+
}
525+
}
526+
return installedOnAllPeers
527+
}
528+
479529
func createCC(t *testing.T, mc *multiorgContext, ccPkg *resource.CCPackage, ccName, ccVersion string) {
480530
installCCReq := resmgmt.InstallCCRequest{Name: ccName, Path: "github.com/example_cc", Version: ccVersion, Package: ccPkg}
481531

532+
// Ensure that Gossip has propagated it's view of local peers before invoking
533+
// install since some peers may be missed if we call InstallCC too early
534+
org1Peers := discoverLocalPeers(t, mc.org1AdminClientContext, 2)
535+
org2Peers := discoverLocalPeers(t, mc.org2AdminClientContext, 1)
536+
482537
// Install example cc to Org1 peers
483538
_, err := mc.org1ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
484539
require.NoError(t, err, "InstallCC for Org1 failed")
@@ -487,69 +542,21 @@ func createCC(t *testing.T, mc *multiorgContext, ccPkg *resource.CCPackage, ccNa
487542
_, err = mc.org2ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
488543
require.NoError(t, err, "InstallCC for Org2 failed")
489544

490-
isInstalled := func(ctxProvider contextAPI.ClientProvider, resMgmt *resmgmt.Client) bool {
491-
ctx, err := ctxProvider()
492-
require.NoErrorf(t, err, "Getting client context for [%s] failed", ctx.Identifier().MSPID)
493-
discoveryProvider := ctx.LocalDiscoveryProvider()
494-
discovery, err := discoveryProvider.CreateLocalDiscoveryService(ctx.Identifier().MSPID)
495-
require.NoErrorf(t, err, "Error creating local discovery service for [%s]", ctx.Identifier().MSPID)
496-
peers, err := discovery.GetPeers()
497-
require.NoErrorf(t, err, "Getting local peers for [%s] failed", ctx.Identifier().MSPID)
498-
499-
t.Logf("Querying [%s] peers to see if chaincode [%s:%s] was installed", ctx.Identifier().MSPID, ccName, ccVersion)
500-
installedOnAllPeers := true
501-
for _, peer := range peers {
502-
t.Logf("Querying [%s] ...", peer.URL())
503-
resp, err := resMgmt.QueryInstalledChaincodes(resmgmt.WithTargets(peer))
504-
require.NoErrorf(t, err, "QueryInstalledChaincodes for peer [%s] failed", peer.URL())
505-
506-
found := false
507-
for _, ccInfo := range resp.Chaincodes {
508-
t.Logf("... found chaincode [%s:%s]", ccInfo.Name, ccInfo.Version)
509-
if ccInfo.Name == ccName && ccInfo.Version == ccVersion {
510-
found = true
511-
break
512-
}
513-
}
514-
if !found {
515-
t.Logf("... chaincode [%s:%s] is not installed on peer [%s]", ccName, ccVersion, peer.URL())
516-
installedOnAllPeers = false
517-
}
518-
}
519-
return installedOnAllPeers
520-
}
521-
522-
installed := false
523-
for i := 0; i < 10; i++ {
524-
if isInstalled(mc.org1AdminClientContext, mc.org1ResMgmt) {
525-
t.Logf("Chaincode [%s:%s] is installed on all peers in Org1", ccName, ccVersion)
526-
installed = true
527-
break
528-
}
529-
t.Logf("Chaincode [%s:%s] is NOT installed on all peers in Org1. Trying again in 2 seconds...", ccName, ccVersion)
530-
time.Sleep(2 * time.Second)
531-
}
545+
// Ensure the CC is installed on all peers in both orgs
546+
installed := queryInstalledCC(t, "Org1", mc.org1ResMgmt, ccName, ccVersion, org1Peers)
532547
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org1")
533548

534-
installed = false
535-
for i := 0; i < 10; i++ {
536-
if isInstalled(mc.org2AdminClientContext, mc.org2ResMgmt) {
537-
t.Logf("Chaincode [%s:%s] is installed on all peers in Org2", ccName, ccVersion)
538-
installed = true
539-
break
540-
}
541-
t.Logf("Chaincode [%s:%s] is NOT installed on all peers in Org2. Trying again in 2 seconds...", ccName, ccVersion)
542-
time.Sleep(2 * time.Second)
543-
}
549+
installed = queryInstalledCC(t, "Org2", mc.org2ResMgmt, ccName, ccVersion, org2Peers)
544550
require.Truef(t, installed, "Expecting chaincode [%s:%s] to be installed on all peers in Org2")
545551

546552
instantiateCC(t, mc.org1ResMgmt, ccName, ccVersion)
547553

548-
found := queryInstantiatedCC(t, mc.org1ResMgmt, channelID, ccName)
549-
require.True(t, found, "QueryInstantiatedChaincodes failed to find instantiated '%s' chaincode", ccName)
554+
// Ensure the CC is instantiated on all peers in both orgs
555+
found := queryInstantiatedCC(t, "Org1", mc.org1ResMgmt, channelID, ccName, ccVersion, org1Peers)
556+
require.True(t, found, "Failed to find instantiated chaincode [%s:%s] in at least one peer in Org1 on channel [%s]", ccName, ccVersion, channelID)
550557

551-
found = queryInstantiatedCC(t, mc.org2ResMgmt, channelID, ccName)
552-
require.True(t, found, "QueryInstantiatedChaincodes failed to find instantiated '%s' chaincode", ccName)
558+
found = queryInstantiatedCC(t, "Org2", mc.org2ResMgmt, channelID, ccName, ccVersion, org2Peers)
559+
require.True(t, found, "Failed to find instantiated chaincode [%s:%s] in at least one peer in Org2 on channel [%s]", ccName, ccVersion, channelID)
553560
}
554561

555562
func instantiateCC(t *testing.T, resMgmt *resmgmt.Client, ccName, ccVersion string) {

0 commit comments

Comments
 (0)