|
| 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 | +// Sleeper chaincode sleeps and works with one state variable |
| 20 | +// Init - 1 param, a sleep time in millisecs |
| 21 | +// Invoke - 4 or 3 params, "put" or "get", value to set and sleep time in millisecs |
| 22 | +// |
| 23 | +// Sleeper can be used to test the "chaincode.executetimeout" property |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "strconv" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/hyperledger/fabric/core/chaincode/shim" |
| 31 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 32 | +) |
| 33 | + |
| 34 | +// SleeperChaincode example simple Chaincode implementation |
| 35 | +type SleeperChaincode struct { |
| 36 | +} |
| 37 | + |
| 38 | +func (t *SleeperChaincode) sleep(sleepTime string) { |
| 39 | + st, _ := strconv.Atoi(sleepTime) |
| 40 | + if st >= 0 { |
| 41 | + time.Sleep(time.Duration(st) * time.Millisecond) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Init initializes chaincode...all it does is sleep a bi |
| 46 | +func (t *SleeperChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { |
| 47 | + args := stub.GetStringArgs() |
| 48 | + |
| 49 | + if len(args) != 1 { |
| 50 | + return shim.Error("Incorrect number of arguments. Expecting 1") |
| 51 | + } |
| 52 | + |
| 53 | + sleepTime := args[0] |
| 54 | + |
| 55 | + t.sleep(sleepTime) |
| 56 | + |
| 57 | + return shim.Success(nil) |
| 58 | +} |
| 59 | + |
| 60 | +// Invoke sets key/value and sleeps a bit |
| 61 | +func (t *SleeperChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { |
| 62 | + function, args := stub.GetFunctionAndParameters() |
| 63 | + if function == "put" { |
| 64 | + if len(args) != 3 { |
| 65 | + return shim.Error("Incorrect number of arguments. Expecting 3") |
| 66 | + } |
| 67 | + |
| 68 | + // Make payment of X units from A to B |
| 69 | + return t.invoke(stub, args) |
| 70 | + } else if function == "get" { |
| 71 | + if len(args) != 2 { |
| 72 | + return shim.Error("Incorrect number of arguments. Expecting 2") |
| 73 | + } |
| 74 | + |
| 75 | + // the old "Query" is now implemtned in invoke |
| 76 | + return t.query(stub, args) |
| 77 | + } |
| 78 | + |
| 79 | + return shim.Error("Invalid invoke function name. Expecting \"put\" or \"get\"") |
| 80 | +} |
| 81 | + |
| 82 | +// Transaction makes payment of X units from A to B |
| 83 | +func (t *SleeperChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { |
| 84 | + // set state |
| 85 | + key := args[0] |
| 86 | + val := args[1] |
| 87 | + |
| 88 | + err := stub.PutState(key, []byte(val)) |
| 89 | + if err != nil { |
| 90 | + return shim.Error(err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + sleepTime := args[2] |
| 94 | + |
| 95 | + //sleep for a bit |
| 96 | + t.sleep(sleepTime) |
| 97 | + |
| 98 | + return shim.Success([]byte("OK")) |
| 99 | +} |
| 100 | + |
| 101 | +// query callback representing the query of a chaincode |
| 102 | +func (t *SleeperChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { |
| 103 | + key := args[0] |
| 104 | + |
| 105 | + // Get the state from the ledger |
| 106 | + val, err := stub.GetState(key) |
| 107 | + if err != nil { |
| 108 | + return shim.Error(err.Error()) |
| 109 | + } |
| 110 | + |
| 111 | + sleepTime := args[1] |
| 112 | + |
| 113 | + //sleep for a bit |
| 114 | + t.sleep(sleepTime) |
| 115 | + |
| 116 | + return shim.Success(val) |
| 117 | +} |
| 118 | + |
| 119 | +func main() { |
| 120 | + err := shim.Start(new(SleeperChaincode)) |
| 121 | + if err != nil { |
| 122 | + fmt.Printf("Error starting Sleeper chaincode: %s", err) |
| 123 | + } |
| 124 | +} |
0 commit comments