Skip to content

Commit

Permalink
[FAB-2980] Replace gob with proto for QueryResult
Browse files Browse the repository at this point in the history
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 <cendhu@gmail.com>
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
cendhu authored and Luis Sanchez committed Apr 20, 2017
1 parent 7e401a1 commit 214489e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
13 changes: 1 addition & 12 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package chaincode

import (
"bytes"
"encoding/gob"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -761,16 +760,6 @@ func (handler *Handler) handleGetStateByRange(msg *pb.ChaincodeMessage) {
}()
}

func getBytes(qresult interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(qresult)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

//getQueryResponse takes an iterator and fetch state to construct QueryResponse
func getQueryResponse(handler *Handler, txContext *transactionContext, iter commonledger.ResultsIterator,
iterID string) (*pb.QueryResponse, error) {
Expand All @@ -790,7 +779,7 @@ func getQueryResponse(handler *Handler, txContext *transactionContext, iter comm
break
}
var resultBytes []byte
resultBytes, err = getBytes(queryResult)
resultBytes, err = proto.Marshal(queryResult.(proto.Message))
if err != nil {
return nil, err
}
Expand Down
22 changes: 12 additions & 10 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.
package shim

import (
"bytes"
"encoding/gob"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -491,11 +489,17 @@ func getStateByPartialCompositeKey(stub ChaincodeStubInterface, objectType strin

func (iter *StateQueryIterator) Next() (*queryresult.KV, error) {
result, err := next(iter.CommonIterator, STATE_QUERY_RESULT)
if err != nil {
return nil, err
}
return result.(*queryresult.KV), err
}

func (iter *HistoryQueryIterator) Next() (*queryresult.KeyModification, error) {
result, err := next(iter.CommonIterator, HISTORY_QUERY_RESULT)
if err != nil {
return nil, err
}
return result.(*queryresult.KeyModification), err
}

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

decoder := gob.NewDecoder(bytes.NewBuffer(queryResultBytes.ResultBytes))

if rType == STATE_QUERY_RESULT {
var stateQueryResult queryresult.KV
if err := decoder.Decode(&stateQueryResult); err != nil {
stateQueryResult := &queryresult.KV{}
if err := proto.Unmarshal(queryResultBytes.ResultBytes, stateQueryResult); err != nil {
return nil, err
}
iter.currentLoc++
return &stateQueryResult, nil
return stateQueryResult, nil

} else if rType == HISTORY_QUERY_RESULT {
var historyQueryResult queryresult.KeyModification
if err := decoder.Decode(&historyQueryResult); err != nil {
historyQueryResult := &queryresult.KeyModification{}
if err := proto.Unmarshal(queryResultBytes.ResultBytes, historyQueryResult); err != nil {
return nil, err
}
iter.currentLoc++
return &historyQueryResult, nil
return historyQueryResult, nil

}
return nil, errors.New("Wrong result type")
Expand Down

0 comments on commit 214489e

Please sign in to comment.