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

Commit 37f0a1a

Browse files
committed
[FAB-9023] SaveChannel Response Struct
This change wraps the transaction ID into a response struct to be consistent with the other APIs in this package. Change-Id: I279dc622d8a0969c25f46bdbff758ed7f50575bb Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 13e434c commit 37f0a1a

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

pkg/client/resmgmt/resmgmt.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ type SaveChannelRequest struct {
9999
// TODO: support pre-signed signature blocks
100100
}
101101

102+
// SaveChannelResponse contains response parameters for Save
103+
type SaveChannelResponse struct {
104+
TransactionID fab.TransactionID
105+
}
106+
102107
//RequestOption func for each Opts argument
103108
type RequestOption func(ctx context.Client, opts *requestOptions) error
104109

@@ -682,24 +687,24 @@ func peersToTxnProcessors(peers []fab.Peer) []fab.ProposalProcessor {
682687
}
683688

684689
// SaveChannel creates or updates channel
685-
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) (fab.TransactionID, error) {
690+
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) (SaveChannelResponse, error) {
686691

687692
opts, err := rc.prepareRequestOpts(options...)
688693
if err != nil {
689-
return fab.EmptyTransactionID, err
694+
return SaveChannelResponse{}, err
690695
}
691696

692697
if req.ChannelConfigPath != "" {
693698
configReader, err := os.Open(req.ChannelConfigPath)
694699
if err != nil {
695-
return fab.EmptyTransactionID, errors.Wrapf(err, "opening channel config file failed")
700+
return SaveChannelResponse{}, errors.Wrapf(err, "opening channel config file failed")
696701
}
697702
defer loggedClose(configReader)
698703
req.ChannelConfig = configReader
699704
}
700705

701706
if req.ChannelID == "" || req.ChannelConfig == nil {
702-
return fab.EmptyTransactionID, errors.New("must provide channel ID and channel config")
707+
return SaveChannelResponse{}, errors.New("must provide channel ID and channel config")
703708
}
704709

705710
logger.Debugf("saving channel: %s", req.ChannelID)
@@ -717,17 +722,17 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
717722
} else if rc.ctx != nil {
718723
signers = append(signers, rc.ctx)
719724
} else {
720-
return fab.EmptyTransactionID, errors.New("must provide signing user")
725+
return SaveChannelResponse{}, errors.New("must provide signing user")
721726
}
722727

723728
configTx, err := ioutil.ReadAll(req.ChannelConfig)
724729
if err != nil {
725-
return fab.EmptyTransactionID, errors.WithMessage(err, "reading channel config file failed")
730+
return SaveChannelResponse{}, errors.WithMessage(err, "reading channel config file failed")
726731
}
727732

728733
chConfig, err := resource.ExtractChannelConfig(configTx)
729734
if err != nil {
730-
return fab.EmptyTransactionID, errors.WithMessage(err, "extracting channel config failed")
735+
return SaveChannelResponse{}, errors.WithMessage(err, "extracting channel config failed")
731736
}
732737

733738
var configSignatures []*common.ConfigSignature
@@ -740,14 +745,14 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
740745

741746
configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
742747
if err != nil {
743-
return fab.EmptyTransactionID, errors.WithMessage(err, "signing configuration failed")
748+
return SaveChannelResponse{}, errors.WithMessage(err, "signing configuration failed")
744749
}
745750
configSignatures = append(configSignatures, configSignature)
746751
}
747752

748753
orderer, err := rc.requestOrderer(&opts, req.ChannelID)
749754
if err != nil {
750-
return fab.EmptyTransactionID, errors.WithMessage(err, "failed to find orderer for request")
755+
return SaveChannelResponse{}, errors.WithMessage(err, "failed to find orderer for request")
751756
}
752757

753758
request := api.CreateChannelRequest{
@@ -762,10 +767,10 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
762767

763768
txID, err := resource.CreateChannel(reqCtx, request)
764769
if err != nil {
765-
return fab.EmptyTransactionID, errors.WithMessage(err, "create channel failed")
770+
return SaveChannelResponse{}, errors.WithMessage(err, "create channel failed")
766771
}
767772

768-
return txID, nil
773+
return SaveChannelResponse{TransactionID: txID}, nil
769774
}
770775

771776
func loggedClose(c io.Closer) {

pkg/client/resmgmt/resmgmt_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,14 +1366,14 @@ func TestSaveChannelSuccess(t *testing.T) {
13661366
assert.NotNil(t, err, "Should have failed to sign configuration")
13671367

13681368
// Test valid Save Channel request (success)
1369-
txID, err := cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r}, WithOrdererURL("example.com"))
1369+
resp, err := cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r}, WithOrdererURL("example.com"))
13701370
assert.Nil(t, err, "error should be nil")
1371-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1371+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
13721372

13731373
// Test valid Save Channel request (success / filename)
1374-
txID, err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfigPath: channelConfig}, WithOrdererURL("example.com"))
1374+
resp, err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfigPath: channelConfig}, WithOrdererURL("example.com"))
13751375
assert.Nil(t, err, "error should be nil")
1376-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1376+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
13771377
}
13781378

13791379
func TestSaveChannelFailure(t *testing.T) {
@@ -1435,9 +1435,9 @@ func TestSaveChannelWithOpts(t *testing.T) {
14351435

14361436
// Test empty option (default order is random orderer from config)
14371437
opts := WithOrdererURL("")
1438-
txID, err := cc.SaveChannel(req, opts)
1438+
resp, err := cc.SaveChannel(req, opts)
14391439
assert.Nil(t, err, "error should be nil")
1440-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1440+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
14411441

14421442
// Test valid orderer ID
14431443
r2, err := os.Open(channelConfig)
@@ -1447,9 +1447,9 @@ func TestSaveChannelWithOpts(t *testing.T) {
14471447
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2}
14481448

14491449
opts = WithOrdererURL("orderer.example.com")
1450-
txID, err = cc.SaveChannel(req, opts)
1450+
resp, err = cc.SaveChannel(req, opts)
14511451
assert.Nil(t, err, "error should be nil")
1452-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1452+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
14531453

14541454
// Test invalid orderer ID
14551455
r3, err := os.Open(channelConfig)
@@ -1505,9 +1505,9 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
15051505
defer r1.Close()
15061506

15071507
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1, SigningIdentities: []msp.SigningIdentity{}}
1508-
txID, err := cc.SaveChannel(req, WithOrdererURL(""))
1508+
resp, err := cc.SaveChannel(req, WithOrdererURL(""))
15091509
assert.Nil(t, err, "Failed to save channel with default signing identity: %s", err)
1510-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1510+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
15111511

15121512
// multiple signing identities
15131513
r2, err := os.Open(channelConfig)
@@ -1516,9 +1516,9 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
15161516

15171517
secondCtx := fcmocks.NewMockContext(mspmocks.NewMockSigningIdentity("second", "second"))
15181518
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2, SigningIdentities: []msp.SigningIdentity{cc.ctx, secondCtx}}
1519-
txID, err = cc.SaveChannel(req, WithOrdererURL(""))
1519+
resp, err = cc.SaveChannel(req, WithOrdererURL(""))
15201520
assert.Nil(t, err, "Failed to save channel with multiple signing identities: %s", err)
1521-
assert.NotEmpty(t, txID, "transaction ID should be populated")
1521+
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
15221522
}
15231523

15241524
func createClientContext(fabCtx context.Client) context.ClientProvider {

0 commit comments

Comments
 (0)