github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/eventsender/eventsender.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  //WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of
    20  //calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has
    21  //to be modified as well with the new ID of chaincode_example02.
    22  //chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of
    23  //hard-coding.
    24  
    25  import (
    26  	"fmt"
    27  	"strconv"
    28  
    29  	"github.com/hyperledger/fabric/core/chaincode/shim"
    30  	pb "github.com/hyperledger/fabric/protos/peer"
    31  )
    32  
    33  // EventSender example simple Chaincode implementation
    34  type EventSender struct {
    35  }
    36  
    37  // Init function
    38  func (t *EventSender) Init(stub shim.ChaincodeStubInterface) pb.Response {
    39  	err := stub.PutState("noevents", []byte("0"))
    40  	if err != nil {
    41  		return shim.Error(err.Error())
    42  	}
    43  
    44  	return shim.Success(nil)
    45  }
    46  
    47  // Invoke function
    48  func (t *EventSender) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    49  	b, err := stub.GetState("noevents")
    50  	if err != nil {
    51  		return shim.Error("Failed to get state")
    52  	}
    53  	noevts, _ := strconv.Atoi(string(b))
    54  
    55  	tosend := "Event " + string(b)
    56  	for _, s := range args {
    57  		tosend = tosend + "," + s
    58  	}
    59  
    60  	err = stub.PutState("noevents", []byte(strconv.Itoa(noevts+1)))
    61  	if err != nil {
    62  		return shim.Error(err.Error())
    63  	}
    64  
    65  	err = stub.SetEvent("evtsender", []byte(tosend))
    66  	if err != nil {
    67  		return shim.Error(err.Error())
    68  	}
    69  	return shim.Success(nil)
    70  }
    71  
    72  // Query function
    73  func (t *EventSender) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    74  	b, err := stub.GetState("noevents")
    75  	if err != nil {
    76  		return shim.Error("Failed to get state")
    77  	}
    78  	jsonResp := "{\"NoEvents\":\"" + string(b) + "\"}"
    79  	return shim.Success([]byte(jsonResp))
    80  }
    81  
    82  func (t *EventSender) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    83  	function, args := stub.GetFunctionAndParameters()
    84  	if function == "invoke" {
    85  		return t.invoke(stub, args)
    86  	} else if function == "query" {
    87  		return t.query(stub, args)
    88  	}
    89  
    90  	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")
    91  }
    92  
    93  func main() {
    94  	err := shim.Start(new(EventSender))
    95  	if err != nil {
    96  		fmt.Printf("Error starting EventSender chaincode: %s", err)
    97  	}
    98  }