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

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channel
     8  
     9  import (
    10  	"context"
    11  	"encoding/json"
    12  	"fmt"
    13  
    14  	"github.com/golang/protobuf/proto"
    15  	cb "github.com/hyperledger/fabric-protos-go/common"
    16  	pb "github.com/hyperledger/fabric-protos-go/peer"
    17  	"github.com/hyperledger/fabric/core/scc/qscc"
    18  	"github.com/hyperledger/fabric/internal/peer/common"
    19  	"github.com/hyperledger/fabric/protoutil"
    20  	"github.com/pkg/errors"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  func getinfoCmd(cf *ChannelCmdFactory) *cobra.Command {
    25  	getinfoCmd := &cobra.Command{
    26  		Use:   "getinfo",
    27  		Short: "get blockchain information of a specified channel.",
    28  		Long:  "get blockchain information of a specified channel. Requires '-c'.",
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			return getinfo(cmd, cf)
    31  		},
    32  	}
    33  	flagList := []string{
    34  		"channelID",
    35  	}
    36  	attachFlags(getinfoCmd, flagList)
    37  
    38  	return getinfoCmd
    39  }
    40  func (cc *endorserClient) getBlockChainInfo() (*cb.BlockchainInfo, error) {
    41  	var err error
    42  
    43  	invocation := &pb.ChaincodeInvocationSpec{
    44  		ChaincodeSpec: &pb.ChaincodeSpec{
    45  			Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
    46  			ChaincodeId: &pb.ChaincodeID{Name: "qscc"},
    47  			Input:       &pb.ChaincodeInput{Args: [][]byte{[]byte(qscc.GetChainInfo), []byte(channelID)}},
    48  		},
    49  	}
    50  
    51  	var prop *pb.Proposal
    52  	c, _ := cc.cf.Signer.Serialize()
    53  	prop, _, err = protoutil.CreateProposalFromCIS(cb.HeaderType_ENDORSER_TRANSACTION, "", invocation, c)
    54  	if err != nil {
    55  		return nil, errors.WithMessage(err, "cannot create proposal")
    56  	}
    57  
    58  	var signedProp *pb.SignedProposal
    59  	signedProp, err = protoutil.GetSignedProposal(prop, cc.cf.Signer)
    60  	if err != nil {
    61  		return nil, errors.WithMessage(err, "cannot create signed proposal")
    62  	}
    63  
    64  	proposalResp, err := cc.cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    65  	if err != nil {
    66  		return nil, errors.WithMessage(err, "failed sending proposal")
    67  	}
    68  
    69  	if proposalResp.Response == nil || proposalResp.Response.Status != 200 {
    70  		return nil, errors.Errorf("received bad response, status %d: %s", proposalResp.Response.Status, proposalResp.Response.Message)
    71  	}
    72  
    73  	blockChainInfo := &cb.BlockchainInfo{}
    74  	err = proto.Unmarshal(proposalResp.Response.Payload, blockChainInfo)
    75  	if err != nil {
    76  		return nil, errors.Wrap(err, "cannot read qscc response")
    77  	}
    78  
    79  	return blockChainInfo, nil
    80  
    81  }
    82  
    83  func getinfo(cmd *cobra.Command, cf *ChannelCmdFactory) error {
    84  	//the global chainID filled by the "-c" command
    85  	if channelID == common.UndefinedParamValue {
    86  		return errors.New("Must supply channel ID")
    87  	}
    88  	// Parsing of the command line is done so silence cmd usage
    89  	cmd.SilenceUsage = true
    90  
    91  	var err error
    92  	if cf == nil {
    93  		cf, err = InitCmdFactory(EndorserRequired, PeerDeliverNotRequired, OrdererNotRequired)
    94  		if err != nil {
    95  			return err
    96  		}
    97  	}
    98  
    99  	client := &endorserClient{cf}
   100  
   101  	blockChainInfo, err := client.getBlockChainInfo()
   102  	if err != nil {
   103  		return err
   104  	}
   105  	jsonBytes, err := json.Marshal(blockChainInfo)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	fmt.Printf("Blockchain info: %s\n", string(jsonBytes))
   111  
   112  	return nil
   113  }