You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过RPC检查流的当前状态?及FlowLogic类recordAuditEvent方法疑问

Hey there! Let's tackle your two questions about Corda flows and RPC step by step:

1. How to Check a Flow's Current Status via RPC

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.progress to 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:

  1. 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?
}
  1. Implement the service using the node's FlowStateMachineManager to 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"
    }
}
  1. Register the service in your node's configuration (add it to the services list in your node's config file)
  2. Call the custom RPC from your client to fetch the status by flow UUID

2. Understanding the 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:17:34