本文将完成:
多个共识、同步、用户证书生成
一组织多个共识节点配置
7个组织10个节点docker启动
测试交易发送验证
长安链官网: https://chainmaker.org.cn/
以7个组织10个节点为例
其中org1有2个共识节点1个同步节点
org7有2个同步节点无共识节点
其他org均1个共识节点
组织节点关系如下表:
组织 | 共识节点 | 同步节点 |
---|---|---|
org1 | node1 node12 | node13 |
org2 | node2 | |
org3 | node3 | |
org4 | node4 | |
org5 | node5 | |
org6 | node6 | |
org7 | node7 node71 |
1、下载编译源码、下载镜像
2、修改chainmaker-cryptogen
证书模板,使得可组织可以生成多个共识节点证书
3、生成证书 ./prepare.sh 7 1 11360 12360
,此时为7个组织1条链,每个组织1个共识节点。
4、为org1添加第二个共识节点。
node1
配置为org1的第二个共识节点node12
node12
配置文件中的consensus1
为consensus2
bc1.yml
,添加共识节点consensus2 nodeId
配置5、为org1添加一个同步节点
node1
配置为org1的第二个共识节点node13
ode13
配置文件中的consensus1
为common1
6、删除组织7的共识节点配置
7、修改docker-compose
文件
8、启动docker
9、发交易
git clone --recurse-submodules [email protected]:chainmaker/chainmaker-go.git
cd chainmaker-go
# 下载镜像
docker pull chainmakerofficial/chainmaker:v1.2.0
# 进入与chainmaker-go同级目录
git clone --recurse-submodules [email protected]:chainmaker/chainmaker-cryptogen.git
cd chainmaker-cryptogen
make
cd chainmaker-go/tools
ln -s ../../chainmaker-cryptogen .
vim chainmaker-cryptogen/config/crypto_config_template.yml
crypto_config[0].count=7
crypto_config[0].node[0].count=2 // 共识节点证书数
crypto_config[0].node[1].count=2 // 同步节点证书数
crypto_config[0].user[0].count=2 // 管理员证书数量
crypto_config[0].user[1].count=2 // 普通证书数量
# crypto_config_template.yml 文件如下
crypto_config:
- domain: chainmaker.org
host_name: wx-org
count: 7 # 如果为1,直接使用host_name,否则添加递增编号
pk_algo: ecc_p256
# pk_algo: sm2
ski_hash: sha256
specs: &specs_ref
expire_year: 10
sans:
- chainmaker.org
- localhost
- 127.0.0.1
location: &location_ref
country: CN
locality: Beijing
province: Beijing
# CA证书配置
ca:
location:
<<: *location_ref
specs:
<<: *specs_ref
# 节点证书配置
node:
- type: consensus
# 共识节点数量
count: 2
# 共识节点配置
location:
<<: *location_ref
specs:
<<: *specs_ref
expire_year: 5
- type: common
# 普通节点数量
count: 2
# 普通节点配置
location:
<<: *location_ref
specs:
<<: *specs_ref
expire_year: 5
user:
- type: admin
# 管理员证书数量
count: 2
# 管理员证书配置
location:
<<: *location_ref
expire_year: 5
- type: client
# 普通用户证书数量
count: 2
# 普通用户证书配置
location:
<<: *location_ref
expire_year: 5
cd chainmaker-go/script
# 生成7个节点 端口为12361-12367的配置
./prepare.sh 7 1 11360 12360
[root@#localhost scripts]# ./prepare.sh 7 1 11360 12360
begin check params...
begin generate certs, cnt: 7
input consensus type (0-SOLO,1-TBFT(default),3-HOTSTUFF,4-RAFT):
input log level (DEBUG|INFO(default)|WARN|ERROR):
begin generate node1 config...
begin generate node2 config...
begin generate node3 config...
begin generate node4 config...
begin generate node5 config...
begin generate node6 config...
begin generate node7 config...
# 生成的证书结构如下
cd ../build
tree config/ -L 2
config/
├── node1
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
├── node2
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
├── node3
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
├── node4
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
├── node5
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
├── node6
│ ├── certs
│ ├── chainconfig
│ ├── chainmaker.yml
│ └── log.yml
└── node7
├── certs
├── chainconfig
├── chainmaker.yml
└── log.yml
21 directories, 14 files
# 最终配置如下
cd chainmaker-go/build
tree config/ -L 2
[root@#localhost build]# tree -L 2
.
├── chainmaker-cmc
│ ├── cmc
│ ├── rust-func-verify-1.2.0.wasm
│ └── testdata
├── config
│ ├── node1
│ ├── node12
│ ├── node13
│ ├── node2
│ ├── node3
│ ├── node4
│ ├── node5
│ ├── node6
│ ├── node7
│ └── node72
├── crypto-config
│ ├── wx-org1.chainmaker.org
│ ├── wx-org2.chainmaker.org
│ ├── wx-org3.chainmaker.org
│ ├── wx-org4.chainmaker.org
│ ├── wx-org5.chainmaker.org
│ ├── wx-org6.chainmaker.org
│ └── wx-org7.chainmaker.org
├── crypto_config.yml
├── nodes.docker-compose.yml
├── nodes_down.sh
└── nodes_up.sh
# 1.复制org1的节点`node1`配置为org1的第二个共识节点
cd chainmaker-go/build/config
cp -r node1 node12
# 2.修改node12/chainmaker.yml配置为org1共识节点二的证书
sed -i "s%consensus1%consensus2%g" node12/chainmaker.yml
# 3. 修改node12/chainconfig/bc1.yml的 consensus.nodes为我们所需的结构:org1 两个共识节点org2-org61个共识节点。
# 并将该consensus配置复制到其他每个节点的bc1.yml中(node1-node7/chainconfig/bc1.yml)
# 注: bc1.yml为创世快的配置,创世快内容各节点须一致才能建立网络达成共识(包括共识节点和同步节点)。
vim node12/chainconfig/bc1.yml
# 1、添加共识节点配置:org_id: "wx-org1.chainmaker.org" 下方node_id加上chainmaker-go\build\config\node12\certs\node\consensus2\consensus2.nodeid的值
# 2、删除org7配置: org_id: "wx-org7.chainmaker.org"
# bc1.yml consensus如下
consensus:
# 共识类型(0-SOLO,1-TBFT,2-MBFT,3-HOTSTUFF,4-RAFT,10-POW)
type: 1
# 共识节点列表,组织必须出现在trust_roots的org_id中,每个组织可配置多个共识节点,节点地址采用libp2p格式
nodes:
- org_id: "wx-org1.chainmaker.org"
node_id:
- "QmbxzXTSfdV98CpD2vWcRScBW8xBQbYoxKf6y6FmGpNsyw"
- "QmVD9NPQfJUjzqYiNm6CXMsC4v3jkrKTvtVGmy9DMkjPP3"
- org_id: "wx-org2.chainmaker.org"
node_id:
- "QmdNzzsTuiUFB8Y7683GeDeDX3EeKRYFzdoYE7Kou3YyHH"
- org_id: "wx-org3.chainmaker.org"
node_id:
- "QmVzz3XwLthWoPVLoRzjANNDfArJSQ9FyoaP1uByHyfwjV"
- org_id: "wx-org4.chainmaker.org"
node_id:
- "QmXbEGttJQ87hV1gYdETbqQVSxgnrrGpapBLpkdCEyTUyx"
- org_id: "wx-org5.chainmaker.org"
node_id:
- "QmVmzAZDynaL5oJ1kzPUWLhVV5wN1NAvtAq6g6xpwW3uZs"
- org_id: "wx-org6.chainmaker.org"
node_id:
- "QmPyxqwhbDsAUEmSoF9FEwvJvJT1JJzKSU7AcxB3RCin8M"
# 复制org1的节点`node1`配置为org1的一个同步节点
cd chainmaker-go/build/config
cp -r node1 node13
# 修改node12/chainmaker.yml配置为org1同步节点一的证书
sed -i "s%consensus1/consensus1%common1/common1%g" node13/chainmaker.yml
# 复制org1的节点`node1`配置为org1的第一、二个同步节点
cp -r node7 node72
# 将node7 node72分别改成同步节点一、二
sed -i "s%consensus1/consensus1%common1/common1%g" node7/chainmaker.yml
sed -i "s%consensus1/consensus1%common2/common2%g" node72/chainmaker.yml
将chainmaker.yml中
net.listen_addr的端口改为11361
seeds的端口改为11361
rpc.port改为12361
# 修改所有文件的seeds为bc1.yml中对应的nodeid
# 将org7的删除,org1第二个共识节点 node12加入
# 并将此seeds配置复制到各个节点中node1-node7 node12 node13 node72
# 最终seeds如下:
seeds:
- "/dns/cm-node101/tcp/11361/p2p/QmbxzXTSfdV98CpD2vWcRScBW8xBQbYoxKf6y6FmGpNsyw"
- "/dns/cm-node1012/tcp/11361/p2p/QmVD9NPQfJUjzqYiNm6CXMsC4v3jkrKTvtVGmy9DMkjPP3"
- "/dns/cm-node102/tcp/11361/p2p/QmdNzzsTuiUFB8Y7683GeDeDX3EeKRYFzdoYE7Kou3YyHH"
- "/dns/cm-node103/tcp/11361/p2p/QmVzz3XwLthWoPVLoRzjANNDfArJSQ9FyoaP1uByHyfwjV"
- "/dns/cm-node104/tcp/11361/p2p/QmXbEGttJQ87hV1gYdETbqQVSxgnrrGpapBLpkdCEyTUyx"
- "/dns/cm-node105/tcp/11361/p2p/QmVmzAZDynaL5oJ1kzPUWLhVV5wN1NAvtAq6g6xpwW3uZs"
- "/dns/cm-node106/tcp/11361/p2p/QmPyxqwhbDsAUEmSoF9FEwvJvJT1JJzKSU7AcxB3RCin8M"
注:需关注ports需与chainmaker.yml中一致 volumes各个组织名称如wx-org1.chainmaker.org不能修改(如修改则log.yml的地址等也需一并修改)
nodes.docker-compose.yml
如下:
version: '3'
services:
node1:
container_name: cm-node101
hostname: cm-node101
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org1.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11111:11361"
- "12111:12361"
volumes:
- ./config/node1:/chainmaker-go/config/wx-org1.chainmaker.org
- ./data/node1_data:/chainmaker-go/data
- ./log/node1_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node101
ipv4_address: 172.45.1.11
node12:
container_name: cm-node1012
hostname: cm-node1012
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org1.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11112:11361"
- "12112:12361"
volumes:
- ./config/node12:/chainmaker-go/config/wx-org1.chainmaker.org
- ./data/node12_data:/chainmaker-go/data
- ./log/node12_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node1012
ipv4_address: 172.45.1.12
node13:
container_name: cm-node1013
hostname: cm-node1013
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org1.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11113:11361"
- "12113:12361"
volumes:
- ./config/node13:/chainmaker-go/config/wx-org1.chainmaker.org
- ./data/node13_data:/chainmaker-go/data
- ./log/node13_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node1013
ipv4_address: 172.45.1.13
node2:
container_name: cm-node102
hostname: cm-node102
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org2.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11121:11361"
- "12121:12361"
volumes:
- ./config/node2:/chainmaker-go/config/wx-org2.chainmaker.org
- ./data/node2_data:/chainmaker-go/data
- ./log/node2_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node102
ipv4_address: 172.45.1.21
node3:
container_name: cm-node103
hostname: cm-node103
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org3.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11131:11361"
- "12131:12361"
volumes:
- ./config/node3:/chainmaker-go/config/wx-org3.chainmaker.org
- ./data/node3_data:/chainmaker-go/data
- ./log/node3_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node103
ipv4_address: 172.45.1.31
node4:
container_name: cm-node104
hostname: cm-node104
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org4.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11141:11361"
- "12141:12361"
volumes:
- ./config/node4:/chainmaker-go/config/wx-org4.chainmaker.org
- ./data/node4_data:/chainmaker-go/data
- ./log/node4_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node104
ipv4_address: 172.45.1.41
node5:
container_name: cm-node105
hostname: cm-node105
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org5.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11151:11361"
- "12151:12361"
volumes:
- ./config/node5:/chainmaker-go/config/wx-org5.chainmaker.org
- ./data/node5_data:/chainmaker-go/data
- ./log/node5_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node105
ipv4_address: 172.45.1.51
node6:
container_name: cm-node106
hostname: cm-node106
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org6.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11161:11361"
- "12161:12361"
volumes:
- ./config/node6:/chainmaker-go/config/wx-org6.chainmaker.org
- ./data/node6_data:/chainmaker-go/data
- ./log/node6_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node106
ipv4_address: 172.45.1.61
node7:
container_name: cm-node107
hostname: cm-node107
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org7.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11171:11361"
- "12171:12361"
volumes:
- ./config/node7:/chainmaker-go/config/wx-org7.chainmaker.org
- ./data/node7_data:/chainmaker-go/data
- ./log/node7_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node107
ipv4_address: 172.45.1.71
node72:
container_name: cm-node1072
hostname: cm-node1072
image: "chainmakerofficial/chainmaker:v1.2.0"
restart: always
working_dir: "/chainmaker-go/bin"
command: "./chainmaker start -c ../config/wx-org7.chainmaker.org/chainmaker.yml > ../log/panic.log"
ports:
- "11172:11361"
- "12172:12361"
volumes:
- ./config/node72:/chainmaker-go/config/wx-org7.chainmaker.org
- ./data/node72_data:/chainmaker-go/data
- ./log/node72_log:/chainmaker-go/log
networks:
localnet-node10:
aliases:
- cm-node1072
ipv4_address: 172.45.1.72
networks:
localnet-node10:
driver: bridge
ipam:
config:
- subnet: 172.45.1.0/16
docker-compose -f nodes.docker-compose.yml up -d
# 停止
docker-compose -f nodes.docker-compose.yml down
# 查看日志,无错误即可
cd build
grep -rn "ERROR\|put block\|all necessary"
cd chainmaker-go
make cmc
cp bin/cmc build/chainmaker-cmc
cd build/chainmaker-cmc
cp ../../test/wasm/rust-func-verify-1.2.0.wasm .
cp ../../tools/sdk/testdata/sdk_config.yml testdata/
cp -r ../crypto-config/ testdata/
# 修改sdk_config.yml端口为docker对外映射端口12111 12121
# 创建合约
./cmc client contract user create \
--admin-key-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/admin1/admin1.tls.key \
--admin-crt-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/admin1/admin1.tls.crt \
--client-crt-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.crt \
--client-key-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.key \
--org-id=wx-org1.chainmaker.org \
--byte-code-path=./rust-func-verify-1.2.0.wasm \
--contract-name=fact_001 \
--runtime-type=WASMER \
--sdk-conf-path=./testdata/sdk_config.yml \
--version=1.0 \
--sync-result=true \
# 执行合约
./cmc client contract user invoke \
--contract-name=fact_001 \
--method=save \
--org-id=wx-org1.chainmaker.org \
--client-crt-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.crt \
--client-key-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.key \
--sdk-conf-path=./testdata/sdk_config.yml \
--sync-result=true \
--params="{\"time\":\"123\",\"file_hash\":\"2352B3523FB3F2B2FB2E254AA5B6\",\"file_name\":\"name.png\"}"
# 查询合约
./cmc client contract user get \
--contract-name=fact_001 \
--method=find_by_file_hash \
--sdk-conf-path=./testdata/sdk_config.yml \
--org-id=wx-org1.chainmaker.org \
--client-crt-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.crt \
--client-key-file-paths=./testdata/crypto-config/wx-org1.chainmaker.org/user/client1/client1.tls.key \
--params="{\"file_hash\":\"2352B3523FB3F2B2FB2E254AA5B6\"}"