[Tutorial] Deploy a Substrate NPoS network in 3 minutes
Hi Polkadot developers:
In this article, we are going to explain how to set up a mining network like Polkadot based on Substrate template. Through this article, you will learn:
- Summary of Kusama/Polkadot launch procedure
- What is a NPoS (Nominated Proof of Stake) network? The difference between NPoS and PoA; and why do we need an NPoS network for mining(staking)?
- How to quickly configure the NPoS network based on the substrate-simple-template we created;
1. Summary of Polkadot launch procedure
In order to ensure the launch of network smooth, the Kusama and Polkadot network had adopted a phase-by-phase method with the following steps:
- Launch a network with PoA consensus
- Switch the consensus from PoA to PoS
- Enable the democracy module
- Remove sudo permissions
- Enable transfer and other modules
Generally speaking, in order to launch a network, developers should follow the same procedure, switching to PoS consensus only when the PoA network runs smooth and solid.
2. Comparison of Consensus
2.1 PoA
PoA stands for Proof of Authority, coined by Gavin Wood, co-founder of Ethereum and Parity Technologies. Validators are approved accounts recording transactions in blocks and receiving rewards and transaction fees in return.
In the Substrate official substrate-node-template repo, a consensus module called Aura is used to implement PoA, which generates blocks with a set of pre-determined authority accounts.
The Aura module is quick and easy for setup, and less error-prone, but the downside is that other people cannot easily participate in mining.
2.2 NPoS
NPoS stands for Nominated Proof of Stake. Let’s take Polkadot network as an example to explain the mining process, which is usually referred to as staking.
The mechanism is designed with two kinds of participants, validators and nominators. Participants interested in maining the network can run a node as validator, and nominators can parcipate by backing up trusted validator candidates with their DOTs. Both get rewarded by the network.
In Substrate, the NPoS consensus requires BABE (Blind Assignment for Blockchain Extension) module. For detailed information about BABE please check out Substrate.dev page.
Unlike PoA, NPoS consensus allows DOT holders to participate in mining.
2.3 Ways to launch a NPoS network
Since the official Substrate node template uses PoA, there are two ways for a developer to implement the NPoS consensus in Substrate.
a) Quick Start: use the simple Substrate node template we prepared in Github, which includes all Substrate default modules, plus the NPoS consensus composed of BABE and GRANDPA modules;
b) Custom and Complex: based on the PoA Substrate node template, manually add BABE module, staking configuration, and a few other related modules.
For the b), there’s a detailed tutorial “Substrate deployment of public test network” written by Kaichao from Parity. (Unfortunately there’s only a Chinese version.
3. Configure and launch the NPoS network
3.1 Introduction to substrate-simple-template
The substrate-simple-template is a code template based on Substrate, that helps developers quickly launch a NPoS network.
Note that since we don’t want to maintain code of each individual pallet, the pallets in this template references those in the official Substrate node template. Therefore, when there’s a new version of any pallet, you can simply bump the version number in the Cargo.toml of the corresponding pallet.
We have documented the configuration and startup procedures in the REAME.md file in the repo.
3.2 Quick Start
3.2.1 Download the source code
git clone git@github.com:OAK-Foundation/substrate-simple-template.git
3.2.2 Install Rust and required components
Follow the steps linked below to install the required libraries.
https://github.com/substrate-developer-hub/substrate-node-template/blob/master/docs/rust-setup.md
3.2.3 Configure Rust toolchain
Note that the latest nightly version of Rust should work. You can change the below nightly version in the command-line to the latest.
rustup default nightly-2021-03-01
rustup target add wasm32-unknown-unknown --toolchain nightly-2021-03-01
3.2.4 Compile the code
cargo build --release
3.3 Spin up multiple nodes
3.3.1 Set the validator’s session keys
Run the following command to generate 3 validator session public keys. Note that the 12-keyword should only be used for local development and not anywhere else.
SECRET="panda dose welcome ostrich brief pull lawn table arrest worth ranch faculty" sh scripts/prepare-test-net.sh 3
The output should be as below.
Replace the keys with vec![…] in node/cli/src/chain_spec.rs with above generated keys.
fn staging_testnet_config_genesis() -> GenesisConfig {
let initial_authorities: Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId)> = vec![...];
#--snip--
}
3.3.2 Generate a chain configuration file
// compile
cargo build --release// Generate configuration file
./target/release/substrate build-spec --disable-default-bootnode --chain staging > customSpec.json
The generated file includes the configuration for startup nodes, the genesisi block, and etc.
3.3.3 Configure the startup node
Generate the startup node’s public key with below command. Note that the 12-keyword should only be used for local development and not anywhere else.
subkey inspect --scheme ed25519 "fire penalty pony chase gift loan grid mule tape wrestle stuff salute"
The output should look like,
Start the node with the generated public key
./target/release/substrate --tmp --dev --node-key 0x74a8cfbadb5d2b0178ec124791bfa8346ac3550a4f689923c806428090055277
The output should look like,
Save the highlighted node identity “12D3Koo….” from the the screenshot.
Change “BootNodes”: [] section to below string in customSpec.json.
["/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWRm651Kd5GmsLTHJbgX5chQS5npx9ttLgo46UsegCMoNM"]
3.3.4 Configure sudo account
Now we generate a public key for the sudo account with below command. The 12-keyword should only be used for local development and not anywhere else.
subkey inspect --scheme sr25519 "royal novel glad piece waste napkin little pioneer decline fancy train sell"
The output should look like below.
Find palletSudo key in the customSpec.json and change it to,
"palletSudo": {
"key": "5Fk6QsYKvDXxdXumGdHnNQ7V7FziREy6qn8WjDLEWF8WsbU3"
},
Run the build command to re-compile.
cargo build --release
3.3.5 Generate a network config file from the customSpec.json
// Generate testnet.json
./target/release/substrate build-spec --disable-default-bootnode --chain customSpec.json > node/cli/res/testnet.json// recompile
cargo build --release
3.3.6 Start 3 validator nodes
// First validator node
./target/release/substrate --chain testnet -d data/validator1 --name validator1 --in-peers 256 --validator --ws-external --rpc-external --rpc-cors all --rpc-methods=unsafe --node-key 0x74a8cfbadb5d2b0178ec124791bfa8346ac3550a4f689923c80642809005527// Second validator node 2
./target/release/substrate --chain testnet -d data/validator2 --name validator2 --validator --port 30334 --ws-port 9946 --rpc-port 9934 --ws-external --rpc-external --rpc-cors all --rpc-methods=unsafe//Third validator node
./target/release/substrate --chain staging -d data/validator3 --name validator3 --validator --port 30335 --ws-port 9947 --rpc-port 9935 --ws-external --rpc-external --rpc-cors all --rpc-methods=unsafe
3.3.7 Set the node’s session keys
Run the command to set the node’s session keys
cd scripts/session_keys
sh run.sh
Restart the node with the commands in chapter 3.3.6, you can see that blocks are being finalized.
2021-06-11 09:40:45 ✨ Imported #3 (0xf18c…97f0)
2021-06-11 09:40:48 ✨ Imported #4 (0x4bdc…aa87)
2021-06-11 09:40:48 💤 Idle (2 peers), best: #4 (0x4bdc…aa87), finalized #1 (0xad8e…f0aa), ⬇ 5.0kiB/s ⬆ 4.8kiB/s
2021-06-11 09:40:51 ✨ Imported #5 (0x4064…6d67)
2021-06-11 09:40:53 💤 Idle (2 peers), best: #5 (0x4064…6d67), finalized #3 (0xf18c…97f0), ⬇ 2.5kiB/s ⬆ 2.2kiB/s
Once all three nodes producing blocks smoothly you have completed the tutorial and set up a network for staking!
Thanks for reading through the article dear friends. If you think it is helpful, please give us a thumb-up below!
3. References
- How does Polkadot’s NPoS mechanism work?
- Substrate.dev Knowledge Base
- Substrate node template repo on Github
Stay tuned for further information about the OAK Network here on Medium, our Twitter channel as well as in our Discord Server!
Website: http://oak.tech/
Email: contact@oak.tech