Skip to content

Commit

Permalink
[FAB-5211] CreateChannel to use TransactionID
Browse files Browse the repository at this point in the history
Change-Id: I55021aa7ee43353537ff3084f0ff5d24c924e39b
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jul 7, 2017
1 parent 6265e33 commit 1d36d9e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 61 deletions.
12 changes: 5 additions & 7 deletions api/apifabclient/fabricclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type FabricClient interface {
GetChannel(name string) Channel
ExtractChannelConfig(configEnvelope []byte) ([]byte, error)
SignChannelConfig(config []byte) (*common.ConfigSignature, error)
CreateChannel(request *CreateChannelRequest) error
CreateChannel(request CreateChannelRequest) (txn.TransactionID, error)
QueryChannelInfo(name string, peers []Peer) (Channel, error)
SetStateStore(stateStore KeyValueStore)
GetStateStore() KeyValueStore
Expand Down Expand Up @@ -72,10 +72,8 @@ type CreateChannelRequest struct {
// required by the channel create policy when using the `config` parameter.
// see signChannelConfig() method of this package
Signatures []*common.ConfigSignature
// optional - transaction ID
// required when using the `config` parameter
TxID string
// optional - nonce
// required when using the `config` parameter
Nonce []byte

// TODO: InvokeChannelRequest allows the TransactionID to be passed in.
// This request struct also has the field for consistency but perhaps it should be removed.
TxnID txn.TransactionID
}
31 changes: 18 additions & 13 deletions pkg/fabric-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,22 +319,27 @@ func (c *Client) SignChannelConfig(config []byte) (*common.ConfigSignature, erro
* required by the channel create policy when using the `config` parameter.
* @returns {Result} Result Object with status on the create process.
*/
func (c *Client) CreateChannel(request *fab.CreateChannelRequest) error {
func (c *Client) CreateChannel(request fab.CreateChannelRequest) (apitxn.TransactionID, error) {
haveEnvelope := false
if request != nil && request.Envelope != nil {
if request.Envelope != nil {
logger.Debug("createChannel - have envelope")
haveEnvelope = true
}
return c.CreateOrUpdateChannel(request, haveEnvelope)
}

// CreateOrUpdateChannel creates a new channel or updates an existing channel.
func (c *Client) CreateOrUpdateChannel(request *fab.CreateChannelRequest, haveEnvelope bool) error {
// Validate request
if request == nil {
return fmt.Errorf("Missing all required input request parameters for initialize channel")
if !haveEnvelope && request.TxnID.ID == "" {
txnID, err := c.NewTxnID()
if err != nil {
return txnID, err
}
request.TxnID = txnID
}

return request.TxnID, c.createOrUpdateChannel(request, haveEnvelope)
}

// createOrUpdateChannel creates a new channel or updates an existing channel.
func (c *Client) createOrUpdateChannel(request fab.CreateChannelRequest, haveEnvelope bool) error {
// Validate request
if request.Config == nil && !haveEnvelope {
return fmt.Errorf("Missing envelope request parameter containing the configuration of the new channel")
}
Expand All @@ -343,11 +348,11 @@ func (c *Client) CreateOrUpdateChannel(request *fab.CreateChannelRequest, haveEn
return fmt.Errorf("Missing signatures request parameter for the new channel")
}

if request.TxID == "" && !haveEnvelope {
if request.TxnID.ID == "" && !haveEnvelope {
return fmt.Errorf("Missing txId request parameter")
}

if request.Nonce == nil && !haveEnvelope {
if request.TxnID.Nonce == nil && !haveEnvelope {
return fmt.Errorf("Missing nonce request parameter")
}

Expand Down Expand Up @@ -380,7 +385,7 @@ func (c *Client) CreateOrUpdateChannel(request *fab.CreateChannelRequest, haveEn
}

// TODO: Move
channelHeader, err := channel.BuildChannelHeader(common.HeaderType_CONFIG_UPDATE, request.Name, request.TxID, 0, "", time.Now())
channelHeader, err := channel.BuildChannelHeader(common.HeaderType_CONFIG_UPDATE, request.Name, request.TxnID.ID, 0, "", time.Now())
if err != nil {
return fmt.Errorf("error when building channel header: %v", err)
}
Expand All @@ -389,7 +394,7 @@ func (c *Client) CreateOrUpdateChannel(request *fab.CreateChannelRequest, haveEn
return fmt.Errorf("Error getting creator: %v", err)
}

header, err := fc.BuildHeader(creator, channelHeader, request.Nonce)
header, err := fc.BuildHeader(creator, channelHeader, request.TxnID.Nonce)
if err != nil {
return fmt.Errorf("error when building header: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/fabric-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestCreateChannel(t *testing.T) {
orderer := mocks.NewMockOrderer(fmt.Sprintf("0.0.0.0:1234"), verifyBroadcast)

// Create channel without envelope
err = client.CreateChannel(&fab.CreateChannelRequest{
_, err = client.CreateChannel(fab.CreateChannelRequest{
Orderer: orderer,
Name: "mychannel",
})
Expand All @@ -146,7 +146,7 @@ func TestCreateChannel(t *testing.T) {
}

// Create channel without orderer
err = client.CreateChannel(&fab.CreateChannelRequest{
_, err = client.CreateChannel(fab.CreateChannelRequest{
Envelope: configTx,
Name: "mychannel",
})
Expand All @@ -155,7 +155,7 @@ func TestCreateChannel(t *testing.T) {
}

// Create channel without name
err = client.CreateChannel(&fab.CreateChannelRequest{
_, err = client.CreateChannel(fab.CreateChannelRequest{
Envelope: configTx,
Orderer: orderer,
})
Expand All @@ -164,12 +164,12 @@ func TestCreateChannel(t *testing.T) {
}

// Test with valid cofiguration
request := &fab.CreateChannelRequest{
request := fab.CreateChannelRequest{
Envelope: configTx,
Orderer: orderer,
Name: "mychannel",
}
err = client.CreateChannel(request)
_, err = client.CreateChannel(request)
if err != nil {
t.Fatalf("Did not expect error from create channel. Got error: %v", err)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/fabric-client/mocks/mockclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,8 @@ func (c *MockClient) SignChannelConfig(config []byte) (*common.ConfigSignature,
}

// CreateChannel ...
func (c *MockClient) CreateChannel(request *fab.CreateChannelRequest) error {
return fmt.Errorf("Not implemented yet")

}

// CreateOrUpdateChannel ...
func (c *MockClient) CreateOrUpdateChannel(request *fab.CreateChannelRequest, haveEnvelope bool) error {
return fmt.Errorf("Not implemented yet")
func (c *MockClient) CreateChannel(request fab.CreateChannelRequest) (apitxn.TransactionID, error) {
return apitxn.TransactionID{}, fmt.Errorf("Not implemented yet")

}

Expand Down
17 changes: 1 addition & 16 deletions pkg/fabric-txn/admin/transactionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,15 @@ func CreateOrUpdateChannel(client fab.FabricClient, ordererUser ca.User, orgUser
var configSignatures []*common.ConfigSignature
configSignatures = append(configSignatures, configSignature)

creator, err := client.GetIdentity()
if err != nil {
return fmt.Errorf("Error getting creator: %v", err)
}
nonce, err := internal.GenerateRandomNonce()
if err != nil {
return fmt.Errorf("Could not compute nonce: %s", err)
}
txID, err := internal.ComputeTxID(nonce, creator)
if err != nil {
return fmt.Errorf("Could not compute TxID: %s", err)
}

request := fab.CreateChannelRequest{
Name: channel.Name(),
Orderer: channel.Orderers()[0],
Config: config,
Signatures: configSignatures,
TxID: txID,
Nonce: nonce,
}

client.SetUserContext(ordererUser)
err = client.CreateChannel(&request)
_, err = client.CreateChannel(request)
if err != nil {
return fmt.Errorf("CreateChannel returned error: %v", err)
}
Expand Down
12 changes: 0 additions & 12 deletions pkg/fabric-txn/internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (

fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
"github.com/hyperledger/fabric/common/crypto"
pb "github.com/hyperledger/fabric/protos/peer"
protos_utils "github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging"
)

Expand Down Expand Up @@ -86,13 +84,3 @@ func RegisterTxEvent(txID apitxn.TransactionID, eventHub fab.EventHub) (chan boo

return done, fail
}

// GenerateRandomNonce generates a random nonce
func GenerateRandomNonce() ([]byte, error) {
return crypto.GetRandomNonce()
}

// ComputeTxID computes a transaction ID from a given nonce and creator ID
func ComputeTxID(nonce []byte, creator []byte) (string, error) {
return protos_utils.ComputeProposalTxID(nonce, creator)
}

0 comments on commit 1d36d9e

Please sign in to comment.