The Ledger API services

The Ledger API is structured as a set of services. The core services are implemented using gRPC and Protobuf, but most applications access this API through the mediation of the language bindings.

This page gives more detail about each of the services in the API, and will be relevant whichever way you’re accessing it.

If you want to read low-level detail about each service, see the protobuf documentation of the API.

Overview

The API is structured as two separate data streams:

  • A stream of commands TO the ledger that allow an application to submit transactions and change state.
  • A stream of transactions and corresponding events FROM the ledger that indicate all state changes that have taken place on the ledger.

Commands are the only way an application can cause the state of the ledger to change, and events are the only mechanism to read those changes.

For an application, the most important consequence of these architectural decisions and implementation is that the ledger API is asynchronous. This means:

  • The outcome of commands is only known some time after they are submitted.
  • The application must deal with successful and erroneous command completions separately from command submission.
  • Ledger state changes are indicated by events received asynchronously from the command submissions that cause them.

The need to handle these issues is a major determinant of application architecture. Understanding the consequences of the API characteristics is important for a successful application design.

For more help understanding these issues so you can build correct, performant and maintainable applications, read the application architecture guide.

Glossary

  • The ledger is a list of transactions. The transaction service returns these.
  • A transaction is a tree of actions, also called events, which are of type create, exercise or archive. The transaction service can return the whole tree, or a flattened list.
  • A submission is a proposed transaction, consisting of a list of commands, which correspond to the top-level actions in that transaction.
  • A completion indicates the success or failure of a submission.

Submitting commands to the ledger

Command submission service

Use the command submission service to submit commands to the ledger. Commands either create a new contract, or exercise a choice on an existing contract.

A call to the command submission service will return as soon as the ledger server has parsed the command, and has either accepted or rejected it. This does not mean the command has been executed, only that the server has looked at the command and decided that its format is acceptable, or has rejected it for syntactic or content reasons.

The on-ledger effect of the command execution will be reported via the transaction service, described below. The completion status of the command is reported via the command completion service. Your application should receive completions, correlate them with command submission, and handle errors and failed commands. Alternatively, you can use the command service, which conveniently wraps the command submission and completion services.

Change ID

Each intended ledger change is identified by its change ID, consisting of of the submitting parties (i.e., the union of party and act_as), application ID and command ID.

Application-specific IDs

The following application-specific IDs, all of which are included in completion events, can be set in commands:

  • A submissionId, returned to the submitting application only. It may be used to correlate specific submissions to specific completions.
  • A commandId, returned to the submitting application only; it can be used to correlate commands to completions.
  • A workflowId, returned as part of the resulting transaction to all applications receiving it. It can be used to track workflows between parties, consisting of several transactions.

For full details, see the proto documentation for the service.

Command deduplication

The command submission service deduplicates submitted commands based on their change ID:

  • Applications can provide a deduplication duration for each command. If this parameter is not set, the default maximum deduplication period is used.
  • A command submission is considered a duplicate submission if the ledger API server is aware of another command within the deduplication period and with the same change ID.
  • A command resubmission will generate a rejection until the original submission was rejected (i.e. the command failed and resulted in a rejected transaction) or until the effective deduplication period has elapsed since the completion of the original command, whichever comes first.
  • Command deduplication is only guaranteed to work if all commands are submitted to the same participant. Ledgers are free to perform additional command deduplication across participants. Consult the respective ledger’s manual for more details.
  • A command submission will return:
    • The result of the submission (Empty or a gRPC error), if the command was submitted outside of the deduplication period of a previous command with the same change ID on the same participant.
    • The status error ALREADY_EXISTS, if the command was discarded by the ledger server because it was sent within the deduplication period of a previous command with the same change ID.
  • If the ledger provides additional command deduplication across participants, the initial command submission might be successful, but ultimately the command can be rejected if the deduplication check fails on the ledger.

For details on how to use command deduplication, see the Application Architecture Guide.

Note

  • The ledger may extend the deduplication period specified in the request arbitrarily, even beyond the maximum deduplication duration specified in the ledger configuration. The deduplication period chosen by the ledger is the effective deduplication period.
  • Regardless, the deduplication period specified in the request is always checked against the configured maximum deduplication duration.
  • A command submission is considered a duplicate submission if the ledger is aware of another command within the effective deduplication period and with the same change ID.
  • The following ledger integrations always extend the deduplication period to the configured maximum deduplication duration:

Command completion service

Use the command completion service to find out the completion status of commands you have submitted.

Completions contain the commandId of the completed command, and the completion status of the command. This status indicates failure or success, and your application should use it to update what it knows about commands in flight, and implement any application-specific error recovery.

For full details, see the proto documentation for the service.

Command service

Use the command service when you want to submit a command and wait for it to be executed. This service is similar to the command submission service, but also receives completions and waits until it knows whether or not the submitted command has completed. It returns the completion status of the command execution.

You can use either the command or command submission services to submit commands to effect a ledger change. The command service is useful for simple applications, as it handles a basic form of coordination between command submission and completion, correlating submissions with completions, and returning a success or failure status. This allow simple applications to be completely stateless, and alleviates the need for them to track command submissions.

For full details, see the proto documentation for the service.

Reading from the ledger

Transaction service

Use the transaction service to listen to changes in the ledger state, reported via a stream of transactions.

Transactions detail the changes on the ledger, and contains all the events (create, exercise, archive of contracts) that had an effect in that transaction.

Transactions contain a transactionId (assigned by the server), the workflowId, the commandId, and the events in the transaction.

Subscribe to the transaction service to read events from an arbitrary point on the ledger. This arbitrary point is specified by the ledger offset. This is important when starting or restarting and application, and to work in conjunction with the active contracts service.

For full details, see the proto documentation for the service.

Transaction and transaction trees

TransactionService offers several different subscriptions. The most commonly used is GetTransactions. If you need more details, you can use GetTransactionTrees instead, which returns transactions as flattened trees, represented as a map of event IDs to events and a list of root event IDs.

Verbosity

The service works in a non-verbose mode by default, which means that some identifiers are omitted:

  • Record IDs
  • Record field labels
  • Variant IDs

You can get these included in requests related to Transactions by setting the verbose field in message GetTransactionsRequest or GetActiveContractsRequest to true.

Active contracts service

Use the active contracts service to obtain a party-specific view of all contracts that are active on the ledger at the time of the request.

The active contracts service returns its response as a stream of batches of the created events that would re-create the state being reported (the size of these batches is left to the ledger implementation). As part of the last message message, the offset at which the reported active contract set was valid is included. This offset can be used to subscribe to the “flat transactions” stream to keep a consistent view of the active contract set without querying the active contract service further.

This is most important at application start, if the application needs to synchronize its initial state with a known view of the ledger. Without this service, the only way to do this would be to read the Transaction Stream from the beginning of the ledger, which can be prohibitively expensive with a large ledger.

For full details, see the proto documentation for the service.

Verbosity

See Verbosity above.

Note

The RPCs exposed as part of the transaction and active contracts services make use of offsets.

An offset is an opaque string of bytes assigned by the participant to each transaction as they are received from the ledger. Two offsets returned by the same participant are guaranteed to be lexicographically ordered: while interacting with a single participant, the offset of two transactions can be compared to tell which was committed earlier. The state of a ledger (i.e. the set of active contracts) as exposed by the Ledger API is valid at a specific offset, which is why the last message your application receives when calling the ActiveContractsService is precisely that offset. In this way, the client can keep track of the relevant state without needing to invoke the ActiveContractsService again, by starting to read transactions from the given offset.

Offsets are also useful to perform crash recovery and failover as documented more in depth in the application architecture page.

You can read more about offsets in the protobuf documentation of the API.

Utility services

Party management service

Use the party management service to allocate parties on the ledger and retrieve information about allocated parties.

Allocating parties is necessary to interact with the ledger. For more information, refer to the pages on Identity Management and the API reference documentation.

Package service

Use the package service to obtain information about Daml packages available on the ledger.

This is useful for obtaining type and metadata information that allow you to interpret event data in a more useful way.

For full details, see the proto documentation for the service.

Ledger identity service

Use the ledger identity service to get the identity string of the ledger that your application is connected to.

You need to include this identity string when submitting commands. Commands with an incorrect identity string are rejected.

For full details, see the proto documentation for the service.

Ledger configuration service

Use the ledger configuration service to subscribe to changes in ledger configuration.

This configuration includes the maximum command deduplication period (see Command Deduplication for details).

For full details, see the proto documentation for the service.

Version service

Use the version service to retrieve information about the Ledger API version.

For full details, see the proto documentation for the service.

Pruning service

Use the pruning service to prune archived contracts and transactions before or at a given offset.

For full details, see the proto documentation for the service.

Testing services

These are only for use for testing with the Sandbox, not for on production ledgers.

Time service

Use the time service to obtain the time as known by the ledger server.

For full details, see the proto documentation for the service.

Reset service

Use the reset service to reset the ledger state, as a quicker alternative to restarting the whole ledger application.

This resets all state in the ledger, including the ledger ID, so clients will have to re-fetch the ledger ID from the identity service after hitting this endpoint.

For full details, see the proto documentation for the service.

Services diagram

../_images/services.svg