Radix Engine Architecture - Constraint Machine
Tags: blockchain, dlt, opensource, programming
Published: 2019-05-20 10:17:42

---
title: Radix Engine Architecture - Constraint Machine
published: true
description: 
tags: #blockchain #dlt #opensource #programming
---

# Introduction

This document will walk through the design logic behind the Constraint Machine, one of the two middle layers in the Radix Engine.

The Constraint Machine is a concept that takes Bitcoin's UTXO model and expands it for generic programming. This machine runs constraint validation against new atoms and historical data.

The other middle layer is the [Atom Model](https://docs.radixdlt.com/alpha/learn/architecture/atom-structure), which takes that core generic programming language and provides much easier abstractions for developing correct state machines.

> Tip: you can read more about the Atom Model structure in this [article](https://docs.radixdlt.com/alpha/learn/architecture/atom-structure).

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-Lexo8Dosy_DqedCT9Ss%2F20.png?alt=media&token=f06dde8e-b18c-4af9-aaab-6dd273dcbe80 "Radix Engine Layers")

# Constraint Machines vs. Computing Machines

Before we dig in, let's explain why we are moving away from the blockchain virtual machine (VM) that made Ethereum and EOS so popular.

At a high level, the traditional computing machine computes a state. Being so generic makes it incredibly powerful, but it brings a few issues:

-It’s difficult to prove correctness
-It’s difficult to state shard

A Constraint Machine on the other hand validates a new state. This is similar to how Bitcoin validates new UTXOs. Although this is a drastic reduction in capability compared to the traditional computing machine, we get the following nice properties:

-Proving correctness is a lot easier
-State sharding is almost built-in

We judge this to be the right tradeoff when creating a distributed ledger, since:

-"Computation" is not something needed as users always know what state they want to get to (we don't need a ledger to compute game physics)
-Provable correctness is much more important than generic programming
-Distributed throughput does not come from computing speed but rather from parallel state sharding

# State Machines

The main use-case for generic development on top of a ledger, as made popular by Ethereum's Smart Contract, is implementing correct State Machines. To this end, the Atom Model is designed to provide a simple interface between State Machine management and the Constraint Machine.

We will now present some State Machine fundamentals, as it’s important for a Radix developer to be aware of these concepts.

>A State Machine is a device which can be in one of a set number of stable conditions depending on its previous condition and on the present values of its inputs.

# State Machine definition
The definition of a state machine, defines its behavior and lifecycle. For example:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoCLQMubVWnA0zzXl%2F21.png?alt=media&token=db055d09-6318-490d-ab09-a05ad3e790c7 "Constraint Machine")

# State Machine instance

A State Machine instance is an actual instance of a state machine with a particular state. There could be more than one instance for a defined State Machine. For example, here are two instances of the state machine defined above:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoFJWzMEg1LJBW7b_%2F22.png?alt=media&token=3a50aa00-8e39-4201-82ec-543a31760555 "Constraint Machine")

# Quantitative vs. Qualitative transitions

As a State Machine designer it is also important to understand Extended State Machines or the notion of Qualitative and Quantitative aspects of a state machine.

# Quantitative Transition

These are transitions to the same state, where "extended state variables" of the state are updated. For example:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoHuL-BW0X22MrVsZ%2F23.png?alt=media&token=4f242ce1-be6c-4a5a-a6f4-5f4c69ade60f "Constraint Machine")

# Qualitative Transition

These are transitions to a different state. For example:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoK4avrJLF3AqsR4e%2F24.png?alt=media&token=0a44a999-0920-4d32-9f5f-81a9963981d9 "Constraint Machine")

# Particles

A particle can be defined as a "sub state of a global state", and it’s the fundamental block when developing on top of the Constraint Machine. When talking about particles there are two parts: a ParticleType and a ParticleInstance. The difference between the two is similar to the difference between State Machine Definitions and State Machine Instances. That is, a ParticleType defines the sub state and a ParticleInstance is an instance of that sub state.

>Tip: you can read more about Particles and Particle types in [this article](https://docs.radixdlt.com/alpha/learn/architecture/particle-types).

# Particle Spins

Particle spins are the key on how particles transition to other particles.
Let’s take as an example, transitioning from a "5 dollars in my pocket" state to a "5 dollars in your pocket".

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoRM8t-bDz2M61KPm%2F29.png?alt=media&token=c98813e6-4299-4ce7-b7bd-02ff7712d888 "Constraint Machine")

# How does the actual particle transition work?

Given that a particle represents a "sub-state", the spin of a particle represents whether or not it is "enabled" or not. In “Spin language”, UP (enabled) or DOWN (not enabled).
This means that a particle transition is represented by the current particle being spun DOWN and the next particle being spun UP.
Back with our example, the "5 dollars in my pocket" state becomes disabled while the "5 dollars in your pocket" state becomes enabled:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoUtN_2xn50LsaSZP%2F30.png?alt=media&token=895499f0-4a16-422f-a537-896bc1055001 "Constraint Machine")

Given this, low level particle state machine transition management comes into applying logic based on what particles must be spun DOWN for a particle to be spun UP.

# Particles and State Machines

Since particles represent a partial state which can be turned on and off, state machines are the perfect construct to manage particles.
There are two main State Machine Instance constructions, both of which use particles in different ways to represent a state:

-State Machine by index
-State Machine by transitions

>Note: certain state machine transitions may be dependent on the state of other state machine instances. Currently state dependency is only supported on Indexable State Machine Instances.

# Indexed State Machine instance

In an indexed State machine instance, every particle has an index associated with it, which represents which state machine instance it is a part of.

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoXho2e09DMKauQKi%2F25.png?alt=media&token=058f7b0d-e60d-4559-bbcb-0125bb0e1daf "Constraint Machine")

>Use case: used mostly for cases where the states of a state machine definition are all located in the same shard (they never moves in space). For example, TokenDefinitions.

# Transition State Machine instance

In a transition State machine instance, every particle has defined what is the required current particle to be transitioned from/to.

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoZlvF1XAUdIAykkB%2F26.png?alt=media&token=63a06ca2-4457-41f0-a017-ab04648281de "Constraint Machine")

>Use case: used for cases when a state machine instance can travel across shards. For example, TokenInstances.

# Indexed State Machine dependencies

An Indexed State Machine Instance transition can have dependencies on another Indexed State Machine Instance.

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoaoqBn-saEByHgcT%2F27.png?alt=media&token=301a548f-093b-4215-9024-3a8c17e53ec6 "Constraint Machine")

>Use case: used for cases where multiple state machine definitions in the same shard have dependencies on each other. For example, Minting Permissions.

# Spin State Machines vs. Particle State Machines

Do not confuse the Spin State machine with the higher level Particle State Machine as they are different abstractions. The Spin State Machine describes the substates lifecycle, while the Particle State Machine describes a generic application object's lifecycle.

Following our “5 dollars” example, the next diagram shows the different level of abstraction between a Particle State machine and a Spin State machine:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-Lexodg2SY7WBP3Kfr64%2F32.png?alt=media&token=2c37c316-1c90-454d-a901-3241a5d77cfe "Logo Title Text 1")

# Atom Model layer

The Constraint Machine is unaware of high-level abstractions such as ParticleTypes and state machines. Instead, it's language consists of: Atoms, Particles, Spins, Indices and Shards.

In order to fill the gap between high level scrypts and the constraint machine, the Atom Model layer converts the high level scrypts into two structures the Constraint Machine can understand:

1.ConstraintProcedures
2.ParticleTypeDefs

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexohSNRnr-97cC-Alg%2F28.png?alt=media&token=dcac7580-95ab-4bc8-9394-9834e1c7d108 "Constraint Machine")

# Constraint Procedures

Constraint Procedures rely on Spin State Machine validations to the Constraint Machine, as the Constraint Machine has no knowledge of Particle state machines. The translation between the two is one of the roles of the Atom Model layer.
Constraint Procedures look like:

>/**
 * Read-only interface into historic particle data
 */
public interface ParticleStore {
  /**
   * Retrieves all particles with the given index
   * TODO: How to manage memory? Should this be paginated or turned into an Observable?
   * @param index index to look up
   * @return list of particles under index
   */
  public List<SpunParticle> getSpuns(EUID index);
}
/**
 * Validates a new particleGroup given historical ledger data.
 * On call to validate, can assume that particleGroup has already passed Constraint Machine invariants.
 * NOTE: not thread-safe when accessing store
 */
public interface ConstraintProcedure {
  /**
   * Validates a particleGroup
   * @param particleGroup particleGroup to validate
   * @param atomMetaData metaData from atom
   * @param store historic ledger store of particles
   * @throws ValidationException when a particleGroup does not pass all constraint requirements
   */
  public void validate(ParticleGroup particleGroup, AtomMetaData atomMetaData, ParticleStore store) throws ValidationException;
}

# Particle Type Definitions

In order to retrieve particles for future validations, they must also be stored in a predictable manner. This is given by ParticleTypeDef, another construct which the Constraint Machine can understand and one which the Atom Model layer must produce from the ConstraintScrypts.
These definitions have the following structure:

>interface ParticleWrite {
  /**
   * Returns the indices under which this particle should be stored
   * TODO: This should always have the destination encoded in it, perhaps there is a way to combine Index and Destinations?
   * @return set of EUID indexes in which this particle should be stored under
   */
  Set<EUID> getIndices();
  /**
   * Returns the destinations (i.e. shard + key) the particle should be stored in
   * @return set of EUID destinations in which this particle should be stored under
   */
  Set<EUID> getDestinations();
}
interface ParticleTypeDef<T extends Particle> {
  Class<T> type();
  SpinStateMachine lifecycle();
  ParticleWrite mapToWrite(T t);
}

# System Calls to Particle Type definitions

The system calls are the mechanism to define the creation and management of state machines using particles. The Atom Model layer creates ParticleTypeDef's dynamically based on all the system calls issued. That is, it can tell which indices and destinations are required by the scrypts alone.

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexolXTcr8qtEHiroM7%2F33.png?alt=media&token=4a88c72f-9cfd-4d15-8c40-b416b679800c "Constraint Machine")

1.A Constraint Scrypt defines a certain ParticleType
2.A Constraint Scrypt defines constraints based on certain indexable data structures it needs
3.An indexable type was presumably defined in another scrypt to be used by other scrypts
4.A shardable type was also presumably defined in another scrypt to be used by other scrypts
5.Given the indexable type used by the constraint in 2, a mapping from an instance to it's destinations and indices can be created

# Constraint Machine

After the Atom Model layer converts all scrypts into ConstraintProcedures and ParticleTypeDefs, it passes all instances of these two structures to the Radix Constraint Machine.

Once the Radix Constraint Machine collects these instances, it can then validate and return write instructions for any new atom given some read-only Atom store.

Note that the Constraint Machine in this case does not update any state, it simply returns what the Atom Write should be without ever updating the Atom Store itself. Because of this, it is a purely functional component meaning that testing is much easier since there is no state maintenance to worry about.

At a high level this looks like:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-Lexoo2etT7Oyeo3BMvZ%2F34.png?alt=media&token=e15c70c5-6910-4cc4-b9c7-08485e90f998 "Constraint Machine")

# Particle groups and dependent state changes

Given that there are multiple particle state transitions possible in an Atom, each of which are possibly dependent on another (e.g. create a token and minting tokens for that token in the same atom) there needs to be a mechanism for determining which state is relevant during validation of a constraint procedure. This is defined by the array of particle groups in an atom. Validation occurs with the following rules:

-Each particle group is validated sequentially
-Once a particle group is validated successfully, the state change of that particle group is recorded and available for subsequent particle group validations as "virtual state" (only in memory not persisted to AtomStore)

This means that state transitions which are dependent on each other should not go into the same particle group.

An example of how particle group processing occurs follows:

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexorLsWt56p-aI-piL%2F35.png?alt=media&token=19e467da-3d16-470c-9f81-8401048e3657 "Constraint Machine")

First particle group is validated with current atom store

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-Lexp6e9Lkc1PBmjebIY%2F36.png?alt=media&token=cb2f5382-b024-4a60-b33a-8ead95bcaf36 "Constraint Machine")

Given that the first particle group passes validation, the second particle group is validated with current atom store + state of first particle group

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexoutGzdAl5Do1BlbU%2F37.png?alt=media&token=9e7b9d2e-2da8-4cea-9644-82c458e19ac8 "Constraint Machine")

The same pattern continues until all particle groups have been validated successfully. On any failure, the machine will exit early and throw a ValidationException.

# Constraint Machine validation

Aside from constraint procedure validations there are other validations dependent on the machine itself. These are all validated prior to Constraint procedures.

For example: maximum atom size, timestamp metadata requirement or maximum number of shards.

# Indexing and sharding

Once all validation has passed the Constraint Machine then returns an AtomWrite which describes how the Atom should be indexed and stored. The Constraint Machine itself never stores state and in this way it can be seen as a purely functional component.

The indexing and sharding of an atom is determined by the particles contained within an atom. Specifically, the particleType of the particle along with the instance maps to a set of indices and shards determined by the Atom Model layer based on the constraint scrypts which define the state machine represented by each ParticleType. This is represented by ParticleTypeDef provided by the Atom Model layer.

Once all of the ParticleWrites are resolved, these are combined to produce an AtomWrite which the constraint machine is able to pass back to inform [Tempo](https://docs.radixdlt.com/alpha/learn/whitepapers/tempo) how the atom should be stored.

If a ParticleType with no matching ParticleTypeDef is found, a Constraint Machine Validation exception is thrown.

![alt text](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LMPbV3hGbTEzGtYlH-m%2F-Lexnp-WzMRymxJsoUM6%2F-LexpEYVRI7mEgKfIW8p%2F38.png?alt=media&token=c4888546-ff53-42ba-a18d-168374cbe8b7 "Constraint Machine")

[Telegram](https://t.me/radix_dlt) for general chat
[​Discord](https://discord.gg/7Q7HSZZ) for developers chat
[​Reddit](https://reddit.com/r/radix) for general discussion
[Forum](https://forum.radixdlt.com/) for technical discussion
​[Twitter](https://twitter.com/radixdlt) for announcements
[​Email](https://radixdlt.typeform.com/to/nyKvMV) newsletter for weekly updates
Mail to [hello@radixdlt.com](hello@radixdlt.com) for general enquiries