I build a simple rust state machine, following the tutorial: "https://www.shawntabrizi.com/rust-state-machine/"
This project is a simple blockchain-style state machine written in Rust. It follows the tutorial from Shawn Tabrizi and includes a minimal runtime with:
systempallet: block numbers and account noncesbalancespallet: transfers between accountsproof_of_existencepallet: create and revoke ownership claims
src/main.rs: runtime wiring and block execution examplessrc/system.rs: system-level statesrc/balances.rs: balances and transfer logicsrc/proof_of_existence.rs: claim and revoke logicsrc/support.rs: shared types (Block,Header,Extrinsic,Dispatch)
- Rust (stable)
- Cargo
cargo runRun all tests:
cargo testRun tests by module:
cargo test --lib system::test:: -- --nocapture
cargo test --lib balances::tests:: -- --nocapture
cargo test --lib proof_of_existence::test:: -- --nocapture-
You can intentionally add invalid extrinsics in
main.rsto demonstrate runtime error handling. -
The tutorial uses
BTreeMapinstead ofHashMapto keep state deterministic (sorted keys) and trait bounds simple (Ord). -
The
macroscrate comes from the tutorial; I did not implement those macros myself. -
I added
src/lib.rsso module tests can be run as library tests (for example,cargo test --lib system::test::). -
We could add transaction fees (gas) by deducting a small amount from the caller for each extrinsic execution (and other blockchain features), but this tutorial does not implement fees (Nobody likes fees).
-
Why can
cargo runshowExtrinsic Errorbut still finish successfully? In this project, an extrinsic failure is logged, but block execution continues. Insrc/main.rs:62, each dispatch result is captured as_resand errors are only printed viamap_err(...). The block still returnsOk(())after processing extrinsics (unless the block number check fails insrc/main.rs:54). This is because we do not want to panic and stop the blockchain because one extrinsic fails