## Module blockchain_state_machines

Source
Expand description

Learn about the way Substrate and FRAME view their blockchains as state machines.

## §State Transition Function

This document briefly explains how in the context of Substrate-based blockchains, we view the blockchain as a decentralized state transition function.

Recall that a blockchain’s main purpose is to help a permissionless set of entities to agree on a shared data-set, and how it evolves. This is called the State, also referred to as “onchain” data, or Storage in the context of FRAME. The state is where the account balance of each user is, for example, stored, and there is a canonical version of it that everyone agrees upon.

Then, recall that a typical blockchain system will alter its state through execution of blocks. The component that dictates how this state alteration can happen is called the state transition function.

    flowchart LR
    	B[Block] --> STF
        S[State] --> STF
        STF --> NS[New State]

In Substrate-based blockchains, the state transition function is called the Runtime. This is explained further in `crate::reference_docs::wasm_meta_protocol`.

With this in mind, we can paint a complete picture of a blockchain as a state machine:

    flowchart LR
        %%{init: {'flowchart' : {'curve' : 'linear'}}}%%
        subgraph BData[Blockchain Database]
            direction LR
            BN[Block N] -.-> BN1[Block N+1]
        end

        subgraph SData[State Database]
            direction LR
            SN[State N] -.-> SN1[State N+1] -.-> SN2[State N+2]
        end

        BN --> STFN[STF]
        SN --> STFN[STF]
        STFN[STF] --> SN1

        BN1 --> STFN1[STF]
        SN1 --> STFN1[STF]
        STFN1[STF] --> SN2

In essence, the state of the blockchain at block N is the outcome of applying the state transition function to the previous state, and the current block as input. This can be mathematically represented as:

    STF = F(State_N, Block_N) -> State_{N+1}