|
| 1 | +package org.hyperledger.fabric.example; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.google.protobuf.ByteString; |
| 6 | +import org.apache.commons.logging.Log; |
| 7 | +import org.apache.commons.logging.LogFactory; |
| 8 | +import org.hyperledger.fabric.shim.ChaincodeBase; |
| 9 | +import org.hyperledger.fabric.shim.ChaincodeStub; |
| 10 | + |
| 11 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 12 | + |
| 13 | +public class SimpleChaincode extends ChaincodeBase { |
| 14 | + |
| 15 | + private static Log _logger = LogFactory.getLog(SimpleChaincode.class); |
| 16 | + |
| 17 | + @Override |
| 18 | + public Response init(ChaincodeStub stub) { |
| 19 | + try { |
| 20 | + _logger.info("Init java simple chaincode"); |
| 21 | + String func = stub.getFunction(); |
| 22 | + if (!func.equals("init")) { |
| 23 | + return newErrorResponse("function other than init is not supported"); |
| 24 | + } |
| 25 | + List<String> args = stub.getParameters(); |
| 26 | + if (args.size() != 4) { |
| 27 | + newErrorResponse("Incorrect number of arguments. Expecting 4"); |
| 28 | + } |
| 29 | + // Initialize the chaincode |
| 30 | + String account1Key = args.get(0); |
| 31 | + int account1Value = Integer.parseInt(args.get(1)); |
| 32 | + String account2Key = args.get(2); |
| 33 | + int account2Value = Integer.parseInt(args.get(3)); |
| 34 | + |
| 35 | + _logger.info(String.format("account %s, value = %s; account %s, value %s", account1Key, account1Value, account2Key, account2Value)); |
| 36 | + stub.putStringState(account1Key, args.get(1)); |
| 37 | + stub.putStringState(account2Key, args.get(3)); |
| 38 | + |
| 39 | + return newSuccessResponse(); |
| 40 | + } catch (Throwable e) { |
| 41 | + return newErrorResponse(e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Response invoke(ChaincodeStub stub) { |
| 47 | + try { |
| 48 | + _logger.info("Invoke java simple chaincode"); |
| 49 | + String func = stub.getFunction(); |
| 50 | + List<String> params = stub.getParameters(); |
| 51 | + if (func.equals("invoke")) { |
| 52 | + return invoke(stub, params); |
| 53 | + } |
| 54 | + if (func.equals("delete")) { |
| 55 | + return delete(stub, params); |
| 56 | + } |
| 57 | + if (func.equals("query")) { |
| 58 | + return query(stub, params); |
| 59 | + } |
| 60 | + return newErrorResponse("Invalid invoke function name. Expecting one of: [\"invoke\", \"delete\", \"query\"]"); |
| 61 | + } catch (Throwable e) { |
| 62 | + return newErrorResponse(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private Response invoke(ChaincodeStub stub, List<String> args) { |
| 67 | + if (args.size() != 3) { |
| 68 | + return newErrorResponse("Incorrect number of arguments. Expecting 3"); |
| 69 | + } |
| 70 | + String accountFromKey = args.get(0); |
| 71 | + String accountToKey = args.get(1); |
| 72 | + |
| 73 | + String accountFromValueStr = stub.getStringState(accountFromKey); |
| 74 | + if (accountFromValueStr == null) { |
| 75 | + return newErrorResponse(String.format("Entity %s not found", accountFromKey)); |
| 76 | + } |
| 77 | + int accountFromValue = Integer.parseInt(accountFromValueStr); |
| 78 | + |
| 79 | + String accountToValueStr = stub.getStringState(accountToKey); |
| 80 | + if (accountToValueStr == null) { |
| 81 | + return newErrorResponse(String.format("Entity %s not found", accountToKey)); |
| 82 | + } |
| 83 | + int accountToValue = Integer.parseInt(accountToValueStr); |
| 84 | + |
| 85 | + int amount = Integer.parseInt(args.get(2)); |
| 86 | + |
| 87 | + if (amount > accountFromValue) { |
| 88 | + return newErrorResponse(String.format("not enough money in account %s", accountFromKey)); |
| 89 | + } |
| 90 | + |
| 91 | + accountFromValue -= amount; |
| 92 | + accountToValue += amount; |
| 93 | + |
| 94 | + _logger.info(String.format("new value of A: %s", accountFromValue)); |
| 95 | + _logger.info(String.format("new value of B: %s", accountToValue)); |
| 96 | + |
| 97 | + stub.putStringState(accountFromKey, Integer.toString(accountFromValue)); |
| 98 | + stub.putStringState(accountToKey, Integer.toString(accountToValue)); |
| 99 | + |
| 100 | + _logger.info("Transfer complete"); |
| 101 | + |
| 102 | + return newSuccessResponse("invoke finished successfully", ByteString.copyFrom(accountFromKey + ": " + accountFromValue + " " + accountToKey + ": " + accountToValue, UTF_8).toByteArray()); |
| 103 | + } |
| 104 | + |
| 105 | + // Deletes an entity from state |
| 106 | + private Response delete(ChaincodeStub stub, List<String> args) { |
| 107 | + if (args.size() != 1) { |
| 108 | + return newErrorResponse("Incorrect number of arguments. Expecting 1"); |
| 109 | + } |
| 110 | + String key = args.get(0); |
| 111 | + // Delete the key from the state in ledger |
| 112 | + stub.delState(key); |
| 113 | + return newSuccessResponse(); |
| 114 | + } |
| 115 | + |
| 116 | + // query callback representing the query of a chaincode |
| 117 | + private Response query(ChaincodeStub stub, List<String> args) { |
| 118 | + if (args.size() != 1) { |
| 119 | + return newErrorResponse("Incorrect number of arguments. Expecting name of the person to query"); |
| 120 | + } |
| 121 | + String key = args.get(0); |
| 122 | + //byte[] stateBytes |
| 123 | + String val = stub.getStringState(key); |
| 124 | + if (val == null) { |
| 125 | + return newErrorResponse(String.format("Error: state for %s is null", key)); |
| 126 | + } |
| 127 | + _logger.info(String.format("Query Response:\nName: %s, Amount: %s\n", key, val)); |
| 128 | + return newSuccessResponse(val, ByteString.copyFrom(val, UTF_8).toByteArray()); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments