Skip to content

Commit

Permalink
FAB-2924 Update chaincode_example05 for clarity
Browse files Browse the repository at this point in the history
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 <nishi.nidamarty@itpeoplecorp.com>
  • Loading branch information
2016Nishi committed Apr 3, 2017
1 parent a785a4c commit dadc939
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
51 changes: 37 additions & 14 deletions examples/chaincode/go/chaincode_example05/chaincode_example05.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -86,44 +85,44 @@ 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")

// update A-=10 and B+=10
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, [][]