19
19
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .COMPLETED ;
20
20
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .DEL_STATE ;
21
21
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .ERROR ;
22
+ import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .GET_QUERY_RESULT ;
22
23
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .GET_STATE ;
23
24
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .GET_STATE_BY_RANGE ;
24
25
import static org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type .INIT ;
48
49
import org .hyperledger .fabric .protos .peer .Chaincode .ChaincodeSpec ;
49
50
import org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage ;
50
51
import org .hyperledger .fabric .protos .peer .ChaincodeShim .ChaincodeMessage .Type ;
52
+ import org .hyperledger .fabric .protos .peer .ChaincodeShim .GetQueryResult ;
51
53
import org .hyperledger .fabric .protos .peer .ChaincodeShim .GetStateByRange ;
52
54
import org .hyperledger .fabric .protos .peer .ChaincodeShim .PutStateInfo ;
53
55
import org .hyperledger .fabric .protos .peer .ChaincodeShim .QueryResponse ;
@@ -217,19 +219,19 @@ private void beforeRegistered(Event event) {
217
219
private void handleInit (ChaincodeMessage message ) {
218
220
new Thread (() -> {
219
221
try {
220
-
222
+
221
223
// Get the function and args from Payload
222
224
final ChaincodeInput input = ChaincodeInput .parseFrom (message .getPayload ());
223
-
225
+
224
226
// Mark as a transaction (allow put/del state)
225
227
markIsTransaction (message .getTxid (), true );
226
-
228
+
227
229
// Create the ChaincodeStub which the chaincode can use to callback
228
230
final ChaincodeStub stub = new ChaincodeStubImpl (message .getTxid (), this , input .getArgsList ());
229
-
231
+
230
232
// Call chaincode's init
231
233
final Response result = chaincode .init (stub );
232
-
234
+
233
235
if (result .getStatus () == Status .SUCCESS_VALUE ) {
234
236
// Send COMPLETED with entire result as payload
235
237
logger .debug (String .format (String .format ("[%s]Init succeeded. Sending %s" , shortID (message ), COMPLETED )));
@@ -239,7 +241,7 @@ private void handleInit(ChaincodeMessage message) {
239
241
logger .error (String .format ("[%s]Init failed. Sending %s" , shortID (message ), ERROR ));
240
242
triggerNextState (newErrorEventMessage (message .getTxid (), result .getMessage (), stub .getEvent ()), true );
241
243
}
242
-
244
+
243
245
} catch (InvalidProtocolBufferException | RuntimeException e ) {
244
246
logger .error (String .format ("[%s]Init failed. Sending %s" , shortID (message ), ERROR ), e );
245
247
triggerNextState (ChaincodeMessage .newBuilder ()
@@ -273,19 +275,19 @@ private void beforeInit(Event event) {
273
275
private void handleTransaction (ChaincodeMessage message ) {
274
276
new Thread (() -> {
275
277
try {
276
-
278
+
277
279
// Get the function and args from Payload
278
280
final ChaincodeInput input = ChaincodeInput .parseFrom (message .getPayload ());
279
-
281
+
280
282
// Mark as a transaction (allow put/del state)
281
283
markIsTransaction (message .getTxid (), true );
282
-
284
+
283
285
// Create the ChaincodeStub which the chaincode can use to callback
284
286
final ChaincodeStub stub = new ChaincodeStubImpl (message .getTxid (), this , input .getArgsList ());
285
-
287
+
286
288
// Call chaincode's invoke
287
289
final Response result = chaincode .invoke (stub );
288
-
290
+
289
291
if (result .getStatus () == Status .SUCCESS_VALUE ) {
290
292
// Send COMPLETED with entire result as payload
291
293
logger .debug (String .format (String .format ("[%s]Invoke succeeded. Sending %s" , shortID (message ), COMPLETED )));
@@ -295,7 +297,7 @@ private void handleTransaction(ChaincodeMessage message) {
295
297
logger .error (String .format ("[%s]Invoke failed. Sending %s" , shortID (message ), ERROR ));
296
298
triggerNextState (newErrorEventMessage (message .getTxid (), result .getMessage (), stub .getEvent ()), true );
297
299
}
298
-
300
+
299
301
} catch (InvalidProtocolBufferException | RuntimeException e ) {
300
302
logger .error (String .format ("[%s]Invoke failed. Sending %s" , shortID (message ), ERROR ), e );
301
303
triggerNextState (ChaincodeMessage .newBuilder ()
@@ -591,6 +593,12 @@ void queryStateClose(String txId, String queryId) {
591
593
.build ().toByteString ());
592
594
}
593
595
596
+ QueryResponse handleGetQueryResult (String txId , String query ) {
597
+ return invokeQueryResponseMessage (txId , GET_QUERY_RESULT , GetQueryResult .newBuilder ()
598
+ .setQuery (query )
599
+ .build ().toByteString ());
600
+ }
601
+
594
602
private QueryResponse invokeQueryResponseMessage (String txId , ChaincodeMessage .Type type , ByteString payload ) {
595
603
try {
596
604
return QueryResponse .parseFrom (invokeChaincodeSupport (txId , type , payload )
@@ -672,19 +680,19 @@ Response handleInvokeChaincode(String chaincodeName, List<byte[]> args, String t
672
680
try {
673
681
// create the channel on which to communicate the response from validating peer
674
682
final Channel <ChaincodeMessage > responseChannel = createChannel (txid );
675
-
683
+
676
684
// Send INVOKE_CHAINCODE message to validator chaincode support
677
685
final ChaincodeMessage message = HandlerHelper .newInvokeChaincodeMessage (txid , invocationSpec .toByteString ());
678
686
logger .debug (String .format ("[%s]Sending %s" , shortID (message ), INVOKE_CHAINCODE ));
679
687
serialSend (message );
680
688
681
689
// wait for response chaincode message
682
690
final ChaincodeMessage outerResponseMessage = receiveChannel (responseChannel );
683
-
691
+
684
692
if (outerResponseMessage == null ) {
685
693
return ChaincodeHelper .newInternalServerErrorResponse ("chaincode invoke returned null" );
686
694
}
687
-
695
+
688
696
logger .debug (String .format ("[%s]Received %s." , shortID (outerResponseMessage .getTxid ()), outerResponseMessage .getType ()));
689
697
690
698
switch (outerResponseMessage .getType ()) {
@@ -695,7 +703,7 @@ Response handleInvokeChaincode(String chaincodeName, List<byte[]> args, String t
695
703
logger .debug (String .format ("[%s]Received %s." , shortID (responseMessage .getTxid ()), responseMessage .getType ()));
696
704
if (responseMessage .getType () == COMPLETED ) {
697
705
// success
698
- return Response .parseFrom (responseMessage .getPayload ());
706
+ return Response .parseFrom (responseMessage .getPayload ());
699
707
} else {
700
708
// error
701
709
return ChaincodeHelper .newInternalServerErrorResponse (responseMessage .getPayload ().toByteArray ());
0 commit comments