博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
六、编写第一个应用【外部nodejs调用】
阅读量:6798 次
发布时间:2019-06-26

本文共 3908 字,大约阅读时间需要 13 分钟。

一、

参考地址:https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html

根据前几节的配置

1、下载代码

git clone https://github.com/hyperledger/fabric-samples.gitcd fabric-samples/fabcar

2、在fabcar下会有如下文件

chaincode    invoke.js       network         package.json    query.js        startFabric.sh

新版本的示例,chaincode在根目录下

3、开启网络配置[注意docker版本17.06,在1.12.6版本没有-e命令]

./startFabric.sh

4、查询一个账本

 

安装node

yum install gcc-c++ npm install

查询[注意配置query.js中的ip地址]

node query.js

展示如下数据

Create a client and set the wallet locationSet wallet path, and associate user  PeerAdmin  with applicationCheck user is enrolled, and set a query URL in the networkMake queryAssigning transaction_id:  f2f45cb045d6290d199e1b2d4eb3b60b1e9cafeff8d09e2b7683dd8578492be7returned from queryQuery result count =  1Response 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"}}]

 5、分析query.js

1》初始化参数,包含了用户ID,信道,链码,网络连接入口

var options = {    wallet_path: path.join(__dirname, './creds'),    user_id: 'PeerAdmin',    channel_id: 'mychannel',    chaincode_id: 'fabcar',    network_url: 'grpc://localhost:7051',};

2》查询代码

var transaction_id = client.newTransactionID();    // queryCar - requires 1 argument, ex: args: ['CAR4'],    // queryAllCars - requires no arguments , ex: args: [''],    const request = {        chaincodeId: options.chaincode_id,        txId: transaction_id,        fcn: 'queryAllCars',        args: ['']    };    return channel.queryByChaincode(request);

这里设置了链码ID,交易ID,以及调用的链码的方法fcn,方法参数args等

3》链码://fabcar/目录下fabcar.go

此文件匹配上文的链码ID,包含了如下方法:initLedgerqueryCar,queryAllCarscreateCar and changeCarOwner

func (s *SmartContract) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {    startKey := "CAR0"    endKey := "CAR999"    resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)

此处就是查询范围内的数据。

查看所有方法

6、测试

cp query.js query1.js vim query1.js

修改内部访问链码方法

const request = {        chaincodeId: options.chaincode_id,        txId: transaction_id,        fcn: 'queryCar',        args: ['CAR4']    };

执行:node query1.js

Create a client and set the wallet locationSet wallet path, and associate user  PeerAdmin  with applicationCheck user is enrolled, and set a query URL in the networkMake queryAssigning transaction_id:  ca88dc3b60f4df009a709f2f5ee5ad3b54f43d03a7e0b931042e2797f70c795dreturned from queryQuery result count =  1Response is  {"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}

7、更新账本数据

使用fabcar目录下的invoke.js

修改,中的 fcn,以及args等参数

var request = {    targets: targets,    chaincodeId: options.chaincode_id,    fcn: '',    args: [''],    chainId: options.channel_id,    txId: tx_id

如下

var request = {    targets: targets,    chaincodeId: options.chaincode_id,    fcn: 'createCar',    args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],    chainId: options.channel_id,    txId: tx_id

执行命令

node invoke.js

成功后会有

The transaction has been committed on peer localhost:7053

执行

cp query1.js query2.jsvim query2.js

将query2.js中查询条件参数,变为CAR10即可

Response is  {"colour":"Red","make":"Chevy","model":"Volt","owner":"Nick"}

ok,可以继续调试其他方法。

转载地址:http://skrgo.baihongyu.com/

你可能感兴趣的文章
5G概念炒的火热,公共WiFi建设却为何不见进展?
查看>>
c/c++中指针学习的两个绝好例子
查看>>
天云郭宏:谈传统IDC困境 指点云建设
查看>>
云计算的最大问题是安全还是隐私?
查看>>
PDMS call Operating System Command
查看>>
如何取消codeblocks对msvcr100.dll的依赖?
查看>>
Jack Ma 你当初UT了没?
查看>>
IBM联手传智播客 落地大数据应用人才培养计划
查看>>
爱尔兰地方议会再次否决一光伏发电项目
查看>>
Appinions:IDC对物联网影响力位居前三
查看>>
《网络空间欺骗:构筑欺骗防御的科学基石》一2.4 集成化网络空间欺骗与计算机防御框架...
查看>>
渤海银行双活数据中心建设值得中小银行借鉴
查看>>
赛普拉斯推出业内首款专为IoT设计的微控制器架构PSoC 6
查看>>
中国人工智能学会通讯——互联网搜索技术的前沿探索 2 文本内容建模
查看>>
如何将数据可视化技术应用于广告投放?
查看>>
有工程师思维吗?什么是工程师思维?
查看>>
长安铃木与滴滴商业战略合作:基于试驾用体验拉近品牌和消费者距离
查看>>
英特尔支持下的Open HPC堆栈将于第四季度发布
查看>>
逻辑回归那些事—使用牛顿法解决实际问题
查看>>
复制云优势 NetScaler增强移动应用体验
查看>>