Overview
Product Features
Underlying Engine
Strengths
Scenarios
Basic Concepts
/* * SimpleAssetDemo implements a simple chaincode to manage an asset */ public class SimpleAssetDemo extends ChaincodeBase { /* * Init is called during chaincode instantiation to initialize any data. */ @Override public Response init(ChaincodeStub stub) { } /* * Invoke is called per transaction on the chaincode. Each transaction is * either a 'get' or a 'set' on the asset created by Init function. The 'set' * method may create a new asset by specifying a new key-value pair. */ @Override public Response invoke(ChaincodeStub stub) { } public static void main(String[] args) { new SimpleAssetDemo().start(args); } }
/* * The init function is used to initialize two key-value pairs. The user-provided input parameters are KEY1_NAME, VALUE1, * KEY2_NAME, VALUE2 */ @Override public Response init(ChaincodeStub stub) { try { _logger.info("Init java simple chaincode"); List<String> args = stub.getParameters(); if (args.size() != 4) { newErrorResponse("Incorrect number of arguments. Expecting 4"); } // Initialize the chaincode String account1Key = args.get(0); int account1Value = Integer.parseInt(args.get(1)); String account2Key = args.get(2); int account2Value = Integer.parseInt(args.get(3)); _logger.info(String.format("account %s, value = %s; account %s, value %s", account1Key, account1Value, account2Key, account2Value)); stub.putStringState(account1Key, args.get(1)); stub.putStringState(account2Key, args.get(3)); return newSuccessResponse(); } catch (Throwable e) { return newErrorResponse(e); } }
// The invoke function subdivides the user-called function into several sub-functions, including invoke, delete, and query. @Override public Response invoke(ChaincodeStub stub) { try { _logger.info("Invoke java simple chaincode"); String func = stub.getFunction(); List<String> params = stub.getParameters(); if (func.equals("invoke")) { return invoke(stub, params); } if (func.equals("delete")) { return delete(stub, params); } if (func.equals("query")) { return query(stub, params); } return newErrorResponse("Invalid invoke function name. Expecting one of: [\\"invoke\\", \\"delete\\", \\"query\\"]"); } catch (Throwable e) { return newErrorResponse(e); } }
// The invoke function realizes the transfer of values between two keys. The input includes KEY1_NAME, KEY2_NAME, and VALUE. private Response invoke(ChaincodeStub stub, List<String> args) { if (args.size() != 3) { return newErrorResponse("Incorrect number of arguments. Expecting 3"); } String accountFromKey = args.get(0); String accountToKey = args.get(1); String accountFromValueStr = stub.getStringState(accountFromKey); if (accountFromValueStr == null) { return newErrorResponse(String.format("Entity %s not found", accountFromKey)); } int accountFromValue = Integer.parseInt(accountFromValueStr); String accountToValueStr = stub.getStringState(accountToKey); if (accountToValueStr == null) { return newErrorResponse(String.format("Entity %s not found", accountToKey)); } int accountToValue = Integer.parseInt(accountToValueStr); int amount = Integer.parseInt(args.get(2)); if (amount > accountFromValue) { return newErrorResponse(String.format("not enough money in account %s", accountFromKey)); } accountFromValue -= amount; accountToValue += amount; _logger.info(String.format("new value of A: %s", accountFromValue)); _logger.info(String.format("new value of B: %s", accountToValue)); stub.putStringState(accountFromKey, Integer.toString(accountFromValue)); stub.putStringState(accountToKey, Integer.toString(accountToValue)); _logger.info("Transfer complete"); return newSuccessResponse("invoke finished successfully", ByteString.copyFrom(accountFromKey + ": " + accountFromValue + " " + accountToKey + ": " + accountToValue, UTF_8).toByteArray()); }
// The delete function is used to delete the specified key from the ledger. The input is KEY_NAME. private Response delete(ChaincodeStub stub, List<String> args) { if (args.size() != 1) { return newErrorResponse("Incorrect number of arguments. Expecting 1"); } String key = args.get(0); // Delete the key from the state in ledger stub.delState(key); return newSuccessResponse(); }
// query mainly retrieves the corresponding value of a KEY. the input is KEY_NAME. private Response query(ChaincodeStub stub, List<String> args) { if (args.size() != 1) { return newErrorResponse("Incorrect number of arguments. Expecting NAME of the person to query"); } String KEY = args.get(0); // byte[] stateBytes String val = stub.getStringState(KEY); if (val == null) { return newErrorResponse(String.format("Error: state for %s is null", KEY)); } _logger.info(String.format("query Response: NAME: %s, Amount: %s ", KEY, val)); return newSuccessResponse(val, ByteString.copyFrom(val, UTF_8).toByteArray()); }
피드백