Back to Home

Rust State Machine

Dot Code School Solutions

This repository contains my solutions for the DotCodeSchool Courses. It also contains some notes I made along the way.

1. State Machine Concept:

A state machine is a computational model that can be in exactly one of a finite number of states at any given time. It transitions between these states based on specific rules or inputs. In computing, state machines are used to model systems with well-defined, predictable behavior.

2. State in Blockchain

2.1 Global State:

Represents the entire data of the blockchain at a specific point in time. This includes account balances, smart contract data, and more.

2.2 State Transition Function:

The rules that define how the blockchain moves from one state to another based on transactions.

3. State Transitions:

4. Genesis State in Blockchains:

The Genesis State is the initial state of a blockchain. It represents the very first block, often referred to as "block 0" or the "genesis block." This block is unique because it is the only block that does not reference a previous block, as it is the starting point of the blockchain. It contains: Initial Accounts and Balances, Network Parameters, Smart Contracts and Code, and any Configuration Settings.

5. Traits

6. Pallet

"Pallet" is a term specific to the Polkadot SDK, which refers to Rust modules which contain logic specific for your blockchain runtime.

7. &'static str

In Rust, &'static str is a type that represents a string slice with a 'static lifetime. A 'static lifetime means that the data is either embedded directly in the program's binary (like string literals) or is explicitly allocated to last for the entire program's execution.

8. Blockchain Client / Host

The blockchain client is the software component responsible for managing the network interactions, consensus mechanism, peer-to-peer communication, transaction propagation, block production, and overall coordination of the blockchain nodes.

9. State transition function / Runtime

The state transition function defines how the blockchain's state changes in response to transactions and blocks. It encapsulates the business logic, rules, and operations that govern the blockchain's behavior. (If applicable) Manages the execution and state of smart contracts.

Blockchain Client vs STF Diagram

10. Enums

pub enum RuntimeCall {
    Balances(balances::Call<Runtime>),
}

pub enum Call<T: Config> {
    Transfer { to: T::AccountID, amount: T::Balance },
}

11. DotCodeSchool Lecture flow:

11.1 Balances Pallet:

11.2 System Pallet

11.3 Runtime Pallet

11.4 Using Named And Generic Types and making them configurable:

11.5 Support Pallet

11.5.1 Block

A block is basically broken up into two parts: the header and a vector of extrinsics.

11.5.1.1 Header

The block header contains metadata about the block which is used to verify that the block is valid. In our simple state machine, we only store the blocknumber in the header, but real blockchains like Polkadot have:

11.5.1.2 Extrinsics

11.5.2 Dispatching Calls

11.6 Proof of Existence Pallet

11.7 Macros in polkadot-sdk

All the pallets and the runtime follow a certain pattern and thus we can write and use macros for them.

11.7.1 The Call Macro

11.7.2 The Runtime Macro