Created
July 1, 2024 14:33
-
-
Save parisyup/8d2b6374b4ba7040039f1e7dddc1e7fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@InitiatingFlow(protocol = "utxo-transaction-transmission-protocol") | |
class sendAndRecieveTransactionFlow : ClientStartableFlow { | |
data class Request( | |
val stateRef: String, | |
val members: List<String>, | |
val forceBackchain: Boolean = false | |
) | |
data class Response(val transactionId: String) | |
@CordaInject | |
lateinit var flowMessaging: FlowMessaging | |
@CordaInject | |
lateinit var utxoLedgerService: UtxoLedgerService | |
@CordaInject | |
lateinit var jsonMarshallingService: JsonMarshallingService | |
@CordaInject | |
lateinit var memberLookup: MemberLookup | |
@CordaInject | |
lateinit var digestService: DigestService | |
private val log = LoggerFactory.getLogger(sendAndRecieveTransactionFlow::class.java) | |
@Suspendable | |
override fun call(requestBody: ClientRequestBody): String { | |
val request = requestBody.getRequestBodyAs(jsonMarshallingService, Request::class.java) | |
val transactionId = StateRef.parse(request.stateRef, digestService).transactionId | |
val transaction = requireNotNull(utxoLedgerService.findSignedTransaction(transactionId)) { | |
"Transaction is not found or verified." | |
} | |
val members = request.members.map { x500 -> | |
requireNotNull(memberLookup.lookup(MemberX500Name.parse(x500))) { | |
"Member $x500 does not exist in the membership group" | |
} | |
} | |
val sessions = members.map { flowMessaging.initiateFlow(it.name) } | |
try { | |
if (request.forceBackchain) { | |
utxoLedgerService.sendTransactionWithBackchain(transaction, sessions) | |
} else { | |
utxoLedgerService.sendTransaction(transaction, sessions) | |
} | |
} catch (e: Exception) { | |
log.warn("Sending transaction for $transactionId failed.", e) | |
throw e | |
} | |
return jsonMarshallingService.format(Response(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}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment