超级账本(版本2.2):编写第一个应用

超级账本(版本2.2):编写第一个应用

关于FabCar

FabCar示例演示了如何查询保存在分类账上的Car(我们的示例业务对象),以及如何更新分类账(向分类账添加新的Car)。 它涉及以下两个组件:

1.示例应用程序:调用区块链网络,调用智能合约中实现的交易。

2.智能合约流程,实现涉及与分类账交互的交易。

我们将按照以下三个步骤进行:

  1. 搭建开发环境。 我们的应用程序需要和网络交互,所以我们需要一个智能合约和 应用程序使用的基础网络。

  2. 浏览一个示例智能合约。 我们将查看示例智能合约 Fabcar 来学习他们的交易,还有应用程序是怎么使用他们来进行查询和更新账本的。

  3. 使用示例应用程序和智能合约交互。 我们的应用程序将使用 FabCar 智能合约来查询和更新账本上的汽车资产。我们将进入到应用程序的代码和他们创建的交易,包括查询一辆汽车,查询一批汽车和创建一辆新车。

准备工作

这里以macOS为例,需要完成以下步骤:

  1. 安装Homebrew
  2. 检查Node SDK的先决条件,以了解要安装的Node级别。
  3. 运行前提安装节点以下载最新版本的节点或选择特定版本,例如:根据先决条件支持的方式安装brew install node @10
  4. 运行npm install

接下来正式开始

设置区块链网络

启动网络

进入你克隆到本地的 fabric-samples 仓库的 fabcar 子目录。

cd fabric-samples/fabcar

使用 startFabric.sh 脚本启动网络。

./startFabric.sh

运行成功以后出现如下结果:

~/fabric-samples/test-network ~/fabric-samples/fabcar
Stopping network
Stopping peer0.org2.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping couchdb1               ... done
Stopping couchdb0               ... done
Stopping orderer.example.com    ... done
Stopping ca_orderer             ... done
Stopping ca_org2                ... done
Stopping ca_org1                ... done
Removing peer0.org2.example.com ... done
Removing peer0.org1.example.com ... done
Removing couchdb1               ... done
Removing couchdb0               ... done
Removing orderer.example.com    ... done
Removing ca_orderer             ... done
Removing ca_org2                ... done
Removing ca_org1                ... done
Removing network net_test
Removing volume net_orderer.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer0.org2.example.com
Removing network net_test
WARNING: Network net_test not found.
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
No containers available for deletion
Untagged: dev-peer0.org2.example.com-fabcar_1-762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506-587e14d21fc7161ab1e246fbd5583966a3ff292484ddd7a52fa40ade4b60b120:latest
Deleted: sha256:ef78e3acc0131a69f2911d24d9160676c940f33309adc0008461711dc65502bd
Deleted: sha256:5b69dcbea5927baa9f7dc4bea24bc517e3175dd6ddbc58bdf36767f6fc321243
Deleted: sha256:d3da3fe824406a2caaeb85a4e064c955b1c51741bdda23f8c4fa7b39416d63b4
Deleted: sha256:041e24a635607f82228f1848bab59ece113464cae88a42fc1642bf172c52736e
Untagged: dev-peer0.org1.example.com-fabcar_1-762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506-5016f400ec9078b1e7c00626e70b0094ec48651ea7f49e14b46a6995345e2431:latest
Deleted: sha256:98ae55fb6effa30af5b260dcb0a01e61b9b5bae51d850ebdf150027a295f385f
Deleted: sha256:d940a2f73110d2827f43c2a7cb79b8bc285ef1d452cdfaab1b1f07bf4c220ec6
Deleted: sha256:13caec355d2df1cf974a59fe6ef0cd1849b4fb6b93f2f1b2b72c74679945b9e7
Deleted: sha256:020673a2736744185f476b009e7e6e941334040cf3da3d5ee7909ffb0c81cc9b
Creating channel 'mychannel'.
If network is not up, starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'couchdb with crypto from 'Certificate Authorities'
Bringing up network
LOCAL_VERSION=2.2.0
DOCKER_IMAGE_VERSION=2.2.0
CA_LOCAL_VERSION=1.4.8
CA_DOCKER_IMAGE_VERSION=1.4.8
Generate certificates using Fabric CA's
Creating network "net_test" with the default driver
Creating ca_orderer ... done
Creating ca_org2    ... done
Creating ca_org1    ... done
Create Org1 Identities
Enroll the CA admin
+ fabric-ca-client enroll -u https://admin:adminpw@localhost:7054 --caname ca-org1 --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:44 [INFO] Created a default configuration file at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/fabric-ca-client-config.yaml
2020/10/10 14:37:44 [INFO] TLS Enabled
2020/10/10 14:37:44 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:44 [INFO] encoded CSR
2020/10/10 14:37:45 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/msp/signcerts/cert.pem
2020/10/10 14:37:45 [INFO] Stored root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/msp/cacerts/localhost-7054-ca-org1.pem
2020/10/10 14:37:45 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/msp/IssuerPublicKey
2020/10/10 14:37:45 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/msp/IssuerRevocationPublicKey
Register peer0
+ fabric-ca-client register --caname ca-org1 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] Configuration file location: /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/fabric-ca-client-config.yaml
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] TLS Enabled
Password: peer0pw
Register user
+ fabric-ca-client register --caname ca-org1 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] Configuration file location: /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/fabric-ca-client-config.yaml
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] TLS Enabled
Password: user1pw
Register the org admin
+ fabric-ca-client register --caname ca-org1 --id.name org1admin --id.secret org1adminpw --id.type admin --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] Configuration file location: /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/fabric-ca-client-config.yaml
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] TLS Enabled
Password: org1adminpw
Generate the peer0 msp
+ fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp --csr.hosts peer0.org1.example.com --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:45 [INFO] encoded CSR
2020/10/10 14:37:45 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/signcerts/cert.pem
2020/10/10 14:37:45 [INFO] Stored root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/cacerts/localhost-7054-ca-org1.pem
2020/10/10 14:37:45 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/IssuerPublicKey
2020/10/10 14:37:45 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/IssuerRevocationPublicKey
Generate the peer0-tls certificates
+ fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls --enrollment.profile tls --csr.hosts peer0.org1.example.com --csr.hosts localhost --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:45 [INFO] encoded CSR
2020/10/10 14:37:45 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/signcerts/cert.pem
2020/10/10 14:37:45 [INFO] Stored TLS root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/tlscacerts/tls-localhost-7054-ca-org1.pem
2020/10/10 14:37:45 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/IssuerPublicKey
2020/10/10 14:37:45 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/IssuerRevocationPublicKey
Generate the user msp
+ fabric-ca-client enroll -u https://user1:user1pw@localhost:7054 --caname ca-org1 -M /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:45 [INFO] encoded CSR
2020/10/10 14:37:45 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/cert.pem
2020/10/10 14:37:45 [INFO] Stored root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/cacerts/localhost-7054-ca-org1.pem
2020/10/10 14:37:45 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/IssuerPublicKey
2020/10/10 14:37:45 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/IssuerRevocationPublicKey
Generate the org admin msp
+ fabric-ca-client enroll -u https://org1admin:org1adminpw@localhost:7054 --caname ca-org1 -M /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org1/tls-cert.pem
2020/10/10 14:37:45 [INFO] TLS Enabled
2020/10/10 14:37:45 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:45 [INFO] encoded CSR
2020/10/10 14:37:46 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/cert.pem
2020/10/10 14:37:46 [INFO] Stored root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/cacerts/localhost-7054-ca-org1.pem
2020/10/10 14:37:46 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/IssuerPublicKey
2020/10/10 14:37:46 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/IssuerRevocationPublicKey
Create Org2 Identities
Enroll the CA admin
+ fabric-ca-client enroll -u https://admin:adminpw@localhost:8054 --caname ca-org2 --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabric-ca/org2/tls-cert.pem
2020/10/10 14:37:46 [INFO] Created a default configuration file at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/fabric-ca-client-config.yaml
2020/10/10 14:37:46 [INFO] TLS Enabled
2020/10/10 14:37:46 [INFO] generating key: &{
   A:ecdsa S:256}
2020/10/10 14:37:46 [INFO] encoded CSR
2020/10/10 14:37:46 [INFO] Stored client certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/msp/signcerts/cert.pem
2020/10/10 14:37:46 [INFO] Stored root CA certificate at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/msp/cacerts/localhost-8054-ca-org2.pem
2020/10/10 14:37:46 [INFO] Stored Issuer public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/msp/IssuerPublicKey
2020/10/10 14:37:46 [INFO] Stored Issuer revocation public key at /Users/zhaozijun/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/msp/IssuerRevocationPublicKey
Register peer0
+ fabric-ca-client register --caname ca-org2 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles /Users/zhaozijun/fabric-samples/test-network/organizations/fabri
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值