From d475cb19d7a69c295d4c04c681f145b02e7d816c Mon Sep 17 00:00:00 2001 From: Chris Barratt Date: Tue, 17 Jan 2023 17:52:33 +0000 Subject: [PATCH] WIP commit pre Dan's suggestions --- .../utxoexample/states/ChatState.java | 29 +++++++++++--- .../workflows/AppendChatResponderFlow.java | 38 +++++++++++++++---- .../workflows/AppendChatSubFlow.java | 36 +++++++++++++++--- .../workflows/ChatStateResults.java | 14 +++++-- .../workflows/CreateNewChatFlow.java | 27 ++++++++++++- .../workflows/CreateNewChatFlowArgs.java | 14 ++++--- .../workflows/GetChatFlowArgs.java | 9 ++++- .../workflows/GetChatResponse.java | 10 ++++- .../workflows/UpdateChatFlowArgs.java | 10 +++-- 9 files changed, 150 insertions(+), 37 deletions(-) diff --git a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java index 289d2a0..8073cac 100644 --- a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java +++ b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java @@ -1,6 +1,8 @@ package com.r3.developers.csdetemplate.utxoexample.states; import com.r3.developers.csdetemplate.utxoexample.contracts.ChatContract; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; import net.corda.v5.base.types.MemberX500Name; import net.corda.v5.ledger.utxo.BelongsToContract; import net.corda.v5.ledger.utxo.ContractState; @@ -20,14 +22,15 @@ data class ChatState( fun updateMessage(messageFrom: MemberX500Name, message: String) = copy(messageFrom = messageFrom, message = message) } - */ +@CordaSerializable @BelongsToContract(ChatContract.class) public class ChatState implements ContractState { public ChatState() { } + @ConstructorForDeserialization public ChatState(UUID id, String chatName, MemberX500Name messageFrom, @@ -81,6 +84,10 @@ public class ChatState implements ContractState { return message; } + public void setMessage(String message) { + this.message = message; + } + @NotNull @Override public List getParticipants() { @@ -91,13 +98,23 @@ public class ChatState implements ContractState { this.participants = participants; } - private UUID id; - private String chatName; - private MemberX500Name messageFrom; - private String message; - List participants; + public UUID id; + public String chatName; + public MemberX500Name messageFrom; + public String message; + public List participants; public ChatState updateMessage(MemberX500Name name, String message) { return new ChatState(chatName, name, message, participants); } + + @Override + public String toString() { + return ChatState.class.getName() + + "(id=" + id + + ", chatName=" + chatName + + ", messageFrom=" + messageFrom + + ", participants=" + participants + + ")"; + } } \ No newline at end of file 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 4abc2fd..028653f 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 @@ -5,9 +5,13 @@ import net.corda.v5.application.flows.CordaInject; import net.corda.v5.application.flows.InitiatedBy; import net.corda.v5.application.flows.ResponderFlow; import net.corda.v5.application.messaging.FlowSession; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; import net.corda.v5.base.annotations.Suspendable; import net.corda.v5.ledger.utxo.UtxoLedgerService; +import net.corda.v5.ledger.utxo.transaction.UtxoLedgerTransaction; import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction; +import net.corda.v5.ledger.utxo.transaction.UtxoTransactionValidator; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,18 +27,36 @@ 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); + if (checkForBannedWords(state.getMessage()) || !checkMessageFromMatchesCounterparty(state, session.getCounterparty())) { + throw new IllegalStateException("Failed verification"); + } + log.info("Verified the transaction - " + ledgerTransaction.getId()); + + } + private FlowSession session; + } + @Suspendable @Override public void call(@NotNull FlowSession session) { + try { - UtxoSignedTransaction finalizedSignedTransaction = utxoLedgerService.receiveFinality(session, ledgerTransaction -> - { - ChatState state = (ChatState) ledgerTransaction.getInputContractStates().get(0); - if (checkForBannedWords(state.getMessage()) || !checkMessageFromMatchesCounterparty(state, session.getCounterparty())) { - throw new IllegalStateException("Failed verification"); - } - log.info("Verified the transaction - " + ledgerTransaction.getId()); - }); + TxValidator txValidator = new TxValidator(session); + + // This is not a problem. + UtxoSignedTransaction finalizedSignedTransaction = utxoLedgerService.receiveFinality(session, txValidator); log.info("Finished responder flow - " + finalizedSignedTransaction.getId()); } catch(Exception e) 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 9eb83a2..36d9825 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 @@ -4,8 +4,11 @@ import com.r3.developers.csdetemplate.utxoexample.states.ChatState; import net.corda.v5.application.flows.*; import net.corda.v5.application.messaging.FlowMessaging; import net.corda.v5.application.messaging.FlowSession; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; import net.corda.v5.base.annotations.Suspendable; import net.corda.v5.base.types.MemberX500Name; +import net.corda.v5.crypto.SecureHash; import net.corda.v5.ledger.utxo.UtxoLedgerService; import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction; import org.slf4j.Logger; @@ -17,13 +20,12 @@ import java.util.List; @InitiatingFlow(protocol = "append-chat-protocol") public class AppendChatSubFlow implements SubFlow { - public AppendChatSubFlow() {} public AppendChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) { this.signedTransaction = signedTransaction; this.otherMember = otherMember; } - private final Logger log = LoggerFactory.getLogger(AppendChatSubFlow.class); + private final static Logger log = LoggerFactory.getLogger(AppendChatSubFlow.class); @CordaInject public UtxoLedgerService ledgerService; @@ -36,24 +38,46 @@ public class AppendChatSubFlow implements SubFlow { public String call() { log.info("AppendChatFlow.call() called"); - + log.info("otherMember = " + otherMember); FlowSession session = flowMessaging.initiateFlow(otherMember); String retVal; try { + List sessionsList = Arrays.asList(session); + log.info("sessionList.size()=" + sessionsList.size()); + UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize( signedTransaction, - List.of(session) + sessionsList ); + retVal = finalizedSignedTransaction.getId().toString(); + //retVal = "The returned"; log.info("Success! Response: " + retVal); } catch (Exception e) { log.warn("Finality failed", e); retVal = "Finality failed, " + e.getMessage(); } + log.info("AppendChatSubFlow call returns=" + retVal); return retVal; } - private UtxoSignedTransaction signedTransaction; - private MemberX500Name otherMember; + 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; + } + + public UtxoSignedTransaction signedTransaction; + public MemberX500Name otherMember; } diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ChatStateResults.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ChatStateResults.java index 383c3de..7fbf2e0 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ChatStateResults.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/ChatStateResults.java @@ -1,9 +1,15 @@ 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; +@CordaSerializable public class ChatStateResults { public ChatStateResults() {} + + @ConstructorForDeserialization public ChatStateResults(UUID id, String chatName, String messageFromName, String message) { this.id = id; this.chatName = chatName; @@ -43,9 +49,9 @@ public class ChatStateResults { this.message = message; } - private UUID id; - private String chatName; - private String messageFromName; - private String message; + public UUID id; + public String chatName; + public String messageFromName; + public String message; } 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 6af7cea..ce5d140 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 @@ -75,6 +75,10 @@ public class CreateNewChatFlow implements RPCStartableFlow { ); NotaryInfo notary = notaryLookup.getNotaryServices().iterator().next(); + /* + // Lambda have problems see https://r3-cev.atlassian.net/browse/CORE-8983 + + // Lambda here. Predicate myPred = memberInfo -> Objects.equals( memberInfo.getMemberProvidedContext().get("corda.notary.service.name"), notary.getName().toString() @@ -84,6 +88,23 @@ public class CreateNewChatFlow implements RPCStartableFlow { MemberInfo thing = lmi.stream().filter(myPred).iterator().next(); PublicKey notaryKey = thing.getLedgerKeys().get(0); + */ + + 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); + break; + } + } + 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)) .setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis())) @@ -91,9 +112,13 @@ public class CreateNewChatFlow implements RPCStartableFlow { .addCommand(new ChatContract.Create()) .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") 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())); } catch (Exception e) { diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlowArgs.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlowArgs.java index 4fb711b..7c2fc62 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlowArgs.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlowArgs.java @@ -1,8 +1,14 @@ package com.r3.developers.csdetemplate.utxoexample.workflows; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; + +@CordaSerializable public class CreateNewChatFlowArgs{ public CreateNewChatFlowArgs() {} + + @ConstructorForDeserialization public CreateNewChatFlowArgs(String chatName, String message, String otherMember) { this.chatName = chatName; this.message = message; @@ -33,9 +39,7 @@ public class CreateNewChatFlowArgs{ this.otherMember = otherMember; } - private String chatName; - private String message; - - - private String otherMember; + public String chatName; + public String message; + public String otherMember; } \ No newline at end of file diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlowArgs.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlowArgs.java index 4250549..a334165 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlowArgs.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatFlowArgs.java @@ -1,10 +1,15 @@ 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; +@CordaSerializable public class GetChatFlowArgs { public GetChatFlowArgs() {} + @ConstructorForDeserialization public GetChatFlowArgs(UUID id, int numberOfRecords ) { this.id = id; this.numberOfRecords = numberOfRecords; @@ -27,6 +32,6 @@ public class GetChatFlowArgs { this.numberOfRecords = numberOfRecords; } - private UUID id; - private int numberOfRecords; + public UUID id; + public int numberOfRecords; } diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatResponse.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatResponse.java index 2425faf..512457e 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatResponse.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/GetChatResponse.java @@ -1,7 +1,13 @@ package com.r3.developers.csdetemplate.utxoexample.workflows; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; + +@CordaSerializable public class GetChatResponse { public GetChatResponse() {} + + @ConstructorForDeserialization public GetChatResponse(String messageFrom, String message) { this.messageFrom = messageFrom; this.message = message; @@ -23,6 +29,6 @@ public class GetChatResponse { this.message = message; } - private String messageFrom; - private String message; + public String messageFrom; + public String message; } diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlowArgs.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlowArgs.java index c171bca..4c91112 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlowArgs.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/UpdateChatFlowArgs.java @@ -1,10 +1,14 @@ package com.r3.developers.csdetemplate.utxoexample.workflows; -import java.util.UUID; +import net.corda.v5.base.annotations.ConstructorForDeserialization; +import net.corda.v5.base.annotations.CordaSerializable; +import java.util.UUID; +@CordaSerializable public class UpdateChatFlowArgs { public UpdateChatFlowArgs() {} + @ConstructorForDeserialization public UpdateChatFlowArgs(UUID id, String message) { this.id = id; this.message = message; @@ -26,7 +30,7 @@ public class UpdateChatFlowArgs { this.message = message; } - private UUID id; - private String message; + public UUID id; + public String message; }