Skip to content

Commit

Permalink
[FAB-1835] Changes return values of chaincode
Browse files Browse the repository at this point in the history
As per FAB-1457, Change return values of chaincode the chaincode inerafces

Change-Id: I95b733c8ddb6800781afe3235e31beedea990da2
Signed-off-by: Ratnakar <asara.ratnakar@gmail.com>
  • Loading branch information
asararatnakar committed Jan 25, 2017
1 parent 3cf2ad9 commit 24926ce
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions test/fixtures/src/github.com/example_cc/example_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,72 @@ package main


import (
"errors"
"fmt"
"strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)

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

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("########### example_cc Init ###########")
_, 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")
return shim.Error("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")
return shim.Error("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")
return shim.Error("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
return shim.Error(err.Error())
}

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

return nil, nil
return shim.Success(nil)


}

func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
return nil, errors.New("Unknown supported call")
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error("Unknown supported call")
}

// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("########### example_cc Invoke ###########")
function, args := stub.GetFunctionAndParameters()

if function != "invoke" {
return nil, errors.New("Unknown function call")
return shim.Error("Unknown function call")
}

if len(args) < 2 {
return nil, errors.New("Incorrect number of arguments. Expecting at least 2")
return shim.Error("Incorrect number of arguments. Expecting at least 2")
}

if args[0] == "delete" {
Expand All @@ -95,18 +99,18 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, erro
// Deletes an entity from its state
return t.move(stub, args)
}
return nil, errors.New("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'")
return shim.Error("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'")
}

func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// must be an invoke
var A, B string // Entities
var Aval, Bval int // Asset holdings
var X int // Transaction value
var err error

if len(args) != 4 {
return nil, errors.New("Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value")
return shim.Error("Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value")
}

A = args[1]
Expand All @@ -116,26 +120,26 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
// 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")
return shim.Error("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
return shim.Error("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))

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

// Perform the execution
X, err = strconv.Atoi(args[3])
if err != nil {
return nil, errors.New("Invalid transaction amount, expecting a integer value")
return shim.Error("Invalid transaction amount, expecting a integer value")
}
Aval = Aval - X
Bval = Bval + X
Expand All @@ -144,43 +148,42 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
return shim.Error(err.Error())
}

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

//return nil, nil
return []byte(fmt.Sprintf("Aval=%d, Bval= %d", Aval, Bval)), nil
return shim.Success(nil);
}

// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
return shim.Error("Incorrect number of arguments. Expecting 1")
}

A := args[1]

// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return nil, errors.New("Failed to delete state")
return shim.Error("Failed to delete state")
}

return nil, nil
return shim.Success(nil)
}

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {

var A string // Entities
var err error

if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the person to query")
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
}

A = args[1]
Expand All @@ -189,17 +192,17 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
return nil, errors.New(jsonResp)
return shim.Error(jsonResp)
}

if Avalbytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
return nil, errors.New(jsonResp)
return shim.Error(jsonResp)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return Avalbytes, nil
return shim.Success(Avalbytes)
}

func main() {
Expand Down

0 comments on commit 24926ce

Please sign in to comment.