Hyperledger Fabric Samples 编写你的第一个应用

版权声明:博客中的文章版权归博主所有,未经授权,禁止转载,转载请注明出处

进入 fabcar

$ cd fabric-samples/fabcar

准备环境

杀掉活跃的容器

$ docker rm -f $(docker ps -aq)

清理缓存的网络

$ docker network prune

删除fabcar智能合约的底层链码图像,如果是第一次运行这个项目可以不执行

$ docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba

安装客户端并启动网络

$ npm install

$ ./startFabric.sh node

注册管理员用户

node enrollAdmin.js

注册user1 用户

node registerUser.js

查询账目

node query.js

运行结果:

Store path:/Users/fujinliang/fabric/fabric-samples/fabcar/hfc-key-store
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"}}]

查询某一个的账目

修改 query.js , 可以查询key为CAR4 的记录

const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR4']
};

运行 node query.js

Response is {"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}

更新账目

修改 invoke.js, 添加一条数据

var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
chainId: 'mychannel',
txId: tx_id
};

执行 node invoke.js,可以添加一条数据进入账目

修改 query.js

const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR10']
};

运行 node query.js ,

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

修改 invoke.js, 修改CAR10的拥有者为 Dave

var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'changeCarOwner',
args: ['CAR10', 'Dave'],
chainId: 'mychannel',
txId: tx_id
};

执行 node invoke.js, 再运行 node query.js , 运行结果:

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

关注公众号
关注公众号

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值