网络连接世界 2019-06-27
翻译自:http://hyperledger-fabric.rea...
我们需要实现如下三个步骤
./byfn.sh -m down
进入到fabric-sample/fabcar目录下:这是一个关于car(汽车)交易的app
cd fabric-samples/fabcar && ls 显示结果 enrollAdmin.js invoke.js package.json query.js registerUser.js startFabric.sh
docker rm -f $(docker ps -aq)
docker network prune
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
首先要确保fabric-ca-client和fabric-client是否已经准备好了,这两个module是Node.js访问超级账本网络的SDK
npm install fabric-client
npm install fabric-ca-client
./startFabric.sh
#运行golang编写的chaincode
./startFabric.sh node
#运行node.js编写的chaincodenode enrollAdmin.js
命令执行成功会创建一个hfc-key-store目录,目录中存放admin的身份标示,一对公私密钥。
node registerUser.js
执行查询命令查询当前汽车信息
node query.js
[{"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后再次运行脚本
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"}
我们通过查看fabric-sample/chaincode/fabcar 查看智能合约代码,我们的app就是调用了智能合约中支持的函数,通过rpc方式。支持函数如下:initLedger用于创建初始化的10个CAR信息
initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
更新账本信息在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
创建后查询所有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"}}]
- 先通过客户端提交交易 - 通过背书节点检查后,客户端得到背书节点响应 - 客户端将背书节点响应结果提交给order排序节点 - order节点创建区块后,广播给所有的peer节点更新账本