Skip to content

Commit 3e13a57

Browse files
[FAB-12960] Adding cc without main method
Java cc without main method should pass compilation, but its container should fail. After latest update to fabric it should return without waiting to timeout. Change-Id: I406745c52d462106d81ae6c5222e4a712e146908 Signed-off-by: gennady <[email protected]>
1 parent 40bf2c1 commit 3e13a57

File tree

4 files changed

+192
-1
lines changed

4 files changed

+192
-1
lines changed

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/FirstNetworkIntegrationTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ public void TestNoBuildChaincodeInstallInstantiateWithoutSrc() throws Exception
110110
assertThat(response.getMessage(), containsString("/chaincode/input/src/main"));
111111
}
112112

113+
@Test(timeout = 120000)
114+
public void TestNoMainChaincodeInstallInstantiate() throws Exception {
115+
116+
final CryptoSuite crypto = CryptoSuite.Factory.getCryptoSuite();
117+
118+
// Create client and set default crypto suite
119+
System.out.println("Creating client");
120+
final HFClient client = HFClient.createNewInstance();
121+
client.setCryptoSuite(crypto);
122+
123+
client.setUserContext(Utils.getAdminUserOrg1TLS());
124+
125+
Channel myChannel = Utils.getMyChannelFirstNetwork(client);
126+
127+
InstallProposalRequest installProposalRequest = generateNoMainInstallRequest(client, "nomaincc", true);
128+
Utils.sendInstallProposals(client, installProposalRequest, myChannel.getPeers().stream().filter(peer -> peer.getName().indexOf("org1") != -1).collect(Collectors.toList()));
129+
130+
// Instantiating chaincode
131+
List<Peer> peer0org1 = myChannel.getPeers().stream().filter(peer -> peer.getName().indexOf("peer0.org1") != -1).collect(Collectors.toList());
132+
InstantiateProposalRequest instantiateProposalRequest = generateInstantiateRequest(client, "nomaincc");
133+
ProposalResponse response = Utils.sendInstantiateProposalReturnFaulureResponse("nomaincc", instantiateProposalRequest, myChannel, peer0org1, myChannel.getOrderers());
134+
135+
assertThat(response.getMessage(), containsString("chaincode registration failed: container exited with 1"));
136+
}
137+
138+
113139
@Test
114140
public void TestSACCChaincodeInstallInstantiateInvokeQuery() throws Exception {
115141

@@ -188,7 +214,6 @@ void RunSBE(HFClient client, Channel channel, String mode) throws NoSuchAlgorit
188214
List<Peer> peer0org2 = Utils.getPeersFromChannel(channel, "peer0.org2");
189215
List<Peer> allpeers0 = Utils.getPeersFromChannel(channel, "peer0");
190216

191-
192217
client.setUserContext(Utils.getUser1Org1TLS());
193218
TransactionProposalRequest proposal = generateSBECCTransactionRequest(client, "setval", mode, "foo");
194219
Utils.sendTransactionProposalInvoke(proposal, channel, peer0org1, channel.getOrderers());
@@ -311,6 +336,10 @@ static public InstallProposalRequest generateNoBuildInstallRequest(HFClient clie
311336
return Utils.generateInstallRequest(client, name, "1.0", "src/test/resources/NoBuildCC", useSrcPrefix);
312337
}
313338

339+
static public InstallProposalRequest generateNoMainInstallRequest(HFClient client, String name, boolean useSrcPrefix) throws IOException, InvalidArgumentException {
340+
return Utils.generateInstallRequest(client, name, "1.0", "src/test/resources/NoMainCC", useSrcPrefix);
341+
}
342+
314343
static public InstantiateProposalRequest generateInstantiateRequest(HFClient client, String name) throws InvalidArgumentException, IOException, ChaincodeEndorsementPolicyParseException, ChaincodeCollectionConfigurationException {
315344
return Utils.generateInstantiateRequest(client, name, "1.0", "src/test/resources/chaincodeendorsementpolicy_2orgs.yaml", null, "init", "a", "100");
316345
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugins {
2+
id 'com.github.johnrengelman.shadow' version '2.0.3'
3+
id 'java'
4+
}
5+
6+
group 'org.hyperledger.fabric-chaincode-java'
7+
version '1.0-SNAPSHOT'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenLocal()
13+
mavenCentral()
14+
}
15+
16+
dependencies {
17+
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.0.0-SNAPSHOT'
18+
testCompile group: 'junit', name: 'junit', version: '4.12'
19+
}
20+
21+
shadowJar {
22+
baseName = 'chaincode'
23+
version = null
24+
classifier = null
25+
26+
manifest {
27+
attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
28+
}
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'fabric-chaincode-example-gradle'
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.hyperledger.fabric.example;
2+
3+
import java.util.List;
4+
5+
import com.google.protobuf.ByteString;
6+
import org.apache.commons.logging.Log;
7+
import org.apache.commons.logging.LogFactory;
8+
import org.hyperledger.fabric.shim.ChaincodeBase;
9+
import org.hyperledger.fabric.shim.ChaincodeStub;
10+
11+
import static java.nio.charset.StandardCharsets.UTF_8;
12+
13+
public class SimpleChaincode extends ChaincodeBase {
14+
15+
private static Log _logger = LogFactory.getLog(SimpleChaincode.class);
16+
17+
@Override
18+
public Response init(ChaincodeStub stub) {
19+
try {
20+
_logger.info("Init java simple chaincode");
21+
String func = stub.getFunction();
22+
if (!func.equals("init")) {
23+
return newErrorResponse("function other than init is not supported");
24+
}
25+
List<String> args = stub.getParameters();
26+
if (args.size() != 4) {
27+
newErrorResponse("Incorrect number of arguments. Expecting 4");
28+
}
29+
// Initialize the chaincode
30+
String account1Key = args.get(0);
31+
int account1Value = Integer.parseInt(args.get(1));
32+
String account2Key = args.get(2);
33+
int account2Value = Integer.parseInt(args.get(3));
34+
35+
_logger.info(String.format("account %s, value = %s; account %s, value %s", account1Key, account1Value, account2Key, account2Value));
36+
stub.putStringState(account1Key, args.get(1));
37+
stub.putStringState(account2Key, args.get(3));
38+
39+
return newSuccessResponse();
40+
} catch (Throwable e) {
41+
return newErrorResponse(e);
42+
}
43+
}
44+
45+
@Override
46+
public Response invoke(ChaincodeStub stub) {
47+
try {
48+
_logger.info("Invoke java simple chaincode");
49+
String func = stub.getFunction();
50+
List<String> params = stub.getParameters();
51+
if (func.equals("invoke")) {
52+
return invoke(stub, params);
53+
}
54+
if (func.equals("delete")) {
55+
return delete(stub, params);
56+
}
57+
if (func.equals("query")) {
58+
return query(stub, params);
59+
}
60+
return newErrorResponse("Invalid invoke function name. Expecting one of: [\"invoke\", \"delete\", \"query\"]");
61+
} catch (Throwable e) {
62+
return newErrorResponse(e);
63+
}
64+
}
65+
66+
private Response invoke(ChaincodeStub stub, List<String> args) {
67+
if (args.size() != 3) {
68+
return newErrorResponse("Incorrect number of arguments. Expecting 3");
69+
}
70+
String accountFromKey = args.get(0);
71+
String accountToKey = args.get(1);
72+
73+
String accountFromValueStr = stub.getStringState(accountFromKey);
74+
if (accountFromValueStr == null) {
75+
return newErrorResponse(String.format("Entity %s not found", accountFromKey));
76+
}
77+
int accountFromValue = Integer.parseInt(accountFromValueStr);
78+
79+
String accountToValueStr = stub.getStringState(accountToKey);
80+
if (accountToValueStr == null) {
81+
return newErrorResponse(String.format("Entity %s not found", accountToKey));
82+
}
83+
int accountToValue = Integer.parseInt(accountToValueStr);
84+
85+
int amount = Integer.parseInt(args.get(2));
86+
87+
if (amount > accountFromValue) {
88+
return newErrorResponse(String.format("not enough money in account %s", accountFromKey));
89+
}
90+
91+
accountFromValue -= amount;
92+
accountToValue += amount;
93+
94+
_logger.info(String.format("new value of A: %s", accountFromValue));
95+
_logger.info(String.format("new value of B: %s", accountToValue));
96+
97+
stub.putStringState(accountFromKey, Integer.toString(accountFromValue));
98+
stub.putStringState(accountToKey, Integer.toString(accountToValue));
99+
100+
_logger.info("Transfer complete");
101+
102+
return newSuccessResponse("invoke finished successfully", ByteString.copyFrom(accountFromKey + ": " + accountFromValue + " " + accountToKey + ": " + accountToValue, UTF_8).toByteArray());
103+
}
104+
105+
// Deletes an entity from state
106+
private Response delete(ChaincodeStub stub, List<String> args) {
107+
if (args.size() != 1) {
108+
return newErrorResponse("Incorrect number of arguments. Expecting 1");
109+
}
110+
String key = args.get(0);
111+
// Delete the key from the state in ledger
112+
stub.delState(key);
113+
return newSuccessResponse();
114+
}
115+
116+
// query callback representing the query of a chaincode
117+
private Response query(ChaincodeStub stub, List<String> args) {
118+
if (args.size() != 1) {
119+
return newErrorResponse("Incorrect number of arguments. Expecting name of the person to query");
120+
}
121+
String key = args.get(0);
122+
//byte[] stateBytes
123+
String val = stub.getStringState(key);
124+
if (val == null) {
125+
return newErrorResponse(String.format("Error: state for %s is null", key));
126+
}
127+
_logger.info(String.format("Query Response:\nName: %s, Amount: %s\n", key, val));
128+
return newSuccessResponse(val, ByteString.copyFrom(val, UTF_8).toByteArray());
129+
}
130+
131+
}

0 commit comments

Comments
 (0)