github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/examples/chaincode/go/map/map.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  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/hyperledger/fabric/core/chaincode/shim"
    24  	pb "github.com/hyperledger/fabric/protos/peer"
    25  )
    26  
    27  // This chaincode implements a simple map that is stored in the state.
    28  // The following operations are available.
    29  
    30  // Invoke operations
    31  // put - requires two arguments, a key and value
    32  // remove - requires a key
    33  // get - requires one argument, a key, and returns a value
    34  // keys - requires no arguments, returns all keys
    35  
    36  // SimpleChaincode example simple Chaincode implementation
    37  type SimpleChaincode struct {
    38  }
    39  
    40  // Init is a no-op
    41  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    42  	return shim.Success(nil)
    43  }
    44  
    45  // Invoke has two functions
    46  // put - takes two arguements, a key and value, and stores them in the state
    47  // remove - takes one argument, a key, and removes if from the state
    48  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    49  	function, args := stub.GetFunctionAndParameters()
    50  	switch function {
    51  	case "put":
    52  		if len(args) < 2 {
    53  			return shim.Error("put operation must include two arguments, a key and value")
    54  		}
    55  		key := args[0]
    56  		value := args[1]
    57  
    58  		err := stub.PutState(key, []byte(value))
    59  		if err != nil {
    60  			fmt.Printf("Error putting state %s", err)
    61  			return shim.Error(fmt.Sprintf("put operation failed. Error updating state: %s", err))
    62  		}
    63  		return shim.Success(nil)
    64  
    65  	case "remove":
    66  		if len(args) < 1 {
    67  			return shim.Error("remove operation must include one argument, a key")
    68  		}
    69  		key := args[0]
    70  
    71  		err := stub.DelState(key)
    72  		if err != nil {
    73  			return shim.Error(fmt.Sprintf("remove operation failed. Error updating state: %s", err))
    74  		}
    75  		return shim.Success(nil)
    76  
    77  	case "get":
    78  		if len(args) < 1 {
    79  			return shim.Error("get operation must include one argument, a key")
    80  		}
    81  		key := args[0]
    82  		value, err := stub.GetState(key)
    83  		if err != nil {
    84  			return shim.Error(fmt.Sprintf("get operation failed. Error accessing state: %s", err))
    85  		}
    86  		return shim.Success(value)
    87  
    88  	case "keys":
    89  		if len(args) < 2 {
    90  			return shim.Error("put operation must include two arguments, a key and value")
    91  		}
    92  		startKey := args[0]
    93  		endKey := args[1]
    94  
    95  		keysIter, err := stub.GetStateByRange(startKey, endKey)
    96  		if err != nil {
    97  			return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
    98  		}
    99  		defer keysIter.Close()
   100  
   101  		var keys []string
   102  		for keysIter.HasNext() {
   103  			response, iterErr := keysIter.Next()
   104  			if iterErr != nil {
   105  				return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
   106  			}
   107  			keys = append(keys, response.Key)
   108  		}
   109  
   110  		for key, value := range keys {
   111  			fmt.Printf("key %d contains %s\n", key, value)
   112  		}
   113  
   114  		jsonKeys, err := json.Marshal(keys)
   115  		if err != nil {
   116  			return shim.Error(fmt.Sprintf("keys operation failed. Error marshaling JSON: %s", err))
   117  		}
   118  
   119  		return shim.Success(jsonKeys)
   120  	case "query":
   121  		query := args[0]
   122  		keysIter, err := stub.GetQueryResult(query)
   123  		if err != nil {
   124  			return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   125  		}
   126  		defer keysIter.Close()
   127  
   128  		var keys []string
   129  		for keysIter.HasNext() {
   130  			response, iterErr := keysIter.Next()
   131  			if iterErr != nil {
   132  				return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   133  			}
   134  			keys = append(keys, response.Key)
   135  		}
   136  
   137  		jsonKeys, err := json.Marshal(keys)
   138  		if err != nil {
   139  			return shim.Error(fmt.Sprintf("query operation failed. Error marshaling JSON: %s", err))
   140  		}
   141  
   142  		return shim.Success(jsonKeys)
   143  	case "history":
   144  		key := args[0]
   145  		keysIter, err := stub.GetHistoryForKey(key)
   146  		if err != nil {
   147  			return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   148  		}
   149  		defer keysIter.Close()
   150  
   151  		var keys []string
   152  		for keysIter.HasNext() {
   153  			response, iterErr := keysIter.Next()
   154  			if iterErr != nil {
   155  				return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   156  			}
   157  			keys = append(keys, response.TxId)
   158  		}
   159  
   160  		for key, txID := range keys {
   161  			fmt.Printf("key %d contains %s\n", key, txID)
   162  		}
   163  
   164  		jsonKeys, err := json.Marshal(keys)
   165  		if err != nil {
   166  			return shim.Error(fmt.Sprintf("query operation failed. Error marshaling JSON: %s", err))
   167  		}
   168  
   169  		return shim.Success(jsonKeys)
   170  
   171  	default:
   172  		return shim.Success([]byte("Unsupported operation"))
   173  	}
   174  }
   175  
   176  func main() {
   177  	err := shim.Start(new(SimpleChaincode))
   178  	if err != nil {
   179  		fmt.Printf("Error starting chaincode: %s", err)
   180  	}
   181  }