Overview
At the core is the concept of a valid ledger: a change is permissible if adding the corresponding commit to the ledger results in a valid ledger. Valid ledgers are those that fulfill three conditions, which are introduced formally below:- Consistency: A consistent Ledger does not allow exercises and fetches on inactive contracts; that is, they cannot act on contracts that have not yet been created or that have already been consumed by an exercise.
- Conformance: A conformant Ledger contains only actions that are allowed by the smart contract logic of the created or used contract. In Daml, templates define this smart contract logic.
- Authorization: In a well-authorized Ledger, the requesters of a change encompass the required authorizers as defined via the controllers and signatories.
Consistency violation example
In this example, Alice tries to transfer her asset twice (“double spend”): once to Bob and once to Charlie, as shown in the following Daml script excerpt. This script is expected to fail at runtime, because it violates consistency.TX 1 and one in TX 2.
Conformance violation example
In the example below, the last transactionTX 4 omits one leg of the DvP workflow:
Bob exercises the Settle choice, but it has only one subaction, namely Alice transferring her IOU.
This violates conformance because the Settle choice body of a SimpleDvP specifies via the two exercise calls that there are always two consequences.
(This situation cannot be expressed as a Daml script scenario
because Daml script ensures that all generated transactions conform to the Daml code.)
Authorization violation examples
Next, we give three examples that show different kinds of authorization violations.Unauthorized transfer
First, Alice attempts to steal Bob’s asset by requesting a transfer in his name. This results in an authorization failure because forTX 1 the actor of the exercise root action differs from the requester.
Skip the propose-accept workflow
Next, Bob wants to skip the propose-accept workflow for creating theSimpleDvP contract and instead creates it out of nowhere and immediately settles it.
This must be treated as an authorization failure, as Alice did not consent to swapping her EUR asset against Bob’s USD asset.
TX 2 is not properly authorized
because Alice is a signatory of the contract #3 created in the first root action even though she did not request the update.
Allocate someone else’s asset
The final example shows that authorization failures may not only happen at root actions. Here, Alice allocates Carol’s CHF asset in the DvP proposal. When Bob tries to settle the DvP, the Exercise to transfer Carol’s asset in the first leg is not properly authorized because Carol did not agree to have her asset transferred away.Transfer choice on her asset.
Interaction with projection
Apart from introducing the validity notion, this page also discusses how validity interacts with privacy, which is defined via projection. To that end, the sections on the different validity conditions analyse the prerequisites under what the following two properties hold:- Preservation: If the Ledger adheres to a condition, then so do the projections. This property ensures that a valid Ledger does not appear as invalid to individual parties, just because they are not privy to all actions on the Ledger.
- Reflection: If the projections adhere to a condition, then so does the Ledger from which these projections were obtained. This property ensures that the condition can be implemented by the distributed Canton protocol where nobody sees the Ledger as a whole.
Consistency
Consistency can be summarized in one sentence: Contracts must be created before they are used, and they cannot be used after they are consumed. This section introduces the notions that are needed to make this precise:- The execution order defines the notions of “before” and “after”.
- Internal consistency ensures that all the operations on a contract happen in the expected order of creation, usage, archival, but does not require that all contracts are created; they may be merely referenced as inputs.
- (Contract) Consistency strengthens internal consistency in that all used contracts must also have been created.
Execution order
The meaning of “before” and “after” is given by establishing an execution order on the nodes of a ledger. The ledger’s graph structure already defines a happens-before order on ledger commits. The execution order extends this happens-before order to all the nodes within the commits’ transactions so that “before” and “after” are also defined for the nodes of a single transaction. This is necessary because a contract can be created and used multiple times within a transaction. In theAcceptAndSettle action of the DvP example, for example,
contract #3 is used twice (once in the non-consuming exercise at the root and once consumingly in the first consequence)
and contract #4 is created and consumed in the same action.
Definition: execution orderFor two distinct nodes n1 and n2 within the same action or transaction, n1 executes before n2
if n1 appears before n2 in the preorder traversal of the (trans)action, noting that the transaction is an ordered forest.
For a ledger, every node in commit c1 executes before every node in commit c2
if the commit c1 happens before c2.
Internal consistency
Internal consistency ensures that if several nodes act on a contract within an action, transaction, or ledger, then those nodes execute in an appropriate order, namely creation, usage, archival. Internal contract consistency does not require Create nodes for all contracts that are used. This way, internal contract consistency is meaningful for pieces of a ledger such as individual transactions or actions, which may use as inputs the contracts created outside of the piece.Definition: internal consistencyAn action, transaction, or ledger is internally consistent for a contract c
if for any two distinct nodes n1 and n2 on c in the action, transaction, or ledger,
all of the following hold:
- If n1 is a Create node, n1 executes before n2.
- If n2 is a consuming Exercise node, then n1 executes before n2.
Internal consistency constrains the order of the commits in a Ledger via the execution order.
In the running DvP example,
TX 0, TX 1, and TX 2 all create contracts that TX 3 uses.
Internal consistency therefore demands that these create nodes execute before the usage nodes in TX 3.
So by the definition of the execution order, TX 0, TX 1, and TX 2 all must happen before TX 3
(although internal consistency does not impose any particular order among TX 0, TX 1, and TX 2).Definition
Consistency strengthens internal consistency in that used contracts actually have been created within the action, transaction, or ledger.Definition: consistencyAn action, transaction, or ledger is consistent for a contract if all of the following hold:
- It is internally consistent for the contract.
- If a node uses the contract, then there is also a node that creates the contract.
TX 0 and thus contains only commits TX 1 to TX 3, it is still internally consistent, but not consistent,
because TX 3 uses the contract #1, but there is no create node for #1 in TX 1 to TX 3.
Consistency and projection
This section looks at the conditions under which projections preserve and reflect (internal) consistency.Projections preserve consistency for stakeholders
For preservation, projections retain the execution order and preserve internal consistency. Yet, consistency itself is preserved in general only for contract stakeholders. For example, Alice’s projection of the DvP workflow is not consistent because it lacksTX 1 and therefore the creation of contract #2 used in TX 3.
Fortunately, consistency behaves well under projections if we look only at contracts the parties are stakeholders of.
In detail, if an action, transaction, or ledger is (internally) consistent for a set of contracts C
and P is a set of parties such that every contract in C has at least one stakeholder in P,
then the projection to P is also (internally) consistent for C.
To see this, note that the execution order of the projection of an action or transaction to P
is the restriction of the execution order of the unprojected action or transaction to the projection.
That is, if n1 and n2 are two nodes in the projection,
then n1 executes before n2 in the projection if and only if
n1 executes before n2 in the original (trans)action.
Accordingly, projections preserve internal consistency of an action or transaction too.
Moreover, the projection to P never removes a Create node if one of the stakeholders is in P.
Therefore, consistency is preserved too.
For ledgers, the same argument applies with the current simplification of totally ordered ledgers.
The causality section relaxes the ordering requirement, but makes sure
that projections continue to preserve (internal) consistency for the parties’ contracts.
Signatories check consistency on projections
From Canton’s perspective, the reflection property is at least as important: If the projection of a (trans)action or ledger to a set of parties P is (internally) consistent for a set of contracts C where each contract has at least one signatory in P, then so is the (trans)action or ledger itself. This statement can be shown with a similar argument. Importantly, reflection requires a signatory of the contracts in P, not just a stakeholder. The following example shows that the propery does not hold if P contains a stakeholder, but no signatory. To that end, we extend theSimpleAsset template with a non-consuming Present choice
so that the issuer and owner can show the asset to a choice observer viewer:
Present command,
and the Synchronizer happens to order Alice’s submission first.
Conformance
The conformance condition constrains the actions that may occur on the ledger. The definitions in this section assume a given contract model (or a model for short) that specifies the set of all possible actions. In practice, Daml templates define such a model as follows:- Choices declare the controller and the choice observers and constrain via their body the valid values in the exercised contract and choice arguments. Their body defines the subactions (by creating, fetching or exercising contracts) and the Exercise result.
-
The
ensureclause on the template constrains the valid arguments of a Create node.
Definition: conformanceAn action conforms to a model if the model contains it.
A transaction conforms to a model if all the actions of the transaction conform to the model.
A ledger conforms to a model if all top-level transactions of the ledger conform to the model.
SimpleDvP.Settle exercises Transfer on two contracts and therefore requires that there are two subactions.
The action on the ledger however has only one of the two subactions and therefore violates conformance.
This example demonstrates why the contract model specifies actions instead of nodes:
a set of acceptable nodes cannot catch when a consequence is missing from an action,
because nodes ignore the tree structure.
Conformance and projection
Like consistency, conformance to a Daml model behaves well under projections. If an action, transaction or ledger conforms to a Daml model, then all their projections also conform to the same Daml model. In fact, Daml models enjoy the stronger property that every subaction of a Daml-conformant action conforms itself. This essentially follows from two observations:- The controllers of any choice may jointly exercise it on any contract, and the signatories of a contract may jointly create the contract, without going through some predefined workflow. So contract creations and choices are essentially public.
- The Daml language is referentially transparent. That is, all inputs and outputs of a transaction are explicitly captured in contracts, choice arguments and exercise results.
AcceptAndSettle choice for Bank 2.
Yet, there is no Ledger API command to fetch a contract, as there are only commands for creating and exercising contracts.
Second, the Ledger API command language does not support feeding the result of an Exercise as an argument to a subsequent command.
For example, suppose that the AcceptAndSettle choice of ProposeSimpleDvP was actually implemented on a helper template AcceptAndSettleDvP as shown below.
Execute choice.
TX 3 consists of two root actions, where the second exercises a choice on a contract created in a consequence of the first.
Authorization
The last validity condition ensures that only the indended parties can request a change, and thereby rules out the authorization violation examples. Authorization requirements are expressed in Daml using the signatories and observers of a contract and the controllers of choices. This section introduces the notions to formalize this:- Required authorizers define the set of parties who must have consented to an action.
- The authorization context captures the parties who have actually authorized an action.
- Well-authorization demands that the authorization context includes all the required authorizers.
Required authorizers
Every node defines a non-empty set of parties who must have consented to the action of this node. This set is called the required authorizers of the node and defined as follows: For Create nodes, the required authorizers are the signatories of the contract, and for Exercise and Fetch nodes, the required authorizers are the actors of the node. For the running example where Bob skips the propose-accept workflow, the following table lists for each party the nodes for which they are a required authorizer. For example, node ③ has the required authorizers Alice and Bob because they are the signatories of contract #3. Required authorizers in the example where Bob tries to skip the propose-accept workflowAuthorization context
In a Canton ledger, a party can authorize a subaction of a commit in two ways:- The requesters of a commit authorize every top-level action of the commit.
- For an Exercise action, the signatories of the input contract and the actors of the action jointly authorize every consequence of the action.
- Bank1 because Bank1 is the signatory of the input contract of the parent node ⑤.
- Alice because Alice is the actor on the parent node ⑤.
Well-authorization
Well-authorization ensures that the authorizing parties and the required authorizers fit together.Definition: Well-authorizationAn action is internally well-authorized if for every proper subaction, the authorization context contains all the required authorizers of the subaction.An action is well-authorized if it is internally well-authorized and the authorization context of the action contains all the required authorizers of the action.A commit is well-authorized if every root action is well-authorized.
TX 0 is well-authorized because it contains only one subaction ①
and the required authorizer Bank1 is also the requester of the commit.
Conversely, the commit TX 2 is not well-authorized because ③’s required authorizers include Alice who is not in ③’s authorization context.
This authorization failure captures the problem with this commit TX 2:
The Ledger does not contain any record of Alice consenting to the DvP.
In contrast, the Exercise action at ④ is well-authorized.
This illustrates how authorization flows from the signatories of a contract to the consequences of the choices.
Assuming that the signatories Alice and Bob entered the SimpleDvP contract #3,
the authorization rules allow Bob, the one controller of the Settle choice, to swap the two assets
even though Bob does not own one of the assets (#1).
In other words, Alice delegates via the SimpleDvp contract #3 to Bob the right to transfer her asset #1.
A similar flow of authorization also happens in the propose-accept workflow for the SimpleDvP contract in the correct workflow:
In TX 2, Alice proposes the ProposeSimpleDvP contract #3 as a signatory.
When Bob accepts the proposal with the Accept choice,
Alice’s authority flows to the creation of the SimpleDvP contract #4,
where both Alice and Bob are signatories.
Well-authorization with projection
The example of the wrongly allocated asset illustrates the difference between well-authorization and internal well-authorization. The action rooted at node ⑨ is internally well-authorized because it has only one proper subaction with node ⑩ whose authorization context includes the required authorizer Bank1. Yet, the action itself is not well-authorized because the required authorizers of ⑨ include Carol, but its authorization context contains only Alice and Bob, as they are signatories of the input contract #4 of node ⑧. The authorization failure disappears in the projection to Bank1 though, because the projection of a ledger forgets the requesters of the commits. So from Bank1’s perspective, the asset transfer looks fine.Authorization vs. conformance
Well-authorization and conformance are both necessary to ensure that the Ledger contains only the intended changes. To illustrate this, we modify the example of the wrongly allocated asset such that node ⑨ specifies Alice as the actor instead of Carol. Then, the action (and the ledger as a whole) is well-authorized. Yet, it no longer conforms to the Daml model, because theTransfer choice defines the controller to be the owner of the asset #1, which is Carol in this case.
This conformance failure does show up in Bank 1’s projection, unlike corresponding the well-authorization failure from the previous section.
Validity
Having formalized the three conditions consistency, conformance and well-authorization, we can now formally define validity.Definition: Valid LedgerA Canton Ledger is valid for a set of parties P if all of the following hold:
- The Ledger is consistent for contracts whose signatories include one of the parties in P.
- The Ledger conforms to the Daml templates.
- Every root action on the Ledger is internally well-authorized and its required authorizers in P are requesters of the commit.