Skip to content

Commit 1b844c2

Browse files
author
Srinivasan Muralidharan
committed
FAB-1200 wrong type assertion on ledger.KV
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 <[email protected]>
1 parent 61affa0 commit 1b844c2

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

core/chaincode/handler.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,7 @@ func (handler *Handler) handleRangeQueryState(msg *pb.ChaincodeMessage) {
636636
if qresult == nil {
637637
break
638638
}
639-
//PDMP - let it panic if not KV
640-
kv := qresult.(ledger.KV)
639+
kv := qresult.(*ledger.KV)
641640
keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value}
642641
keysAndValues = append(keysAndValues, &keyAndValue)
643642
}
@@ -735,8 +734,7 @@ func (handler *Handler) handleRangeQueryStateNext(msg *pb.ChaincodeMessage) {
735734
if qresult != nil {
736735
break
737736
}
738-
//PDMP - let it panic if not KV
739-
kv := qresult.(ledger.KV)
737+
kv := qresult.(*ledger.KV)
740738
keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value}
741739
keysAndValues = append(keysAndValues, &keyAndValue)
742740
}

peer/chaincode/common_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package chaincode
1818

1919
import (
20+
"encoding/json"
2021
"testing"
2122

23+
pb "github.com/hyperledger/fabric/protos/peer"
2224
"github.com/stretchr/testify/require"
2325
)
2426

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

7779
require.Error(result)
7880
}
81+
82+
func TestCheckValidJSON(t *testing.T) {
83+
validJSON := `{"Args":["a","b","c"]}`
84+
input := &pb.ChaincodeInput{}
85+
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
86+
t.Fail()
87+
t.Logf("Chaincode argument error: %s", err)
88+
return
89+
}
90+
91+
validJSON = `{"Function":"f", "Args":["a","b","c"]}`
92+
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
93+
t.Fail()
94+
t.Logf("Chaincode argument error: %s", err)
95+
return
96+
}
97+
98+
validJSON = `{"Function":"f", "Args":[]}`
99+
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
100+
t.Fail()
101+
t.Logf("Chaincode argument error: %s", err)
102+
return
103+
}
104+
105+
validJSON = `{"Function":"f"}`
106+
if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
107+
t.Fail()
108+
t.Logf("Chaincode argument error: %s", err)
109+
return
110+
}
111+
}
112+
113+
func TestCheckInvalidJSON(t *testing.T) {
114+
invalidJSON := `{["a","b","c"]}`
115+
input := &pb.ChaincodeInput{}
116+
if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
117+
t.Fail()
118+
t.Logf("Bar argument error should have been caught: %s", invalidJSON)
119+
return
120+
}
121+
122+
invalidJSON = `{"Function":}`
123+
if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
124+
t.Fail()
125+
t.Logf("Chaincode argument error: %s", err)
126+
t.Logf("Bar argument error should have been caught: %s", invalidJSON)
127+
return
128+
}
129+
}

protos/peer/chaincodeunmarshall.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package peer
18+
19+
import (
20+
"encoding/json"
21+
22+
"github.com/hyperledger/fabric/core/util"
23+
)
24+
25+
type strArgs struct {
26+
Function string
27+
Args []string
28+
}
29+
30+
// UnmarshalJSON converts the string-based REST/JSON input to
31+
// the []byte-based current ChaincodeInput structure.
32+
func (c *ChaincodeInput) UnmarshalJSON(b []byte) error {
33+
sa := &strArgs{}
34+
err := json.Unmarshal(b, sa)
35+
if err != nil {
36+
return err
37+
}
38+
allArgs := sa.Args
39+
if sa.Function != "" {
40+
allArgs = append([]string{sa.Function}, sa.Args...)
41+
}
42+
c.Args = util.ToChaincodeArgs(allArgs...)
43+
return nil
44+
}

0 commit comments

Comments
 (0)