Skip to content

Commit

Permalink
[FAB-3221] Java cc getFunctionAndParameters()
Browse files Browse the repository at this point in the history
ChaincodeStub:
 - add getFunction()
 - add getParameters()
 - rename getArgsAsStrings() to getStringArgs()

Change-Id: Iea537353f3c40b205b8f5fb0213a85733b5dbdab
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Apr 26, 2017
1 parent 908af1a commit b306d7b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.hyperledger.fabric.shim;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
Expand All @@ -41,9 +40,31 @@ public interface ChaincodeStub {
* {@link Chaincode#init(ChaincodeStub)} or
* {@link Chaincode#invoke(ChaincodeStub)}.
*
* @return a list of arguments cast to a UTF-8 string
* @return a list of arguments cast to UTF-8 strings
*/
List<String> getArgsAsStrings();
List<String> getStringArgs();

/**
* A convenience method that returns the first argument of the chaincode
* invocation for use as a function name.
*
* The bytes of the first argument are decoded as a UTF-8 string.
*
* @return the function name
*/
String getFunction();

/**
* A convenience method that returns all except the first argument of the
* chaincode invocation for use as the parameters to the function returned
* by #{@link ChaincodeStub#getFunction()}.
*
* The bytes of the arguments are decoded as a UTF-8 strings and returned as
* a list of string parameters..
*
* @return a list of parameters
*/
List<String> getParameters();

/**
* Returns the transaction id
Expand Down Expand Up @@ -134,7 +155,7 @@ default Response invokeChaincode(String chaincodeName, List<byte[]> args) {
* @return
*/
default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String> args, String channel) {
return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(StandardCharsets.UTF_8)).collect(Collectors.toList()), channel);
return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(UTF_8)).collect(toList()), channel);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.hyperledger.fabric.shim;

import static java.util.stream.Collectors.toList;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -54,10 +56,26 @@ public List<byte[]> getArgs() {
* @see org.hyperledger.fabric.shim.ChaincodeStub#getArgsAsStrings()
*/
@Override
public List<String> getArgsAsStrings() {
public List<String> getStringArgs() {
return args.stream().map(x -> x.toStringUtf8()).collect(Collectors.toList());
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getFunction()
*/
@Override
public String getFunction() {
return getStringArgs().size() > 0 ? getStringArgs().get(0) : null;
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getParameters()
*/
@Override
public List<String> getParameters() {
return getStringArgs().stream().skip(1).collect(toList());
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#setEvent(java.lang.String, byte[])
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public Response init(ChaincodeStub stub) {
@Override
public Response invoke(ChaincodeStub stub) {
try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final List<String> args = argList.subList(0, argList.size());
final String function = stub.getFunction();
final List<String> args = stub.getParameters();

switch (function) {
case "init":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import static org.hyperledger.fabric.shim.ChaincodeHelper.newInternalServerErrorResponse;
import static org.hyperledger.fabric.shim.ChaincodeHelper.newSuccessResponse;

import java.util.List;

import javax.json.Json;

import org.apache.commons.logging.Log;
Expand All @@ -44,19 +42,18 @@ public class SimpleSample extends ChaincodeBase {

@Override
public Response init(ChaincodeStub stub) {
final List<String> args = stub.getArgsAsStrings();
if(!args.get(0).equals("init")) {
return newBadRequestResponse(format("Unknown function: %s", args.get(0)));
final String function = stub.getFunction();
if(!function.equals("init")) {
return newBadRequestResponse(format("Unknown function: %s", function));
}
return init(stub, args.stream().skip(1).toArray(String[]::new));
return init(stub, stub.getParameters().stream().toArray(String[]::new));
}

@Override
public Response invoke(ChaincodeStub stub) {
try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final String[] args = argList.stream().skip(1).toArray(String[]::new);
final String function = stub.getFunction();
final String[] args = stub.getParameters().stream().toArray(String[]::new);

switch (function) {
case "transfer":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public class Example02 extends ChaincodeBase {
@Override
public Response init(ChaincodeStub stub) {
try {
final List<String> args = stub.getArgsAsStrings();
switch (args.get(0)) {
final String function = stub.getFunction();
switch (function) {
case "init":
return init(stub, args.stream().skip(1).toArray(String[]::new));
return init(stub, stub.getParameters().stream().toArray(String[]::new));
default:
return newBadRequestResponse(format("Unknown function: %s", args.get(0)));
return newBadRequestResponse(format("Unknown function: %s", function));
}
} catch (NumberFormatException e) {
return newBadRequestResponse(e.toString());
Expand All @@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) {
public Response invoke(ChaincodeStub stub) {

try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final String[] args = argList.stream().skip(1).toArray(String[]::new);
final String function = stub.getFunction();
final String[] args = stub.getParameters().stream().toArray(String[]::new);

switch (function) {
case "invoke":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Response init(ChaincodeStub stub) {
// expects to be called with: { "init", key, value }
try {

final List<String> args = stub.getArgsAsStrings();
final List<String> args = stub.getStringArgs();
if(args.size() != 3) {
return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more.");
}
Expand All @@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) {
public Response invoke(ChaincodeStub stub) {
// expects to be called with: { "invoke"|"query", chaincodeName, key }
try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final String[] args = argList.stream().skip(1).toArray(String[]::new);
final String function = stub.getFunction();
final String[] args = stub.getParameters().stream().toArray(String[]::new);

switch (function) {
case "invoke":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Response init(ChaincodeStub stub) {
// expects to be called with: { "init", key, value }
try {

final List<String> args = stub.getArgsAsStrings();
final List<String> args = stub.getStringArgs();
if(args.size() != 3) {
return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more.");
}
Expand All @@ -57,9 +57,8 @@ public Response init(ChaincodeStub stub) {
public Response invoke(ChaincodeStub stub) {
// expects to be called with: { "invoke"|"query", chaincodeName, key }
try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final String[] args = argList.stream().skip(1).toArray(String[]::new);
final String function = stub.getFunction();
final String[] args = stub.getParameters().stream().toArray(String[]::new);

switch (function) {
case "invoke":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public Response init(ChaincodeStub stub) {

@Override
public Response invoke(ChaincodeStub stub) {
if(stub.getArgsAsStrings().isEmpty())
if(stub.getArgs().isEmpty())
return newBadRequestResponse(format("No arguments specified."));

switch (stub.getArgsAsStrings().get(0)) {
switch (stub.getStringArgs().get(0)) {
case "runtimeException":
throw new RuntimeException("Exception thrown as requested.");
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ public Response init(ChaincodeStub stub) {
public Response invoke(ChaincodeStub stub) {

try {
final List<String> argList = stub.getArgsAsStrings();
final String function = argList.get(0);
final String function = stub.getFunction();

switch (function) {
case "invoke":
return doInvoke(stub, argList.stream().skip(1).collect(toList()));
return doInvoke(stub, stub.getParameters());
case "query":
return doQuery(stub);
default:
Expand Down

0 comments on commit b306d7b

Please sign in to comment.