Skip to content

Commit dadc939

Browse files
committed
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 <[email protected]>
1 parent a785a4c commit dadc939

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

examples/chaincode/go/chaincode_example05/chaincode_example05.go

+37-14
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,33 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
6161

6262
// Invoke queries another chaincode and updates its own state
6363
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
64-
var sum string // Sum entity
65-
var Aval, Bval, sumVal int // value of sum entity - to be computed
64+
var sum, channelName string // Sum entity
65+
var Aval, Bval, sumVal int // value of sum entity - to be computed
6666
var err error
6767

68-
if len(args) != 2 {
69-
return shim.Error("Incorrect number of arguments. Expecting 2")
68+
if len(args) < 2 {
69+
return shim.Error("Incorrect number of arguments. Expecting atleast 2")
7070
}
7171

72-
chaincodeURL := args[0] // Expecting "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02"
72+
chaincodeName := args[0] // Expecting name of the chaincode you would like to call, this name would be given during chaincode install time
7373
sum = args[1]
7474

75+
if len(args) > 2 {
76+
channelName = args[2]
77+
} else {
78+
channelName = ""
79+
}
80+
7581
// Query chaincode_example02
7682
f := "query"
7783
queryArgs := util.ToChaincodeArgs(f, "a")
78-
response := stub.InvokeChaincode(chaincodeURL, queryArgs, "")
84+
85+
// if chaincode being invoked is on the same channel,
86+
// then channel defaults to the current channel and args[2] can be "".
87+
// If the chaincode being called is on a different channel,
88+
// then you must specify the channel name in args[2]
89+
90+
response := stub.InvokeChaincode(chaincodeName, queryArgs, channelName)
7991
if response.Status != shim.OK {
8092
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload)
8193
fmt.Printf(errStr)
@@ -89,7 +101,7 @@ func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string
89101
}
90102

91103
queryArgs = util.ToChaincodeArgs(f, "b")
92-
response = stub.InvokeChaincode(chaincodeURL, queryArgs, "")
104+
response = stub.InvokeChaincode(chaincodeName, queryArgs, channelName)
93105
if response.Status != shim.OK {
94106
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload)
95107
fmt.Printf(errStr)
@@ -116,21 +128,32 @@ func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string
116128
}
117129

118130
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
119-
var sum string // Sum entity
120-
var Aval, Bval, sumVal int // value of sum entity - to be computed
131+
var sum, channelName string // Sum entity
132+
var Aval, Bval, sumVal int // value of sum entity - to be computed
121133
var err error
122134

123-
if len(args) != 2 {
124-
return shim.Error("Incorrect number of arguments. Expecting 2")
135+
if len(args) < 2 {
136+
return shim.Error("Incorrect number of arguments. Expecting atleast 2")
125137
}
126138

127-
chaincodeURL := args[0]
139+
chaincodeName := args[0] // Expecting name of the chaincode you would like to call, this name would be given during chaincode install time
128140
sum = args[1]
129141

142+
if len(args) > 2 {
143+
channelName = args[2]
144+
} else {
145+
channelName = ""
146+
}
147+
130148
// Query chaincode_example02
131149
f := "query"
132150
queryArgs := util.ToChaincodeArgs(f, "a")
133-
response := stub.InvokeChaincode(chaincodeURL, queryArgs, "")
151+
152+
// if chaincode being invoked is on the same channel,
153+
// then channel defaults to the current channel and args[2] can be "".
154+
// If the chaincode being called is on a different channel,
155+
// then you must specify the channel name in args[2]
156+
response := stub.InvokeChaincode(chaincodeName, queryArgs, channelName)
134157
if response.Status != shim.OK {
135158
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload)
136159
fmt.Printf(errStr)
@@ -144,7 +167,7 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
144167
}
145168

146169
queryArgs = util.ToChaincodeArgs(f, "b")
147-
response = stub.InvokeChaincode(chaincodeURL, queryArgs, "")
170+
response = stub.InvokeChaincode(chaincodeName, queryArgs, channelName)
148171
if response.Status != shim.OK {
149172
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload)
150173
fmt.Printf(errStr)

examples/chaincode/go/chaincode_example05/chaincode_example05_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import (
2323
ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
2424
)
2525

26-
// chaincode_example02's hash is used here and must be updated if the example is changed
27-
var example02Url = "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02"
26+
var chaincodeName = "ex02"
2827

2928
// chaincode_example05 looks like it wanted to return a JSON response to Query()
3029
// 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) {
7675
}
7776
}
7877

79-
func TestExample04_Init(t *testing.T) {
78+
func TestExample05_Init(t *testing.T) {
8079
scc := new(SimpleChaincode)
8180
stub := shim.NewMockStub("ex05", scc)
8281

@@ -86,44 +85,44 @@ func TestExample04_Init(t *testing.T) {
8685
checkState(t, stub, "sumStoreName", "432")
8786
}
8887

89-
func TestExample04_Query(t *testing.T) {
88+
func TestExample05_Query(t *testing.T) {
9089
scc := new(SimpleChaincode)
9190
stub := shim.NewMockStub("ex05", scc)
9291

9392
ccEx2 := new(ex02.SimpleChaincode)
94-
stubEx2 := shim.NewMockStub("ex02", ccEx2)
93+
stubEx2 := shim.NewMockStub(chaincodeName, ccEx2)
9594
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
96-
stub.MockPeerChaincode(example02Url, stubEx2)
95+
stub.MockPeerChaincode(chaincodeName, stubEx2)
9796

9897
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
9998

10099
// a + b = 111 + 222 = 333
101-
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "333") // example05 doesn't return JSON?
100+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "333") // example05 doesn't return JSON?
102101
}
103102

104-
func TestExample04_Invoke(t *testing.T) {
103+
func TestExample05_Invoke(t *testing.T) {
105104
scc := new(SimpleChaincode)
106105
stub := shim.NewMockStub("ex05", scc)
107106

108107
ccEx2 := new(ex02.SimpleChaincode)
109-
stubEx2 := shim.NewMockStub("ex02", ccEx2)
108+
stubEx2 := shim.NewMockStub(chaincodeName, ccEx2)
110109
checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")})
111-
stub.MockPeerChaincode(example02Url, stubEx2)
110+
stub.MockPeerChaincode(chaincodeName, stubEx2)
112111

113112
checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})
114113

115114
// a + b = 222 + 333 = 555
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?
115+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")})
116+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON?
118117
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222")
119118
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333")
120119

121120
// update A-=10 and B+=10
122121
checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")})
123122

124123
// a + b = 212 + 343 = 555
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?
124+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")})
125+
checkQuery(t, stub, [][]byte{[]byte("query"), []byte(chaincodeName), []byte("sumStoreName"), []byte("")}, "555") // example05 doesn't return JSON?
127126
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212")
128127
checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343")
129128
}

0 commit comments

Comments
 (0)