Skip to content

Commit f6a6003

Browse files
author
Srinivasan Muralidharan
committed
FAB-390 let Mock chaincode UT framework implement [][]byte arguments
Mock chaincode implements (function string, args []string) interfaces for backward compatibility. We need to move away from this so we can implement []byte arguments using new API. Change-Id: I5751b6a16d1a668bcc6156b47e221e43554e27f9 Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent 37837fd commit f6a6003

File tree

6 files changed

+62
-64
lines changed

6 files changed

+62
-64
lines changed

core/chaincode/shim/mockstub.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,26 @@ func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub
103103
}
104104

105105
// Initialise this chaincode, also starts and ends a transaction.
106-
func (stub *MockStub) MockInit(uuid string, function string, args []string) ([]byte, error) {
107-
stub.args = getBytes(function, args)
106+
func (stub *MockStub) MockInit(uuid string, args [][]byte) ([]byte, error) {
107+
stub.args = args
108108
stub.MockTransactionStart(uuid)
109109
bytes, err := stub.cc.Init(stub)
110110
stub.MockTransactionEnd(uuid)
111111
return bytes, err
112112
}
113113

114114
// Invoke this chaincode, also starts and ends a transaction.
115-
func (stub *MockStub) MockInvoke(uuid string, function string, args []string) ([]byte, error) {
116-
stub.args = getBytes(function, args)
115+
func (stub *MockStub) MockInvoke(uuid string, args [][]byte) ([]byte, error) {
116+
stub.args = args
117117
stub.MockTransactionStart(uuid)
118118
bytes, err := stub.cc.Invoke(stub)
119119
stub.MockTransactionEnd(uuid)
120120
return bytes, err
121121
}
122122

123123
// Query this chaincode
124-
func (stub *MockStub) MockQuery(function string, args []string) ([]byte, error) {
125-
stub.args = getBytes(function, args)
124+
func (stub *MockStub) MockQuery(args [][]byte) ([]byte, error) {
125+
stub.args = args
126126
// no transaction needed for queries
127127
bytes, err := stub.cc.Query(stub)
128128
return bytes, err
@@ -258,11 +258,10 @@ func (stub *MockStub) DeleteRow(tableName string, key []Column) error {
258258
// and register it with stub1 by calling stub1.MockPeerChaincode("stub2Hash", stub2)
259259
func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error) {
260260
// TODO "args" here should possibly be a serialized pb.ChaincodeInput
261-
function, params := getFuncArgs(args)
262261
otherStub := stub.Invokables[chaincodeName]
263262
mockLogger.Debug("MockStub", stub.Name, "Invoking peer chaincode", otherStub.Name, args)
264263
// function, strings := getFuncArgs(args)
265-
bytes, err := otherStub.MockInvoke(stub.Uuid, function, params)
264+
bytes, err := otherStub.MockInvoke(stub.Uuid, args)
266265
mockLogger.Debug("MockStub", stub.Name, "Invoked peer chaincode", otherStub.Name, "got", bytes, err)
267266
return bytes, err
268267
}
@@ -276,8 +275,7 @@ func (stub *MockStub) QueryChaincode(chaincodeName string, args [][]byte) ([]byt
276275
return nil, errors.New("Could not find peer chaincode to query")
277276
}
278277
mockLogger.Debug("MockStub", stub.Name, "Querying peer chaincode", otherStub.Name, args)
279-
function, params := getFuncArgs(args)
280-
bytes, err := otherStub.MockQuery(function, params)
278+
bytes, err := otherStub.MockQuery(args)
281279
mockLogger.Debug("MockStub", stub.Name, "Queried peer chaincode", otherStub.Name, "got", bytes, err)
282280
return bytes, err
283281
}

examples/chaincode/go/chaincode_example02/chaincode_example02_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/hyperledger/fabric/core/chaincode/shim"
2323
)
2424

25-
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
26-
_, err := stub.MockInit("1", "init", args)
25+
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
26+
_, err := stub.MockInit("1", args)
2727
if err != nil {
2828
fmt.Println("Init failed", err)
2929
t.FailNow()
@@ -43,7 +43,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
4343
}
4444

4545
func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
46-
bytes, err := stub.MockQuery("query", []string{name})
46+
bytes, err := stub.MockQuery([][]byte{[]byte("query"), []byte(name)})
4747
if err != nil {
4848
fmt.Println("Query", name, "failed", err)
4949
t.FailNow()
@@ -58,8 +58,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
5858
}
5959
}
6060

61-
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
62-
_, err := stub.MockInvoke("1", "query", args)
61+
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
62+
_, err := stub.MockInvoke("1", args)
6363
if err != nil {
6464
fmt.Println("Invoke", args, "failed", err)
6565
t.FailNow()
@@ -71,7 +71,7 @@ func TestExample02_Init(t *testing.T) {
7171
stub := shim.NewMockStub("ex02", scc)
7272

7373
// Init A=123 B=234
74-
checkInit(t, stub, []string{"A", "123", "B", "234"})
74+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")})
7575

7676
checkState(t, stub, "A", "123")
7777
checkState(t, stub, "B", "234")
@@ -82,7 +82,7 @@ func TestExample02_Query(t *testing.T) {
8282
stub := shim.NewMockStub("ex02", scc)
8383

8484
// Init A=345 B=456
85-
checkInit(t, stub, []string{"A", "345", "B", "456"})
85+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345"), []byte("B"), []byte("456")})
8686

8787
// Query A
8888
checkQuery(t, stub, "A", "345")
@@ -96,17 +96,17 @@ func TestExample02_Invoke(t *testing.T) {
9696
stub := shim.NewMockStub("ex02", scc)
9797

9898
// Init A=567 B=678
99-
checkInit(t, stub, []string{"A", "567", "B", "678"})
99+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")})
100100

101101
// Invoke A->B for 123
102-
checkInvoke(t, stub, []string{"A", "B", "123"})
102+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")})
103103
checkQuery(t, stub, "A", "444")
104104
checkQuery(t, stub, "B", "801")
105105

106106
// Invoke B->A for 234
107-
checkInvoke(t, stub, []string{"B", "A", "234"})
107+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")})
108+
checkQuery(t, stub, "A", "678")
109+
checkQuery(t, stub, "B", "567")
108110
checkQuery(t, stub, "A", "678")
109111
checkQuery(t, stub, "B", "567")
110-
checkState(t, stub, "A", "678")
111-
checkState(t, stub, "B", "567")
112112
}

examples/chaincode/go/chaincode_example03/chaincode_example03_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/hyperledger/fabric/core/chaincode/shim"
2323
)
2424

25-
func checkInit(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string) {
26-
_, err := stub.MockInit("1", "init", args)
25+
func checkInit(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
26+
_, err := stub.MockInit("1", args)
2727
if err != nil {
2828
fmt.Println("Init failed", err)
2929
t.FailNow()
@@ -42,8 +42,8 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
4242
}
4343
}
4444

45-
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string, value string) {
46-
_, err := stub.MockInit("1", "query", args)
45+
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
46+
_, err := stub.MockInit("1", args)
4747
bytes, err := scc.Query(stub)
4848
if err != nil {
4949
// expected failure
@@ -62,8 +62,8 @@ func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []
6262
}
6363
}
6464

65-
func checkInvoke(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string) {
66-
_, err := stub.MockInvoke("1", "query", args)
65+
func checkInvoke(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
66+
_, err := stub.MockInvoke("1", args)
6767
if err != nil {
6868
fmt.Println("Invoke", args, "failed", err)
6969
t.FailNow()
@@ -75,7 +75,7 @@ func TestExample03_Init(t *testing.T) {
7575
stub := shim.NewMockStub("ex03", scc)
7676

7777
// Init A=123 B=234
78-
checkInit(t, scc, stub, []string{"A", "123"})
78+
checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123")})
7979

8080
checkState(t, stub, "A", "123")
8181
}
@@ -85,10 +85,10 @@ func TestExample03_Query(t *testing.T) {
8585
stub := shim.NewMockStub("ex03", scc)
8686

8787
// Init A=345 B=456
88-
checkInit(t, scc, stub, []string{"A", "345"})
88+
checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345")})
8989

9090
// Query A
91-
checkQuery(t, scc, stub, []string{"A", "345"}, "345")
91+
checkQuery(t, scc, stub, [][]byte{[]byte("query"), []byte("A"), []byte("345")})
9292
}
9393

9494
func TestExample03_Invoke(t *testing.T) {

examples/chaincode/go/chaincode_example04/chaincode_example04.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type SimpleChaincode struct {
3434
func (t *SimpleChaincode) GetChaincodeToCall() string {
3535
//This is the hashcode for github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02
3636
//if the example is modifed this hashcode will change!!
37-
chainCodeToCall := "5e4584bebfabb2353042abd98bae4fa569e2cb41117f53adce8673702cdbabe7695d7b8ff254fb1e0cb86d48b4753ae08f06a84b5fa267dce753b3f420f5e273"
37+
chainCodeToCall := "dbf03d6840375cf5dd188a745311a34f6c449cfe5e0d5dbfffa1b22106b08bbb64669662684e71d9c1c2f476e5ba24ff35b93314e35b09124ea72c8297480be8"
3838
return chainCodeToCall
3939
}
4040

examples/chaincode/go/chaincode_example04/chaincode_example04_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
// this is the response to any successful Invoke() on chaincode_example04
2727
var eventResponse = "{\"Name\":\"Event\",\"Amount\":\"1\"}"
2828

29-
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
30-
_, err := stub.MockInit("1", "init", args)
29+
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
30+
_, err := stub.MockInit("1", args)
3131
if err != nil {
3232
fmt.Println("Init failed", err)
3333
t.FailNow()
@@ -47,7 +47,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
4747
}
4848

4949
func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
50-
bytes, err := stub.MockQuery("query", []string{name})
50+
bytes, err := stub.MockQuery([][]byte{[]byte("query"), []byte(name)})
5151
if err != nil {
5252
fmt.Println("Query", name, "failed", err)
5353
t.FailNow()
@@ -62,8 +62,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
6262
}
6363
}
6464

65-
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
66-
_, err := stub.MockInvoke("1", "query", args)
65+
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
66+
_, err := stub.MockInvoke("1", args)
6767
if err != nil {
6868
fmt.Println("Invoke", args, "failed", err)
6969
t.FailNow()
@@ -75,7 +75,7 @@ func TestExample04_Init(t *testing.T) {
7575
stub := shim.NewMockStub("ex04", scc)
7676

7777
// Init A=123 B=234
78-
checkInit(t, stub, []string{"Event", "123"})
78+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("123")})
7979

8080
checkState(t, stub, "Event", "123")
8181
}
@@ -85,7 +85,7 @@ func TestExample04_Query(t *testing.T) {
8585
stub := shim.NewMockStub("ex04", scc)
8686

8787
// Init A=345 B=456
88-
checkInit(t, stub, []string{"Event", "1"})
88+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
8989

9090
// Query A
9191
checkQuery(t, stub, "Event", eventResponse)
@@ -97,20 +97,20 @@ func TestExample04_Invoke(t *testing.T) {
9797

9898
ccEx2 := new(ex02.SimpleChaincode)
9999
stubEx2 := shim.NewMockStub("ex02", ccEx2)
100-
checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
100+
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
101101
stub.MockPeerChaincode(scc.GetChaincodeToCall(), stubEx2)
102102

103103
// Init A=567 B=678
104-
checkInit(t, stub, []string{"Event", "1"})
104+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
105105

106106
// Invoke A->B for 10 via Example04's chaincode
107-
checkInvoke(t, stub, []string{"Event", "1"})
107+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
108108
checkQuery(t, stub, "Event", eventResponse)
109109
checkQuery(t, stubEx2, "a", "101")
110110
checkQuery(t, stubEx2, "b", "232")
111111

112112
// Invoke A->B for 10 via Example04's chaincode
113-
checkInvoke(t, stub, []string{"Event", "1"})
113+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
114114
checkQuery(t, stub, "Event", eventResponse)
115115
checkQuery(t, stubEx2, "a", "91")
116116
checkQuery(t, stubEx2, "b", "242")

examples/chaincode/go/chaincode_example05/chaincode_example05_test.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func jsonResponse(name string, value string) string {
3232
return fmt.Sprintf("jsonResponse = \"{\"Name\":\"%v\",\"Value\":\"%v\"}", name, value)
3333
}
3434

35-
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
36-
_, err := stub.MockInit("1", "init", args)
35+
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
36+
_, err := stub.MockInit("1", args)
3737
if err != nil {
3838
fmt.Println("Init failed", err)
3939
t.FailNow()
@@ -52,8 +52,8 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, expect string) {
5252
}
5353
}
5454

55-
func checkQuery(t *testing.T, stub *shim.MockStub, args []string, expect string) {
56-
bytes, err := stub.MockQuery("query", args)
55+
func checkQuery(t *testing.T, stub *shim.MockStub, args [][]byte, expect string) {
56+
bytes, err := stub.MockQuery(args)
5757
if err != nil {
5858
fmt.Println("Query", args, "failed", err)
5959
t.FailNow()
@@ -68,8 +68,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, args []string, expect string)
6868
}
6969
}
7070

71-
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
72-
_, err := stub.MockInvoke("1", "query", args)
71+
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
72+
_, err := stub.MockInvoke("1", args)
7373
if err != nil {
7474
fmt.Println("Invoke", args, "failed", err)
7575
t.FailNow()
@@ -81,7 +81,7 @@ func TestExample04_Init(t *testing.T) {
8181
stub := shim.NewMockStub("ex05", scc)
8282

8383
// Init A=123 B=234
84-
checkInit(t, stub, []string{"sumStoreName", "432"})
84+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("432")})
8585

8686
checkState(t, stub, "sumStoreName", "432")
8787
}
@@ -92,13 +92,13 @@ func TestExample04_Query(t *testing.T) {
9292

9393
ccEx2 := new(ex02.SimpleChaincode)
9494
stubEx2 := shim.NewMockStub("ex02", ccEx2)
95-
checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
95+
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
9696
stub.MockPeerChaincode(example02Url, stubEx2)
9797

98-
checkInit(t, stub, []string{"sumStoreName", "0"})
98+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
9999

100100
// a + b = 111 + 222 = 333
101-
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "333") // example05 doesn't return JSON?
101+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "333") // example05 doesn't return JSON?
102102
}
103103

104104
func TestExample04_Invoke(t *testing.T) {
@@ -107,23 +107,23 @@ func TestExample04_Invoke(t *testing.T) {
107107

108108
ccEx2 := new(ex02.SimpleChaincode)
109109
stubEx2 := shim.NewMockStub("ex02", ccEx2)
110-
checkInit(t, stubEx2, []string{"a", "222", "b", "333"})
110+
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")})
111111
stub.MockPeerChaincode(example02Url, stubEx2)
112112

113-
checkInit(t, stub, []string{"sumStoreName", "0"})
113+
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
114114

115115
// a + b = 222 + 333 = 555
116-
checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
117-
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
118-
checkQuery(t, stubEx2, []string{"a"}, "222")
119-
checkQuery(t, stubEx2, []string{"b"}, "333")
116+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
117+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
118+
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222")
119+
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333")
120120

121121
// update A-=10 and B+=10
122-
checkInvoke(t, stubEx2, []string{"a", "b", "10"})
122+
checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")})
123123

124124
// a + b = 212 + 343 = 555
125-
checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
126-
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
127-
checkQuery(t, stubEx2, []string{"a"}, "212")
128-
checkQuery(t, stubEx2, []string{"b"}, "343")
125+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
126+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
127+
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212")
128+
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343")
129129
}

0 commit comments

Comments
 (0)