medicinecoin / substrate-stencil

A template for kick starting a Substrate based PoS chain.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

substrate-stencil

A template for kick starting a Rust and Blockchain project using Substrate.

Features

This template includes the minimum required components to start a PoS testnet, inspired by substrate-node-template.

  • Consensus related pallets: Babe & GRANDPA
  • Staking related pallets: staking, session, authorship, im-online, offences, utility
  • Governance related pallets: collective, membership, elections-phragmen, democracy, treasure

Notes: The code is un-audited and not production ready, use it at your own risk.

๐Ÿšด Usage

๐Ÿ“ฆ Installation

Install Rust and Substrate dependencies:

curl https://getsubstrate.io -sSf | bash -s -- --fast

Install cargo generate:

cargo install cargo-generate

๐Ÿ› ๏ธ Generate & Build

Generate your project based the template:

cargo generate --git https://github.com/kaichaosun/substrate-stencil.git --branch v2.0.0 --name my-project

cd my-project

Build source code:

cargo build --release

๐Ÿƒ Run

Purge any existing developer chain state:

./target/release/my-project purge-chain --dev

Start a development chain with:

./target/release/my-project --dev

Detailed logs may be shown by running the node with the following environment variables set: RUST_LOG=debug RUST_BACKTRACE=1 cargo run -- --dev.

๐Ÿ•ธ๏ธ Multi-Node Local Testnet

If you want to see the multi-node consensus algorithm in action locally, then you can create a local testnet with two validator nodes for Alice and Bob, who are the initial authorities of the genesis chain that have been endowed with testnet units.

Optionally, give each node a name and expose them so they are listed on the Polkadot telemetry site.

You'll need two terminal windows open.

We'll start Alice's substrate node first on default TCP port 30333 with her chain database stored locally at /tmp/alice. The bootnode ID of her node is QmRpheLN4JWdAnY7HGJfWFNbfkQCb6tFf4vvA6hgjMZKrR, which is generated from the --node-key value that we specify below:

cargo run -- \
  --base-path /tmp/alice \
  --chain=local \
  --alice \
  --node-key 0000000000000000000000000000000000000000000000000000000000000001 \
  --telemetry-url 'ws://telemetry.polkadot.io:1024 0' \
  --validator

In the second terminal, we'll start Bob's substrate node on a different TCP port of 30334, and with his chain database stored locally at /tmp/bob. We'll specify a value for the --bootnodes option that will connect his node to Alice's bootnode ID on TCP port 30333:

cargo run -- \
  --base-path /tmp/bob \
  --bootnodes /ip4/127.0.0.1/tcp/30333/p2p/QmRpheLN4JWdAnY7HGJfWFNbfkQCb6tFf4vvA6hgjMZKrR \
  --chain=local \
  --bob \
  --port 30334 \
  --telemetry-url 'ws://telemetry.polkadot.io:1024 0' \
  --validator

Additional CLI usage options are available and may be shown by running cargo run -- --help.

๐Ÿณ Run in Docker

First, install Docker and Docker Compose.

Then run the following command to start a single node development chain.

./scripts/docker_run.sh

This command will firstly compile your code, and then start a local development network. You can also replace the default command (cargo build --release && ./target/release/node-template --dev --ws-external) by appending your own. A few useful ones are as follow.

# Run Substrate node without re-compiling
./scripts/docker_run.sh ./target/release/node-template --dev --ws-external

# Purge the local dev chain
./scripts/docker_run.sh ./target/release/node-template purge-chain --dev

# Check whether the code is compilable
./scripts/docker_run.sh cargo check

โšก Run public testnet

  • Modify the genesis config in chain_spec.rs
  • Build spec, ./target/release/<your-project-name> build-spec --chain staging > my-staging.json
  • Change original spec to encoded raw spec, ./target/release/<your-project-name> build-spec --chain=my-staging.json --raw > my-staging-raw.json
  • Start your bootnodes, node key can be generate with command ./target/release/substrate key generate-node-key.
    ./target/release/<your-project-name> \
         --node-key <your-node-key> \
         --base-path /tmp/bootnode1 \
         --chain my-staging-raw.json \
         --name bootnode1
  • Start your initial validators,
    ./target/release/<your-project-name> \
        --base-path  /tmp/validator1 \
        --chain   my-staging-raw.json \
        --bootnodes  /ip4/<your-bootnode-ip>/tcp/30333/p2p/<your-bootnode-peerid> \
        --port 30336 \
        --ws-port 9947 \
        --rpc-port 9936 \
        --name  validator1 \
        --validator 
  • Insert session keys
  • Attract enough validators from community in waiting
  • Call force_new_era in staking pallet with sudo, rotate to PoS validators
  • Enable governance, and remove sudo
  • Enable transfer and other functions

About

A template for kick starting a Substrate based PoS chain.

License:The Unlicense


Languages

Language:Rust 99.6%Language:Shell 0.4%