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

Commit 1d43fc8

Browse files
committed
[FAB-8321] Resource Mgmt: Query Instantiated CCs
Change-Id: I61375d3fc1f4aa1edc9fb595a83748171ebcecca Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
1 parent 6b39e3a commit 1d43fc8

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

pkg/client/resmgmt/opts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ func WithTargets(targets ...fab.Peer) RequestOption {
2020
}
2121
}
2222

23+
//WithTarget encapsulates fab.Peer target to RequestOption
24+
func WithTarget(target fab.Peer) RequestOption {
25+
return func(opts *Opts) error {
26+
opts.Targets = []fab.Peer{target}
27+
return nil
28+
}
29+
}
30+
2331
//WithTargetFilter encapsulates resmgmtclient TargetFilter targets to resmgmtclient RequestOption
2432
func WithTargetFilter(targetFilter TargetFilter) RequestOption {
2533
return func(opts *Opts) error {

pkg/client/resmgmt/resmgmt.go

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ package resmgmt
99

1010
import (
1111
"io/ioutil"
12+
"math/rand"
1213
"time"
1314

1415
config "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
1516
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
17+
"github.com/hyperledger/fabric-sdk-go/pkg/fab/channel"
1618
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
1719

1820
"github.com/hyperledger/fabric-sdk-go/pkg/context"
@@ -413,6 +415,54 @@ func (rc *Client) QueryInstalledChaincodes(proposalProcessor fab.ProposalProcess
413415
return rc.resource.QueryInstalledChaincodes(proposalProcessor)
414416
}
415417

418+
// QueryInstantiatedChaincodes queries the instantiated chaincodes on a peer for specific channel.
419+
// Valid option is WithTarget. If not specified it will query any peer on this channel
420+
func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...RequestOption) (*pb.ChaincodeQueryResponse, error) {
421+
422+
opts, err := rc.prepareRequestOpts(options...)
423+
if err != nil {
424+
return nil, err
425+
}
426+
427+
ctx := &fabContext{
428+
ProviderContext: rc.provider,
429+
IdentityContext: rc.identity,
430+
}
431+
432+
var target fab.ProposalProcessor
433+
if len(opts.Targets) >= 1 {
434+
target = opts.Targets[0]
435+
} else {
436+
// discover peers on this channel
437+
discovery, err := rc.discoveryProvider.NewDiscoveryService(channelID)
438+
if err != nil {
439+
return nil, errors.WithMessage(err, "failed to create channel discovery service")
440+
}
441+
// default filter will be applied (if any)
442+
targets, err := rc.getDefaultTargets(discovery)
443+
if err != nil {
444+
return nil, errors.WithMessage(err, "failed to get default target for query instantiated chaincodes")
445+
}
446+
447+
// select random channel peer
448+
r := rand.New(rand.NewSource(time.Now().Unix()))
449+
randomNumber := r.Intn(len(targets))
450+
target = targets[randomNumber]
451+
}
452+
453+
l, err := channel.NewLedger(ctx, channelID)
454+
if err != nil {
455+
return nil, err
456+
}
457+
458+
responses, err := l.QueryInstantiatedChaincodes([]fab.ProposalProcessor{target})
459+
if err != nil {
460+
return nil, err
461+
}
462+
463+
return responses[0], nil
464+
}
465+
416466
// QueryChannels queries the names of all the channels that a peer has joined.
417467
// Returns the details of all channels that peer has joined.
418468
func (rc *Client) QueryChannels(proposalProcessor fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
@@ -579,7 +629,7 @@ func peersToTxnProcessors(peers []fab.Peer) []fab.ProposalProcessor {
579629
// SaveChannel creates or updates channel
580630
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) error {
581631

582-
opts, err := rc.prepareSaveChannelOpts(options...)
632+
opts, err := rc.prepareRequestOpts(options...)
583633
if err != nil {
584634
return err
585635
}
@@ -658,14 +708,14 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
658708
return nil
659709
}
660710

661-
//prepareSaveChannelOpts Reads chmgmt.Opts from chmgmt.Option array
662-
func (rc *Client) prepareSaveChannelOpts(options ...RequestOption) (Opts, error) {
663-
saveChannelOpts := Opts{}
711+
//prepareRequestOpts prepares rrequest options
712+
func (rc *Client) prepareRequestOpts(options ...RequestOption) (Opts, error) {
713+
opts := Opts{}
664714
for _, option := range options {
665-
err := option(&saveChannelOpts)
715+
err := option(&opts)
666716
if err != nil {
667-
return saveChannelOpts, errors.WithMessage(err, "Failed to read save channel opts")
717+
return opts, errors.WithMessage(err, "Failed to read opts")
668718
}
669719
}
670-
return saveChannelOpts, nil
720+
return opts, nil
671721
}

test/integration/orgs/multiple_orgs_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
134134
// Load specific targets for move funds test
135135
loadOrgPeers(t, sdk)
136136

137+
// Verify that example CC is instantiated on Org1 peer
138+
chaincodeQueryResponse, err := org1ResMgmt.QueryInstantiatedChaincodes("orgchannel")
139+
if err != nil {
140+
t.Fatalf("QueryInstantiatedChaincodes return error: %v", err)
141+
}
142+
143+
found := false
144+
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
145+
if chaincode.Name == "exampleCC" {
146+
found = true
147+
}
148+
}
149+
150+
if !found {
151+
t.Fatalf("QueryInstantiatedChaincodes failed to find instantiated exampleCC chaincode")
152+
}
153+
137154
// Org1 user connects to 'orgchannel'
138155
chClientOrg1User, err := sdk.NewClient(fabsdk.WithUser("User1"), fabsdk.WithOrg(org1)).Channel("orgchannel")
139156
if err != nil {

test/integration/sdk/resmgmt_queries_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,30 @@ func TestResMgmtClientQueries(t *testing.T) {
5555

5656
testInstalledChaincodes(t, ccID, target, client)
5757

58+
testInstantiatedChaincodes(t, testSetup.ChannelID, ccID, target, client)
59+
5860
testQueryChannels(t, testSetup.ChannelID, target, client)
5961

6062
}
63+
func testInstantiatedChaincodes(t *testing.T, channelID string, ccID string, target fab.ProposalProcessor, client *resmgmt.Client) {
64+
65+
chaincodeQueryResponse, err := client.QueryInstantiatedChaincodes(channelID, resmgmt.WithTarget(target.(fab.Peer)))
66+
if err != nil {
67+
t.Fatalf("QueryInstantiatedChaincodes return error: %v", err)
68+
}
69+
70+
found := false
71+
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
72+
t.Logf("**InstantiatedCC: %s", chaincode)
73+
if chaincode.Name == ccID {
74+
found = true
75+
}
76+
}
77+
78+
if !found {
79+
t.Fatalf("QueryInstantiatedChaincodes failed to find instantiated %s chaincode", ccID)
80+
}
81+
}
6182

6283
func testInstalledChaincodes(t *testing.T, ccID string, target fab.ProposalProcessor, client *resmgmt.Client) {
6384

0 commit comments

Comments
 (0)