substrate create a custom chain Launch a Blockchain Add Alice to your network Create a new runtime module Upgrade our chain

展示如何构建一个 Substrate 区块链的 Runtime Library

  1. make sure node and npm is installed
  2. Rust and other dependencies for substrate
1
curl https://getsubstrate.io -sSf | bash

scripts for utils

进入到上面的项目中。

1
2
./substrate-node-new substrate-node-template <author-name>
./substrate-ui-new substrate

This will create a folder called substrate-node-template and substrate-ui with the corresponding repositories cloned in them.

Launch a Blockchain

If you have set up everything correctly, you can now start a substrate dev chain! In substrate-node-template run:

1
./target/release/substrate-node-template --dev

if you run into any errors starting or running your node, you may need to purge the chain files on your computer. You can do this by running:

1
2
3
4
// 优先使用这个
rm -rf ~/Library/Application Support/Substrate/chains/dev/
// or
cargo run -- purge-chain --dev

If everything is working it should start producing blocks!

To interact with the blockchain, you need to start the Substrate UI. Navigate to the substrate-ui folder and run:

1
npm run dev

Finally, if open your browser to http://localhost:8000, you should be able to interact with your new chain!

Add Alice to your network

Alice is a hard-coded account in the substrate system, which is pre-funded to make your life easier. Alice may already be added to your network if you used the latest version of the substrate-node-template.

1
2
3
4
5
6
subkey restore Alice

> Seed
> 0x416c696365202020202020202020202020202020202020202020202020202020 is account:
> SS58: 5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ
Hex: 0xd172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f

Then in the Substrate UI, you can go into the Wallet section and add Alice using her seed and name.
402e50b0.png

If all is working correctly, you can now go into the Send Funds section and send funds from Alice to Default. You will see that Alice has a bunch of units pre-funded in her account, so send some and wait for the green checkmark and an updated balance for Default to show that the transfer has been successfully recorded on the blockchain.

Create a new runtime module

Now it’s time to create our own runtime.

Open up the substrate-node-template folder and create a new file:

1
./runtime/src/demo.rs

This is where our new runtime module will live. Inline comments will hopefully give you insight to what the code is doing.

First, we will need to import a few libraries at the top of our file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Encoding library
use parity_codec::Encode;

// Enables access to the runtime storage
use srml_support::{StorageValue, dispatch::Result};

// Enables us to do hashing
use runtime_primitives::traits::Hash;

// Enables access to account balances and interacting with signed messages
use {balances, system::{self, ensure_signed}};

// Includes the standard rust imports for things like Vec
use rstd::prelude::*;

All modules need to expose a configuration trait. In this case, our trait inherits from the Balances module’s trait since we will be using features and functions made available to us there.

1
pub trait Trait: balances::Trait {}

Upgrade our chain