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