-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABN-887 NodeSDK - deploy Java chaincode
Allow for the install and instantiation of Java chaincode. Change-Id: I25082c6f4a6241dc5b1eb8e5980eaea2fbc1735f Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
- Loading branch information
Showing
19 changed files
with
776 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2017, 2018 IBM All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const path = require('path'); | ||
const sbuf = require('stream-buffers'); | ||
const utils = require('../utils.js'); | ||
const walk = require('ignore-walk'); | ||
|
||
const logger = utils.getLogger('JavaPackager.js'); | ||
|
||
const BasePackager = require('./BasePackager'); | ||
|
||
class JavaPackager extends BasePackager { | ||
|
||
/** | ||
* Package chaincode source and metadata files for deployment. | ||
* @param {string} chaincodePath The path to the top-level directory containing the source code. | ||
* @param {string} [metadataPath] The path to the top-level directory containing metadata descriptors | ||
* @returns {Promise.<byte[]>} | ||
*/ | ||
async package (chaincodePath, metadataPath) { | ||
logger.debug('packaging Java source from %s', chaincodePath); | ||
|
||
const buffer = new sbuf.WritableStreamBuffer(); | ||
let descriptors = await this.findSource(chaincodePath); | ||
if (metadataPath){ | ||
logger.debug('packaging metadata files from %s', metadataPath); | ||
|
||
const metaDescriptors = await super.findMetadataDescriptors(metadataPath); | ||
descriptors = descriptors.concat(metaDescriptors); | ||
} | ||
await super.generateTarGz(descriptors, buffer); | ||
|
||
return buffer.getContents(); | ||
} | ||
|
||
/** | ||
* Given an input 'filePath', recursively parse the filesystem for any files | ||
* that fit the criteria for being valid java chaincode source | ||
* note: currently all files found in the source path are included | ||
* | ||
* @param filePath | ||
* @returns {Promise} | ||
*/ | ||
async findSource (filePath) { | ||
const descriptors = []; | ||
|
||
const files = await walk({path: filePath, follow: true}); | ||
if (files) { | ||
files.forEach((entry) => { | ||
const desc = { | ||
name: path.join('src', entry).split('\\').join('/'), // for windows style paths | ||
fqp: path.join(filePath, entry) | ||
}; | ||
|
||
logger.debug('adding descriptor entry', desc); | ||
descriptors.push(desc); | ||
}); | ||
} else { | ||
logger.debug(' No files found at this path %s', filePath); | ||
} | ||
|
||
return descriptors; | ||
} | ||
} | ||
|
||
module.exports = JavaPackager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'fabric-chaincode-example-gradle' |
158 changes: 158 additions & 0 deletions
158
.../src/java_cc/example_cc/src/main/java/org/hyperledger/fabric/example/SimpleChaincode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
# Copyright IBM Corp. All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.hyperledger.fabric.example; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
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("move")) { | ||
return move(stub, params); | ||
} | ||
if (func.equals("delete")) { | ||
return delete(stub, params); | ||
} | ||
if (func.equals("query")) { | ||
return query(stub, params); | ||
} | ||
if (func.equals("throwError")) { | ||
return newErrorResponse("throwError: an error occurred"); | ||
} | ||
return newErrorResponse("Invalid invoke function name. Expecting one of: [\"move\", \"delete\", \"query\"] but found::" + func); | ||
} catch (Throwable e) { | ||
return newErrorResponse(e); | ||
} | ||
} | ||
|
||
private Response move(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"); | ||
|
||
Map<String, byte[]> transientMap = stub.getTransient(); | ||
if (null != transientMap) { | ||
if (transientMap.containsKey("event") && transientMap.get("event") != null) { | ||
stub.setEvent("event", transientMap.get("event")); | ||
} | ||
if (transientMap.containsKey("result") && transientMap.get("result") != null) { | ||
return newSuccessResponse(transientMap.get("result")); | ||
} | ||
} | ||
return newSuccessResponse("move succeed",ByteString.copyFrom("move succeed", 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'fabric-chaincode-example-gradle' |
Oops, something went wrong.