Skip to content

Commit 2830cfb

Browse files
committed
[FAB-884] implement basic query cli
Send proposal to endorser and print invoke result as query result. Change-Id: Ib9d2b382df079c4d8039e4c1599472ec35f8ac61 Signed-off-by: jiangyaoguo <[email protected]>
1 parent 44b3c13 commit 2830cfb

File tree

3 files changed

+53
-65
lines changed

3 files changed

+53
-65
lines changed

peer/chaincode/common.go

+52-34
Original file line numberDiff line numberDiff line change
@@ -166,46 +166,46 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
166166
invocation.IdGenerationAlg = customIDGenAlg
167167
}
168168

169-
if invoke {
170-
endorserClient, err := common.GetEndorserClient(cmd)
171-
if err != nil {
172-
return fmt.Errorf("Error getting endorser client %s: %s", chainFuncName, err)
173-
}
169+
endorserClient, err := common.GetEndorserClient(cmd)
170+
if err != nil {
171+
return fmt.Errorf("Error getting endorser client %s: %s", chainFuncName, err)
172+
}
174173

175-
// TODO: how should we get signing ID from the command line?
176-
mspID := "DEFAULT"
177-
id := "PEER"
178-
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspID}, Value: id}
174+
// TODO: how should we get signing ID from the command line?
175+
mspID := "DEFAULT"
176+
id := "PEER"
177+
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspID}, Value: id}
179178

180-
// TODO: how should we obtain the config for the MSP from the command line? a hardcoded test config?
181-
signer, err := msp.GetManager().GetSigningIdentity(signingIdentity)
182-
if err != nil {
183-
return fmt.Errorf("Error obtaining signing identity for %s: %s\n", signingIdentity, err)
184-
}
179+
// TODO: how should we obtain the config for the MSP from the command line? a hardcoded test config?
180+
signer, err := msp.GetManager().GetSigningIdentity(signingIdentity)
181+
if err != nil {
182+
return fmt.Errorf("Error obtaining signing identity for %s: %s\n", signingIdentity, err)
183+
}
185184

186-
creator, err := signer.Serialize()
187-
if err != nil {
188-
return fmt.Errorf("Error serializing identity for %s: %s\n", signingIdentity, err)
189-
}
185+
creator, err := signer.Serialize()
186+
if err != nil {
187+
return fmt.Errorf("Error serializing identity for %s: %s\n", signingIdentity, err)
188+
}
190189

191-
var prop *pb.Proposal
192-
prop, err = putils.CreateProposalFromCIS(invocation, creator)
193-
if err != nil {
194-
return fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
195-
}
190+
var prop *pb.Proposal
191+
prop, err = putils.CreateProposalFromCIS(invocation, creator)
192+
if err != nil {
193+
return fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
194+
}
196195

197-
var signedProp *pb.SignedProposal
198-
signedProp, err = putils.GetSignedProposal(prop, signer)
199-
if err != nil {
200-
return fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err)
201-
}
196+
var signedProp *pb.SignedProposal
197+
signedProp, err = putils.GetSignedProposal(prop, signer)
198+
if err != nil {
199+
return fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err)
200+
}
202201

203-
var proposalResp *pb.ProposalResponse
204-
proposalResp, err = endorserClient.ProcessProposal(context.Background(), signedProp)
205-
if err != nil {
206-
return fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err)
207-
}
202+
var proposalResp *pb.ProposalResponse
203+
proposalResp, err = endorserClient.ProcessProposal(context.Background(), signedProp)
204+
if err != nil {
205+
return fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err)
206+
}
208207

208+
if invoke {
209209
if proposalResp != nil {
210210
// assemble a signed transaction (it's an Envelope message)
211211
env, err := putils.CreateSignedTx(prop, signer, proposalResp)
@@ -218,8 +218,26 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
218218
return fmt.Errorf("Error sending transaction %s: %s\n", chainFuncName, err)
219219
}
220220
}
221-
222221
logger.Infof("Invoke result: %v", proposalResp)
222+
} else {
223+
if proposalResp == nil {
224+
return fmt.Errorf("Error query %s by endorsing: %s\n", chainFuncName, err)
225+
}
226+
227+
if chaincodeQueryRaw {
228+
if chaincodeQueryHex {
229+
err = errors.New("Options --raw (-r) and --hex (-x) are not compatible\n")
230+
return
231+
}
232+
fmt.Print("Query Result (Raw): ")
233+
os.Stdout.Write(proposalResp.Response.Payload)
234+
} else {
235+
if chaincodeQueryHex {
236+
fmt.Printf("Query Result: %x\n", proposalResp.Response.Payload)
237+
} else {
238+
fmt.Printf("Query Result: %s\n", string(proposalResp.Response.Payload))
239+
}
240+
}
223241
}
224242

225243
return nil

peer/chaincode/query.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ var chaincodeQueryCmd = &cobra.Command{
4242
}
4343

4444
func chaincodeQuery(cmd *cobra.Command, args []string) error {
45-
return fmt.Errorf("chaincode query is deprecated and should not be used")
45+
return chaincodeInvokeOrQuery(cmd, args, false)
4646
}

peer/chaincode/query_test.go

-30
This file was deleted.

0 commit comments

Comments
 (0)