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