tidy GetChatResponse

This commit is contained in:
mattbradburyr3 2023-01-29 20:54:48 +00:00
parent c90af45c6b
commit 6e703bca2e
9 changed files with 18 additions and 187 deletions

View File

@ -1,23 +0,0 @@
package com.r3.developers.csdetemplate.utilities;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Predicate;
import java.util.stream.Collectors;
//
//public final class CorDappHelpers {
// public static <T> T findAndExpectExactlyOne(Collection<T> collection, Predicate<? super T> filterFn, String exceptionMsg)
// {
// Collection<T> results = collection.stream().filter(filterFn).collect(Collectors.toList());
// if(results.size() != 1){
// throw new RuntimeException(exceptionMsg);
// }
// return results.iterator().next();
// }
//
// public static <T> T findAndExpectExactlyOne(Collection<T> collection, String exceptionMsg) {
// return findAndExpectExactlyOne(collection, e -> true, exceptionMsg);
// }
//}

View File

@ -1,11 +1,7 @@
package com.r3.developers.csdetemplate.utxoexample.workflows; package com.r3.developers.csdetemplate.utxoexample.workflows;
import net.corda.v5.base.annotations.ConstructorForDeserialization;
import net.corda.v5.base.annotations.CordaSerializable;
import java.util.UUID; import java.util.UUID;
//@CordaSerializable
public class ChatStateResults { public class ChatStateResults {
private UUID id; private UUID id;
@ -15,7 +11,6 @@ public class ChatStateResults {
public ChatStateResults() {} public ChatStateResults() {}
// @ConstructorForDeserialization
public ChatStateResults(UUID id, String chatName, String messageFromName, String message) { public ChatStateResults(UUID id, String chatName, String messageFromName, String message) {
this.id = id; this.id = id;
this.chatName = chatName; this.chatName = chatName;
@ -27,34 +22,15 @@ public class ChatStateResults {
return id; return id;
} }
// public void setId(UUID id) {
// this.id = id;
// }
public String getChatName() { public String getChatName() {
return chatName; return chatName;
} }
// public void setChatName(String chatName) {
// this.chatName = chatName;
// }
public String getMessageFromName() { public String getMessageFromName() {
return messageFromName; return messageFromName;
} }
// public void setMessageFromName(String messageFromName) {
// this.messageFromName = messageFromName;
// }
public String getMessage() { public String getMessage() {
return message; return message;
} }
// public void setMessage(String message) {
// this.message = message;
// }
} }

View File

@ -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;
@ -28,7 +27,6 @@ import java.util.UUID;
import static java.util.Objects.*; import static java.util.Objects.*;
//@InitiatingFlow(protocol = "create-chat-protocol")
public class CreateNewChatFlow implements RPCStartableFlow { public class CreateNewChatFlow implements RPCStartableFlow {
private final static Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class); private final static Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class);
@ -51,10 +49,9 @@ public class CreateNewChatFlow implements RPCStartableFlow {
@Suspendable @Suspendable
@Override @Override
// public String call(@NotNull RPCRequestData requestBody) throws IllegalArgumentException {
public String call( RPCRequestData requestBody) { 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);
@ -74,23 +71,7 @@ public class CreateNewChatFlow implements RPCStartableFlow {
); );
NotaryInfo notary = notaryLookup.getNotaryServices().iterator().next(); NotaryInfo notary = notaryLookup.getNotaryServices().iterator().next();
/*
// Lambda have problems see https://r3-cev.atlassian.net/browse/CORE-8983
// Lambda here.
Predicate<MemberInfo> myPred = memberInfo -> Objects.equals(
memberInfo.getMemberProvidedContext().get("corda.notary.service.name"),
notary.getName().toString()
);
List<MemberInfo> lmi = memberLookup.lookup();
MemberInfo thing = lmi.stream().filter(myPred).iterator().next();
PublicKey notaryKey = thing.getLedgerKeys().get(0);
*/
// Quasar checkpointing has a bugs handling lambdas in flows.
// This is being worked upon.
PublicKey notaryKey = null; PublicKey notaryKey = null;
for(MemberInfo memberInfo: memberLookup.lookup()){ for(MemberInfo memberInfo: memberLookup.lookup()){
if(Objects.equals( if(Objects.equals(
@ -105,16 +86,6 @@ public class CreateNewChatFlow implements RPCStartableFlow {
throw new CordaRuntimeException("No notary PublicKey found"); throw new CordaRuntimeException("No notary PublicKey found");
} }
// 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() UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder()
.setNotary(new Party(notary.getName(), notaryKey)) .setNotary(new Party(notary.getName(), notaryKey))
.setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis())) .setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis()))
@ -122,13 +93,9 @@ public class CreateNewChatFlow implements RPCStartableFlow {
.addCommand(new ChatContract.Create()) .addCommand(new ChatContract.Create())
.addSignatories(chatState.getParticipants()); .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));
@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));");
return flowEngine.subFlow(new FinalizeChatSubFlow(signedTransaction, otherMember.getName())); return flowEngine.subFlow(new FinalizeChatSubFlow(signedTransaction, otherMember.getName()));
} }
catch (Exception e) { catch (Exception e) {
@ -138,7 +105,6 @@ public class CreateNewChatFlow implements RPCStartableFlow {
} }
} }
/* /*
RequestBody for triggering the flow via http-rpc: RequestBody for triggering the flow via http-rpc:
{ {

View File

@ -1,9 +1,5 @@
package com.r3.developers.csdetemplate.utxoexample.workflows; package com.r3.developers.csdetemplate.utxoexample.workflows;
import net.corda.v5.base.annotations.ConstructorForDeserialization;
import net.corda.v5.base.annotations.CordaSerializable;
//@CordaSerializable - we're not sending it down the wire so we don't need this.
public class CreateNewChatFlowArgs{ public class CreateNewChatFlowArgs{
// Serialisation service requires a default constructor // Serialisation service requires a default constructor
@ -13,8 +9,6 @@ public class CreateNewChatFlowArgs{
private String message; private String message;
private String otherMember; private String otherMember;
// @ConstructorForDeserialization // don't appear to need this
public CreateNewChatFlowArgs(String chatName, String message, String otherMember) { public CreateNewChatFlowArgs(String chatName, String message, String otherMember) {
this.chatName = chatName; this.chatName = chatName;
this.message = message; this.message = message;
@ -25,25 +19,11 @@ public class CreateNewChatFlowArgs{
return chatName; return chatName;
} }
// public void setChatName(String chatName) {
// this.chatName = chatName;
// }
public String getMessage() { public String getMessage() {
return message; return message;
} }
// public void setMessage(String message) {
// this.message = message;
// }
public String getOtherMember() { public String getOtherMember() {
return otherMember; return otherMember;
} }
// public void setOtherMember(String otherMember) {
// this.otherMember = otherMember;
// }
} }

View File

@ -10,16 +10,12 @@ import net.corda.v5.base.types.MemberX500Name;
import net.corda.v5.ledger.utxo.UtxoLedgerService; import net.corda.v5.ledger.utxo.UtxoLedgerService;
import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction; import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction;
import net.corda.v5.ledger.utxo.transaction.UtxoTransactionValidator; import net.corda.v5.ledger.utxo.transaction.UtxoTransactionValidator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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") @InitiatedBy(protocol = "finalize-chat-protocol")
public class FinalizeChatResponderFlow implements ResponderFlow { public class FinalizeChatResponderFlow implements ResponderFlow {
private final static Logger log = LoggerFactory.getLogger(FinalizeChatResponderFlow.class); private final static Logger log = LoggerFactory.getLogger(FinalizeChatResponderFlow.class);
@ -42,7 +38,6 @@ public class FinalizeChatResponderFlow implements ResponderFlow {
log.info("Verified the transaction - " + ledgerTransaction.getId()); log.info("Verified the transaction - " + ledgerTransaction.getId());
}; };
// This is not a problem.
UtxoSignedTransaction finalizedSignedTransaction = utxoLedgerService.receiveFinality(session, txValidator); UtxoSignedTransaction finalizedSignedTransaction = utxoLedgerService.receiveFinality(session, txValidator);
log.info("Finished responder flow - " + finalizedSignedTransaction.getId()); log.info("Finished responder flow - " + finalizedSignedTransaction.getId());
} }

View File

@ -16,16 +16,7 @@ import java.util.List;
@InitiatingFlow(protocol = "finalize-chat-protocol") @InitiatingFlow(protocol = "finalize-chat-protocol")
public class FinalizeChatSubFlow implements SubFlow<String> { public class FinalizeChatSubFlow implements SubFlow<String> {
// these need to be private + good practice is to declare them at the start of the class private final static Logger log = LoggerFactory.getLogger(FinalizeChatSubFlow.class);
// java code conventions:
/*
* According to Code Conventions for the Java Programming Language, the parts of a class or interface declaration should appear in the following order:
* Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
* Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
* Constructors
* Methods
* */
private final UtxoSignedTransaction signedTransaction; private final UtxoSignedTransaction signedTransaction;
private final MemberX500Name otherMember; private final MemberX500Name otherMember;
@ -34,8 +25,6 @@ public class FinalizeChatSubFlow implements SubFlow<String> {
this.otherMember = otherMember; this.otherMember = otherMember;
} }
private final static Logger log = LoggerFactory.getLogger(FinalizeChatSubFlow.class);
@CordaInject @CordaInject
public UtxoLedgerService ledgerService; public UtxoLedgerService ledgerService;
@ -47,13 +36,12 @@ public class FinalizeChatSubFlow implements SubFlow<String> {
public String call() { public String call() {
log.info("FinalizeChatFlow.call() called"); log.info("FinalizeChatFlow.call() called");
// log.info("otherMember = " + otherMember);
FlowSession session = flowMessaging.initiateFlow(otherMember); FlowSession session = flowMessaging.initiateFlow(otherMember);
String result; String result;
try { try {
List<FlowSession> sessionsList = Arrays.asList(session); List<FlowSession> sessionsList = Arrays.asList(session);
// log.info("sessionList.size()=" + sessionsList.size());
UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize( UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize(
signedTransaction, signedTransaction,
@ -62,31 +50,12 @@ public class FinalizeChatSubFlow implements SubFlow<String> {
result = finalizedSignedTransaction.getId().toString(); result = finalizedSignedTransaction.getId().toString();
log.info("Success! Response: " + result); log.info("Success! Response: " + result);
} catch (Exception e) { } catch (Exception e) {
log.warn("Finality failed", e); log.warn("Finality failed", e);
result = "Finality failed, " + e.getMessage(); result = "Finality failed, " + e.getMessage();
} }
// log.info("FinalizeChatSubFlow call returns=" + retVal);
return result; return result;
} }
// We don't need getters and setters as the properties should be private
// 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;
// }
} }

View File

@ -16,8 +16,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.*; import java.util.*;
//import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne;
import static java.util.Objects.*; import static java.util.Objects.*;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
@ -44,17 +42,6 @@ public class GetChatFlow implements RPCStartableFlow {
if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found"); if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found");
StateAndRef<ChatState> chatStateAndRef = chatStateAndRefsWithId.get(0); StateAndRef<ChatState> chatStateAndRef = chatStateAndRefsWithId.get(0);
// log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size());
// log.info("GetChatFlow stateAndRefs = " + stateAndRefs);
// StateAndRef<ChatState> state = findAndExpectExactlyOne(stateAndRefs,
// stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()),
// "did not find an unique ChatState"
// );
return jsonMarshallingService.format(resolveMessagesFromBackchain(chatStateAndRef, flowArgs.getNumberOfRecords() )); return jsonMarshallingService.format(resolveMessagesFromBackchain(chatStateAndRef, flowArgs.getNumberOfRecords() ));
} }
@ -77,11 +64,6 @@ public class GetChatFlow implements RPCStartableFlow {
"Transaction " + transactionId + " not found." "Transaction " + transactionId + " not found."
); );
// ChatState output = findAndExpectExactlyOne(
// transaction.getOutputStates(ChatState.class),
// "Expecting one and only one ChatState output for transaction " + transactionId
// );
List<ChatState> chatStates = transaction.getOutputStates(ChatState.class); List<ChatState> chatStates = transaction.getOutputStates(ChatState.class);
if (chatStates.size() != 1) throw new CordaRuntimeException( if (chatStates.size() != 1) throw new CordaRuntimeException(
"Expecting one and only one ChatState output for transaction " + transactionId + "."); "Expecting one and only one ChatState output for transaction " + transactionId + ".");

View File

@ -1,18 +1,13 @@
package com.r3.developers.csdetemplate.utxoexample.workflows; package com.r3.developers.csdetemplate.utxoexample.workflows;
import net.corda.v5.base.annotations.ConstructorForDeserialization;
import net.corda.v5.base.annotations.CordaSerializable;
import java.util.UUID; import java.util.UUID;
//@CordaSerializable
public class GetChatFlowArgs { public class GetChatFlowArgs {
private UUID id; private UUID id;
private int numberOfRecords; private int numberOfRecords;
public GetChatFlowArgs() {} public GetChatFlowArgs() {}
// @ConstructorForDeserialization
public GetChatFlowArgs(UUID id, int numberOfRecords ) { public GetChatFlowArgs(UUID id, int numberOfRecords ) {
this.id = id; this.id = id;
this.numberOfRecords = numberOfRecords; this.numberOfRecords = numberOfRecords;
@ -22,18 +17,7 @@ public class GetChatFlowArgs {
return id; return id;
} }
// public void setId(UUID id) {
// this.id = id;
// }
public int getNumberOfRecords() { public int getNumberOfRecords() {
return numberOfRecords; return numberOfRecords;
} }
// public void setNumberOfRecords(int numberOfRecords) {
// this.numberOfRecords = numberOfRecords;
// }
} }

View File

@ -3,11 +3,14 @@ package com.r3.developers.csdetemplate.utxoexample.workflows;
import net.corda.v5.base.annotations.ConstructorForDeserialization; import net.corda.v5.base.annotations.ConstructorForDeserialization;
import net.corda.v5.base.annotations.CordaSerializable; import net.corda.v5.base.annotations.CordaSerializable;
@CordaSerializable //@CordaSerializable
public class GetChatResponse { public class GetChatResponse {
private String messageFrom;
private String message;
public GetChatResponse() {} public GetChatResponse() {}
@ConstructorForDeserialization // @ConstructorForDeserialization
public GetChatResponse(String messageFrom, String message) { public GetChatResponse(String messageFrom, String message) {
this.messageFrom = messageFrom; this.messageFrom = messageFrom;
this.message = message; this.message = message;
@ -17,18 +20,17 @@ public class GetChatResponse {
return messageFrom; return messageFrom;
} }
public void setMessageFrom(String messageFrom) { // public void setMessageFrom(String messageFrom) {
this.messageFrom = messageFrom; // this.messageFrom = messageFrom;
} // }
public String getMessage() { public String getMessage() {
return message; return message;
} }
public void setMessage(String message) { // public void setMessage(String message) {
this.message = message; // this.message = message;
} // }
public String messageFrom;
public String message;
} }