Commit f6a60035 authored by Srinivasan Muralidharan's avatar Srinivasan Muralidharan
Browse files

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: default avatarSrinivasan Muralidharan <muralisr@us.ibm.com>
parent 37837fdf
......@@ -103,8 +103,8 @@ func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub
}
// Initialise this chaincode, also starts and ends a transaction.
func (stub *MockStub) MockInit(uuid string, function string, args []string) ([]byte, error) {
stub.args = getBytes(function, args)
func (stub *MockStub) MockInit(uuid string, args [][]byte) ([]byte, error) {
stub.args = args
stub.MockTransactionStart(uuid)
bytes, err := stub.cc.Init(stub)
stub.MockTransactionEnd(uuid)
......@@ -112,8 +112,8 @@ func (stub *MockStub) MockInit(uuid string, function string, args []string) ([]b
}
// Invoke this chaincode, also starts and ends a transaction.
func (stub *MockStub) MockInvoke(uuid string, function string, args []string) ([]byte, error) {
stub.args = getBytes(function, args)
func (stub *MockStub) MockInvoke(uuid string, args [][]byte) ([]byte, error) {
stub.args = args
stub.MockTransactionStart(uuid)
bytes, err := stub.cc.Invoke(stub)
stub.MockTransactionEnd(uuid)
......@@ -121,8 +121,8 @@ func (stub *MockStub) MockInvoke(uuid string, function string, args []string) ([
}
// Query this chaincode
func (stub *MockStub) MockQuery(function string, args []string) ([]byte, error) {
stub.args = getBytes(function, args)
func (stub *MockStub) MockQuery(args [][]byte) ([]byte, error) {
stub.args = args
// no transaction needed for queries
bytes, err := stub.cc.Query(stub)
return bytes, err
......@@ -258,11 +258,10 @@ func (stub *MockStub) DeleteRow(tableName string, key []Column) error {
// and register it with stub1 by calling stub1.MockPeerChaincode("stub2Hash", stub2)
func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error) {
// TODO "args" here should possibly be a serialized pb.ChaincodeInput
function, params := getFuncArgs(args)
otherStub := stub.Invokables[chaincodeName]
mockLogger.Debug("MockStub", stub.Name, "Invoking peer chaincode", otherStub.Name, args)
// function, strings := getFuncArgs(args)
bytes, err := otherStub.MockInvoke(stub.Uuid, function, params)
bytes, err := otherStub.MockInvoke(stub.Uuid, args)
mockLogger.Debug("MockStub", stub.Name, "Invoked peer chaincode", otherStub.Name, "got", bytes, err)
return bytes, err
}
......@@ -276,8 +275,7 @@ func (stub *MockStub) QueryChaincode(chaincodeName string, args [][]byte) ([]byt
return nil, errors.New("Could not find peer chaincode to query")
}
mockLogger.Debug("MockStub", stub.Name, "Querying peer chaincode", otherStub.Name, args)
function, params := getFuncArgs(args)
bytes, err := otherStub.MockQuery(function, params)
bytes, err := otherStub.MockQuery(args)
mockLogger.Debug("MockStub", stub.Name, "Queried peer chaincode", otherStub.Name, "got", bytes, err)
return bytes, err
}
......
......@@ -22,8 +22,8 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
)
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInit("1", "init", args)
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
if err != nil {
fmt.Println("Init failed", err)
t.FailNow()
......@@ -43,7 +43,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
}
func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
bytes, err := stub.MockQuery("query", []string{name})
bytes, err := stub.MockQuery([][]byte{[]byte("query"), []byte(name)})
if err != nil {
fmt.Println("Query", name, "failed", err)
t.FailNow()
......@@ -58,8 +58,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
}
}
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInvoke("1", "query", args)
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInvoke("1", args)
if err != nil {
fmt.Println("Invoke", args, "failed", err)
t.FailNow()
......@@ -71,7 +71,7 @@ func TestExample02_Init(t *testing.T) {
stub := shim.NewMockStub("ex02", scc)
// Init A=123 B=234
checkInit(t, stub, []string{"A", "123", "B", "234"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")})
checkState(t, stub, "A", "123")
checkState(t, stub, "B", "234")
......@@ -82,7 +82,7 @@ func TestExample02_Query(t *testing.T) {
stub := shim.NewMockStub("ex02", scc)
// Init A=345 B=456
checkInit(t, stub, []string{"A", "345", "B", "456"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345"), []byte("B"), []byte("456")})
// Query A
checkQuery(t, stub, "A", "345")
......@@ -96,17 +96,17 @@ func TestExample02_Invoke(t *testing.T) {
stub := shim.NewMockStub("ex02", scc)
// Init A=567 B=678
checkInit(t, stub, []string{"A", "567", "B", "678"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")})
// Invoke A->B for 123
checkInvoke(t, stub, []string{"A", "B", "123"})
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")})
checkQuery(t, stub, "A", "444")
checkQuery(t, stub, "B", "801")
// Invoke B->A for 234
checkInvoke(t, stub, []string{"B", "A", "234"})
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")})
checkQuery(t, stub, "A", "678")
checkQuery(t, stub, "B", "567")
checkQuery(t, stub, "A", "678")
checkQuery(t, stub, "B", "567")
checkState(t, stub, "A", "678")
checkState(t, stub, "B", "567")
}
......@@ -22,8 +22,8 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
)
func checkInit(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string) {
_, err := stub.MockInit("1", "init", args)
func checkInit(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
if err != nil {
fmt.Println("Init failed", err)
t.FailNow()
......@@ -42,8 +42,8 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
}
}
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string, value string) {
_, err := stub.MockInit("1", "query", args)
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
bytes, err := scc.Query(stub)
if err != nil {
// expected failure
......@@ -62,8 +62,8 @@ func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []
}
}
func checkInvoke(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args []string) {
_, err := stub.MockInvoke("1", "query", args)
func checkInvoke(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInvoke("1", args)
if err != nil {
fmt.Println("Invoke", args, "failed", err)
t.FailNow()
......@@ -75,7 +75,7 @@ func TestExample03_Init(t *testing.T) {
stub := shim.NewMockStub("ex03", scc)
// Init A=123 B=234
checkInit(t, scc, stub, []string{"A", "123"})
checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123")})
checkState(t, stub, "A", "123")
}
......@@ -85,10 +85,10 @@ func TestExample03_Query(t *testing.T) {
stub := shim.NewMockStub("ex03", scc)
// Init A=345 B=456
checkInit(t, scc, stub, []string{"A", "345"})
checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345")})
// Query A
checkQuery(t, scc, stub, []string{"A", "345"}, "345")
checkQuery(t, scc, stub, [][]byte{[]byte("query"), []byte("A"), []byte("345")})
}
func TestExample03_Invoke(t *testing.T) {
......
......@@ -34,7 +34,7 @@ type SimpleChaincode struct {
func (t *SimpleChaincode) GetChaincodeToCall() string {
//This is the hashcode for github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02
//if the example is modifed this hashcode will change!!
chainCodeToCall := "5e4584bebfabb2353042abd98bae4fa569e2cb41117f53adce8673702cdbabe7695d7b8ff254fb1e0cb86d48b4753ae08f06a84b5fa267dce753b3f420f5e273"
chainCodeToCall := "dbf03d6840375cf5dd188a745311a34f6c449cfe5e0d5dbfffa1b22106b08bbb64669662684e71d9c1c2f476e5ba24ff35b93314e35b09124ea72c8297480be8"
return chainCodeToCall
}
......
......@@ -26,8 +26,8 @@ import (
// this is the response to any successful Invoke() on chaincode_example04
var eventResponse = "{\"Name\":\"Event\",\"Amount\":\"1\"}"
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInit("1", "init", args)
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
if err != nil {
fmt.Println("Init failed", err)
t.FailNow()
......@@ -47,7 +47,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
}
func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
bytes, err := stub.MockQuery("query", []string{name})
bytes, err := stub.MockQuery([][]byte{[]byte("query"), []byte(name)})
if err != nil {
fmt.Println("Query", name, "failed", err)
t.FailNow()
......@@ -62,8 +62,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, name string, value string) {
}
}
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInvoke("1", "query", args)
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInvoke("1", args)
if err != nil {
fmt.Println("Invoke", args, "failed", err)
t.FailNow()
......@@ -75,7 +75,7 @@ func TestExample04_Init(t *testing.T) {
stub := shim.NewMockStub("ex04", scc)
// Init A=123 B=234
checkInit(t, stub, []string{"Event", "123"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("123")})
checkState(t, stub, "Event", "123")
}
......@@ -85,7 +85,7 @@ func TestExample04_Query(t *testing.T) {
stub := shim.NewMockStub("ex04", scc)
// Init A=345 B=456
checkInit(t, stub, []string{"Event", "1"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
// Query A
checkQuery(t, stub, "Event", eventResponse)
......@@ -97,20 +97,20 @@ func TestExample04_Invoke(t *testing.T) {
ccEx2 := new(ex02.SimpleChaincode)
stubEx2 := shim.NewMockStub("ex02", ccEx2)
checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
stub.MockPeerChaincode(scc.GetChaincodeToCall(), stubEx2)
// Init A=567 B=678
checkInit(t, stub, []string{"Event", "1"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
// Invoke A->B for 10 via Example04's chaincode
checkInvoke(t, stub, []string{"Event", "1"})
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
checkQuery(t, stub, "Event", eventResponse)
checkQuery(t, stubEx2, "a", "101")
checkQuery(t, stubEx2, "b", "232")
// Invoke A->B for 10 via Example04's chaincode
checkInvoke(t, stub, []string{"Event", "1"})
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("Event"), []byte("1")})
checkQuery(t, stub, "Event", eventResponse)
checkQuery(t, stubEx2, "a", "91")
checkQuery(t, stubEx2, "b", "242")
......
......@@ -32,8 +32,8 @@ func jsonResponse(name string, value string) string {
return fmt.Sprintf("jsonResponse = \"{\"Name\":\"%v\",\"Value\":\"%v\"}", name, value)
}
func checkInit(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInit("1", "init", args)
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
if err != nil {
fmt.Println("Init failed", err)
t.FailNow()
......@@ -52,8 +52,8 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, expect string) {
}
}
func checkQuery(t *testing.T, stub *shim.MockStub, args []string, expect string) {
bytes, err := stub.MockQuery("query", args)
func checkQuery(t *testing.T, stub *shim.MockStub, args [][]byte, expect string) {
bytes, err := stub.MockQuery(args)
if err != nil {
fmt.Println("Query", args, "failed", err)
t.FailNow()
......@@ -68,8 +68,8 @@ func checkQuery(t *testing.T, stub *shim.MockStub, args []string, expect string)
}
}
func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
_, err := stub.MockInvoke("1", "query", args)
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInvoke("1", args)
if err != nil {
fmt.Println("Invoke", args, "failed", err)
t.FailNow()
......@@ -81,7 +81,7 @@ func TestExample04_Init(t *testing.T) {
stub := shim.NewMockStub("ex05", scc)
// Init A=123 B=234
checkInit(t, stub, []string{"sumStoreName", "432"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("432")})
checkState(t, stub, "sumStoreName", "432")
}
......@@ -92,13 +92,13 @@ func TestExample04_Query(t *testing.T) {
ccEx2 := new(ex02.SimpleChaincode)
stubEx2 := shim.NewMockStub("ex02", ccEx2)
checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
stub.MockPeerChaincode(example02Url, stubEx2)
checkInit(t, stub, []string{"sumStoreName", "0"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
// a + b = 111 + 222 = 333
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "333") // example05 doesn't return JSON?
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "333") // example05 doesn't return JSON?
}
func TestExample04_Invoke(t *testing.T) {
......@@ -107,23 +107,23 @@ func TestExample04_Invoke(t *testing.T) {
ccEx2 := new(ex02.SimpleChaincode)
stubEx2 := shim.NewMockStub("ex02", ccEx2)
checkInit(t, stubEx2, []string{"a", "222", "b", "333"})
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")})
stub.MockPeerChaincode(example02Url, stubEx2)
checkInit(t, stub, []string{"sumStoreName", "0"})
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
// a + b = 222 + 333 = 555
checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
checkQuery(t, stubEx2, []string{"a"}, "222")
checkQuery(t, stubEx2, []string{"b"}, "333")
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222")
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333")
// update A-=10 and B+=10
checkInvoke(t, stubEx2, []string{"a", "b", "10"})
checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")})
// a + b = 212 + 343 = 555
checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
checkQuery(t, stubEx2, []string{"a"}, "212")
checkQuery(t, stubEx2, []string{"b"}, "343")
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212")
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343")
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment