Between its creation and its final inclusion on the ledger, a transaction will generally occupy one of three states:
TransactionBuilder. A transaction’s initial state. This is the only state during which the transaction is mutable, so we must add all the required components before moving on.
SignedTransaction. 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.
LedgerTransaction. The transaction has been “resolved” - for example, its inputs have been converted from references to actual states - allowing the transaction to be fully inspected.
We can visualise the transitions between the three stages as follows:
A transaction consists of six types of components:
- 0+ input states
- 0+ output states
- 0+ reference input states
0 or 1 time-window
- A transaction with a time-window must also have a notary
Each component corresponds to a specific class in the Corda API. The following section describes each component class, and how it is created.
An input state is added to a transaction as a
StateAndRef, which combines:
ContractStateas the output of a specific transaction
StateRef uniquely identifies an input state, allowing the notary to mark it as historic. It is made up of:
- The hash of the transaction that generated the state
- The state’s index in the outputs of that transaction
StateRef links an input state back to the transaction that created it. This means that transactions form
“chains” linking each input back to an original issuance transaction. This allows nodes verifying the transaction
to “walk the chain” and verify that each input was generated through a valid sequence of transactions.
Reference input states
A reference input state is added to a transaction as a
ReferencedStateAndRef can be
obtained from a
StateAndRef by calling the
StateAndRef.referenced() method which returns a
Handling of update races:
When using reference states in a transaction, it may be the case that a notarisation failure occurs. This is most likely because the creator of the state (being used as a reference state in your transaction), has just updated it.
Typically, the creator of such reference data will have implemented flows for syndicating the updates out to users. However it is inevitable that there will be a delay between the state being used as a reference being consumed, and the nodes using it receiving the update.
This is where the
WithReferencedStatesFlow comes in. Given a flow which uses reference states, the
WithReferencedStatesFlow will execute the the flow as a subFlow. If the flow fails due to a
for a reference state, then it will be suspended until the state refs for the reference states are consumed. In this
case, a consumption means that:
- the owner of the reference state has updated the state with a valid, notarised transaction
- the owner of the reference state has shared the update with the node attempting to run the flow which uses the reference state
- The node has successfully committed the transaction updating the reference state (and all the dependencies), and added the updated reference state to the vault.
At the point where the transaction updating the state being used as a reference is committed to storage and the vault
update occurs, then the
WithReferencedStatesFlow will wake up and re-execute the provided flow.
Since a transaction’s output states do not exist until the transaction is committed, they cannot be referenced as the
outputs of previous transactions. Instead, we create the desired output states as
ContractState instances, and
add them to the transaction directly:
In cases where an output state represents an update of an input state, we may want to create the output state by basing it on the input state:
Before our output state can be added to a transaction, we need to associate it with a contract. We can do this by
wrapping the output state in a
StateAndContract, which combines:
ContractStaterepresenting the output states
Stringidentifying the contract governing the state
A command is added to the transaction as a
Command, which combines:
CommandDatainstance indicating the command’s type
List<PublicKey>representing the command’s required signers
Attachments are identified by their hash:
The attachment with the corresponding hash must have been uploaded ahead of time via the node’s RPC interface.
Time windows represent the period during which the transaction must be notarised. They can have a start and an end time, or be open at either end:
We can also define a time window as an
Instant plus/minus a time tolerance (e.g. 30 seconds):
Or as a start-time plus a duration:
Creating a builder
The first step when creating a transaction proposal is to instantiate a
If the transaction has input states or a time-window, we need to instantiate the builder with a reference to the notary that will notarise the inputs and verify the time-window:
We discuss the selection of a notary in Writing CorDapp Flows.
If the transaction does not have any input states or a time-window, it does not require a notary, and can be instantiated without one:
The next step is to build up the transaction proposal by adding the desired components.
We can add components to the builder using the
withItems takes a
vararg of objects and adds them to the builder based on their type:
StateAndRefobjects are added as input states
ReferencedStateAndRefobjects are added as reference input states
StateAndContractobjects are added as output states
StateAndContractare wrappers around a
ContractStateoutput that link the output to a specific contract
Commandobjects are added as commands
SecureHashobjects are added as attachments
TimeWindowobject replaces the transaction’s existing
TimeWindow, if any
Passing in objects of any other type will cause an
IllegalArgumentException to be thrown.
Here’s an example usage of
There are also individual methods for adding components.
Here are the methods for adding inputs and attachments:
An output state can be added as a
ContractState, contract class name and notary:
We can also leave the notary field blank, in which case the transaction’s default notary is used:
Or we can add the output state as a
TransactionState, which already specifies the output’s contract and notary:
Commands can be added as a
CommandData and a
For the time-window, we can set a time-window directly:
Or define the time-window as a time plus a duration (e.g. 45 seconds):
Signing the builder
Once the builder is ready, we finalize it by signing it and converting it into a
We can either sign with our legal identity key:
Or we can also choose to use another one of our public keys:
Either way, the outcome of this process is to create an immutable
SignedTransaction with our signature over it.
SignedTransaction is a combination of:
- An immutable transaction
- A list of signatures over that transaction
Before adding our signature to the transaction, we’ll want to verify both the transaction’s contents and the transaction’s signatures.
Verifying the transaction’s contents
If a transaction has inputs, we need to retrieve all the states in the transaction’s dependency chain before we can
verify the transaction’s contents. This is because the transaction is only valid if its dependency chain is also valid.
We do this by requesting any states in the chain that our node doesn’t currently have in its local storage from the
proposer(s) of the transaction. This process is handled by a built-in flow called
Writing CorDapp Flows for more details.
We can now verify the transaction’s contents to ensure that it satisfies the contracts of all the transaction’s input and output states:
Checking that the transaction meets the contract constraints is only part of verifying the transaction’s contents. We will usually also want to perform our own additional validation of the transaction contents before signing, to ensure that the transaction proposal represents an agreement we wish to enter into.
SignedTransaction holds its inputs as
StateRef instances, and its attachments as
instances, which do not provide enough information to properly validate the transaction’s contents. We first need to
SecureHash instances into actual
Attachment instances, which
we can then inspect.
We achieve this by using the
ServiceHub to convert the
SignedTransaction into a
We can now perform our additional verification. Here’s a simple example:
Verifying the transaction’s signatures
Aside from verifying that the transaction’s contents are valid, we also need to check that the signatures are valid. A valid signature over the hash of the transaction prevents tampering.
We can verify that all the transaction’s required signatures are present and valid as follows:
However, we’ll often want to verify the transaction’s existing signatures before all of them have been collected. For
this we can use
SignedTransaction.verifySignaturesExcept, which takes a
vararg of the public keys for
which the signatures are allowed to be missing:
There is also an overload of
SignedTransaction.verifySignaturesExcept, which takes a
Collection of the
public keys for which the signatures are allowed to be missing:
If the transaction is missing any signatures without the corresponding public keys being passed in, a
SignaturesMissingException is thrown.
We can also choose to simply verify the signatures that are present:
Be very careful, however - this function neither guarantees that the signatures that are present are required, nor checks whether any signatures are missing.
Signing the transaction
Once we are satisfied with the contents and existing signatures over the transaction, we add our signature to the
SignedTransaction to indicate that we approve the transaction.
We can sign using our legal identity key, as follows:
Or we can choose to sign using another one of our public keys:
We can also generate a signature over the transaction without adding it to the transaction directly.
We can do this with our legal identity key:
Or using another one of our public keys:
Notarising and recording
Notarising and recording a transaction is handled by a built-in flow called
Writing CorDapp Flows for