如何通过RPC检查流的当前状态?及FlowLogic类recordAuditEvent方法疑问
Hey there! Let's tackle your two questions about Corda flows and RPC step by step:
There are two common scenarios here, depending on whether you're tracking a flow you initiated or querying any arbitrary flow on a node:
Scenario 1: Tracking a flow you started from the client
If you initiated the flow directly via your RPC client, you can use the FlowHandle or TrackedFlowHandle objects returned by rpc.startFlow() or rpc.startTrackedFlow() to monitor status in real-time:
- Use
flowHandle.isDone()to quickly check if the flow has completed - Subscribe to
trackedFlowHandle.progressto get step-by-step updates as the flow runs - Use
flowHandle.returnValue.getOrThrow()to wait for and retrieve the final result
Example code snippet:
// Connect to the node via RPC val rpc = CordaRPCClient(hostAndPort).start(username, password).proxy // Start a tracked flow and get its handle val trackedFlowHandle = rpc.startTrackedFlow(::MyCustomFlow, transactionArgs) // Subscribe to progress updates trackedFlowHandle.progress.subscribe { update -> println("Flow update: $update") } // Check if the flow is completed if (trackedFlowHandle.isDone) { println("Flow has finished!") } // Retrieve the final result val flowResult = trackedFlowHandle.returnValue.getOrThrow()
Scenario 2: Querying any flow's status on a node
If you need to check the status of a flow you didn't initiate (or don't have the original handle for), you'll need to expose a custom RPC endpoint on the node:
- Define the RPC interface with a method to fetch flow status by ID:
import net.corda.core.messaging.RPC import java.util.UUID interface FlowStatusRpcService { @RPC fun getFlowStatus(flowId: UUID): String? }
- Implement the service using the node's
FlowStateMachineManagerto look up flow state:
import net.corda.core.node.ServiceHub import net.corda.core.node.services.FlowStateMachineManager import java.util.UUID class FlowStatusRpcServiceImpl(private val serviceHub: ServiceHub) : FlowStatusRpcService { private val flowStateMachineManager: FlowStateMachineManager = serviceHub.getFlowStateMachineManager() override fun getFlowStatus(flowId: UUID): String? { val flowStateMachine = flowStateMachineManager.getStateMachineById(flowId) return flowStateMachine?.state?.status?.name // Returns status like "RUNNING", "SUCCEEDED", "FAILED" } }
- Register the service in your node's configuration (add it to the
serviceslist in your node's config file) - Call the custom RPC from your client to fetch the status by flow UUID
recordAuditEvent(...) Method in FlowLogic Let's break down what this method does, how to use it, and whether it fits your flow status check scenario:
What it is
recordAuditEvent() is a built-in method in FlowLogic designed to log audit trail events during flow execution. It's meant to track key actions (like flow initiation, transaction signing, or state updates) for compliance, debugging, or auditing purposes—not to query flow status.
How to use it
You can call it directly within your FlowLogic subclass, passing in an event type and optional metadata to contextualize the event:
override fun call(): YourResultType { // Log when the flow starts recordAuditEvent( eventType = "MY_FLOW_INITIATED", additionalParameters = mapOf( "flowId" to runId.uuid.toString(), "initiator" to ourIdentity.name.toString(), "counterparty" to counterparty.name.toString() ) ) // ... rest of your flow logic ... // Log when a key step completes recordAuditEvent( eventType = "TRANSACTION_SIGNED", additionalParameters = mapOf("txId" to signedTx.id.toString()) ) return result }
Is it suitable for checking flow status?
No, this method is not designed for querying flow status. It writes events to the node's audit logs, but it doesn't expose any way to retrieve current flow state. For status checks, stick to the RPC methods outlined in section 1 above.
内容的提问来源于stack exchange,提问作者Mauro

