From 2a026de47215e69a7b6cd3e7dc3a08f7a814af6b Mon Sep 17 00:00:00 2001 From: djmil Date: Thu, 21 Sep 2023 23:15:57 +0200 Subject: [PATCH] minor cleanup --- .../cordacheckers/contracts/GameCommand.java | 7 ++-- .../contracts/UtxoLedgerTransactionUtil.java | 36 ++++--------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/corda/contracts/src/main/java/djmil/cordacheckers/contracts/GameCommand.java b/corda/contracts/src/main/java/djmil/cordacheckers/contracts/GameCommand.java index ceeacfb..53d9442 100644 --- a/corda/contracts/src/main/java/djmil/cordacheckers/contracts/GameCommand.java +++ b/corda/contracts/src/main/java/djmil/cordacheckers/contracts/GameCommand.java @@ -73,10 +73,10 @@ public class GameCommand implements Command { return move; } - /* * Session initiator/respondent */ + public MemberX500Name getCounterparty(StateAndRef gameStateSar) { final GameState gameState = gameStateSar.getState().getContractState(); return gameState.getOpponentName(getInitiator(gameState)); @@ -191,9 +191,7 @@ public class GameCommand implements Command { requireThat(inActors.containsAll(outActors) && outActors.containsAll(inActors), IN_OUT_PARTICIPANTS); - final var activePlayerName = getInitiator(inGameBoardState); - requireThat(inGameBoardState.getActivePlayerName().compareTo(activePlayerName) == 0, SURRENDER_ACTOR + "expected " +inGameBoardState.getActivePlayerName() +" actual " +activePlayerName); - + final var activePlayerName = getInitiator(inGameBoardState); final var expectedWinnerName = inGameBoardState.getOpponentName(activePlayerName); requireThat(outGameResultState.getWinnerName().compareTo(expectedWinnerName) == 0, "Expected winner "+expectedWinnerName.getCommonName() +", proposed winner " +outGameResultState.getWinnerName().getCommonName()); @@ -237,7 +235,6 @@ public class GameCommand implements Command { static final String SURRENDER_INPUT_STATE = "SURRENDER command should have exactly one GameBoardState input state"; static final String SURRENDER_OUTPUT_STATE = "SURRENDER command should have exactly one GameResultState output state"; - static final String SURRENDER_ACTOR = "Only active player can SURRENDER"; static final String MOVE_INPUT_STATE = "MOVE command should have exactly one GameBoardState input state"; static final String MOVE_OUTPUT_STATE = "MOVE command should have exactly one GameBoardState output state"; diff --git a/corda/contracts/src/main/java/djmil/cordacheckers/contracts/UtxoLedgerTransactionUtil.java b/corda/contracts/src/main/java/djmil/cordacheckers/contracts/UtxoLedgerTransactionUtil.java index 469014a..2553b1d 100644 --- a/corda/contracts/src/main/java/djmil/cordacheckers/contracts/UtxoLedgerTransactionUtil.java +++ b/corda/contracts/src/main/java/djmil/cordacheckers/contracts/UtxoLedgerTransactionUtil.java @@ -11,49 +11,25 @@ public class UtxoLedgerTransactionUtil { return single(utxoTrx.getCommands(clazz), clazz); } - // public static StateAndRef getSingleReferenceSar(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return singleSar(utxoTrx.getReferenceStateAndRefs(clazz), clazz); - // } - public static T getSingleInputState(UtxoLedgerTransaction utxoTrx, Class clazz) { return single(utxoTrx.getInputStates(clazz), clazz); } - // public static StateAndRef getSingleInputSar(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return singleSar(utxoTrx.getInputStateAndRefs(clazz), clazz); - // } - public static T getSingleOutputState(UtxoLedgerTransaction utxoTrx, Class clazz) { return single(utxoTrx.getOutputStates(clazz), clazz); } - // public static Optional getOptionalCommand(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return optional(utxoTrx.getCommands(clazz), clazz); - // } - - // public static Optional getOptionalReferenceState(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return optional(utxoTrx.getReferenceStates(clazz), clazz); - // } - - // public static Optional getOptionalInputState(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return optional(utxoTrx.getInputStates(clazz), clazz); - // } - - // public static Optional getOptionalOutputState(UtxoLedgerTransaction utxoTrx, Class clazz) { - // return optional(utxoTrx.getOutputStates(clazz), clazz); - // } - - private static Optional optional(List list, Class clazz) { - return list - .stream() - .reduce((a, b) -> {throw new IllegalStateException(MULTIPLE_INSTANCES_OF +clazz.getName());}); - } - private static T single(List list, Class clazz) { return optional(list, clazz) .orElseThrow( () -> new IllegalStateException(NO_INSTANCES_OF +clazz.getName()) ); } + private static Optional optional(List list, Class clazz) { + return list + .stream() + .reduce((a, b) -> {throw new IllegalStateException(MULTIPLE_INSTANCES_OF +clazz.getName());}); + } + private static String MULTIPLE_INSTANCES_OF = "Multiple instances of "; private static String NO_INSTANCES_OF = "No instances of "; }