diff --git a/core/chaincode/handler.go b/core/chaincode/handler.go index 240e414d651..591f66f0b40 100644 --- a/core/chaincode/handler.go +++ b/core/chaincode/handler.go @@ -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) } @@ -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) } diff --git a/peer/chaincode/common_test.go b/peer/chaincode/common_test.go index 33dc564ffbe..2de1dde15c9 100644 --- a/peer/chaincode/common_test.go +++ b/peer/chaincode/common_test.go @@ -17,8 +17,10 @@ package chaincode import ( + "encoding/json" "testing" + pb "github.com/hyperledger/fabric/protos/peer" "github.com/stretchr/testify/require" ) @@ -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 + } +} diff --git a/protos/peer/chaincodeunmarshall.go b/protos/peer/chaincodeunmarshall.go new file mode 100644 index 00000000000..97ae74e177d --- /dev/null +++ b/protos/peer/chaincodeunmarshall.go @@ -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 +}