mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 23:45:10 +00:00
[FAB-11397] Adding java cc
Added java project sources under chaincode Added support for "-l java" to byfn.sh Change-Id: I7038aed9b21ad9bf51bcb58c6b71ceb1f161813f Signed-off-by: gennady <gennady@il.ibm.com>
This commit is contained in:
parent
bfdc0b6e7a
commit
4030ebdb3d
4 changed files with 187 additions and 0 deletions
34
chaincode/chaincode_example02/java/build.gradle
Normal file
34
chaincode/chaincode_example02/java/build.gradle
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright IBM Corp. 2018 All Rights Reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
plugins {
|
||||
id 'com.github.johnrengelman.shadow' version '2.0.3'
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group 'org.hyperledger.fabric'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile group: 'org.hyperledger.fabric', name: 'fabric-chaincode-shim', version: '1.3.0-SNAPSHOT'
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
baseName = 'chaincode'
|
||||
version = null
|
||||
classifier = null
|
||||
|
||||
manifest {
|
||||
attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
|
||||
}
|
||||
}
|
||||
7
chaincode/chaincode_example02/java/settings.gradle
Normal file
7
chaincode/chaincode_example02/java/settings.gradle
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/*
|
||||
* Copyright IBM Corp. 2017 All Rights Reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
rootProject.name = 'fabric-chaincode-example-gradle'
|
||||
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
Copyright IBM Corp., DTCC All Rights Reserved.
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
package org.hyperledger.fabric.example;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.netty.handler.ssl.OpenSsl;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.hyperledger.fabric.shim.ChaincodeBase;
|
||||
import org.hyperledger.fabric.shim.ChaincodeStub;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
public class SimpleChaincode extends ChaincodeBase {
|
||||
|
||||
private static Log _logger = LogFactory.getLog(SimpleChaincode.class);
|
||||
|
||||
@Override
|
||||
public Response init(ChaincodeStub stub) {
|
||||
try {
|
||||
_logger.info("Init java simple chaincode");
|
||||
String func = stub.getFunction();
|
||||
if (!func.equals("init")) {
|
||||
return newErrorResponse("function other than init is not supported");
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
// Deletes an entity from state
|
||||
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 callback representing the query of a chaincode
|
||||
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:\nName: %s, Amount: %s\n", key, val));
|
||||
return newSuccessResponse(val, ByteString.copyFrom(val, UTF_8).toByteArray());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("OpenSSL avaliable: " + OpenSsl.isAvailable());
|
||||
new SimpleChaincode().start(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -28,6 +28,10 @@ if [ "$LANGUAGE" = "node" ]; then
|
|||
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/node/"
|
||||
fi
|
||||
|
||||
if [ "$LANGUAGE" = "java" ]; then
|
||||
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/java/"
|
||||
fi
|
||||
|
||||
echo "Channel name : "$CHANNEL_NAME
|
||||
|
||||
# import utils
|
||||
|
|
|
|||
Loading…
Reference in a new issue