github.com/Hnampk/my-fabric@v0.0.0-20201028083322-75069da399c0/integration/lifecycle/chaincode/caller/chaincode.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package caller
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-chaincode-go/shim"
    11  	pb "github.com/hyperledger/fabric-protos-go/peer"
    12  )
    13  
    14  // CC example simple Chaincode implementation
    15  type CC struct {
    16  }
    17  
    18  func (t *CC) Init(stub shim.ChaincodeStubInterface) pb.Response {
    19  	err := stub.PutState("foo", []byte("caller:foo"))
    20  	if err != nil {
    21  		return shim.Error(err.Error())
    22  	}
    23  
    24  	return shim.Success(nil)
    25  }
    26  
    27  func (t *CC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    28  	fn, args := stub.GetFunctionAndParameters()
    29  	switch fn {
    30  	case "INVOKE":
    31  		err := stub.PutState("foo", []byte("caller:bar"))
    32  		if err != nil {
    33  			return shim.Error(err.Error())
    34  		}
    35  
    36  		return stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("INVOKE")}, "")
    37  
    38  	case "QUERYCALLEE":
    39  		response := stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("QUERY")}, args[1])
    40  		if response.Status >= 400 {
    41  			return shim.Error(response.Message)
    42  		}
    43  
    44  		return shim.Success(response.Payload)
    45  
    46  	case "QUERY":
    47  		val, err := stub.GetState("foo")
    48  		if err != nil {
    49  			return shim.Error(err.Error())
    50  		}
    51  
    52  		return shim.Success(val)
    53  	default:
    54  		return shim.Error("unknown function")
    55  	}
    56  }