Skip to content

Commit f87de8e

Browse files
committed
[FAB-15632] Add name element to transaction annotation
Change-Id: I895c55449ee6924a836144d9bee4d86424f46e4a Signed-off-by: James Taylor <[email protected]>
1 parent d726175 commit f87de8e

File tree

6 files changed

+81
-11
lines changed

6 files changed

+81
-11
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@
2929
* @return
3030
*/
3131
boolean submit() default true;
32+
33+
/**
34+
* The name of the callable transaction if it should be different to the method
35+
* name
36+
*
37+
* @return the transaction name
38+
*/
39+
String name() default "";
3240
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class TxFunctionImpl implements TxFunction {
2323
private static Logger logger = Logger.getLogger(TxFunctionImpl.class);
2424

2525
private Method method;
26+
private String name;
2627
private TransactionType type;
2728
private Routing routing;
2829
private TypeSchema returnSchema;
@@ -77,6 +78,14 @@ public TxFunctionImpl(Method m, ContractDefinition contract) {
7778
this.type = TransactionType.QUERY;
7879
}
7980

81+
String txnName = m.getAnnotation(Transaction.class).name();
82+
if (!txnName.isEmpty()) {
83+
this.name = txnName;
84+
}
85+
}
86+
87+
if (name == null) {
88+
this.name = m.getName();
8089
}
8190

8291
this.routing = new RoutingImpl(m, contract.getContractImpl());
@@ -108,7 +117,7 @@ public TxFunctionImpl(Method m, ContractDefinition contract) {
108117

109118
@Override
110119
public String getName() {
111-
return this.method.getName();
120+
return name;
112121
}
113122

114123
@Override
@@ -133,7 +142,7 @@ public TransactionType getType() {
133142

134143
@Override
135144
public String toString() {
136-
return this.method.getName() + " @" + Integer.toHexString(System.identityHashCode(this));
145+
return name + " @" + Integer.toHexString(System.identityHashCode(this));
137146
}
138147

139148
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public SystemContract() {
1919

2020
}
2121

22-
@Transaction(submit = false)
23-
public String GetMetadata() {
22+
@Transaction(submit = false, name = "GetMetadata")
23+
public String getMetadata() {
2424
String jsonmetadata = MetadataBuilder.getMetadata();
2525
return jsonmetadata;
2626
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public class SampleContract implements ContractInterface {
2525
static public int t1Invoked = 0;
2626
static public int i1Invoked = 0;
2727

28+
@Transaction(name = "t4")
29+
public String tFour() {
30+
31+
System.out.println("SampleContract::T4 Done");
32+
return "Transaction 4";
33+
}
34+
2835
@Transaction
2936
public String t3() {
3037
throw new RuntimeException("T3 fail!");

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,58 @@ public void testInvokeTxnThatExists() {
100100
assertThat(SampleContract.t1Invoked, is(1));
101101
}
102102

103+
@Test
104+
public void testInvokeTxnWithDefinedName() {
105+
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
106+
r.findAllContracts();
107+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
108+
109+
List<String> args = new ArrayList<>();
110+
args.add("samplecontract:t4");
111+
args.add("asdf");
112+
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
113+
114+
SampleContract.beforeInvoked = 0;
115+
SampleContract.afterInvoked = 0;
116+
SampleContract.doWorkInvoked = 0;
117+
SampleContract.t1Invoked = 0;
118+
119+
Chaincode.Response response = r.invoke(s);
120+
assertThat(response, is(notNullValue()));
121+
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
122+
assertThat(response.getStringPayload(), is(equalTo("Transaction 4")));
123+
assertThat(SampleContract.beforeInvoked, is(1));
124+
assertThat(SampleContract.afterInvoked, is(1));
125+
assertThat(SampleContract.doWorkInvoked, is(0));
126+
assertThat(SampleContract.t1Invoked, is(0));
127+
}
128+
129+
@Test
130+
public void testInvokeTxnWithDefinedNameUsingMethodName() {
131+
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
132+
r.findAllContracts();
133+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
134+
135+
List<String> args = new ArrayList<>();
136+
args.add("samplecontract:tFour");
137+
args.add("asdf");
138+
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
139+
140+
SampleContract.beforeInvoked = 0;
141+
SampleContract.afterInvoked = 0;
142+
SampleContract.doWorkInvoked = 0;
143+
SampleContract.t1Invoked = 0;
144+
145+
Chaincode.Response response = r.invoke(s);
146+
assertThat(response, is(notNullValue()));
147+
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
148+
assertThat(response.getStringPayload(), is(startsWith("java.lang.IllegalStateException")));
149+
assertThat(SampleContract.beforeInvoked, is(0));
150+
assertThat(SampleContract.afterInvoked, is(0));
151+
assertThat(SampleContract.doWorkInvoked, is(0));
152+
assertThat(SampleContract.t1Invoked, is(0));
153+
}
154+
103155
@Test
104156
public void testInvokeTxnThatDoesNotExist() {
105157
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
*/
66
package org.hyperledger.fabric.contract.metadata;
77

8-
import static org.hamcrest.Matchers.hasEntry;
9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertNull;
11-
import static org.junit.Assert.assertThat;
12-
138
import java.io.Serializable;
149
import java.util.HashMap;
15-
import java.util.Map;
1610

1711
import org.hyperledger.fabric.contract.systemcontract.SystemContract;
1812
import org.junit.Before;
@@ -48,7 +42,7 @@ public void systemContract() {
4842

4943
// access the system contract to extract the metadata
5044
SystemContract system = new SystemContract();
51-
String metadatacompressed = system.GetMetadata();
45+
String metadatacompressed = system.getMetadata();
5246

5347
}
5448

0 commit comments

Comments
 (0)