This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
list.go
154 lines (130 loc) · 4.08 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
/*
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/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var getInstalledChaincodes bool
var getInstantiatedChaincodes bool
var chaincodeListCmd *cobra.Command
const list_cmdname = "list"
// installCmd returns the cobra command for Chaincode Deploy
func listCmd(cf *ChaincodeCmdFactory) *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)
},
}
flagList := []string{
"channelID",
"installed",
"instantiated",
"peerAddresses",
"tlsRootCertFiles",
"connectionProfile",
}
attachFlags(chaincodeListCmd, flagList)
return chaincodeListCmd
}
func getChaincodes(cmd *cobra.Command, cf *ChaincodeCmdFactory) 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)
if err != nil {
return err
}
}
creator, err := cf.Signer.Serialize()
if err != nil {
return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
}
var prop *pb.Proposal
if getInstalledChaincodes && (!getInstantiatedChaincodes) {
prop, _, err = utils.CreateGetInstalledChaincodesProposal(creator)
} else if getInstantiatedChaincodes && (!getInstalledChaincodes) {
prop, _, err = utils.CreateGetChaincodesProposal(channelID, creator)
} else {
return fmt.Errorf("Must explicitly specify \"--installed\" or \"--instantiated\"")
}
if err != nil {
return fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
}
var signedProp *pb.SignedProposal
signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
if err != nil {
return fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err)
}
// list is currently only supported for one peer
proposalResponse, err := cf.EndorserClients[0].ProcessProposal(context.Background(), signedProp)
if err != nil {
return errors.Errorf("Error endorsing %s: %s", chainFuncName, err)
}
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)
}
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
}