github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/lifecycle/chaincode/caller/chaincode.go (about)

     1  /*
     2  Copyright hechain. 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  func (t *CC) Init(stub shim.ChaincodeStubInterface) pb.Response {
    18  	err := stub.PutState("foo", []byte("caller:foo"))
    19  	if err != nil {
    20  		return shim.Error(err.Error())
    21  	}
    22  
    23  	return shim.Success(nil)
    24  }
    25  
    26  func (t *CC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    27  	fn, args := stub.GetFunctionAndParameters()
    28  	switch fn {
    29  	case "INVOKE":
    30  		err := stub.PutState("foo", []byte("caller:bar"))
    31  		if err != nil {
    32  			return shim.Error(err.Error())
    33  		}
    34  
    35  		return stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("INVOKE")}, "")
    36  
    37  	case "QUERYCALLEE":
    38  		response := stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("QUERY")}, args[1])
    39  		if response.Status >= 400 {
    40  			return shim.Error(response.Message)
    41  		}
    42  
    43  		return shim.Success(response.Payload)
    44  
    45  	case "QUERY":
    46  		val, err := stub.GetState("foo")
    47  		if err != nil {
    48  			return shim.Error(err.Error())
    49  		}
    50  
    51  		return shim.Success(val)
    52  	default:
    53  		return shim.Error("unknown function")
    54  	}
    55  }