github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/channel/list.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channel
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  	"fmt"
    13  
    14  	"github.com/golang/protobuf/proto"
    15  	common2 "github.com/hyperledger/fabric-protos-go/common"
    16  	pb "github.com/hyperledger/fabric-protos-go/peer"
    17  	"github.com/hyperledger/fabric/core/scc/cscc"
    18  	"github.com/hyperledger/fabric/protoutil"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  type endorserClient struct {
    23  	cf *ChannelCmdFactory
    24  }
    25  
    26  func listCmd(cf *ChannelCmdFactory) *cobra.Command {
    27  	// Set the flags on the channel start command.
    28  	return &cobra.Command{
    29  		Use:   "list",
    30  		Short: "List of channels peer has joined.",
    31  		Long:  "List of channels peer has joined.",
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			if len(args) != 0 {
    34  				return fmt.Errorf("trailing args detected: %s", args)
    35  			}
    36  			// Parsing of the command line is done so silence cmd usage
    37  			cmd.SilenceUsage = true
    38  			return list(cf)
    39  		},
    40  	}
    41  }
    42  
    43  func (cc *endorserClient) getChannels() ([]*pb.ChannelInfo, error) {
    44  	var err error
    45  
    46  	invocation := &pb.ChaincodeInvocationSpec{
    47  		ChaincodeSpec: &pb.ChaincodeSpec{
    48  			Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
    49  			ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
    50  			Input:       &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetChannels)}},
    51  		},
    52  	}
    53  
    54  	var prop *pb.Proposal
    55  	c, _ := cc.cf.Signer.Serialize()
    56  	prop, _, err = protoutil.CreateProposalFromCIS(common2.HeaderType_ENDORSER_TRANSACTION, "", invocation, c)
    57  	if err != nil {
    58  		return nil, errors.New(fmt.Sprintf("Cannot create proposal, due to %s", err))
    59  	}
    60  
    61  	var signedProp *pb.SignedProposal
    62  	signedProp, err = protoutil.GetSignedProposal(prop, cc.cf.Signer)
    63  	if err != nil {
    64  		return nil, errors.New(fmt.Sprintf("Cannot create signed proposal, due to %s", err))
    65  	}
    66  
    67  	proposalResp, err := cc.cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    68  	if err != nil {
    69  		return nil, errors.New(fmt.Sprintf("Failed sending proposal, got %s", err))
    70  	}
    71  
    72  	if proposalResp.Response == nil || proposalResp.Response.Status != 200 {
    73  		return nil, errors.New(fmt.Sprintf("Received bad response, status %d: %s", proposalResp.Response.Status, proposalResp.Response.Message))
    74  	}
    75  
    76  	var channelQueryResponse pb.ChannelQueryResponse
    77  	err = proto.Unmarshal(proposalResp.Response.Payload, &channelQueryResponse)
    78  	if err != nil {
    79  		return nil, errors.New(fmt.Sprintf("Cannot read channels list response, %s", err))
    80  	}
    81  
    82  	return channelQueryResponse.Channels, nil
    83  }
    84  
    85  func list(cf *ChannelCmdFactory) error {
    86  	var err error
    87  	if cf == nil {
    88  		cf, err = InitCmdFactory(EndorserRequired, PeerDeliverNotRequired, OrdererNotRequired)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  
    94  	client := &endorserClient{cf}
    95  
    96  	if channels, err := client.getChannels(); err != nil {
    97  		return err
    98  	} else {
    99  		fmt.Println("Channels peers has joined: ")
   100  
   101  		for _, channel := range channels {
   102  			fmt.Printf("%s\n", channel.ChannelId)
   103  		}
   104  	}
   105  
   106  	return nil
   107  }