From e9c1139bfda6b6aed7bfda9171a21703d2da3451 Mon Sep 17 00:00:00 2001 From: Chris Barratt Date: Wed, 18 Jan 2023 11:25:33 +0000 Subject: [PATCH] Fixes to avoid Quasar bug, bug fix in chat verification, switch from List.of to Array.asList --- .../workflows/AppendChatResponderFlow.java | 26 ++++++++++++++----- .../workflows/AppendChatSubFlow.java | 1 - .../workflows/CreateNewChatFlow.java | 4 ++- .../workflows/ResponderValidationHelpers.java | 2 +- .../utxoexample/workflows/UpdateChatFlow.java | 11 ++++++++ .../workflows/utilities/CorDappHelpers.java | 1 - 6 files changed, 35 insertions(+), 10 deletions(-) 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/AppendChatResponderFlow.java index 028653f..928e996 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/AppendChatResponderFlow.java @@ -20,25 +20,24 @@ import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderVali import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty; @InitiatedBy(protocol = "append-chat-protocol") -class AppendChatResponderFlow implements ResponderFlow { +public class AppendChatResponderFlow implements ResponderFlow { private final Logger log = LoggerFactory.getLogger(AppendChatResponderFlow.class); @CordaInject public UtxoLedgerService utxoLedgerService; - @CordaSerializable + /* static public class TxValidator implements UtxoTransactionValidator { private final Logger log = LoggerFactory.getLogger(AppendChatResponderFlow.class); - @ConstructorForDeserialization public TxValidator(FlowSession session) { this.session = session; } @Override public void checkTransaction(@NotNull UtxoLedgerTransaction ledgerTransaction) { - ChatState state = (ChatState) ledgerTransaction.getInputContractStates().get(0); + ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0); if (checkForBannedWords(state.getMessage()) || !checkMessageFromMatchesCounterparty(state, session.getCounterparty())) { throw new IllegalStateException("Failed verification"); } @@ -48,12 +47,27 @@ public UtxoLedgerService utxoLedgerService; private FlowSession session; } + */ + @Suspendable @Override public void call(@NotNull FlowSession session) { - + log.info("AppendChatResponderFlow.call() called"); try { - TxValidator txValidator = new TxValidator(session); + //TxValidator txValidator = new TxValidator(session); + UtxoTransactionValidator txValidator = ledgerTransaction -> { + ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0); + log.info("ChatState.getMessage() = " + state.getMessage()); + log.info("ledgerTransaction.getOutputContractStates().size() = " + ledgerTransaction.getOutputContractStates().size()) ; + log.info("session.getCounterParty()=" + session.getCounterparty()); + log.info("state.getMessageFrom()=" + state.getMessageFrom()); + log.info("checkForBannedWords(state.getMessage) = " + checkForBannedWords(state.getMessage())); + log.info("checkMessageFromMatchesCounterparty(state, session.getCounterparty())=" + checkMessageFromMatchesCounterparty(state, session.getCounterparty())); + if (checkForBannedWords(state.getMessage()) || !checkMessageFromMatchesCounterparty(state, session.getCounterparty())) { + throw new IllegalStateException("Failed verification"); + } + log.info("Verified the transaction - " + ledgerTransaction.getId()); + }; // This is not a problem. UtxoSignedTransaction finalizedSignedTransaction = utxoLedgerService.receiveFinality(session, txValidator); diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatSubFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatSubFlow.java index 36d9825..7350702 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatSubFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatSubFlow.java @@ -52,7 +52,6 @@ public class AppendChatSubFlow implements SubFlow { ); retVal = finalizedSignedTransaction.getId().toString(); - //retVal = "The returned"; log.info("Success! Response: " + retVal); } catch (Exception e) { log.warn("Finality failed", e); 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 ce5d140..81b3ec0 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 @@ -32,7 +32,7 @@ import static java.util.Objects.*; @InitiatingFlow(protocol = "create-chat-protocol") public class CreateNewChatFlow implements RPCStartableFlow { - private final Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class); + private final static Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class); @CordaInject public JsonMarshallingService jsonMarshallingService; @@ -90,6 +90,8 @@ public class CreateNewChatFlow implements RPCStartableFlow { */ + // Quasar checkpointing has a bugs handling lambdas in flows. + // This is being worked upon. PublicKey notaryKey = null; for(MemberInfo info: memberLookup.lookup()){ if(Objects.equals(info.getMemberProvidedContext().get("corda.notary.service.name"), notary.getName().toString()) ) { 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 245d9bb..3b88738 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 @@ -17,7 +17,7 @@ public final class ResponderValidationHelpers { @Suspendable public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) { - return state.getMessageFrom() == otherMember; + return state.getMessageFrom().equals(otherMember); } // This class just introduces a scope for some helper functions and should not be instantiated. 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 e218af4..d3e4d31 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 @@ -108,3 +108,14 @@ public class UpdateChatFlow implements RPCStartableFlow { } } +/* +RequestBody for triggering the flow via http-rpc: +{ + "clientRequestId": "update-1", + "flowClassName": "com.r3.developers.csdetemplate.utxoexample.workflows.UpdateChatFlow", + "requestData": { + "id":" ** chat id **", + "message": "How are you today?" + } +} + */ \ No newline at end of file diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/utilities/CorDappHelpers.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/utilities/CorDappHelpers.java index 7a22c44..ca09a17 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/utilities/CorDappHelpers.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/utilities/CorDappHelpers.java @@ -13,7 +13,6 @@ public final class CorDappHelpers { Collection results = collection.stream().filter(filterFn).collect(Collectors.toList()); if(results.size() != 1){ throw new IllegalArgumentException(exceptionMsg); - //throw new Exception(exceptionMsg); } return results.iterator().next(); }