tidied CreateChatFlow

This commit is contained in:
mattbradburyr3 2023-01-29 14:08:41 +00:00
parent 974a6be2b5
commit 1c620b2fbc
4 changed files with 35 additions and 29 deletions

View File

@ -34,7 +34,6 @@ public class ChatContract implements Contract {
@Override @Override
public void verify(UtxoLedgerTransaction transaction) { public void verify(UtxoLedgerTransaction transaction) {
// Command command = requireNonNull( transaction.getCommands().get(0), "Require a single command"); // this doesn't ensure there is one command // 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."); requireThat( transaction.getCommands().size() == 1, "Require a single command.");
Command command = transaction.getCommands().get(0); Command command = transaction.getCommands().get(0);
@ -58,7 +57,6 @@ public class ChatContract implements Contract {
input.getParticipants().containsAll(output.getParticipants()) && input.getParticipants().containsAll(output.getParticipants()) &&
output.getParticipants().containsAll(input.getParticipants()), output.getParticipants().containsAll(input.getParticipants()),
"When command is Update participants must not change."); "When command is Update participants must not change.");
} }
else { else {
throw new CordaRuntimeException("Unsupported command"); throw new CordaRuntimeException("Unsupported command");

View File

@ -5,8 +5,8 @@ import com.r3.developers.csdetemplate.utxoexample.states.ChatState;
import net.corda.v5.application.flows.*; import net.corda.v5.application.flows.*;
import net.corda.v5.application.marshalling.JsonMarshallingService; import net.corda.v5.application.marshalling.JsonMarshallingService;
import net.corda.v5.application.membership.MemberLookup; 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.annotations.Suspendable;
import net.corda.v5.base.exceptions.CordaRuntimeException;
import net.corda.v5.base.types.MemberX500Name; import net.corda.v5.base.types.MemberX500Name;
import net.corda.v5.ledger.common.NotaryLookup; import net.corda.v5.ledger.common.NotaryLookup;
import net.corda.v5.ledger.common.Party; 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.ledger.utxo.transaction.UtxoTransactionBuilder;
import net.corda.v5.membership.MemberInfo; import net.corda.v5.membership.MemberInfo;
import net.corda.v5.membership.NotaryInfo; import net.corda.v5.membership.NotaryInfo;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,28 +44,28 @@ public class CreateNewChatFlow implements RPCStartableFlow {
@CordaInject @CordaInject
public NotaryLookup notaryLookup; public NotaryLookup notaryLookup;
@CordaInject
public FlowMessaging flowMessaging;
@CordaInject @CordaInject
public FlowEngine flowEngine; public FlowEngine flowEngine;
@NotNull // @NotNull
@Suspendable @Suspendable
@Override @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"); log.info("CreateNewChatFlow.call() called");
try { try {
CreateNewChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, CreateNewChatFlowArgs.class); CreateNewChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, CreateNewChatFlowArgs.class);
MemberInfo myInfo = memberLookup.myInfo(); MemberInfo myInfo = memberLookup.myInfo();
MemberInfo otherMember = requireNonNull( MemberInfo otherMember = requireNonNull(
memberLookup.lookup(MemberX500Name.parse(flowArgs.getOtherMember())), 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(), flowArgs.getChatName(),
myInfo.getName(), myInfo.getName(),
flowArgs.getMessage(), flowArgs.getMessage(),
@ -92,19 +91,28 @@ public class CreateNewChatFlow implements RPCStartableFlow {
// Quasar checkpointing has a bugs handling lambdas in flows. // Quasar checkpointing has a bugs handling lambdas in flows.
// This is being worked upon. // This is being worked upon.
PublicKey notaryKey = null; PublicKey notaryKey = null;
for(MemberInfo info: memberLookup.lookup()){ for(MemberInfo memberInfo: memberLookup.lookup()){
if(Objects.equals(info.getMemberProvidedContext().get("corda.notary.service.name"), notary.getName().toString()) ) { if(Objects.equals(
notaryKey = info.getLedgerKeys().get(0); memberInfo.getMemberProvidedContext().get("corda.notary.service.name"),
notary.getName().toString())) {
notaryKey = memberInfo.getLedgerKeys().get(0);
break; 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()); // This exception would never be reached because if 'notaryLookup.getNotaryServices().iterator().next()'
log.info("chatState = " + chatState); // didn't return a value, it would have thrown a NoSuchElementException
log.info("chatState.getParticipants().size() = " + chatState.getParticipants().size()); // 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() UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder()
.setNotary(new Party(notary.getName(), notaryKey)) .setNotary(new Party(notary.getName(), notaryKey))
@ -114,17 +122,17 @@ public class CreateNewChatFlow implements RPCStartableFlow {
.addSignatories(chatState.getParticipants()); .addSignatories(chatState.getParticipants());
log.info("Before UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(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().size() = " + myInfo.getLedgerKeys().size());
log.info("myInfo.getLedgerKeys().get(0) = " + myInfo.getLedgerKeys().get(0)); // log.info("myInfo.getLedgerKeys().get(0) = " + myInfo.getLedgerKeys().get(0));
@SuppressWarnings("DEPRECATION") @SuppressWarnings("DEPRECATION")
UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0)); UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0));
log.info("After 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())); return flowEngine.subFlow(new FinalizeChatSubFlow(signedTransaction, otherMember.getName()));
} }
catch (Exception e) { catch (Exception e) {
log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'"); log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'");
throw e; throw new CordaRuntimeException(e.getMessage());
} }
} }
} }

View File

@ -14,14 +14,14 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
@InitiatingFlow(protocol = "append-chat-protocol") @InitiatingFlow(protocol = "append-chat-protocol")
public class AppendChatSubFlow implements SubFlow<String> { public class FinalizeChatSubFlow implements SubFlow<String> {
public AppendChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) {
this.signedTransaction = signedTransaction; this.signedTransaction = signedTransaction;
this.otherMember = otherMember; this.otherMember = otherMember;
} }
private final static Logger log = LoggerFactory.getLogger(AppendChatSubFlow.class); private final static Logger log = LoggerFactory.getLogger(FinalizeChatSubFlow.class);
@CordaInject @CordaInject
public UtxoLedgerService ledgerService; public UtxoLedgerService ledgerService;

View File

@ -94,7 +94,7 @@ public class UpdateChatFlow implements RPCStartableFlow {
@SuppressWarnings("DEPRECATION") @SuppressWarnings("DEPRECATION")
UtxoSignedTransaction signedTransaction = txBuilder.toSignedTransaction(myInfo.getLedgerKeys().get(0)); 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) { } catch (Exception e) {
log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'"); log.warn("Failed to process utxo flow for request body '$requestBody' because:'${e.message}'");
throw e; throw e;