github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 (cc *endorserClient) getChannels() ([]*pb.ChannelInfo, error) { 37 var err error 38 39 invocation := &pb.ChaincodeInvocationSpec{ 40 ChaincodeSpec: &pb.ChaincodeSpec{ 41 Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), 42 ChaincodeId: &pb.ChaincodeID{Name: "cscc"}, 43 Input: &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetChannels)}}, 44 }, 45 } 46 47 var prop *pb.Proposal 48 c, _ := cc.cf.Signer.Serialize() 49 prop, _, err = utils.CreateProposalFromCIS(common2.HeaderType_ENDORSER_TRANSACTION, "", invocation, c) 50 if err != nil { 51 return nil, errors.New(fmt.Sprintf("Cannot create proposal, due to %s", err)) 52 } 53 54 var signedProp *pb.SignedProposal 55 signedProp, err = utils.GetSignedProposal(prop, cc.cf.Signer) 56 if err != nil { 57 return nil, errors.New(fmt.Sprintf("Cannot create signed proposal, due to %s", err)) 58 } 59 60 proposalResp, err := cc.cf.EndorserClient.ProcessProposal(context.Background(), signedProp) 61 if err != nil { 62 return nil, errors.New(fmt.Sprintf("Failed sending proposal, got %s", err)) 63 } 64 65 if proposalResp.Response == nil || proposalResp.Response.Status != 200 { 66 return nil, errors.New(fmt.Sprint("Received bad response, status", proposalResp.Response.Status)) 67 } 68 69 var channelQueryResponse pb.ChannelQueryResponse 70 err = proto.Unmarshal(proposalResp.Response.Payload, &channelQueryResponse) 71 if err != nil { 72 return nil, errors.New(fmt.Sprintf("Cannot read channels list response, %s", err)) 73 } 74 75 return channelQueryResponse.Channels, nil 76 } 77 78 func listCmd(cf *ChannelCmdFactory) *cobra.Command { 79 // Set the flags on the channel start command. 80 81 return &cobra.Command{ 82 Use: "list", 83 Short: "List of channels peer has been joined to.", 84 Long: "List of channels peer has been joined to.", 85 RunE: func(cmd *cobra.Command, args []string) error { 86 return list(cf) 87 }, 88 } 89 } 90 91 func list(cf *ChannelCmdFactory) error { 92 var err error 93 if cf == nil { 94 cf, err = InitCmdFactory(true) 95 if err != nil { 96 return err 97 } 98 } 99 100 client := &endorserClient{cf} 101 102 if channels, err := client.getChannels(); err != nil { 103 return err 104 } else { 105 fmt.Println("Channels peers has joined to:") 106 for _, channel := range channels { 107 fmt.Println("\t", channel.ChannelId) 108 } 109 } 110 111 return nil 112 }