This file provides strict guidance to Codex when working with this Substrate blockchain repository. These rules are non-negotiable and must be followed exactly.
Torus is a stake-driven peer-to-peer network built on Substrate. The blockchain manages agents (validators and miners), token emissions, staking, and governance. Code quality is critical - runtime panics will halt the entire chain.
- @README.md - Project overview, quick start, network setup
- @CONTRIBUTING.md - Development setup (Nix), guidelines, testing
- @docs/pallet-structure.md - Architecture and API design
- @docs/xtask-manual.md - Development tooling guide
- @docs/linear-emission.md - Token distribution algorithm
torus0: Agent registration, staking, burn mechanisms, fee managementemission0: Token distribution with linear emission algorithm, weight controlgovernance: Proposals, voting, treasury, roles (allocators, curators)permission0: Permission and access control
The permission0 pallet manages delegated permissions and access control within the Torus network. Key components:
Core Permission Types (pallets/permission0/src/permission.rs):
PermissionContract<T>- Main permission structure with delegator, recipient, scope, duration, and enforcementPermissionId- Unique permission identifier (H256 hash)PermissionScope<T>- Defines what actions the permission coversNamespaceScope<T>- Defines namespace path permissions for delegation
Permission Scopes (pallets/permission0/src/permission/):
pallets/permission0/src/permission/curator.rs-CuratorPermissionsandCuratorScopetypespallets/permission0/src/permission/emission.rs-EmissionAllocation,DistributionControl, andEmissionScopetypes
Implementation Handlers (pallets/permission0/src/ext/):
pallets/permission0/src/ext/curator_impl.rs- Functions for curator permission enforcementpallets/permission0/src/ext/emission_impl.rs- Functions for emission permission enforcementpallets/permission0/src/ext/namespace_impl.rs- Functions for namespace permission enforcement
- API-first design: Each pallet has separate
apicrate to prevent circular dependencies - Domain separation: Complex logic split into focused modules (agent.rs, stake.rs, etc.)
- Storage efficiency: Use container types to minimize state size
- Zero-panic policy: Runtime code must NEVER panic under any circumstances
- All pallet tests are located within the /tests folder in each pallet's folder
# Development environment (REQUIRED - provides correct Rust version, dependencies)
nix develop
# Running tests and checks
just # Run all checks and tests (default)
just check # Clippy linting only
just test # Test suite only
cargo xtask coverage # Generate code coverage report
# Local development
cargo xtask run local --alice # Run local node with Alice account
cargo xtask generate-spec gen-new # Create new chain spec
cargo build --release # Build the node- MUST NEVER use
unwrap(),expect(),assert!(), or any panicking operations in pallet code - MUST ALWAYS use
ensure!macro for validation, NEVERassert! - MUST ALWAYS use the
?operator for error propagation - MUST ALWAYS use pattern matching with proper error handling:
let Some(value) = some_option else { return Err(Error::<T>::SomeError.into()); };
- MUST NEVER use raw arithmetic operators (
+,-,*,/) in runtime code - MUST ALWAYS use
saturating_add(),saturating_sub(),saturating_mul()for balance operations - MUST ALWAYS use
checked_div()for division - NEVER the/operator - MUST ALWAYS use
FixedU128for ALL percentage and ratio calculations - MUST ALWAYS handle overflow explicitly with
checked_*operations when needed
- MUST ALWAYS write functions as generic
pub fn name<T: Config>()rather thanimpl<T: Config> Pallet<T> - MUST ALWAYS use type aliases:
AccountIdOf<T>,BalanceOf<T>, etc. - MUST ALWAYS validate ALL inputs in private functions before storage operations
- MUST NEVER expose unsafe operations through public functions
- MUST ALWAYS use
try_mutatewhen the operation can fail - MUST ALWAYS check existence with
contains_keybefore accessing storage - MUST ALWAYS use
BoundedVecfor ALL storage collections - MUST ALWAYS validate data BEFORE storage mutations
- MUST NEVER perform multiple storage writes when one would suffice
- MUST ALWAYS use manual call indexing:
#[pallet::call_index(n)] - MUST ALWAYS specify weight info for ALL extrinsics
- MUST ALWAYS emit appropriate events after state changes
- MUST ALWAYS use
ensure_signed(origin)?at the start of signed extrinsics
- MUST ALWAYS document ALL storage items, extrinsics, errors, and public functions
- MUST ALWAYS use
///doc comments for items exported to client SDKs - MUST NEVER leave TODOs or incomplete implementations in production code
- MUST ALWAYS use
polkadot_sdkumbrella crate - NEVER individual substrate dependencies - MUST ALWAYS use
use crate::*for intra-pallet imports - MUST NEVER use wildcard imports except for preludes
- MUST ALWAYS use
BoundedVec<T, ConstU32<MAX>>for storage, NEVERVec<T> - MUST ALWAYS convert with error handling:
BoundedVec::try_from(vec)? - MUST ALWAYS use proper type conversions with
.try_into()and handle errors - MUST NEVER use
asfor lossy numeric conversions
- MUST ALWAYS emit events after successful state changes:
Pallet::<T>::deposit_event(Event::<T>::SomethingHappened(who, what));
- MUST ALWAYS validate string data is UTF-8:
ensure!(core::str::from_utf8(bytes).is_ok(), Error::<T>::InvalidUtf8);
- MUST ALWAYS check bounds before operations:
ensure!(value <= T::MaxValue::get(), Error::<T>::ValueTooLarge);
- MUST ALWAYS write comprehensive tests for ALL extrinsics
- MUST ALWAYS test error conditions and edge cases
- MUST ALWAYS benchmark ALL extrinsics for weight calculation
- MUST NEVER merge code without adequate test coverage
- MUST ALWAYS use
VersionedMigrationfor storage migrations - MUST ALWAYS increment storage version when changing storage
- MUST NEVER modify storage structure without a migration
- MUST ALWAYS test migrations with realistic data
- MUST ALWAYS run
cargo fmtbefore committing - MUST ALWAYS fix ALL clippy warnings
- MUST ALWAYS use descriptive variable names, no single letters
- MUST NEVER use repetitive and redundant comments within code
- MUST NEVER ignore compiler or clippy warnings with
#[allow(...)]
- MUST run
cargo fmt - MUST run
just checkand fix all warnings - MUST run
just testand ensure all pass - MUST run
cargo xtask coverageto verify coverage - MUST test runtime upgrades if storage changed