Cover....1
Title Page....2
Copyright and Credits....3
Dedicated....4
Contributors ....5
Table of Contents ....8
Preface....16
Part 1: Blockchains and Rust....22
Chapter 1: Blockchains with Rust....24
Laying the foundation with the building blocks of blockchains....24
Blocks....26
Hashes....27
Transactions....28
Security....29
Storage versus compute....30
Exploring the backbone of blockchains....31
Decentralization....31
Peers, nodes, validators, and collators....32
Consensus....34
Mining....36
Forking....37
Permissioned versus permissionless....38
Understanding decentralization....39
Replication....40
Governance....40
Cryptocurrencies and gas fees....40
Decentralized platforms....41
Tokens versus coins and ICOs....42
Smart contracts and NFTs....44
DAOs....45
Non-censorable apps....46
Digital assets with real-world limits....46
Scaling the blockchain....47
The blockchain trilemma....47
Sharding....48
Interoperability....49
Consensus for scale....49
Parallel processing....50
Layer 2s and side chains....50
ZK rollups and optimistic rollups....51
Introducing smart contracts....51
The future of the adoption of blockchains....52
Industries disrupted....52
Sociocultural and economic changes....52
Summary....53
Chapter 2: Rust – Necessary Concepts for Building Blockchains....54
Introducing Rust....55
The benefit of being statically typed....55
A dive into Rust’s applicability as a systems programming language....55
The reliability of Rust....56
The Rust ownership memory management model....57
Garbage collection....58
Speed and performance....58
Futures, error handling, and memory safety....59
Rust’s advantage for blockchains....59
Blockchains that use Rust....59
Foundry for Ethereum....60
The Fe, Move, and ink! languages....60
Interesting blockchain projects built with Rust....61
Advantages of Rust-based languages compared to Solidity....62
Learning basic Rust concepts....63
Variables and constants....63
Data types....65
Tuples and arrays....66
Numeric operations....67
Stack....68
Heap....68
V-tables....68
Slices....69
Strings....70
Enums....71
Exploring intermediate Rust concepts....73
Control flow....73
While loops....74
Functions....75
Match control flow....76
Structs....77
Vectors....78
Delving deep into advanced Rust concepts....80
Hashmaps....80
Ownership and borrowing....81
Crates, modules, and cargo....83
Summary....85
Part 2: Building the Blockchain....86
Chapter 3: Building a Custom Blockchain....88
Technical requirements....88
Windows installation....89
Mac installation....89
Ubuntu installation....89
VS Code....90
rust-analyzer....92
Cargo....93
Planning our first blockchain project....94
Structs....94
Required functions....99
Getting started with building the blockchain....104
Block....104
Creating the genesis block....108
Using helper functions....111
Exploring embedded databases....115
Summary....117
Chapter 4: Adding More Features to Our Custom Blockchain....118
Technical requirements....118
Connecting the blocks....119
Libraries powering blockchain operations....119
Blockchain functions....121
Starting the node server....133
The server....133
Server struct and implemented methods....134
Enums....136
Helper functions....137
The serve function....144
The Node struct....145
Summary....147
Chapter 5: Finishing Up Our Custom Blockchain....148
Technical requirements....148
Adding memory pools....149
Implementing a memory pool....149
The BlockinTransit implementation....152
Implementing transactions....154
Understanding TXInput transactions....154
Understanding TXOutput transactions....156
Understanding the Transaction implementation....158
Utilizing UTXOs and developing wallets....162
Implementing UTXOSet....162
Implementing wallets....164
Wallets....166
Setting up configurations and utilities....168
The Config implementation....169
Utility functions....170
Understanding the lib.rs file....171
Understanding the Main.rs file....173
Using your custom blockchain....175
Creating a new blockchain....176
Creating a new wallet....177
Checking the wallet balance....178
Starting a node....179
Sending currency....180
Listing all wallet addresses....181
Printing the blockchain....181
Rebuilding the UTXO set....182
Summary....184
Part 3: Building Apps....186
Chapter 6: Using Foundry to Build on Ethereum....188
Introducing Ethereum and Foundry....189
Understanding Ethereum....189
Why Rust and Foundry?....190
Installing Foundry....191
First steps with Foundry....192
Exploring Foundry....193
Working on an existing Foundry project....193
Dependencies....194
Project layout....196
Overview of Forge....197
Forge Standard Library overview....198
Forge commands....199
Understanding Foundry with Cast, Anvil, and Chisel....200
Overview of Cast....200
Overview of Anvil....201
Overview of Chisel....201
Cast, Anvil, and Chisel important commands....202
Testing and deployment....203
Writing tests....204
Fork and fuzz testing....206
Invariant and differential testing....207
Deployment and verification....208
Gas reports and snapshots....208
A project using Foundry....209
Getting started....210
A basic NFT....210
Testing the program....213
Gas reports....213
Summary....215
Chapter 7: Exploring Solana by Building a dApp....216
Introducing dApps....217
What are dApps?....217
Types of dApps....218
Benefits of dApps....219
Setting up the environment for Solana....220
Installing Rust....220
Introducing Solana....221
Why Solana?....223
Generating a local key pair....226
Working with Solana frameworks and tools....227
Introducing Anchor....227
Creating a new Anchor project....230
Building and deploying a dApp....231
Building and deploying with Anchor....231
Running a local ledger....232
Updating the program ID....234
Utilizing Anchor scripts....234
Testing your dApp....235
Creating accounts for our custom dApp....237
Defining accounts for our custom dApp....238
Implementation of message account structure....239
Understanding account sizing and rent in Solana....240
Sizing message accounts....240
Implementation in code....241
Creating our first instruction....242
Introduction to instruction creation....242
Establishing account constraints....245
Implementing logic....247
Safeguarding against invalid data....251
Instruction versus transaction....253
Creating tests for our instructions....254
Creating a client for tests....256
Sending a message....257
Summary....262
Chapter 8: Exploring NEAR by Building a dApp....264
Technical requirements....265
Prerequisites....265
Installation....265
Introducing NEAR....267
Why choose NEAR?....267
Understanding the foundational elements of NEAR....268
Learning about the advanced concepts of NEAR....275
Transactions and gas....275
Data flow....276
Tokens and avoiding loss....280
Storage options....281
Validators and consensus....284
NEAR SDK....284
Getting started with the NEAR blockchain....286
The Contract class....286
State and data structures....289
Transfers and actions....290
Cross contract calls....292
NEAR CLI deep dive....293
Creating our first project with NEAR....293
Understanding the structure and rules of the crossword game....294
Setting up the development environment....294
Creating a smart contract skeleton....295
Testing and deployment....298
Interacting with the contract....299
Summary....300
Part 4: Polkadot and Substrate....302
Chapter 9: Exploring Polkadot, Kusama, and Substrate....304
Introducing Polkadot....304
Interoperability....306
Relay chain....307
Parathreads....308
Bridges....308
Accounts....309
Transactions....309
Tokens and assets....313
NFTs....313
Understanding the core concepts of PolkaDot....314
XCM....314
Shared security....315
Pallets....316
Staking....317
Advanced staking concepts....318
Main actors....320
NPoS election algorithms....322
Learning about Kusama....323
Governance and on-chain upgrades....324
Chaos and experimentation....324
Introducing Substrate....325
Substrate architecture....325
Client and runtime....327
Network types....328
Node types....328
Diving deep into Substrate....329
Runtime interfaces....329
Core primitives....330
FRAME....331
Building custom pallets....332
Forkless and runtime upgrades....333
Consensus....334
Summary....337
Chapter 10: Hands-On with Substrate....338
Technical requirements....338
Installing Substrate....338
Building our own blockchain....339
Starting a local node....339
Installing a frontend template....342
Starting the frontend template....342
Transferring the funds....344
Simulating a network....346
Starting the first blockchain node....346
Adding more nodes....349
Verifying block production....350
Summary....352
Part 5: The Future of Blockchains....354
Chapter 11: Future of Rust for Blockchains....356
What the future looks like for Rust blockchains....356
Popular blockchains....357
Upcoming blockchains....360
Upcoming Rust Web3 projects....365
The Rust community....368
Jobs in the Web3 space....369
Popular job roles....369
How to find Web3 jobs....371
Building a career....372
Going beyond this book....373
Summary....374
Index....376
Other Books You May Enjoy....389
Save time, improve stability, and optimize program memory while building decentralized applications on a blockchain using the features and capabilities of Rust
Before Rust, blockchain developers didn’t have a systems programming language that was type-safe, fast, and, at the same time, had stable and easy memory management. Rust proved to be a boon for developers and works as the perfect solution for not only blockchain and protocol development but also dApp development.
Rust for Blockchain Application Development focuses on demonstrating solutions that can help blockchain developers productize decentralized applications using Rust, which is a complex language with a steep learning curve.
This book starts with basic Rust language concepts and then builds on these concepts to enable you to develop your own blockchain from scratch. As you progress, you’ll learn how to build dApps on popular chains like Solana and NEAR. You’ll also be guided through creating Ethereum dApps using Foundry (Rust). Finally, you’ll develop a custom blockchain using Substrate by Parity (Polkadot). The book provides a complete 360-degree view of Rust in the blockchain ecosystem.
By the end of this Rust book, you’ll have a thorough understanding of how to apply your Rust knowledge to building dApps and blockchains from scratch.
This Rust programming book is for blockchain developers interested in building dApps on popular blockchains using Rust. Blockchain architects wanting to save time required to go through documentation and understand each technology can also use this book as a quick-start guide. Experience in building applications on blockchain is required, and familiarity with Rust will be helpful but not necessary.