Skip to content

Commit 214489e

Browse files
cendhuLuis Sanchez
authored and
Luis Sanchez
committed
[FAB-2980] Replace gob with proto for QueryResult
QueryResults (i.e., KV and KeyModification structs) are serialized using gob in the peer and deserialized in golang chaincode shim. As gob encoding is not supported in java, this CR replaces gob with proto (provided that QueryResults are defined as protobuf message -- https://gerrit.hyperledger.org/r/#/c/7687/). Change-Id: Ice9133fdc30a59397aa0670050401502150f60f5 Signed-off-by: senthil <[email protected]> Signed-off-by: Luis Sanchez <[email protected]>
1 parent 7e401a1 commit 214489e

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

core/chaincode/handler.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package chaincode
1818

1919
import (
2020
"bytes"
21-
"encoding/gob"
2221
"fmt"
2322
"io"
2423
"sync"
@@ -761,16 +760,6 @@ func (handler *Handler) handleGetStateByRange(msg *pb.ChaincodeMessage) {
761760
}()
762761
}
763762

764-
func getBytes(qresult interface{}) ([]byte, error) {
765-
var buf bytes.Buffer
766-
enc := gob.NewEncoder(&buf)
767-
err := enc.Encode(qresult)
768-
if err != nil {
769-
return nil, err
770-
}
771-
return buf.Bytes(), nil
772-
}
773-
774763
//getQueryResponse takes an iterator and fetch state to construct QueryResponse
775764
func getQueryResponse(handler *Handler, txContext *transactionContext, iter commonledger.ResultsIterator,
776765
iterID string) (*pb.QueryResponse, error) {
@@ -790,7 +779,7 @@ func getQueryResponse(handler *Handler, txContext *transactionContext, iter comm
790779
break
791780
}
792781
var resultBytes []byte
793-
resultBytes, err = getBytes(queryResult)
782+
resultBytes, err = proto.Marshal(queryResult.(proto.Message))
794783
if err != nil {
795784
return nil, err
796785
}

core/chaincode/shim/chaincode.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ limitations under the License.
1919
package shim
2020

2121
import (
22-
"bytes"
23-
"encoding/gob"
2422
"errors"
2523
"flag"
2624
"fmt"
@@ -491,11 +489,17 @@ func getStateByPartialCompositeKey(stub ChaincodeStubInterface, objectType strin
491489

492490
func (iter *StateQueryIterator) Next() (*queryresult.KV, error) {
493491
result, err := next(iter.CommonIterator, STATE_QUERY_RESULT)
492+
if err != nil {
493+
return nil, err
494+
}
494495
return result.(*queryresult.KV), err
495496
}
496497

497498
func (iter *HistoryQueryIterator) Next() (*queryresult.KeyModification, error) {
498499
result, err := next(iter.CommonIterator, HISTORY_QUERY_RESULT)
500+
if err != nil {
501+
return nil, err
502+
}
499503
return result.(*queryresult.KeyModification), err
500504
}
501505

@@ -515,23 +519,21 @@ func (iter *CommonIterator) HasNext() bool {
515519
func getResultFromBytes(queryResultBytes *pb.QueryResultBytes, iter *CommonIterator,
516520
rType resultType) (commonledger.QueryResult, error) {
517521

518-
decoder := gob.NewDecoder(bytes.NewBuffer(queryResultBytes.ResultBytes))
519-
520522
if rType == STATE_QUERY_RESULT {
521-
var stateQueryResult queryresult.KV
522-
if err := decoder.Decode(&stateQueryResult); err != nil {
523+
stateQueryResult := &queryresult.KV{}
524+
if err := proto.Unmarshal(queryResultBytes.ResultBytes, stateQueryResult); err != nil {
523525
return nil, err
524526
}
525527
iter.currentLoc++
526-
return &stateQueryResult, nil
528+
return stateQueryResult, nil
527529

528530
} else if rType == HISTORY_QUERY_RESULT {
529-
var historyQueryResult queryresult.KeyModification
530-
if err := decoder.Decode(&historyQueryResult); err != nil {
531+
historyQueryResult := &queryresult.KeyModification{}
532+
if err := proto.Unmarshal(queryResultBytes.ResultBytes, historyQueryResult); err != nil {
531533
return nil, err
532534
}
533535
iter.currentLoc++
534-
return &historyQueryResult, nil
536+
return historyQueryResult, nil
535537

536538
}
537539
return nil, errors.New("Wrong result type")

0 commit comments

Comments
 (0)