-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
171 lines (142 loc) · 4.61 KB
/
list.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
chaincodeListCmd *cobra.Command
getInstalledChaincodes bool
getInstantiatedChaincodes bool
)
// listCmd returns the cobra command for listing
// the installed or instantiated chaincodes
func listCmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command {
chaincodeListCmd = &cobra.Command{
Use: "list",
Short: "Get the instantiated chaincodes on a channel or installed chaincodes on a peer.",
Long: "Get the instantiated chaincodes in the channel if specify channel, or get installed chaincodes on the peer",
RunE: func(cmd *cobra.Command, args []string) error {
return getChaincodes(cmd, cf, cryptoProvider)
},
}
flagList := []string{
"channelID",
"installed",
"instantiated",
"peerAddresses",
"tlsRootCertFiles",
"connectionProfile",
}
attachFlags(chaincodeListCmd, flagList)
return chaincodeListCmd
}
func getChaincodes(cmd *cobra.Command, cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) error {
if getInstantiatedChaincodes && channelID == "" {
return errors.New("The required parameter 'channelID' is empty. Rerun the command with -C flag")
}
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true
var err error
if cf == nil {
cf, err = InitCmdFactory(cmd.Name(), true, false, cryptoProvider)
if err != nil {
return err
}
}
creator, err := cf.Signer.Serialize()
if err != nil {
return fmt.Errorf("error serializing identity: %s", err)
}
var proposal *pb.Proposal
if getInstalledChaincodes == getInstantiatedChaincodes {
return errors.New("must explicitly specify \"--installed\" or \"--instantiated\"")
}
if getInstalledChaincodes {
proposal, _, err = protoutil.CreateGetInstalledChaincodesProposal(creator)
}
if getInstantiatedChaincodes {
proposal, _, err = protoutil.CreateGetChaincodesProposal(channelID, creator)
}
if err != nil {
return errors.WithMessage(err, "error creating proposal")
}
var signedProposal *pb.SignedProposal
signedProposal, err = protoutil.GetSignedProposal(proposal, cf.Signer)
if err != nil {
return errors.WithMessage(err, "error creating signed proposal")
}
// list is currently only supported for one peer
proposalResponse, err := cf.EndorserClients[0].ProcessProposal(context.Background(), signedProposal)
if err != nil {
return errors.WithMessage(err, "error endorsing proposal")
}
if proposalResponse.Response == nil {
return errors.Errorf("proposal response had nil response")
}
if proposalResponse.Response.Status != int32(cb.Status_SUCCESS) {
return errors.Errorf("bad response: %d - %s", proposalResponse.Response.Status, proposalResponse.Response.Message)
}
return printResponse(getInstalledChaincodes, getInstantiatedChaincodes, proposalResponse)
}
// printResponse prints the information included in the response
// from the server. If getInstalledChaincodes is set to false, the
// proposal response will be interpreted as containing instantiated
// chaincode information.
func printResponse(getInstalledChaincodes, getInstantiatedChaincodes bool, proposalResponse *pb.ProposalResponse) error {
cqr := &pb.ChaincodeQueryResponse{}
err := proto.Unmarshal(proposalResponse.Response.Payload, cqr)
if err != nil {
return err
}
if getInstalledChaincodes {
fmt.Println("Get installed chaincodes on peer:")
} else {
fmt.Printf("Get instantiated chaincodes on channel %s:\n", channelID)
}
for _, chaincode := range cqr.Chaincodes {
fmt.Printf("%v\n", ccInfo{chaincode}.String())
}
return nil
}
type ccInfo struct {
*pb.ChaincodeInfo
}
func (cci ccInfo) String() string {
b := bytes.Buffer{}
md := reflect.ValueOf(*cci.ChaincodeInfo)
md2 := reflect.Indirect(reflect.ValueOf(*cci.ChaincodeInfo)).Type()
for i := 0; i < md.NumField(); i++ {
f := md.Field(i)
val := f.String()
if isBytes(f) {
val = hex.EncodeToString(f.Bytes())
}
if len(val) == 0 {
continue
}
// Skip the proto-internal generated fields
if strings.HasPrefix(md2.Field(i).Name, "XXX") {
continue
}
b.WriteString(fmt.Sprintf("%s: %s, ", md2.Field(i).Name, val))
}
return b.String()[:len(b.String())-2]
}
func isBytes(v reflect.Value) bool {
return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8
}