github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/channel/getinfo.go (about)

     1  /*
     2  Copyright hechain. 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  	"github.com/hechain20/hechain/core/scc/qscc"
    16  	"github.com/hechain20/hechain/internal/peer/common"
    17  	"github.com/hechain20/hechain/protoutil"
    18  	cb "github.com/hyperledger/fabric-protos-go/common"
    19  	pb "github.com/hyperledger/fabric-protos-go/peer"
    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  
    41  func (cc *endorserClient) getBlockChainInfo() (*cb.BlockchainInfo, error) {
    42  	var err error
    43  
    44  	invocation := &pb.ChaincodeInvocationSpec{
    45  		ChaincodeSpec: &pb.ChaincodeSpec{
    46  			Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
    47  			ChaincodeId: &pb.ChaincodeID{Name: "qscc"},
    48  			Input:       &pb.ChaincodeInput{Args: [][]byte{[]byte(qscc.GetChainInfo), []byte(channelID)}},
    49  		},
    50  	}
    51  
    52  	var prop *pb.Proposal
    53  	c, _ := cc.cf.Signer.Serialize()
    54  	prop, _, err = protoutil.CreateProposalFromCIS(cb.HeaderType_ENDORSER_TRANSACTION, "", invocation, c)
    55  	if err != nil {
    56  		return nil, errors.WithMessage(err, "cannot create proposal")
    57  	}
    58  
    59  	var signedProp *pb.SignedProposal
    60  	signedProp, err = protoutil.GetSignedProposal(prop, cc.cf.Signer)
    61  	if err != nil {
    62  		return nil, errors.WithMessage(err, "cannot create signed proposal")
    63  	}
    64  
    65  	proposalResp, err := cc.cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    66  	if err != nil {
    67  		return nil, errors.WithMessage(err, "failed sending proposal")
    68  	}
    69  
    70  	if proposalResp.Response == nil || proposalResp.Response.Status != 200 {
    71  		return nil, errors.Errorf("received bad response, status %d: %s", proposalResp.Response.Status, proposalResp.Response.Message)
    72  	}
    73  
    74  	blockChainInfo := &cb.BlockchainInfo{}
    75  	err = proto.Unmarshal(proposalResp.Response.Payload, blockChainInfo)
    76  	if err != nil {
    77  		return nil, errors.Wrap(err, "cannot read qscc response")
    78  	}
    79  
    80  	return blockChainInfo, nil
    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  }