Skip to content

Commit

Permalink
FAB-1129 Add cc return value to proposal response
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1129

The return value from the chaincode invocation was not set by the endorser in
the payload of the Response message. This has been corrected.

Change-Id: I580e18d1813bc3447ef9900737ca3095746c3414
Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net>
  • Loading branch information
ale-linux committed Nov 15, 2016
1 parent 82e72f4 commit 3ac1bd3
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/endorser/endorser.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, prop *pb.Proposal) (*pb.
//1 -- simulate
//TODO what do we do with response ? We need it for Invoke responses for sure
//Which field in PayloadResponse will carry return value ?
_, simulationResult, ccevent, err := e.simulateProposal(ctx, prop, hdrExt.ChaincodeID, txsim)
result, simulationResult, ccevent, err := e.simulateProposal(ctx, prop, hdrExt.ChaincodeID, txsim)
if err != nil {
return &pb.ProposalResponse{Response: &pb.Response2{Status: 500, Message: err.Error()}}, err
}
Expand All @@ -361,6 +361,11 @@ func (e *Endorser) ProcessProposal(ctx context.Context, prop *pb.Proposal) (*pb.
return nil, err
}

// Set the proposal response payload - it
// contains the "return value" from the
// chaincode invocation
pResp.Response.Payload = result

return pResp, nil
}

Expand Down
144 changes: 144 additions & 0 deletions examples/chaincode/go/invokereturnsvalue/invokereturnsvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

//WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of
//calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has
//to be modified as well with the new ID of chaincode_example02.
//chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of
//hard-coding.

import (
"errors"
"fmt"
"strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

// Init method of chaincode
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
_, args := stub.GetFunctionAndParameters()
var A, B string // Entities
var Aval, Bval int // Asset holdings
var err error

if len(args) != 4 {
return nil, errors.New("Incorrect number of arguments. Expecting 4")
}

// Initialize the chaincode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}

err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return nil, err
}

return []byte("OK"), nil
}

// Invoke transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
_, args := stub.GetFunctionAndParameters()

var A, B string // Entities
var Aval, Bval int // Asset holdings
var X int // Transaction value
var err error

if len(args) != 3 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}

A = args[0]
B = args[1]

// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))

Bvalbytes, err := stub.GetState(B)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Bvalbytes == nil {
return nil, errors.New("Entity not found")
}
Bval, _ = strconv.Atoi(string(Bvalbytes))

// Perform the execution
X, err = strconv.Atoi(args[2])
if err != nil {
return nil, errors.New("Invalid transaction amount, expecting a integer value")
}
Aval = Aval - X
Bval = Bval + X
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}

err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return nil, err
}

return []byte(fmt.Sprintf("{%d,%d}", Aval, Bval)), nil
}

// Query callback representing the query of a chaincode. Not used in 1.0 (remove when interface is removed)
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
return nil, nil
}

func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
"fmt"
"testing"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte, retval []byte) {
result, err := stub.MockInit("1", args)
if err != nil {
fmt.Println("Init failed", err)
t.FailNow()
}
if retval != nil {
if result == nil {
fmt.Printf("Init returned nil, expected %s", string(retval))
t.FailNow()
}
if string(result) != string(retval) {
fmt.Printf("Init returned %s, expected %s", string(result), string(retval))
t.FailNow()
}
}
}

func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
bytes := stub.State[name]
if bytes == nil {
fmt.Println("State", name, "failed to get value")
t.FailNow()
}
if string(bytes) != value {
fmt.Println("State value", name, "was not", value, "as expected")
t.FailNow()
}
}

func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte, retval []byte) {
result, err := stub.MockInvoke("1", args)
if err != nil {
fmt.Println("Invoke", args, "failed", err)
t.FailNow()
}

if retval != nil {
if result == nil {
fmt.Printf("Invoke returned nil, expected %s", string(retval))
t.FailNow()
}
if string(result) != string(retval) {
fmt.Printf("Invoke returned %s, expected %s", string(result), string(retval))
t.FailNow()
}
}
}

func Test_Init(t *testing.T) {
scc := new(SimpleChaincode)
stub := shim.NewMockStub("ex02", scc)

// Init A=123 B=234
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")}, []byte("OK"))

checkState(t, stub, "A", "123")
checkState(t, stub, "B", "234")
}

func Test_Invoke(t *testing.T) {
scc := new(SimpleChaincode)
stub := shim.NewMockStub("ex02", scc)

// Init A=567 B=678
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")}, []byte("OK"))

// Invoke A->B for 123
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")}, []byte("{444,801}"))

// Invoke B->A for 234
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")}, []byte("{567,678}"))
}

0 comments on commit 3ac1bd3

Please sign in to comment.