Written in front:
Changan chain documentation: https://docs.chainmaker.org.cn/
This series of tutorials is based on the official documents of Changan Chain. A lot of text descriptions I directly copied the official documents, I just sorted out my own use process. Also welcome to leave a message to discuss.
Start the chain via the command line tool
1.1. Overview
Through this article, you will be able to build a multi-node cluster of Changan chain, and use command line tools and SDK to complete the experience of Changan chain functions.
The following steps are required to build the Changan chain through source code and search data on the chain. This article will demonstrate them one by one.
- Download the source code of Changan chain and certificate management tool
- Compile the source code
- Generate node certificate
- Compilation and installation package production
- Start the cluster of nodes
- View node status
- Use the CMC tool to install, call, and query a contract
- Use the GO SDK to verify the contract
2.2. Environment dependencies
2.2.1. Hardware dependencies
configuration | minimum configuration | Recommended configuration |
---|---|---|
CPU | 1.5GHz | 2.4GHz |
Memory | 8GB | 16GB |
core | 4 Nuclear | 8 cores |
bandwidth | 2Mb | 10Mb |
2.2.2. Software dependencies
The current document is completed under centos7.6 operation. The following is the list of dependent software required for this demonstration:
name | Version | describe | Is it necessary |
---|---|---|---|
git | / | source code management | yes |
golang | 1.16+ | Compiler Environment | yes |
docker | 18+ | Run the container independently | no |
docker-compose | / | container management component | no |
gcc | 7.3+ | Compilation environment dependencies | yes |
glibc | 2.18 | Smart contract execution environment depends on | yes |
tmux | / | The default quickstart command depends on | no |
The wasmer runtime library libwasmer_runtime_c_api.so | / | The library is in chainmaker-go/main the directory, add the library path to the system PATH environment variable, and the startup script is included by default. The following configuration is required for separate startup: cd deployPath/lib cp xxx/main/libwasmer_runtime_c_api.so libwasmer.so export LD_LIBRARY_PATH=deployPath/lib :$LD_LIBRARY_PATH |
yes |
2.3. Environment construction
The following will introduce 使用脚本搭建
and 使用Docker搭建
two ways to build the environment, just choose one.
2.3.1. Using scripts to build
apply to
Linux
,MacOS
2.3.1.1. Source code download
Download the source code from the official website of Changan Chain : https://git.chainmaker.org.cn/chainmaker/chainmaker-go
It is currently a private warehouse, and account registration is required first
- Download
chainmaker
source code to local
$ git clone https://git.chainmaker.org.cn/chainmaker/chainmaker-go.git
- Download the source code of the certificate generation tool to the local
$ git clone -b v2.2.0 https://git.chainmaker.org.cn/chainmaker/chainmaker-cryptogen.git
Download to the local directory you want to place:
2.3.1.2. Source code compilation
- Compile the certificate generation tool
$ cd chainmaker-cryptogen
$ make
You can also view the local project directory:
2.3.1.3. Configuration file generation
- Will compiled
chainmaker-cryptogen
, soft link tochainmaker-go/tools
the directory
# 进入工具目录
$ cd chainmaker-go/tools
# 软连接chainmaker-cryptogen到tools目录下
$ ln -s ../../chainmaker-cryptogen/ .
- After version 2.1, ChainMaker supports multiple identity modes. Since the directory structure and content of configuration files vary greatly under different identity modes, we provide different scripts for generating configuration files. According to the scenario, select the required identity mode PermissionedWithCert, PermissionedWithKey, Public (see Identity and Rights Management for details ), and choose one of the following scripts :
Identity mode Here, I first used PermissionedWithCert, but it needs to add users to the organization. Later I changed to Public mode.
2.3.1.3.3. Public
Newly added identity mode after version 2.1, see identity rights management for details
Enter chainmaker-go/scripts
the directory, execute prepare_pk.sh
the script to generate a single-chain 4-node cluster configuration, which is stored in the chainmaker-go/build
path
prepare_pk.sh
The script supports generating public and private keys and configurations of 4/7/10/13/16 nodes
# 进入脚本目录
$ cd ../scripts
# 查看脚本帮助
$ ./prepare_pk.sh -h
Usage:
prepare_pk.sh node_cnt(4/7/10/13/16) chain_cnt(1-4) p2p_port(default:11301) rpc_port(default:12301)
eg1: prepare_pk.sh 4 1
eg2: prepare_pk.sh 4 1 11301 12301
# 生成单链4节点集群的密钥和配置
$ ./prepare_pk.sh 4 1
begin check params...
begin generate keys, cnt: 4
input consensus type (5-DPOS(default)):
input log level (DEBUG|INFO(default)|WARN|ERROR):
input hash type (SHA256(default)|SM3):
enable docker vm (YES|NO(default))
config node total 4
begin generate node1 config...
begin generate node2 config...
begin generate node3 config...
begin generate node4 config...
# 查看生成好的节点密钥和配置
$ tree -L 3 ../build/
../build/
├── config
│ ├── node1
│ │ ├── admin
│ │ ├── chainconfig
│ │ ├── chainmaker.yml
│ │ ├── log.yml
│ │ ├── node1.key
│ │ ├── node1.nodeid
│ │ ├── node1.pem
│ │ └── user
│ ├── node2
│ │ ├── admin
│ │ ├── chainconfig
│ │ ├── chainmaker.yml
│ │ ├── log.yml
│ │ ├── node2.key
│ │ ├── node2.nodeid
│ │ ├── node2.pem
│ │ └── user
│ ├── node3
│ │ ├── admin
│ │ ├── chainconfig
│ │ ├── chainmaker.yml
│ │ ├── log.yml
│ │ ├── node3.key
│ │ ├── node3.nodeid
│ │ ├── node3.pem
│ │ └── user
│ └── node4
│ ├── admin
│ ├── chainconfig
│ ├── chainmaker.yml
│ ├── log.yml
│ ├── node4.key
│ ├── node4.nodeid
│ ├── node4.pem
│ └── user
├── crypto-config
│ ├── node1
│ │ ├── admin
│ │ ├── node1.key
│ │ ├── node1.nodeid
│ │ ├── node1.pem
│ │ └── user
│ ├── node2
│ │ ├── admin
│ │ ├── node2.key
│ │ ├── node2.nodeid
│ │ ├── node2.pem
│ │ └── user
│ ├── node3
│ │ ├── admin
│ │ ├── node3.key
│ │ ├── node3.nodeid
│ │ ├── node3.pem
│ │ └── user
│ └── node4
│ ├── admin
│ ├── node4.key
│ ├── node4.nodeid
│ ├── node4.pem
│ └── user
└── crypto_config.yml
Check out the help script:
Generate keys and configuration for a single-chain 4-node cluster:
View the generated node key and configuration:
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Yy7og9k5-1659584466471)(https://img.chengxuka.com/[email protected])]
Or view the local directory directly:
Here is a pair of keys:
- About auto-generated port descriptions
prepare.sh
The configuration generated by the script is deployed on a single server by default, so the automatically generated port numbers are incremented sequentially from a starting port number, and the starting port number can be modified through command line parameters .
There are mainly 2 ports, p2p
port (for node interconnection) and rpc
port (for communication between client and node), p2p
the starting port is 11301
, and rpc
the starting port is 12301
.
If the configuration of 4 nodes is generated, p2p
the ports are: 11301、11302、11303、11304
, and rpc
the ports are:12301、12302、12303、12304
If you want to generate a fixed port number in multi-machine deployment, please refer to: 【Multi-machine deployment】
2.3.1.4. Compilation and installation package making
- Execute
build_release.sh
the script after generating the certificate (prepare.sh script), the module will be compiledchainmaker-go
, packaged and installed, and stored in thechainmaker-go/build/release
path
$ ./build_release.sh
$ tree ../build/release/
../build/release/
├── chainmaker-v2.2.1-node1-20220803115954-x86_64.tar.gz
├── chainmaker-v2.2.1-node2-20220803115954-x86_64.tar.gz
├── chainmaker-v2.2.1-node3-20220803115954-x86_64.tar.gz
├── chainmaker-v2.2.1-node4-20220803115954-x86_64.tar.gz
└── crypto-config-20220803115954.tar.gz
View catalog:
2.3.1.5. Start node cluster
- Executing
cluster_quick_start.sh
the script will decompress each installation package, call the scriptbin
in the directorystart.sh
, and startchainmaker
the node
$ ./cluster_quick_start.sh normal
running result:
hanru@localhost scripts % ./cluster_quick_start.sh normal
===> Unzip chainmaker installation package
x chainmaker-v2.2.1-node1/
x chainmaker-v2.2.1-node1/bin/
x chainmaker-v2.2.1-node1/config/
x chainmaker-v2.2.1-node1/lib/
x chainmaker-v2.2.1-node1/log/
x chainmaker-v2.2.1-node1/lib/wxdec
x chainmaker-v2.2.1-node1/lib/libwasmer.dylib
x chainmaker-v2.2.1-node1/config/node1/
x chainmaker-v2.2.1-node1/config/node1/log.yml
x chainmaker-v2.2.1-node1/config/node1/node1.nodeid
x chainmaker-v2.2.1-node1/config/node1/chainconfig/
x chainmaker-v2.2.1-node1/config/node1/admin/
x chainmaker-v2.2.1-node1/config/node1/user/
x chainmaker-v2.2.1-node1/config/node1/chainmaker.yml
x chainmaker-v2.2.1-node1/config/node1/node1.pem
x chainmaker-v2.2.1-node1/config/node1/node1.key
x chainmaker-v2.2.1-node1/config/node1/user/client1/
x chainmaker-v2.2.1-node1/config/node1/user/client1/client1.pem
x chainmaker-v2.2.1-node1/config/node1/user/client1/client1.key
x chainmaker-v2.2.1-node1/config/node1/user/client1/client1.addr
x chainmaker-v2.2.1-node1/config/node1/admin/admin1/
x chainmaker-v2.2.1-node1/config/node1/admin/admin2/
x chainmaker-v2.2.1-node1/config/node1/admin/admin5/
x chainmaker-v2.2.1-node1/config/node1/admin/admin4/
x chainmaker-v2.2.1-node1/config/node1/admin/admin3/
x chainmaker-v2.2.1-node1/config/node1/admin/admin3/admin3.pem
x chainmaker-v2.2.1-node1/config/node1/admin/admin3/admin3.key
x chainmaker-v2.2.1-node1/config/node1/admin/admin4/admin4.pem
x chainmaker-v2.2.1-node1/config/node1/admin/admin4/admin4.key
x chainmaker-v2.2.1-node1/config/node1/admin/admin5/admin5.pem
x chainmaker-v2.2.1-node1/config/node1/admin/admin5/admin5.key
x chainmaker-v2.2.1-node1/config/node1/admin/admin2/admin2.pem
x chainmaker-v2.2.1-node1/config/node1/admin/admin2/admin2.key
x chainmaker-v2.2.1-node1/config/node1/admin/admin1/admin1.pem
x chainmaker-v2.2.1-node1/config/node1/admin/admin1/admin1.key
x chainmaker-v2.2.1-node1/config/node1/chainconfig/bc1.yml
x chainmaker-v2.2.1-node1/bin/docker-vm-standalone-start.sh
x chainmaker-v2.2.1-node1/bin/restart.sh
x chainmaker-v2.2.1-node1/bin/docker-vm-standalone-stop.sh
x chainmaker-v2.2.1-node1/bin/init.sh
x chainmaker-v2.2.1-node1/bin/run.sh
x chainmaker-v2.2.1-node1/bin/stop.sh
x chainmaker-v2.2.1-node1/bin/chainmaker
x chainmaker-v2.2.1-node1/bin/chainmaker.service
x chainmaker-v2.2.1-node1/bin/start.sh
x chainmaker-v2.2.1-node2/
x chainmaker-v2.2.1-node2/bin/
x chainmaker-v2.2.1-node2/config/
x chainmaker-v2.2.1-node2/lib/
x chainmaker-v2.2.1-node2/log/
x chainmaker-v2.2.1-node2/lib/wxdec
x chainmaker-v2.2.1-node2/lib/libwasmer.dylib
x chainmaker-v2.2.1-node2/config/node2/
x chainmaker-v2.2.1-node2/config/node2/log.yml
x chainmaker-v2.2.1-node2/config/node2/chainconfig/
x chainmaker-v2.2.1-node2/config/node2/admin/
x chainmaker-v2.2.1-node2/config/node2/user/
x chainmaker-v2.2.1-node2/config/node2/node2.pem
x chainmaker-v2.2.1-node2/config/node2/node2.nodeid
x chainmaker-v2.2.1-node2/config/node2/chainmaker.yml
x chainmaker-v2.2.1-node2/config/node2/node2.key
x chainmaker-v2.2.1-node2/config/node2/user/client1/
x chainmaker-v2.2.1-node2/config/node2/user/client1/client1.pem
x chainmaker-v2.2.1-node2/config/node2/user/client1/client1.key
x chainmaker-v2.2.1-node2/config/node2/user/client1/client1.addr
x chainmaker-v2.2.1-node2/config/node2/admin/admin1/
x chainmaker-v2.2.1-node2/config/node2/admin/admin2/
x chainmaker-v2.2.1-node2/config/node2/admin/admin5/
x chainmaker-v2.2.1-node2/config/node2/admin/admin4/
x chainmaker-v2.2.1-node2/config/node2/admin/admin3/
x chainmaker-v2.2.1-node2/config/node2/admin/admin3/admin3.pem
x chainmaker-v2.2.1-node2/config/node2/admin/admin3/admin3.key
x chainmaker-v2.2.1-node2/config/node2/admin/admin4/admin4.pem
x chainmaker-v2.2.1-node2/config/node2/admin/admin4/admin4.key
x chainmaker-v2.2.1-node2/config/node2/admin/admin5/admin5.pem
x chainmaker-v2.2.1-node2/config/node2/admin/admin5/admin5.key
x chainmaker-v2.2.1-node2/config/node2/admin/admin2/admin2.pem
x chainmaker-v2.2.1-node2/config/node2/admin/admin2/admin2.key
x chainmaker-v2.2.1-node2/config/node2/admin/admin1/admin1.pem
x chainmaker-v2.2.1-node2/config/node2/admin/admin1/admin1.key
x chainmaker-v2.2.1-node2/config/node2/chainconfig/bc1.yml
x chainmaker-v2.2.1-node2/bin/docker-vm-standalone-start.sh
x chainmaker-v2.2.1-node2/bin/restart.sh
x chainmaker-v2.2.1-node2/bin/docker-vm-standalone-stop.sh
x chainmaker-v2.2.1-node2/bin/init.sh
x chainmaker-v2.2.1-node2/bin/run.sh
x chainmaker-v2.2.1-node2/bin/stop.sh
x chainmaker-v2.2.1-node2/bin/chainmaker
x chainmaker-v2.2.1-node2/bin/chainmaker.service
x chainmaker-v2.2.1-node2/bin/start.sh
x chainmaker-v2.2.1-node3/
x chainmaker-v2.2.1-node3/bin/
x chainmaker-v2.2.1-node3/config/
x chainmaker-v2.2.1-node3/lib/
x chainmaker-v2.2.1-node3/log/
x chainmaker-v2.2.1-node3/lib/wxdec
x chainmaker-v2.2.1-node3/lib/libwasmer.dylib
x chainmaker-v2.2.1-node3/config/node3/
x chainmaker-v2.2.1-node3/config/node3/log.yml
x chainmaker-v2.2.1-node3/config/node3/node3.nodeid
x chainmaker-v2.2.1-node3/config/node3/chainconfig/
x chainmaker-v2.2.1-node3/config/node3/admin/
x chainmaker-v2.2.1-node3/config/node3/user/
x chainmaker-v2.2.1-node3/config/node3/node3.pem
x chainmaker-v2.2.1-node3/config/node3/chainmaker.yml
x chainmaker-v2.2.1-node3/config/node3/node3.key
x chainmaker-v2.2.1-node3/config/node3/user/client1/
x chainmaker-v2.2.1-node3/config/node3/user/client1/client1.pem
x chainmaker-v2.2.1-node3/config/node3/user/client1/client1.key
x chainmaker-v2.2.1-node3/config/node3/user/client1/client1.addr
x chainmaker-v2.2.1-node3/config/node3/admin/admin1/
x chainmaker-v2.2.1-node3/config/node3/admin/admin2/
x chainmaker-v2.2.1-node3/config/node3/admin/admin5/
x chainmaker-v2.2.1-node3/config/node3/admin/admin4/
x chainmaker-v2.2.1-node3/config/node3/admin/admin3/
x chainmaker-v2.2.1-node3/config/node3/admin/admin3/admin3.pem
x chainmaker-v2.2.1-node3/config/node3/admin/admin3/admin3.key
x chainmaker-v2.2.1-node3/config/node3/admin/admin4/admin4.pem
x chainmaker-v2.2.1-node3/config/node3/admin/admin4/admin4.key
x chainmaker-v2.2.1-node3/config/node3/admin/admin5/admin5.pem
x chainmaker-v2.2.1-node3/config/node3/admin/admin5/admin5.key
x chainmaker-v2.2.1-node3/config/node3/admin/admin2/admin2.pem
x chainmaker-v2.2.1-node3/config/node3/admin/admin2/admin2.key
x chainmaker-v2.2.1-node3/config/node3/admin/admin1/admin1.pem
x chainmaker-v2.2.1-node3/config/node3/admin/admin1/admin1.key
x chainmaker-v2.2.1-node3/config/node3/chainconfig/bc1.yml
x chainmaker-v2.2.1-node3/bin/docker-vm-standalone-start.sh
x chainmaker-v2.2.1-node3/bin/restart.sh
x chainmaker-v2.2.1-node3/bin/docker-vm-standalone-stop.sh
x chainmaker-v2.2.1-node3/bin/init.sh
x chainmaker-v2.2.1-node3/bin/run.sh
x chainmaker-v2.2.1-node3/bin/stop.sh
x chainmaker-v2.2.1-node3/bin/chainmaker
x chainmaker-v2.2.1-node3/bin/chainmaker.service
x chainmaker-v2.2.1-node3/bin/start.sh
x chainmaker-v2.2.1-node4/
x chainmaker-v2.2.1-node4/bin/
x chainmaker-v2.2.1-node4/config/
x chainmaker-v2.2.1-node4/lib/
x chainmaker-v2.2.1-node4/log/
x chainmaker-v2.2.1-node4/lib/wxdec
x chainmaker-v2.2.1-node4/lib/libwasmer.dylib
x chainmaker-v2.2.1-node4/config/node4/
x chainmaker-v2.2.1-node4/config/node4/log.yml
x chainmaker-v2.2.1-node4/config/node4/chainconfig/
x chainmaker-v2.2.1-node4/config/node4/admin/
x chainmaker-v2.2.1-node4/config/node4/user/
x chainmaker-v2.2.1-node4/config/node4/chainmaker.yml
x chainmaker-v2.2.1-node4/config/node4/node4.pem
x chainmaker-v2.2.1-node4/config/node4/node4.nodeid
x chainmaker-v2.2.1-node4/config/node4/node4.key
x chainmaker-v2.2.1-node4/config/node4/user/client1/
x chainmaker-v2.2.1-node4/config/node4/user/client1/client1.pem
x chainmaker-v2.2.1-node4/config/node4/user/client1/client1.key
x chainmaker-v2.2.1-node4/config/node4/user/client1/client1.addr
x chainmaker-v2.2.1-node4/config/node4/admin/admin1/
x chainmaker-v2.2.1-node4/config/node4/admin/admin2/
x chainmaker-v2.2.1-node4/config/node4/admin/admin5/
x chainmaker-v2.2.1-node4/config/node4/admin/admin4/
x chainmaker-v2.2.1-node4/config/node4/admin/admin3/
x chainmaker-v2.2.1-node4/config/node4/admin/admin3/admin3.pem
x chainmaker-v2.2.1-node4/config/node4/admin/admin3/admin3.key
x chainmaker-v2.2.1-node4/config/node4/admin/admin4/admin4.pem
x chainmaker-v2.2.1-node4/config/node4/admin/admin4/admin4.key
x chainmaker-v2.2.1-node4/config/node4/admin/admin5/admin5.pem
x chainmaker-v2.2.1-node4/config/node4/admin/admin5/admin5.key
x chainmaker-v2.2.1-node4/config/node4/admin/admin2/admin2.pem
x chainmaker-v2.2.1-node4/config/node4/admin/admin2/admin2.key
x chainmaker-v2.2.1-node4/config/node4/admin/admin1/admin1.pem
x chainmaker-v2.2.1-node4/config/node4/admin/admin1/admin1.key
x chainmaker-v2.2.1-node4/config/node4/chainconfig/bc1.yml
x chainmaker-v2.2.1-node4/bin/docker-vm-standalone-start.sh
x chainmaker-v2.2.1-node4/bin/restart.sh
x chainmaker-v2.2.1-node4/bin/docker-vm-standalone-stop.sh
x chainmaker-v2.2.1-node4/bin/init.sh
x chainmaker-v2.2.1-node4/bin/run.sh
x chainmaker-v2.2.1-node4/bin/stop.sh
x chainmaker-v2.2.1-node4/bin/chainmaker
x chainmaker-v2.2.1-node4/bin/chainmaker.service
x chainmaker-v2.2.1-node4/bin/start.sh
===> Starting chainmaker cluster
START ==> /Users/hanru/Documents/程序咖/chainmaker/chainmaker-go/build/release/chainmaker-v2.2.1-node1
chainmaker is startting, pls check log...
START ==> /Users/hanru/Documents/程序咖/chainmaker/chainmaker-go/build/release/chainmaker-v2.2.1-node2
chainmaker is startting, pls check log...
START ==> /Users/hanru/Documents/程序咖/chainmaker/chainmaker-go/build/release/chainmaker-v2.2.1-node3
chainmaker is startting, pls check log...
START ==> /Users/hanru/Documents/程序咖/chainmaker/chainmaker-go/build/release/chainmaker-v2.2.1-node4
chainmaker is startting, pls check log...
hanru@localhost scripts %
Startup effect:
- After the startup is successful, back up *.gar.gz to prevent the file from being overwritten when it is decompressed again at the next startup
$ mkdir -p ../build/bak
$ mv ../build/release/*.tar.gz ../build/bak
Make a backup:
If you need to shut down the cluster, use the script:
$ ./cluster_quick_stop.sh
2.3.1.6. Check that the node is started and used normally
- Check if the process exists
$ ps -ef|grep chainmaker | grep -v grep
25261 2146 4 19:55 pts/20 00:00:01 ./chainmaker start -c ../config/wx-org1.chainmaker.org/chainmaker.yml
25286 2146 4 19:55 pts/20 00:00:01 ./chainmaker start -c ../config/wx-org2.chainmaker.org/chainmaker.yml
25309 2146 4 19:55 pts/20 00:00:01 ./chainmaker start -c ../config/wx-org3.chainmaker.org/chainmaker.yml
25335 2146 4 19:55 pts/20 00:00:01 ./chainmaker start -c ../config/wx-org4.chainmaker.org/chainmaker.yml
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-LQdXgf2V-1659584466473)(https://img.chengxuka.com/[email protected])]
- Check if the port is listening
$ netstat -lptn | grep 1230
tcp6 0 0 :::12301 :::* LISTEN 25261/./chainmaker
tcp6 0 0 :::12302 :::* LISTEN 25286/./chainmaker
tcp6 0 0 :::12303 :::* LISTEN 25309/./chainmaker
tcp6 0 0 :::12304 :::* LISTEN 25335/./chainmaker
Under mac, the command is slightly changed: netstat -anv | grep 1230
- Check if the node has
ERROR
logs
$ cat ../build/release/*/bin/panic.log
$ cat ../build/release/*/log/system.log
$ cat ../build/release/*/log/system.log |grep "ERROR\|put block\|all necessary"
You can also see these 4 services when you open the Activity Monitor: