如何获得Corda的交易记录? [英] How to get transaction history in Corda?

查看:104
本文介绍了如何获得Corda的交易记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获得状态,我可以使用保险柜,但是交易呢?我如何通过txHash获得它们?是否可以通过CordaRPCOps做到这一点,有 internalVerifiedTransactionsSnapshot 方法,但是现在不推荐使用。

To get state I can use Vault, but what about transactions? How I can get them, for example, by txHash? Is it possible to do this by CordaRPCOps, there is internalVerifiedTransactionsSnapshot method, but it is deprecated now.

推荐答案

首先,请注意,从Corda 3开始,对于任何用于检索事务或其依赖关系的方法的行为,都没有稳定性保证。特别是,我们不能保证所检索的事务集在Corda版本之间不会改变。

First, note that as of Corda 3, there are no stability guarantees regarding the behaviour of any method to retrieve a transaction or its dependencies. In particular, we cannot guarantee that the set of transactions retrieved will not change across Corda versions.

这是因为在未来的Corda版本中,节点可能只会交换事务链以SGX加密形式。然后,将在节点上的SGX飞地内验证这些交易链。这将防止节点看到他们正在验证的事务的内容(请参见此处的博客文章: https://www.corda.net/2017/06/corda-sgx-privacy-update/ )。甚至可能只允许节点查看它们正在签名的交易的某些部分。

This is because in future versions of Corda, nodes will likely only exchange transaction chains in SGX-encrypted form. These transaction chains will then be verified inside an SGX enclave on the node. This will prevent nodes from seeing the contents of the transactions they are verifying (see the blogpost here: https://www.corda.net/2017/06/corda-sgx-privacy-update/). This may even go so far as to only allow nodes to see certain parts of the transactions they are signing.

从Corda 3开始检索交易的方式

1。使用 CordaRPCOps.internalVerifiedTransactionsSnapshot

1. Using CordaRPCOps.internalVerifiedTransactionsSnapshot

如果通过RPC与节点进行交互,则 CordaRPCOps.internalVerifiedTransactionsSnapshot 返回所有已记录交易的列表。

If you are interacting with the node via RPC, CordaRPCOps.internalVerifiedTransactionsSnapshot returns a list of all recorded transactions.

如果您只想获得单个交易并且知道其哈希值,您可以这样写:

If you only wanted to get a single transaction and you knew its hash, you could write:

val transactions = cordaRPCOps.internalVerifiedTransactionsSnapshot()
val signedTransaction = transactions
    .find { it.id == transactionHash }
    ?: throw IllegalArgumentException("Unknown transaction hash.")

请注意,返回的交易类型为 SignedTransaction 。此表单不包含事务的附件或输入(仅包含附件哈希和输入状态引用)。

Note that the transactions returned are of type SignedTransaction. This form does not contain the transaction's attachments or inputs (only the attachment hashes and input state references).

要通过RPC检索事务的附件,可以编写:

To retrieve a transaction's attachments via RPC, you could write:

val transactions = cordaRPCOps.internalVerifiedTransactionsSnapshot()
val signedTransaction = transactions
        .find { it.id == transactionHash }
        ?: throw IllegalArgumentException("Unknown transaction hash.")

val attachmentHashes = signedTransaction.tx.attachments
val attachmentStreams = attachmentHashes.map { hash -> cordaRPCOps.openAttachment(hash) }

要通过RPC检索事务的输入,您可以编写: / p>

And to retrieve a transaction's inputs via RPC, you could write:

val transactions = cordaRPCOps.internalVerifiedTransactionsSnapshot()
val signedTransaction = transactions
        .find { it.id == transactionHash }
        ?: throw IllegalArgumentException("Unknown transaction hash.")

val inputStateRefs = signedTransaction.inputs
val inputStates = inputStateRefs.map { stateRef ->
    val transaction = transactions.find { it.id == stateRef.txhash }
            ?: throw IllegalArgumentException("Unknown transaction hash.")
    transaction.tx.outputStates[stateRef.index]
}

2。使用 ServiceHub

2. Using the ServiceHub

如果您可以访问节点的 ServiceHub (例如在流或Corda服务中),您可以使用 serviceHub.validatedTransactions.track()。snapshot 来获取所有交易,并 serviceHub.validatedTransactions.getTransaction(transactionHash)通过哈希获取特定交易。

If you are in a situation where you have access to the node's ServiceHub (e.g. within a flow or a Corda service), you can use serviceHub.validatedTransactions.track().snapshot to get all transactions, and serviceHub.validatedTransactions.getTransaction(transactionHash) to get a specific transaction by hash.

返回的交易类型为 SignedTransaction 。此表单不包含事务的附件或输入(仅包含附件哈希和输入状态引用)。

Note that the transactions returned are of type SignedTransaction. This form does not contain the transaction's attachments or inputs (only the attachment hashes and input state references).

要转换 SignedTransaction LedgerTransaction (解决附件和输入的地方)中,您可以这样写:

To convert the SignedTransaction to a LedgerTransaction (where the attachments and inputs are resolved), you could write:

val signedTransaction = serviceHub.validatedTransactions.getTransaction(transactionHash)
val ledgerTransaction = signedTransaction.toLedgerTransaction(serviceHub)

3。通过连接到节点的数据库

您可以直接连接到支持该节点的SQL数据库,并使用SQL查询来检索事务。

You can connect directly to the SQL database backing the node, and retrieve the transactions using an SQL query.

这篇关于如何获得Corda的交易记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆