fabric-hello world

1、查询[注意配置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"}}]
复制代码

 2、分析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,包含了如下方法:initLedgerqueryCar,queryAllCarscreateCar and changeCarOwner

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

    resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)

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

查看所有方法

3、测试

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"}
复制代码

4、更新账本数据

使用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,可以继续调试其他方法。



转载自:http://www.cnblogs.com/bjlhx/p/7195894.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值