From 4c58c0b0a8d56dc725d38e0297d0bc8df9183c45 Mon Sep 17 00:00:00 2001 From: mattbradburyr3 Date: Sun, 29 Jan 2023 18:34:51 +0000 Subject: [PATCH] tidied up FinalizeResponderFlow --- .../workflows/CreateNewChatFlow.java | 2 +- .../workflows/FinalizeChatResponderFlow.java | 27 ++++++++-- .../workflows/FinalizeChatSubFlow.java | 51 ++++++++++--------- .../workflows/ResponderValidationHelpers.java | 29 +++++++---- 4 files changed, 70 insertions(+), 39 deletions(-) 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 cb5bf45..b8965a7 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 @@ -28,7 +28,7 @@ import java.util.UUID; import static java.util.Objects.*; -@InitiatingFlow(protocol = "create-chat-protocol") +//@InitiatingFlow(protocol = "create-chat-protocol") public class CreateNewChatFlow implements RPCStartableFlow { private final static Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class); diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java index a4741e8..d5d31cf 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatResponderFlow.java @@ -6,6 +6,7 @@ import net.corda.v5.application.flows.InitiatedBy; import net.corda.v5.application.flows.ResponderFlow; import net.corda.v5.application.messaging.FlowSession; import net.corda.v5.base.annotations.Suspendable; +import net.corda.v5.base.types.MemberX500Name; import net.corda.v5.ledger.utxo.UtxoLedgerService; import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction; import net.corda.v5.ledger.utxo.transaction.UtxoTransactionValidator; @@ -13,8 +14,11 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords; -import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty; +import java.util.Arrays; +import java.util.List; + +//import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords; +//import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty; @InitiatedBy(protocol = "finalize-chat-protocol") public class FinalizeChatResponderFlow implements ResponderFlow { @@ -25,8 +29,10 @@ public class FinalizeChatResponderFlow implements ResponderFlow { @Suspendable @Override - public void call(@NotNull FlowSession session) { - log.info("AppendChatResponderFlow.call() called"); + public void call(FlowSession session) { + + log.info("FinalizeChatResponderFlow.call() called"); + try { UtxoTransactionValidator txValidator = ledgerTransaction -> { ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0); @@ -45,4 +51,17 @@ public class FinalizeChatResponderFlow implements ResponderFlow { log.warn("Exceptionally finished responder flow", e); } } + + + @Suspendable + Boolean checkForBannedWords(String str) { + List bannedWords = Arrays.asList("banana", "apple", "pear"); + return bannedWords.stream().anyMatch(str::contains); + } + + @Suspendable + Boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) { + return state.getMessageFrom().equals(otherMember); + } + } 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 95ec536..2ab4ff5 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 @@ -16,6 +16,10 @@ import java.util.List; @InitiatingFlow(protocol = "finalize-chat-protocol") public class FinalizeChatSubFlow implements SubFlow { + // these need to be private + good practice is to declare them at the start of the class + private final UtxoSignedTransaction signedTransaction; + private final MemberX500Name otherMember; + public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { this.signedTransaction = signedTransaction; this.otherMember = otherMember; @@ -33,46 +37,47 @@ public class FinalizeChatSubFlow implements SubFlow { @Suspendable public String call() { -// log.info("AppendChatFlow.call() called"); + log.info("FinalizeChatFlow.call() called"); // log.info("otherMember = " + otherMember); FlowSession session = flowMessaging.initiateFlow(otherMember); - String retVal; + String result; try { List sessionsList = Arrays.asList(session); - log.info("sessionList.size()=" + sessionsList.size()); +// log.info("sessionList.size()=" + sessionsList.size()); UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize( signedTransaction, sessionsList ); - retVal = finalizedSignedTransaction.getId().toString(); - log.info("Success! Response: " + retVal); + result = finalizedSignedTransaction.getId().toString(); + log.info("Success! Response: " + result); } catch (Exception e) { log.warn("Finality failed", e); - retVal = "Finality failed, " + e.getMessage(); + result = "Finality failed, " + e.getMessage(); } - log.info("AppendChatSubFlow call returns=" + retVal); - return retVal; +// log.info("FinalizeChatSubFlow call returns=" + retVal); + return result; } - public UtxoSignedTransaction getSignedTransaction() { - return signedTransaction; - } + // We don't need getters and setters as the properties should be private - public void setSignedTransaction(UtxoSignedTransaction signedTransaction) { - this.signedTransaction = signedTransaction; - } +// public UtxoSignedTransaction getSignedTransaction() { +// return signedTransaction; +// } +// +// public void setSignedTransaction(UtxoSignedTransaction signedTransaction) { +// this.signedTransaction = signedTransaction; +// } +// +// public MemberX500Name getOtherMember() { +// return otherMember; +// } +// +// public void setOtherMember(MemberX500Name otherMember) { +// this.otherMember = otherMember; +// } - public MemberX500Name getOtherMember() { - return otherMember; - } - public void setOtherMember(MemberX500Name otherMember) { - this.otherMember = otherMember; - } - - public UtxoSignedTransaction signedTransaction; - public MemberX500Name otherMember; } diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ResponderValidationHelpers.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ResponderValidationHelpers.java index 3b88738..91e35dc 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ResponderValidationHelpers.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ResponderValidationHelpers.java @@ -7,19 +7,26 @@ import net.corda.v5.base.types.MemberX500Name; import java.util.Arrays; import java.util.List; + +// I think we should avoid static imports, it's an extra level of complexity we don't need to add. +// The Kotlin helper functions are more simple because Kotlin supports functions which are in the package but +// not in a class. + public final class ResponderValidationHelpers { - public final static List bannedWords = Arrays.asList("banana", "apple", "pear"); +// public final static List bannedWords = Arrays.asList("banana", "apple", "pear"); +// +// @Suspendable +// public static boolean checkForBannedWords(String str) { +// return bannedWords.stream().anyMatch(str::contains); +// } - @Suspendable - public static boolean checkForBannedWords(String str) { - return bannedWords.stream().anyMatch(str::contains); - } - - @Suspendable - public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) { - return state.getMessageFrom().equals(otherMember); - } +// @Suspendable +// public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) { +// return state.getMessageFrom().equals(otherMember); +// } // This class just introduces a scope for some helper functions and should not be instantiated. - private ResponderValidationHelpers() {} +// private ResponderValidationHelpers() {} + } +