substrate-knowledge-map

2022-05-05 Internal Training

Table of Content - [Substrate Knowledge Map](#substrate-knowledge-map) - [Topic 0: Navigating Substrate Documentation](#topic-0-navigating-substrate-documentation) - [Additional resources](#additional-resources) - [Quiz #1](#quiz-1) - [Topic 1: Run & Interact with Substrate](#topic-1-run--interact-with-substrate) - [Set up your local development environment](#set-up-your-local-development-environment) - [Lab #1](#lab-1) - [Interact with a Substrate network using Polkadot-JS apps](#interact-with-a-substrate-network-using-polkadot-js-apps) - [Quiz #2](#quiz-2) - [Lab #2](#lab-2) - [Lab #3](#lab-3) - [Topic 2: Substrate Development Preliminaries](#topic-2-substrate-development-preliminaries) - [Rust](#rust) - [How blockchains work](#how-blockchains-work) - [Quiz #3](#quiz-3) - [Topic 3: Substrate Runtime](#topic-3-substrate-runtime) - [High level architecture](#high-level-architecture) - [Quiz #4](#quiz-4) - [Runtime development topics](#runtime-development-topics) - [Lab #4](#lab-4) - [Lab #5](#lab-5) - [Lab #6](#lab-6) - [Quiz #5](#quiz-5) - [Topic 4: Running Node Template / Alt Producer together](#topic-4-running-node-template--alt-producer-together) - [Topic 5: Polkadot JS API](#topic-5-polkadot-js-api) - [Quiz #6](#quiz-6) - [Lab #7](#lab-7) - [Interacting with Node Template / Alt-producer using Polkadot JS-API](#interacting-with-node-template--alt-producer-using-polkadot-js-api) - [Topic 6: Basic Code Walkthrough for Node Template / Alt Producer](#topic-6-basic-code-walkthrough-for-node-template--alt-producer) - [Topic 7: Smart contracts](#topic-7-smart-contracts) - [Quiz #7](#quiz-7) - [What we do not cover](#what-we-do-not-cover) - [Others](#others) - [Appendix](#appendix) - [Appendix 1: Terms clarification](#appendix-1-terms-clarification) - [Appendix 2: Smart Contract vs Runtime Development](#appendix-2-smart-contract-vs-runtime-development)

Substrate Knowledge Map

The Substrate Knowledge Map provides information for you to develop a non-trivial application in Substrate.

Each section contains basic information on each topic, with links to additional documentation for you to dig deeper. Within each section, you’ll find a mix of quizzes and labs to test your knowledge as your progress through the map. The goal of the labs and quizzes is to help you consolidate what you’ve learned and put it to practice with some hands-on activities.

Topic 0: Navigating Substrate Documentation

If you need any community support, please look for support on Stack Overflow questions tagged with “substrate” or Substrate & Polkadot Stack Exchange.

Use the following links to explore the sites and resources available on each:

Substrate Developer Hub has the most comprehensive all-round coverage about Substrate, from a “big picture” explanation of architecture to specific technical concepts. The site also provides tutorials to guide you as your learn the Substrate framework and the API reference documentation. Check this site first if you want to look up information about Substrate runtime development.

The site consists of:

Developers are encouraged to start new Substrate projects based on these templates.

Additional resources

Quiz #1

👉 Submit your answers to Quiz #1

Topic 1: Run & Interact with Substrate

Set up your local development environment

Here you will set up your local machine to install the Rust compiler—ensuring that you have both stable and nightly versions installed. Both stable and nightly versions are required because currently a Substrate runtime is compiled to a native binary using the stable Rust compiler, then compiled to a WebAssembly (WASM) binary, which only the nightly Rust compiler can do.

Also refer to:

Lab #1

👉 Complete Lab #1: Run a Substrate node

Interact with a Substrate network using Polkadot-JS apps

Polkadot JS Apps is the canonical front-end to interact with any Substrate-based chain.

You can configure whichever endpoint you want it to connected to, even to your localhost running node. Refer to the following two diagrams.

  1. Click on the top left side showing your currently connected network:

assets/01-polkadot-app-endpoint.png

  1. Scroll to the bottom of the menu, open DEVELOPMENT, and choose either Local Node or Custom to specify your own endpoint.

assets/02-polkadot-app-select-endpoint.png

Quiz #2

👉 Complete Quiz #2

Lab #2

👉 Complete Lab #2: Using Polkadot-JS Apps

Notes: If you are connecting Apps to a custom chain (or your locally-running node), you may need to specify your chain’s custom data types in JSON under Settings > Developer.

Polkadot-JS Apps only receives a series of bytes from the blockchain. It is up to the developer to tell it how to decode and interpret these custom data type. To learn more on this, refer to:

You will also need to create an account. To do so, follow these steps on account generation. You’ll learn that you can also use the Polkadot-JS Browser Plugin (a Metamask-like browser extension to manage your Substrate accounts) and it will automatically be imported into Polkadot-JS Apps.

Notes: When you run a Substrate chain in development mode (with the --dev flag), well-known accounts (Alice, Bob, Charlie, etc.) are always created for you.

Lab #3

👉 Complete Lab #3: Create an Account

Topic 2: Substrate Development Preliminaries

You need to know some Rust programming concepts and have a good understanding on how blockchain technology works in order to make the most of developing with Substrate. The following resources will help you brush up in these areas.

Rust

You will need to familiarize yourself with Rust to understand how Substrate is built and how to make the most of its capabilities.

If you are new to Rust, or need a brush up on your Rust knowledge, please refer to The Rust Book. You could still continue learning about Substrate without knowing Rust, but we recommend you come back to this section whenever in doubt about what any of the Rust syntax you’re looking at means. Here are the parts of the Rust book we recommend you familiarize yourself with:

How blockchains work

Given that you’ll be writing a blockchain runtime, you need to know what a blockchain is, and how it works. The Web3 Blockchain Fundamental MOOC Youtube video series provides a good basis for understanding key blockchain concepts and how blockchains work.

The lectures we recommend you watch are: lectures 1 - 7 and lecture 10. That’s 8 lectures, or about 4 hours of video.

Quiz #3

👉 Complete Quiz #3

Topic 3: Substrate Runtime

High level architecture

To know more about the high level architecture of Substrate, please go through the Knowledge Base articles on Getting Started: Overview and Getting Started: Architecture.

In this document, you will develop a Substrate runtime with FRAME (v2). This is what a Substrate node consists of.

assets/03-substrate-architecture.png

Each node has many components that manage things like:

The runtime is particularly interesting because it contains the business logic (aka “state transition function”) that codifies the chain’s functionality. The runtime contains a collection of pallets that are configured to work together.

On the node level, Substrate leverages libp2p for the p2p networking layer and puts the transaction pool, consensus mechanism, and underlying data storage (a key-value database) on the node level. These components all work “under the hood”, and in this knowledge map we won’t cover them in detail except for mentioning their existence.

Quiz #4

👉 Complete Quiz #4

Runtime development topics

In our Developer Hub, we have a thorough coverage on various subjects you need to know to develop with Substrate. So here we just list out the key topics and reference back to Developer Hub. Please go through the following key concepts and the directed resources to know the fundamentals of runtime development.

Lab #4

👉 Complete Lab #4: Adding a Pallet into a Runtime

Notes: All of the above concepts we leverage on the #[pallet::*] macro to define them in the code. If you are interested to learn more about what other types of pallet macros exist go to the FRAME macro API documentation and this doc on some frequently used Substrate macros.

Lab #5

👉 Complete Lab #5: Building a Proof-of-Existence dApp

Lab #6

👉 Complete Lab #6: Building a Substrate Kitties dApp

Quiz #5

👉 Complete Quiz #5

Topic 4: Running Node Template / Alt Producer together

Some usual commands:

All the commands are (mostly) the same for both node-template and alt-producer.

Topic 5: Polkadot JS API

Polkadot JS API is the javascript API for Substrate. By using it you can build a javascript front end or utility and interact with any Substrate-based blockchain.

The Substrate Front-end Template is an example of using Polkadot JS API in a React front-end.

Quiz #6

👉 Complete Quiz #6: Using Polkadot-JS API

Lab #7

👉 Complete Lab #7: Using Polkadot-JS API

Interacting with Node Template / Alt-producer using Polkadot JS-API

Topic 6: Basic Code Walkthrough for Node Template / Alt Producer

Substrate Node Template

Alt Producer

Topic 7: Smart contracts

Learn about the difference between smart contract development vs Substrate runtime development, and when to use each here.

In Substrate, you can program smart contracts using ink!.

Quiz #7

👉 Complete Quiz #7: Using ink!

What we do not cover

A lot 😄

Others

Appendix

Appendix 1: Terms clarification

Appendix 2: Smart Contract vs Runtime Development

Smart Contract Development

Traditional smart contract platforms allow users to publish additional logic on top of some core blockchain logic. Since smart contract logic can be published by anyone, including malicious actors and inexperienced developers, there are a number of intentional safeguards and restrictions built around these public smart contract platforms. For example:

These safeguards and restrictions make running smart contracts slower and more costly. However, it’s important to consider the different developer audiences for contract development versus Substrate runtime development.

Building decentralized applications with smart contracts allows your community to extend and develop on top of your runtime logic without worrying about proposals, runtime upgrades, and so on. You can also use smart contracts as a testing ground for future runtime changes, but done in an isolated way that protects your network from any errors the changes might introduce.

In summary, smart contract development:

Substrate Runtime Development

Unlike traditional smart contract development, Substrate runtime development offers none of the network protections or safeguards. Instead, as a runtime developer, you have total control over how the blockchain behaves. However, this level of control also means that there is a higher barrier to entry.

Substrate is a framework for building blockchains, which almost makes comparing it to smart contract development like comparing apples and oranges. With the Substrate framework, developers can build smart contracts but that is only a fraction of using Substrate to its full potential.

With Substrate, you have full control over the underlying logic that your network’s nodes will run. You also have full access for modifying and controlling each and every storage item across your runtime modules. As you progress through this map, you’ll discover concepts and techniques that will help you to unlock the potential of the Substrate framework, giving you the freedom to build the blockchain that best suits the needs of your application.

You’ll also discover how you can upgrade the Substrate runtime with a single transaction instead of having to organize a community hard-fork. Upgradeability is one of the primary design features of the Substrate framework.

In summary, runtime development:

To learn more about using smart contracts within Substrate, refer to the Smart Contract - Overview page as well as the Polkadot Builders Guide.