本教程的所有内容均在fabcar目录中进行
cd fabric-samples/fabcar
本博客对应v2.0.0版本
注意:在新版本fabric-sample中fabcar文件夹下不存在enrollAdmin.js invoke.js query.js registerUser.js 等几个js文件,只需进入javascript或javascript-low-level文件夹中将js文件复制到fabcar即可
此步意义在删除之前所调用过的网络和容器
删除所有容器
docker rm -f $(docker ps -aq)
清除网络
Press 'y' when prompted by the command
docker network prune
若以前运行过本教程,删除chaincode镜像
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
3.安装npm
运行npm install
npm install
意义为根据package.json文件中的信息安装所需的依赖
注意:当使用代理上网时,为npm设置代理可以加速安装
$ npm config set proxy http://server:port
$ npm config set https-proxy http://server:port
4.启动网络
使用脚本startFabric.sh来启动你的网络,这会使你的多个 Fabric容器运转起来
sudo ./startFabric.sh
注意:当运行此条命令时,需要first-network文件夹的路径中在fabcar的 …/(前一级文件夹)
5.登记管理用户(Admin)当启动这个网络时,管理用户admin就被注册了。此时发送一个enroll调用到CA服务,来获取用户的等级证书。SDK以及应用程序需要这个证书来为admin生成用户对象,这样才能调用这个管理用户admin来生成新的用户。
这个程序会生成证书和密钥材料到新的文件夹hfc-key-store。
sudo node enrollAdmin.js
注意:当运行此条命令时,需要以下路径中的文件
/home/username/go/src/github.com/hyperledger/fabric/scripts/first-network/crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem
,但这要求first-network的路径与前一个命令中路径不同,因此需要在前一指令运行后将first-network文件夹移至scripts文件夹(上一指令中的前一级
我们现在就可以使用我们新生成的admin eCert证书来和CA服务通信,以注册和登记新的用户了。用户user1就是我们用来查询和更新账本的识别号。这里需要注意的一点,只有管理者标记(admin identity)才能发起新用户的注册和登记调用。为user1发送注册和登记调用,
sudo node registerUser.js
现在就可以与账本进行交互了
7.查询账本查询账本直接调用query.js
node query.js
查询结果为
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"}}]
以下是query.js中已经为我们写好的调用函数
// queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],
// queryAllCars chaincode function - requires no arguments , ex: args: [''],
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryAllCars',
args: ['']
};
可以在上方的注释中看到,我们有两个写好的查询函数可以调用:queryCar、queryAllCars。此处我们调用了queryAllCars查询得到所有汽车的数据
当然我们也可以调用queryCar函数,直接修改query.js部分代码
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR4']
};
重新运行query.js
查询结果如下
{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}
8.更新账本
更新账本的代码保存在自带的invoke.js中,js中代码如下所示
// createCar chaincode function - requires 5 args, ex: args: ['CAR12', 'Honda','Accord', 'Black', 'Tom'],
// changeCarOwner chaincode function - requires 2 args , ex: args: ['CAR10', 'Barry'],
// must send the proposal to endorsing peers
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: '',
args: [''],
chainId: 'mychannel',
txId: tx_id
};
可以看到我们可以调用createCar、changeCarOwner两个函数对账本进行更新
首先我们修改invoke.js调用createCar函数创建一个新的汽车,代码如下
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'createCar',
args: ['CAR10', 'Honda','Accord', 'Black', 'Tom'],
chainId: 'mychannel',
txId: tx_id
};
保存后运行
node invoke.js
成功后,我们通过修改query.j来进行查询
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR10']
};
运行query.js,即可得到查询结果
若想将车的主人修改为Andy,可以继续修改invoke.js如下
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'changeCarOwner',
args: ['CAR10', 'Andy'],
chainId: 'mychannel',
txId: tx_id
};
再次运行invoke.js、query.js可以看到车的主人已经改变
9.编写你的第一个应用已完成