From 312dce5780211cbf978145416f3cb49b3c4a6aed Mon Sep 17 00:00:00 2001 From: Faris Alblooki <66366646+parisyup@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:02:17 +0400 Subject: [PATCH] clean up sendAndRecieveTransaction --- .../sendAndRecieveTransaction/README.md | 8 ++-- .../workflows/ReceiveTransactionFlow.java | 1 + .../workflows/RecieveTransactionFlowArgs.java | 18 -------- .../workflows/sendAndReceiveTransaction.java | 17 +++++-- .../sendAndRecieveTransaction/README.md | 6 ++- .../obligation/workflows/FinalizeIOUFlow.kt | 2 - .../workflows/IssueSanctionsListFlow.kt | 4 -- .../sendAndRecieveTransactionFlow.kt | 44 ++++++++++++------- 8 files changed, 50 insertions(+), 50 deletions(-) delete mode 100644 java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/RecieveTransactionFlowArgs.java delete mode 100644 kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/IssueSanctionsListFlow.kt diff --git a/java-samples/sendAndRecieveTransaction/README.md b/java-samples/sendAndRecieveTransaction/README.md index 2c051ec..c2cf41d 100644 --- a/java-samples/sendAndRecieveTransaction/README.md +++ b/java-samples/sendAndRecieveTransaction/README.md @@ -43,7 +43,9 @@ Go to `POST /flow/{holdingidentityshorthash}`, enter the identity short hash(Ali } } ``` -The stateRef of the transaction will be returned as a result of the flow. + +After trigger the create-IOU flow, hop to `GET /flow/{holdingidentityshorthash}/{clientrequestid}` and enter the short hash(Alice's hash) and client request id to view the flow result +The stateRef of the transaction will be returned as a result of the flow query. Which is the "flowResult" #### Step 2: Sending a copy of the transaction to a third party. If a member needs to share a copy of their transaction with another member, @@ -54,9 +56,9 @@ we can execute the following request body with her short hash: ``` { "clientRequestId": "sendAndRecieve-1", - "flowClassName": "com.r3.developers.samples.obligation.workflows.sendAndReceiveTransaction", + "flowClassName": "com.r3.developers.samples.obligation.workflows.sendAndRecieveTransactionFlow", "requestBody": { - "stateRef": "[STATEREF ID HERE]", + "stateRef": "STATEREF ID HERE", "members": ["CN=Dave, OU=Test Dept, O=R3, L=London, C=GB"], "forceBackchain": "false" } diff --git a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/ReceiveTransactionFlow.java b/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/ReceiveTransactionFlow.java index 0544ce8..fbf7303 100644 --- a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/ReceiveTransactionFlow.java +++ b/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/ReceiveTransactionFlow.java @@ -19,6 +19,7 @@ public class ReceiveTransactionFlow implements ResponderFlow { @Suspendable @Override public void call(FlowSession session) { + // Receive the transaction and log its details. var transaction = utxoLedgerService.receiveTransaction(session); log.info("Received transaction - " + transaction.getId()); } diff --git a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/RecieveTransactionFlowArgs.java b/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/RecieveTransactionFlowArgs.java deleted file mode 100644 index 35d9e73..0000000 --- a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/RecieveTransactionFlowArgs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.r3.developers.samples.obligation.workflows; - -public class RecieveTransactionFlowArgs { - - private String transactionId; - - public RecieveTransactionFlowArgs(){} - public RecieveTransactionFlowArgs(String transactionId) { - this.transactionId = transactionId; - } - - public String getTransactionId() { - return transactionId; - } -} - - - diff --git a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/sendAndReceiveTransaction.java b/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/sendAndReceiveTransaction.java index eaca99e..fc76ecb 100644 --- a/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/sendAndReceiveTransaction.java +++ b/java-samples/sendAndRecieveTransaction/workflows/src/main/java/com/r3/developers/samples/obligation/workflows/sendAndReceiveTransaction.java @@ -41,19 +41,26 @@ public class sendAndReceiveTransaction implements ClientStartableFlow { @Override public String call(ClientRequestBody requestBody) { sendAndRecieveTransactionArgs request = requestBody.getRequestBodyAs(jsonMarshallingService, sendAndRecieveTransactionArgs.class); - SecureHash transactionId = StateRef.parse(request.getStateRef(), digestService).getTransactionId(); + + // Parse the state reference to obtain the transaction ID. + SecureHash transactionId = StateRef.parse(request.getStateRef() + ":0", digestService).getTransactionId(); + + // Retrieve the signed transaction from the ledger. var transaction = requireNotNull(utxoLedgerService.findSignedTransaction(transactionId), "Transaction is not found or verified."); + // Map the X500 names in the request to Member objects, ensuring each member exists. var members = request.getMembers().stream() .map(x500 -> requireNotNull(memberLookup.lookup(MemberX500Name.parse(x500)), "Member " + x500 + " does not exist in the membership group")) .collect(Collectors.toList()); + // Initialize the sessions with the memebers that will be used to send the transaction. var sessions = members.stream() .map(member -> flowMessaging.initiateFlow(member.getName())) .collect(Collectors.toList()); + // Send the transaction with or without backchain depending on the request. try { if (request.isForceBackchain()) { utxoLedgerService.sendTransactionWithBackchain(transaction, sessions); @@ -61,11 +68,13 @@ public String call(ClientRequestBody requestBody) { utxoLedgerService.sendTransaction(transaction, sessions); } } catch (Exception e) { + // Log and rethrow any exceptions encountered during transaction sending. log.warn("Sending transaction for " + transactionId + " failed.", e); throw e; } - String response = jsonMarshallingService.format(new RecieveTransactionFlowArgs(transactionId.toString())); + // Format and log the successful transaction response. + String response = jsonMarshallingService.format(transactionId.toString()); log.info("SendTransaction is successful. Response: " + response); return response; } @@ -79,10 +88,10 @@ private T requireNotNull(T obj, String message) { /* RequestBody for triggering the flow via http-rpc: { - "clientRequestId": "sendAndRecieve-2", + "clientRequestId": "sendAndRecieve-1", "flowClassName": "com.r3.developers.samples.obligation.workflows.sendAndReceiveTransaction", "requestBody": { - "stateRef": "SHA-256D:8DFDD6723450146F64CE22D6B39D4127652ABF45066E0BDD7C63BC18998EBD9A:0", + "stateRef": "STATE REF ID HERE", "members": ["CN=Charlie, OU=Test Dept, O=R3, L=London, C=GB"], "forceBackchain": "false" } diff --git a/kotlin-samples/sendAndRecieveTransaction/README.md b/kotlin-samples/sendAndRecieveTransaction/README.md index 04f71f2..a6c1cea 100644 --- a/kotlin-samples/sendAndRecieveTransaction/README.md +++ b/kotlin-samples/sendAndRecieveTransaction/README.md @@ -43,7 +43,9 @@ Go to `POST /flow/{holdingidentityshorthash}`, enter the identity short hash(Ali } } ``` -The stateRef of the transaction will be returned as a result of the flow. + +After trigger the create-IOU flow, hop to `GET /flow/{holdingidentityshorthash}/{clientrequestid}` and enter the short hash(Alice's hash) and client request id to view the flow result +The stateRef of the transaction will be returned as a result of the flow query. Which is the "flowResult" #### Step 2: Sending a copy of the transaction to a third party. If a member needs to share a copy of their transaction with another member, @@ -56,7 +58,7 @@ we can execute the following request body with her short hash: "clientRequestId": "sendAndRecieve-1", "flowClassName": "com.r3.developers.samples.obligation.workflows.sendAndRecieveTransactionFlow", "requestBody": { - "stateRef": "[STATEREF ID HERE]", + "stateRef": "STATEREF ID HERE", "members": ["CN=Dave, OU=Test Dept, O=R3, L=London, C=GB"], "forceBackchain": "false" } diff --git a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/FinalizeIOUFlow.kt b/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/FinalizeIOUFlow.kt index 0dd5e54..9c09db4 100644 --- a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/FinalizeIOUFlow.kt +++ b/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/FinalizeIOUFlow.kt @@ -89,8 +89,6 @@ class FinalizeIOUResponderFlow: ResponderFlow { log.info("Verified the transaction- ${ledgerTransaction.id}") } log.info("Finished responder flow - ${finalizedSignedTransaction.transaction.id}") - log.warn("HEY OVER HERE!") - log.warn(finalizedSignedTransaction.transaction.outputStateAndRefs.map { it.ref.toString() }.toString()) } // Soft fails the flow and log the exception. catch (e: Exception) { diff --git a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/IssueSanctionsListFlow.kt b/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/IssueSanctionsListFlow.kt deleted file mode 100644 index 042e7e8..0000000 --- a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/IssueSanctionsListFlow.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.r3.developers.samples.obligation.workflows - -class IssueSanctionsListFlow { -} \ No newline at end of file diff --git a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/sendAndRecieveTransactionFlow.kt b/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/sendAndRecieveTransactionFlow.kt index 4219f5c..3c6e4d2 100644 --- a/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/sendAndRecieveTransactionFlow.kt +++ b/kotlin-samples/sendAndRecieveTransaction/workflows/src/main/kotlin/com/r3/developers/samples/obligation/workflows/sendAndRecieveTransactionFlow.kt @@ -20,8 +20,6 @@ class sendAndRecieveTransactionFlow : ClientStartableFlow { val forceBackchain: Boolean = false ) - data class Response(val transactionId: String) - @CordaInject lateinit var flowMessaging: FlowMessaging @@ -42,18 +40,26 @@ class sendAndRecieveTransactionFlow : ClientStartableFlow { @Suspendable override fun call(requestBody: ClientRequestBody): String { val request = requestBody.getRequestBodyAs(jsonMarshallingService, Request::class.java) - val transactionId = StateRef.parse(request.stateRef, digestService).transactionId + + // Parse the state reference to obtain the transaction ID. + val transactionId = StateRef.parse(request.stateRef + ":0", digestService).transactionId + + // Retrieve the signed transaction from the ledger. val transaction = requireNotNull(utxoLedgerService.findSignedTransaction(transactionId)) { "Transaction is not found or verified." } + // Map the X500 names in the request to Member objects, ensuring each member exists. val members = request.members.map { x500 -> requireNotNull(memberLookup.lookup(MemberX500Name.parse(x500))) { "Member $x500 does not exist in the membership group" } } + + // Initialize the sessions with the memebers that will be used to send the transaction. val sessions = members.map { flowMessaging.initiateFlow(it.name) } + // Send the transaction with or without backchain depending on the request. try { if (request.forceBackchain) { utxoLedgerService.sendTransactionWithBackchain(transaction, sessions) @@ -61,37 +67,41 @@ class sendAndRecieveTransactionFlow : ClientStartableFlow { utxoLedgerService.sendTransaction(transaction, sessions) } } catch (e: Exception) { + // Log and rethrow any exceptions encountered during transaction sending. log.warn("Sending transaction for $transactionId failed.", e) throw e } - return jsonMarshallingService.format(Response(transactionId.toString())).also { + // Format and log the successful transaction response. + return jsonMarshallingService.format(transactionId.toString()).also { log.info("SendTransaction is successful. Response: $it") } } -} -@InitiatedBy(protocol = "utxo-transaction-transmission-protocol") -class ReceiveTransactionFlow: ResponderFlow { - private val log = LoggerFactory.getLogger(ReceiveTransactionFlow::class.java) - @CordaInject - lateinit var utxoLedgerService: UtxoLedgerService - @Suspendable - override fun call(session: FlowSession) { - val transaction = utxoLedgerService.receiveTransaction(session) - log.info("Received transaction - ${transaction.id}") + @InitiatedBy(protocol = "utxo-transaction-transmission-protocol") + class ReceiveTransactionFlow : ResponderFlow { + private val log = LoggerFactory.getLogger(ReceiveTransactionFlow::class.java) + + @CordaInject + lateinit var utxoLedgerService: UtxoLedgerService + + @Suspendable + override fun call(session: FlowSession) { + // Receive the transaction and log its details. + val transaction = utxoLedgerService.receiveTransaction(session) + log.info("Received transaction - ${transaction.id}") + } } } - /* RequestBody for triggering the flow via http-rpc: { - "clientRequestId": "sendAndRecieve-2", + "clientRequestId": "sendAndRecieve-1", "flowClassName": "com.r3.developers.samples.obligation.workflows.sendAndRecieveTransactionFlow", "requestBody": { - "stateRef": "SHA-256D:01EE53398B06F1E59C8064564E49EA31C1E396ECF130D6158E71FE60A26148D2:0", + "stateRef": "STATE REF ID HERE", "members": ["CN=Charlie, OU=Test Dept, O=R3, L=London, C=GB"], "forceBackchain": "false"