> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canton.network/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If you are building a Canton Network application, start from https://docs.canton.network/appdev/quickstart.md. To run only a local network for your own Daml code, without building the QuickStart demo, see https://docs.canton.network/sdks-tools/development-tools/localnet.md.

# Structure

> How the Canton Ledger records party interactions: actions, transactions, inputs and outputs, and commits.

Canton enables multi-party workflows by providing parties with a *virtual global ledger* (VGL),
which encodes the current state of their shared contracts written in a smart contract language.
At a high level, the interactions are visualized below:
Three parties Alice, Bob, and Charlie connect independently to a virtual global ledger, depicted as a large box.
They hold different views of that ledger in their local databases shown as blue icons.
The global ledger is virtual in the sense that no single entity typically sees this global ledger in its entirety;
it is an imaginary database that represents the union of all parties' databases.

![A conceptual diagram of the Virtual Global Ledger.](https://mintcdn.com/cantonfoundation/7L0UWajfTopOkoQm/images/docs_website/da-ledger-model.svg?fit=max\&auto=format\&n=7L0UWajfTopOkoQm\&q=85\&s=22e922c129cbedfae34028b64d1f3724)

The Ledger Model defines:

1. What the changes and the ledgers look like - the structure of the Canton Ledger
2. Who sees which changes and data - the [privacy model](https://docs.canton.network/overview/reference/ledger-model/privacy.md) for the Canton Ledger
3. What changes to the ledger are allowed and who can request them - the [integrity model](https://docs.canton.network/overview/reference/ledger-model/integrity.md) for the Canton Ledger

This section looks at the structure of a ledger that records the interactions between the parties as ledger changes.
The definitions presented here address the first question: "What do changes and ledgers look like?".
The basic building blocks of the recorded interactions are [actions](#actions), which get grouped into [transactions](#transactions), *updates*, *commits*, and the [Ledger](#ledger).

## Running example

### Templates

The running example for the Ledger Model is a simple multi-party interaction of two parties atomically swapping their digital assets.
A digital asset is modeled as a `SimpleAsset` with an issuer, an owner, and the asset description.
The owner can transfer such an asset to a new owner with the `Transfer` choice.

> [!NOTE]
>
> These Daml templates are for illustration purposes only.
> They are not meant for real-world usage as they are heavily simplified.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
template SimpleAsset with
    issuer : Party
    owner : Party
    asset : Text
  where
    signatory issuer
    observer owner

    choice Transfer : ContractId SimpleAsset
      with
        newOwner : Party
      controller owner
      do
        create this with owner = newOwner
```

An atomic swap, also known as delivery versus payment (DvP), combines two asset transfers between the parties in a single transaction.
The `SimpleDvP` template below captures the agreement between two parties `partyA` and `partyB` to swap ownership of the two allocated assets.
Either party to the DvP can execute the swap by exercising the `Settle` choice.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
template SimpleDvP with
    party1 : Party
    party2 : Party
    asset1 : ContractId SimpleAsset
    asset2 : ContractId SimpleAsset
  where
    signatory party1
    signatory party2

    choice Settle : (ContractId SimpleAsset, ContractId SimpleAsset)
      with
        actor : Party
      controller actor
      do
        assert $ actor == party1 || actor == party2
        new1 <- exercise asset1 Transfer with newOwner = party2
        new2 <- exercise asset2 Transfer with newOwner = party1
        pure (new1, new2)
```

To create a DvP contract instance, the parties go through the usual propose-accept workflow pattern shown next.
Party `proposer` creates a proposal for the party `counterparty` with their allocated asset and the description of the asset they expect to swap with.
The `counterparty` can accept the proposal with the `Accept` choice to create a `SimpleDvP` contract, or can accept and immediately settle the swap with the `AcceptAndSettle` choice.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
template ProposeSimpleDvP with
    proposer : Party
    counterparty : Party
    allocated : ContractId SimpleAsset
    expected : SimpleAsset
  where
    signatory proposer
    observer counterparty

    choice Accept : ContractId SimpleDvP
      with
        toBeAllocated : ContractId SimpleAsset
      controller counterparty
      do
        fetchedAsset <- fetch toBeAllocated
        assert $ fetchedAsset == expected
        create $ SimpleDvP with
          party1 = proposer
          party2 = counterparty
          asset1 = allocated
          asset2 = toBeAllocated

    nonconsuming choice AcceptAndSettle : (ContractId SimpleAsset, ContractId SimpleAsset)
      with
        toBeAllocated: ContractId SimpleAsset
      controller counterparty
      do
        dvp <- exercise self $ Accept with ..
        exercise dvp $ Settle with actor = counterparty
```

### Workflow

Most of the examples in this section look at the following Daml Script scenarios
based on the above templates.
Two banks first each issue one asset to either Alice or Bob and then Alice proposes a DvP to Bob.
Bob accepts the proposal and settles the DvP.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
  let eurAsset = SimpleAsset with
        issuer = bank1
        owner = alice
        asset = "1 EUR"
  eur <- submit bank1 do createCmd eurAsset
    
  let usdAsset = SimpleAsset with
        issuer = bank2
        owner = bob
        asset = "1 USD"
  usd <- submit bank2 do createCmd usdAsset
    
  proposeDvP <- submit alice $ do
    createCmd ProposeSimpleDvP with
        proposer = alice
        counterparty = bob
        allocated = eur
        expected = usdAsset
  disclosedEur <- fromSome <$> queryDisclosure alice eur
```

Acceptance and settlement can happen either in a single step via the `AcceptAndSettle` choice.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
  (newUsd, newEur) <- submit (actAs bob <> disclose disclosedEur) $ do
      exerciseCmd proposeDvP $ AcceptAndSettle with toBeAllocated = usd
```

Or in two separate steps with `Accept` followed by `Settle`:

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
  dvp <- submit bob $
    do exerciseCmd proposeDvp $ Accept with toBeAllocated = usd

  (newUsd, newEur) <- submit (actAs bob <> disclose disclosedEur) $ do
      exerciseCmd dvp $ Settle with actor = bob
```

## Actions

### Hierarchical structure

One of the main features of the Ledger Model is a *hierarchical action structure*.
This structure is illustrated using Bob settling the DvP by exercising the `Settle` choice in the above scenario.
Alice and Bob have allocated their assets (contracts #1 and #2) on the ledger to a `SimpleDvp` contract (#4).
These contracts appears as inputs (dashed boxes on the left) in the diagram below.

![The settlement action on the \`SimpleDvp\` contract between Alice and Bob, with the two legs of the swap as consequences.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/dvp-settle-action.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=92dcd6bdccf7c5050568bbe072b6cb47)

Exercising the `Settle` choice yields an Exercise action,
which is the tree of nodes shown in blue.
The input contracts on the left are not part of the action.
The root node describes the parameters of the choice and references the `SimpleDvp` input contract #4.
It has two subtrees, which perform the asset transfers automatically as part of the `Settle` choice.

1. The left subtree represents Alice exercising the `Transfer` choice on her `SimpleAsset` contract #1.
   It consists of two nodes:
   The root node describes the parameters of the choice and the input contract #1.
   The child node, which is a one-node subtree of its own, encodes the creation of Bob's new `SimpleAsset` contract #5.

2. The right subtree is analogous:
   The root node of the subtree describes Bob exercising the `Transfer` choice on his `SimpleAsset` contract #2,
   and its child encodes the creation of Alice's new `SimpleAsset` contract #6.

Notably, the Exercise action is the whole tree even though the root node already describes all the relevant parameters.
The Ledger Model focuses on actions rather than nodes because the root node cannot exist on its own, without its children,
as the choice body in the Daml model must always execute when the choice is exercised.
The [integrity section](https://docs.canton.network/overview/reference/ledger-model/integrity.md) goes into the details of this.

Nevertheless actions are not indivisible, but hierarchical:
The left and right subtrees are actions in their own right,
namely the Exercise actions for Alice and Bob exercising their `Transfer` choice on their `SimpleAsset` input contracts #1 and #2, respectively.
And each of the two subtrees contains another subtree,
namely the creation of Bob's and Alice's new `SimpleAsset` contracts #5 and #6.
Each of these subtrees is an action in its own right.
This hierarchical structure induces a [subaction relationship explained below](#subactions)
and forms the basis for the [privacy model](https://docs.canton.network/overview/reference/ledger-model/privacy.md).

### Definition

Overall, the settlement in the above example contains two types of actions:

1. Creating contracts

2. Exercising choices on contracts.

These are also the two main kinds of actions in the Ledger Model.

A **node** is one of the following:

1. A **Create** node records the creation of the contract.
   It contains the following pieces of information:

   * The **contract ID** is a unique identifier of the contract.
     It is equivalent to the transaction output (TxO) in ledgers based on [unspent transaction outputs (UTxO)](#inputs-and-outputs).

   * The **template ID** identifies the Daml code associated with the contract,
     and its arguments define the **contract instance**, which is the immutable data associated with the contract ID.

   * The **signatories** are the non-empty set of parties that must authorize the creation and archival of the contract.

   * The **contract observers**, or just observers for short, are the set of parties that will be informed about the contract creation and archival, in addition to the signatories.

   In Daml, the signatories and contract observers are determined by the `signatory` and `observer` clauses defined by the template.

   Create nodes are depicted as shown below.
   Diagrams often omit fields with empty values and observers that are also signatories.

   ![The structure of a Create node.](https://mintcdn.com/cantonfoundation/QAGFSphBsRkeZIBi/images/docs_website/create-node.svg?fit=max\&auto=format\&n=QAGFSphBsRkeZIBi\&q=85\&s=9c64445fa65cb58481369dade79bc801)

2. An **Exercise** node records the parameters of a choice that one or more parties have exercised on a contract.
   It contains the following pieces of information:

   * An exercise **kind**, which is either **consuming** or
     **non-consuming**. Once consumed, a contract cannot be used again;
     for example, Alice must not be able to transfer her asset twice, as this would be double spending.
     In contrast, contracts exercised in a non-consuming fashion can be reused, for example for expressing a delegation from one party to another.

   * The **contract ID** on which the choice is exercised.
     This contract is called the **input contract**.

   * The **interface ID** if this choice was exercised through a Daml interface.

   * The **template ID** that defines the smart contract code for the choice with the given **choice name**;
     and the **choice arguments** that are passed to the smart contract code.

   * An associated set of parties called **actors**.
     These are the parties who perform the action.
     They are specified in the `controller` clause in the Daml template.

   * An associated set of **choice observers**.
     These parties will be informed about the choice being exercised.

   * The **exercise result** as the Daml value returned by evaluating the choice body.

   Exercise nodes are depicted as shown below, where the consequences are indicated by arrows ordered left-to-right.
   Diagrams omit the kind if it is consuming, empty field values, and choice observers that are also actors.

   ![The structure of an Exercise node.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/exercise-node.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=52697fa7f7bd95a232ddd2c1588c62b6)

3. A **Fetch** node on a contract, which demonstrates that the contract exists and is active at the time of fetching.
   A Fetch behaves like a non-consuming Exercise with no consequences, and can be repeated.
   The fetch node contains the following pieces of information, analogous to Exercise nodes:
   **contract ID**, **interface ID**, **template ID**, and the **actors**, namely the parties who fetch the contract.

   Fetch nodes are depicted as shown below.

   ![The structure of a Fetch node.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/fetch-node.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=6c854090ce405fc45ba634de15d55ca1)

An **action** consists of a **root node** and a list of **consequences**, which are themselves actions.
This gives rise to the tree structure of an action: The root node of an action has as children the root nodes of its consequences.

An action inherits its kind from its root node:

1. A **Create action** has a Create node as the root.
   The consequences are empty.

2. An **Exercise action** has an Exercise node as the root
   and the consequences are the subactions.
   The Exercise action is the **parent action** of its consequences.

3. A **Fetch action** has a Fetch node as the root.
   The consequences are empty.

The terminology on nodes extends to actions via the root node.
For example, the signatories of a Create action are the signatories of the Create node,
and an Exercise action is (non)consuming if and only if its root node is.
Moreover, an Exercise or a Fetch action on a contract is said to **use** the contract.
Finally, a consuming Exercise is said to **consume** (or **archive**) its contract.

### Examples

An example of a Fetch action appears in the `Accept` choice on a DvP proposal contract from the template `ProposeSimpleDvP`.
The choice body fetches the `SimpleAsset` that Bob allocates to the DvP,
which checks that the asset contract is active and brings the contract instance into the computation,
so that the choice implementation can assert that this asset meets the expectation expressed in the proposal contract.
The next diagram shows this Exercise action with the Fetch action as its first consequence.

![The accept action on Alice's \`ProposeSimpleDvP\` exercised by Bob.](https://mintcdn.com/cantonfoundation/QAGFSphBsRkeZIBi/images/docs_website/dvp-propose-accept-action.svg?fit=max\&auto=format\&n=QAGFSphBsRkeZIBi\&q=85\&s=7985f68cb49127abf9d49ef7688d6c67)

A non-consuming Exercise shows up in the combined `AcceptAndSettle` choice on the `ProposeSimpleDvP` contract:
This choice is non-consuming so that the `Accept` choice exercised in the choice body can consume the proposal contract.
As the next diagram shows, non-consuming Exercises yield multiple references to the same input contract #3.
The diagram also shows that fetches have the same effect: input contract #2 is used twice.

![The accept-and-settle action on Alice's \`ProposeSimpleDvP\` exercised by Bob.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/dvp-propose-accept-and-settle-action.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=7fb2fa37d4a5585387ad222a9afe9349)

### Subactions

This example again highlights the hierarchical structure of actions:
The `AcceptAndSettle` action contains the corresponding actions for `Accept` and `Settle` as its consequences.

More generally, for an action *act*, its **proper subactions** are all actions in the consequences of
*act*, together with all of their proper subactions.
Additionally, *act* is a (non-proper) **subaction** of itself.

The subaction relation is visualized below for Bob's `Settle` Exercise.
Each borderless box contains an action (via its tree of nodes) and the nesting of these boxes encodes the subaction relation.
In detail, both the blue and purple boxes are proper subactions of Bob's `Settle` action shown in grey.
The green box is a proper subaction of the blue and the grey boxes, and the yellow box is a proper subaction of the purple and the grey boxes.

![The subactions of Bob exercising the \`Settle\` choice on the DvP contract.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/dvp-settle-subactions.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=18826aee2e0d531ab1f060dfc332fc00)

## Transactions

A **transaction** is a list of actions that are executed atomically.
Those actions are called the **root actions** of the transaction.
That is, for a transaction *tx = act*<sub>1</sub>*, …, act*<sub>n</sub>, every *act*<sub>i</sub> is a root action.
For example, if Alice and Charlie have made one DvP proposal each for Bob, then Bob may want to both accept simulataneously.
To that end, Bob exercises both `Accept` choices in a single transaction with two root actions (blue and purple), as shown next.
Visually, transactions are delimited by the dashed lines on both sides, to distinguish them from actions.
Like for actions, the input contracts on the left are not part of the transaction.

![A transaction with two top-level actions where Bob accepts two DvP proposal, one from Alice and one from Charlie.](https://mintcdn.com/cantonfoundation/QAGFSphBsRkeZIBi/images/docs_website/dvp-accept-two.svg?fit=max\&auto=format\&n=QAGFSphBsRkeZIBi\&q=85\&s=ee3f6ea1f8df103166460369377101d2)

For another example, the consequences of an Exercise action are a list of actions and therefore form a transaction
In the example of the `Settle` action on Alice's and Bob's `SimpleDvP`,
the consequences of the `Settle` action form the following transaction,
where actions are ordered left-to-right as before.
The transaction consists of two root actions (blue and purple), namely the two `Transfer` actions of the two legs of the DvP.

![The consequences of the Settle action are a transaction of two actions, namely the two Transfer legs of the DvP.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/dvp-settle-consequences-are-transactions.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=5b0538ddeb688764316720634ea98d6a)

The hierarchical structure of actions extends to transactions and yields the notion of subtransactions.
A **proper subtransaction** of a transaction is obtained by (repeatedly) replacing an action by its consequences;
and a **subtransaction** of a transaction is either the transaction itself or a proper subtransaction thereof.

For example, given the transaction shown above consisting only of the two consequences of the `Settle` action,
the next diagram shows all seven proper non-empty subtransactions, each with their dashed delimiters.

![All proper subtransactions of the consequences of the \`Settle\` action.](https://mintcdn.com/cantonfoundation/zmlOjLpKuDjnaObr/images/docs_website/dvp-settle-consequences-subtransactions.svg?fit=max\&auto=format\&n=zmlOjLpKuDjnaObr\&q=85\&s=809e7066dbf0b44649bada35081b06d9)

The [privacy model](https://docs.canton.network/overview/reference/ledger-model/privacy.md) uses the concept of subtransactions to define the visibility rules.

## Inputs and outputs

The Ledger Model falls into the category of [(extended) UTxO-style ledgers](https://en.wikipedia.org/wiki/Unspent_transaction_output)
where the set of unspent transaction outputs (UTxOs) constitutes the current state of a ledger.
Here, the **transaction outputs** are the contract IDs of the contracts created in a transaction.
When a contract is consumed, its contract ID is spent and thus removed from the UTxO set.
The data associated with each UTxO is immutable;
modifications happen by consuming a contract ID and recreating a new contract with a different contract ID.

This Ledger Model extends the UTxO model in two aspects:

* A transaction may use a contract without consuming it, for example by exercising a non-consuming choice or fetching it.
  In such a case, the contract ID remains in the set of UTxOs even though it appears as an input to a transaction.

* Transactions are structured hierarchically and contract IDs created in the transaction may be consumed within the same transaction.
  For example, inside the `AcceptAndSettle` action, the created `SimpleDvP` in the first consequence is consumed by the second consequence.
  Such contracts are called **transient**.

These aspects are discussed in more detail in the remaining sections of the Ledger Model.

## Ledger

The transaction structure records the contents of a party interaction.
The ledger records two more aspects of an interaction:

* An identifier to uniquely refer a particular party interaction.

* The parties who requested a particular party interaction.

Due to the [privacy model](https://docs.canton.network/overview/reference/ledger-model/privacy.md), not everyone sees all parts of a party interaction.
A unique identifier for a party interaction allows different parties to correlate whether they see parts of the same interactions.
The notion of an **update** adds such an identifier.
It consists of a single transaction and the so-called **update ID**, a string.
Examples in the Ledger Model use update IDs of the form `TX i` for some number `i`, similar to the transaction view in Daml Studio.
On the Ledger API, update IDs are arbitrary strings whose lexicographic order is independent from their order on the ledger.

A **commit** adds the information *who requested a party interaction*.
It consists of an update and the one or more parties that requested it.
Those parties are called the **requesters** of the commit.
In Daml Script, the requesters correspond to the `actAs` parties given to the `submit` commands.

**Definition: Ledger**

A **Ledger** is a directed acyclic graph (DAG) of commits, where the update IDs are unique.

**Definition: top-level action**

For a commit, the root actions of its transaction are called the **top-level actions**.
A top-level action of any ledger commit is also a top-level action of the ledger.

A Canton Ledger thus represents the full history of all actions taken by parties.
The graph structure of the Ledger induces a **happens-before order** on the commits in the ledger.
We say that commit *c*<sub>1</sub> *happens before* *c*<sub>2</sub> if and only if the ledger contains a non-empty path from *c*<sub>1</sub> to *c*<sub>2</sub>,
or equivalently, the transitive closure of the graph contains an edge from *c*<sub>1</sub> to *c*<sub>2</sub>.

> [!NOTE]
>
> The [integrity conditions](https://docs.canton.network/overview/reference/ledger-model/integrity.md) on a ledger require that the happens-before order respects the lifecycle of contracts.
> For example, the commit that creates a contract must happen before the commit that spends the contract unless they are the same.
> For the next few sections, we will consider only ledgers that meet these conditions.

Visually, a ledger can be represented as a sequence growing from left to right as time progresses.
Below, dashed vertical lines in purple mark the boundaries of commits,
and each commit is annotated with its requester(s) and the update ID.
Blue arrows link each Exercise and Fetch action to the Create action of the input contract.
These arrows highlight that the ledger forms a **transaction graph** in the sense of a UTXO blockchain.

For example, the following Daml Script encodes the whole workflow of the running DvP example.

```haskell theme={"theme":{"light":"github-light","dark":"github-dark"}}
  let eurAsset = SimpleAsset with
        issuer = bank1
        owner = alice
        asset = "1 EUR"
  eur <- submit bank1 do createCmd eurAsset
    
  let usdAsset = SimpleAsset with
        issuer = bank2
        owner = bob
        asset = "1 USD"
  usd <- submit bank2 do createCmd usdAsset
    
  proposeDvP <- submit alice $ do
    createCmd ProposeSimpleDvP with
        proposer = alice
        counterparty = bob
        allocated = eur
        expected = usdAsset
  disclosedEur <- fromSome <$> queryDisclosure alice eur

  (newUsd, newEur) <- submit (actAs bob <> disclose disclosedEur) $ do
      exerciseCmd proposeDvP $ AcceptAndSettle with toBeAllocated = usd
```

This workflow gives rise to the ledger shown below with four commits:

* In the first commit, Bank 1 requests the creation of the `SimpleAsset` of `1 EUR` issued to Alice (contract #1).

* In the second commit, Bank 2 requests the creation of the `SimpleAsset` of `1 USD` issued to Bob (contract #2).

* In the third commit, Alice requests the creation of the `ProposeSimpleDvP` (contract #3).

* In the fourth commit, Bob requests to exercise the `AcceptAndSettle` choice on the DvP proposal.

![The sequence of commits for the whole DvP workflow. First, banks 1 and 2 issue the assets, then Alice proposes the DvP, and finally Bob accepts and settles it.](https://mintcdn.com/cantonfoundation/QAGFSphBsRkeZIBi/images/docs_website/dvp-ledger.svg?fit=max\&auto=format\&n=QAGFSphBsRkeZIBi\&q=85\&s=2905a7d8b725484c0035079b45d2ba07)

> [!NOTE]
>
> The integrity constraints do not impose an order between independent commits.
> In this example, there need not be edges among the first three commits `TX 0`, `TX 1`, and `TX 2`,
> so they could be presented in any order.
>
> As the Ledger is a DAG, one can always extend the order into a linear sequence via a topological sort.
> For the next sections, we pretend that the Ledger is totally ordered (unless otherwise specified).
> We discuss the more general partial orders in the [causality section](https://docs.canton.network/overview/reference/ledger-model/causality.md).
