Learn / Architecture

Subtensor Architecture

How Subtensor works under the hood. This guide explains the runtime architecture, what you can query, and what's computed on-chain.

What is Subtensor?

Subtensor is the blockchain layer of the Bittensor network. It's built on Substrate, the same framework that powers Polkadot, and handles:

Runtime Architecture

Subtensor is a Substrate runtime compiled to WebAssembly (WASM). The blockchain logic runs inside a WASM virtual machine on every node, so runtime upgrades can be deployed without restarting the network.

Subtensor Node
Runtime (WASM)
Compiled Rust logic
Upgradeable on-chain
Metadata
Describes the runtime
Queryable interface
RPC API
Storage (RocksDB)
Networking (litep2p)
Note

The metadata describes what you can call and query. The WASM runtime contains the actual logic. You can query metadata, but you can't query the Rust code directly.

What's in Metadata

Runtime metadata describes the interface to the blockchain – this is what you can query and interact with via RPC.

Pallets

Modules that group related functionality. SubtensorModule, Balances, System, etc.

View all pallets

Events

Notifications emitted when things happen. StakeAdded, NeuronRegistered, WeightsSet.

247 events across 27 pallets

Calls

Extrinsics you can submit. add_stake, register, set_weights, transfer.

263 calls across 27 pallets

Storage

On-chain state you can query. Alpha, Uids, Weights, TotalIssuance.

311 storage items across 27 pallets

Constants

Fixed values baked into the runtime. Can change on runtime upgrade.

127 constants across 27 pallets

Errors

Failure modes for calls. NotEnoughBalanceToStake, InvalidUid, etc.

326 errors across 27 pallets

Block Execution Flow

Each block goes through a defined execution flow. Understanding this helps you know when things happen.

1

on_initialize

Start of block. Epoch checks happen here. If tempo blocks have passed, epoch() runs.

2

Execute Extrinsics

User transactions are processed in order. Each call either succeeds (emits events) or fails (returns error). Storage is updated.

3

on_finalize

End of block. Cleanup and finalization. Block hash is computed.

4

Block Produced

Block is sealed and propagated to other nodes. ~12 second block time.

Epochs and Yuma Consensus

An epoch is a period after which rewards are calculated and distributed. Each subnet has its own tempo (epoch length in blocks).

What Happens Each Epoch

1
Collect Weights

Gather the weight matrix from all validators : who scored whom and how much.

2

Calculate stake -weighted median of weights. This clips outliers and finds consensus on miner scores. Runs in epoch/run_epoch.rs.

3
Compute Incentive & Dividends

Miners get incentive (based on their rank). Validators get dividends (based on their bonds/stake).

4
Distribute Emissions

TAO is minted and distributed to neurons based on their incentive + dividends. Total emission per block is ~0.5 TAO (post first halving, decreasing over time).

Yuma Math is Not in Metadata

The consensus algorithm is compiled Rust code – not queryable metadata. To understand the math, you need to read the source code in epoch/math.rs.

Key Storage Items

All of these values are stored on-chain and queryable via RPC. They differ in when they update: some change immediately when a user submits a transaction, others are recomputed by Yuma Consensus at each epoch boundary.

ValueUpdatesStorage Key
Stake amountOn transactionSubtensorModule.Alpha
Weight matrixOn transactionSubtensorModule.Weights
Neuron UIDOn transactionSubtensorModule.Uids
RankEach epochSubtensorModule.Rank
TrustEach epochSubtensorModule.Trust
IncentiveEach epochSubtensorModule.Incentive
DividendsEach epochSubtensorModule.Dividends
Pending emissionsEach blockSubtensorModule.PendingServerEmission, PendingValidatorEmission

Source Code Reference

The Subtensor runtime is open source. Key files for understanding the implementation:

Continue Learning