From 4d07326b78d89ccfbfcaa2ad7f4080714fff75ec Mon Sep 17 00:00:00 2001 From: mattbradburyr3 Date: Sun, 29 Jan 2023 20:33:28 +0000 Subject: [PATCH] tidied remaining flows --- .../utilities/CorDappHelpers.java | 30 +++++------ .../utxoexample/workflows/GetChatFlow.java | 54 ++++++++++++------- .../utxoexample/workflows/UpdateChatFlow.java | 14 ++--- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utilities/CorDappHelpers.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utilities/CorDappHelpers.java index e66299c..b392a00 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utilities/CorDappHelpers.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utilities/CorDappHelpers.java @@ -6,18 +6,18 @@ import java.util.List; import java.util.concurrent.Callable; import java.util.function.Predicate; import java.util.stream.Collectors; - -public final class CorDappHelpers { - public static T findAndExpectExactlyOne(Collection collection, Predicate filterFn, String exceptionMsg) - { - Collection results = collection.stream().filter(filterFn).collect(Collectors.toList()); - if(results.size() != 1){ - throw new RuntimeException(exceptionMsg); - } - return results.iterator().next(); - } - - public static T findAndExpectExactlyOne(Collection collection, String exceptionMsg) { - return findAndExpectExactlyOne(collection, e -> true, exceptionMsg); - } -} +// +//public final class CorDappHelpers { +// public static T findAndExpectExactlyOne(Collection collection, Predicate filterFn, String exceptionMsg) +// { +// Collection results = collection.stream().filter(filterFn).collect(Collectors.toList()); +// if(results.size() != 1){ +// throw new RuntimeException(exceptionMsg); +// } +// return results.iterator().next(); +// } +// +// public static T findAndExpectExactlyOne(Collection collection, String exceptionMsg) { +// return findAndExpectExactlyOne(collection, e -> true, exceptionMsg); +// } +//} diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlow.java index 376a89d..7938418 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlow.java @@ -6,6 +6,7 @@ import net.corda.v5.application.flows.RPCRequestData; import net.corda.v5.application.flows.RPCStartableFlow; import net.corda.v5.application.marshalling.JsonMarshallingService; import net.corda.v5.base.annotations.Suspendable; +import net.corda.v5.base.exceptions.CordaRuntimeException; import net.corda.v5.crypto.SecureHash; import net.corda.v5.ledger.utxo.StateAndRef; import net.corda.v5.ledger.utxo.UtxoLedgerService; @@ -16,8 +17,9 @@ import org.slf4j.LoggerFactory; import java.util.*; -import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne; +//import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne; import static java.util.Objects.*; +import static java.util.stream.Collectors.toList; public class GetChatFlow implements RPCStartableFlow { @@ -29,27 +31,36 @@ public class GetChatFlow implements RPCStartableFlow { @CordaInject public UtxoLedgerService ledgerService; - @NotNull +// @NotNull @Override @Suspendable - public String call(RPCRequestData requestBody) throws IllegalArgumentException { + public String call(RPCRequestData requestBody) { + GetChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, GetChatFlowArgs.class); - List> stateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class); - log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size()); - log.info("GetChatFlow stateAndRefs = " + stateAndRefs); + List> chatStateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class); + List> chatStateAndRefsWithId = chatStateAndRefs.stream() + .filter(sar -> sar.getState().getContractState().getId().equals(flowArgs.getId())).collect(toList()); + if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found"); + StateAndRef chatStateAndRef = chatStateAndRefsWithId.get(0); - StateAndRef state = findAndExpectExactlyOne(stateAndRefs, - stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()), - "did not find an unique ChatState" - ); - return jsonMarshallingService.format(resolveMessagesFromBackchain(state, flowArgs.getNumberOfRecords() )); +// log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size()); +// log.info("GetChatFlow stateAndRefs = " + stateAndRefs); + +// StateAndRef state = findAndExpectExactlyOne(stateAndRefs, +// stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()), +// "did not find an unique ChatState" +// ); + + + + return jsonMarshallingService.format(resolveMessagesFromBackchain(chatStateAndRef, flowArgs.getNumberOfRecords() )); } @NotNull @Suspendable - private List resolveMessagesFromBackchain(StateAndRef stateAndRef, int numberOfRecords) throws IllegalArgumentException { + private List resolveMessagesFromBackchain(StateAndRef stateAndRef, int numberOfRecords) { List messages = new LinkedList<>(); @@ -58,17 +69,24 @@ public class GetChatFlow implements RPCStartableFlow { boolean moreBackchain = true; while (moreBackchain) { + SecureHash transactionId = currentStateAndRef.getRef().getTransactionHash(); UtxoLedgerTransaction transaction = requireNonNull( ledgerService.findLedgerTransaction(transactionId), - "Transaction $transactionId not found" + "Transaction " + transactionId + " not found." ); - ChatState output = findAndExpectExactlyOne( - transaction.getOutputStates(ChatState.class), - "Expecting one and only one ChatState output for transaction " + transactionId - ); + +// ChatState output = findAndExpectExactlyOne( +// transaction.getOutputStates(ChatState.class), +// "Expecting one and only one ChatState output for transaction " + transactionId +// ); + + List chatStates = transaction.getOutputStates(ChatState.class); + if (chatStates.size() != 1) throw new CordaRuntimeException( + "Expecting one and only one ChatState output for transaction " + transactionId + "."); + ChatState output = chatStates.get(0); messages.add(new GetChatResponse(output.getMessageFrom().toString(), output.getMessage())); recordsToFetch--; @@ -78,7 +96,7 @@ public class GetChatFlow implements RPCStartableFlow { if (inputStateAndRefs.isEmpty() || recordsToFetch == 0) { moreBackchain = false; } else if (inputStateAndRefs.size() > 1) { - throw new IllegalArgumentException("More than one input state found for transaction " + transactionId + "."); + throw new CordaRuntimeException("More than one input state found for transaction " + transactionId + "."); } else { currentStateAndRef = inputStateAndRefs.get(0); } diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlow.java index 932d629..3336129 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlow.java @@ -72,15 +72,15 @@ public class UpdateChatFlow implements RPCStartableFlow { // "Multiple or zero Chat states with id " + flowArgs.getId() + " found" // ); - List> chatStates = ledgerService.findUnconsumedStatesByType(ChatState.class); - List> chatStatesWithId = chatStates.stream() + List> chatStateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class); + List> chatStateAndRefsWithId = chatStateAndRefs.stream() .filter(sar -> sar.getState().getContractState().getId().equals(flowArgs.getId())).collect(toList()); - if (chatStatesWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found"); - StateAndRef stateAndRef = chatStatesWithId.get(0); + if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found"); + StateAndRef chatStateAndRef = chatStateAndRefsWithId.get(0); MemberInfo myInfo = memberLookup.myInfo(); - ChatState state = stateAndRef.getState().getContractState(); + ChatState state = chatStateAndRef.getState().getContractState(); List members = state.getParticipants().stream().map( it -> requireNonNull(memberLookup.lookup(it), "Member not found from public Key "+ it + ".") @@ -95,10 +95,10 @@ public class UpdateChatFlow implements RPCStartableFlow { ChatState newChatState = state.updateMessage(myInfo.getName(), flowArgs.getMessage()); UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder() - .setNotary(stateAndRef.getState().getNotary()) + .setNotary(chatStateAndRef.getState().getNotary()) .setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis())) .addOutputState(newChatState) - .addInputState(stateAndRef.getRef()) + .addInputState(chatStateAndRef.getRef()) .addCommand(new ChatContract.Update()) .addSignatories(newChatState.getParticipants());