Dune Software Architecture¶
This document contains two section. The first section, which should be readable by anyone, describes the main elements of Dune from a distance. It abstracts from all plumbing and both internal and system dependencies to give a simple view of the main components, their responsibilities and interactions. The second part is written for developers, and is at the level of OPAM packages.
The Big Picture¶
The diagram below shows a very coarse grained architecture of Dune.
The characteristic that makes Dune unique is its self-amending property. The part that amends itself is called the economic protocol (the green eye of the octopus), sometimes abbreviated by protocol or even proto in the source code. The rest of a Dune node is what we call the shell (the blue octopus).
The protocol is responsible for interpreting the transactions and other administrative operations. It also has the responsibility to detect erroneous blocks.
An important thing to notice is that the protocol always sees only one block chain. In other words, a linear sequence of blocks since the genesis. It does not know that it lives in an open network where nodes can propose alternative heads.
Only the shell knows about the multiple heads. It is responsible for choosing between the various chain proposals that come from the bakers (the programs that cook new blocks) of the network. The shell has the responsibility of selecting and downloading alternative chains, feed them to the protocol, which in turn has the responsibility to check them for errors, and give them an absolute score. The shell then simply selects the valid head of highest absolute score. This part of the shell is called the validator.
The rest of the shell includes the peer-to-peer layer, the disk storage of blocks, the operations to allow the node to transmit the chain data to new nodes and the versioned state of the ledger. In-between the validator, the peer-to-peer layer and the storage sits a component called the distributed database, that abstracts the fetching and replication of new chain data to the validator.
Protocols are compiled using a tweaked OCaml compiler (green part on the left of the picture) that does two things. First, it checks that the protocol’s main module has the right type. A good analogy is to see protocol as plug-ins, and in this case, it means that it respects the common plugin interface. Then, it restricts the typing environment of the protocol’s code so that it only calls authorized modules and functions. Seeing protocols as plug-ins, it means that the code only called primitives from the plug-in API. It is a form of statically enforced sandboxing.
Finally, the RPC layer (in yellow on the right in the picture) is an
important part of the node. It is how the client, third party
applications and daemons can interact with the node and introspect its
state. This component uses the mainstream JSON format and HTTP protocol.
It uses in-house libraries
(via the module Data_encoding). It
is fully inter-operable, and auto descriptive, using JSON schema.
Software Architecture and Packages Relationship¶
The diagram below shows the main OPAM packages present in the source
code of Dune, and their dependencies. The
tezos- prefix has been
dropped for clarity.
Base and below¶
At the center, the tezos-base package is where the blockchain specific code starts. Before it are the set of libraries that are used everywhere for basic operations.
- tezos-stdlib contains a few extensions over the OCaml standard library (a few string primitives, an
Optionmodule, etc.), a few
Lwtutilities, and a
Comparemodule that implements monomorphic comparison operators.
- tezos-data-encoding is the in-house combinator-based serialization library. From a single type description
t encoding, the code can read to and write from values of type
tboth binary and JSON representations. For both, the library provides machine and human-readable documentations by the use of documentation combinators. The JSON part depends on ocplib-json-typed. A tutorial is available for this library.
- tezos-error-monad is an in-house monadic interface to the OCaml
('a, 'b) resulttype, that fixes the
'bto an extensible type
error(actually a list, to hold an error trace). When extending the type, programmers must also call the
register_errorfunction that registers a pretty printer and an encoding for serialization. A tutorial is available for this library.
- tezos-rpc provides the basics of Dune’ RPC service mechanism. It provides combinators for building service hierarchies à la Ocsigen/Eliom, registering and calling services. This module is based on ocplib-resto, that allows for automatic generation of a machine and human-readable of the hierarchy of services: the structure of URLs and the expected formats for input and output bodies, via the use of
- tezos-crypto wraps the external cryptography libraries that we use. We try to use minimal reference implementations, with as thin as possible bindings, and rely on libraries from the HACL* project, written and verified in the F* programming language, and extracted to C.
- tezos-micheline is the concrete syntax used by Michelson, the language of smart contracts. It mostly contains the generic, untyped AST, a printer and a parser.
- tezos-base wraps all these module in a common foundation for all the other components of Dune, and introduces the data structures of the blockchain (e.g.
P2p_identity) that are shared between the shell, economic protocol, client, daemons and third party software. It also rewraps some modules from
cryptoas functors that build all-in-one modules for a given type (for instance, the module for block hashes contains all possible converters, command line and RPC argument builders, pretty printers, an autocompleter, etc.). This package also contains the
cli_entriesmodule that we use to handle command line parsing in some executables.
The shell is the part of the node responsible for all communications, peer-to-peer and RPC, acting as a cocoon around the economic protocols.
- tezos-shell-services contains the definition of the node’s service hierarchy, and calling functions to use in the client (or any third party software). As this library is linked into the client to call the services in a type-safe way, only the description of services is done here. The registration of handlers is done in the rest of the node’s implementation.
- tezos-rpc-http-client and tezos-rpc-http-server use cohttp to implement the RPC over HTTP server and client, allowing to make actual use of services declared using tezos-rpc.
- tezos-p2p is the in-house peer-to-peer layer.
- tezos-storage contains the raw simple key-value store used for the chain data, and the raw versioned key-value store used for storing the ledger’s context (one version per block). This is implemented using irmin and currently lmdb.
- tezos-protocol-updater maintains the table of available protocol versions, embedded or dynamically linked.
- tezos-shell implements the scheduling of block validations, the mempool management, and the distributed database. A description is available in this document.
The Economic Protocol Environment and Compiler¶
Economic protocols are compiled by a specific version of the OCaml compiler. This compiler restricts the set of modules available to the economic protocol, as a form of static sandboxing. It also generates a functorized version of the protocol, to make the execution of the protocol in alternative environment possible.
- tezos-protocol-environment-sigs contains the modules that are available to the economic protocol. A review of this sandbox is available here. This modules include a stripped down standard library, and interfaces to the crypto APIs, RPC definitions, and key-value store.
- tezos-protocol-compiler is the compiler for economic protocols: an alternative driver to the OCaml ocaml-compiler-libs that typechecks within the protocol environment, and performs some more checks on the protocol code.
tezos-protocol-xxxis produced by the protocol compiler and contains a functorized version of protocol
xxxthat takes its standard library as parameter. This parameter can be filled with any of the implementations described in the two points below.
- tezos-shell-context implements a context representation
- that is accepted by the protocol environment. The node uses this instance to read and write data on disk.
- tezos-protocol-environment contains the protocol generic environment. It also defines two different context instances: one that simulates the key-value store in memory for testing, and one whose context function are dummy ones which can be used when only the types and non contextual functions of the protocol are needed.
tezos-embedded-protocol-xxxcontains a version of protocol
xxxwhose standard library is pre-instantiated to the shell’s implementation, these are the ones that are linked into the node. It also contains a module that registers the protocol in the node’s protocol table.
The Embedded Economic Protocols¶
Three economic protocols are included in the main Dune repository.
- tezos-protocol-000-Ps9mPmXa (tezos-embedded-protocol-000-Ps9mPmXa) is the protocol of the genesis block. It accepts a single block, signed by a activator whose public key is hardcoded, that single action is to switch to a new protocol chosen by the activator.
- tezos-protocol-005-PsBabyM1 (tezos-embedded-protocol-005-PsBabyM1) is the first real protocol of Dune. A tutorial is available to start reading the protocol’s code.
- tezos-protocol-demo-noops (tezos-embedded-protocol-demo-noops) is just a demo protocol that does nothing interesting but has the right shape.
The Client Library¶
- tezos-client-base define the client context, which is an object whose methods allow for: accessing a wallet of keys, interacting via the user, making RPC calls, and signing data using signer plug-ins. Most of the, including RPC calling functions from tezos-shell-services and tezos-protocol-005-PsBabyM1, are abstracted over this object type. That way, it is possible to use the same code for different platforms or toolkits.
- tezos-client-005-PsBabyM1 provides some functions to perform the operations of protocol alpha using the wallet and signers from the client context.
- tezos-client-commands plugs the basic context access functions from tezos-client-base as handlers for the commands of the
dune-clientcommand line wallet.
- tezos-client-005-PsBabyM1-commands plugs the functions from tezos-client-005-PsBabyM1 as handlers for the alpha specific commands of the
dune-clientcommand line wallet.
- tezos-client-000-Ps9mPmXa contains the basic activator commands available on the genesis protocol.
- tezos-client-base-unix implements configuration file and wallet storage in Unix files, user interaction via the Unix console, and terminal based signer plug-ins.
The tests are split into various packages, testing more and more
elements while following the dependency chain. Use
make test to
tezos-client(in directory src/bin_client/test/): end-to-end tests as shell scripts that launch a local sandboxed node and performs various tasks using the client
tezos-p2p(in directory src/lib_p2p/test/): tests of the peer-to-peer layer, independently of the Dune gossip protocol (establishing connections, propagating peers, etc.)
tezos-protocol-environment(in directory src/lib_protocol_environment/test/): tests for the in-memory context implementation.
tezos-shell(in directory src/lib_shell/test/): tests for the chain data storage.
tezos-stdlib(in directory src/lib_stdlib/test/): tests for the basic data structures.
tezos-storage(in directory src/lib_storage/test/): tests for the versioned key-value context.
tezos-protocol-005-PsBabyM1(in directory src/proto_005_PsBabyM1/lib_protocol/test/): tests of the alpha protocol (without launching a node).
tezos-crypto(in directory src/lib_crypto/test/): tests for the in-house merkle trees.
tezos-data-encoding(in directory src/lib_data_encoding/test/): tests for the JSON and binary serialization and deserialization.
The Final Executables¶
- tezos-node provides the node launcher binary
dune-node. All the algorithmic being implemented in the shell, this package only implements the node’s CLI. It also provides the sandboxed node shell script launcher (see the main readme).
- tezos-client provides the
dune-admin-clientbinaries. The former contains a small command line wallet, the latter an administration tool for the node. It also provides a shell script that configures a shell environment to interact with a sandboxed node.
- tezos-baker-005-PsBabyM1 provides the
- tezos-endorser-005-PsBabyM1 provides the
- tezos-accuser-005-PsBabyM1 provides the
- tezos-protocol-compiler provides the
dune-protocol-compilerbinary that is used by the node to compile new protocols on the fly, and that can be used for developing new protocols.