@@ -9,10 +9,12 @@ package resmgmt
99
1010import (
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.
418468func (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
580630func (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}
0 commit comments