Skip to content

Commit

Permalink
[FAB-7324] Deploy Node CC using Java SDK
Browse files Browse the repository at this point in the history
Deploy Node CC using Java SDK

Change-Id: Ic0db999944b9bf1528a1ca3b1f5e33ad3429da9f
Signed-off-by: Saad Karim <skarim@us.ibm.com>
  • Loading branch information
Saad Karim committed Feb 21, 2018
1 parent f28a4b7 commit a44c2aa
Show file tree
Hide file tree
Showing 29 changed files with 1,083 additions and 684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> id
mspRoleType = MSPRole.MSPRoleType.PEER;
break;
default:
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, peer or orderer in role got %s ", key, name));
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, or peer in role got %s ", key, name));
}

MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ public Collection<ProposalResponse> sendInstantiationProposal(InstantiateProposa
instantiateProposalbuilder.context(transactionContext);
instantiateProposalbuilder.argss(instantiateProposalRequest.getArgs());
instantiateProposalbuilder.chaincodeName(instantiateProposalRequest.getChaincodeName());
instantiateProposalbuilder.chaincodeType(instantiateProposalRequest.getChaincodeLanguage());
instantiateProposalbuilder.chaincodePath(instantiateProposalRequest.getChaincodePath());
instantiateProposalbuilder.chaincodeVersion(instantiateProposalRequest.getChaincodeVersion());
instantiateProposalbuilder.chaincodEndorsementPolicy(instantiateProposalRequest.getChaincodeEndorsementPolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public static TransactionProposalRequest newInstance(User userContext) {

}

public void setChaincodeLanguage(Type chaincodeLanguage) {
this.chaincodeLanguage = chaincodeLanguage;
}

/**
* Transient data added to the proposal that is not added to the ledger.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ public TransactionRequest setArgs(byte[]... args) {
//Mirror Fabric try not expose any of its classes
public enum Type {
JAVA,
GO_LANG
GO_LANG,
NODE
}

public Type getChaincodeLanguage() {
Expand All @@ -229,7 +230,7 @@ public Type getChaincodeLanguage() {
/**
* The chaincode language type: default type Type.GO_LANG
*
* @param chaincodeLanguage . Type.Java Type.GO_LANG
* @param chaincodeLanguage . Type.Java Type.GO_LANG Type.NODE
*/
public void setChaincodeLanguage(Type chaincodeLanguage) {
this.chaincodeLanguage = chaincodeLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public String createDiagnosticFile(byte[] bytes) {

}

public String createDiagnosticTarFile(byte[] bytes) {

return createDiagnosticFile(bytes, null, "tgz");

}

public String createDiagnosticFile(String msg) {

return createDiagnosticFile(msg.getBytes(StandardCharsets.UTF_8), null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.hyperledger.fabric.protos.peer.Chaincode;
import org.hyperledger.fabric.protos.peer.FabricProposal;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;

import static org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type.GOLANG;
Expand All @@ -32,7 +33,7 @@ public CSCCProposalBuilder context(TransactionContext context) {
}

@Override
public FabricProposal.Proposal build() throws ProposalException {
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {

ccType(GOLANG);
chaincodeID(CHAINCODE_ID_CSCC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type;
import org.hyperledger.fabric.protos.peer.FabricProposal;
import org.hyperledger.fabric.sdk.TransactionRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;
import org.hyperledger.fabric.sdk.helper.Utils;

import static java.lang.String.format;
Expand All @@ -39,6 +42,11 @@
public class InstallProposalBuilder extends LSCCProposalBuilder {

private static final Log logger = LogFactory.getLog(InstallProposalBuilder.class);
private static final boolean IS_TRACE_LEVEL = logger.isTraceEnabled();

private static final Config config = Config.getConfig();
private static final DiagnosticFileDumper diagnosticFileDumper = IS_TRACE_LEVEL
? config.getDiagnosticFileDumper() : null;

private String chaincodePath;

Expand Down Expand Up @@ -81,7 +89,7 @@ public InstallProposalBuilder setChaincodeSource(File chaincodeSource) {
}

@Override
public FabricProposal.Proposal build() throws ProposalException {
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {

constructInstallProposal();
return super.build();
Expand Down Expand Up @@ -153,10 +161,30 @@ private void createNetModeTransaction() throws IOException {
}
break;

case NODE:

// chaincodePath is not applicable and must be null
// chaincodeSource may be a File or InputStream

// 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();
targetPathPrefix = "src"; //Paths.get("src", chaincodePath).toString();
}
break;
default:
throw new IllegalArgumentException("Unexpected chaincode language: " + chaincodeLanguage);
}

ccType(ccType);

final byte[] data;
String chaincodeID = chaincodeName + "::" + chaincodePath + "::" + chaincodeVersion;

Expand All @@ -172,16 +200,29 @@ private void createNetModeTransaction() throws IOException {
throw new IllegalArgumentException(message);
}

logger.info(format("Installing '%s' %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s'",
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);

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",
chaincodeID, dplang));
}

}

final ChaincodeDeploymentSpec depspec = createDeploymentSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import com.google.protobuf.ByteString;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.protos.peer.Chaincode;
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec;
import org.hyperledger.fabric.protos.peer.FabricProposal;
import org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy;
import org.hyperledger.fabric.sdk.TransactionRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;

Expand All @@ -40,6 +42,7 @@ public class InstantiateProposalBuilder extends LSCCProposalBuilder {
private String chaincodeName;
private List<String> argList;
private String chaincodeVersion;
private TransactionRequest.Type chaincodeType = TransactionRequest.Type.GO_LANG;

private byte[] chaincodePolicy = null;
protected String action = "deploy";
Expand Down Expand Up @@ -78,6 +81,14 @@ public InstantiateProposalBuilder chaincodeName(String chaincodeName) {

}

public InstantiateProposalBuilder chaincodeType(TransactionRequest.Type chaincodeType) {

this.chaincodeType = chaincodeType;

return this;

}

public void chaincodEndorsementPolicy(ChaincodeEndorsementPolicy policy) {
if (policy != null) {
this.chaincodePolicy = policy.getChaincodeEndorsementPolicyAsBytes();
Expand All @@ -90,31 +101,52 @@ public InstantiateProposalBuilder argss(List<String> argList) {
}

@Override
public FabricProposal.Proposal build() throws ProposalException {
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {

constructInstantiateProposal();
return super.build();
}

private void constructInstantiateProposal() throws ProposalException {
private void constructInstantiateProposal() throws ProposalException, InvalidArgumentException {

try {

createNetModeTransaction();

} catch (InvalidArgumentException exp) {
logger.error(exp);
throw exp;
} catch (Exception exp) {
logger.error(exp);
throw new ProposalException("IO Error while creating install transaction", exp);
}
}

private void 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import org.hyperledger.fabric.protos.peer.Chaincode;
import org.hyperledger.fabric.protos.peer.FabricProposal;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.omg.CORBA.DynAnyPackage.Invalid;

import static org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type.GOLANG;

Expand All @@ -31,9 +33,8 @@ public LSCCProposalBuilder context(TransactionContext context) {
}

@Override
public FabricProposal.Proposal build() throws ProposalException {
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {

ccType(GOLANG);
chaincodeID(CHAINCODE_ID_LSCC);

return super.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeHeaderExtension;
import org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeProposalPayload;
import org.hyperledger.fabric.sdk.TransactionRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;

import static java.lang.String.format;
Expand Down Expand Up @@ -82,19 +83,31 @@ public ProposalBuilder context(TransactionContext context) {
return this;
}

public ProposalBuilder request(TransactionRequest request) {
public ProposalBuilder request(TransactionRequest request) throws InvalidArgumentException {
this.request = request;

chaincodeID(request.getChaincodeID().getFabricChaincodeID());
ccType(request.getChaincodeLanguage() == TransactionRequest.Type.JAVA ?
Chaincode.ChaincodeSpec.Type.JAVA : Chaincode.ChaincodeSpec.Type.GOLANG);

switch (request.getChaincodeLanguage()) {
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: " + request.getChaincodeLanguage());
}

transientMap = request.getTransientMap();

return this;
}

public FabricProposal.Proposal build() throws ProposalException {
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
if (request != null && request.noChannelID()) {
channelID = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,8 @@ public int read(User registrar) throws AffiliationException, InvalidArgumentExce

logger.debug(format("affiliation url: %s, registrar: %s done.", readAffURL, registrar));
HFCAAffiliationResp resp = getResponse(result);
System.out.println("aff read resp: " + result);
System.out.println("resp.getChildren().size(): " + resp.getChildren().size());
this.childHFCAAffiliations = resp.getChildren();

System.out.println("this.childHFCAAffiliations: " + this.childHFCAAffiliations.size());
this.identities = resp.getIdentities();
this.deleted = false;
return resp.statusCode;
Expand Down
Loading

0 comments on commit a44c2aa

Please sign in to comment.