github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/channel/list.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package channel
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	"github.com/hyperledger/fabric/core/scc/cscc"
    25  	common2 "github.com/hyperledger/fabric/protos/common"
    26  	pb "github.com/hyperledger/fabric/protos/peer"
    27  	"github.com/hyperledger/fabric/protos/utils"
    28  	"github.com/spf13/cobra"
    29  	"golang.org/x/net/context"
    30  )
    31  
    32  type endorserClient struct {
    33  	cf *ChannelCmdFactory
    34  }
    35  
    36  func listCmd(cf *ChannelCmdFactory) *cobra.Command {
    37  	// Set the flags on the channel start command.
    38  	return &cobra.Command{
    39  		Use:   "list",
    40  		Short: "List of channels peer has joined.",
    41  		Long:  "List of channels peer has joined.",
    42  		RunE: func(cmd *cobra.Command, args []string) error {
    43  			return list(cf)
    44  		},
    45  	}
    46  }
    47  
    48  func (cc *endorserClient) getChannels() ([]*pb.ChannelInfo, error) {
    49  	var err error
    50  
    51  	invocation := &pb.ChaincodeInvocationSpec{
    52  		ChaincodeSpec: &pb.ChaincodeSpec{
    53  			Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
    54  			ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
    55  			Input:       &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetChannels)}},
    56  		},
    57  	}
    58  
    59  	var prop *pb.Proposal
    60  	c, _ := cc.cf.Signer.Serialize()
    61  	prop, _, err = utils.CreateProposalFromCIS(common2.HeaderType_ENDORSER_TRANSACTION, "", invocation, c)
    62  	if err != nil {
    63  		return nil, errors.New(fmt.Sprintf("Cannot create proposal, due to %s", err))
    64  	}
    65  
    66  	var signedProp *pb.SignedProposal
    67  	signedProp, err = utils.GetSignedProposal(prop, cc.cf.Signer)
    68  	if err != nil {
    69  		return nil, errors.New(fmt.Sprintf("Cannot create signed proposal, due to %s", err))
    70  	}
    71  
    72  	proposalResp, err := cc.cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    73  	if err != nil {
    74  		return nil, errors.New(fmt.Sprintf("Failed sending proposal, got %s", err))
    75  	}
    76  
    77  	if proposalResp.Response == nil || proposalResp.Response.Status != 200 {
    78  		return nil, errors.New(fmt.Sprint("Received bad response, status", proposalResp.Response.Status))
    79  	}
    80  
    81  	var channelQueryResponse pb.ChannelQueryResponse
    82  	err = proto.Unmarshal(proposalResp.Response.Payload, &channelQueryResponse)
    83  	if err != nil {
    84  		return nil, errors.New(fmt.Sprintf("Cannot read channels list response, %s", err))
    85  	}
    86  
    87  	return channelQueryResponse.Channels, nil
    88  }
    89  
    90  func list(cf *ChannelCmdFactory) error {
    91  	var err error
    92  	if cf == nil {
    93  		cf, err = InitCmdFactory(EndorserRequired, OrdererNotRequired)
    94  		if err != nil {
    95  			return err
    96  		}
    97  	}
    98  
    99  	client := &endorserClient{cf}
   100  
   101  	if channels, err := client.getChannels(); err != nil {
   102  		return err
   103  	} else {
   104  		logger.Infof("Channels peers has joined to: ")
   105  
   106  		for _, channel := range channels {
   107  			logger.Infof("%s ", channel.ChannelId)
   108  		}
   109  	}
   110  
   111  	return nil
   112  }