@@ -168,7 +168,7 @@ func (rc *Client) JoinChannel(channelID string, options ...RequestOption) error
168168 return errors .New ("must provide channel ID" )
169169 }
170170
171- opts , err := rc .prepareResmgmtOpts (options ... )
171+ opts , err := rc .prepareRequestOpts (options ... )
172172 if err != nil {
173173 return errors .WithMessage (err , "failed to get opts for JoinChannel" )
174174 }
@@ -182,17 +182,12 @@ func (rc *Client) JoinChannel(channelID string, options ...RequestOption) error
182182 return errors .New ("No targets available" )
183183 }
184184
185- // TODO: should the code to get orderers from sdk config be part of channel service?
186- oConfig , err := rc .ctx .Config ().ChannelOrderers (channelID )
185+ ordererCfg , err := rc .ordererConfig (& opts , channelID )
187186 if err != nil {
188- return errors .WithMessage (err , "failed to load orderer config" )
189- }
190- if len (oConfig ) == 0 {
191- return errors .Errorf ("no orderers are configured for channel %s" , channelID )
187+ return errors .WithMessage (err , "failed to find orderer config" )
192188 }
193189
194- // TODO: handle more than the first orderer.
195- orderer , err := rc .ctx .InfraProvider ().CreateOrdererFromConfig (& oConfig [0 ])
190+ orderer , err := rc .ctx .InfraProvider ().CreateOrdererFromConfig (ordererCfg )
196191 if err != nil {
197192 return errors .WithMessage (err , "failed to create orderers from config" )
198193 }
@@ -306,7 +301,7 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
306301 return nil , err
307302 }
308303
309- opts , err := rc .prepareResmgmtOpts (options ... )
304+ opts , err := rc .prepareRequestOpts (options ... )
310305 if err != nil {
311306 return nil , errors .WithMessage (err , "failed to get opts for InstallCC" )
312307 }
@@ -450,7 +445,7 @@ func (rc *Client) sendCCProposal(ccProposalType chaincodeProposalType, channelID
450445 return err
451446 }
452447
453- opts , err := rc .prepareResmgmtOpts (options ... )
448+ opts , err := rc .prepareRequestOpts (options ... )
454449 if err != nil {
455450 return errors .WithMessage (err , "failed to get opts for cc proposal" )
456451 }
@@ -555,18 +550,6 @@ func checkRequiredCCProposalParams(channelID string, req InstantiateCCRequest) e
555550 return nil
556551}
557552
558- //prepareResmgmtOpts Reads Opts from Option array
559- func (rc * Client ) prepareResmgmtOpts (options ... RequestOption ) (Opts , error ) {
560- resmgmtOpts := Opts {}
561- for _ , option := range options {
562- err := option (& resmgmtOpts )
563- if err != nil {
564- return resmgmtOpts , errors .WithMessage (err , "Failed to read resource management opts" )
565- }
566- }
567- return resmgmtOpts , nil
568- }
569-
570553func createAndSendTransaction (sender fab.Sender , request fab.TransactionRequest ) (* fab.TransactionResponse , error ) {
571554
572555 tx , err := sender .CreateTransaction (request )
@@ -648,18 +631,9 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
648631 configSignatures = append (configSignatures , configSignature )
649632 }
650633
651- // Figure out orderer configuration
652- var ordererCfg * core.OrdererConfig
653- if opts .OrdererID != "" {
654- ordererCfg , err = rc .ctx .Config ().OrdererConfig (opts .OrdererID )
655- } else {
656- // Default is random orderer from configuration
657- ordererCfg , err = rc .ctx .Config ().RandomOrdererConfig ()
658- }
659-
660- // Check if retrieving orderer configuration went ok
661- if err != nil || ordererCfg == nil {
662- return errors .Errorf ("failed to retrieve orderer config: %s" , err )
634+ ordererCfg , err := rc .ordererConfig (& opts , req .ChannelID )
635+ if err != nil {
636+ return errors .WithMessage (err , "failed to find orderer config" )
663637 }
664638
665639 orderer , err := orderer .New (rc .ctx .Config (), orderer .FromOrdererConfig (ordererCfg ))
@@ -692,32 +666,9 @@ func (rc *Client) QueryConfigFromOrderer(channelID string, options ...RequestOpt
692666 return nil , err
693667 }
694668
695- chCfg , err := rc .ctx . Config (). ChannelConfig ( channelID )
669+ ordererCfg , err := rc .ordererConfig ( & opts , channelID )
696670 if err != nil {
697- return nil , err
698- }
699-
700- var ordererCfg * core.OrdererConfig
701-
702- // Figure out orderer configuration (first try opts, then random channel orderer, then random orderer )
703- if opts .OrdererID != "" {
704-
705- ordererCfg , err = rc .ctx .Config ().OrdererConfig (opts .OrdererID )
706-
707- } else if chCfg != nil && len (chCfg .Orderers ) > 0 {
708-
709- // random channel orderer
710- randomNumber := rand .Intn (len (chCfg .Orderers ))
711- ordererCfg , err = rc .ctx .Config ().OrdererConfig (chCfg .Orderers [randomNumber ])
712-
713- } else {
714- // random orderer from configuration
715- ordererCfg , err = rc .ctx .Config ().RandomOrdererConfig ()
716- }
717-
718- // Check if retrieving orderer configuration went ok
719- if err != nil || ordererCfg == nil {
720- return nil , errors .Errorf ("failed to retrieve orderer config: %s" , err )
671+ return nil , errors .WithMessage (err , "failed to find orderer config" )
721672 }
722673
723674 orderer , err := orderer .New (rc .ctx .Config (), orderer .FromOrdererConfig (ordererCfg ))
@@ -734,6 +685,38 @@ func (rc *Client) QueryConfigFromOrderer(channelID string, options ...RequestOpt
734685
735686}
736687
688+ func (rc * Client ) ordererConfig (opts * Opts , channelID string ) (* core.OrdererConfig , error ) {
689+ if opts .OrdererID != "" {
690+ ordererCfg , err := rc .ctx .Config ().OrdererConfig (opts .OrdererID )
691+ if err != nil {
692+ return nil , errors .WithMessage (err , "orderer not found" )
693+ }
694+ if ordererCfg == nil {
695+ return nil , errors .New ("orderer not found" )
696+ }
697+ return ordererCfg , nil
698+ }
699+
700+ orderers , err := rc .ctx .Config ().ChannelOrderers (channelID )
701+
702+ // TODO: Not sure that we should fallback to global orderers section.
703+ // For now - not doing so.
704+ //if err != nil || len(orderers) == 0 {
705+ // orderers, err = rc.ctx.Config().OrderersConfig()
706+ //}
707+
708+ if err != nil {
709+ return nil , errors .WithMessage (err , "orderers lookup failed" )
710+ }
711+ if len (orderers ) == 0 {
712+ return nil , errors .New ("no orderers found" )
713+ }
714+
715+ // random channel orderer
716+ randomNumber := rand .Intn (len (orderers ))
717+ return & orderers [randomNumber ], nil
718+ }
719+
737720// prepareRequestOpts prepares request options
738721func (rc * Client ) prepareRequestOpts (options ... RequestOption ) (Opts , error ) {
739722 opts := Opts {}
0 commit comments