tidied up FinalizeResponderFlow

This commit is contained in:
mattbradburyr3 2023-01-29 18:34:51 +00:00
parent 1fb18290ca
commit 4c58c0b0a8
4 changed files with 70 additions and 39 deletions

View File

@ -28,7 +28,7 @@ import java.util.UUID;
import static java.util.Objects.*; import static java.util.Objects.*;
@InitiatingFlow(protocol = "create-chat-protocol") //@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);

View File

@ -6,6 +6,7 @@ import net.corda.v5.application.flows.InitiatedBy;
import net.corda.v5.application.flows.ResponderFlow; import net.corda.v5.application.flows.ResponderFlow;
import net.corda.v5.application.messaging.FlowSession; import net.corda.v5.application.messaging.FlowSession;
import net.corda.v5.base.annotations.Suspendable; 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.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;
@ -13,8 +14,11 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords; import java.util.Arrays;
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty; 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 {
@ -25,8 +29,10 @@ public class FinalizeChatResponderFlow implements ResponderFlow {
@Suspendable @Suspendable
@Override @Override
public void call(@NotNull FlowSession session) { public void call(FlowSession session) {
log.info("AppendChatResponderFlow.call() called");
log.info("FinalizeChatResponderFlow.call() called");
try { try {
UtxoTransactionValidator txValidator = ledgerTransaction -> { UtxoTransactionValidator txValidator = ledgerTransaction -> {
ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0); ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0);
@ -45,4 +51,17 @@ public class FinalizeChatResponderFlow implements ResponderFlow {
log.warn("Exceptionally finished responder flow", e); log.warn("Exceptionally finished responder flow", e);
} }
} }
@Suspendable
Boolean checkForBannedWords(String str) {
List<String> bannedWords = Arrays.asList("banana", "apple", "pear");
return bannedWords.stream().anyMatch(str::contains);
}
@Suspendable
Boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) {
return state.getMessageFrom().equals(otherMember);
}
} }

View File

@ -16,6 +16,10 @@ 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 UtxoSignedTransaction signedTransaction;
private final MemberX500Name otherMember;
public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) {
this.signedTransaction = signedTransaction; this.signedTransaction = signedTransaction;
this.otherMember = otherMember; this.otherMember = otherMember;
@ -33,46 +37,47 @@ public class FinalizeChatSubFlow implements SubFlow<String> {
@Suspendable @Suspendable
public String call() { public String call() {
// log.info("AppendChatFlow.call() called"); log.info("FinalizeChatFlow.call() called");
// log.info("otherMember = " + otherMember); // log.info("otherMember = " + otherMember);
FlowSession session = flowMessaging.initiateFlow(otherMember); FlowSession session = flowMessaging.initiateFlow(otherMember);
String retVal; String result;
try { try {
List<FlowSession> sessionsList = Arrays.asList(session); List<FlowSession> sessionsList = Arrays.asList(session);
log.info("sessionList.size()=" + sessionsList.size()); // log.info("sessionList.size()=" + sessionsList.size());
UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize( UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize(
signedTransaction, signedTransaction,
sessionsList sessionsList
); );
retVal = finalizedSignedTransaction.getId().toString(); result = finalizedSignedTransaction.getId().toString();
log.info("Success! Response: " + retVal); log.info("Success! Response: " + result);
} catch (Exception e) { } catch (Exception e) {
log.warn("Finality failed", e); log.warn("Finality failed", e);
retVal = "Finality failed, " + e.getMessage(); result = "Finality failed, " + e.getMessage();
} }
log.info("AppendChatSubFlow call returns=" + retVal); // log.info("FinalizeChatSubFlow call returns=" + retVal);
return retVal; return result;
} }
public UtxoSignedTransaction getSignedTransaction() { // We don't need getters and setters as the properties should be private
return signedTransaction;
}
public void setSignedTransaction(UtxoSignedTransaction signedTransaction) { // public UtxoSignedTransaction getSignedTransaction() {
this.signedTransaction = signedTransaction; // 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;
} }

View File

@ -7,19 +7,26 @@ import net.corda.v5.base.types.MemberX500Name;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 class ResponderValidationHelpers {
public final static List<String> bannedWords = Arrays.asList("banana", "apple", "pear"); // public final static List<String> bannedWords = Arrays.asList("banana", "apple", "pear");
//
// @Suspendable
// public static boolean checkForBannedWords(String str) {
// return bannedWords.stream().anyMatch(str::contains);
// }
@Suspendable // @Suspendable
public static boolean checkForBannedWords(String str) { // public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) {
return bannedWords.stream().anyMatch(str::contains); // 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. // This class just introduces a scope for some helper functions and should not be instantiated.
private ResponderValidationHelpers() {} // private ResponderValidationHelpers() {}
} }