Skip to content

Commit

Permalink
[FAB-3695] Fix endorser and orderer required check
Browse files Browse the repository at this point in the history
Fix the endorser and orderer requirement checking in peer channel cmd.

* Fix the wrong flag between isOrdererRequired and isEndorserRequired.
* Add seperate isOrdererRequired flag to indicate whether we need
  orderer.
* Use typedef to help people catch the boolean flag better.

Change-Id: Ib01221d98ebb4c2c9647604b0d8c86d04769738c
Signed-off-by: Baohua Yang <baohyang@cn.ibm.com>
  • Loading branch information
yeasy committed May 11, 2017
1 parent 41f80f9 commit d6b9bab
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
22 changes: 17 additions & 5 deletions peer/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const (
longDes = "Operate a channel: create|fetch|join|list."
)

type OrdererRequirement bool
type EndorserRequirement bool

const (
EndorserRequired EndorserRequirement = true
EndorserNotRequired EndorserRequirement = false
OrdererRequired OrdererRequirement = true
OrdererNotRequired OrdererRequirement = false
)

var (
// join related variables.
genesisBlockPath string
Expand Down Expand Up @@ -89,8 +99,8 @@ type ChannelCmdFactory struct {
BroadcastFactory BroadcastClientFactory
}

// InitCmdFactory init the ChannelCmdFactor with default clients
func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
// InitCmdFactory init the ChannelCmdFactor with clients to endorser and orderer according to params
func InitCmdFactory(isEndorserRequired EndorserRequirement, isOrdererRequired OrdererRequirement) (*ChannelCmdFactory, error) {
var err error

cmdFact := &ChannelCmdFactory{}
Expand All @@ -104,14 +114,16 @@ func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
return common.GetBroadcastClient(orderingEndpoint, tls, caFile)
}

//for join, we need the endorser as well
if isOrdererRequired {
//for join and list, we need the endorser as well
if isEndorserRequired {
cmdFact.EndorserClient, err = common.GetEndorserClient()
if err != nil {
return nil, fmt.Errorf("Error getting endorser client %s: %s", channelFuncName, err)
}
} else {
}

//for create and fetch, we need the orderer as well
if isOrdererRequired {
if len(strings.Split(orderingEndpoint, ":")) != 2 {
return nil, fmt.Errorf("Ordering service endpoint %s is not valid or missing", orderingEndpoint)
}
Expand Down
2 changes: 1 addition & 1 deletion peer/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func create(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {

var err error
if cf == nil {
cf, err = InitCmdFactory(false)
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion peer/channel/fetchconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func fetchCmd(cf *ChannelCmdFactory) *cobra.Command {
func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
var err error
if cf == nil {
cf, err = InitCmdFactory(false)
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion peer/channel/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func executeJoin(cf *ChannelCmdFactory) (err error) {
func join(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
var err error
if cf == nil {
cf, err = InitCmdFactory(true)
cf, err = InitCmdFactory(EndorserRequired, OrdererNotRequired)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion peer/channel/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func listCmd(cf *ChannelCmdFactory) *cobra.Command {
func list(cf *ChannelCmdFactory) error {
var err error
if cf == nil {
cf, err = InitCmdFactory(true)
cf, err = InitCmdFactory(EndorserRequired, OrdererNotRequired)
if err != nil {
return err
}
Expand Down

0 comments on commit d6b9bab

Please sign in to comment.