DEV Community

Cover image for 109 polkadot substrate : private network
565.ee
565.ee

Posted on

109 polkadot substrate : private network

introduce

Generate your own keys

Generate a second set of keys

Modify an existing chain specification

Convert the chain specification to use the raw format

Start the first node

Add keys to the keystore

Enable other participants to join

Substrate Tutorials , Substrate 教程

Contact 联系方式

• introduce

This tutorial illustrates how you can start a small, standalone blockchain network with an authority set of private validators.

All blockchains require the nodes in the network to agree on the set of messages and their order to successfully create blocks and progress from one block to the next. Each block represents the state of data at a specific point in time and the nodes' agreement on the state is called consensus. There are several different algorithms used to reach consensus, including the following:

• Proof of work consensus depends on the computational work done by validator nodes to add valid blocks to the chain.

• Proof of stake consensus selects the validators to add valid blocks to the chain based on the cryptocurrency holdings that they have locked up as a stake in the network.

• Proof of authority consensus relies on a set of approved account identities to act as validators. The nodes associated with approved accounts have the authority to put transactions into blocks.

The Substrate node template uses a proof of authority consensus model also referred to as authority round or Aura consensus. The Aura consensus protocol limits block production to a rotating list of authorized accounts—authorities—that create blocks in a round robin fashion.

In this tutorial, you'll see how this consensus model works in practice, first, by using the predefined accounts that are part of the node template, then by adding a new authority to the approved set.

• Generate your own keys

Now that you know how to start and connect running nodes as peers using command-line options, you are ready to generate your own secret keys instead of using the predefined account keys. It's important to remember that each participant in the blockchain network is responsible for generating unique keys.

You have already used some command-line options to start your local blockchain node using the predefined alice and bob accounts. You can also use command-line options to generate random keys to use with Substrate.

As a best practice, you should use an air-gapped computer that has never been connected to the internet when you generate keys for a production blockchain. At a minimum, you should disconnect from the internet before you generate any keys you intend to use on a public or private blockchain that is not under your control.

For this tutorial, however, you can use node template key subcommand to generate random keys locally and can remain connected to the internet.

• Open a terminal shell on your computer.

• Change to the root directory where you compiled the Substrate node template.

• Generate a random secret phrase and keys by running the following command:

./target/release/node-template key generate --scheme Sr25519 --password-interactive
Enter fullscreen mode Exit fullscreen mode

• Type a password for the generated keys.

The command generates keys and displays output similar to the following:

Secret phrase:  pig giraffe ceiling enter weird liar orange decline behind total despair fly
Secret seed:       0x0087016ebbdcf03d1b7b2ad9a958e14a43f2351cd42f2f0a973771b90fb0112f
Public key (hex):  0x1a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
Account ID:        0x1a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
Public key (SS58): 5CfBuoHDvZ4fd8jkLQicNL8tgjnK8pVG9AiuJrsNrRAx6CNW
SS58 Address:      5CfBuoHDvZ4fd8jkLQicNL8tgjnK8pVG9AiuJrsNrRAx6CNW
Enter fullscreen mode Exit fullscreen mode

• Use the secret phrase for the account you just generated to derive keys using the Ed25519 signature scheme.

For example, run a command similar to the following:

./target/release/node-template key inspect --password-interactive --scheme Ed25519 "pig giraffe ceiling enter weird liar orange decline behind total despair fly"
Enter fullscreen mode Exit fullscreen mode

• Type the password you used to generate keys.

The command displays output similar to the following:

Secret phrase `pig giraffe ceiling enter weird liar orange decline behind total despair fly` is account:
Secret seed:       0x0087016ebbdcf03d1b7b2ad9a958e14a43f2351cd42f2f0a973771b90fb0112f
Public key (hex):  0x2577ba03f47cdbea161851d737e41200e471cd7a31a5c88242a527837efc1e7b
Public key (SS58): 5CuqCGfwqhjGzSqz5mnq36tMe651mU9Ji8xQ4JRuUTvPcjVN
Account ID:        0x2577ba03f47cdbea161851d737e41200e471cd7a31a5c88242a527837efc1e7b
SS58 Address:      5CuqCGfwqhjGzSqz5mnq36tMe651mU9Ji8xQ4JRuUTvPcjVN
Enter fullscreen mode Exit fullscreen mode

• Generate a second set of keys

• For this tutorial, the private network consists of just two nodes, so you need two sets of keys. You have a few options to continue the tutorial:

You can use the keys for one of the predefined accounts.

You can repeat the steps in the previous section using a different identity on your local computer to generate a second key pair.

You can derive a child key pair to simulate a second identity on your local computer.

You can recruit other participants to generate the keys required to join your private network.

• For illustration purposes, the second set of keys used in this tutorial are:

Sr25519: 5EJPj83tJuJtTVE2v7B9ehfM7jNT44CBFaPWicvBwYyUKBS6 for aura.
Ed25519: 5FeJQsfmbbJLTH1pvehBxrZrT5kHvJFj84ZaY5LK7NU87gZS for grandpa.

• Modify an existing chain specification

After you generate the keys to use with your blockchain, you are ready to create a custom chain specification using those key pairs then share your custom chain specification with trusted network participants called validators.

To enable others to participate in your blockchain network, you should ensure that they generate their own keys. If other participants have generated their key pairs, you can create a custom chain specification to replace the local chain specification that you used previously.

This tutorial illustrates how to create a two-node network. You can follow the same steps to add more nodes to your network

Previously, you added nodes to the blockchain using the predefined local chain specification using the --chain local command-line option. Instead of writing a completely new chain specification, you can modify the one that you have used before.

To create a new chain specification based on an existing specification:

• Open a terminal shell on your computer.

• Change to the root directory where you compiled the Substrate node template.

• Export the local chain specification to a file named customSpec.json by running the following command:

./target/release/node-template build-spec --disable-default-bootnode --chain local > customSpec.json
Enter fullscreen mode Exit fullscreen mode

• Preview the first few fields in the customSpec.json file by running the following command:

head customSpec.json
Enter fullscreen mode Exit fullscreen mode

The command displays the first fields from the file. For example:

{
  "name": "Local Testnet",
  "id": "local_testnet",
  "chainType": "Local",
  "bootNodes": [],
  "telemetryEndpoints": null,
  "protocolId": null,
  "properties": null,
  "consensusEngine": null,
  "codeSubstitutes": {},
Enter fullscreen mode Exit fullscreen mode

• Preview the last fields in the customSpec.json file by running the following command:

tail -n 80 customSpec.json
Enter fullscreen mode Exit fullscreen mode

This command displays the last sections following the Wasm binary field, including the details for several of the pallets—such as the sudo and balances pallets—that are used in the runtime.

• Open the customSpec.json file in a text editor.

• Modify aura field to specify the nodes with the authority to create blocks by adding the Sr25519 SS58 address keys for each network participant.

"aura": {
    "authorities": [
      "5CfBuoHDvZ4fd8jkLQicNL8tgjnK8pVG9AiuJrsNrRAx6CNW",
      "5EJPj83tJuJtTVE2v7B9ehfM7jNT44CBFaPWicvBwYyUKBS6"
    ]
  },
Enter fullscreen mode Exit fullscreen mode

• Modify the grandpa field to specify the nodes with the authority to finalize blocks by adding the Ed25519 SS58 address keys for each network participant.

"grandpa": {
    "authorities": [
      [
        "5CuqCGfwqhjGzSqz5mnq36tMe651mU9Ji8xQ4JRuUTvPcjVN",
        1
      ],
      [
        "5FeJQsfmbbJLTH1pvehBxrZrT5kHvJFj84ZaY5LK7NU87gZS",
        1
      ]
    ]
  },
Enter fullscreen mode Exit fullscreen mode

• Convert the chain specification to use the raw format

After you prepare a chain specification with the information you want to use, you must convert it into a raw specification before it can be used. The raw chain specification includes the same information as the unconverted specification. However, the raw chain specification also contains encoded storage keys that the node uses to reference the data in its local storage. Distributing a raw chain specification ensures that each node stores the data using the proper storage keys.

To convert a chain specification to use the raw format:

Open a terminal shell on your computer.

Change to the root directory where you compiled the Substrate node template.

Convert the customSpec.json chain specification to the raw format with the file name customSpecRaw.json by running the following command:

./target/release/node-template build-spec --chain=customSpec.json --raw --disable-default-bootnode > customSpecRaw.json
Enter fullscreen mode Exit fullscreen mode

• Start the first node

As the first participant in the private blockchain network, you are responsible for starting the first node, called the bootnode.

To start the first node:

• Open a terminal shell on your computer.

• Change to the root directory where you compiled the Substrate node template.

• Start the first node using the custom chain specification by running a command similar to the following:

./target/release/node-template \
--base-path /tmp/node01 \
--chain ./customSpecRaw.json \
--port 30333 \
--ws-port 9945 \
--rpc-port 9933 \
--telemetry-url "wss://telemetry.polkadot.io/submit/ 0" \
--validator \
--rpc-methods Unsafe \
--name MyNode01
Enter fullscreen mode Exit fullscreen mode

• Add keys to the keystore

After you start the first node, no blocks are yet produced. The next step is to add two types of keys to the keystore for each node in the network.

For each node:

Add the aura authority keys to enable block production.

Add the grandpa authority keys to enable block finalization.

To insert keys into the keystore:

• Open a terminal shell on your computer.

• Change to the root directory where you compiled the Substrate node template.

• Insert the aura secret key generated from the key subcommand by running a command similar to the following:

./target/release/node-template key insert --base-path /tmp/node01 \
--chain customSpecRaw.json \
--scheme Sr25519 \
--suri <your-secret-seed> \
--password-interactive \
--key-type aura
Enter fullscreen mode Exit fullscreen mode

• Insert the grandpa secret key generated from the key subcommand by running a command similar to the following:

./target/release/node-template key insert --base-path /tmp/node01 \
--chain customSpecRaw.json \
--scheme Ed25519 \
--suri <your-secret-key> \
--password-interactive \
--key-type gran
Enter fullscreen mode Exit fullscreen mode

• Verify that your keys are in the keystore for node01 by running the following command:

ls /tmp/node01/chains/local_testnet/keystore
Enter fullscreen mode Exit fullscreen mode

The command displays output similar to the following:

617572611441ddcb22724420b87ee295c6d47c5adff0ce598c87d3c749b776ba9a647f04
6772616e1441ddcb22724420b87ee295c6d47c5adff0ce598c87d3c749b776ba9a647f04
Enter fullscreen mode Exit fullscreen mode

• Enable other participants to join

You can now allow other validators to join the network using the --bootnodes and --validator command-line options.

To add a second validator to the private network:

• Open a terminal shell on a second computer.

• Change to the root directory where you compiled the Substrate node template.

• Start a second blockchain node by running the following command:

./target/release/node-template \
--base-path /tmp/node02 \
--chain ./customSpecRaw.json \
--port 30334 \
--ws-port 9946 \
--rpc-port 9934 \
--telemetry-url "wss://telemetry.polkadot.io/submit/ 0" \
--validator \
--rpc-methods Unsafe \
--name MyNode02 \
--bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWLmrYDLoNTyTYtRdDyZLWDe1paxzxTw5RgjmHLfzW96SX \
--password-interactive
Enter fullscreen mode Exit fullscreen mode

• Add the aura secret key generated from the key subcommand by running a command similar to the following:

./target/release/node-template key insert --base-path /tmp/node02 \
--chain customSpecRaw.json \
--scheme Sr25519 \
--suri <second-participant-secret-seed> \
--password-interactive \
--key-type aura
Enter fullscreen mode Exit fullscreen mode

• Add the grandpa secret key generated from the key subcommand to the local keystore by running a command similar to the following:

./target/release/node-template key insert --base-path /tmp/node02 \
--chain customSpecRaw.json \
--scheme Ed25519 \
--suri <second-participant-secret-seed> \
--password-interactive \
--key-type gran
Enter fullscreen mode Exit fullscreen mode

• Verify that your keys are in the keystore for node02 by running the following command:

ls /tmp/node02/chains/local_testnet/keystore
Enter fullscreen mode Exit fullscreen mode

The command displays output similar to the following:

617572611a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
6772616e1a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
Enter fullscreen mode Exit fullscreen mode

Substrate nodes require a restart after inserting a grandpa key, so you must shut down and restart nodes before you see blocks being finalized.

• Shut down the node by pressing Control-c.

• Restart the second blockchain node by running the following command:

./target/release/node-template \
--base-path /tmp/node02 \
--chain ./customSpecRaw.json \
--port 30334 \
--ws-port 9946 \
--rpc-port 9934 \
--telemetry-url "wss://telemetry.polkadot.io/submit/ 0" \
--validator \
--rpc-methods Unsafe \
--name MyNode02 \
--bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWLmrYDLoNTyTYtRdDyZLWDe1paxzxTw5RgjmHLfzW96SX \
--password-interactive
Enter fullscreen mode Exit fullscreen mode

After both nodes have added their keys to their respective keystores—located under /tmp/node01 and /tmp/node02—and been restarted, you should see the same genesis block and state root hashes.

You should also see that each node has one peer (1 peers), and they have produced a block proposal (best: #2 (0xe111…c084)). After a few seconds, you should see new blocks being finalized on both nodes.

• Substrate Tutorials , Substrate 教程

CN 中文 Github Substrate 教程 : github.com/565ee/Substrate_CN

CN 中文 CSDN Substrate 教程 : blog.csdn.net/wx468116118

EN 英文 Github Substrate Tutorials : github.com/565ee/Substrate_EN

EN 英文 dev.to Substrate Tutorials : dev.to/565ee

• Contact 联系方式

Homepage : 565.ee

GitHub : github.com/565ee

Email : 565.eee@gmail.com

Facebook : facebook.com/565.ee

Twitter : twitter.com/565_eee

Telegram : t.me/ee_565

Top comments (0)