Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get transaction history in Corda?

Tags:

corda

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.

like image 838
spartaneg Avatar asked May 29 '18 23:05

spartaneg


People also ask

What does a transaction in Corda represent?

Transactions on Corda A transaction updates the ledger by consuming existing input states and outputting new states. The states the transaction consumes are marked “historic”. Every state is immutable—it can't be changed. This is called an UTXO (unspent transaction output) model.

At what point does a transaction become immutable in Corda?

The transaction now has one or more digital signatures, making it immutable. This is the transaction type that is passed around to collect additional signatures and that is recorded on the ledger.

What does Corda contract verify?

In the context of a CorDapp, contracts define rules for verifying transaction inputs and outputs. A CorDapp can have more than one contract, and each contract defines the rules for one or more states.

How does Corda ensure each output state is only consumed once?

The answer is with the help of a “notary”. A notary is a trusted party (or parties working together) which guarantees that a particular state is only consumed once. Each state has a specific notary, which must sign any transaction in which that state is consumed.


2 Answers

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.

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.

Ways to retrieve transactions as of Corda 3

1. Using 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.")

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).

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) }

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. Using the ServiceHub

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.

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).

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. By connecting to the node's database

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

like image 64
Joel Avatar answered Oct 17 '22 17:10

Joel


That's right, although please note that the ServiceHub and SQL approaches are basically the same thing as the deprecated RPC and may also stop working in future (or not, depending on how we manage the transition to an encrypted ledger).

There are other approaches you can use. For instance you could aggregate the bits of history you care about up into the latest version of the state. This also lets you restrict the view of the history once SGX lands.

like image 34
Mike Hearn Avatar answered Oct 17 '22 17:10

Mike Hearn