From 1c620b2fbc1677269c0a68433ac790df895a0a58 Mon Sep 17 00:00:00 2001 From: mattbradburyr3 Date: Sun, 29 Jan 2023 14:08:41 +0000 Subject: [PATCH] tidied CreateChatFlow --- .../utxoexample/contracts/ChatContract.java | 2 - .../workflows/CreateNewChatFlow.java | 54 +++++++++++-------- ...tSubFlow.java => FinalizeChatSubFlow.java} | 6 +-- .../utxoexample/workflows/UpdateChatFlow.java | 2 +- 4 files changed, 35 insertions(+), 29 deletions(-) rename workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/{AppendChatSubFlow.java => FinalizeChatSubFlow.java} (89%) diff --git a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java index 1367913..15f6b54 100644 --- a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java +++ b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java @@ -34,7 +34,6 @@ public class ChatContract implements Contract { @Override public void verify(UtxoLedgerTransaction transaction) { - // Command command = requireNonNull( transaction.getCommands().get(0), "Require a single command"); // this doesn't ensure there is one command requireThat( transaction.getCommands().size() == 1, "Require a single command."); Command command = transaction.getCommands().get(0); @@ -58,7 +57,6 @@ public class ChatContract implements Contract { input.getParticipants().containsAll(output.getParticipants()) && output.getParticipants().containsAll(input.getParticipants()), "When command is Update participants must not change."); - } else { throw new CordaRuntimeException("Unsupported command"); 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 c433fab..3910645 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 @@ -5,8 +5,8 @@ import com.r3.developers.csdetemplate.utxoexample.states.ChatState; import net.corda.v5.application.flows.*; 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.base.types.MemberX500Name; import net.corda.v5.ledger.common.NotaryLookup; import net.corda.v5.ledger.common.Party; @@ -15,7 +15,6 @@ 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; @@ -45,28 +44,28 @@ public class CreateNewChatFlow implements RPCStartableFlow { @CordaInject public NotaryLookup notaryLookup; - @CordaInject - public FlowMessaging flowMessaging; - @CordaInject public FlowEngine flowEngine; - @NotNull +// @NotNull @Suspendable @Override - public String call(@NotNull RPCRequestData requestBody) throws IllegalArgumentException { +// public String call(@NotNull RPCRequestData requestBody) throws IllegalArgumentException { + public String call(RPCRequestData requestBody) { log.info("CreateNewChatFlow.call() called"); + try { CreateNewChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, CreateNewChatFlowArgs.class); MemberInfo myInfo = memberLookup.myInfo(); MemberInfo otherMember = requireNonNull( memberLookup.lookup(MemberX500Name.parse(flowArgs.getOtherMember())), - "can't find other member" + "MemberLookup can't find otherMember specified in flow arguments." ); - ChatState chatState = new ChatState(UUID.randomUUID(), + ChatState chatState = new ChatState( + UUID.randomUUID(), flowArgs.getChatName(), myInfo.getName(), flowArgs.getMessage(), @@ -92,19 +91,28 @@ 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()) ) { - notaryKey = info.getLedgerKeys().get(0); + for(MemberInfo memberInfo: memberLookup.lookup()){ + if(Objects.equals( + memberInfo.getMemberProvidedContext().get("corda.notary.service.name"), + notary.getName().toString())) { + notaryKey = memberInfo.getLedgerKeys().get(0); break; } } - if(notary == null) { - throw new NullPointerException("No notary found"); + + if(notaryKey == null) { + throw new CordaRuntimeException("No notary PublicKey found"); } - log.info("notary.getName()=" + notary.getName()); - log.info("chatState = " + chatState); - log.info("chatState.getParticipants().size() = " + chatState.getParticipants().size()); + // This exception would never be reached because if 'notaryLookup.getNotaryServices().iterator().next()' + // didn't return a value, it would have thrown a NoSuchElementException +// if(notary == null) { +// throw new NullPointerException("No notary found"); +// } + +// log.info("notary.getName()=" + notary.getName()); +// log.info("chatState = " + chatState); +// log.info("chatState.getParticipants().size() = " + chatState.getParticipants().size()); UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder() .setNotary(new Party(notary.getName(), notaryKey)) @@ -114,17 +122,17 @@ public class CreateNewChatFlow implements RPCStartableFlow { .addSignatories(chatState.getParticipants()); - log.info("Before UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0));"); - log.info("myInfo.getLedgerKeys().size() = " + myInfo.getLedgerKeys().size()); - log.info("myInfo.getLedgerKeys().get(0) = " + myInfo.getLedgerKeys().get(0)); +// log.info("Before UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0));"); +// log.info("myInfo.getLedgerKeys().size() = " + myInfo.getLedgerKeys().size()); +// log.info("myInfo.getLedgerKeys().get(0) = " + myInfo.getLedgerKeys().get(0)); @SuppressWarnings("DEPRECATION") UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0)); - log.info("After UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0));"); - return flowEngine.subFlow(new AppendChatSubFlow(signedTransaction, otherMember.getName())); +// log.info("After UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0));"); + return flowEngine.subFlow(new FinalizeChatSubFlow(signedTransaction, otherMember.getName())); } catch (Exception e) { log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'"); - throw e; + throw new CordaRuntimeException(e.getMessage()); } } } 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/FinalizeChatSubFlow.java similarity index 89% rename from workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/AppendChatSubFlow.java rename to workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/FinalizeChatSubFlow.java index 5c4d552..7d982af 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/FinalizeChatSubFlow.java @@ -14,14 +14,14 @@ import java.util.Arrays; import java.util.List; @InitiatingFlow(protocol = "append-chat-protocol") -public class AppendChatSubFlow implements SubFlow { +public class FinalizeChatSubFlow implements SubFlow { - public AppendChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { + public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { this.signedTransaction = signedTransaction; this.otherMember = otherMember; } - private final static Logger log = LoggerFactory.getLogger(AppendChatSubFlow.class); + private final static Logger log = LoggerFactory.getLogger(FinalizeChatSubFlow.class); @CordaInject public UtxoLedgerService ledgerService; 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 e682871..d1b0fa4 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 @@ -94,7 +94,7 @@ public class UpdateChatFlow implements RPCStartableFlow { @SuppressWarnings("DEPRECATION") UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0)); - return flowEngine.subFlow(new AppendChatSubFlow(signedTransaction, otherMember.getName())); + return flowEngine.subFlow(new FinalizeChatSubFlow(signedTransaction, otherMember.getName())); } catch (Exception e) { log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'"); throw e;