Search in sources :

Example 1 with ChaincodeInput

use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput in project fabric-sdk-java by hyperledger.

the class ProposalBuilder method createChaincodeInvocationSpec.

private ChaincodeInvocationSpec createChaincodeInvocationSpec(Chaincode.ChaincodeID chaincodeID, ChaincodeSpec.Type langType) {
    List<ByteString> allArgs = new ArrayList<>();
    if (argList != null && argList.size() > 0) {
        // If we already have an argList then the Builder subclasses have already set the arguments
        // for chaincodeInput. Accept the list and pass it on to the chaincodeInput builder
        // TODO need to clean this logic up so that common protobuf struct builds are in one place
        allArgs = argList;
    } else if (request != null) {
        // if argList is empty and we have a Request, build the chaincodeInput args array from the Request args and argbytes lists
        allArgs.add(ByteString.copyFrom(request.getFcn(), UTF_8));
        List<String> args = request.getArgs();
        if (args != null && args.size() > 0) {
            for (String arg : args) {
                allArgs.add(ByteString.copyFrom(arg.getBytes(UTF_8)));
            }
        }
        // TODO currently assume that chaincodeInput args are strings followed by byte[].
        // Either agree with Fabric folks that this will always be the case or modify all Builders to expect
        // a List of Objects and determine if each list item is a string or a byte array
        List<byte[]> argBytes = request.getArgBytes();
        if (argBytes != null && argBytes.size() > 0) {
            for (byte[] arg : argBytes) {
                allArgs.add(ByteString.copyFrom(arg));
            }
        }
    }
    if (IS_DEBUG_LEVEL) {
        StringBuilder logout = new StringBuilder(1000);
        logout.append(format("ChaincodeInvocationSpec type: %s, chaincode name: %s, chaincode path: %s, chaincode version: %s", langType.name(), chaincodeID.getName(), chaincodeID.getPath(), chaincodeID.getVersion()));
        String sep = "";
        logout.append(" args(");
        for (ByteString x : allArgs) {
            logout.append(sep).append("\"").append(logString(new String(x.toByteArray(), UTF_8))).append("\"");
            sep = ", ";
        }
        logout.append(")");
        logger.debug(logout.toString());
    }
    ChaincodeInput chaincodeInput = ChaincodeInput.newBuilder().addAllArgs(allArgs).build();
    ChaincodeSpec chaincodeSpec = ChaincodeSpec.newBuilder().setType(langType).setChaincodeId(chaincodeID).setInput(chaincodeInput).build();
    return ChaincodeInvocationSpec.newBuilder().setChaincodeSpec(chaincodeSpec).build();
}
Also used : ChaincodeInput(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput) ChaincodeSpec(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) Utils.logString(org.hyperledger.fabric.sdk.helper.Utils.logString)

Example 2 with ChaincodeInput

use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput in project fabric-sdk-java by hyperledger.

the class ProtoUtils method createDeploymentSpec.

public static ChaincodeDeploymentSpec createDeploymentSpec(Type ccType, String name, String chaincodePath, String chaincodeVersion, List<String> args, byte[] codePackage) {
    ChaincodeID.Builder chaincodeIDBuilder = ChaincodeID.newBuilder().setName(name).setVersion(chaincodeVersion);
    if (chaincodePath != null) {
        chaincodeIDBuilder = chaincodeIDBuilder.setPath(chaincodePath);
    }
    ChaincodeID chaincodeID = chaincodeIDBuilder.build();
    // build chaincodeInput
    List<ByteString> argList = new ArrayList<>(args == null ? 0 : args.size());
    if (args != null && args.size() != 0) {
        for (String arg : args) {
            argList.add(ByteString.copyFrom(arg.getBytes(UTF_8)));
        }
    }
    ChaincodeInput chaincodeInput = ChaincodeInput.newBuilder().addAllArgs(argList).build();
    // Construct the ChaincodeSpec
    ChaincodeSpec chaincodeSpec = ChaincodeSpec.newBuilder().setType(ccType).setChaincodeId(chaincodeID).setInput(chaincodeInput).build();
    if (isDebugLevel) {
        StringBuilder sb = new StringBuilder(1000);
        sb.append("ChaincodeDeploymentSpec chaincode cctype: ").append(ccType.name()).append(", name:").append(chaincodeID.getName()).append(", path: ").append(chaincodeID.getPath()).append(", version: ").append(chaincodeID.getVersion());
        String sep = "";
        sb.append(" args(");
        for (ByteString x : argList) {
            sb.append(sep).append("\"").append(logString(new String(x.toByteArray(), UTF_8))).append("\"");
            sep = ", ";
        }
        sb.append(")");
        logger.debug(sb.toString());
    }
    ChaincodeDeploymentSpec.Builder chaincodeDeploymentSpecBuilder = ChaincodeDeploymentSpec.newBuilder().setChaincodeSpec(// .setEffectiveDate(context.getFabricTimestamp())
    chaincodeSpec).setExecEnv(ChaincodeDeploymentSpec.ExecutionEnvironment.DOCKER);
    if (codePackage != null) {
        chaincodeDeploymentSpecBuilder.setCodePackage(ByteString.copyFrom(codePackage));
    }
    return chaincodeDeploymentSpecBuilder.build();
}
Also used : ChaincodeInput(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput) ChaincodeSpec(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec) ChaincodeDeploymentSpec(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ChaincodeID(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID) ByteString(com.google.protobuf.ByteString) Utils.logString(org.hyperledger.fabric.sdk.helper.Utils.logString) Utils.toHexString(org.hyperledger.fabric.sdk.helper.Utils.toHexString)

Aggregations

ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 ChaincodeInput (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput)2 ChaincodeSpec (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec)2 Utils.logString (org.hyperledger.fabric.sdk.helper.Utils.logString)2 List (java.util.List)1 ChaincodeDeploymentSpec (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec)1 ChaincodeID (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID)1 Utils.toHexString (org.hyperledger.fabric.sdk.helper.Utils.toHexString)1 ProtoUtils.getSignatureHeaderAsByteString (org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString)1