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

Commit 95febaf

Browse files
committed
[FAB-8725] JoinChannel should obey WithOrdererID
Change-Id: I1258ecb37ce0d1417aff6b82485f0c6d29ceb2fd Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 60d97ee commit 95febaf

File tree

3 files changed

+100
-82
lines changed

3 files changed

+100
-82
lines changed

pkg/client/resmgmt/resmgmt.go

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
570553
func 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
738721
func (rc *Client) prepareRequestOpts(options ...RequestOption) (Opts, error) {
739722
opts := Opts{}

pkg/client/resmgmt/resmgmt_test.go

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/golang/protobuf/proto"
18+
"github.com/pkg/errors"
19+
"github.com/stretchr/testify/assert"
1720
"google.golang.org/grpc"
1821

22+
txnmocks "github.com/hyperledger/fabric-sdk-go/pkg/client/common/mocks"
1923
"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
24+
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
2025
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
2126
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
27+
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
28+
fcmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
2229
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
2330
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
31+
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr"
2432
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
2533
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
26-
"github.com/pkg/errors"
27-
28-
txnmocks "github.com/hyperledger/fabric-sdk-go/pkg/client/common/mocks"
29-
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
30-
31-
"github.com/golang/protobuf/proto"
32-
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
33-
fcmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
34-
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr"
3534
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
3635
)
3736

@@ -166,12 +165,6 @@ func TestJoinChannelRequiredParameters(t *testing.T) {
166165
t.Fatalf("Should have failed for empty channel name")
167166
}
168167

169-
// Test error when creating channel from configuration
170-
err = rc.JoinChannel("error")
171-
if err == nil {
172-
t.Fatalf("Should have failed with generated error in NewChannel")
173-
}
174-
175168
// Setup test client with different msp (default targets cannot be calculated)
176169
ctx := setupTestContext("test", "otherMSP")
177170
config := getNetworkConfig(t)
@@ -278,6 +271,40 @@ func TestJoinChannelDiscoveryError(t *testing.T) {
278271

279272
}
280273

274+
func TestOrdererConfigFail(t *testing.T) {
275+
276+
ctx := setupTestContext("test", "Org1MSP")
277+
278+
// No channel orderer, no global orderer
279+
noOrdererConfig, err := config.FromFile("./testdata/noorderer_test.yaml")()
280+
assert.Nil(t, err)
281+
282+
ctx.SetConfig(noOrdererConfig)
283+
rc := setupResMgmtClient(ctx, nil, t)
284+
285+
opts := Opts{}
286+
orderer, err := rc.ordererConfig(&opts, "mychannel")
287+
assert.Nil(t, orderer)
288+
assert.NotNil(t, err, "should fail since no orderer has been configured")
289+
}
290+
291+
/*
292+
func TestOrdererConfigFromOpts(t *testing.T) {
293+
ctx := setupTestContext("test", "Org1MSP")
294+
295+
// No channel orderer, no global orderer
296+
noOrdererConfig, err := config.FromFile("./testdata/ccproposal_test.yaml")()
297+
assert.Nil(t, err)
298+
299+
ctx.SetConfig(noOrdererConfig)
300+
rc := setupResMgmtClient(ctx, nil, t)
301+
302+
opts := Opts{}
303+
orderer, err := rc.ordererConfig(&opts, "mychannel")
304+
assert.Nil(t, orderer)
305+
assert.NotNil(t, err, "should fail since no orderer has been configured")
306+
}*/
307+
281308
func TestJoinChannelNoOrdererConfig(t *testing.T) {
282309

283310
ctx := setupTestContext("test", "Org1MSP")
@@ -291,9 +318,7 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) {
291318
rc := setupResMgmtClient(ctx, nil, t)
292319

293320
err = rc.JoinChannel("mychannel")
294-
if err == nil {
295-
t.Fatalf("Should have failed to join channel since no orderer has been configured")
296-
}
321+
assert.NotNil(t, err, "Should have failed to join channel since no orderer has been configured")
297322

298323
// Misconfigured channel orderer
299324
invalidChOrdererConfig, err := config.FromFile("./testdata/invalidchorderer_test.yaml")()
@@ -1432,6 +1457,17 @@ func TestSaveChannelWithOpts(t *testing.T) {
14321457
}
14331458
}
14341459

1460+
func TestJoinChannelWithInvalidOpts(t *testing.T) {
1461+
1462+
cc := setupDefaultResMgmtClient(t)
1463+
opts := WithOrdererID("Invalid")
1464+
err := cc.JoinChannel("mychannel", opts)
1465+
if err == nil {
1466+
t.Fatal("Should have failed for invalid orderer ID")
1467+
}
1468+
1469+
}
1470+
14351471
func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
14361472
grpcServer := grpc.NewServer()
14371473
defer grpcServer.Stop()
@@ -1447,6 +1483,7 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
14471483
GRPCOptions: grpcOpts,
14481484
}
14491485
mockConfig.SetCustomRandomOrdererCfg(oConfig)
1486+
mockConfig.SetCustomOrdererCfg(oConfig)
14501487
ctx.SetConfig(mockConfig)
14511488

14521489
cc := setupResMgmtClient(ctx, nil, t)

pkg/fab/mocks/mockconfig.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ func (c *MockConfig) ChannelPeers(name string) ([]config.ChannelPeer, error) {
245245

246246
// ChannelOrderers returns a list of channel orderers
247247
func (c *MockConfig) ChannelOrderers(name string) ([]config.OrdererConfig, error) {
248-
oConfig := config.OrdererConfig{
249-
URL: "example.com",
250-
}
248+
oConfig, err := c.OrdererConfig("")
251249

252-
return []config.OrdererConfig{oConfig}, nil
250+
return []config.OrdererConfig{*oConfig}, err
253251
}
254252

255253
// NetworkPeers returns the mock network peers configuration

0 commit comments

Comments
 (0)