From dadc939d3d3dd17174b17bb6936da75db3a187c8 Mon Sep 17 00:00:00 2001 From: "nishi.nidamarty" Date: Thu, 30 Mar 2017 17:37:48 -0400 Subject: [PATCH] FAB-2924 Update chaincode_example05 for clarity added passing in channel name as an argument to InvokeChaincode and tested chaincode calling chaincode in 1.0 successsfully updated chaincode_example05_test.go Change-Id: Ie291fb30d50c31807699698131ae019cd0e448f6 Signed-off-by: nishi.nidamarty --- .../chaincode_example05.go | 51 ++++++++++++++----- .../chaincode_example05_test.go | 27 +++++----- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/examples/chaincode/go/chaincode_example05/chaincode_example05.go b/examples/chaincode/go/chaincode_example05/chaincode_example05.go index 6578bd69c31..7b4cdb75819 100644 --- a/examples/chaincode/go/chaincode_example05/chaincode_example05.go +++ b/examples/chaincode/go/chaincode_example05/chaincode_example05.go @@ -61,21 +61,33 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { // Invoke queries another chaincode and updates its own state func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { - var sum string // Sum entity - var Aval, Bval, sumVal int // value of sum entity - to be computed + var sum, channelName string // Sum entity + var Aval, Bval, sumVal int // value of sum entity - to be computed var err error - if len(args) != 2 { - return shim.Error("Incorrect number of arguments. Expecting 2") + if len(args) < 2 { + return shim.Error("Incorrect number of arguments. Expecting atleast 2") } - chaincodeURL := args[0] // Expecting "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02" + chaincodeName := args[0] // Expecting name of the chaincode you would like to call, this name would be given during chaincode install time sum = args[1] + if len(args) > 2 { + channelName = args[2] + } else { + channelName = "" + } + // Query chaincode_example02 f := "query" queryArgs := util.ToChaincodeArgs(f, "a") - response := stub.InvokeChaincode(chaincodeURL, queryArgs, "") + + // if chaincode being invoked is on the same channel, + // then channel defaults to the current channel and args[2] can be "". + // If the chaincode being called is on a different channel, + // then you must specify the channel name in args[2] + + response := stub.InvokeChaincode(chaincodeName, queryArgs, channelName) if response.Status != shim.OK { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) fmt.Printf(errStr) @@ -89,7 +101,7 @@ func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string } queryArgs = util.ToChaincodeArgs(f, "b") - response = stub.InvokeChaincode(chaincodeURL, queryArgs, "") + response = stub.InvokeChaincode(chaincodeName, queryArgs, channelName) if response.Status != shim.OK { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) fmt.Printf(errStr) @@ -116,21 +128,32 @@ func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string } func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { - var sum string // Sum entity - var Aval, Bval, sumVal int // value of sum entity - to be computed + var sum, channelName string // Sum entity + var Aval, Bval, sumVal int // value of sum entity - to be computed var err error - if len(args) != 2 { - return shim.Error("Incorrect number of arguments. Expecting 2") + if len(args) < 2 { + return shim.Error("Incorrect number of arguments. Expecting atleast 2") } - chaincodeURL := args[0] + chaincodeName := args[0] // Expecting name of the chaincode you would like to call, this name would be given during chaincode install time sum = args[1] + if len(args) > 2 { + channelName = args[2] + } else { + channelName = "" + } + // Query chaincode_example02 f := "query" queryArgs := util.ToChaincodeArgs(f, "a") - response := stub.InvokeChaincode(chaincodeURL, queryArgs, "") + + // if chaincode being invoked is on the same channel, + // then channel defaults to the current channel and args[2] can be "". + // If the chaincode being called is on a different channel, + // then you must specify the channel name in args[2] + response := stub.InvokeChaincode(chaincodeName, queryArgs, channelName) if response.Status != shim.OK { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) fmt.Printf(errStr) @@ -144,7 +167,7 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) } queryArgs = util.ToChaincodeArgs(f, "b") - response = stub.InvokeChaincode(chaincodeURL, queryArgs, "") + response = stub.InvokeChaincode(chaincodeName, queryArgs, channelName) if response.Status != shim.OK { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) fmt.Printf(errStr) diff --git a/examples/chaincode/go/chaincode_example05/chaincode_example05_test.go b/examples/chaincode/go/chaincode_example05/chaincode_example05_test.go index 476f940497a..1f6d3047b67 100644 --- a/examples/chaincode/go/chaincode_example05/chaincode_example05_test.go +++ b/examples/chaincode/go/chaincode_example05/chaincode_example05_test.go @@ -23,8 +23,7 @@ import ( ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" ) -// chaincode_example02's hash is used here and must be updated if the example is changed -var example02Url = "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02" +var chaincodeName = "ex02" // chaincode_example05 looks like it wanted to return a JSON response to Query() // it doesn't actually do this though, it just returns the sum value @@ -76,7 +75,7 @@ func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) { } } -func TestExample04_Init(t *testing.T) { +func TestExample05_Init(t *testing.T) { scc := new(SimpleChaincode) stub := shim.NewMockStub("ex05", scc) @@ -86,35 +85,35 @@ func TestExample04_Init(t *testing.T) { checkState(t, stub, "sumStoreName", "432") } -func TestExample04_Query(t *testing.T) { +func TestExample05_Query(t *testing.T) { scc := new(SimpleChaincode) stub := shim.NewMockStub("ex05", scc) ccEx2 := new(ex02.SimpleChaincode) - stubEx2 := shim.NewMockStub("ex02", ccEx2) + stubEx2 := shim.NewMockStub(chaincodeName, ccEx2) checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")}) - stub.MockPeerChaincode(example02Url, stubEx2) + stub.MockPeerChaincode(chaincodeName, stubEx2) checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")}) // a + b = 111 + 222 = 333 - checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "333") // example05 doesn't return JSON? + checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "333") // example05 doesn't return JSON? } -func TestExample04_Invoke(t *testing.T) { +func TestExample05_Invoke(t *testing.T) { scc := new(SimpleChaincode) stub := shim.NewMockStub("ex05", scc) ccEx2 := new(ex02.SimpleChaincode) - stubEx2 := shim.NewMockStub("ex02", ccEx2) + stubEx2 := shim.NewMockStub(chaincodeName, ccEx2) checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")}) - stub.MockPeerChaincode(example02Url, stubEx2) + stub.MockPeerChaincode(chaincodeName, stubEx2) checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")}) // a + b = 222 + 333 = 555 - 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? + checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}) + checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON? checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222") checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333") @@ -122,8 +121,8 @@ func TestExample04_Invoke(t *testing.T) { checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")}) // a + b = 212 + 343 = 555 - 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? + checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}) + checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON? checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212") checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343") }