Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit a44c2aa

Browse files
author
Saad Karim
committed
[FAB-7324] Deploy Node CC using Java SDK
Deploy Node CC using Java SDK Change-Id: Ic0db999944b9bf1528a1ca3b1f5e33ad3429da9f Signed-off-by: Saad Karim <skarim@us.ibm.com>
1 parent f28a4b7 commit a44c2aa

File tree

29 files changed

+1083
-684
lines changed

29 files changed

+1083
-684
lines changed

src/main/java/org/hyperledger/fabric/sdk/ChaincodeEndorsementPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> id
190190
mspRoleType = MSPRole.MSPRoleType.PEER;
191191
break;
192192
default:
193-
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, peer or orderer in role got %s ", key, name));
193+
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, or peer in role got %s ", key, name));
194194
}
195195

196196
MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType)

src/main/java/org/hyperledger/fabric/sdk/Channel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ public Collection<ProposalResponse> sendInstantiationProposal(InstantiateProposa
14921492
instantiateProposalbuilder.context(transactionContext);
14931493
instantiateProposalbuilder.argss(instantiateProposalRequest.getArgs());
14941494
instantiateProposalbuilder.chaincodeName(instantiateProposalRequest.getChaincodeName());
1495+
instantiateProposalbuilder.chaincodeType(instantiateProposalRequest.getChaincodeLanguage());
14951496
instantiateProposalbuilder.chaincodePath(instantiateProposalRequest.getChaincodePath());
14961497
instantiateProposalbuilder.chaincodeVersion(instantiateProposalRequest.getChaincodeVersion());
14971498
instantiateProposalbuilder.chaincodEndorsementPolicy(instantiateProposalRequest.getChaincodeEndorsementPolicy());

src/main/java/org/hyperledger/fabric/sdk/TransactionProposalRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public static TransactionProposalRequest newInstance(User userContext) {
2828

2929
}
3030

31+
public void setChaincodeLanguage(Type chaincodeLanguage) {
32+
this.chaincodeLanguage = chaincodeLanguage;
33+
}
34+
3135
/**
3236
* Transient data added to the proposal that is not added to the ledger.
3337
*

src/main/java/org/hyperledger/fabric/sdk/TransactionRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ public TransactionRequest setArgs(byte[]... args) {
219219
//Mirror Fabric try not expose any of its classes
220220
public enum Type {
221221
JAVA,
222-
GO_LANG
222+
GO_LANG,
223+
NODE
223224
}
224225

225226
public Type getChaincodeLanguage() {
@@ -229,7 +230,7 @@ public Type getChaincodeLanguage() {
229230
/**
230231
* The chaincode language type: default type Type.GO_LANG
231232
*
232-
* @param chaincodeLanguage . Type.Java Type.GO_LANG
233+
* @param chaincodeLanguage . Type.Java Type.GO_LANG Type.NODE
233234
*/
234235
public void setChaincodeLanguage(Type chaincodeLanguage) {
235236
this.chaincodeLanguage = chaincodeLanguage;

src/main/java/org/hyperledger/fabric/sdk/helper/DiagnosticFileDumper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public String createDiagnosticFile(byte[] bytes) {
8787

8888
}
8989

90+
public String createDiagnosticTarFile(byte[] bytes) {
91+
92+
return createDiagnosticFile(bytes, null, "tgz");
93+
94+
}
95+
9096
public String createDiagnosticFile(String msg) {
9197

9298
return createDiagnosticFile(msg.getBytes(StandardCharsets.UTF_8), null, null);

src/main/java/org/hyperledger/fabric/sdk/transaction/CSCCProposalBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.hyperledger.fabric.protos.peer.Chaincode;
1818
import org.hyperledger.fabric.protos.peer.FabricProposal;
19+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
1920
import org.hyperledger.fabric.sdk.exception.ProposalException;
2021

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

3435
@Override
35-
public FabricProposal.Proposal build() throws ProposalException {
36+
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
3637

3738
ccType(GOLANG);
3839
chaincodeID(CHAINCODE_ID_CSCC);

src/main/java/org/hyperledger/fabric/sdk/transaction/InstallProposalBuilder.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type;
3131
import org.hyperledger.fabric.protos.peer.FabricProposal;
3232
import org.hyperledger.fabric.sdk.TransactionRequest;
33+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3334
import org.hyperledger.fabric.sdk.exception.ProposalException;
35+
import org.hyperledger.fabric.sdk.helper.Config;
36+
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;
3437
import org.hyperledger.fabric.sdk.helper.Utils;
3538

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

4144
private static final Log logger = LogFactory.getLog(InstallProposalBuilder.class);
45+
private static final boolean IS_TRACE_LEVEL = logger.isTraceEnabled();
46+
47+
private static final Config config = Config.getConfig();
48+
private static final DiagnosticFileDumper diagnosticFileDumper = IS_TRACE_LEVEL
49+
? config.getDiagnosticFileDumper() : null;
4250

4351
private String chaincodePath;
4452

@@ -81,7 +89,7 @@ public InstallProposalBuilder setChaincodeSource(File chaincodeSource) {
8189
}
8290

8391
@Override
84-
public FabricProposal.Proposal build() throws ProposalException {
92+
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
8593

8694
constructInstallProposal();
8795
return super.build();
@@ -153,10 +161,30 @@ private void createNetModeTransaction() throws IOException {
153161
}
154162
break;
155163

164+
case NODE:
165+
166+
// chaincodePath is not applicable and must be null
167+
// chaincodeSource may be a File or InputStream
168+
169+
// Verify that chaincodePath is null
170+
if (!Utils.isNullOrEmpty(chaincodePath)) {
171+
throw new IllegalArgumentException("chaincodePath must be null for Node chaincode");
172+
}
173+
174+
dplang = "Node";
175+
ccType = Type.NODE;
176+
if (null != chaincodeSource) {
177+
178+
projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
179+
targetPathPrefix = "src"; //Paths.get("src", chaincodePath).toString();
180+
}
181+
break;
156182
default:
157183
throw new IllegalArgumentException("Unexpected chaincode language: " + chaincodeLanguage);
158184
}
159185

186+
ccType(ccType);
187+
160188
final byte[] data;
161189
String chaincodeID = chaincodeName + "::" + chaincodePath + "::" + chaincodeVersion;
162190

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

175-
logger.info(format("Installing '%s' %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s'",
203+
logger.info(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s'",
176204
chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix, chaincodePath));
177205

178206
// generate chaincode source tar
179207
data = Utils.generateTarGz(projectSourceDir, targetPathPrefix);
180208

209+
if (null != diagnosticFileDumper) {
210+
211+
logger.trace(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s' tar file dump %s",
212+
chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix,
213+
chaincodePath, diagnosticFileDumper.createDiagnosticTarFile(data)));
214+
}
215+
181216
} else {
182217
logger.info(format("Installing '%s' %s chaincode chaincodePath:'%s' from input stream",
183218
chaincodeID, dplang, chaincodePath));
184219
data = IOUtils.toByteArray(chaincodeInputStream);
220+
221+
if (null != diagnosticFileDumper) {
222+
logger.trace(format("Installing '%s' language %s chaincode from input stream",
223+
chaincodeID, dplang));
224+
}
225+
185226
}
186227

187228
final ChaincodeDeploymentSpec depspec = createDeploymentSpec(

src/main/java/org/hyperledger/fabric/sdk/transaction/InstantiateProposalBuilder.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import com.google.protobuf.ByteString;
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
import org.hyperledger.fabric.protos.peer.Chaincode;
2627
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec;
2728
import org.hyperledger.fabric.protos.peer.FabricProposal;
2829
import org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy;
30+
import org.hyperledger.fabric.sdk.TransactionRequest;
2931
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3032
import org.hyperledger.fabric.sdk.exception.ProposalException;
3133

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

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

7982
}
8083

84+
public InstantiateProposalBuilder chaincodeType(TransactionRequest.Type chaincodeType) {
85+
86+
this.chaincodeType = chaincodeType;
87+
88+
return this;
89+
90+
}
91+
8192
public void chaincodEndorsementPolicy(ChaincodeEndorsementPolicy policy) {
8293
if (policy != null) {
8394
this.chaincodePolicy = policy.getChaincodeEndorsementPolicyAsBytes();
@@ -90,31 +101,52 @@ public InstantiateProposalBuilder argss(List<String> argList) {
90101
}
91102

92103
@Override
93-
public FabricProposal.Proposal build() throws ProposalException {
104+
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
94105

95106
constructInstantiateProposal();
96107
return super.build();
97108
}
98109

99-
private void constructInstantiateProposal() throws ProposalException {
110+
private void constructInstantiateProposal() throws ProposalException, InvalidArgumentException {
100111

101112
try {
102113

103114
createNetModeTransaction();
104115

116+
} catch (InvalidArgumentException exp) {
117+
logger.error(exp);
118+
throw exp;
105119
} catch (Exception exp) {
106120
logger.error(exp);
107121
throw new ProposalException("IO Error while creating install transaction", exp);
108122
}
109123
}
110124

111-
private void createNetModeTransaction() {
125+
private void createNetModeTransaction() throws InvalidArgumentException {
112126
logger.debug("NetModeTransaction");
113127

128+
if (chaincodeType == null) {
129+
throw new InvalidArgumentException("Chaincode type is required");
130+
}
131+
114132
List<String> modlist = new LinkedList<>();
115133
modlist.add("init");
116134
modlist.addAll(argList);
117135

136+
switch (chaincodeType) {
137+
case JAVA:
138+
ccType(Chaincode.ChaincodeSpec.Type.JAVA);
139+
break;
140+
case NODE:
141+
ccType(Chaincode.ChaincodeSpec.Type.NODE);
142+
break;
143+
case GO_LANG:
144+
ccType(Chaincode.ChaincodeSpec.Type.GOLANG);
145+
break;
146+
default:
147+
throw new InvalidArgumentException("Requested chaincode type is not supported: " + chaincodeType);
148+
}
149+
118150
ChaincodeDeploymentSpec depspec = createDeploymentSpec(ccType,
119151
chaincodeName, chaincodePath, chaincodeVersion, modlist, null);
120152

src/main/java/org/hyperledger/fabric/sdk/transaction/LSCCProposalBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
import org.hyperledger.fabric.protos.peer.Chaincode;
1717
import org.hyperledger.fabric.protos.peer.FabricProposal;
18+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
1819
import org.hyperledger.fabric.sdk.exception.ProposalException;
20+
import org.omg.CORBA.DynAnyPackage.Invalid;
1921

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

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

3335
@Override
34-
public FabricProposal.Proposal build() throws ProposalException {
36+
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
3537

36-
ccType(GOLANG);
3738
chaincodeID(CHAINCODE_ID_LSCC);
3839

3940
return super.build();

src/main/java/org/hyperledger/fabric/sdk/transaction/ProposalBuilder.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeHeaderExtension;
3535
import org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeProposalPayload;
3636
import org.hyperledger.fabric.sdk.TransactionRequest;
37+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3738
import org.hyperledger.fabric.sdk.exception.ProposalException;
3839

3940
import static java.lang.String.format;
@@ -82,19 +83,31 @@ public ProposalBuilder context(TransactionContext context) {
8283
return this;
8384
}
8485

85-
public ProposalBuilder request(TransactionRequest request) {
86+
public ProposalBuilder request(TransactionRequest request) throws InvalidArgumentException {
8687
this.request = request;
8788

8889
chaincodeID(request.getChaincodeID().getFabricChaincodeID());
89-
ccType(request.getChaincodeLanguage() == TransactionRequest.Type.JAVA ?
90-
Chaincode.ChaincodeSpec.Type.JAVA : Chaincode.ChaincodeSpec.Type.GOLANG);
90+
91+
switch (request.getChaincodeLanguage()) {
92+
case JAVA:
93+
ccType(Chaincode.ChaincodeSpec.Type.JAVA);
94+
break;
95+
case NODE:
96+
ccType(Chaincode.ChaincodeSpec.Type.NODE);
97+
break;
98+
case GO_LANG:
99+
ccType(Chaincode.ChaincodeSpec.Type.GOLANG);
100+
break;
101+
default:
102+
throw new InvalidArgumentException("Requested chaincode type is not supported: " + request.getChaincodeLanguage());
103+
}
91104

92105
transientMap = request.getTransientMap();
93106

94107
return this;
95108
}
96109

97-
public FabricProposal.Proposal build() throws ProposalException {
110+
public FabricProposal.Proposal build() throws ProposalException, InvalidArgumentException {
98111
if (request != null && request.noChannelID()) {
99112
channelID = "";
100113
}

0 commit comments

Comments
 (0)