Hyperledger Fabric 1.2系列fabcar: 3. fabcar 网络启动过程

Hyperledger Fabric 1.2系列fabcar: 3. fabcar 网络启动过程

执行 startFabric 脚本

# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1

# 清空原有 net_basic 网络
docker-compose -f docker-compose.yml down
Removing network net_basic
WARNING: Network net_basic not found.

# 启动 ca、orderer、peer、couchdb
docker-compose -f docker-compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb
Creating network "net_basic" with the default driver
Creating ca.example.com      ... done
Creating orderer.example.com ... done
Creating couchdb             ... done
Creating peer0.org1.example.com ... done

# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}


# Create the channel
# 通过 channel.tx 文件创建通道,-c 指定通道名称, -f 指定文件位置
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
2018-09-28 11:45:09.091 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-09-28 11:45:09.162 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND}
2018-09-28 11:45:09.163 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-09-28 11:45:09.370 UTC [cli/common] readBlock -> INFO 004 Received block: 0

# Join peer0.org1.example.com to the channel.
# 将 peer 节点加入到通道。-b 指定通道的创始区块
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block
2018-09-28 11:45:09.799 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-09-28 11:45:10.012 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
Creating cli ... done

# vscc和escc是两个系统chaincode 主要的作用是对用户的 chaincode 进行相关验证和背书。在实例化 chaincode 的时候指定。
2018-09-28 11:45:12.697 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-09-28 11:45:12.697 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-09-28 11:45:14.266 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
2018-09-28 11:45:14.625 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-09-28 11:45:14.626 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-09-28 11:45:54.690 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

Total setup execution time : 65 secs ...

# 下面是文档,指导之后的操作步骤
Start by installing required packages run 'npm install'
Then run 'node enrollAdmin.js', then 'node registerUser'

The 'node invoke.js' will fail until it has been updated with valid arguments
The 'node query.js' may be run at anytime once the user has been registered

输入docker ps -a 查看启动的容器:

VirtualBox:~$ docker ps -a
CONTAINER ID        IMAGE                                                                                                    COMMAND                  CREATED             STATUS              PORTS                                            NAMES
246c4298f3e2        dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba   "chaincode -peer.add…"   14 hours ago        Up 14 hours                                                          dev-peer0.org1.example.com-fabcar-1.0
9249c48132cd        hyperledger/fabric-tools                                                                                 "/bin/bash"              14 hours ago        Up 14 hours                                                          cli
4f58c4988ba1        hyperledger/fabric-peer                                                                                  "peer node start"        14 hours ago        Up 14 hours         0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
ffb583ae7a8f        hyperledger/fabric-orderer                                                                               "orderer"                14 hours ago        Up 14 hours         0.0.0.0:7050->7050/tcp                           orderer.example.com
ec27a0cf0e79        hyperledger/fabric-couchdb                                                                               "tini -- /docker-ent…"   14 hours ago        Up 14 hours         4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp       couchdb
daf018fa13b6        hyperledger/fabric-ca                                                                                    "sh -c 'fabric-ca-se…"   14 hours ago        Up 14 hours         0.0.0.0:7054->7054/tcp                           ca.example.com
VirtualBox:~$

我们可以看到启动了:一个 chaincode 容器,一个cli容器,一个peer节点,一个orderer节点,一个couchdb容器,一个 ca 容器。

注册用户

稍后我们会通过node enrollAdmin.js命令来注册 admin 用户,我们先执行 docker logs -f ca.example.com 查看 ca 的日志。然后另外启动一个窗口,执行node enrollAdmin.js命令。 可以查看到日志内容如下:

node enrollAdmin.js窗口的日志内容:

VirtualBox:~/code/fabric/src/fabric-samples/fabcar$ node enrollAdmin.js

# 证书的存储位置
 Store path:/home/sharex2/code/fabric/src/fabric-samples/fabcar/hfc-key-store
(node:18297) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead

# 注册 admin 用户
Successfully enrolled admin user "admin"
Assigned the admin user to the fabric client ::{
    "name": "admin",                                    
    "mspid": "Org1MSP",
    "roles": null,
    "affiliation": "",
    "enrollmentSecret": "",
    "enrollment": {
        "signingIdentity": "576a8fda068a65c4fa6efac385070fa6c5ed35a1d11de364609dcfe7a71e43da",
        "identity": {
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAaigAwIBAgIUKgcgnRyBiEnUW0KJIsEIhTMQG0kwCgYIKoZIzj0EAwIw\nczELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMT\nE2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgwOTI5MDE1OTAwWhcNMTkwOTI5MDIw\nNDAwWjAhMQ8wDQYDVQQLEwZjbGllbnQxDjAMBgNVBAMTBWFkbWluMFkwEwYHKoZI\nzj0CAQYIKoZIzj0DAQcDQgAE1M5rpjJFr02zQabHjCYLRmzwSkf6puTfk13BKy8m\nzjE5NCZ6RvILPEE8JdaI2qg4yIwjK/qECnXcVg3JbDmBGKNsMGowDgYDVR0PAQH/\nBAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFJTWgUHUkBijfu3pgZngkHoa\nQYvWMCsGA1UdIwQkMCKAIEI5qg3NdtruuLoM2nAYUdFFBNMarRst3dusalc2Xkl8\nMAoGCCqGSM49BAMCA0gAMEUCIQCwb8+ZwgWJ3c18tcW8hRGVgqT1LPYx/x/AxxFC\nXYAYSwIgRxtanecPUbNoVq0l94zqhLuvEGcQtw3X2cSSyZHVlJo=\n-----END CERTIFICATE-----\n"
        }
    }
}

可以看到 admin 用户注册成功了。然后再执行node registerUser.js

VirtualBox:~/code/fabric/src/fabric-samples/fabcar$ node registerUser.js
 Store path:/home/sharex2/code/fabric/src/fabric-samples/fabcar/hfc-key-store
(node:18384) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead
Successfully loaded admin from persistence
Successfully registered user1 - secret:oPvfLxDmnRzj
Successfully enrolled member user "user1"
User1 was successfully registered and enrolled and is ready to interact with the fabric network

用户 user1 注册成功了

我们来看下,执行完两个注册用户命令之后,都生成了什么证书。

VirtualBox:~/code/fabric/src/fabric-samples/fabcar$ tree hfc-key-store/
hfc-key-store/
├── 576a8fda068a65c4fa6efac385070fa6c5ed35a1d11de364609dcfe7a71e43da-priv
├── 576a8fda068a65c4fa6efac385070fa6c5ed35a1d11de364609dcfe7a71e43da-pub
├── 97bbde8be6b15b12012478fedc6e1ab03311411ccd77ba88baaa361eb2cd72a0-priv
├── 97bbde8be6b15b12012478fedc6e1ab03311411ccd77ba88baaa361eb2cd72a0-pub
├── admin
└── user1

0 directories, 6 files

有注册的用户,以及公私密钥。

查看 CA 的日志内容

我们查看docker logs -f ca.example.com窗口的内容:


VirtualBox:~$ docker logs -f ca.example.com

# 启动服务之前的内容
# 创建默认的配置文件   (这里面的内容可以去 CA 的 docker 里面查看)
2018/09/28 11:44:54 [INFO] Created default configuration file at /etc/hyperledger/fabric-ca-server/fabric-ca-server-config.yaml
2018/09/28 11:44:54 [INFO] Starting server in home directory: /etc/hyperledger/fabric-ca-server
2018/09/28 11:44:54 [INFO] Server Version: 1.3.0-rc1
2018/09/28 11:44:54 [INFO] Server Levels: &{Identity:1 Affiliation:1 Certificate:1 Credential:1 RAInfo:1 Nonce:1}
2018/09/28 11:44:54 [INFO] The CA key and certificate files already exist
2018/09/28 11:44:54 [INFO] Key file location: /etc/hyperledger/fabric-ca-server-config/4239aa0dcd76daeeb8ba0cda701851d14504d31aad1b2ddddbac6a57365e497c_sk
2018/09/28 11:44:54 [INFO] Certificate file location: /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
2018/09/28 11:44:54 [INFO] Initialized sqlite3 database at /etc/hyperledger/fabric-ca-server/fabric-ca-server.db
2018/09/28 11:44:54 [INFO] The issuer key was successfully stored. The public key is at: /etc/hyperledger/fabric-ca-server/IssuerPublicKey, secret key is at: /etc/hyperledger/fabric-ca-server/msp/keystore/IssuerSecretKey
2018/09/28 11:44:54 [INFO] Idemix issuer revocation public and secret keys were generated for CA 'ca.example.com'
2018/09/28 11:44:54 [INFO] The revocation key was successfully stored. The public key is at: /etc/hyperledger/fabric-ca-server/IssuerRevocationPublicKey, private key is at: /etc/hyperledger/fabric-ca-server/msp/keystore/IssuerRevocationPrivateKey
2018/09/28 11:44:55 [INFO] Home directory for default CA: /etc/hyperledger/fabric-ca-server
2018/09/28 11:44:55 [INFO] Listening on http://0.0.0.0:7054

# 注册 CA 认证部分内容
# 注册 admin 用户的log
2018/09/29 02:03:39 [INFO] signed certificate with serial number 239936558646363351728825994521488687700162648905
2018/09/29 02:03:39 [INFO] 172.18.0.1:60994 POST /api/v1/enroll 201 0 "OK"

# 注册 user1 的 log
2018/09/29 02:17:23 [INFO] 172.18.0.1:32770 POST /api/v1/register 201 0 "OK"
2018/09/29 02:17:23 [INFO] signed certificate with serial number 349144519259928801587884783730001097600426690521
2018/09/29 02:17:23 [INFO] 172.18.0.1:32774 POST /api/v1/enroll 201 0 "OK"

执行查询操作

执行node query.js

VirtualBox:~/code/fabric/src/fabric-samples/fabcar$ node query.js
Store path:/home/sharex2/code/fabric/src/fabric-samples/fabcar/hfc-key-store
(node:19021) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead
Successfully loaded user1 from persistence
Query has completed, checking results
Response is  [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]

可以看到,从hfs-key-store文件夹内读取 user1的证书,并返回了CAR0~CAR9的内容。

总结

ok,现在网络就可以运行起来了。稍后我们会解析 query 的整个流程以及 query.js 脚本的内容。

;