Skip to content

Commit

Permalink
[FAB-15720] prevent duplicate transactions
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jt-nti committed Jun 14, 2019
1 parent f87de8e commit 9cc6553
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ public ContractInterface getContractImpl() {
public TxFunction addTxFunction(Method m) {
logger.debug(() -> "Adding method " + m.getName());
TxFunction txFn = new TxFunctionImpl(m, this);
txFunctions.put(txFn.getName(), txFn);
TxFunction previousTxnFn = txFunctions.put(txFn.getName(), txFn);
if (previousTxnFn != null) {
String message = String.format("Duplicate transaction method %s", previousTxnFn.getName());
ContractRuntimeException cre = new ContractRuntimeException(message);
logger.severe(() -> logger.formatError(cre));
throw cre;
}
return txFn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

import java.lang.reflect.Method;
import java.security.Permission;

import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.ContractRuntimeException;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl;
import org.junit.Before;
Expand Down Expand Up @@ -89,4 +92,18 @@ public void checkPermission(Permission perm) {
System.setSecurityManager(null);
}
}

@Test
public void duplicateTransaction() throws NoSuchMethodException, SecurityException {
ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class);

ContractInterface contract = new SampleContract();
Method m = contract.getClass().getMethod("t2", new Class[] {});

thrown.expect(ContractRuntimeException.class);
thrown.expectMessage("Duplicate transaction method t2");

cf.addTxFunction(m);
cf.addTxFunction(m);
}
}

0 comments on commit 9cc6553

Please sign in to comment.