Skip to content

Commit

Permalink
clean up sendAndRecieveTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
parisyup committed Jul 4, 2024
1 parent 2dd0f18 commit 312dce5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 50 deletions.
8 changes: 5 additions & 3 deletions java-samples/sendAndRecieveTransaction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,40 @@ 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);
} else {
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;
}
Expand All @@ -79,10 +88,10 @@ private <T> 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"
}
Expand Down
6 changes: 4 additions & 2 deletions kotlin-samples/sendAndRecieveTransaction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class sendAndRecieveTransactionFlow : ClientStartableFlow {
val forceBackchain: Boolean = false
)

data class Response(val transactionId: String)

@CordaInject
lateinit var flowMessaging: FlowMessaging

Expand All @@ -42,56 +40,68 @@ 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)
} else {
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"
Expand Down

0 comments on commit 312dce5

Please sign in to comment.