- The Ledger API
- The Canton Ledger Model
Causality Examples
A deployed Canton Ledger need not totally order all transactions, unlike the totally ordered Ledger assumed in the Structure section of the Canton Ledger Model. The following examples illustrate these ordering guarantees of the Ledger API. They use a paint counteroffer workflow: the Bank creates an Iou for Alice (A); Alice creates a CounterOffer for the painter (P) and a ShowIou contract; exercising ShowIou fetches the Iou and discloses it to the painter; the painter accepting the CounterOffer transfers the Iou and creates a PaintAgree contract. The party projections are as follows:Stakeholders of a Contract See Creation and Archival in the Same Order
Every Canton Ledger orders the creation of the CounterOffer A P Bank before the painter exercising the consuming choice on the CounterOffer. (If the Create was ordered after the Exercise, the resulting shared ledger would be inconsistent, which violates the validity guarantee of Canton ledgers.) Accordingly, Alice will see the creation before the archival on her transaction stream and so will the painter. This does not depend on whether they are hosted on the same Participant Node.Signatories of a Contract and Stakeholder Actors See Usages After the Creation and Before the Archival
The Fetch A (Iou Bank A) action comes after the creation of the Iou Bank A and before its archival, for both Alice and the Bank, because the Bank is a signatory of the Iou Bank A contract and Alice is a stakeholder of the Iou Bank A contract and an actor on the Fetch action.Commits Are Atomic
Alice sees the Create of her Iou before the creation of the CounterOffer, because the CounterOffer is created in the same commit as the Fetch of the Iou and the Fetch commit comes after the Create of the Iou.Non-Consuming Usages in Different Commits May Appear in Different Orders
Suppose that the Bank exercises a non-consuming choice on the Iou Bank A without consequences while Alice creates the CounterOffer. In the ledger shown below, the Bank’s commit comes before Alice’s commit.Out-of-Band Causality Is Not Respected
The following examples assume that Alice splits up her commit into two as follows:Counteroffer workflow with four commits.
Divulged Actions Do Not Induce Order
The painter witnesses the fetching of Alice’s Iou when the ShowIou contract is consumed. The painter also witnesses the Exercise of the Iou when Alice exercises the transfer choice as a consequence of the painter accepting the CounterOffer. However, as the painter is not a stakeholder of Alice’s Iou contract, he may observe Alice’s ShowIou commit after the archival of the Iou as part of accepting the CounterOffer. In practice, this can happen in a setup where two Participant Nodes N1 and N2 host the painter. He sees the divulged Iou and the created CounterOffer through N1’s ledger-effects update stream and then submits the acceptance through N1. Like in the previous example, N2 does not know about the dependence of the two commits. Accordingly, N2 may output the accepting transaction before the ShowIou contract on the transaction stream. Even though this may seem unexpected, it is in line with stakeholder-based ledgers: Since the painter is not a stakeholder of the Iou contract, he should not care about the archivals or creates of the contract. In fact, the divulged Iou contract shows up neither in the painter’s State Service nor in the ACS delta in the update stream. Such witnessed events are included in the ledger-effects update stream as a convenience: They relieve the painter from computing the consequences of the choice and enable him to check that the action conforms to the Daml model. Similarly, being an actor of an Exercise action induces order with respect to other uses of the contract only if the actor is a contract stakeholder. This is because non-stakeholder actors of an Exercise action merely authorize the action, but they do not track whether the contract is active; this is what signatories and contract observers are for. Analogously, choice observers of an Exercise action benefit from the ordering guarantees only if they are contract stakeholders.The Ordering Guarantees Depend on the Party
By the previous example, for the painter, fetching the Iou is not ordered before transferring the Iou. For Alice, however, the Fetch must appear before the Exercise because Alice is a stakeholder on the Iou contract. This shows that the ordering guarantees depend on the party.Causality Graphs
The above examples indicate that Canton ledgers order transactions only partially. Canton ledgers can be represented as finite directed acyclic graphs (DAG) of transactions.Definition: causality graphA causality graph is a finite directed acyclic graph G of transactions that is transitively closed.
Transitively closed means that whenever v1 -> v2 and v2 -> v3 are edges in G,
then there is also an edge v1 -> v3 in G.
Definition: action orderFor a causality graph G,
the induced action order on the actions in the transactions combines the graph-induced order between transactions with the execution order of actions inside each transaction.
It is the least partial order that includes the following ordering relations between two actions act1 and act2:
- act1 and act2 belong to the same transaction and act1 precedes act2 in the transaction.
- act1 and act2 belong to different transactions in vertices tx1 and tx2 and there is a path in G from tx1 to tx2.
Checking for an edge instead of a path in G from tx1 to tx2 is equivalent
because causality graphs are transitively closed.
The definition uses path because the figures below omit transitive edges for readability.
Causality graph for the counteroffer workflow with four commits.
Consistency
Consistency ensures that a causality graph sufficiently orders all the transactions. It generalizes ledger consistency from the Canton Ledger Model as explained below.Definition: Causal consistency for a contractLet G be a causality graph and X be a set of actions on a contract c that belong to transactions in G.
The graph G is causally consistent for the contract c on X if all of the following hold:
- If X is not empty, then X contains exactly one Create action. This action precedes all other actions in X in G’s action order.
- If X contains a consuming Exercise action act, then act follows all actions in X other than act in G’s action order.
Definition: Causal consistency for a keyLet G be a causality graph and X be a set of actions on a key k that belong to transactions in G.
The graph G is causally consistent for the key k on X if all of the following hold:
- All Create and consuming Exercise actions in X are totally ordered in G’s action order and Creates and consuming Exercises alternate, starting with Create. Every consecutive Create-Exercise pair acts on the same contract.
- All NoSuchKey actions in X are action-ordered with respect to all Create and consuming Exercise actions in X. No NoSuchKey action is action-ordered between a Create action and its subsequent consuming Exercise action in X.
Definition: Consistency for a causality graphLet X be a subset of the actions in a causality graph G.
Then G is consistent on X (or X-consistent) if G is causally consistent for all contracts c on the set of actions on c in X and for all keys k on the set of actions on k in X.
G is consistent if G is consistent on all the actions in G.
Definition: Minimal consistent causality graphAn X-consistent causality graph G is X-minimal if no strict subgraph of G (same vertices, fewer edges) is an X-consistent causality graph.
If X is the set of all actions in G, then X is omitted.
We can focus on parts of the causality graph by restricting the set X.
If X consists of the actions on Iou contracts, this causality graph is X-consistent.
Yet, it is not X-minimal since the edge tx2 -> tx4 can be removed without violating X-consistency: the edge is required only because of the CounterOffer actions, which are excluded from X.
The X-minimal consistent causality graph looks as follows, where the actions in X are highlighted in red.
Minimal consistent causality graph for the highlighted actions.
From Causality Graphs to Ledgers
Since causality graphs are acyclic, their vertices can be sorted topologically and the resulting list is again a causality graph, where every vertex has an outgoing edge to all later vertices. If the original causality graph is X-consistent, then so is the topological sort, as topological sorting merely adds edges. For example, the transactions on the ledger in the out-of-band causality example are a topological sort of the corresponding causality graph. Conversely, we can reduce an X-consistent causality graph to only the causal dependencies that X-consistency imposes. This gives a minimal X-consistent causality graph.Definition: Reduction of a consistent causality graphFor an X-consistent causality graph G, there exists a unique minimal X-consistent causality graph reduceX(G) with the same vertices and the edges being a subset of G.
The graph reduceX(G) is called the X-reduction of G.
As before, X is omitted if it contains all actions in G.
The reduction reduceX(G) of an X-consistent causality graph G can be computed as follows:
- Set the vertices of G’ to the vertices of G.
- The causal consistency conditions for contracts and keys demand that certain pairs of actions act1 and act2 in X must be action-ordered. For each such pair, determine the actions’ ordering in G and add an edge to G’ from the earlier action’s transaction to the later action’s transaction.
- reduceX(G) is the transitive closure of G’.
- Ledger Consistency according to Canton Ledger Model
- Consistency of causality graph
Local Ledgers
As explained in the Canton Ledger Model, parties see only a projection of the shared ledger for privacy reasons. Like consistency, projection extends to causality graphs as follows.Definition: Stakeholder informeeA party P is a stakeholder informee of an action act if all of the following holds:
- P is an informee of act.
- If act is an action on a contract then P is a stakeholder of the contract.
Definition: Causal consistency for a partyA causality graph G is consistent for a party P (P-consistent) if G is consistent on all the actions that P is a stakeholder informee of.
Definition: Projection of a consistent causality graphThe projection projP(G) of a consistent causality graph G to a party P is the P-reduction of the following causality graph G’:
- The vertices of G’ are the vertices of G projected to P, excluding empty projections.
- There is an edge between two vertices v1 and v2 in G’ if there is an edge from the G-vertex corresponding to v1 to the G-vertex corresponding to v2.
Projections of the split counteroffer causality graph.
Ledger API Ordering Guarantees
The Update Service provides the updates as a stream of Daml transactions and the State Service summarizes all the updates up to a given point by the contracts that are active at this point. Conceptually, both services are derived from the local ledger that the Participant Node manages for each hosted party. That is, the ledger-effects update stream (transaction shapeTRANSACTION_SHAPE_LEDGER_EFFECTS) for a party is a topological sort of the party’s local ledger.
The ACS-delta update stream (transaction shape TRANSACTION_SHAPE_ACS_DELTA) contains precisely the CreatedEvents and ArchivedEvents
that correspond to Create and consuming Exercise actions in the ledger-effects update stream where the party is a stakeholder of the affected contract.
The transactions output by the Update Service omit Fetch and NoSuchKey actions
that are part of the transactions in the local ledger.
The Fetch and NoSuchKey actions are thus removed before the Update Service outputs the transactions.
The virtual shared causality graph exists only as a concept, to reason about Canton ledger guarantees.
A deployed Canton ledger in general does not store or even construct such a shared causality graph.
The Participant Nodes merely maintain the local ledgers for their parties.
They synchronize these local ledgers to the extent that they remain consistent.
That is, all the local ledgers can in theory be combined into a consistent single causality graph of which they are projections.
Explaining the Causality Examples
The causality examples can be explained in terms of causality graphs and local ledgers as follows:- Stakeholders of a Contract See Creation and Archival in the Same Order Causal consistency for the contract requires that the Create comes before the consuming Exercise action on the contract. As all stakeholders are informees on Create and consuming Exercise actions of their contracts, the stakeholder’s local ledgers impose this order on the actions.
- Signatories of a Contract and Stakeholder Actors See Usages After the Creation and Before the Archival Causal consistency for the contract requires that the Create comes before the non-consuming Exercise and Fetch actions of a contract and that consuming Exercises follow them. Since signatories and stakeholder actors are informees of Create, Exercise, and Fetch actions, the stakeholder’s local ledgers impose this order on the actions.
- Commits Are Atomic Local ledgers are DAGs of (projected) transactions. Topologically sorting such a DAG cannot interleave one transaction with another, even if the transaction consists of several top-level actions.
- Non-Consuming Usages in Different Commits May Appear in Different Orders Causal consistency does not require ordering between non-consuming usages of a contract. As there is no other action in the transaction that would prescribe an ordering, the Participant Nodes can output them in any order.
- Out-of-Band Causality Is Not Respected Out-of-band data flow is not captured by causal consistency and therefore does not induce ordering.
- Divulged Actions Do Not Induce Order The painter is not an informee of the Fetch and Exercise actions on Alice’s Iou; he merely witnesses them. The painter’s local ledger therefore does not order tx3 before tx4. So the painter’s transaction stream can output tx4 before tx3.
- The Ordering Guarantees Depend on the Party Alice is an informee of the Fetch and Exercise actions on her Iou. Unlike for the painter, her local ledger does order tx3 before tx4, so Alice is guaranteed to observe tx3 before tx4 on all Participant Nodes through which she is connect to the Canton ledger.