github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/chaincode_example04/chaincode_example04.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	"github.com/hyperledger/fabric/common/util"
    24  	"github.com/hyperledger/fabric/core/chaincode/shim"
    25  	pb "github.com/hyperledger/fabric/protos/peer"
    26  )
    27  
    28  // This chaincode is a test for chaincode invoking another chaincode - invokes chaincode_example02
    29  
    30  // SimpleChaincode example simple Chaincode implementation
    31  type SimpleChaincode struct {
    32  }
    33  
    34  // Init takes two arguements, a string and int. These are stored in the key/value pair in the state
    35  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    36  	var event string // Indicates whether event has happened. Initially 0
    37  	var eventVal int // State of event
    38  	var err error
    39  	_, args := stub.GetFunctionAndParameters()
    40  	if len(args) != 2 {
    41  		return shim.Error("Incorrect number of arguments. Expecting 2")
    42  	}
    43  
    44  	// Initialize the chaincode
    45  	event = args[0]
    46  	eventVal, err = strconv.Atoi(args[1])
    47  	if err != nil {
    48  		return shim.Error("Expecting integer value for event status")
    49  	}
    50  	fmt.Printf("eventVal = %d\n", eventVal)
    51  
    52  	err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
    53  	if err != nil {
    54  		return shim.Error(err.Error())
    55  	}
    56  
    57  	return shim.Success(nil)
    58  }
    59  
    60  // Invoke invokes another chaincode - chaincode_example02, upon receipt of an event and changes event state
    61  func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    62  	var event string // Event entity
    63  	var eventVal int // State of event
    64  	var err error
    65  
    66  	if len(args) != 3 {
    67  		return shim.Error("Incorrect number of arguments. Expecting 3")
    68  	}
    69  
    70  	chainCodeToCall := args[0]
    71  	event = args[1]
    72  	eventVal, err = strconv.Atoi(args[2])
    73  	if err != nil {
    74  		return shim.Error("Expected integer value for event state change")
    75  	}
    76  
    77  	if eventVal != 1 {
    78  		fmt.Printf("Unexpected event. Doing nothing\n")
    79  		return shim.Success(nil)
    80  	}
    81  
    82  	f := "invoke"
    83  	invokeArgs := util.ToChaincodeArgs(f, "a", "b", "10")
    84  	response := stub.InvokeChaincode(chainCodeToCall, invokeArgs, "")
    85  	if response.Status != shim.OK {
    86  		errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", string(response.Payload))
    87  		fmt.Printf(errStr)
    88  		return shim.Error(errStr)
    89  	}
    90  
    91  	fmt.Printf("Invoke chaincode successful. Got response %s", string(response.Payload))
    92  
    93  	// Write the event state back to the ledger
    94  	err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
    95  	if err != nil {
    96  		return shim.Error(err.Error())
    97  	}
    98  
    99  	return response
   100  }
   101  
   102  func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   103  	var event string // Event entity
   104  	var err error
   105  
   106  	if len(args) < 1 {
   107  		return shim.Error("Incorrect number of arguments. Expecting entity to query")
   108  	}
   109  
   110  	event = args[0]
   111  	var jsonResp string
   112  
   113  	// Get the state from the ledger
   114  	eventValbytes, err := stub.GetState(event)
   115  	if err != nil {
   116  		jsonResp = "{\"Error\":\"Failed to get state for " + event + "\"}"
   117  		return shim.Error(jsonResp)
   118  	}
   119  
   120  	if eventValbytes == nil {
   121  		jsonResp = "{\"Error\":\"Nil value for " + event + "\"}"
   122  		return shim.Error(jsonResp)
   123  	}
   124  
   125  	if len(args) > 3 {
   126  		chainCodeToCall := args[1]
   127  		queryKey := args[2]
   128  		channel := args[3]
   129  		f := "query"
   130  		invokeArgs := util.ToChaincodeArgs(f, queryKey)
   131  		response := stub.InvokeChaincode(chainCodeToCall, invokeArgs, channel)
   132  		if response.Status != shim.OK {
   133  			errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error())
   134  			fmt.Printf(errStr)
   135  			return shim.Error(errStr)
   136  		}
   137  		jsonResp = string(response.Payload)
   138  	} else {
   139  		jsonResp = "{\"Name\":\"" + event + "\",\"Amount\":\"" + string(eventValbytes) + "\"}"
   140  	}
   141  	fmt.Printf("Query Response: %s\n", jsonResp)
   142  
   143  	return shim.Success([]byte(jsonResp))
   144  }
   145  
   146  // Invoke is called by fabric to execute a transaction
   147  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
   148  	function, args := stub.GetFunctionAndParameters()
   149  	if function == "invoke" {
   150  		return t.invoke(stub, args)
   151  	} else if function == "query" {
   152  		return t.query(stub, args)
   153  	}
   154  
   155  	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")
   156  }
   157  
   158  func main() {
   159  	err := shim.Start(new(SimpleChaincode))
   160  	if err != nil {
   161  		fmt.Printf("Error starting Simple chaincode: %s", err)
   162  	}
   163  }