From d56db30724250fbe5ef9bab71ff3bfe65a53efea Mon Sep 17 00:00:00 2001 From: mattbradburyr3 Date: Sun, 29 Jan 2023 15:42:32 +0000 Subject: [PATCH] tidy UpdateChatFlow --- .../workflows/CreateNewChatFlow.java | 5 ++- ...ow.java => FinalizeChatResponderFlow.java} | 6 +-- .../workflows/FinalizeChatSubFlow.java | 2 +- .../utxoexample/workflows/UpdateChatFlow.java | 42 ++++++++++++------- 4 files changed, 34 insertions(+), 21 deletions(-) rename workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/{AppendChatResponderFlow.java => FinalizeChatResponderFlow.java} (90%) diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java index 3910645..3082d0a 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java @@ -15,6 +15,7 @@ import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction; import net.corda.v5.ledger.utxo.transaction.UtxoTransactionBuilder; import net.corda.v5.membership.MemberInfo; import net.corda.v5.membership.NotaryInfo; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,11 +48,11 @@ public class CreateNewChatFlow implements RPCStartableFlow { @CordaInject public FlowEngine flowEngine; -// @NotNull + @Suspendable @Override // public String call(@NotNull RPCRequestData requestBody) throws IllegalArgumentException { - public String call(RPCRequestData requestBody) { + public String call( RPCRequestData requestBody) { log.info("CreateNewChatFlow.call() called"); diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatResponderFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java similarity index 90% rename from workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatResponderFlow.java rename to workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java index 27ee73e..a4741e8 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatResponderFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java @@ -16,9 +16,9 @@ import org.slf4j.LoggerFactory; import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords; import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty; -@InitiatedBy(protocol = "append-chat-protocol") -public class AppendChatResponderFlow implements ResponderFlow { - private final static Logger log = LoggerFactory.getLogger(AppendChatResponderFlow.class); +@InitiatedBy(protocol = "finalize-chat-protocol") +public class FinalizeChatResponderFlow implements ResponderFlow { + private final static Logger log = LoggerFactory.getLogger(FinalizeChatResponderFlow.class); @CordaInject public UtxoLedgerService utxoLedgerService; diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatSubFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatSubFlow.java index 7d982af..e6891cf 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatSubFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatSubFlow.java @@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.List; -@InitiatingFlow(protocol = "append-chat-protocol") +@InitiatingFlow(protocol = "finalize-chat-protocol") public class FinalizeChatSubFlow implements SubFlow { public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { 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 d1b0fa4..75d8da0 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 @@ -10,6 +10,7 @@ import net.corda.v5.application.marshalling.JsonMarshallingService; import net.corda.v5.application.membership.MemberLookup; import net.corda.v5.application.messaging.FlowMessaging; import net.corda.v5.base.annotations.Suspendable; +import net.corda.v5.base.exceptions.CordaRuntimeException; import net.corda.v5.ledger.common.NotaryLookup; import net.corda.v5.ledger.utxo.StateAndRef; import net.corda.v5.ledger.utxo.UtxoLedgerService; @@ -27,6 +28,7 @@ import java.util.stream.Collectors; import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne; import static java.util.Objects.*; +import static java.util.stream.Collectors.toList; public class UpdateChatFlow implements RPCStartableFlow { @@ -41,16 +43,17 @@ public class UpdateChatFlow implements RPCStartableFlow { @CordaInject public UtxoLedgerService ledgerService; - @CordaInject - public NotaryLookup notaryLookup; - - @CordaInject - public FlowMessaging flowMessaging; +// @CordaInject +// public NotaryLookup notaryLookup; +// +// @CordaInject +// public FlowMessaging flowMessaging; @CordaInject public FlowEngine flowEngine; - @NotNull + +// @NotNull // this is a jetbrains annotation - we don't want to depend on jet brains packages. @Suspendable @Override public String call(RPCRequestData requestBody) { @@ -61,24 +64,33 @@ public class UpdateChatFlow implements RPCStartableFlow { UpdateChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, UpdateChatFlowArgs.class); // Look up state (this is very inefficient) - StateAndRef stateAndRef = findAndExpectExactlyOne( - ledgerService.findUnconsumedStatesByType(ChatState.class), - sAndR -> sAndR.getState().getContractState().getId().equals(flowArgs.getId()), - "Multiple or zero Chat states with id " + flowArgs.getId() + " found" - ); + // Removing this because it's an unnecessary level of abstraction, as it's an example for all abilities of programmer + // we want it to be as straight forward as possible. + // Also it's inconsistent with code below it. +// StateAndRef stateAndRef = findAndExpectExactlyOne( +// ledgerService.findUnconsumedStatesByType(ChatState.class), +// sAndR -> sAndR.getState().getContractState().getId().equals(flowArgs.getId()), +// "Multiple or zero Chat states with id " + flowArgs.getId() + " found" +// ); + + List> chatStates = ledgerService.findUnconsumedStatesByType(ChatState.class); + List> chatStatesWithId = chatStates.stream() + .filter(sar -> sar.getState().getContractState().getId() == flowArgs.getId()).collect(toList()); + if (chatStatesWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.id + " found"); + StateAndRef stateAndRef = chatStatesWithId.get(0); + MemberInfo myInfo = memberLookup.myInfo(); ChatState state = stateAndRef.getState().getContractState(); List members = state.getParticipants().stream().map( it -> requireNonNull(memberLookup.lookup(it), "Member not found from Key") - ).collect(Collectors.toList()); + ).collect(toList()); // Now we want to check that there is only one member other than ourselves in the chat. members.remove(myInfo); - if(members.size() != 1) { - throw new RuntimeException("Should be only one participant other than the initiator"); - } + if(members.size() != 1) throw new RuntimeException("Should be only one participant other than the initiator"); + MemberInfo otherMember = members.get(0); ChatState newChatState = state.updateMessage(myInfo.getName(), flowArgs.getMessage());