Skip to content

Commit

Permalink
FABJ-355 JSDK run Java chaincode
Browse files Browse the repository at this point in the history
Change-Id: Ib5ac19888d0e497046d46c9a6931d348d65fd8c5
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Sep 11, 2018
1 parent 8779804 commit 89f652d
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/test/fixture/sdkintegration/javacc/sample1/build.gradle
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'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'fabric-chaincode-example-gradle'

Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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);
}
return newErrorResponse("Invalid invoke function name. Expecting one of: [\"move\", \"delete\", \"query\"]");
} 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();
// 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.hyperledger.fabric.sdkintegration;

import java.io.IOException;

import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.TransactionRequest.Type;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.junit.Test;


/*
This runs a version of end2end but with Java chaincode.
It requires that End2endIT has been run already to do all enrollment and setting up of orgs,
creation of the channels. None of that is specific to chaincode deployment language.
*/

public class End2endJavaIT extends End2endIT {

{
testName = "End2endJavaIT"; //Just print out what test is really running.

// This is what changes are needed to deploy and run Node code.

// this is relative to src/test/fixture and is where the Node chaincode source is.
CHAIN_CODE_FILEPATH = "sdkintegration/javacc/sample1"; //override path to Node code
CHAIN_CODE_PATH = null; //This is used only for GO.
CHAIN_CODE_NAME = "example_cc_java"; // chaincode name.
CHAIN_CODE_LANG = Type.JAVA; //language is Java.
}

@Override
void blockWalker(HFClient client, Channel channel) throws InvalidArgumentException, ProposalException, IOException {
// block walker depends on the state of the chain after go's end2end. Nothing here is language specific so
// there is no loss in coverage for not doing this.
}

@Override
@Test
public void setup() throws Exception {
sampleStore = new SampleStore(sampleStoreFile);
enrollUsersSetup(sampleStore);
runFabricTest(sampleStore); // just run fabric tests.
}

@Override
Channel constructChannel(String name, HFClient client, SampleOrg sampleOrg) throws Exception {
// override this method since we don't want to construct the channel that's been done.
// Just get it out of the samplestore!

client.setUserContext(sampleOrg.getPeerAdmin());

return sampleStore.getChannel(client, name).initialize();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
UpdateChannelIT.class,
NetworkConfigIT.class,
End2endNodeIT.class,
End2endJavaIT.class,
End2endAndBackAgainNodeIT.class,
End2endIdemixIT.class,
PrivateDataIT.class,
Expand Down
3 changes: 2 additions & 1 deletion suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress checks="." files="src/test/fixture/sdkintegration/javacc/example_cc/src/main/java/example/SimpleChaincode.java"/>
<suppress checks="."
files="src/test/fixture/sdkintegration/javacc/sample1/src/main/java/org/hyperledger/fabric/example/SimpleChaincode.java"/>
</suppressions>

0 comments on commit 89f652d

Please sign in to comment.