github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/chaincode_example03/chaincode_example03.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  // This program is an erroneous chaincode program that attempts to put state in query context - query should return error
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  
    24  	"github.com/hyperledger/fabric/core/chaincode/shim"
    25  	pb "github.com/hyperledger/fabric/protos/peer"
    26  )
    27  
    28  // SimpleChaincode example simple Chaincode implementation
    29  type SimpleChaincode struct {
    30  }
    31  
    32  // Init takes a string and int. These are stored as a key/value pair in the state
    33  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    34  	var A string // Entity
    35  	var Aval int // Asset holding
    36  	var err error
    37  	_, args := stub.GetFunctionAndParameters()
    38  	if len(args) != 2 {
    39  		return shim.Error("Incorrect number of arguments. Expecting 2")
    40  	}
    41  
    42  	// Initialize the chaincode
    43  	A = args[0]
    44  	Aval, err = strconv.Atoi(args[1])
    45  	if err != nil {
    46  		return shim.Error("Expecting integer value for asset holding")
    47  	}
    48  	fmt.Printf("Aval = %d\n", Aval)
    49  
    50  	// Write the state to the ledger - this put is legal within Run
    51  	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    52  	if err != nil {
    53  		return shim.Error(err.Error())
    54  	}
    55  
    56  	return shim.Success(nil)
    57  }
    58  
    59  // Invoke is a no-op
    60  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    61  	function, args := stub.GetFunctionAndParameters()
    62  	if function == "query" {
    63  		return t.query(stub, args)
    64  	}
    65  
    66  	return shim.Error("Invalid invoke function name. Expecting \"query\"")
    67  }
    68  
    69  func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    70  	var A string // Entity
    71  	var Aval int // Asset holding
    72  	var err error
    73  
    74  	if len(args) != 2 {
    75  		return shim.Error("Incorrect number of arguments. Expecting 2")
    76  	}
    77  
    78  	A = args[0]
    79  	Aval, err = strconv.Atoi(args[1])
    80  	if err != nil {
    81  		return shim.Error("Expecting integer value for asset holding")
    82  	}
    83  	fmt.Printf("Aval = %d\n", Aval)
    84  
    85  	// Write the state to the ledger - this put is illegal within Run
    86  	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    87  	if err != nil {
    88  		jsonResp := "{\"Error\":\"Cannot put state within chaincode query\"}"
    89  		return shim.Error(jsonResp)
    90  	}
    91  
    92  	return shim.Success(nil)
    93  }
    94  
    95  func main() {
    96  	err := shim.Start(new(SimpleChaincode))
    97  	if err != nil {
    98  		fmt.Printf("Error starting chaincode: %s", err)
    99  	}
   100  }