HyperLedger Fabcar 学习笔记(基于超级账本编写第一个应用)

ulysses · · 3688 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

HyperLedger Fabcar 学习笔记(基于超级账本编写第一个应用)

翻译自:http://hyperledger-fabric.rea...

编写第一个应用程序

我们需要实现如下三个步骤

  1. 首先设置一个开发环境
  2. 学习一些简单的我们app将会用到的智能合约的参数
  3. 开发一个app能够更新查询一个账本

设置开发环境

  1. 首先确保必备的fabric image等已经安装成功,如果没有安装,请参考(https://hyperledger-fabric.re...
  2. 下载fabric-samples,并且安装必备的运行工具。(参考地址:https://hyperledger-fabric.re...
  3. 上述步骤如果不想看英文的同学,可以参考(搭建第一个超级账本网络的方法)https://segmentfault.com/a/11...
  4. 如果按照建立超级账本网络的教程运行,可能会启动着一个网络,会对接下来的流程有影响,需要关闭网络。

    ./byfn.sh -m down

  5. 进入到fabric-sample/fabcar目录下:这是一个关于car(汽车)交易的app

    cd fabric-samples/fabcar  && ls
    显示结果
    enrollAdmin.js     invoke.js       package.json    query.js        registerUser.js startFabric.sh
  6. 杀死当前运行的所有docker 镜像服务

    docker rm -f $(docker ps -aq)

  7. 清除所有缓存网络
    docker network prune
  8. 如果已经运行过此教程,还需要执行如下操作,删除chaincode镜像,如果第一次运行是不会有的
    docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba

安装客户端并登录网络

  1. 首先要确保fabric-ca-client和fabric-client是否已经准备好了,这两个module是Node.js访问超级账本网络的SDK

    npm install fabric-client
    npm install fabric-ca-client

    • 此时会在fabcar目录下显示node_module目录以及一个package.json的文件。此部分如果不理解可以学习node.js相关知识。
  2. 通过命令创建一个超级账本网络

    ./startFabric.sh #运行golang编写的chaincode

  • 说明:此脚本会创建创建一个超级账本网络,包括ca-server peer order等节点,安装并实例化了chaincode。
    ./startFabric.sh node#运行node.js编写的chaincode

App如何使用网络

创建Admin用户

  1. 首先应用的admin用户应该向ca-server发送一个证书登记请求,接受一个对于这个user的登记证书(eCert),后续我们会根据使用这个admin注册和认证一个新的user。命令如下:

    node enrollAdmin.js

    命令执行成功会创建一个hfc-key-store目录,目录中存放admin的身份标示,一对公私密钥。

创建普通User用户

  1. 创建一个普通用户user1,这个用户用来查询和更新账本。admin用户身份用来创建user1用户。执行如下命令:

    node registerUser.js

查询账本信息

  1. 执行查询命令查询当前汽车信息

    node query.js

    • 会显示全部的CAR信息CAR0-CAR9
    [{"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"}}]
  2. 我们可以尝试修改query.js后再次运行脚本

    const request = {
    //targets : --- letting this default to the peers assigned to the channel
        chaincodeId: 'fabcar',
        fcn: 'queryCar',    //此处修改为查询单个CAR信息
        args: ['CAR5']        //通过修改参数查询指定的CAR信息
    };
    显示结果如下
    Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-store
    Successfully loaded user1 from persistence
    Query has completed, checking results
    Response is  {"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}
  3. 我们通过查看fabric-sample/chaincode/fabcar 查看智能合约代码,我们的app就是调用了智能合约中支持的函数,通过rpc方式。支持函数如下:initLedger用于创建初始化的10个CAR信息

    initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
  4. APP执行流程图

更新账本信息

  1. 更新账本信息在APP侧和查询相似,就是通过智能合约提供的接口更新数据。当前提供的功能包括创建CAR,以及修改CAR的owner属性。我们可以通过修改invoke.js代码实现对不同接口的调用。

    var request = {
            //targets: let default to the peer assigned to the client
            chaincodeId: 'fabcar',
            fcn: 'createCar',
            args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'],
            chainId: 'mychannel',
            txId: tx_id
        };
    执行结果:
    lydeiMac:fabcar ly$ node invoke.js 
    Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-store
    Successfully loaded user1 from persistence
    Assigning transaction_id:  a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2e
    Transaction proposal was good
    Successfully sent Proposal and received ProposalResponse: Status - 200, message - "OK"
    The transaction has been committed on peer localhost:7053
    Send transaction promise and event listener promise have completed
    Successfully sent transaction to the orderer.
    Successfully committed the change to the ledger by the peer
  2. 创建后查询所有CAR信息,可以看到增加了CAR12信息

    [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR12", "Record":{"colour":"Black","make":"Honda","model":"Accord","owner":"Tom"}},{"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"}}]
  3. 合约执行流程如图:

- 先通过客户端提交交易
- 通过背书节点检查后,客户端得到背书节点响应
- 客户端将背书节点响应结果提交给order排序节点
- order节点创建区块后,广播给所有的peer节点更新账本

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3688 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传