六、编写第一个应用【外部nodejs调用】
一、
参考地址:https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html
根据前几节的配置
1、下载代码
git clone https://github.com/hyperledger/fabric-samples.git
cd 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 location
Set wallet path, and associate user PeerAdmin with application
Check user is enrolled, and set a query URL in the network
Make query
Assigning transaction_id: f2f45cb045d6290d199e1b2d4eb3b60b1e9cafeff8d09e2b7683dd8578492be7
returned from query
Query result count = 1
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"}}]

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》链码:/chaincode/fabcar/目录下fabcar.go
此文件匹配上文的链码ID,包含了如下方法:initLedger, queryCar,queryAllCars, createCar 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 location
Set wallet path, and associate user PeerAdmin with application
Check user is enrolled, and set a query URL in the network
Make query
Assigning transaction_id: ca88dc3b60f4df009a709f2f5ee5ad3b54f43d03a7e0b931042e2797f70c795d
returned from query
Query result count = 1
Response 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.js
vim query2.js
将query2.js中查询条件参数,变为CAR10即可
Response is {"colour":"Red","make":"Chevy","model":"Volt","owner":"Nick"}
ok,可以继续调试其他方法。
六、编写第一个应用【外部nodejs调用】的更多相关文章
- 006-基于hyperledger fabric1.4( 官方文档)编写第一个应用【外部nodejs调用】
一.概述 官方原文地址 Writing Your First Application如果对fabric网络的基本运行机制不熟悉的话,请看这里. 注意:本教程是对fabric应用以及如何使用智能合约的简 ...
- nodejs调用delphi编写的dll
公司的业务需要,nodejs要读取文件版本号. 同事要求我用delphi编写dll,以供nodejs调用,结果通过json返回. delphi代码如下: function GetFileInfo(AP ...
- C#基础知识-编写第一个程序(二)
通过上一篇数据类型已经介绍了C#中最基本的15种预定义数据类型,了解每一种类型代表的数据以及每种类型的取值范围,这是很重要也是最基本.下面我们通过实例来了解每个类型如何去使用.编写C#程序时我们需要用 ...
- 内核第三讲,进入ring0,以及编写第一个内核驱动程序.
内核第三讲,进入ring0,以及编写第一个内核驱动程序. PS: 请下配置双机调试,下方有可能用到.如果不配置,则你可以不用调试, 博客连接: http://www.cnblogs.com/iBina ...
- 深入了解以太坊虚拟机第4部分——ABI编码外部方法调用的方式
在本系列的上一篇文章中我们看到了Solidity是如何在EVM存储器中表示复杂数据结构的.但是如果无法交互,数据就是没有意义的.智能合约就是数据和外界的中间体. 在这篇文章中我们将会看到Solidit ...
- 用nodejs调用webservice
用nodejs调用webservice,是用soap包实现的. 步骤如下: 第一步:安装soap包 npm install soap 第二部:调用webservice var soap = requ ...
- Xamarin iOS编写第一个应用程序创建工程
Xamarin iOS编写第一个应用程序创建工程 在Xcode以及Xamarin安装好后,就可以在Xamarin Studio中编写程序了.本节将主要讲解在Xamarin Studio中如何进行工程的 ...
- 简单上手nodejs调用c++(c++和js的混合编程)
因为项目的原因,最近经常使用node.js搭RESTful接口. 性能还是很不错啦,感觉比Spring Boot之类的要快.而且在不错的性能之外,只要程序结构组织好,别让太多的回调把程序结构搞乱,整体 ...
- django之创建第3个项目:编写第一个模板文件
1.django结构 2.在站点blog下创建templates文件夹,专门用于存放模板文件 3.在templates文件夹下创建index.html文件 #index.html <!DOCTY ...
随机推荐
- shell脚本6--循环,比较
for循环 for var in list; do commands;#使用变量$var done example: for i in {a..z}; do actions; done; 后者 for ...
- 游戏UI规范
在满足效果的前提下,尽量做到UI资源做到复用和最小化 1. 背景1和背景2分开切,可以组合成各种不同的面包背景图 2. 背景1和背景2在没有花纹的情况下,中间纯色的部分切4个像素做就公共个缩放就可 ...
- Cocos Creator存储和读取用户数据--官方文档
存储数据 cc.sys.localStorage.setItem(key, value) 上面的方法需要两个参数,用来索引的字符串键值 key,和要保存的字符串数据 value. 假如我们要保存玩家最 ...
- px与rem的换算
在线转化工具: http://www.ofmonkey.com/front/rem rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个参考值,这个参考值设置为多少, ...
- Spring-context 实现Hello World
Spring-context 实现Hello World 本文作为Spring入门笔记,用Spring-context实现控制台的hello world Spring简介 Spring是一个开放源代码 ...
- static之静态初始化块
static之静态初始化块 所有的静态初始化块都优先执行,其次才是非静态的初始化块和构造函数,它们的执行顺序是: 父类的静态初始化块 子类的静态初始化块 父类的初始化块 父类的 ...
- 牛客网测试题--小a和黄金街道
小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们拿走的太多,于是要求小a和小b通过做一个游戏来决定最后得到的黄金的数量.游戏规则是这样的:假设道路长度为米(左端点 ...
- uploadify Cookie 验证登入上传问题
上传文件时必须验证是否已登入. 当用FormsAuthentication做登入,使用FormsAuthentication.FormsCookieName进行验证是否已登入即可. <scrip ...
- mysql -- this is incompatible with sql_mode=only_full_group_by
select @@GLOBAL.sql_mode; set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER ...
- 分享一个整理Mac储存空间的软件,有效清理xcode的垃圾
苹果的电脑还是比windows的要好,我这台用了3年时间,作为编程工作天天用,现在愣是一点儿也没有卡顿,如果换作是windows的电脑,我敢保证肯定是卡成狗,相信做编程的你肯定少补了一台mac. ma ...