forked from hyperledger/fabric
/
queryinstalled.go
180 lines (152 loc) · 5 KB
/
queryinstalled.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
lb "github.com/hyperledger/fabric-protos-go/peer/lifecycle"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// InstalledQuerier holds the dependencies needed to query
// the installed chaincodes
type InstalledQuerier struct {
Command *cobra.Command
Input *InstalledQueryInput
EndorserClient EndorserClient
Signer Signer
Writer io.Writer
}
type InstalledQueryInput struct {
OutputFormat string
}
// QueryInstalledCmd returns the cobra command for listing
// the installed chaincodes
func QueryInstalledCmd(i *InstalledQuerier, cryptoProvider bccsp.BCCSP) *cobra.Command {
chaincodeQueryInstalledCmd := &cobra.Command{
Use: "queryinstalled",
Short: "Query the installed chaincodes on a peer.",
Long: "Query the installed chaincodes on a peer.",
RunE: func(cmd *cobra.Command, args []string) error {
if i == nil {
ccInput := &ClientConnectionsInput{
CommandName: cmd.Name(),
EndorserRequired: true,
ChannelID: channelID,
PeerAddresses: peerAddresses,
TLSRootCertFiles: tlsRootCertFiles,
ConnectionProfilePath: connectionProfilePath,
TLSEnabled: viper.GetBool("peer.tls.enabled"),
}
cc, err := NewClientConnections(ccInput, cryptoProvider)
if err != nil {
return err
}
iqInput := &InstalledQueryInput{
OutputFormat: output,
}
// queryinstalled only supports one peer connection,
// which is why we only wire in the first endorser
// client
i = &InstalledQuerier{
Command: cmd,
EndorserClient: cc.EndorserClients[0],
Input: iqInput,
Signer: cc.Signer,
Writer: os.Stdout,
}
}
return i.Query()
},
}
flagList := []string{
"peerAddresses",
"tlsRootCertFiles",
"connectionProfile",
"output",
}
attachFlags(chaincodeQueryInstalledCmd, flagList)
return chaincodeQueryInstalledCmd
}
// Query returns the chaincodes installed on a peer
func (i *InstalledQuerier) Query() error {
if i.Command != nil {
// Parsing of the command line is done so silence cmd usage
i.Command.SilenceUsage = true
}
proposal, err := i.createProposal()
if err != nil {
return errors.WithMessage(err, "failed to create proposal")
}
signedProposal, err := signProposal(proposal, i.Signer)
if err != nil {
return errors.WithMessage(err, "failed to create signed proposal")
}
proposalResponse, err := i.EndorserClient.ProcessProposal(context.Background(), signedProposal)
if err != nil {
return errors.WithMessage(err, "failed to endorse proposal")
}
if proposalResponse == nil {
return errors.New("received nil proposal response")
}
if proposalResponse.Response == nil {
return errors.New("received proposal response with nil response")
}
if proposalResponse.Response.Status != int32(cb.Status_SUCCESS) {
return errors.Errorf("query failed with status: %d - %s", proposalResponse.Response.Status, proposalResponse.Response.Message)
}
if strings.ToLower(i.Input.OutputFormat) == "json" {
return printResponseAsJSON(proposalResponse, &lb.QueryInstalledChaincodesResult{}, i.Writer)
}
return i.printResponse(proposalResponse)
}
// printResponse prints the information included in the response
// from the server.
func (i *InstalledQuerier) printResponse(proposalResponse *pb.ProposalResponse) error {
qicr := &lb.QueryInstalledChaincodesResult{}
err := proto.Unmarshal(proposalResponse.Response.Payload, qicr)
if err != nil {
return errors.Wrap(err, "failed to unmarshal proposal response's response payload")
}
fmt.Fprintln(i.Writer, "Installed chaincodes on peer:")
for _, chaincode := range qicr.InstalledChaincodes {
fmt.Fprintf(i.Writer, "Package ID: %s, Label: %s\n", chaincode.PackageId, chaincode.Label)
}
return nil
}
func (i *InstalledQuerier) createProposal() (*pb.Proposal, error) {
args := &lb.QueryInstalledChaincodesArgs{}
argsBytes, err := proto.Marshal(args)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal args")
}
ccInput := &pb.ChaincodeInput{
Args: [][]byte{[]byte("QueryInstalledChaincodes"), argsBytes},
}
cis := &pb.ChaincodeInvocationSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{Name: lifecycleName},
Input: ccInput,
},
}
signerSerialized, err := i.Signer.Serialize()
if err != nil {
return nil, errors.WithMessage(err, "failed to serialize identity")
}
proposal, _, err := protoutil.CreateProposalFromCIS(cb.HeaderType_ENDORSER_TRANSACTION, "", cis, signerSerialized)
if err != nil {
return nil, errors.WithMessage(err, "failed to create ChaincodeInvocationSpec proposal")
}
return proposal, nil
}