Skip to content

Commit 9cc6553

Browse files
committed
[FAB-15720] prevent duplicate transactions
Throw an exception a transaction is defined with the same name more than once, either by ovewloading methods, or by specifying a name in the Transaction annotation Change-Id: I5c2371e3efaafc26ccd68a5e97ee9b59a40946fb Signed-off-by: James Taylor <[email protected]>
1 parent f87de8e commit 9cc6553

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ public ContractInterface getContractImpl() {
9090
public TxFunction addTxFunction(Method m) {
9191
logger.debug(() -> "Adding method " + m.getName());
9292
TxFunction txFn = new TxFunctionImpl(m, this);
93-
txFunctions.put(txFn.getName(), txFn);
93+
TxFunction previousTxnFn = txFunctions.put(txFn.getName(), txFn);
94+
if (previousTxnFn != null) {
95+
String message = String.format("Duplicate transaction method %s", previousTxnFn.getName());
96+
ContractRuntimeException cre = new ContractRuntimeException(message);
97+
logger.severe(() -> logger.formatError(cre));
98+
throw cre;
99+
}
94100
return txFn;
95101
}
96102

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import static org.hamcrest.Matchers.startsWith;
1010
import static org.junit.Assert.assertThat;
1111

12+
import java.lang.reflect.Method;
1213
import java.security.Permission;
1314

15+
import org.hyperledger.fabric.contract.ContractInterface;
16+
import org.hyperledger.fabric.contract.ContractRuntimeException;
1417
import org.hyperledger.fabric.contract.annotation.Contract;
1518
import org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl;
1619
import org.junit.Before;
@@ -89,4 +92,18 @@ public void checkPermission(Permission perm) {
8992
System.setSecurityManager(null);
9093
}
9194
}
95+
96+
@Test
97+
public void duplicateTransaction() throws NoSuchMethodException, SecurityException {
98+
ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class);
99+
100+
ContractInterface contract = new SampleContract();
101+
Method m = contract.getClass().getMethod("t2", new Class[] {});
102+
103+
thrown.expect(ContractRuntimeException.class);
104+
thrown.expectMessage("Duplicate transaction method t2");
105+
106+
cf.addTxFunction(m);
107+
cf.addTxFunction(m);
108+
}
92109
}

0 commit comments

Comments
 (0)