use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec in project fabric-sdk-java by hyperledger.
the class InstallProposalBuilder method createNetModeTransaction.
private void createNetModeTransaction() throws IOException {
logger.debug("createNetModeTransaction");
if (null == chaincodeSource && chaincodeInputStream == null) {
throw new IllegalArgumentException("Missing chaincodeSource or chaincodeInputStream in InstallRequest");
}
if (null != chaincodeSource && chaincodeInputStream != null) {
throw new IllegalArgumentException("Both chaincodeSource and chaincodeInputStream in InstallRequest were set. Specify one or the other");
}
final Type ccType;
File projectSourceDir = null;
String targetPathPrefix = null;
String dplang;
File metainf = null;
if (null != chaincodeMetaInfLocation) {
if (!chaincodeMetaInfLocation.exists()) {
throw new IllegalArgumentException(format("Directory to find chaincode META-INF %s does not exist", chaincodeMetaInfLocation.getAbsolutePath()));
}
if (!chaincodeMetaInfLocation.isDirectory()) {
throw new IllegalArgumentException(format("Directory to find chaincode META-INF %s is not a directory", chaincodeMetaInfLocation.getAbsolutePath()));
}
metainf = new File(chaincodeMetaInfLocation, "META-INF");
logger.trace("META-INF directory is " + metainf.getAbsolutePath());
if (!metainf.exists()) {
throw new IllegalArgumentException(format("The META-INF directory does not exist in %s", chaincodeMetaInfLocation.getAbsolutePath()));
}
if (!metainf.isDirectory()) {
throw new IllegalArgumentException(format("The META-INF in %s is not a directory.", chaincodeMetaInfLocation.getAbsolutePath()));
}
File[] files = metainf.listFiles();
if (files == null) {
throw new IllegalArgumentException("null for listFiles on: " + chaincodeMetaInfLocation.getAbsolutePath());
}
if (files.length < 1) {
throw new IllegalArgumentException(format("The META-INF directory %s is empty.", metainf.getAbsolutePath()));
}
logger.trace(format("chaincode META-INF found %s", metainf.getAbsolutePath()));
}
switch(chaincodeLanguage) {
case GO_LANG:
// Verify that chaincodePath is being passed
if (Utils.isNullOrEmpty(chaincodePath)) {
throw new IllegalArgumentException("Missing chaincodePath in InstallRequest");
}
dplang = "Go";
ccType = Type.GOLANG;
if (null != chaincodeSource) {
projectSourceDir = Paths.get(chaincodeSource.toString(), "src", chaincodePath).toFile();
targetPathPrefix = Paths.get("src", chaincodePath).toString();
}
break;
case JAVA:
// Verify that chaincodePath is null
if (!Utils.isNullOrEmpty(chaincodePath)) {
throw new IllegalArgumentException("chaincodePath must be null for Java chaincode");
}
dplang = "Java";
ccType = Type.JAVA;
if (null != chaincodeSource) {
targetPathPrefix = "src";
projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
}
break;
case NODE:
// Verify that chaincodePath is null
if (!Utils.isNullOrEmpty(chaincodePath)) {
throw new IllegalArgumentException("chaincodePath must be null for Node chaincode");
}
dplang = "Node";
ccType = Type.NODE;
if (null != chaincodeSource) {
projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
// Paths.get("src", chaincodePath).toString();
targetPathPrefix = "src";
}
break;
default:
throw new IllegalArgumentException("Unexpected chaincode language: " + chaincodeLanguage);
}
ccType(ccType);
final byte[] data;
String chaincodeID = chaincodeName + "::" + chaincodePath + "::" + chaincodeVersion;
if (chaincodeSource != null) {
if (!projectSourceDir.exists()) {
final String message = "The project source directory does not exist: " + projectSourceDir.getAbsolutePath();
logger.error(message);
throw new IllegalArgumentException(message);
}
if (!projectSourceDir.isDirectory()) {
final String message = "The project source directory is not a directory: " + projectSourceDir.getAbsolutePath();
logger.error(message);
throw new IllegalArgumentException(message);
}
logger.info(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s'", chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix, chaincodePath));
// generate chaincode source tar
data = Utils.generateTarGz(projectSourceDir, targetPathPrefix, metainf);
if (null != diagnosticFileDumper) {
logger.trace(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s' tar file dump %s", chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix, chaincodePath, diagnosticFileDumper.createDiagnosticTarFile(data)));
}
} else {
logger.info(format("Installing '%s' %s chaincode chaincodePath:'%s' from input stream", chaincodeID, dplang, chaincodePath));
data = IOUtils.toByteArray(chaincodeInputStream);
if (null != diagnosticFileDumper) {
logger.trace(format("Installing '%s' language %s chaincode from input stream tar file dump %s", chaincodeID, dplang, diagnosticFileDumper.createDiagnosticTarFile(data)));
}
}
final ChaincodeDeploymentSpec depspec = createDeploymentSpec(ccType, this.chaincodeName, this.chaincodePath, this.chaincodeVersion, null, data);
// set args
final List<ByteString> argList = new ArrayList<>();
argList.add(ByteString.copyFrom(action, StandardCharsets.UTF_8));
argList.add(depspec.toByteString());
args(argList);
}
use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec in project fabric-sdk-java by hyperledger.
the class InstantiateProposalBuilder method createNetModeTransaction.
private void createNetModeTransaction() throws InvalidArgumentException {
logger.debug("NetModeTransaction");
if (chaincodeType == null) {
throw new InvalidArgumentException("Chaincode type is required");
}
List<String> modlist = new LinkedList<>();
modlist.add("init");
modlist.addAll(argList);
switch(chaincodeType) {
case JAVA:
ccType(Chaincode.ChaincodeSpec.Type.JAVA);
break;
case NODE:
ccType(Chaincode.ChaincodeSpec.Type.NODE);
break;
case GO_LANG:
ccType(Chaincode.ChaincodeSpec.Type.GOLANG);
break;
default:
throw new InvalidArgumentException("Requested chaincode type is not supported: " + chaincodeType);
}
ChaincodeDeploymentSpec depspec = createDeploymentSpec(ccType, chaincodeName, chaincodePath, chaincodeVersion, modlist, null);
List<ByteString> argList = new ArrayList<>();
argList.add(ByteString.copyFrom(action, StandardCharsets.UTF_8));
argList.add(ByteString.copyFrom(context.getChannelID(), StandardCharsets.UTF_8));
argList.add(depspec.toByteString());
if (chaincodePolicy != null) {
argList.add(ByteString.copyFrom(chaincodePolicy));
}
args(argList);
}
use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec 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();
}
Aggregations