Skip to content

Commit

Permalink
FAB-1200 wrong type assertion on ledger.KV
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1200

While investing table structures (still in works) found that
chaincode handler was asserting the wrong KV type.

Also found that an UnmarshallJSON was missing on proto.ChaincodeInput.
This function was defined in transaction.go (wrong file for a
ChaincodeInput function!) which went away when old Transaction was
axed. Interesting that the tests in common_test.go did not catch that.
Added explicit tests for Unmarshalling JSON into ChaincodeInput.

Change-Id: I3adc92c888efee974c5f537bc911cb0a253d1e01
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Nov 28, 2016
1 parent 61affa0 commit 1b844c2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
6 changes: 2 additions & 4 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,7 @@ func (handler *Handler) handleRangeQueryState(msg *pb.ChaincodeMessage) {
if qresult == nil {
break
}
//PDMP - let it panic if not KV
kv := qresult.(ledger.KV)
kv := qresult.(*ledger.KV)
keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value}
keysAndValues = append(keysAndValues, &keyAndValue)
}
Expand Down Expand Up @@ -735,8 +734,7 @@ func (handler *Handler) handleRangeQueryStateNext(msg *pb.ChaincodeMessage) {
if qresult != nil {
break
}
//PDMP - let it panic if not KV
kv := qresult.(ledger.KV)
kv := qresult.(*ledger.KV)
keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value}
keysAndValues = append(keysAndValues, &keyAndValue)
}
Expand Down
51 changes: 51 additions & 0 deletions peer/chaincode/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package chaincode

import (
"encoding/json"
"testing"

pb "github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -76,3 +78,52 @@ func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) {

require.Error(result)
}

func TestCheckValidJSON(t *testing.T) {
validJSON := `{"Args":["a","b","c"]}`
input := &pb.ChaincodeInput{}
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
t.Fail()
t.Logf("Chaincode argument error: %s", err)
return
}

validJSON = `{"Function":"f", "Args":["a","b","c"]}`
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
t.Fail()
t.Logf("Chaincode argument error: %s", err)
return
}

validJSON = `{"Function":"f", "Args":[]}`
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
t.Fail()
t.Logf("Chaincode argument error: %s", err)
return
}

validJSON = `{"Function":"f"}`
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
t.Fail()
t.Logf("Chaincode argument error: %s", err)
return
}
}

func TestCheckInvalidJSON(t *testing.T) {
invalidJSON := `{["a","b","c"]}`
input := &pb.ChaincodeInput{}
if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
t.Fail()
t.Logf("Bar argument error should have been caught: %s", invalidJSON)
return
}

invalidJSON = `{"Function":}`
if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
t.Fail()
t.Logf("Chaincode argument error: %s", err)
t.Logf("Bar argument error should have been caught: %s", invalidJSON)
return
}
}
44 changes: 44 additions & 0 deletions protos/peer/chaincodeunmarshall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package peer

import (
"encoding/json"

"github.com/hyperledger/fabric/core/util"
)

type strArgs struct {
Function string
Args []string
}

// UnmarshalJSON converts the string-based REST/JSON input to
// the []byte-based current ChaincodeInput structure.
func (c *ChaincodeInput) UnmarshalJSON(b []byte) error {
sa := &strArgs{}
err := json.Unmarshal(b, sa)
if err != nil {
return err
}
allArgs := sa.Args
if sa.Function != "" {
allArgs = append([]string{sa.Function}, sa.Args...)
}
c.Args = util.ToChaincodeArgs(allArgs...)
return nil
}

0 comments on commit 1b844c2

Please sign in to comment.