Skip to content

Commit 72dc716

Browse files
committed
[FAB-15214] Remove init annotation
Change-Id: Icca0581f451227fd0a1b68b3e02255b012b60ac7 Signed-off-by: James Taylor <[email protected]>
1 parent f581165 commit 72dc716

File tree

8 files changed

+65
-80
lines changed

8 files changed

+65
-80
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hyperledger.fabric.contract;
88

99
import org.hyperledger.fabric.shim.ChaincodeStub;
10+
import org.hyperledger.fabric.shim.ResponseUtils;
1011

1112
/**
1213
* Interface all contracts should implement
@@ -29,7 +30,7 @@ public interface ContractInterface {
2930
* Invoked once method for transaction not exist in contract
3031
*/
3132
default void unknownTransaction() {
32-
throw new IllegalStateException("Undefined contract method called");
33+
throw new IllegalStateException("Undefined contract method called");
3334
}
3435

3536
/**

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/annotation/Init.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractExecutionService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.commons.logging.LogFactory;
2121
import org.hyperledger.fabric.contract.Context;
2222
import org.hyperledger.fabric.contract.ContractInterface;
23-
import org.hyperledger.fabric.contract.annotation.Init;
2423
import org.hyperledger.fabric.contract.annotation.Transaction;
2524
import org.hyperledger.fabric.contract.execution.ExecutionService;
2625
import org.hyperledger.fabric.contract.execution.InvocationRequest;
@@ -119,7 +118,7 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
119118
if (method.getName().equals("getContext")) {
120119
return context.get();
121120
} else if (method.getDeclaringClass() != Object.class && method.getDeclaringClass() != ContractInterface.class &&
122-
(method.getAnnotation(Init.class) != null || method.getAnnotation(Transaction.class) != null)) {
121+
method.getAnnotation(Transaction.class) != null) {
123122
contractObject.beforeTransaction();
124123
Object result = proxy.invokeSuper(obj, args);
125124
contractObject.afterTransaction();

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/TransactionType.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.hyperledger.fabric.contract.routing;
77

88
public enum TransactionType {
9-
INIT,
109
INVOKE,
1110
QUERY,
1211
DEFAULT

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hyperledger.fabric.contract.ContractInterface;
2121
import org.hyperledger.fabric.contract.annotation.Contract;
2222
import org.hyperledger.fabric.contract.annotation.DataType;
23-
import org.hyperledger.fabric.contract.annotation.Init;
2423
import org.hyperledger.fabric.contract.annotation.Transaction;
2524
import org.hyperledger.fabric.contract.execution.InvocationRequest;
2625
import org.hyperledger.fabric.contract.routing.ContractDefinition;
@@ -139,9 +138,9 @@ public void findAndSetContracts(TypeRegistry typeRegistry) {
139138
if (!seenClass.contains(className)) {
140139
ContractDefinition contract = addNewContract(cl);
141140

142-
logger.debug("Searching init and invoke annotated methods");
141+
logger.debug("Searching annotated methods");
143142
for (Method m : cl.getMethods()) {
144-
if ((m.getAnnotation(Transaction.class) != null) || (m.getAnnotation(Init.class) != null)) {
143+
if (m.getAnnotation(Transaction.class) != null) {
145144
logger.debug("Found annotated method " + m.getName());
146145

147146
contract.addTxFunction(m);

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TxFunctionImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import org.hyperledger.fabric.Logger;
1111
import org.hyperledger.fabric.contract.ContractInterface;
12-
import org.hyperledger.fabric.contract.annotation.Init;
1312
import org.hyperledger.fabric.contract.annotation.Transaction;
1413
import org.hyperledger.fabric.contract.routing.ContractDefinition;
1514
import org.hyperledger.fabric.contract.routing.TransactionType;
@@ -76,10 +75,6 @@ public TxFunctionImpl(Method m, ContractDefinition contract) {
7675
}
7776

7877
}
79-
if (m.getAnnotation(Init.class) != null) {
80-
this.type = TransactionType.INIT;
81-
logger.debug(()-> "Found Init method: " + m.getName());
82-
}
8378

8479
this.routing = new RoutingImpl(m,contract.getContractImpl());
8580

fabric-chaincode-shim/src/test/java/contract/SampleContract.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.hyperledger.fabric.contract.ContractInterface;
1212
import org.hyperledger.fabric.contract.annotation.Contract;
1313
import org.hyperledger.fabric.contract.annotation.Default;
14-
import org.hyperledger.fabric.contract.annotation.Init;
1514
import org.hyperledger.fabric.contract.annotation.Transaction;
1615

1716
import io.swagger.v3.oas.annotations.info.Contact;
@@ -31,11 +30,9 @@ public class SampleContract implements ContractInterface {
3130
static public int t1Invoked = 0;
3231
static public int i1Invoked = 0;
3332

34-
@Init
35-
public String i1() {
36-
i1Invoked++;
37-
System.out.println("SampleContract::Init Done");
38-
return "Init done";
33+
@Transaction
34+
public String t3() {
35+
throw new RuntimeException("T3 fail!");
3936
}
4037

4138
@Transaction

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.hamcrest.Matchers.equalTo;
1111
import static org.hamcrest.Matchers.is;
1212
import static org.hamcrest.Matchers.notNullValue;
13+
import static org.hamcrest.Matchers.startsWith;
1314
import static org.junit.Assert.assertThat;
1415

1516
import java.util.ArrayList;
@@ -36,31 +37,17 @@ public void testCreateAndScan() {
3637
r.findAllContracts();
3738
ChaincodeStub s = new ChaincodeStubNaiveImpl();
3839

39-
// Test Init routing
40-
List<String> args = new ArrayList<>();
41-
args.add("samplecontract:i1");
42-
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
43-
InvocationRequest request = ExecutionFactory.getInstance().createRequest(s);
44-
assertThat(request.getNamespace(), is(equalTo(SampleContract.class.getAnnotation(Contract.class).namespace())));
45-
assertThat(request.getMethod(), is(equalTo("i1")));
46-
assertThat(request.getRequestName(), is(equalTo("samplecontract:i1")));
47-
assertThat(request.getArgs(), is(empty()));
48-
org.hyperledger.fabric.contract.routing.TxFunction.Routing routing = r.getRouting(request);
49-
assertThat(routing.getContractClass().getName(), is(equalTo(SampleContract.class.getName())));
50-
assertThat(routing.getMethod().getName(), is(equalTo("i1")));
51-
//
52-
5340
// Test Transaction routing
54-
args = new ArrayList<>();
41+
List<String> args = new ArrayList<>();
5542
args.add("samplecontract:t1");
5643
args.add("asdf");
5744
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
58-
request = ExecutionFactory.getInstance().createRequest(s);
45+
InvocationRequest request = ExecutionFactory.getInstance().createRequest(s);
5946
assertThat(request.getNamespace(), is(equalTo(SampleContract.class.getAnnotation(Contract.class).namespace())));
6047
assertThat(request.getMethod(), is(equalTo("t1")));
6148
assertThat(request.getRequestName(), is(equalTo("samplecontract:t1")));
6249
assertThat(request.getArgs(), is(contains(s.getArgs().get(1))));
63-
routing = r.getRouting(request);
50+
org.hyperledger.fabric.contract.routing.TxFunction.Routing routing = r.getRouting(request);
6451
assertThat(routing.getContractClass().getName(), is(equalTo(SampleContract.class.getName())));
6552
assertThat(routing.getMethod().getName(), is(equalTo("t1")));
6653

@@ -73,36 +60,27 @@ public void testInit() {
7360
ChaincodeStub s = new ChaincodeStubNaiveImpl();
7461

7562
List<String> args = new ArrayList<>();
76-
args.add("samplecontract:i1");
63+
args.add("samplecontract:t1");
64+
args.add("asdf");
7765
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
7866

7967
SampleContract.beforeInvoked = 0;
8068
SampleContract.afterInvoked = 0;
81-
SampleContract.i1Invoked = 0;
69+
SampleContract.doWorkInvoked = 0;
70+
SampleContract.t1Invoked = 0;
71+
8272
Chaincode.Response response = r.init(s);
8373
assertThat(response, is(notNullValue()));
84-
assertThat(response.getStringPayload(), is(equalTo("Init done")));
74+
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
75+
assertThat(response.getStringPayload(), is(equalTo("asdf")));
8576
assertThat(SampleContract.beforeInvoked, is(1));
8677
assertThat(SampleContract.afterInvoked, is(1));
87-
assertThat(SampleContract.i1Invoked, is(1));
88-
89-
args.set(0, "samplecontract:i2");
90-
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
91-
92-
response = r.init(s);
93-
assertThat(response, is(notNullValue()));
94-
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
95-
96-
args.set(0, "samplecontract:t1");
97-
args.add("arg text");
98-
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
99-
response = r.init(s);
100-
assertThat(response, is(notNullValue()));
101-
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
78+
assertThat(SampleContract.doWorkInvoked, is(1));
79+
assertThat(SampleContract.t1Invoked, is(1));
10280
}
10381

10482
@Test
105-
public void testInvoke() {
83+
public void testInvokeTxnThatExists() {
10684
ContractRouter r = new ContractRouter(new String[]{"-a", "127.0.0.1:7052", "-i", "testId"});
10785
r.findAllContracts();
10886
ChaincodeStub s = new ChaincodeStubNaiveImpl();
@@ -119,24 +97,62 @@ public void testInvoke() {
11997

12098
Chaincode.Response response = r.invoke(s);
12199
assertThat(response, is(notNullValue()));
100+
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
122101
assertThat(response.getStringPayload(), is(equalTo("asdf")));
123102
assertThat(SampleContract.beforeInvoked, is(1));
124103
assertThat(SampleContract.afterInvoked, is(1));
125104
assertThat(SampleContract.doWorkInvoked, is(1));
126105
assertThat(SampleContract.t1Invoked, is(1));
106+
}
127107

128-
args.set(0, "samplecontract:notsupposedtoexist");
108+
@Test
109+
public void testInvokeTxnThatDoesNotExist() {
110+
ContractRouter r = new ContractRouter(new String[]{"-a", "127.0.0.1:7052", "-i", "testId"});
111+
r.findAllContracts();
112+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
113+
114+
List<String> args = new ArrayList<>();
115+
args.add("samplecontract:notsupposedtoexist");
116+
args.add("asdf");
129117
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
130118

131-
response = r.invoke(s);
119+
SampleContract.beforeInvoked = 0;
120+
SampleContract.afterInvoked = 0;
121+
SampleContract.doWorkInvoked = 0;
122+
SampleContract.t1Invoked = 0;
123+
124+
Chaincode.Response response = r.invoke(s);
132125
assertThat(response, is(notNullValue()));
133126
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
127+
assertThat(response.getStringPayload(), is(startsWith("java.lang.IllegalStateException")));
128+
assertThat(SampleContract.beforeInvoked, is(0));
129+
assertThat(SampleContract.afterInvoked, is(0));
130+
assertThat(SampleContract.doWorkInvoked, is(0));
131+
assertThat(SampleContract.t1Invoked, is(0));
132+
}
133+
134+
@Test
135+
public void testInvokeTxnThatThrowsAnException() {
136+
ContractRouter r = new ContractRouter(new String[]{"-a", "127.0.0.1:7052", "-i", "testId"});
137+
r.findAllContracts();
138+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
134139

135-
args.set(0, "samplecontract:i1");
140+
List<String> args = new ArrayList<>();
141+
args.add("samplecontract:t3");
142+
args.add("asdf");
136143
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
137-
response = r.invoke(s);
144+
145+
SampleContract.beforeInvoked = 0;
146+
SampleContract.afterInvoked = 0;
147+
SampleContract.doWorkInvoked = 0;
148+
149+
Chaincode.Response response = r.invoke(s);
138150
assertThat(response, is(notNullValue()));
139-
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
151+
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
152+
assertThat(response.getStringPayload(), is(startsWith("java.lang.RuntimeException: T3 fail!")));
153+
assertThat(SampleContract.beforeInvoked, is(1));
154+
assertThat(SampleContract.afterInvoked, is(0));
155+
assertThat(SampleContract.doWorkInvoked, is(0));
140156
}
141157

142158
@Test

0 commit comments

Comments
 (0)