|
| 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 | +//WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of |
| 20 | +//calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has |
| 21 | +//to be modified as well with the new ID of chaincode_example02. |
| 22 | +//chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of |
| 23 | +//hard-coding. |
| 24 | + |
| 25 | +import ( |
| 26 | + "errors" |
| 27 | + "fmt" |
| 28 | + "strconv" |
| 29 | + |
| 30 | + "github.com/hyperledger/fabric/core/chaincode/shim" |
| 31 | +) |
| 32 | + |
| 33 | +// SimpleChaincode example simple Chaincode implementation |
| 34 | +type SimpleChaincode struct { |
| 35 | +} |
| 36 | + |
| 37 | +// Init method of chaincode |
| 38 | +func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 39 | + _, args := stub.GetFunctionAndParameters() |
| 40 | + var A, B string // Entities |
| 41 | + var Aval, Bval int // Asset holdings |
| 42 | + var err error |
| 43 | + |
| 44 | + if len(args) != 4 { |
| 45 | + return nil, errors.New("Incorrect number of arguments. Expecting 4") |
| 46 | + } |
| 47 | + |
| 48 | + // Initialize the chaincode |
| 49 | + A = args[0] |
| 50 | + Aval, err = strconv.Atoi(args[1]) |
| 51 | + if err != nil { |
| 52 | + return nil, errors.New("Expecting integer value for asset holding") |
| 53 | + } |
| 54 | + B = args[2] |
| 55 | + Bval, err = strconv.Atoi(args[3]) |
| 56 | + if err != nil { |
| 57 | + return nil, errors.New("Expecting integer value for asset holding") |
| 58 | + } |
| 59 | + fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) |
| 60 | + |
| 61 | + // Write the state to the ledger |
| 62 | + err = stub.PutState(A, []byte(strconv.Itoa(Aval))) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + err = stub.PutState(B, []byte(strconv.Itoa(Bval))) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + return []byte("OK"), nil |
| 73 | +} |
| 74 | + |
| 75 | +// Invoke transaction makes payment of X units from A to B |
| 76 | +func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 77 | + _, args := stub.GetFunctionAndParameters() |
| 78 | + |
| 79 | + var A, B string // Entities |
| 80 | + var Aval, Bval int // Asset holdings |
| 81 | + var X int // Transaction value |
| 82 | + var err error |
| 83 | + |
| 84 | + if len(args) != 3 { |
| 85 | + return nil, errors.New("Incorrect number of arguments. Expecting 3") |
| 86 | + } |
| 87 | + |
| 88 | + A = args[0] |
| 89 | + B = args[1] |
| 90 | + |
| 91 | + // Get the state from the ledger |
| 92 | + // TODO: will be nice to have a GetAllState call to ledger |
| 93 | + Avalbytes, err := stub.GetState(A) |
| 94 | + if err != nil { |
| 95 | + return nil, errors.New("Failed to get state") |
| 96 | + } |
| 97 | + if Avalbytes == nil { |
| 98 | + return nil, errors.New("Entity not found") |
| 99 | + } |
| 100 | + Aval, _ = strconv.Atoi(string(Avalbytes)) |
| 101 | + |
| 102 | + Bvalbytes, err := stub.GetState(B) |
| 103 | + if err != nil { |
| 104 | + return nil, errors.New("Failed to get state") |
| 105 | + } |
| 106 | + if Bvalbytes == nil { |
| 107 | + return nil, errors.New("Entity not found") |
| 108 | + } |
| 109 | + Bval, _ = strconv.Atoi(string(Bvalbytes)) |
| 110 | + |
| 111 | + // Perform the execution |
| 112 | + X, err = strconv.Atoi(args[2]) |
| 113 | + if err != nil { |
| 114 | + return nil, errors.New("Invalid transaction amount, expecting a integer value") |
| 115 | + } |
| 116 | + Aval = Aval - X |
| 117 | + Bval = Bval + X |
| 118 | + fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) |
| 119 | + |
| 120 | + // Write the state back to the ledger |
| 121 | + err = stub.PutState(A, []byte(strconv.Itoa(Aval))) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + |
| 126 | + err = stub.PutState(B, []byte(strconv.Itoa(Bval))) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return []byte(fmt.Sprintf("{%d,%d}", Aval, Bval)), nil |
| 132 | +} |
| 133 | + |
| 134 | +// Query callback representing the query of a chaincode. Not used in 1.0 (remove when interface is removed) |
| 135 | +func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 136 | + return nil, nil |
| 137 | +} |
| 138 | + |
| 139 | +func main() { |
| 140 | + err := shim.Start(new(SimpleChaincode)) |
| 141 | + if err != nil { |
| 142 | + fmt.Printf("Error starting Simple chaincode: %s", err) |
| 143 | + } |
| 144 | +} |
0 commit comments