@@ -15,6 +15,7 @@ import (
1515 "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
1616 "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
1717 "github.com/hyperledger/fabric-sdk-go/pkg/fab/channel"
18+ "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig"
1819 "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
1920
2021 "github.com/hyperledger/fabric-sdk-go/pkg/context"
@@ -444,8 +445,7 @@ func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...Reque
444445 }
445446
446447 // select random channel peer
447- r := rand .New (rand .NewSource (time .Now ().Unix ()))
448- randomNumber := r .Intn (len (targets ))
448+ randomNumber := rand .Intn (len (targets ))
449449 target = targets [randomNumber ]
450450 }
451451
@@ -707,7 +707,64 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
707707 return nil
708708}
709709
710- //prepareRequestOpts prepares rrequest options
710+ // QueryConfigFromOrderer config returns channel configuration from orderer
711+ // Valid request option is WithOrdererID
712+ // If orderer id is not provided orderer will be defaulted to channel orderer (if configured) or random orderer from config
713+ func (rc * Client ) QueryConfigFromOrderer (channelID string , options ... RequestOption ) (fab.ChannelCfg , error ) {
714+
715+ opts , err := rc .prepareRequestOpts (options ... )
716+ if err != nil {
717+ return nil , err
718+ }
719+
720+ chCfg , err := rc .provider .Config ().ChannelConfig (channelID )
721+ if err != nil {
722+ return nil , err
723+ }
724+
725+ var ordererCfg * core.OrdererConfig
726+
727+ // Figure out orderer configuration (first try opts, then random channel orderer, then random orderer )
728+ if opts .OrdererID != "" {
729+
730+ ordererCfg , err = rc .provider .Config ().OrdererConfig (opts .OrdererID )
731+
732+ } else if chCfg != nil && len (chCfg .Orderers ) > 0 {
733+
734+ // random channel orderer
735+ randomNumber := rand .Intn (len (chCfg .Orderers ))
736+ ordererCfg , err = rc .provider .Config ().OrdererConfig (chCfg .Orderers [randomNumber ])
737+
738+ } else {
739+ // random orderer from configuration
740+ ordererCfg , err = rc .provider .Config ().RandomOrdererConfig ()
741+ }
742+
743+ // Check if retrieving orderer configuration went ok
744+ if err != nil || ordererCfg == nil {
745+ return nil , errors .Errorf ("failed to retrieve orderer config: %s" , err )
746+ }
747+
748+ orderer , err := orderer .New (rc .provider .Config (), orderer .FromOrdererConfig (ordererCfg ))
749+ if err != nil {
750+ return nil , errors .WithMessage (err , "failed to resolve orderer" )
751+ }
752+
753+ ctx := & fabContext {
754+ Providers : rc .provider ,
755+ Identity : rc .identity ,
756+ }
757+
758+ channelConfig , err := chconfig .New (ctx , channelID , chconfig .WithOrderer (orderer ))
759+ if err != nil {
760+ return nil , errors .WithMessage (err , "QueryConfig failed" )
761+ }
762+
763+ return channelConfig .Query ()
764+
765+ }
766+
767+ // prepareRequestOpts prepares request options
711768func (rc * Client ) prepareRequestOpts (options ... RequestOption ) (Opts , error ) {
712769 opts := Opts {}
713770 for _ , option := range options {
0 commit comments