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

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package simple
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"strconv"
    13  
    14  	"github.com/hyperledger/fabric-chaincode-go/shim"
    15  	pb "github.com/hyperledger/fabric-protos-go/peer"
    16  )
    17  
    18  // SimpleChaincode example simple Chaincode implementation
    19  type SimpleChaincode struct{}
    20  
    21  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    22  	fmt.Println("Init invoked")
    23  	_, args := stub.GetFunctionAndParameters()
    24  	var A, B string    // Entities
    25  	var Aval, Bval int // Asset holdings
    26  	var err error
    27  
    28  	if len(args) != 4 {
    29  		return shim.Error("Incorrect number of arguments. Expecting 4")
    30  	}
    31  
    32  	// Initialize the chaincode
    33  	A = args[0]
    34  	Aval, err = strconv.Atoi(args[1])
    35  	if err != nil {
    36  		return shim.Error("Expecting integer value for asset holding")
    37  	}
    38  	B = args[2]
    39  	Bval, err = strconv.Atoi(args[3])
    40  	if err != nil {
    41  		return shim.Error("Expecting integer value for asset holding")
    42  	}
    43  	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
    44  
    45  	// Write the state to the ledger
    46  	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    47  	if err != nil {
    48  		return shim.Error(err.Error())
    49  	}
    50  
    51  	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
    52  	if err != nil {
    53  		return shim.Error(err.Error())
    54  	}
    55  
    56  	fmt.Println("Init returning with success")
    57  	return shim.Success(nil)
    58  }
    59  
    60  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    61  	fmt.Println("ex02 Invoke")
    62  	if os.Getenv("DEVMODE_ENABLED") != "" {
    63  		fmt.Println("invoking in devmode")
    64  	}
    65  	function, args := stub.GetFunctionAndParameters()
    66  	switch function {
    67  	case "invoke":
    68  		// Make payment of X units from A to B
    69  		return t.invoke(stub, args)
    70  	case "delete":
    71  		// Deletes an entity from its state
    72  		return t.delete(stub, args)
    73  	case "query":
    74  		// the old "Query" is now implemented in invoke
    75  		return t.query(stub, args)
    76  	case "respond":
    77  		// return with an error
    78  		return t.respond(stub, args)
    79  	case "mspid":
    80  		// Checks the shim's GetMSPID() API
    81  		return t.mspid(args)
    82  	case "event":
    83  		return t.event(stub, args)
    84  	default:
    85  		return shim.Error(`Invalid invoke function name. Expecting "invoke", "delete", "query", "respond", "mspid", or "event"`)
    86  	}
    87  }
    88  
    89  // Transaction makes payment of X units from A to B
    90  func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    91  	var A, B string    // Entities
    92  	var Aval, Bval int // Asset holdings
    93  	var X int          // Transaction value
    94  	var err error
    95  
    96  	if len(args) != 3 {
    97  		return shim.Error("Incorrect number of arguments. Expecting 3")
    98  	}
    99  
   100  	A = args[0]
   101  	B = args[1]
   102  
   103  	// Get the state from the ledger
   104  	// TODO: will be nice to have a GetAllState call to ledger
   105  	Avalbytes, err := stub.GetState(A)
   106  	if err != nil {
   107  		return shim.Error("Failed to get state")
   108  	}
   109  	if Avalbytes == nil {
   110  		return shim.Error("Entity not found")
   111  	}
   112  	Aval, _ = strconv.Atoi(string(Avalbytes))
   113  
   114  	Bvalbytes, err := stub.GetState(B)
   115  	if err != nil {
   116  		return shim.Error("Failed to get state")
   117  	}
   118  	if Bvalbytes == nil {
   119  		return shim.Error("Entity not found")
   120  	}
   121  	Bval, _ = strconv.Atoi(string(Bvalbytes))
   122  
   123  	// Perform the execution
   124  	X, err = strconv.Atoi(args[2])
   125  	if err != nil {
   126  		return shim.Error("Invalid transaction amount, expecting a integer value")
   127  	}
   128  	Aval = Aval - X
   129  	Bval = Bval + X
   130  	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
   131  
   132  	// Write the state back to the ledger
   133  	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
   134  	if err != nil {
   135  		return shim.Error(err.Error())
   136  	}
   137  
   138  	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
   139  	if err != nil {
   140  		return shim.Error(err.Error())
   141  	}
   142  
   143  	return shim.Success(nil)
   144  }
   145  
   146  // Deletes an entity from state
   147  func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   148  	if len(args) != 1 {
   149  		return shim.Error("Incorrect number of arguments. Expecting 1")
   150  	}
   151  
   152  	A := args[0]
   153  
   154  	// Delete the key from the state in ledger
   155  	err := stub.DelState(A)
   156  	if err != nil {
   157  		return shim.Error("Failed to delete state")
   158  	}
   159  
   160  	return shim.Success(nil)
   161  }
   162  
   163  // query callback representing the query of a chaincode
   164  func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   165  	var A string // Entities
   166  	var err error
   167  
   168  	if len(args) != 1 {
   169  		return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
   170  	}
   171  
   172  	A = args[0]
   173  
   174  	// Get the state from the ledger
   175  	Avalbytes, err := stub.GetState(A)
   176  	if err != nil {
   177  		jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
   178  		return shim.Error(jsonResp)
   179  	}
   180  
   181  	if Avalbytes == nil {
   182  		jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
   183  		return shim.Error(jsonResp)
   184  	}
   185  
   186  	jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
   187  	fmt.Printf("Query Response:%s\n", jsonResp)
   188  	return shim.Success(Avalbytes)
   189  }
   190  
   191  // respond simply generates a response payload from the args
   192  func (t *SimpleChaincode) respond(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   193  	if len(args) != 3 {
   194  		return shim.Error("expected three arguments")
   195  	}
   196  
   197  	status, err := strconv.ParseInt(args[0], 10, 32)
   198  	if err != nil {
   199  		return shim.Error(err.Error())
   200  	}
   201  	message := args[1]
   202  	payload := []byte(args[2])
   203  
   204  	return pb.Response{
   205  		Status:  int32(status),
   206  		Message: message,
   207  		Payload: payload,
   208  	}
   209  }
   210  
   211  // mspid simply calls shim.GetMSPID() to verify the mspid was properly passed from the peer
   212  // via the CORE_PEER_LOCALMSPID env var
   213  func (t *SimpleChaincode) mspid(args []string) pb.Response {
   214  	if len(args) != 0 {
   215  		return shim.Error("expected no arguments")
   216  	}
   217  
   218  	// Get the mspid from the env var
   219  	mspid, err := shim.GetMSPID()
   220  	if err != nil {
   221  		jsonResp := "{\"Error\":\"Failed to get mspid\"}"
   222  		return shim.Error(jsonResp)
   223  	}
   224  
   225  	if mspid == "" {
   226  		jsonResp := "{\"Error\":\"Empty mspid\"}"
   227  		return shim.Error(jsonResp)
   228  	}
   229  
   230  	fmt.Printf("MSPID:%s\n", mspid)
   231  	return shim.Success([]byte(mspid))
   232  }
   233  
   234  // event emits a chaincode event
   235  func (t *SimpleChaincode) event(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   236  	if len(args) != 2 {
   237  		return shim.Error("Incorrect number of arguments. Expecting 2")
   238  	}
   239  
   240  	if err := stub.SetEvent(args[0], []byte(args[1])); err != nil {
   241  		return shim.Error(err.Error())
   242  	}
   243  
   244  	return shim.Success(nil)
   245  }