当前位置:网站首页>108 Polkadot substrate: private network
108 Polkadot substrate: private network
2022-07-20 08:54:00 【565.ee】
• Introduce
• Generate your own key
• Generate a second set of keys
• Modify the existing chain specification
• Convert the chain specification to use the original format
• Start the first node
• Allow other participants to join
• Substrate Tutorials , Substrate course
• Contact Contact information
• Introduce
This tutorial explains how to use a set of private validators to start a small independent blockchain network .
All blockchains require the nodes in the network to agree on the message set and its order , To successfully create blocks and advance from one block to the next . Each block represents the data state at a specific point in time , The agreement of the node on this state is called consensus . There are several different algorithms for reaching consensus , Include :
• The workload proof consensus depends on the computational work done by the verifier node to add valid blocks to the chain .
• The equity certification consensus selects the verifier to add effective blocks to the chain according to the cryptocurrency holdings they lock into the equity in the network .
• Authoritative consensus evidence relies on a set of approved account identities to act as verifiers . The node associated with the approved account has the right to put the transaction into the block .
Substrate The node template uses the authoritative proof consensus model , Also known as authority wheel or Aura Consensus .Aura The consensus agreement limits the block production to the authorized account that creates the block in a circular way ( jurisdiction ) In the rotation list of .
In this tutorial , You will see how this consensus model works in practice , First , By using predefined accounts as part of the node template , Then add new permissions to the approval set .
• Generate your own key
Now you know how to use command line options to start and connect a running node as a peer , You can generate your own key , Instead of using a predefined account key . It's important to remember , Each participant in the blockchain network is responsible for generating a unique key .
You have used some command-line options to use predefined alice and bob The account starts the local blockchain node . You can also use the command line option to generate a random key to communicate with Substrate Use it together .
As best practice , When you generate a key for the production blockchain , You should use an air gap computer that is never connected to the Internet . At least , Before generating any keys that you intend to use on public or private blockchains that are not under your control , You should disconnect from the Internet .
however , For this tutorial , You can use node templatekey The subcommand generates a random key locally and keeps the same with Internet The connection of .
• Open the terminal housing on your computer .
• Switch to compile Substrate The root directory of the node template .
• Generate random key phrases and keys by running the following command :
./target/release/node-template key generate --scheme Sr25519 --password-interactive
• Type the password for the generated key .
This command generates the key 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
• Use the passphrase of the account you just generated Ed25519 Signature scheme derived key .
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"
• Type the password used to generate the key .
This 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
• Generate a second set of keys
• For this tutorial , The private network contains only two nodes , So you need two sets of keys . You have several options to continue this tutorial :
You can use the key for one of the predefined accounts .
You can repeat the steps in the previous section using a different identity on the local computer , To generate a second key pair .
You can derive a child key pair to simulate a second identity on the local computer .
You can recruit other participants to generate the keys needed to join your private network .
• For illustrative purposes , The second set of keys used in this tutorial is :
Sr25519:5EJPj83tJuJtTVE2v7B9ehfM7jNT44CBFaPWicvBwYyUKBS6 be used for aura.
Ed25519:5FeJQsfmbbJLTH1pvehBxrZrT5kHvJFj84ZaY5LK7NU87gZS by grandpa.
• Modify the existing chain specification
After generating the key for the blockchain , You can use these key pairs to create custom chain specifications , Then share your custom chain specification with trusted network participants called validators .
To enable others to participate in your blockchain network , You should make sure they generate their own keys . If other participants have already generated their key pairs , You can create custom chain specifications to replace local The chain specification you used before .
This tutorial explains how to create a two node network . You can follow the same steps to add more nodes to your network .
before , You use the command line options to use the predefined local Chain specification adds nodes to blockchain .–chain local No need to write a new chain specification , You can modify the specifications used before .
To create a new chain specification based on an existing specification :
• Open the terminal housing on your computer .
• Switch to compile Substrate The root directory of the node template .
• Export the local chain specification to customSpec.json Name the file by running the following command :
./target/release/node-template build-spec --disable-default-bootnode --chain local > customSpec.json
• customSpec.json Preview the first few fields in the file by running the following command :
head customSpec.json
This command displays the first field in the file . for example :
{
"name": "Local Testnet",
"id": "local_testnet",
"chainType": "Local",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"properties": null,
"consensusEngine": null,
"codeSubstitutes": {},
• customSpec.json Preview the last field in the file by running the following command :
tail -n 80 customSpec.json
This command shows Wasm The last part after the binary field , Include details of several pallets used in the runtime , for example sudo and balances Tray .
• customSpec.json Open the file in a text editor .
• modify aura Field to add... For each network participant Sr25519 SS58 Address key to specify the node that is authorized to create the block .
"aura": {
"authorities": [
"5CfBuoHDvZ4fd8jkLQicNL8tgjnK8pVG9AiuJrsNrRAx6CNW",
"5EJPj83tJuJtTVE2v7B9ehfM7jNT44CBFaPWicvBwYyUKBS6"
]
},
• Modify this grandpa Field to add... For each network participant Ed25519 SS58 Address key to specify the node authorized to complete the block .
"grandpa": {
"authorities": [
[
"5CuqCGfwqhjGzSqz5mnq36tMe651mU9Ji8xQ4JRuUTvPcjVN",
1
],
[
"5FeJQsfmbbJLTH1pvehBxrZrT5kHvJFj84ZaY5LK7NU87gZS",
1
]
]
},
• Convert the chain specification to use the original format
After you have prepared the chain specification containing the information you want to use , You must first convert it to the original specification , Then you can use it . The original chain specification contains the same information as the untransformed specification . however , The original chain specification also contains encoded storage keys that nodes use to reference data in their local storage . Distributing the original chain specification ensures that each node stores data with the correct storage key .
To convert the chain specification to use the original format :
Open the terminal housing on your computer .
Switch to compile Substrate The root directory of the node template .
By running the following command customSpec.json The chain specification is converted to the original format with the file name :customSpecRaw.json
./target/release/node-template build-spec --chain=customSpec.json --raw --disable-default-bootnode > customSpecRaw.json
• Start the first node
As the first participant in the private blockchain network , You are responsible for starting the first node , be called bootnode.
Start the first node :
• Open the terminal housing on your computer .
• Switch to compile Substrate The root directory of the node template .
• By running a command similar to the following , Start the first node using the custom chain specification :
./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
• Add the key to the keystore
After starting the first node , No blocks have been generated . The next step is to add two types of keys to the keystore of each node in the network .
For each node :
add to aura Permission key to enable block production .
add to grandpa Permission key to enable block completion .
To insert a key into the keystore :
• Open the terminal housing on your computer .
• Switch to compile Substrate The root directory of the node template .
• Insert... By running a command similar to the following aura Keys generated from subcommands :key
./target/release/node-template key insert --base-path /tmp/node01 \
--chain customSpecRaw.json \
--scheme Sr25519 \
--suri <your-secret-seed> \
--password-interactive \
--key-type aura
• Insert... By running a command similar to the following grandpa Keys generated from subcommands :key
./target/release/node-template key insert --base-path /tmp/node01 \
--chain customSpecRaw.json \
--scheme Ed25519 \
--suri <your-secret-key> \
--password-interactive \
--key-type gran
• node01 Verify that your key is in the keystore by running the following command :
ls /tmp/node01/chains/local_testnet/keystore
This command displays output similar to the following :
617572611441ddcb22724420b87ee295c6d47c5adff0ce598c87d3c749b776ba9a647f04
6772616e1441ddcb22724420b87ee295c6d47c5adff0ce598c87d3c749b776ba9a647f04
• Allow other participants to join
You can now use –bootnodes and –validator Command line options allow other authenticators to join the network .
Add a second authenticator to the private network :
• Open the terminal housing on the second computer .
• Switch to compile Substrate The root directory of the node template .
• Start 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
• Add... By running a command similar to the following aura Keys generated from subcommands :key
./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
• By running a command similar to the following ,grandpa Add the key generated from the subcommand to the local keystore :key
./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
• node02 Verify that your key is in the keystore by running the following command :
ls /tmp/node02/chains/local_testnet/keystore
This command displays output similar to the following :
617572611a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
6772616e1a4cc824f6585859851f818e71ac63cf6fdc81018189809814677b2a4699cf45
Substrate The node is inserting grandpa The key needs to be restarted , So you must shut down and restart the node before you see the block complete .
• Press Control-c Close the node .
• 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
Add their keys to their respective keystores at both nodes ( be located and Next )/tmp/node01 and /tmp/node02 After reboot , You should see the same Genesis block and state root hash .
You should also see that each node has a peer node (1 peers), And they generate a block proposal (best: #2 (0xe111…c084)). After a few seconds , You should see that the new block on both nodes is complete .
• Substrate Tutorials , Substrate course
CN chinese Github Substrate course : github.com/565ee/Substrate_CN
CN chinese CSDN Substrate course : blog.csdn.net/wx468116118
EN english Github Substrate Tutorials : github.com/565ee/Substrate_EN
EN english dev.to Substrate Tutorials : dev.to/565ee
• Contact Contact information
Homepage : 565.ee
WeChat official account : wx468116118
WeChat QQ : 468116118
GitHub : github.com/565ee
CSDN : blog.csdn.net/wx468116118
Email : [email protected]
边栏推荐
- 关于Fetch的基础
- 54-Object. Defineproperty method
- ThinkPHP5 验证码
- 项目代码编程规范(设计类和方法、变量、for语句格式 、while语句、 switch语句 、封装事务)
- 10S polkadot substrate : 建立代币合约
- Why are there tripartite payments?
- JS 获取当前日期与时间
- How to build your own fourth party payment platform?
- ES6语法(let、const、var 的区别、解构赋值、箭头函数、剩余参数、Array 的扩展方法)
- 10Q polkadot substrate : 使用map存储值
猜你喜欢
106 polkadot substrate : 无叉升级
正则表达式(用户名表单验证/验证座机号码/正则替换replace)
[JS] shallow copy and deep copy
基于C语言实现的学生管理系统
Page search highlighting function, the page automatically matches keywords, scrolls to the middle of the screen, and highlights.
91 pop up case - father passes son - son passes father
仿网易云音乐的YY音乐微信小程序源码
php json变数组问题
数据代理理解
简单的本地图片服务器的搭建
随机推荐
WordPress函数大全,学会了你就可以做主题了
无需编程,通过配置零代码生成CRUD RESTful API
VS2019修改背景+高度自定义字体颜色
接入聚合的两个核心功能介绍
mysql聚簇索引和非聚簇索引的区别
聚合支付滿足各行業接入多種支付通道
thinkphp6临时关闭布局
sequelize 常见默认值与列参数
仿网易云音乐的YY音乐微信小程序源码
【数据库基础】MySql基础总结
TS 联合类型
Using NVM use, exit status 1 and exit status 145 are garbled
The difference between payment on behalf and distribution
腾讯地图api 请求来源未被授权, 此次请求来源域名
慧眼识花草微信小程序源码/植物辨别微信小程序源码
Simple construction of local image server
ES6语法(let、const、var 的区别、解构赋值、箭头函数、剩余参数、Array 的扩展方法)
el-transfer 左侧右侧默认数据展示
性能测试基础笔试题
Page search highlighting function, the page automatically matches keywords, scrolls to the middle of the screen, and highlights.